var commandCue = Array() ;
var loadhtml = '<table height="100%" width="100%" bgcolor="#DDDDDD"><tr><td align="center" valign="middle"><img src="/images/ajax-loader.gif"></td></tr></table>';

function xmlhttpGet(url) {
  var statusDisplay ;

  var xmlHttpReq = false;
  var self = this;

  if (window.XMLHttpRequest) {
    // Mozilla/Safari
    self.xmlHttpReq = new XMLHttpRequest();
  }
  else if (window.ActiveXObject) {
    // IE
    self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
  }

  var requestURL = url ;

  self.xmlHttpReq.open('POST', requestURL, false);
  self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  self.xmlHttpReq.send('');
  if (self.xmlHttpReq.readyState == 4) {
        return self.xmlHttpReq.responseText ;
  }

}

function xmlhttpPost(divname, url, formdata) {
  var statusDisplay ;
  var xmlHttpReq = false;
  var self = this;

  if (window.XMLHttpRequest) {
    // Mozilla/Safari
    self.xmlHttpReq = new XMLHttpRequest();
  }
  else if (window.ActiveXObject) {
    // IE
    self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
  }

  var requestURL = url ;

  self.xmlHttpReq.open('POST', requestURL, true);
  self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  self.xmlHttpReq.onreadystatechange =
     function() {
       var responseText = "" ;
       if (self.xmlHttpReq.readyState == 4) {
         responseText = self.xmlHttpReq.responseText ;
         ajaxCallBack(divname, responseText) ;
       }
     }

  self.xmlHttpReq.send(formdata);
}

function ajaxCallBack(divname, responseText) {
  var statusDisplay ;
  document.getElementById(divname).innerHTML = responseText;
  setTimeout("postCue()", 10) ;
}


function cueXMLHTTPPost(divname, url, loader) {
  var cuecount = commandCue.length ;

  if (loader == 'true'){
  	document.getElementById(divname).innerHTML = loadhtml;
  }

  var commandArray = Array()
  commandArray['divname'] = divname ;
  commandArray['url'] = url ;
  commandArray['formdata'] = '';

  commandCue[cuecount] = commandArray ;

  if (cuecount ==0) {
    // because the cue was empty, kick the cue in to action
    setTimeout("postCue()", 10) ;
  }
}

function postCue() {
  var cuecount = commandCue.length ;
  if (cuecount > 0) {
     var command = commandCue.shift() ;
     xmlhttpPost(command['divname'], command['url'], command['formdata']) ;
  }
}

function cueXMLHTTPPostForm(divname, formname, url, loader) {
  var cuecount = commandCue.length ;
  var formdata = "";
  var thisform = formname;
 
	// Loop through form fields
	for (i=0; i < thisform.length; i++)
	{
		 //Build Send String
		 if(thisform.elements[i].type == "text"){ //Handle Textbox's
				  formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
		 }else if(thisform.elements[i].type == "textarea"){ //Handle textareas
				  formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
		 }else if(thisform.elements[i].type == "checkbox"){ //Handle checkbox's
				 formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].checked + "&";
		 }else if(thisform.elements[i].type == "radio"){ //Handle Radio buttons
				  if(thisform.elements[i].checked==true){
					 formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].value + "&";
				  }
		 }else{
				  //finally, this should theoretically this is a select box.
				  formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
		 }
    }
	
  if (loader == 'true'){
  	document.getElementById(divname).innerHTML = loadhtml;
  }
  
  var commandArray = Array()
  commandArray['divname'] = divname ;
  commandArray['url'] = url;
  commandArray['formdata'] = formdata;
  
  commandCue[cuecount] = commandArray ;
  if (cuecount ==0) {
    // because the cue was empty, kick the cue in to action
    setTimeout("postCue()", 10) ;
  }

  return false;
}