//browser independent httprequest obj
function getHTMLRequest(readyState){
    var xhr = null;
    /*if (window.XMLHttpRequest){
        xhr =  new XMLHttpRequest();     // Firefox, Safari, ...
        xhr.onload = readyState;
    }else if (window.ActiveXObject){   // ActiveX version
        xhr =  new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
        xhr.onreadystatechange = readyState;
    }
    return xhr;   */ 
    
    if (typeof window.ActiveXObject != 'undefined' ){
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
       xhr.onreadystatechange = readyState;
   }else{
       xhr = new XMLHttpRequest();
       xhr.onload = readyState;
   }
   return xhr;
}
//lifted from http://www.devarticles.com/c/a/XML/XML-in-the-Browser-Submitting-forms-using-AJAX/5/
//modified somewhat
/*
 * This function accepts two arguments. The first is a reference to a form object, 
 * the other is the name of a validating function in the form of a string. 
 * The elements array of the form object is looped through, and a switch construct 
 * decides what to do with each of the relevant types of form elements 
 * (in this case just text and select-one). If the element is a text field, the eval function 
 * is used to call the validating function that is specified by argument two, which is 
 * sent the value of the form element.
 */
function getSqlFormValues(fobj,valFunc){
   var str = "";
   var valueArr = null;
   var val = "";
   var cmd = "";
   str += 'sql=' + getqry(true) + "&";
   for(var i = 0;i < fobj.elements.length;i++){
       switch(fobj.elements[i].type){
           case "text":
                if(valFunc){
                    //use single quotes for argument so that the value of
                    //fobj.elements[i].value is treated as a string not a literal
                    cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
                    val = eval(cmd)
                }
                str += fobj.elements[i].name +
                 "=" + URLEncode(fobj.elements[i].value) + "&";
                 break;
           case "select-one":
                str += fobj.elements[i].name +
                "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].text + "&";
                //alert(fobj.elements[i].name+", "+fobj.elements[i].options[fobj.elements[i].selectedIndex].text);
                break;
           case "hidden":
           case "textarea":
               
                str += fobj.elements[i].name +"="+ URLEncode(fobj.elements[i].value)+"&";          
                break;
           default:break;
       }
   }
   str = str.substr(0, (str.length - 1)); 
   return str;
}

function sendSyntax(f,url){
    hideExecButtons();
    document.getElementById('message').className = 'happy';
    var str = getSqlFormValues(f, null);
    var xhr = null;   
    if (typeof window.ActiveXObject != 'undefined' ){
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
       xhr.onreadystatechange = function(){showMessage(xhr);};
   }else{
       xhr = new XMLHttpRequest();
       xhr.onload = function(){showMessage(xhr);};
   }
    xhr.open( "POST", 'submitjobhelper.aspx', true );
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xhr.send(str);
    return xhr;
}
function sendQuery(f, url) {
    document.getElementById('message').className = 'happy';
    var str = getSqlFormValues(f, null);
    
    var xhr = null;   
    if (typeof window.ActiveXObject != 'undefined' ){
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
       xhr.onreadystatechange = function(){fetchResults(xhr);};
   }else{
       xhr = new XMLHttpRequest();
       xhr.onload = function(){fetchResults(xhr);};
   }
    xhr.open( "POST", 'submitjobhelper.aspx', true );
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xhr.send(str);
    return xhr;
}
function initHist(){
    document.getElementById('message').className = 'happy';
    var str = '';
    var xhr = null;   
    if (typeof window.ActiveXObject != 'undefined' ){
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
       xhr.onreadystatechange = function(){fetchResults(xhr);};
   }else{
       xhr = new XMLHttpRequest();
       xhr.onload = function(){showMessage(xhr);};
   }
    xhr.open( "POST", 'submitjobhelper.aspx', true );
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    xhr.send(str);
    return xhr;
}
function showMessage(xhr) {
     if (xhr.readyState == 4) {
        stopTimer();
        if(xhr.responseXML.normalize)//combines textnodes in ff, to get past 4096 limit.  ie7 doesn't need this
                xhr.responseXML.normalize();
        if(xhr.responseXML.getElementsByTagName('message')[0].firstChild)
            document.getElementById('message').innerHTML = URLDecode(xhr.responseXML.getElementsByTagName('message')[0].firstChild.data);
        if(xhr.responseXML.getElementsByTagName('hist')[0].firstChild){
            document.getElementById('hist').innerHTML = URLDecode(xhr.responseXML.getElementsByTagName('hist')[0].firstChild.data);
        }
        var mtype = xhr.responseXML.getElementsByTagName('message')[0].attributes.getNamedItem("mtype").value;
        if(mtype == 'happy'){
            document.getElementById('message').className = 'happy';
        }else
            document.getElementById('message').className = 'message';
    }
    showExecButtons();
}

function fetchResults(xhr){    
    if (xhr.readyState == 4) {
        stopTimer();
        var mtype = xhr.responseXML.getElementsByTagName('message')[0].attributes.getNamedItem("mtype").value;
        var jobid = xhr.responseXML.getElementsByTagName('jobid')[0].firstChild.data;        
        showMessage(xhr);   
        if(jobid > -1){
            window.open('jobdetails.aspx?id='+jobid,'_top','');
        }else{
            if(mtype == 'happy'){
                setEditorMid();
                window.frames['resultPane'].location = 'output/vodataclean.aspx';
            }
        }
    }
}
