/*this script is used for dynamic searching in find ir and ir stats section*/

/*this 2 variables priority being set do default values
 *however they are being modified in stats section
 **/
var scriptToSendAjaxCall="stats/showIRs.php";
var elementIdToReturnScriptOutput="irRecordsContainer"
var statsurl="";


var worktype=null;
var priority=null;
var devco=null;
var reviewType=null;
var Feature=null;
var artefact=null;
var userid=null;
var artefacttype=null;
var irstatus=null;
var before=null;
var after=null;

var moderator=false;
var author=false;
var inspector=false;

var displayOptions=false;

var URL="";

function ajaxEditInspection(name, value, elementid, filetosend, thisid){

    urlpart=null
    if(loopSelected(thisid))
        urlpart=name+"="+loopSelected(thisid)+"&"

    var xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var url=filetosend+"?"
    if(urlpart)
        url=url+urlpart
    url=url+"&sid="+Math.random()


    xmlHttp.onreadystatechange = function()
    {
        if (xmlHttp.readyState == 4)
        {

            if (xmlHttp.status == 200)
            {
                document.getElementById(elementid).innerHTML=xmlHttp.responseText;
                setSearchVariables(name, value, thisid);

            }
            else
            {
                alert("There was a problem while using XMLHTTP:\n" + xmlHttp.statusText);
            }
        }

    }

    if(elementid){
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
    }
}

function setSearchVariables(name, value, objector){
    //alert("ASdF");
    if(name=="worktype"){
        worktype=loopSelected(objector);
    }
    if(name=="priority"){
        priority=loopSelected(objector);
    }
    if(name=="devco"){
        devco=loopSelected(objector);
    }
   
    if(name=="before"){
        before=value;
    }
    if(name=="after"){
        after=value;
    }   
    //loopSelected(objector)

   
    URL='';
    makeURL();
    ajaxDisplayRecords();
    
}

function ajaxDisplayRecords(){

    var gurl=scriptToSendAjaxCall+"?"
    gurl=gurl+URL+statsurl
    gurl=gurl+"&sid="+Math.random();
    var xmlHttp=GetXmlHttpObject();


    if (xmlHttp)
    {
        xmlHttp.onreadystatechange = function()
        {
            if (xmlHttp.readyState == 1)
                document.getElementById(elementIdToReturnScriptOutput).innerHTML="<h3>LOADING<h3>";
            if (xmlHttp.readyState == 2)
                document.getElementById(elementIdToReturnScriptOutput).innerHTML="<h3>LOADING<h3>";
            if (xmlHttp.readyState == 3)
                document.getElementById(elementIdToReturnScriptOutput).innerHTML="<h3>LOADING<h3>";
            if (xmlHttp.readyState == 4)
            {

                if (xmlHttp.status == 200)
                {
                    document.getElementById(elementIdToReturnScriptOutput).innerHTML=xmlHttp.responseText;
                    //this part of code is tricky :D
                    //basically it works like a switch but conditions are different a little bit
                    //we want to switch it on when user type the first character then the options panel
                    //is displayed when user puts more characters option panel is NOT refreshed
                    //this was to avoid checking out previously ckecked boxes
                    //the panel dissapears when there is 0 characters in a inpt text element
                    if(userid!=null&&userid.length>=1 && displayOptions==false){
                        ajaxDispalyUserTypes(); displayOptions=true;
                    }
                    if(userid!=null&&userid.length==0 && displayOptions==true){
                        ajaxDispalyUserTypes(); displayOptions=false; resetUserIDOptions();
                    }

                //   if(artefacttype)
                //   ajaxGetArtefact();

                }
                else
                {
                    alert("There was a problem while using XMLHTTP:\n" + xmlHttp.statusText);
                }
            }

        }
    }
    xmlHttp.open("GET",gurl,true);
    xmlHttp.send(null);
}

function ajaxRefreshTable(sqlquerry, whereClause){
    var elementid='irRecordsContainer'
    var filetosend='showIRs.php';

    var urlpart;
    urlpart='query='+sqlquerry
    urlpart+='&whereClause='+whereClause

    var xmlHttp=GetXmlHttpObject();
    //element_to_change=elementid;
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var url=filetosend+"?"
    url=url+urlpart
    url=url+"&sid="+Math.random()
    //alert("FULL URL: "+url+"ELEMENT TO CHANGE: "+elementid);

    xmlHttp.onreadystatechange = function(){


        if (xmlHttp.readyState == 0){
            document.getElementById(elementid).innerHTML=".";
        }
        else if (xmlHttp.readyState == 1){
            document.getElementById(elementid).innerHTML="..";
        }
        else if (xmlHttp.readyState == 2){
            document.getElementById(elementid).innerHTML="...";
        }
        else if (xmlHttp.readyState == 3){
            document.getElementById(elementid).innerHTML="almost done";
        }
        else if (xmlHttp.readyState == 4)
        {
            //if the request was a success the response html is written into productdiv div.

            if (xmlHttp.status == 200)
            {
                document.getElementById(elementid).innerHTML=xmlHttp.responseText
            }
            else
            {
                alert("There was a problem while using XMLHTTP:\n" + xmlHttp.statusText);
            }
        }
    }

    //xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("POST",filetosend,true)

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    //this condition is to speed up the post method
    //refer to http://www.openjs.com/articles/ajax_xmlhttp_using_post.php article
    if (navigator.platform != "Win32"
        || (/MSIE \d+\.\d+;/.test(navigator.userAgent)) == false
        || (/Windows NT 5\.0[;\)]/.test(navigator.userAgent)) == false) {
        xmlHttp.setRequestHeader("Content-length", urlpart.length);
        xmlHttp.setRequestHeader("Connection", "close");
    }
    xmlHttp.send(urlpart);

}



function ajaxDispalyUserTypes(){
    var gurl="../ajaxpackage/ar-userid.php"+"?"
    gurl+="userid="+userid
    gurl+="&sid="+Math.random();
    var xmlHttp=GetXmlHttpObject();


    if (xmlHttp)
    {
        xmlHttp.onreadystatechange = function()
        {
            if (xmlHttp.readyState == 1){
                document.getElementById('userOptions').innerHTML=".."
            }
            if (xmlHttp.readyState == 2){
                document.getElementById('userOptions').innerHTML="..."
            }
            if (xmlHttp.readyState == 3){
                document.getElementById('userOptions').innerHTML="...."
            }
            if (xmlHttp.readyState == 4)
            {

                if (xmlHttp.status == 200)
                {
                    document.getElementById('userOptions').innerHTML=xmlHttp.responseText;
                }
                else
                {
                    alert("There was a problem while using XMLHTTP:\n" + xmlHttp.statusText);
                }
            }

        }
    }


    xmlHttp.open("GET",gurl,true);
    xmlHttp.send(null);

}

function makeURL(){

    if(worktype){
        URL+="worktype="+worktype+"&"
    }
    if(priority){
        URL+="priority="+priority+"&"
    }
    if(devco){
        URL+="devco="+devco+"&"
    }
    if(Feature){
        URL+="Feature="+Feature+"&"
    }
    if(artefact){
        URL+="artefact="+artefact+"&"
    }
    if(irstatus){
        URL+="irstatus="+irstatus+"&"
    }
    if(userid){
        URL+="userid="+userid+"&"
    }
    if(author){
        URL+="author="+author+"&"
    }
    if(moderator){
        URL+="moderator="+moderator+"&"
    }
    if(inspector){
        URL+="inspector="+inspector+"&"
    }
    if(artefacttype){
        URL+="artefacttype="+artefacttype+"&"
    }
    if(before){
        URL+="before="+before+"&"
    }
    if(after){
        URL+="after="+after+"&"
    }

//alert("URL:  "+URL)
}

//--------------------------STANDARD FUNCTION-------------------------
function GetXmlHttpObject()
{

    var xmlhttp=false;

    //if the browser is mozilla/safari/opera
    if(window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
    //or internet explorer
    else if(window.ActiveXObject)
    {
        try
        {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e)
            {
                xmlhttp = false;
            }
        }
    }
    return xmlhttp;

}

function resetUserIDOptions(){
    moderator=false;
    author=false;
    inspector=false;

}

function ajaxGetArtefact(){

    var gurl="../ajaxpackage/artefact.php"+"?"

        if(artefacttype)
        gurl+="artefacttype="+artefacttype+"&"

    gurl=gurl+"&sid="+Math.random();
    var xmlHttp=GetXmlHttpObject();
    //alert (gurl)

    if (xmlHttp)
    {
        xmlHttp.onreadystatechange = function()
        {
            if (xmlHttp.readyState == 4)
            {

                if (xmlHttp.status == 200)
                {
                    document.getElementById('artefactbox').innerHTML=xmlHttp.responseText;
                }
                else
                {
                    alert("There was a problem while using XMLHTTP:\n" + xmlHttp.statusText);
                }
            }

        }
    }
    xmlHttp.open("GET",gurl,true);
    xmlHttp.send(null);
}



function loopSelected(theselectelement)
{
    //var txtSelectedValuesObj = document.getElementById('txtSelectedValues');
    var selectedArray = new Array();
    var selObj = document.getElementById(theselectelement);
    var i;
    var count = 0;
    for (i=0; i<selObj.options.length; i++) {
        if (selObj.options[i].selected) {
            selectedArray[count] = selObj.options[i].value;
            count++;
        }
    }
    //txtSelectedValuesObj.value = selectedArray;
    //alert("ARRAY:"+URL)
    var result=selectedArray.toString();
    return result
}
