/**
 * MAIN DOUCMENT: YOU MUST READ IT BEFORE MAKING ANY CHANGE
 * @author Deepesh Shah Chheda
 * @desc These ara AJAX related functions which are used for asynchronous intercation
 *      with back end of the system.
 *      getXMLHttpObject function is used for initial object creation as per the browser.
 *      There are two ways to have AJAX interaction
 *      FIRST: CALL getUpdateStatus method which will do all magic alone.
 *      SECOND: CALL getUpdateStatus_CallBack method which uses callback function and
 *              does the magic in collaboration with ajaxGET method.
 *              SECOND OPTION HAS BUG i.e. Its not working for simultanoeus calls.
 *
 *      Currently it uses only GET method only and handles normal response TEXT.
 *
 * @todo    implement POST method.
 *          handle XML data
 *          handle JSON data
 *          Keep UPDATING these document
 *          Resolve BUG for SECOND options or stick to FIRST OPTION
 */

/**
 * @name getUpdatedStatus
 * @param ID <int>
 * @param Status <boolean>
 * @param Type <string>
 * @param Div <string>
 * @desc This is the main AJAX function which will do the magic asychronously.
 * @author Deepesh Shah Chheda
 */
function getUpdatedStatus(intID, blnStatus, strType, strDiv)
{
    var xmlHTTP;    //FOR XMLHttpRequest Object

    if(strType == "ERROR")
    {
        var url="/error/admin/editDo.php?JS=Y&T=US&PAGE="+strType+"&rand=" + Math.random() + "&ID=" + intID + "&S=" + blnStatus;
        xmlHTTP=getXMLHttpObject();
        if(xmlHTTP != false)
        {
            xmlHTTP.onreadystatechange= function()
            {
                if (xmlHTTP.readyState==4 || xmlHTTP.readyState=="complete")
                {
                    if(xmlHTTP.status == 200)
                    {
                        if(xmlHTTP.responseText =="OK")
                        {
                            btnDelete = "btnDelete"+intID;
                            divDelete = "divRow"+intID;
                            strDeleteButton = "<input type='button' id='"+btnDelete+"' name='"+btnDelete+"' value='Delete' class='submit' onclick='deleteRow("+intID+", \"ERROR\", \""+divDelete+"\", \""+btnDelete+"\");' />";
                            //alert(strDeleteButton);
                            document.getElementById(strDiv).innerHTML = "Reviewed " + strDeleteButton;
                        }
                        else
                        {
                            document.getElementById(strDiv).innerHTML = "Something went wrong in getUpdatedStatus, Try again later.";
                        }
                        //document.getElementById(strDiv).innerHTML=xmlHTTP.responseText;
                    }
                    else if(xmlHTTP.status == 404)
                    {
                        alert("AJAX Error 404: Page not found");
                    }
                    else
                    {
                        alert("AJAX: Something wierd happend: " +xmlHTTP.status);
                    }
                }
                /*else
                { //alert("Ready State:" + xmlHTTP.readyState);
                }*/
            }
            xmlHTTP.open("GET", url , true);
            xmlHTTP.send(null);
        }
        else
        {
            alert("Oops AJAX not working, Either you are using Mobile or Browser seems to be quite old. Upgrade it.");
            return false;
        }
    }
    else
    {
        return false;
    }
}//ENDS: getUpdatedStatus

function ajaxDeleteRow(intID, strType, strDiv)
{
    var xmlHTTP;    //FOR XMLHttpRequest Object

    if(strType == "ERROR")
    {
        var url="/error/admin/editDo.php?JS=Y&T=DR&PAGE="+strType+"&rand=" + Math.random() + "&ID=" + intID;
        xmlHTTP=getXMLHttpObject();
        if(xmlHTTP != false)
        {
            xmlHTTP.onreadystatechange= function()
            {
                if (xmlHTTP.readyState==4 || xmlHTTP.readyState=="complete")
                {
                    if(xmlHTTP.status == 200)
                    {
                        if(xmlHTTP.responseText == "OK")
                        {
                            document.getElementById(strDiv).innerHTML = "<td colspan='11' class='text-bold'>Record Deleted</td>";
                        }
                        else
                        {
                            document.getElementById(strDiv).innerHTML = "Something went wrong in ajaxDeleteRow, Try again later.";
                            //alert(xmlHTTP.responseText);
                        }
                        //document.getElementById(strDiv).innerHTML=xmlHTTP.responseText;
                    }
                    else if(xmlHTTP.status == 404)
                    {
                        alert("AJAX Error 404: Page not found");
                    }
                    else
                    {
                        alert("AJAX: Something wierd happend: " +xmlHTTP.status);
                    }
                }
                /*else
                { //alert("Ready State:" + xmlHTTP.readyState);
                }*/
            }
            xmlHTTP.open("GET", url , true);
            xmlHTTP.send(null);
        }
        else
        {
            alert("Oops AJAX not working, Either you are using Mobile or Browser seems to be quite old. Upgrade it.");
            return false;
        }
    }
    else
    {
        return false;
    }
}//ENDS: ajaxDeleteRow

/**
 *@name ajaxGET
 *@param URL <string> well formed URL
 *@param isAynch <boolean> TRUE: Asynchronous and FALSE: Synchronous. Use Asyncrhronous mode
 *@param myFunction <function> Call back function
 *@desc This function will create object for XMLHttpRequest and handle the callback function.
 *      It uses GET method.
 *@author Deepesh Shah Chheda
 */
function ajaxGET(strURL, isAsynch, myFunction)
{
    
    objXHR=getXMLHttpObject();
    objXHR.onreadystatechange=myFunction;
    objXHR.open("GET",strURL,isAsynch);
    objXHR.send();
} //ENDS: ajaxGET

/**
 * @name getUpdatedStatus_CallBack
 * @param ID <int>
 * @param Status <boolean>
 * @param Type <string>
 * @param Div <string>
 * @desc This is the main AJAX function which will do the magic asychronously.
 *          This will assign call back funtion to ajaxGET which uses GET method
 *          This is an alternative for getUpdatedStatus method.
 * @author Deepesh Shah Chheda
 */
function getUpdatedStatus_CallBack(intID, blnStatus, strType, strDiv)
{
    
    if(strType == "ERROR")
    {
        var strURL="/admin/updateStatusDo.php?PAGE="+strType+"&sid=" + Math.random() + "&ID=" + intID + "&status=" + blnStatus;
    }
    else
    {
        return false;
    }

    ajaxGET(strURL, true, function()
        {
            if (objXHR.readyState==4 || objXHR.readyState=="complete")
            {
                if(objXHR.status == 200)
                {
                    document.getElementById(strDiv).innerHTML=objXHR.responseText
                }
                else if(objXHR.status == 404)
                {
                    alert("AJAX Error 404: Page not found");
                }
                else
                {
                    alert("AJAX: Something wierd happend: " +objXHR.status);
                }
            }
            /*else
            { //alert("Ready State:" + xmlHTTP.readyState);
            }*/
        });
}// ENDS: getUpdatedStatus_CallBack

/**
 * @author Deepesh Shah Chheda
 * @name getXMLHttpObject
 * @desc it will instantiate the object of xmlHTTPRequest according to browser versions.
 * @param handler function for xmlHTTPRequest object
 * @output return xmlHTTPRequest object
 * @IMPORTANT This function is final so DO NOT CHANGE IT.
 */
function getXMLHttpObject()
{
    var objXMLHttp=null;
    //FOR ALL THE LATEST BROWSERS LIKE FIREFOX 1.2, IE 6, CHROME, SAFARI, OPERA 7.7 and GREATER
    try
    {
        objXMLHttp = new XMLHttpRequest();
    }
    catch (e1)
    {
        // Internet Explorer Browsers 5 and 5.5
        try
        {
            objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e2)
        {
            try
            {
                objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e3)
            {
                // Something went wrong
                alert("Oops AJAX not working, Either you are using Mobile or Browser seems to be quite old. Upgrade it.");
                return false;
            }
            return objXMLHttp; //THIRD TRY
        }
        return objXMLHttp; //SECOND TRY
    }
    return objXMLHttp; //TOPMOST TRY
}// ENDS: getXMLHttpObject
