
var AjaxEnginePage;
var XMLHTTP;

AjaxEnginePage = "AjaxEngine.aspx";

//Global XMLHTTP Request object

//Creating and setting the instance of appropriate XMLHTTP Request object to a "XmlHttp" variable  
function CreateXMLHTTP()
{
	
	try
	{
		XMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XMLHTTP = null;
		}
	}
	//Creating object in Mozilla and Safari 
	if(!XMLHTTP && typeof XMLHttpRequest != "undefined") 
	{
		XMLHTTP = new XMLHttpRequest();
	}
}

function CheckTransactionStatus(a_nTransactionID) 
{	
    //alert(a_nTransactionID);
	// construct the URL 
	var requestUrl =AjaxEnginePage  + "?Action=TS&TID=" + encodeURIComponent(a_nTransactionID);

	CreateXMLHTTP();
	
	// If browser supports XMLHTTPRequest object
	if(XMLHTTP)
	{
		//Setting the event handler for the response
		XMLHTTP.onreadystatechange = GetStatus;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XMLHTTP.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XMLHTTP.send(null);		
	}
}

function SendEmail() 
{	
    var emailtype = '';
    if (document.getElementById('hdnEmailType') != null) {
        emailtype = document.getElementById('hdnEmailType').value;
        emailtype = encodeURIComponent(emailtype);
    }
    var enquiry = '';
    if (document.getElementById('txtMessage') != null) {
        enquiry = document.getElementById('txtMessage').value;
        enquiry = encodeURIComponent(enquiry);
    }
    var emailAddress = '';
    if (document.getElementById('hdnEmailAddress') != null) {
        emailAddress = document.getElementById('hdnEmailAddress').value;
        emailAddress = encodeURIComponent(emailAddress);
    }
    
    var fullName = '';
    if (document.getElementById('hdnFullName') != null) {
        fullName = document.getElementById('hdnFullName').value;
        //alert('fullName: ' + fullName);
        fullName = encodeURIComponent(fullName);
    }
    
    if (enquiry != "")
    {

        // construct the URL 
        //0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678X";
	    var requestUrl = AjaxEnginePage  + "?Action=Email&type=" + emailtype;
        if (emailAddress.length > 0) 
        {
            requestUrl = requestUrl  + "&em=" + emailAddress; 
        }
        if (fullName.length > 0) 
        {
            requestUrl = requestUrl  + "&fn=" + fullName; 
        }
        requestUrl = requestUrl + "&txt=" + enquiry; 
        
	    CreateXMLHTTP();
    	
	    // If browser supports XMLHTTPRequest object
	    if(XMLHTTP)
	    {
		    //Setting the event handler for the response
		    XMLHTTP.onreadystatechange = GetEmailStatus;
    		
		    //Initializes the request object with GET (METHOD of posting), 
		    //Request URL and sets the request as asynchronous.
		    XMLHTTP.open("GET", requestUrl,  true);
    		
		    //Sends the request to server
		    XMLHTTP.send(null);		
	    }
    }
    else
    {
        if (emailtype == 6 || emailtype == 7) // Specific message for updating account or site
        {
            document.getElementById('error_div').innerHTML = "Please enter your proposed changes";
        }
        else // General message
        {
            document.getElementById('error_div').innerHTML = "Please enter your query";
        }
            
    }   

}

function SubmitRegistrationRequest() 
{	
    var l_sUserID = '';
    if (document.getElementById('hdnUserID') != null) {
        l_sUserID = document.getElementById('hdnUserID').value;
        l_sUserID = encodeURIComponent(l_sUserID);
    }
    var l_sProductCsv = '';
    if (document.getElementById('hdnProductCsv') != null) {
        l_sProductCsv = document.getElementById('hdnProductCsv').value;
        l_sProductCsv = encodeURIComponent(l_sProductCsv);
    }
    
    if (l_sProductCsv != "")
    {
        // construct the URL 
	    var requestUrl = AjaxEnginePage  + "?Action=RegRequest";
//        if (l_sProductCsv.length > 0) 
//        {
            requestUrl = requestUrl  + "&ids=" + l_sProductCsv; 
//        }
//        if (l_sUserID.length > 0) 
//        {
            requestUrl = requestUrl  + "&u=" + l_sUserID; 
//        }
       
	    CreateXMLHTTP();
    	
	    // If browser supports XMLHTTPRequest object
	    if(XMLHTTP)
	    {
		    //Setting the event handler for the response
		    XMLHTTP.onreadystatechange = GetRegistrationRequestStatus;
    		
		    //Initializes the request object with GET (METHOD of posting), 
		    //Request URL and sets the request as asynchronous.
		    XMLHTTP.open("GET", requestUrl,  true);
    		
		    //Sends the request to server
		    XMLHTTP.send(null);		
	    }
    }
}

function GetRegistrationRequestStatus()
{

	// To make sure receiving response data from server is completed
	if(XMLHTTP.readyState == 4)
	{
		//Valid Response is received
		if(XMLHTTP.status == 200)
		{			
			var l_oStatusNode= XMLHTTP.responseXML.documentElement.getElementsByTagName('status');
		    var l_sStatus = GetInnerText(l_oStatusNode[0]);
		    document.getElementById('pSuccessMessage').innerHTML = l_sStatus;  
		}
		else //something is wrong 
		{
		    document.getElementById('pSuccessMessage').innerHTML = "An error has occurred sending your requested changes. Please contact onlinelistingtool@failteireland.ie<br /><br />";

		}
	}
}

function GetEmailStatus()
{

	// To make sure receiving response data from server is completed
	if(XMLHTTP.readyState == 4)
	{
		//Valid Response is received
		if(XMLHTTP.status == 200)
		{			
			var l_oStatusNode= XMLHTTP.responseXML.documentElement.getElementsByTagName('status');
		    var l_sStatus = GetInnerText(l_oStatusNode[0]);
		    document.getElementById('pSuccessMessage').innerHTML = l_sStatus;  
		    
		    hideDiv('intro_text');
		    hideDiv('text_input');
		    hideDiv('submit_buttons');
		    hideDiv('error_div');
	        showDiv('divClosebutton'); 
		    
		}
		else //something is wrong 
		{
		    document.getElementById('error_div').innerHTML = "An error has occurred sending your requested changes. Please contact onlinelistingtool@failteireland.ie<br /><br />";
		    hideDiv('intro_text');
		    hideDiv('text_input'); 
		    hideDiv('submit_buttons'); 
	        showDiv('divClosebutton'); 
		}
	}
}

function GetStatus()
{
	// To make sure receiving response data from server is completed
	if(XMLHTTP.readyState == 4)
	{
		//Valid Response is received
		if(XMLHTTP.status == 200)
		{			

			var l_oStatusNode= XMLHTTP.responseXML.documentElement.getElementsByTagName('status');
		    var l_sStatus = GetInnerText(l_oStatusNode[0]);
		    var l_oMessageNode= XMLHTTP.responseXML.documentElement.getElementsByTagName('message');
		    var l_sMessage = GetInnerText(l_oMessageNode[0]);
            //alert('l_sStatus: ' + l_sStatus + ', l_sMessage' + l_sMessage);
            
		    if (l_sStatus == 'Pending') {
		        showDiv('divCartSummary');
		        hideDiv('divConfirmationSuccess');
		        hideDiv('divGoToYourSitesButton');
		        hideDiv('divConfirmationFailure');
		        hideDiv('divConfirmNav');
		        showDiv('divPaymentNav');
		        
		    } else if (l_sStatus == 'Successful') {
		        hideDiv('divCartSummary');
		        showDiv('divConfirmationSuccess');
		        showDiv('divGoToYourSitesButton');
		        hideDiv('divConfirmationFailure');
		        showDiv('divConfirmNav');
		        hideDiv('divPaymentNav');
		       
		    } else if (l_sStatus == 'Failed') {
		        hideDiv('divCartSummary');
		        hideDiv('divConfirmationSuccess');
		        showDiv('divGoToYourSitesButton');
		        setMessage('divMessage', l_sMessage);
		        showDiv('divConfirmationFailure');
		        hideDiv('divConfirmNav');
		        showDiv('divPaymentNav');	        
		    }
		}
		else //something is wrong 
		{
			alert("Could not retreive data from the server" );
		}
	}
}

function showDiv(id) 
{
    try 
    {
	    if (document.getElementById) { // DOM3 = IE5, NS6
		    document.getElementById(id).style.display = 'block';
	    }
	    else {
		    if (document.layers) { // Netscape 4
			    document.id.display = 'block';
		    }
		    else { // IE 4
			    document.all.id.style.display = 'block';
		    }
	    }
	}
	catch(err) 
	{}
}

function setMessage(id, message) 
{
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).innerHTML = message;
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.innerHTML = message;
		}
		else { // IE 4
			document.all.id.innerHTML = message;
		}
	}
}


function hideDiv(id)
{
    try 
    {
	    if (document.getElementById)   // DOM3 = IE5, NS6
	    {
		    document.getElementById(id).style.display = 'none';
	    }
	    else 
	    {
		    if (document.layers) // Netscape 4
		    { 
			    document.id.display = 'none';
		    }
		    else // IE 4
		    { 
			    document.all.id.style.display = 'none';
		    }
	    }
	}
	catch(err) 
	{}
}


function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}



