
/**********************************************************************************************************
* NB. THERE IS ALSO A COPY OF THIS FILE IN TT\SITE\SCIPTS
**********************************************************************************************************/

/**********************************************************************************************************
*Author:		Geoff Phair
*Date:			18-7-08
*Overview:		Calls a page, and handles the response.
*               Doesn't do anything after response is received.
*               Doesn't show alerts unless there is an error message in the response.
**********************************************************************************************************/
function Ajax_PageCall( strFileString )
{
    var blnSuccess;
    var objXmlHttp;
//alert(strFileString);
    blnSuccess = false;
    
    //Create the object
    objXmlHttp = Ajax_OpenJavasctiptobjXmlHttpObject()

    objXmlHttp.onreadystatechange=function()
    {
        /*
        This essentially is a listener waiting for a response from the File in strFileString.
        readyState values:
        0 (Uninitialized) The object has been created, but not initialized (the open method has not been called). 
        1 (Open) The object has been created, but the send method has not been called.  
        2 (Sent) The send method has been called. responseText is not available. responseBody is not available.  
        3 (Receiving) Some data has been received. responseText is not available. responseBody is not available.  
        4 (Loaded) All the data has been received. responseText is available. responseBody is available.  
        */    
        if(objXmlHttp.readyState==4)
	    {
            if (objXmlHttp.status  == 200) 
	        {
		        //expects a simple text response.
		        //If 'error' appears in the response, an alert is displayed
		        var strResponse = objXmlHttp.responseText;
		        if (strResponse == "OK") 
		        {
                    // success
                    blnSuccess = true;
		        } 
		        else if (strResponse != "") 
		        {
			        //show the error message
			        alert("Error: " + strResponse);
		        }
	        }
        } 
        else 
        {
	        //;
        }
    }
    //This will run the file in strFileString, which is where all the work gets done.
    //eg. objXmlHttp.open("GET","airfares-match.asp?IataFrom=" + strDeparture);
    objXmlHttp.open("GET", strFileString);
    objXmlHttp.send(null);  
    return blnSuccess;
}

/**********************************************************************************************************
*Author:		Mark Evans
*Date:			9th Mar 08
*Overview:		Clear all elements from a dropdown
*Parameters:	strDropdown - name of the dropdown
**********************************************************************************************************/
function Ajax_clearOptions(strDropdown) 
{
    var selbox = document.getElementById(strDropdown);
    selbox.options.length = 0;
}


/**********************************************************************************************************
*Author:		Mark Evans
*Date:			9th Mar 08
*Overview:		Used for one dropdown creating the contents of a second dropdown. First it disables the 
*				dropdown to be populated, then opens an Ajax object and uses this to open and execute 
*				the file specified.
**********************************************************************************************************/
function Ajax_MatchSearch(objDropdown, strFileString)
{
    if (objDropdown == undefined)
    {
        return;
    }
    
    //alert(strFileString); 
	var blnSuccess;
    var objXmlHttp;
    
    //Disable the dropdown box we are creating items for.
    objDropdown.disabled = true;
    Ajax_clearOptions(objDropdown.id);
    Ajax_setOptions(objDropdown.id, 'Loading...', '');

    blnSuccess = false;
    
    //Create the object
    objXmlHttp = Ajax_OpenJavasctiptobjXmlHttpObject()

    objXmlHttp.onreadystatechange = function() {
        /*
        This essentially is a listener waiting for a response from the File in strFileString.
        readyState values:
        0 (Uninitialized) The object has been created, but not initialized (the open method has not been called). 
        1 (Open) The object has been created, but the send method has not been called.  
        2 (Sent) The send method has been called. responseText is not available. responseBody is not available.  
        3 (Receiving) Some data has been received. responseText is not available. responseBody is not available.  
        4 (Loaded) All the data has been received. responseText is available. responseBody is available.  
        */
        if (objXmlHttp.readyState == 4)
        {
            if (objXmlHttp.status == 200) {

                //outputs javascript so we can eval, to execute the code that it sends back (in this case a simple alert box);
                var strResponse = objXmlHttp.responseText;
                if (strResponse.indexOf('alert(\'Error') == 0) {
                    try {
                        eval(strResponse);
                    }
                    catch (e) {
                        //alert('Internal processing error.');
                        objDropdown.disabled = true;
                        objDropdown.options.length = 0;
                        objDropdown.options[0] = new Option('Internal processing error', '');
                    }
                }
                else {
                    try {
                        //execute the returned javascript
                        eval(strResponse);
                    }
                    catch (e) {
                        alert(e.description);
                        alert(strResponse);
                        //clear all the items from the dropdown and show the error as the only element
                        objDropdown.disabled = true;
                        objDropdown.options.length = 0;
                        objDropdown.options[0] = new Option('Error processing Request', '');
                    }
                }
            }
            else {
                objDropdown.disabled = false;
            }
        }

    }
    
	//This will run the file in strFileString, which is where all the work gets done.
    //eg. objXmlHttp.open("GET","airfares-match.asp?IataFrom=" + strDeparture);
    objXmlHttp.open("GET", strFileString);
    objXmlHttp.send(null);  
    return blnSuccess;
}


/**********************************************************************************************************
*Author:		Mark Evans
*Date:			9th Mar 08
*Overview:		Creates an Ajax object, or errors if browser doesn't support Ajax.
**********************************************************************************************************/
function Ajax_OpenJavasctiptobjXmlHttpObject()
{
    var objXmlHttp;

    try
	    {
	    // Firefox, Opera 8.0+, Safari
	    objXmlHttp=new XMLHttpRequest();
	    }
    catch (e)
	    {
	    // Internet Explorer
	    try
		    {
		    objXmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		    }
	    catch (e)
		    {
		    try
			    {
			    objXmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			    }
		    catch (e)
			    {
			    alert("Error: Your browser does not support AJAX!");
			    return false;
			    }
		    }
	    }

	return objXmlHttp;
}


/**********************************************************************************************************
*Author:		Mark Evans
*Date:			9th Mar 08
*Overview:		Add an element to a dropdown
*Parameters:	strDropdown - name of the dropdown; strText - element text; strValue - element value
**********************************************************************************************************/
function Ajax_setOptions(strDropdown, strText, strValue) 
{
    var selbox = document.getElementById(strDropdown);
    selbox.options[selbox.options.length] = new Option(strText, strValue);
}


/**********************************************************************************************************
*Author:		Mark Evans
*Date:			9th Mar 08
*Overview:		Opens an Ajax object and uses this to open and execute the file specified, sending any
*               return values back to the page.
**********************************************************************************************************/
function Ajax_WriteToPage(strFileString) 
{
 //alert(strFileString);
    var blnSuccess;
    var objXmlHttp;
    //Create the object
    objXmlHttp = Ajax_OpenJavasctiptobjXmlHttpObject()

    objXmlHttp.onreadystatechange = function() 
    {
        /*
        This essentially is a listener waiting for a response from the File in strFileString.
        readyState values:
        0 (Uninitialized) The object has been created, but not initialized (the open method has not been called). 
        1 (Open) The object has been created, but the send method has not been called.  
        2 (Sent) The send method has been called. responseText is not available. responseBody is not available.  
        3 (Receiving) Some data has been received. responseText is not available. responseBody is not available.  
        4 (Loaded) All the data has been received. responseText is available. responseBody is available.  
        */
        if (objXmlHttp.readyState == 4) {
            if (objXmlHttp.status == 200) {
                //expects a simple text response.
                //If 'error' appears in the response, an alert is displayed
                var strResponse = objXmlHttp.responseText;
                if (strResponse.indexOf('alert(\'Error') <= 0) {
                    try {
                        eval(strResponse);
                    }
                    catch (e) {
                        alert(e.description);
                        alert(strResponse);
                    }
                }
                else if (strResponse != "") {
                    //show the error message
                    alert("Error: " + strResponse);
                }
            }
        }
    }
    //This will run the file in strFileString, which is where all the work gets done.
    //eg. objXmlHttp.open("GET","airfares-match.asp?IataFrom=" + strDeparture);
    objXmlHttp.open("GET", strFileString);
    objXmlHttp.send(null);
    return blnSuccess;
}


/**********************************************************************************************************
*Author:		Mark Evans
*Date:			18 May 2009
*Overview:		Opens an Ajax object and uses this to open and execute the file specified, sending any
*               return values through to the nominated callback function.
**********************************************************************************************************/
function Ajax_WriteToPageWithCallback(strFileString, CallbackFunction)
{
    //alert(strFileString);
    var blnSuccess;
    var objXmlHttp;
    //Create the object
    objXmlHttp = Ajax_OpenJavasctiptobjXmlHttpObject()

    objXmlHttp.onreadystatechange = function() 
    {
        /*
        This essentially is a listener waiting for a response from the File in strFileString.
        readyState values:
        0 (Uninitialized) The object has been created, but not initialized (the open method has not been called). 
        1 (Open) The object has been created, but the send method has not been called.  
        2 (Sent) The send method has been called. responseText is not available. responseBody is not available.  
        3 (Receiving) Some data has been received. responseText is not available. responseBody is not available.  
        4 (Loaded) All the data has been received. responseText is available. responseBody is available.  
        */
        if (objXmlHttp.readyState == 4) 
        {
            if (objXmlHttp.status == 200) 
            {
                //expects a simple text response.
                //If 'error' appears in the response, an alert is displayed
                var strResponse = objXmlHttp.responseText;
                if (strResponse.indexOf('alert(\'Error') <= 0) {
                    try 
                    {
                        //eval(strResponse);
                        CallbackFunction(strResponse);
                    }
                    catch (e) 
                    {
                        alert(e.description);
                        alert(strResponse);
                    }
                }
                else if (strResponse != "") 
                {
                    //show the error message
                    alert("Error: " + strResponse);
                }
            }
        }
    }
    //This will run the file in strFileString, which is where all the work gets done.
    //eg. objXmlHttp.open("GET","airfares-match.asp?IataFrom=" + strDeparture);
    objXmlHttp.open("GET", strFileString);
    objXmlHttp.send(null);
    return blnSuccess;
}
