//********************************************************
// XML Requests - Multiple
// modified from http://www.drakware.com/?e=3
//********************************************************

var xmlreqs = new Array();

function createXMLRequest(callback) 
{
	this.busy = 0;
	this.callback = callback;
	this.xmlhttp = false;
	
	if (window.XMLHttpRequest)     { this.xmlhttp = new XMLHttpRequest(); }
	else if (window.ActiveXObject) { this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	
	if ( !this.xmlhttp ) alert("Error initializing XMLHttpRequest!");
}

function nextXMLRequest(callback)
{
    var pos=0;
	for (; pos<xmlreqs.length; pos++) { 
        if (!xmlreqs[pos].busy) { xmlreqs[pos].callback=callback; return pos; }
    }
	xmlreqs[pos] = new createXMLRequest(callback);
	return pos;
}

function xmlreqGET(url,callback,context)
{
    var pos = nextXMLRequest(callback);
    var req = xmlreqs[pos];
    var ctx = context;
    
    req.busy = 1;
    req.xmlhttp.open("GET",url,true);
    req.xmlhttp.onreadystatechange = function() {
        if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos,ctx); }
    }
    
    if (window.XMLHttpRequest)      req.xmlhttp.send(null);
    else if (window.ActiveXObject)  req.xmlhttp.send();
}

function xmlreqPOST(url,data,callback,context) 
{
    var pos = nextXMLRequest(callback);
    var req = xmlreqs[pos];
    var ctx = context;
    
    req.busy = 1;
    req.xmlhttp.open("POST",url,true);
    req.xmlhttp.onreadystatechange = function() {
        if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos,ctx); }
    }
    req.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.xmlhttp.send(data);
}

function xmlhttpChange(pos,ctx) {
    var req = xmlreqs[pos];
	if (typeof(req) != 'undefined' && req.busy && req.xmlhttp.readyState == 4) { 
	   req.callback(req.xmlhttp.status,req.xmlhttp.responseText,ctx); // responseText or responseXML?
       req.busy = 0;
	}
}


function divHTML(div_id,content_html){
 var e=document.getElementById(div_id);
 if (e){ 
  if (typeof(content_html)=='undefined') return e.innerHTML; 
  e.innerHTML=content_html; }
 else alert('divHTML: Unknown DIV id ('+div_id+')');
}

var coverDIV;
function coverDIVwithHTML(div_id,content_html){
 if (!coverDIV){
  coverDIV=document.createElement('div');
  coverDIV.className='cover';
  coverDIV.innerHTML=content_html;
 }
 var targetDIV=document.getElementById(div_id);
 setStyle(coverDIV,'top',getStyle(targetDIV,'top'));
 setStyle(coverDIV,'left',getStyle(targetDIV,'left'));
 setStyle(coverDIV,'width',getStyle(targetDIV,'width'));
 setStyle(coverDIV,'height',getStyle(targetDIV,'height'));
 targetDIV.parentNode.insertBefore(coverDIV,targetDIV);
}
function removeDIVcover()
{
 if (coverDIV&&coverDIV.parentNode)
  coverDIV.parentNode.removeChild(coverDIV);
}

function xmlreqLoad(div_id,url,content_html){
 if (typeof(content_html)!='undefined') coverDIVwithHTML(div_id,content_html);
 xmlreqGET(url,xmlreqLoad_resp,div_id);
}
function xmlreqLoad_resp(status,xml_html,div_id){
 removeDIVcover();
 if (status==200) { divHTML(div_id,xml_html); }
 else divHTML(div_id,'<div class="error">error: '+status+'</div>');
}
