var xml_dispatch_request=null;
var MapIsVisible=false;

function PageQuery(q) 
{
        if(q.length > 1) 
                this.q = q.substring(1, q.length);
        else 
                this.q = null;
                
        this.keyValuePairs = new Array();
        if(q) 
        {
                for(var i=0; i < this.q.split("&").length; i++) 
                {
                        this.keyValuePairs[i] = this.q.split("&")[i];
                }
        }
        
        this.getKeyValuePairs = function() { return this.keyValuePairs; }
        this.getValue = function(s) 
        {
                for(var j=0; j < this.keyValuePairs.length; j++) 
                {
                        if(this.keyValuePairs[j].split("=")[0] == s)
                        return this.keyValuePairs[j].split("=")[1];
                }
                return false;
        }
        
        this.getParameters = function() 
        {
                var a = new Array(this.getLength());
                for(var j=0; j < this.keyValuePairs.length; j++) 
                {
                        a[j] = this.keyValuePairs[j].split("=")[0];
                }
                return a;
        }
        
        this.getLength = function() { return this.keyValuePairs.length; } 
}

function queryString(key)
{
        var page = new PageQuery(window.location.search); 
        return unescape(page.getValue(key)); 
}

function RequestPageXML(callback,xml_url) 
{           
        
    xml_dispatch_request=null;
    response=null;    
    submit_xml_url = xml_url;
    
    
    if (window.XMLHttpRequest) 
    {
        xml_dispatch_request = new XMLHttpRequest();
        xml_dispatch_request.callback=callback;
        xml_dispatch_request.onreadystatechange = Request_XML_Callback;
        xml_dispatch_request.open("GET", submit_xml_url, true);
        xml_dispatch_request.send(null);    
    } 
    else if (window.ActiveXObject) 
    {
        xml_dispatch_request = new ActiveXObject("Microsoft.XMLHTTP");
        if (xml_dispatch_request) 
        {
            xml_dispatch_request.callback=callback;
            xml_dispatch_request.onreadystatechange = Request_XML_Callback;
            xml_dispatch_request.open("GET", submit_xml_url, true);
            xml_dispatch_request.send();
        }
    }    
}




function Request_XML_Callback()
{
    if (xml_dispatch_request.readyState == 4 && xml_dispatch_request.status == 200) 
    {                            
		response  = xml_dispatch_request.responseXML.documentElement; 
		xml_dispatch_request.callback(response);
    }		

}
                                                        
function Update()
{                 
	MapIsVisible = (queryString("map")=="true");                                                                                       
        
    var sXMLURL;                                
    var callid = queryString("callid");
    
    //exit if they haven't chosen a call ID yet
    if (callid=="false")
            return;
            
    sXMLURL = "xml_call_status.asp?callid=" + callid + "&hash=" + Math.random();                                                        
    RequestPageXML(Update_Reply,sXMLURL);
                                                                                                       
        
}     

function Update_Reply(response)
{	
    var msg = response.getElementsByTagName("message");
    var sMsg = msg[0].getAttribute("text");
    if ( document.getElementById("MessageBox") )
            document.getElementById("MessageBox").innerHTML=sMsg;
    
    var ShowMap = (parseInt(msg[0].getAttribute("showMap")) == 1);
    
    
    if (ShowMap)
    {
            if (!MapIsVisible)
            {
                    document.location = document.location + "&map=true";
                    return;
            }
    }
    else
    {
            if (MapIsVisible)
            {
                    document.location = "index.asp?callid=" + callid;
                    return;
            }                                        
    } 
    
    window.setTimeout(Update,2000);                               
}                           
    
