//AJAX stuff
//Usage:
//	var ajax = new ajaxService;
//
//	var ajaxResult = ajax.ajaxSubmit("ajax/submit_program.php?program=" + programID,"");

function ajaxService() {
};

ajaxService.prototype.initialize = function() {
	try {
		// Mozilla / Safari / etc.
		this._xh = new XMLHttpRequest();
	} catch (e) {
		// Explorer
		var ieDefinitions = new Array(
		'MSXML2.XMLHTTP.5.0',
		'MSXML2.XMLHTTP.4.0',
		'MSXML2.XMLHTTP.3.0',
		'MSXML2.XMLHTTP',
		'Microsoft.XMLHTTP'
		);
		var success = false;
		for (var i=0;i < ieDefinitions.length && !success; i++) {
			try {
				this._xh = new ActiveXObject(ieDefinitions[i]);
				success = true;
			} catch (e) {
				//exception handling here
			}
		}
		if ( !success ) {
			//exception handling here
			return false;
		}
		return true;
	}
}

ajaxService.prototype.progress = function() {
	actualState = this._xh.readyState;
	return (actualState && (actualState < 4));
}

ajaxService.prototype.process = function() {
	if (this._xh.readyState == 4 && this._xh.status == 200) {
		this.processComplete = true;
	}
}

ajaxService.prototype.ajaxSubmit = function(url,data) {
	if (!this._xh) {
		this.initialize();
	}
	if (!this.progress()) {
		this._xh.open("GET",url,false);
		this._xh.send(data);
		if (this._xh.readyState == 4 && this._xh.status == 200) {
			return this._xh.responseXML;
		}
	}
	return null;
}

//select box - loop trhough and select the correct one, handle 'other' case
function selectPick(elemID, value) {
	selectBox = document.getElementById(elemID);
	
	//loop through options and try to match value
	var i;
	for(i = 0; i < selectBox.options.length; i++) {
		if(selectBox.options[i].value == value) {
			selectBox.selectedIndex = i;
			return;	
		}
	}
	
	other = document.getElementById(elemID + 'Other');
	
	if(other) {
		//insert value and display other field, selecting 'other' box
		other.value = value;
		other.style.visibility='visible';
		
		selectBox.selectedIndex = i-1;
	}
}

//radio button - loop trhough and select the correct one, handle 'other' case
function selectRadio(formElem,elemName,value) {
	
	//loop through options and try to match value
	var i;
	for(i = 0; i < formElem.elements[elemName].length; i++) {
		if(formElem.elements[elemName][i].value == value) {
			formElem.elements[elemName][i].checked = true;
			return;	
		}
	}
	
	other = document.getElementById(elemName + 'Other');
	
	if(other) {
		//insert value and display other field, selecting 'other' box
		other.value = value;
		other.style.visibility='visible';
		other.style.display='inline';
		
		formElem.elements[elemName][i-1].checked = true;
	}
}

//selected registration methods
function selectChange(elem, disp) {
	if(elem.options[elem.selectedIndex].value == "Other") {
		//show field
		target = document.getElementById(elem.id + 'Other');
		target.style.visibility='visible';
		if(disp != null)
			target.style.display='inline';
	}
	else {
		//hide field
		target = document.getElementById(elem.id + 'Other');
		target.style.visibility='hidden';		
		if(disp != null)
			target.style.display='none';
	}
}

function radioChange(elem, disp) {
	if(elem.value == "Other") {
		//show field
		target = document.getElementById(elem.id + 'Other');
		target.style.visibility='visible';
		if(disp != null)
			target.style.display='inline';
	}
	else {
		//hide field
		target = document.getElementById(elem.id + 'Other');
		target.style.visibility='hidden';		
		if(disp != null)
			target.style.display='none';
	}
}

function checkChange(elem, disp) {
	if(elem.checked) {
		//show field
		target = document.getElementById(elem.id + 'Other');
		target.style.visibility='visible';
		if(disp != null)
			target.style.display='inline';
	}
	else {
		//hide field
		target = document.getElementById(elem.id + 'Other');
		target.style.visibility='hidden';		
		if(disp != null)
			target.style.display='none';
	}
}

function appendinputTypeClasses() { 
 if (!document.getElementsByTagName ) 
  return; 
 var inputs = document.getElementsByTagName('input'); 
 var inputLen = inputs.length; 
 for ( i=0;i<inputLen;i++ ) { 
  if ( inputs[i].getAttribute('type') ) 
   inputs[i].className += ' '+inputs[i].getAttribute('type'); 
 } 
} 	

// Splintered striper 1.3
// reworking of Zebra Tables and similar methods which works not only for tables and even/odd rows,
// but as a general DOM means of assigning any number of classes to children of a parent element.
// Patrick H. Lauke aka redux / www.splintered.co.uk
// Distributed under the Creative Commons Attribution-ShareAlike license - http://creativecommons.org/licenses/by-sa/2.0/


/*
 * Summary:      Core experiment function that applies any number of classes to all child elements
 *               contained in all occurences of a parent element (either with or without a specific class)
 * Parameters:   parentElementTag - parent tag name
 *               parentElementClass - class assigned to the parent; if null, all parentElementTag elements will be affected
 *               childElementTag -  tag name of the child elements to apply the styles to
 *               styleClasses - comma separated list of any number of style classes (using 2 classes gives the classic "zebra" effect)
 * Return:       none
 */
function striper(parentElementTag, parentElementClass, childElementTag, styleClasses)
{
	var i=0,currentParent,currentChild;
	// capability and sanity check
	if ((document.getElementsByTagName)&&(parentElementTag)&&(childElementTag)&&(styleClasses)) {
		// turn the comma separate list of classes into an array
		var styles = styleClasses.split(',');
		// get an array of all parent tags
		var parentItems = document.getElementsByTagName(parentElementTag);
		// loop through all parent elements
		while (currentParent = parentItems[i++]) {
			// if parentElementClass was null, or if the current parent's class matches the specified class
			if ((parentElementClass == null)||(currentParent.className == parentElementClass)) {
				var j=0,k=0;
				// get all child elements in the current parent element
				var childItems = currentParent.getElementsByTagName(childElementTag);
				// loop through all child elements
				while (currentChild = childItems[j++]) {
					// based on the current element and the number of styles in the array, work out which class to apply
					k = (j+(styles.length-1)) % styles.length;
					// add the class to the child element - if any other classes were already present, they're kept intact
					currentChild.className = currentChild.className+" "+styles[k];
				}
			}
		}
	}
}

function addLoadEvent(func) {  
	var oldonload = window.onload;  
	if(typeof window.onload != 'function') {  
	  window.onload = func;  
	} 
	else {  
	  window.onload = function() {  
    	oldonload();  
    	func();  
  	}  
 	}  
}  

function hideunhide(str,option)
{
	elem = document.getElementById(str);
 if(option == null) {
		if(elem.style.display=='inline') {
			elem.style.display='none';  		
		}
		else {
			elem.style.display='inline';  	
		}
	}
	else if(option == 'empty') {
		if(elem.style.display=='none') {
			elem.style.display='';  		
		}
		else {
			elem.style.display='none';  	
		}		
	}
	else {
		if(elem.style.display=='block') {
			elem.style.display='none';  		
		}
		else {
			elem.style.display='block';  	
		}		
	}
}

//submits a program type change
function confirmApplication(id,type) {
	
	//first ask for confirmation 
	input_box=confirm("Are you sure you want to confirm this application?");
	if (input_box==false)
	{ 
		return;
	}	
	
	var ajax = new ajaxService;
	var ajaxResult = ajax.ajaxSubmit("/ajax/submit_confirmation.php?del=0&id=" + id + "&type=" + type,"");
	var resultString = ajaxResult.getElementsByTagName('result')[0].firstChild.data;
	
	if(resultString == "Success") {
		window.location.reload();
	}
	else {
		alert(resultString);
	}
}

function deleteApplication(id,type) {
	
	//first ask for confirmation 
	input_box=confirm("Are you sure you want to delete this application?");
	if (input_box==false)
	{ 
		return;
	}	
	
	var ajax = new ajaxService;
	var ajaxResult = ajax.ajaxSubmit("/ajax/submit_confirmation.php?del=1&id=" + id + "&type=" + type,"");
	var resultString = ajaxResult.getElementsByTagName('result')[0].firstChild.data;
	
	if(resultString == "Success") {
		window.location.reload();
	}
	else {
		alert(resultString);
	}
}

function acceptApplication(id) {
	
	//first ask for confirmation 
	input_box=confirm("Are you sure you want to accept this application?");
	if (input_box==false)
	{ 
		return;
	}	
	
	var ajax = new ajaxService;
	var ajaxResult = ajax.ajaxSubmit("/ajax/submit_appstatuschange.php?del=0&status=1&id=" + id,"");
	var resultString = ajaxResult.getElementsByTagName('result')[0].firstChild.data;
	
	if(resultString == "Success") {
		window.location.reload();
	}
	else {
		alert(resultString);
	}
}

function rejectApplication(id) {
	
	//first ask for confirmation 
	input_box=confirm("Are you sure you want to reject this application?");
	if (input_box==false)
	{ 
		return;
	}	
	
	var ajax = new ajaxService;
	var ajaxResult = ajax.ajaxSubmit("/ajax/submit_appstatuschange.php?del=0&status=2&id=" + id,"");
	var resultString = ajaxResult.getElementsByTagName('result')[0].firstChild.data;
	
	if(resultString == "Success") {
		window.location.reload();
	}
	else {
		alert(resultString);
	}
}

function deleteStudentApplication(id) {
	
	//first ask for confirmation 
	input_box=confirm("Are you sure you want to delete this application?");
	if (input_box==false)
	{ 
		return;
	}	
	
	var ajax = new ajaxService;
	var ajaxResult = ajax.ajaxSubmit("/ajax/submit_appstatuschange.php?del=1&id=" + id,"");
	var resultString = ajaxResult.getElementsByTagName('result')[0].firstChild.data;
	
	if(resultString == "Success") {
		window.location.reload();
	}
	else {
		alert(resultString);
	}
}



function checkWildfireSurveySubmit() {
	
	return true;
}
