// ajax.js
// Rev. A1.0 LNixon 02/09/09
// Rev. A1.1 LNixon 07/10/09 Added getSessionVar(session_var, id) which gets teh session var and returns it to the innerHTML of the node with id

var mywindow;

function newXmlHttp () {
	// create a new ajax object and return it
	var xmlHttp;
	try	{  // Firefox, Opera 8.0+, Safari  
    	xmlHttp = new XMLHttpRequest();
    }
	catch (e) {  // Internet Explorer  
    	try	{    
        	xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");    
        }
  		catch (e) {    
        	try {      
            	xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
    		catch (e) {      
            	alert("Your browser does not support AJAX!");      
                return false;      
            }    
		}  
	}
	return xmlHttp;
}

function ajax_ATI(url, pageElement, callMessage, id) {
	// call url with id returning results to pageElement and sending wait message
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
	var xmlHttp = newXmlHttp();
	var value = document.getElementById(id).value;
	var post_pairs = 'sid='+Math.random()+
		'&id='+id+
		'&value='+value
	//confirm(post_pairs);
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			//if(mywindow) mywindow.document.write(xmlHttp.responseText);
			document.getElementById(pageElement).innerHTML = xmlHttp.responseText;
		} else {
			document.getElementById(pageElement).innerHTML = callMessage;
		}
    }
	xmlHttp.open("POST", url, true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	xmlHttp.send(post_pairs);  	
}

function ajax_ATI_id(url, pageElement, callMessage, id) {
	// call url with id returning results to pageElement and sending wait message
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
	var xmlHttp = newXmlHttp();
	var post_pairs = 'sid='+Math.random()+
		'&id='+id
	//confirm(post_pairs);
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			//if(mywindow) mywindow.document.write(xmlHttp.responseText);
			document.getElementById(pageElement).innerHTML = xmlHttp.responseText;
		} else {
			document.getElementById(pageElement).innerHTML = callMessage;
		}
    }
	xmlHttp.open("POST", url, true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	xmlHttp.send(post_pairs);  	
}

function setSessionVar(session_var, session_value) {
	// set session var with value
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
	var xmlHttp = newXmlHttp();
	var value = session_value;
	var post_pairs = 'sid='+Math.random()+
		'&session_var='+session_var+
		'&session_value='+session_value
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(mywindow) mywindow.document.write(xmlHttp.responseText);
		}
    }
	//confirm(post_pairs);	
	xmlHttp.open("POST", "Ajax/setSessionVar.php", true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	xmlHttp.send(post_pairs);  	
}

function getSessionVar(session_var, id) {
	// get session var and return value to the id innerHTML
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
	var xmlHttp = newXmlHttp();
	var post_pairs = 'sid='+Math.random()+
		'&session_var='+session_var
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(mywindow) mywindow.document.write(xmlHttp.responseText);
			document.getElementById(id).innerHTML = xmlHttp.responseText;
		}
    }
	//confirm(post_pairs);	
	xmlHttp.open("POST", "Ajax/getSessionVar.php", true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	xmlHttp.send(post_pairs);  	
}

function setPostVar(post_var, post_value) {
	// set POST var with value
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
	var xmlHttp = newXmlHttp();
	var value = post_value;
	var post_pairs = 'sid='+Math.random()+
		'&post_var='+post_var+
		'&post_value='+post_value
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(mywindow) mywindow.document.write(xmlHttp.responseText);
		}
    }
	xmlHttp.open("POST", "Ajax/setPostVar.php", true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	xmlHttp.send(post_pairs);  	
}

function setGetVar(get_var, get_value) {
	// set POST var with value
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
	var xmlHttp = newXmlHttp();
	var value = get_value;
	var get_pairs = 'sid='+Math.random()+
		'&get_var='+get_var+
		'&get_value='+get_value
	//confirm(post_pairs);	
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(mywindow) mywindow.document.write(xmlHttp.responseText);
		}
    }
	xmlHttp.open("GET", "Ajax/setGetVar.php?"+get_pair, true); 
	xmlHttp.send(null);  	
}

function post_form_values(id) {
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
	setPostVar("id", id);
	var select_list = document.getElementsByTagName("select");
	//confirm(select_list[0].options[0].value);
	for (var i = 0; i < select_list.length; i++) { 
		select_elem = select_list[i]
		options_list = select_elem.options
		for (var j = 0; j < options_list.length; j++) {
			if(select_list[i].options[j].selected) {
				setPostVar(select_list[i].name, select_list[i].options[j].value);
			}
		}
	}
	var inputs = document.getElementsByTagName("textarea");
	for(i = 0; i < inputs.length; i++) {
		setPostVar(inputs[i].name, inputs[i].value);
	}
	
	var inputs = document.getElementsByTagName("input");
	for(i = 0; i < inputs.length; i++) {
		if(inputs[i].type == "button") {
			setPostVar(inputs[i].name, inputs[i].value);
		}
		if(inputs[i].type == "checkbox") {
			setPostVar(inputs[i].name+"_checked", inputs[i].checked);
			setPostVar(inputs[i].name, inputs[i].value);
		}
		if(inputs[i].type == "file") {
			setPostVar(inputs[i].name, inputs[i].value);
		}
		if(inputs[i].type == "hidden") {
			setPostVar(inputs[i].name, inputs[i].value);
		}
		if(inputs[i].type == "password") {
			setPostVar(inputs[i].name+"_readOnly", inputs[i].value);
			setPostVar(inputs[i].name, inputs[i].value);
		}
		if(inputs[i].type == "radio") {
			setPostVar(inputs[i].name+"_defaultChecked", inputs[i].value);
			setPostVar(inputs[i].name, inputs[i].value);
		}
		if(inputs[i].type == "reset") {
			setPostVar(inputs[i].name, inputs[i].value);
		}
		if(inputs[i].type == "submit") {
			setPostVar(inputs[i].name, inputs[i].value);
		}
		if(inputs[i].type == "text") {
			setPostVar(inputs[i].name, inputs[i].value);
		}
	}
}