var commandCue = Array() ;

function xmlhttpPost(divname, url) {
  var statusDisplay ;

  var xmlHttpReq = false;
  var self = this;
  var tempString ;

  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('');
}

function ajaxCallBack(divname, responseText) {
  var statusDisplay ;

  document.getElementById(divname).innerHTML = responseText;
  setTimeout("postCue()", 10) ;
}

// ############################################
// call this function to do a cued AJAX call
// ############################################
function cueXMLHTTPPost(divname, url) {
  var cuecount = commandCue.length ;
//alert(cuecount) ;

  var commandArray = Array()
  commandArray['divname'] = divname ;
  commandArray['url'] = url ;

  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']) ;
  }
}

// ############################################
// call this function to do an async ajax call - pauses all other browser requests until the div has been replaced
// ############################################
function asyncXMLHTTPPost(divname, url) {
  var statusDisplay ;

  var xmlHttpReq = false;
  var self = this;
  var tempString ;

  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');
  var responseText = "" ;
  self.xmlHttpReq.send('');
    if (self.xmlHttpReq.readyState == 4) {
     responseText = self.xmlHttpReq.responseText ;
     ajaxCallBack(divname, responseText) ;
    }
}


// ############################################
// function just to replace the div straight - no cueing - nothing
// ############################################
function replaceDivWithURL(divname, url) {
	if (divname != "mytestGRID")
	  {	
	  		
		  document.getElementById(divname).innerHTML = '<div align="center"><img src="images/fat2.gif" /></div>'
	  }
  var http = false;

  if(navigator.appName == "Microsoft Internet Explorer") {
    http = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    http = new XMLHttpRequest();
  } 

  http.open("GET", url, true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
		
	  if (divname == 'mytestGRID')
	  {
		  document.getElementById(divname).innerHTML = document.getElementById(divname).innerHTML + http.responseText;
	  }
	  else
	  {
		  if (divname != 'twitterDIV')
		  {
		  document.getElementById(divname).innerHTML = http.responseText;
		  }
		  else
		  {
			 //SPLIT CALL BY THE [JS] TAG
			splitme = http.responseText.split('[JS]');
			
			//PUT IN HTML
			document.getElementById(divname).innerHTML = splitme[0];
			
			//PASS SCRIPT BLOCK INTO THE CURRENT PAGE
			var myVal = document.createElement('SCRIPT');
			myVal.type = 'text/javascript';
			myVal.text = splitme[1];
			document.body.appendChild(myVal);
		  }
	  }
	  //DAVES ADDITION SO THAT IF THE DIV IS CALLED 'mytestnew' THEN THE LYTEBOX ITEMS ARE UPDATED
	  //THIS IS USED WHEN AJAX IS USED TO GET BACK HTML CONTAINS LYTEBAX CALLS
	  if (divname == 'mytestnew')
	  {
	  LyteBox.prototype.updateLyteboxItems();
	  }
    }
  }

  http.send(null);
}


// ############################################
// function just to replace the PARENT div straight - no queueing - Parent Div replacement
// ############################################
function replaceParentDivWithURL(divname, url) {
window.parent.document.getElementById(divname).innerHTML = '<div align="center">Please wait whilst we process your crazy request<br><img src="images/fat2.gif" /></div>'
  var http = false;

  if(navigator.appName == "Microsoft Internet Explorer") {
    http = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    http = new XMLHttpRequest();
  } 

  http.open("GET", url, true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      window.parent.document.getElementById(divname).innerHTML = http.responseText;
    }
  }

  http.send(null);
}


// ########################################################
// BELOW TWO FUNCTIONS ARE USED TO POPULATE A DROPDOWN BOX
// ########################################################
function ajaxFillDropDown(url)
{
if(navigator.appName == "Microsoft Internet Explorer") {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    xmlHttp = new XMLHttpRequest();
  } 

	xmlHttp.onreadystatechange=ajaxFillDropDownDOIT;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function ajaxFillDropDownDOIT()
{
	if (xmlHttp.readyState==4)
	{ 
		var templist = xmlHttp.responseText
		
		var twoparts = templist.split("@@");
		
		list = twoparts[1]
		
		var mydropdown = document.getElementById(twoparts[0])
		mydropdown.options.length = 0;
		
		var allarray = list.split("|");	
		counter = 1
		if(list != "")
		{
			var allarray = list.split("|");
			
			for(i = 0; i < allarray.length; i++)
			{
				subarray = allarray[i].split("#")
				mydropdown.options[i] = new Option(subarray[1],subarray[0])
			}
		}
	}
}