var xmlHttp; 
var requestURL = 'RemoteCall.aspx?command='; 
var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
//netscape, safari, mozilla behave the same??? 
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0; 

function exec_remote_command_get(strCommand, strParams, commandResultsHandler)
{ 
    if(strCommand.length > 0)
    { 
        //Append the name to search for to the requestURL 
        var url = requestURL + strCommand + strParams; 
            
        //Create the xmlHttp object to use in the request 
        //stateChangeHandler will fire when the state has changed, i.e. data is received back 
        // This is non-blocking (asynchronous) 
        xmlHttp = GetXmlHttpObject(commandResultsHandler); 
                        
        //Send the xmlHttp get to the specified url 
        xmlHttp_Get(xmlHttp, url); 
    } 
    else 
    { 
        alert("Please provide a command."); 
    } 
} 

function exec_remote_command_post(strCommand, strXML, commandResultsHandler)
{ 
    if(strName.length > 0)
    { 
        //Append the name to search for to the requestURL 
        var url = requestURL + strCommand; 
            
        //Create the xmlHttp object to use in the request 
        //stateChangeHandler will fire when the state has changed, i.e. data is received back 
        // This is non-blocking (asynchronous) 
        xmlHttp = GetXmlHttpObject(commandResultsHandler); 
                        
        //Send the xmlHttp get to the specified url 
        xmlHttp_Post(xmlHttp, url, strXML); 
    } 
    else 
    { 
        alert('Please provide a command.'); 
    } 
}

function DisplayErrorHandler() {
    //readyState of 4 or 'complete' represents that data has been returned 
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete') {
        var str = new String(TrimString(xmlHttp.responseText));
        //alert(str);
        if (str != "") {
            var w = document.getElementById('tblMain').clientWidth;
            var h = document.getElementById('tblMain').clientHeight;

            var divWidth = new Number(450);
            var divHeight = new Number(200);
            var divLeft = new Number(((w / 2) - (divWidth / 2)));
            var divTop = new Number(150);

            document.getElementById("divErrorPopup").style.position = "absolute";
            document.getElementById("divErrorPopup").style.top = divTop;
            document.getElementById("divErrorPopup").style.left = divLeft;
            document.getElementById("divErrorPopup").style.height = divHeight;
            document.getElementById("divErrorPopup").style.width = divWidth;

            //See how much we execute and what we display if any.
            var idx = str.indexOf("<script>", 0);
            if (idx > -1) {
                //Remove it from the message.
                var scr = new String(str.substr(idx, ((str.indexOf("</script>", 0) + 9) - idx)));

                //Remove the script from the rest of the message.
                str = str.replace(scr, "");

                scr = scr.replace("<script>", "");
                scr = scr.replace("</script>", "");
                eval(scr);
            }

            if (str != "") {
                if (is_ie) {
                    document.frames("ifrMessage").SetMessage(str);
                    document.getElementById('divErrorPopup').style.display = "inline";
                }
                else {
                    //var col = window.frames["ifrMessage"].document.body.getElementsByTagName("DIV");
                    //col[0].innerHTML = str;

                    var ct = new Number(0);
                    //The messages are small.  There should not be any more than 10 of any of the script elements.
                    for (ct = 1; ct < 10; ct++) {
                        if (str.indexOf("<br/>") >= 0)
                            str = str.replace("<br/>", "\n");

                        if (str.indexOf("<br/>") >= 0)
                            str = str.replace("<br/>", "\n");

                        if (str.indexOf("<table>") >= 0)
                            str = str.replace("<table>", "");

                        if (str.indexOf("<table>") >= 0)
                            str = str.replace("<table>", "");

                        if (str.indexOf("</table>") >= 0)
                            str = str.replace("</table>", "");

                        if (str.indexOf("</table>") >= 0)
                            str = str.replace("</table>", "");

                        if (str.indexOf("<tr>") >= 0)
                            str = str.replace("<tr>", "");

                        if (str.indexOf("<tr>") >= 0)
                            str = str.replace("<tr>", "");

                        if (str.indexOf("</tr>") >= 0)
                            str = str.replace("</tr>", "");

                        if (str.indexOf("</tr>") >= 0)
                            str = str.replace("</tr>", "");

                        if (str.indexOf("<td>") >= 0)
                            str = str.replace("<td>", "");

                        if (str.indexOf("<td>") >= 0)
                            str = str.replace("<td>", "");

                        if (str.indexOf("</td>") >= 0)
                            str = str.replace("</td>", "");

                        if (str.indexOf("</td>") >= 0)
                            str = str.replace("</td>", "");

                        if (str.indexOf("<center>") >= 0)
                            str = str.replace("<center>", "");

                        if (str.indexOf("<CENTER>") >= 0)
                            str = str.replace("<CENTER>", "");
                    }

                    alert(str);
                }
            }
        }
    }
} 

function LogServiceHitsHandler() 
{
	//We don't need to inform the user of anything here.
}

function xmlHttp_Get(xmlhttp, url) 
{ 
    xmlhttp.open('GET', url, true); 
    xmlhttp.send('key'); 
} 

function xmlHttp_Post(xmlhttp, url, xml) 
{ 
    xmlhttp.open('POST', url, true); 
    xmlhttp.send(xml); 
} 

function GetXmlHttpObject(handler) 
{ 
    var objXmlHttp = null;    //Holds the local xmlHTTP object instance 

    //Depending on the browser, try to create the xmlHttp object 
    if(is_ie)
    { 
        //The object to create depends on version of IE 
        //If it isn't ie5, then default to the Msxml2.XMLHTTP object 
        var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'; 
            
        //Attempt to create the object 
        try{ 
            objXmlHttp = new ActiveXObject(strObjName); 
            objXmlHttp.onreadystatechange = handler; 
        } 
        catch(e){ 
        //Object creation errored 
            alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled'); 
            return; 
        } 
    } 
    else if(is_opera)
    { 
        //Opera has some issues with xmlHttp object functionality 
        alert('Opera detected. The page may not behave as expected.'); 
        return; 
    } 
    else
    { 
        // Mozilla | Netscape | Safari 
        objXmlHttp = new XMLHttpRequest(); 
        objXmlHttp.onload = handler; 
        objXmlHttp.onerror = handler; 
    } 
        
    //Return the instantiated object 
    return objXmlHttp; 
} 

function TrimString(str)
{
	var arrMatches = str.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (arrMatches == null) ? "" : arrMatches[1];
}


function HandleCredentialsValid() {
    //Don't display anything.
}

function HandleCredentialsError() {
    alert("The Bing credentials are invalid.");
}


