var xmlHttp; 
var xmlHttp1;
var xmlHttp2;
var xmlHttp3;
var xmlHttp4;
var graphState;

function showHelp(reportType){ 
    xmlHttp1=GetXmlHttpObject();
    if (xmlHttp1==null){
        alert ("Browser does not support HTTP Request");
        return;
    }
    var helpurl;

    helpurl=reportType+"_help.php";
    
    reportClear(document.getElementById("HelpDiv"));
    //helpLoading(document.getElementById("HelpDiv"));
    xmlHttp1.onreadystatechange=helpChanged; 
    xmlHttp1.open("GET",helpurl,true);
    xmlHttp1.send(null);     
}

function closeHelp(){ 
    reportClear(document.getElementById("HelpDiv"));
}

function showStats(reportType,param){ 

    document.getElementById("hider").style.visibility="visible";    //MASK covers ALL 
    
    xmlHttp1=GetXmlHttpObject();
    if (xmlHttp1==null){
        alert ("Browser does not support HTTP Request");
        return;
    }
    


    reportClear(document.getElementById("GridDiv"));
    
    statsPreparingText(document.getElementById("GraphDiv"), reportType);
    //if (reportType=="Dashboard") {
    //    var graphurl=reportType+"_grid.php?"; 
    //   graphurl=graphurl+param+"&sid="+Math.random()+"&auth=Upgrade";
    //    xmlHttp1.onreadystatechange=dashboardChanged; 
    //    xmlHttp1.open("GET",graphurl,true);       
    //    xmlHttp1.send(null);
    ////} else {
        var graphurl=reportType+"_graph.php?"; 
        graphurl=graphurl+param+"&sid="+Math.random()+"&auth=Upgrade";
        xmlHttp1.onreadystatechange=graphChanged; 
        xmlHttp1.open("GET",graphurl,true);       
        xmlHttp1.send(null);
    //}

    // Need to clear helpDiv
     reportClear(document.getElementById("HelpDiv"));   
        
    //document.getElementById("hider").style.visibility="hidden";     //MASK is hidden    
}

function graphChanged(){ 
    if(xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete"){ 
        document.getElementById("GraphDiv").innerHTML=xmlHttp1.responseText;
        
        var reportType=document.report.rptName.value;
        var param = '&a='+document.report.site.value+'&s='+document.report.start.value+'&e='+document.report.end.value;

        var gridurl=reportType+"_grid.php?";
        gridurl=gridurl+param+"&sid="+Math.random()+"&auth=Blank";

        xmlHttp3=GetXmlHttpObject();     
        xmlHttp3.onreadystatechange=gridChanged; 
        xmlHttp3.open("GET",gridurl,true);
        xmlHttp3.send(null);
    }
}

function helpChanged(){ 
    if(xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete"){ 
        document.getElementById("HelpDiv").innerHTML=xmlHttp1.responseText;
    } 
}

function dashboardChanged(){
    if (xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete"){
        document.getElementById('GridDiv').innerHTML=xmlHttp1.responseText;
        
        var reportType=document.report.rptName.value;
        var param = '&a='+document.report.site.value+'&s='+document.report.start.value+'&e='+document.report.end.value;
        
        var graphurl2=reportType+"_graph.php?";
        graphurl2=graphurl2+param+"&sid="+Math.random()+"&auth=Blank";

        xmlHttp2=GetXmlHttpObject();     
        xmlHttp2.onreadystatechange=dashChanged1; 
        xmlHttp2.open("GET",graphurl2,true);
        xmlHttp2.send(null);        
    }  
}

function dashChanged1(){
    if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete"){
         //setTimeout("document.getElementById('DashboardDiv1').innerHTML=xmlHttp2.responseText",1000);
         document.getElementById('GraphDiv').innerHTML=xmlHttp2.responseText;
         document.getElementById('hider').style.visibility='hidden'; 
         
 
        //var reportType=document.report.rptName.value;
        //var param = '&a='+document.report.site.value+'&s='+document.report.start.value+'&e='+document.report.end.value;
        
        //var graphurl3=reportType+"_graph2.php?";
        //graphurl3=graphurl3+param+"&sid="+Math.random()+"&auth=Blank";

        //xmlHttp3=GetXmlHttpObject();     
        //xmlHttp3.onreadystatechange=dashChanged2; 
        //xmlHttp3.open("GET",graphurl3,true);
        //xmlHttp3.send(null);
    }  
}

function dashChanged2(){
    if (xmlHttp3.readyState==4 || xmlHttp3.readyState=="complete"){
        
        //setTimeout("document.getElementById('DashboardDiv2').innerHTML=xmlHttp3.responseText",500);
        document.getElementById('GridDiv2').innerHTML=xmlHttp3.responseText;
        document.getElementById('hider').style.visibility='hidden';
        //var reportType=document.report.rptName.value;
        //var param = '&a='+document.report.site.value+'&s='+document.report.start.value+'&e='+document.report.end.value;
        
        //var graphurl4=reportType+"_graph2.php?";
        //graphurl4=graphurl4+param+"&sid="+Math.random()+"&auth=Blank";

        //xmlHttp4=GetXmlHttpObject();     
        //xmlHttp4.onreadystatechange=dashChanged3; 
        //xmlHttp4.open("GET",graphurl4,true);
        //xmlHttp4.send(null); 
    }  
}

function dashChanged3(){
    if (xmlHttp4.readyState==4 || xmlHttp4.readyState=="complete"){
        //setTimeout("document.getElementById('DashboardDiv3').innerHTML=xmlHttp4.responseText",500);
     
        document.getElementById('SubGraphDiv1').innerHTML=xmlHttp4.responseText;
        document.getElementById('hider').style.visibility='hidden';
         /*
        var reportType=document.report.rptName.value;
        var param = '&a='+document.report.site.value+'&s='+document.report.start.value+'&e='+document.report.end.value;
        
        var graphurl4=reportType+"_graph3.php?";
        graphurl4=graphurl4+param+"&sid="+Math.random()+"&auth=Blank";

        xmlHttp4=GetXmlHttpObject();     
        xmlHttp4.onreadystatechange=dashChanged3; 
        xmlHttp4.open("GET",graphurl4,true);
        xmlHttp4.send(null); 
        */

    }  
}
function dashChanged4(){
    if (xmlHttp3.readyState==4 || xmlHttp3.readyState=="complete"){
        setTimeout("document.getElementById('DashboardDiv2').innerHTML=xmlHttp3.responseText",1000);
        //document.getElementById('DashboardDiv2').innerHTML=xmlHttp3.responseText;
        document.getElementById('hider').style.visibility='hidden';
        /*
        var reportType=document.report.rptName.value;
        var param = '&a='+document.report.site.value+'&s='+document.report.start.value+'&e='+document.report.end.value;
        
        var graphurl3=reportType+"_graph2.php?";
        graphurl3=graphurl3+param+"&sid="+Math.random()+"&auth=Blank";

        xmlHttp3=GetXmlHttpObject();     
        xmlHttp3.onreadystatechange=dashChanged2; 
        xmlHttp3.open("GET",graphurl3,true);
        xmlHttp3.send(null); 
        */

    }  
}
        


function gridChanged(){
    if (xmlHttp3.readyState==4 || xmlHttp3.readyState=="complete"){
        //setTimeout("document.getElementById('GridDiv2').innerHTML=xmlHttp2.responseText; document.getElementById('hider').style.visibility='hidden';",1000); 
        //hello
        document.getElementById('GridDiv').innerHTML=xmlHttp3.responseText; 
        document.getElementById('hider').style.visibility='hidden';
    }  
}

function gridChanged2(){
    if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete"){
        //setTimeout("document.getElementById('GridDiv2').innerHTML=xmlHttp2.responseText; document.getElementById('hider').style.visibility='hidden';",1000); 
        //hello
        document.getElementById('GridDiv2').innerHTML=xmlHttp2.responseText; 
        document.getElementById('hider').style.visibility='hidden';
    }  
}

function GetXmlHttpObject(){
    var xmlHttp=null;
    try { xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
    } catch (e) {
        //Internet Explorer
        //try { return new ActiveXObject("Msxml2.XMLHTTP.7.0") } catch(e) {
            try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {
                //try { return new ActiveXObject("Msxml2.XMLHTTP.5.0") } catch(e) {
                    //try { return new ActiveXObject("Msxml2.XMLHTTP.4.0") } catch(e) {
                        try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {
                            try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {
                                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                            }
                        }
                    //}
                //}
            }
        //}
    }
    return xmlHttp;
}

function reportLoadingMore(element){
    while(element.hasChildNodes()){
        element.removeChild(element.lastChild);
    }
    var image = document.createElement("img");
        image.setAttribute("src","loader3.gif");
    image.setAttribute("alt","Loading...");
    element.appendChild(image);
    var p=document.createElement("p");
    element.appendChild(p);
    var txt=document.createTextNode('Loading statistics report...');
    element.appendChild(txt);
} 

function reportLoadingText(element){
    while(element.hasChildNodes()){
        element.removeChild(element.lastChild);
    }
    var p=document.createElement("p");
    element.appendChild(p);
    var txt=document.createTextNode('Loading statistics report...');
    element.appendChild(txt);
} 

function helpLoading(element){
    while(element.hasChildNodes()){
        element.removeChild(element.lastChild);
    }
    var image = document.createElement("img");
        image.setAttribute("src","loader3.gif");
    image.setAttribute("alt","Loading...");
    element.appendChild(image);
    var p=document.createElement("p");
    element.appendChild(p);
} 

function reportLoading(element){
    while(element.hasChildNodes()){
        element.removeChild(element.lastChild);
    }
    var image = document.createElement("img");
    image.setAttribute("src","spinner2.gif");
    image.setAttribute("alt","Loading...");
    element.appendChild(image);
    var p=document.createElement("p");
    p.setAttribute("style","align:center;font-size:13px;");
    element.appendChild(p);
    var txt=document.createTextNode('Your stats are loading');
    p.appendChild(txt);
}

function statsPreparingText(element, rpt){
    while(element.hasChildNodes()){
        element.removeChild(element.lastChild);
    }
    var txtMsg="Preparing "+rpt+" report...";
    var txt=document.createTextNode(txtMsg);
    element.appendChild(txt);
}

function statsLoadingText(element, rpt){
    while(element.hasChildNodes()){
        element.removeChild(element.lastChild);
    }
    var txtMsg="Loading "+rpt+" report...";
    var txt=document.createTextNode(txtMsg);
    element.appendChild(txt);
}

function statsLoading(element, rpt){
    while(element.hasChildNodes()){
        element.removeChild(element.lastChild);
    }
    var image = document.createElement("img");
    image.setAttribute("src","spinner2.gif");
    image.setAttribute("alt","Loading stats...");
    element.appendChild(image);
    var p=document.createElement("p");
    element.appendChild(p);
    var txtMsg="Preparing graph for "+rpt+" report...";
    var txt=document.createTextNode(txtMsg);
    element.appendChild(txt);
}

function reportClear(element){
    while(element.hasChildNodes()){
        element.removeChild(element.lastChild);
    }
}

function showGrid(reportUrl){ 
    xmlHttp3=GetXmlHttpObject();
    if (xmlHttp3==null){
        alert ("Browser can execute HTTP Request");
        return;
    }

    var gridurl;
    gridurl=reportUrl+"&sid="+Math.random();
    reportLoading(document.getElementById("GridDiv"));
    xmlHttp3.onreadystatechange=gridChanged; 
    xmlHttp3.open("GET",gridurl,true);
    xmlHttp3.send(null); 
}