//
// (c) Travel Power Limited 2006-08
//
var com;
if (!com) com={};
else if (typeof com != "object")
	throw new Error("com already exists and is not an object");

if (!com.travelpower) com.travelpower = {}
else if (typeof com.travelpower != "object")
	throw new Error("com.travelpower already exists and is not an object");

// -----------------------------------
// Define com.travelpower.Ajax
// -----------------------------------
//
if (com.travelpower.Ajax)
	throw new Error("com.travelpower.Ajax already exists");

com.travelpower.Ajax = function(thisURL, arrayOfFields) 
{
    /// <summary>Creates an Ajax Object</summary>
    /// <param name="thisURL" type="string">The AJAX Service URL</param>
    /// <param name="arrayOfFields" type="string">Pairs of Node Name + Node Data</param>
    /// <returns type="object">new Ajax Object</returns>

    //alert("com.travelpower.Ajax:"+arguments.length);
	
	this.Command="";
	this.URL="";
	
	if (arguments.length > 0)
	{
		this.SetURL(arguments[0]);

		for (feDataField=1; feDataField<arguments.length; feDataField++)
		{
			this.AddFields(arguments[feDataField],arguments[feDataField+1]);
			
			feDataField++;
		}
	}
	//alert(this.Command);
}

com.travelpower.Ajax.Encode = function(fieldName, thisText) 
{
	return(fieldName+"="+thisText.replace(/&/g,"%26"));
}

com.travelpower.Ajax.prototype.SetURL = function(thisURL) 
{
    /// <summary>Sets the traget URL</summary>
    /// <param name="thisURL" type="string">The AJAX Service URL</param>
    /// <returns>NULL</returns>
    if (arguments.length > 0)
	{
		this.URL=arguments[0];
	}
}

com.travelpower.Ajax.prototype.AddFields = function(arrayOfFields) 
{
    /// <summary>Sets the parameters</summary>
    /// <param name="arrayOfFields" type="string">Pairs of Node Name + Node Data</param>
    /// <returns>NULL</returns>
    if (arguments.length > 1)
	{
		for (feAddFields=0; feAddFields<arguments.length; feAddFields++)
		{
			if (this.Command=="")
				this.Command=com.travelpower.Ajax.Encode(arguments[feAddFields],arguments[feAddFields+1]);
			else
				this.Command=this.Command + "&" + com.travelpower.Ajax.Encode(arguments[feAddFields],arguments[feAddFields+1]);
			
			feAddFields++;
		}
	}
}

com.travelpower.Ajax.prototype.PostFormInSync = function(callbackFunction) 
{
    /// <summary>Posts the Form in Synchronus mode</summary>
    /// <param name="callbackFunction" type="function">The Callback function</param>
    /// <returns>NULL</returns>
    this.z_PostForm(false, callbackFunction);
}

com.travelpower.Ajax.prototype.PostFormASync = function(callbackFunction) 
{
    /// <summary>Posts the Form in Asynchronus mode</summary>
    /// <param name="callbackFunction" type="function">The Callback function</param>
    /// <returns>NULL</returns>
    this.z_PostForm(true, callbackFunction);
}

com.travelpower.Ajax.prototype.z_PostForm = function(syncType,callbackFunction) 
{
    /// <summary>PRIVATE Posts the Form in syncType mode</summary>
    /// <param name="syncType" type="string">Sync Type</param>
    /// <param name="callbackFunction" type="function">The Callback function</param>
    /// <returns>NULL</returns>
    var xmlHttpReq = false;
    var self = this;

    // Mozilla/Safari
    if (window.XMLHttpRequest) 
    {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) 
    {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    self.xmlHttpReq.open('POST', this.URL, syncType);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    
    self.xmlHttpReq.onreadystatechange = function() 
    {
    	//alert("TPAjaxRequest:"+"self.xmlHttpReq.readyState:"+self.xmlHttpReq.readyState);
        if (self.xmlHttpReq.readyState == 4) 
        {
            var lResponse=self.xmlHttpReq.responseText;
            callbackFunction(lResponse);
        }
    }
    //alert("TPAjaxRequest:"+"send");
    self.xmlHttpReq.send(this.Command);
    //alert("TPAjaxRequest:"+"sent");
}
//
// Static procedures - do not use + phase out
//
var AjaxURL = new String();
var AjaxXMLRequest = new String();
var AjaxXMLResponse = new String();

function TPAjaxRequest(thisFunction, asyncRequest)
{
    //alert("TPAjaxRequest:"+"asyncRequest:"+asyncRequest);

	if (asyncRequest==null)
		asyncRequest=true;

    //alert("TPAjaxRequest:"+"asyncRequest:"+asyncRequest);
		
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', AjaxURL, asyncRequest);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
    	//alert("TPAjaxRequest:"+"self.xmlHttpReq.readyState:"+self.xmlHttpReq.readyState);
        if (self.xmlHttpReq.readyState == 4) {
            AjaxXMLResponse=self.xmlHttpReq.responseText;
            thisFunction(AjaxXMLResponse);
        }
    }
    //alert("TPAjaxRequest:"+"send");
    self.xmlHttpReq.send(AjaxXMLRequest);
    //alert("TPAjaxRequest:"+"sent");
}

//alert("TPAjax OK");
//