/*

 	Suite of routines supporting AJAX calls from web pages
	By: gary conley 
	Date: 08/13/2007
	
*/	


<!-- create XMLHTTP Object -->
function createXMLHTTP() {
	if (typeof XMLHttpRequest != "undefined")
		return new XMLHttpRequest();
	 else if (typeof window.ActiveXObject != "undefined")
	 	return new ActiveXObject("Msxml2.XMLHttp");
	 else throw ("createXMLHTTP method failed - browser does not support this method");	
}

/* Use this call when you want to process a GET operation.  Parameters should be passed in the URL
   (eg: ?java=ABC&mynextparam=nextparam&)
*/   
<!-- process HTTP Request -->
function httpRequestGet(url) {
	var httpObj = createXMLHTTP();
	httpObj.open("GET", url, false);
	httpObj.setRequestHeader("Content-Type", "text/xml");
	httpObj.send(null);
	if (httpObj.status == 200) {
		return httpObj.responseText;
	}
	else {
		throw httpObj.responseText;
	}		
}

/* Use this call when you want to process a POST operation.  Parameters should be passed in the DATA
   variable.  It gets passed seperatly.  The values in the DATA variable should be name/value pairs
   seperated by an ampersand (&). eg: java=ABC&mynextparam=nextparam
*/   

<!-- process HTTP Request Post -->
function httpRequestPost(url, data) {
	var httpObj = createXMLHTTP();
	httpObj.open("POST", url, false);
	httpObj.setRequestHeader("Content-Type", "text/xml");
	httpObj.send(data);
	if (httpObj.status == 200) {
		return httpObj.responseText;
	}
	else {
		throw httpObj.responseText;
	}		
}
/* Use this method when you want to pass the data associated with an HTML form.  This method operates
   like the the httpRequestPost method above with the added convenience that it will automatically
   parse the data entered into a form into name/value pairs and pass it in the DATA variable.
*/
<!-- process HTTP Request with form -->
function httpRequestFormPOST (url, form) {
	var data = getFormFieldsAsNameValue(form);
	var httpObj = createXMLHTTP();
	httpObj.open("POST", url, false);
	httpObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	httpObj.send(data);
	if (httpObj.status == 200) {
		return httpObj.responseText;
		}
	   else {
	    throw httpObj.responseText;
		}	
}

/* Use this method to convert form data to name/value pairs. */
<!-- convert form fields to name / value pairs-->
function getFormFieldsAsNameValue(form) {
	var formElements = form.elements;
	var data = "";
	for (i=0; i < form.elements.length; i ++) 
		{
		if (formElements[i].tagName == "INPUT" && formElements[i].name != "__VIEWSTATE") 
			{
			if (formElements[i].type== "hidden")
			   data += formElements[i].name + "=" + escape(formElements[i].value) + "&";
			  else if (formElements[i].type == "text") 
               data += formElements[i].name + "=" + escape(formElements[i].value) + "&";
              else if ((formElements[i].type == "radio" || formElements[i].type == "checkbox") && formElements[i].checked)
			   data += formElements[i].name + "=" + formElements[i].value + "&";
			}
	     else if (formElements[i].tagName == "SELECT" && formElements[i].options.length != 0)
	      data += formElements[i].name + "=" + formElements[i].options[formElements[i].selectedIndex].value + "&";
	     else if (formElements[i].tagName == "TEXTAREA") 
	   	  data += formElements[i].name + "=" + escape(formElements[i].value) + "&";	 		
		}
	data = data.substring(0, data.length-1);
	return data;
}	


