// stores the reference to the XMLHttpRequest object

var xmlHttpProcess;// = createXmlHttpRequestObject();
var xmlHttpProcessPC;
var xmlHttpProcessPCL;

// retrieves the XMLHttpRequest object

function createXmlHttpRequestObject()
{
    // will store the reference to the XMLHttpRequest object

    var xmlHttp;
    // if running Internet Explorer

    if(window.ActiveXObject)
    {
        try
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp = false;
        }
    }
    // if running Mozilla or other browsers

    else
    {
        try
        {
            xmlHttp = new XMLHttpRequest();
        }
        catch (e)
        {
            xmlHttp = false;
        }
    }
    // return the created object or display an error message

    if (!xmlHttp)
    {
        alert("Error creating the XMLHttpRequest object.");
        return false;
    }
    else
        return xmlHttp;
}

// make asynchronous HTTP request using the XMLHttpRequest object

function process()
{
    //alert("ok");
    // proceed only if the xmlHttp object isn't busy
    xmlHttpProcess=createXmlHttpRequestObject();
    if (xmlHttpProcess.readyState == 4 || xmlHttpProcess.readyState == 0)
    {
        // retrieve the name typed by the user on the form
        document.getElementById("divMessage").innerHTML = "<img src='WebTopUp/WebTopUpOnVOS/DevelopmentCode/UserInterface/images/loading.gif' />";
        var userName = encodeURIComponent(document.getElementById("userName").value);

        var userType = encodeURIComponent(document.getElementById("userType").value);
    
        // execute the checkUserName.php page from the server

        xmlHttpProcess.open("GET", rootPath+"AjaxRequest/checkUserNameForRetailer.php?userName=" + userName + "&userType=" + userType, true);
        // define the method to handle server responses

        xmlHttpProcess.onreadystatechange = handleServerResponse;
        // make the server request

        xmlHttpProcess.send(null);
    }
//else
// if the connection is busy, try again after one second

//setTimeout('process()', 1000);
}

function processPreferredCurrency(mobility, operatorName)
{
    //alert(mobility+'|'+operatorName);

    var countryName = encodeURIComponent(document.getElementById("countryName").value);
    var mobileNumber = encodeURIComponent(document.getElementById("mobileNumber").value);
    var amount = encodeURIComponent(document.getElementById("amount").value);

    xmlHttpProcessPC=createXmlHttpRequestObject();

    if(retailerCurrecny == countryCurrencyArray[countryName])
        {
            document.getElementById("error").innerHTML = '';
            document.getElementById('error').className="";
            
            document.getElementById("preferredCurrencyAmount").value = '' + amount + '';
            document.getElementById("divMessage").innerHTML="";

            return false;
         }
    
    if (xmlHttpProcessPC.readyState == 4 || xmlHttpProcessPC.readyState == 0)
    {
        // retrieve the name typed by the user on the form
        

        if (isNaN( amount) || mobileNumber.length == 0){
            document.getElementById("divMessage").innerHTML="";
            return false;
        }
        // execute the checkUserName.php page from the server
        
        xmlHttpProcessPC.open("GET", rootPath+"AjaxRequest/preferredCurrencyRechargeAmount.php?countryName=" + countryName+ "&operatorName=" + operatorName+ "&operatorMobility=" + mobility+  "&mobileNumber=" + mobileNumber+ "&amount=" + amount, true);
        // define the method to handle server responses

        xmlHttpProcessPC.onreadystatechange = handleServerResponseForPC;
        // make the server request

        xmlHttpProcessPC.send(null);
        document.getElementById("divMessage").innerHTML = "<img src='WebTopUp/WebTopUpOnVOS/DevelopmentCode/UserInterface/images/loading.gif' />";
        document.getElementById("proceed").disabled = true;
    }
    return true;
}

function processPreferredCurrencyForLoad(payeeUser, payeeUserType, loadAmount)
{
    var payeeCurrency = encodeURIComponent(document.getElementById("payeeCurrency").value);
    //alert(payeeCurrency);
    //alert(payerCurrency);
    var payeeName = payeeUser;
    var payeeType = payeeUserType;
    var amount = loadAmount;
    if (isNaN(amount) || amount ==0 ){
    document.getElementById("divMessage").innerHTML="";
        return;
    }
    //alert(payeeName+payeeType+amount);
    xmlHttpProcessPCL=createXmlHttpRequestObject();

     if(payerCurrency == payeeCurrency)
    {
        
        document.getElementById("error").innerHTML = '';
        document.getElementById('error').className="";
        document.getElementById("divMessage").innerHTML="";
        document.getElementById("preferredCurrencyAmount").value = '' + amount + '';
        
        return;
     }

    if (xmlHttpProcessPCL.readyState == 4 || xmlHttpProcessPCL.readyState == 0)
    {
        
        // execute the checkUserName.php page from the server

        xmlHttpProcessPCL.open("GET", rootPath+"AjaxRequest/preferredCurrencyLoadAmount.php?payeeUser=" + payeeName + "&payeeType=" + payeeType+ "&amount=" + amount, true);
        // define the method to handle server responses

        xmlHttpProcessPCL.onreadystatechange = handleServerResponseForLoad;
        // make the server request

        xmlHttpProcessPCL.send(null);
        document.getElementById("divMessage").innerHTML = "<img src='WebTopUp/WebTopUpOnVOS/DevelopmentCode/UserInterface/images/loading.gif' />";
        document.getElementById("loadAirtime").disabled = true;
    }
}

// executed automatically when a message is received from the server

function handleServerResponse()
{
    // move forward only if the transaction has completed

    if (xmlHttpProcess.readyState == 4)
    {
        // status of 200 indicates the transaction completed successfully

        if (xmlHttpProcess.status == 200)
        {
            // extract the XML retrieved from the server
      
            xmlResponse = xmlHttpProcess.responseXML;
            // obtain the document element (the root element) of the XML structure

            xmlDocumentElement = xmlResponse.documentElement;
            // get the text message, which is in the first child of

            // the the document element
      
            helloMessage = xmlDocumentElement.firstChild.data;
            // update the client display using the data received from the server
    
            document.getElementById("divMessage").innerHTML = '' + helloMessage + '';
      
        // restart sequence

        //setTimeout('process()', 1000);
        }
        // a HTTP status different than 200 signals an error

        else
        {
            alert("There was a problem accessing the server: " + xmlHttpProcess.statusText);
        }
    }
}

function handleServerResponseForPC()
{
    
    // move forward only if the transaction has completed

    if (xmlHttpProcessPC.readyState == 4)
    {
        // status of 200 indicates the transaction completed successfully

        if (xmlHttpProcessPC.status == 200)
        {
            // extract the XML retrieved from the server

            xmlResponse = xmlHttpProcessPC.responseXML;
            // obtain the document element (the root element) of the XML structure

            xmlDocumentElement = xmlResponse.documentElement;
            // get the text message, which is in the first child of

            // the the document element
            //var xmlDoc=xmlHttp.responseXML.documentElement;
            amountResponse = xmlDocumentElement.getElementsByTagName("amount")[0].childNodes[0].nodeValue;
            currencyResonse = xmlDocumentElement.getElementsByTagName("currency")[0].childNodes[0].nodeValue;
            //helloMessage = xmlDocumentElement.firstChild.data;
            // update the client display using the data received from the server
            
            if(amountResponse == 'operator not found')
            {
                document.getElementById("divMessage").innerHTML = '';
                document.getElementById("error").innerHTML = 'Operator Not Found. Please check your Number';
                document.getElementById('error').className="error";
                document.getElementById('amount').selectedIndex = 0;
            }else if(amountResponse == 'operator mobility supports')
            {
                document.getElementById("divMessage").innerHTML = '';
                document.getElementById("error").innerHTML = 'Please select an Operator';
                document.getElementById('error').className="error";
                document.getElementById('amount').selectedIndex = 0;
            }
            else
                {
                    displayMessage = amountResponse + '  ' + currencyResonse;
                    document.getElementById("error").innerHTML = '';
                    document.getElementById('error').className="";
                    document.getElementById("divMessage").innerHTML = '' + displayMessage + '';
                    document.getElementById("proceed").disabled = false;

                    document.getElementById("preferredCurrencyAmount").value = '' + amountResponse + '';
                }
        // restart sequence

        //setTimeout('process()', 1000);
        }
        // a HTTP status different than 200 signals an error

        else
        {
            alert("There was a problem accessing the server: " + xmlHttpProcessPC.statusText);
        }
    }
}

function handleServerResponseForLoad()
{
    // move forward only if the transaction has completed

    if (xmlHttpProcessPCL.readyState == 4)
    {
        // status of 200 indicates the transaction completed successfully

        if (xmlHttpProcessPCL.status == 200)
        {
            // extract the XML retrieved from the server

            xmlResponse = xmlHttpProcessPCL.responseXML;
            // obtain the document element (the root element) of the XML structure

            xmlDocumentElement = xmlResponse.documentElement;
            // get the text message, which is in the first child of

            // the the document element

            //helloMessage = xmlDocumentElement.firstChild.data;
            amountResponse = xmlDocumentElement.getElementsByTagName("amount")[0].childNodes[0].nodeValue;
            currencyResonse = xmlDocumentElement.getElementsByTagName("currency")[0].childNodes[0].nodeValue;
            // update the client display using the data received from the server
            //alert(helloMessage);
            document.getElementById("divMessage").innerHTML = '' + amountResponse + ' '+ currencyResonse + '';
            document.getElementById("preferredCurrencyAmount").value = '' + amountResponse + '';
            document.getElementById("loadAirtime").disabled = false;
        // restart sequence

        //setTimeout('process()', 1000);
        }
        // a HTTP status different than 200 signals an error

        else
        {
            alert("There was a problem accessing the server: " + xmlHttpProcessPCL.statusText);
        }
    }
}

