//-- -------------------------------------------
//-- ADD ONLOAD EVENT
//-- -------------------------------------------
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}




//-- -------------------------------------------
//-- DOM VARIABLES
//-- -------------------------------------------
var dom = (document.getElementById)?true:false;
var IE = document.all?true:false




//-- -------------------------------------------
//-- FUNCTION TO CLEAR THE SEARCH BOX TEXT
//-- -------------------------------------------
function clearField(Field,fieldValue) {
	if (Field.value	== fieldValue) {
		Field.value = '';
	}
}


//-- -------------------------------------------
//-- FUNCTION TO RESET THE SEARCH BOX TEXT
//-- -------------------------------------------
function resetField(Field,fieldValue) {
	if (Field.value	== '') {
		Field.value = fieldValue;
	}
}




//-- -------------------------------------------
//-- GENERIC ACCESSIBLE POPUP SCRIPT
//-- -------------------------------------------
var _POPUP_FEATURES = 'width=790,height=525,scrollbars=no';

function raw_popup(url, target, features) {
    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target)) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}

function link_popup(src, features) {
    return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}


function isUndefined(v) {
    var undef;
    return v===undef;
}



//-- -------------------------------------------
//-- FUNCTION TO PRINT PAGE
//-- -------------------------------------------
function printpage() {
	if (window.print) { window.print(); }
	else {
		alert ("To print this page please select the 'Print' option from your browser's 'File' menu.")
	}
	return;
}




//-- -------------------------------------------
//-- AUTOMATICALLY UPDATE THE END DATE
//-- -------------------------------------------
function syncDate(sourceObj,target) {
	var sourceValue = sourceObj.options[sourceObj.selectedIndex].value;
	var targetObj = document.getElementById(target);
	targetObj.value = sourceValue;
}









//-- -------------------------------------------
//-- AJAX FUNCTIONS FOR EVENT SEARCH
//-- -------------------------------------------
var originalLocationHTML;
var originalTypeHTML;
var firstLocationUpdate = true;
var firstTypeUpdate = true;





function activateSelectBoxes() {

	if(typeof http != "object") return;
	if (document.getElementById("eventLocationSelect")) {
		document.getElementById("e_type_id").onchange = getLocationOptions;
		document.getElementById("e_loc_id").onchange = getTypeOptions;
	}
}





getLocationOptions = function() {

	removeErrorMessage();
	var selectedTypeID = this.options[this.selectedIndex].value;

	if (firstLocationUpdate == true) {
		originalLocationHTML = document.getElementById("eventLocationSelect").innerHTML;
		firstLocationUpdate = false;
	}

	document.getElementById("eventLocationSelect").innerHTML = "<label for=\"e_loc_id\" class=\"b\">Event location:</label><p class=\"loading\">Loading data</p>";

	try {

		// Build the URL to connect to
		var AJAXurl = "getEventOptions.asp"
		AJAXurl += "?selectedType=e_type";
		AJAXurl += "&selectedID=" + selectedTypeID;

		// Open a connection to the server
		http.open("GET", AJAXurl, true);
		http.onreadystatechange = updateLocationSelect;
		http.send(null);

	}
	catch (e) {
		setTimeout("getLocationOptions();", 10);
	}
}





function updateLocationSelect() {

	if (http.readyState == 4) {

		if(http.status != 200) {
			alert(http.status);
		}

		try {
			if (http.responseText == "false") {
				document.getElementById("eventLocationSelect").innerHTML = originalLocationHTML;
				document.getElementById("e_loc_id").onchange = getTypeOptions;
			}
			else {
				document.getElementById("eventLocationSelect").innerHTML = http.responseText;
				document.getElementById("e_loc_id").onchange = "";
			}
		}
		catch (e) {
			setTimeout("updateLocationSelect();", 10);
		}
	}
}





getTypeOptions = function() {

	removeErrorMessage();
	var selectedLocID = this.options[this.selectedIndex].value;

	if (firstTypeUpdate == true) {
		originalTypeHTML = document.getElementById("eventTypeSelect").innerHTML;
		firstTypeUpdate = false;
	}

	document.getElementById("eventTypeSelect").innerHTML = "<label for=\"e_type_id\" class=\"b\">Event type:</label><p class=\"loading\">Loading data</p>";

	try {
		// Build the URL to connect to
		var AJAXurl = "getEventOptions.asp"
		AJAXurl += "?selectedType=e_loc";
		AJAXurl += "&selectedID=" + selectedLocID;

		// Open a connection to the server
		http.open("GET", AJAXurl, true);
		http.onreadystatechange = updateTypeSelect;
		http.send(null);

	}
	catch (e) {
		setTimeout("getTypeOptions();", 10);
	}
}





function updateTypeSelect() {

	if (http.readyState == 4) {

		if(http.status != 200) {
			alert(http.status);
		}

		try {
			if (http.responseText == "false") {
				writeErrorMessage("loc");
				document.getElementById("eventTypeSelect").innerHTML = originalTypeHTML;
				document.getElementById("e_type_id").onchange = getTypeOptions;
			}
			else {
				document.getElementById("eventTypeSelect").innerHTML = http.responseText;
				document.getElementById("e_type_id").onchange = "";
			}
		}
		catch (e) {
			setTimeout("updateTypeSelect();", 10);
		}
	}
}





function writeErrorMessage(eType) {

	var errorP = document.getElementById("smFormError");
	errorP.className = "error"
	if (eType == "loc") {
		errorP.innerHTML = "There are currently no upcoming events in the region";
	}
	if (eType == "type") {
		errorP.innerHTML = "There are currently no upcoming of this type";
	}
}





function removeErrorMessage() {
	var errorP = document.getElementById("smFormError");
	errorP.className = "hide"
	errorP.innerHTML="";
}




addLoadEvent(activateSelectBoxes);





