// JavaScript functions
function adduploadrow(id){
	// do a quick check
	if (document.getElementById(id).getElementsByTagName("TR").length > 25){
		alert("Sorry, you need to upload before adding any more");
		return false;
	}
	
	// add the row
	var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
	var row = document.createElement("TR");
	var td1 = document.createElement("TD");
	
	var el = document.createElement('input');
	el.type = 'file';
	el.name = 'FILES[]';
	el.size = 40;
	td1.appendChild(el);
	
	row.appendChild(td1);
	tbody.appendChild(row);
}

function createobject(){
	var request_object;
	var browser = navigator.appName;
	
	if (browser == "Microsoft Internet Explorer"){
		request_object = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		request_object = new XMLHttpRequest();
	}
	
	return request_object;
}

function innerText(node){
	 return (node.textContent || node.innerText || node.text);
}

function optionsForVenue(){
	// populate nights list
	var e = document.getElementById("venueid").selectedIndex;
	var selectedVenue = document.getElementById("venueid").options[e].value;
	var requestUrl = "../backend/xml-venue.php?id=" + selectedVenue;
	
	req = createobject();
    req.onreadystatechange = optionsForVenueDone
    req.open("GET", requestUrl, true);
	req.send(null);
}

function optionsForVenueDone(){
	if(req.readyState == 4){
			optionsForVenuePopulate(req.responseXML);
	}
}

function optionsForVenuePopulate(node, target){
	var nightList = document.getElementById("nightid");
	var eventList = document.getElementById("eventid");
	
	// clear the lists
	for (var count = nightList.options.length-1; count > 0; count--){
		nightList.options[count] = null;
	}
	
	for (var count = eventList.options.length-1; count > 0; count--){
		eventList.options[count] = null;
	}
	
	// add new content, if exists
	var nl = node.getElementsByTagName("night");
	for( var i = 0; i < nl.length; i++ ) {
		var nli = nl.item( i );
		var id = nli.getAttribute( 'id' );
		var name = nli.firstChild.nodeValue;
		
		var elOption = document.createElement( 'option' );
		elOption.value = id;
		elOption.innerHTML = name;
		nightList.appendChild( elOption );
	}
	
	var nl = node.getElementsByTagName("event");
	for( var i = 0; i < nl.length; i++ ) {
		var nli = nl.item( i );
		var id = nli.getAttribute( 'id' );
		var name = nli.firstChild.nodeValue;
		
		var elOption = document.createElement( 'option' );
		elOption.value = id;
		elOption.innerHTML = name;
		eventList.appendChild( elOption );
	}
}