var commandCue = Array() ;
var loadhtml = '';

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 ;
  }
  else
  {
	  return '';
  }
}

function xmlhttpPost(divname, url, formdata, error) {
  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) {
         if (self.xmlHttpReq.status == 200) {
         	responseText = self.xmlHttpReq.responseText ;
         	ajaxCallBack(divname, responseText) ;
         }
         else
         {
         	if (error == ''){
         		alert('Connection Lost');
         	}
		else
		{
			eval(error);
		}
         }
       }
     }

  self.xmlHttpReq.send(formdata);
}

function ajaxCallBack(divname, responseText) {
  var statusDisplay ;
  if (divname != ''){
	  document.getElementById(divname).innerHTML = responseText;
  }
  setTimeout("postCue()", 10) ;
}


function cueXMLHTTPPost(divname, url, loader, error) {
  var cuecount = commandCue.length ;

  if (loader == 'true'){
  }

  var commandArray = Array()
  commandArray['divname'] = divname ;
  commandArray['url'] = url ;
  commandArray['formdata'] = '';
  commandArray['error'] = error;

  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'], command['error']) ;
  }
}

function cueXMLHTTPPostForm(divname, formname, url, loader, error) {
  var cuecount = commandCue.length ;
  var formdata = "";
  var thisform = formname;
  
  if (loader == 'true'){
  }
  
  // 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) + "&";
         }
    }
  var commandArray = Array()
  commandArray['divname'] = divname ;
  commandArray['url'] = url;
  commandArray['formdata'] = formdata;
  commandArray['error'] = error;

  commandCue[cuecount] = commandArray ;
  if (cuecount ==0) {
    // because the cue was empty, kick the cue in to action
    setTimeout("postCue()", 10) ;
  }

  return false;
}

