//AJAX get request
//file = ajax server page
//parameters = get parameter string
//display = id of object used to display response.
function makeGETRequest(file, parameters, display){ 
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var url=file;
	url=url+"?sid="+Math.random();
	url=url+"&"+parameters;
	xmlHttp.onreadystatechange = 	function stateChanged(){ 
										if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
											document.getElementById(display).innerHTML=xmlHttp.responseText;
										} 
									}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


//is this IE or Firefox?
function GetXmlHttpObject(){ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest();
	}else if (window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

function makePOSTRequest(url, parameters, display) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = function alertContents() {
										  if (http_request.readyState == 4) {
											 if (http_request.status == 200) {
												//alert(http_request.responseText);
												result = http_request.responseText;
												document.getElementById(display).innerHTML = result;            
											 } else {
												alert('There was a problem with the request.');
											 }
										  }
										}

      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }


function postForm(file, obj, display) {
      var poststr = getFormFields(obj);
      makePOSTRequest(file, poststr, display);
}
   
function getFormFields(obj) {
	var getstr = "";
  for (i=0; i<obj.childNodes.length; i++) {
	 if (obj.childNodes[i].tagName == "INPUT") {
		if (obj.childNodes[i].type == "hidden") {
		   getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
		}
		if (obj.childNodes[i].type == "text") {
		   getstr += obj.childNodes[i].name + "=" + encodeURI(obj.childNodes[i].value) + "&";
		}
		if (obj.childNodes[i].type == "checkbox") {
		   if (obj.childNodes[i].checked) {
			  getstr += obj.childNodes[i].name + "=" + encodeURI(obj.childNodes[i].value) + "&";
		   } else {
			  getstr += obj.childNodes[i].name + "=&";
		   }
		}
		if (obj.childNodes[i].type == "radio") {
		   if (obj.childNodes[i].checked) {
			  getstr += obj.childNodes[i].name + "=" + encodeURI(obj.childNodes[i].value) + "&";
		   }
		}
	 }   
	 if (obj.childNodes[i].tagName == "SELECT") {
		var sel = obj.childNodes[i];
		getstr += sel.name + "=" + encodeURI(sel.options[sel.selectedIndex].value) + "&";
	 }
	 if (obj.childNodes[i].tagName == "TEXTAREA") {
		var sel = obj.childNodes[i];
		getstr += sel.name + "=" + encodeURI(sel.value) + "&";
	 }
	 
  }
  
	return getstr;
}
