//Author: Pratik.Gohil
function Ajax()
{
	var self = this;
	
	this.Url = "";
	this.Params = "";
	this.Method = "POST";		
	this.CallBackFunction = "";	
	this.IsAsync = true;
	this.LoaderContainer = null;
	
	
	this.ResponseXML = null;
	this.XMLDoc = null;
	this.ResponseText = "";
	this.ResponseHTML = "";
	this.ErrorMessage = "";
	this.NotificationMessage = "";
	this.SuccessMessage = "";
	
	this.XMLHttpRequest = null;
	
	//When request is in loading state
	this.OnLoading = function() 
	{
	
	};
	
	//When request is in loaded state
	this.OnLoaded = function() 
	{
	
	};
	
	//When request is in loaded state
	this.OnInteractive = function()
	{
	
	};
	
	//After completion invoke the call back function
	this.OnCompletion = function() 
	{		
		if(this.CallBackFunction)
		{	
		    this.CallBackFunction.Ajax = self;	
			this.CallBackFunction();
		}
	};	
	
	// Show loader
	this.ShowLoader = function()
	{
		if(this.LoaderContainer)
		{
			this.LoaderContainer.style.display = "";
			this.LoaderContainer.innerHTML = "<div style=\"margin-top:10px;margin-bottom:10px;vertical-align:middle;text-align:center;width:100%;color:red;font-family:Verdana;font-size:10px;font-weight:bold;\">Please wait...<br/><img src=\"http://www.tabtab.com/App_Themes/UserThemeNew/Img/loading.gif\" /></div>";
		}
		else
			alert("You have not defined loading container.");
	};
	
	// Show loader
	this.HideLoader = function()
	{
		if(this.LoaderContainer)
		{
			this.LoaderContainer.style.display = "none";
			this.LoaderContainer.innerHTML = "";
		}
		else
			alert("You have not defined loading container.");
	};
		
	// This method has been used to post/get data content.
	this.Send = function()
	{	
		if( window.ActiveXObject )
			{this.XMLHttpRequest = new ActiveXObject( "Microsoft.XMLHTTP" );}
		else
			{this.XMLHttpRequest = new XMLHttpRequest();}	
		
		if (this.XMLHttpRequest) 
		{				
			if (this.Method == "GET") 
				{this.XMLHttpRequest.open(this.Method, this.Url, this.IsAsync);}
			else 
				{this.XMLHttpRequest.open(this.Method, this.Url, this.IsAsync);}
				
			if (this.Method == "POST")
			{
				try 
				{
					this.XMLHttpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded')  
				} catch (e) {}
			}			
				
			if(this.Params)
				{this.XMLHttpRequest.send(this.CreateParams(this.Params));}
			else
				{this.XMLHttpRequest.send(null);}
			this.XMLHttpRequest.onreadystatechange = function() 
			{
				switch (self.XMLHttpRequest.readyState)
				{
					case 1:
						self.OnLoading();
						break;
					case 2:
						self.OnLoaded();
						break;
					case 3:
						self.OnInteractive();
						break;
					case 4:						
						self.ResponseText = self.XMLHttpRequest.responseText;						
						self.ResponseXML = self.XMLHttpRequest.responseXML;
						self.ResponseHTML = self.GetResponseHTML();						
						self.OnCompletion();						
						break;
				}
			};
		}		
	};
	
	//Function used to get response text
	this.GetResponseText = function()
	{
		return this.ResponseText;	
	};
	
	//Function used to get response xml document
	this.GetResponseDocument = function()
	{
		return this.ResponseXML;	
	};
	
	//Function used to validate status
	this.HasError = function()
	{			
		try
		{
			self.ErrorMessage = "";
			self.XMLDoc = this.ResponseXML;			
			if(self.XMLDoc.getElementsByTagName("Error").length > 0)
			{
				this.ErrorMessage = self.XMLGetNodeValue("Error");
				return true;
			}
			else				
				{return false;}
		}
		catch(e){return true;}
		finally {}
	};
	
	//Function used to validate status
	this.HasNotification = function()
	{			
		try
		{
			self.NotificationMessage = "";
			self.XMLDoc = this.ResponseXML;			
			if(self.XMLDoc.getElementsByTagName("Notification").length > 0)
			{
				this.NotificationMessage = self.XMLGetNodeValue("Notification");
				return true;
			}
			else				
				{return false;}
		}
		catch(e){return false;}
		finally {}
	};
	
	//Function used to validate status
	this.HasSuccess = function()
	{			
		try
		{
			self.SuccessMessage = "";
			self.XMLDoc = this.ResponseXML;			
			if(self.XMLDoc.getElementsByTagName("Success").length > 0)
			{
				this.SuccessMessage = self.XMLGetNodeValue("Success");
				return true;
			}
			else				
				{return false;}
		}
		catch(e){return false;}
		finally {}
	};
	
	//Function used to get response html
	this.GetResponseHTML = function()
	{
		try
		{
			self.XMLDoc = this.ResponseXML;		
			if(!this.HasError())
			{	
				return self.XMLGetNodeValue("Control");
			}
			else				
				{return "";}	
		}
		catch(e) 
			{return "";}
		finally {}
	};
	
	this.XMLGetNode = function(nodeName, index)
	{
		if(self.XMLDoc)
		{
			if(self.XMLDoc.getElementsByTagName(nodeName).length > 0 && index)
				{ return self.XMLDoc.getElementsByTagName(nodeName)[index];}
			else if(self.XMLDoc.getElementsByTagName(nodeName).length > 0 )
				{ return self.XMLDoc.getElementsByTagName(nodeName)[0];}
			else
				{return null;}	
		}	
	};
	
	//Get text value of given node IE, Mozilla, Firefox, Netscape, opera
	this.XMLGetNodeValue = function(nodeName, index)
	{
		var xmlNode = null;
		
		try
		{	
			xmlNode = self.XMLGetNode(nodeName, index);
			if(!xmlNode)
				{return null;}
			else if (typeof xmlNode == 'string') 
				{return xmlNode;}
			else if (typeof xmlNode.textContent != 'undefined') 
				{return xmlNode.textContent;}
			else if (typeof xmlNode.innerText != 'undefined')
				{return xmlNode.innerText;}
			else if (typeof xmlNode.text != 'undefined') 
				{return xmlNode.text;}
			else if(typeof xmlNode.value != 'undefined')
				{return xmlNode.value;}
			else
				{return null;}
		}
		catch(e)
		{ return null;}
		finally
		{ xmlNode = null;}
	};
	
	// Create posted data
	this.CreateParams = function( nodes )
	{		
		var value = "";
		var postedParams = "";	
		for( theNode in nodes )
		{
			value = nodes[theNode].toString();			
			value = value.replace(/&/gi, " ");
			postedParams += "&" + theNode + "=" + value;
		}
		postedParams = encodeURI(postedParams);	
		
		value = null;
		return postedParams.substring(1);
	};
};

Ajax.LoaderContainer = null;

// Static method for displaying loader
Ajax.ShowLoader = function()
{
	if(Ajax.LoaderContainer)
	{
		Ajax.LoaderContainer.style.display = "";
		Ajax.LoaderContainer.innerHTML = "<div style=\"margin-top:10px;margin-bottom:10px;vertical-align:middle;text-align:center;width:100%;color:red;font-family:Verdana;font-size:10px;font-weight:bold;\">Please wait...<br/><img src=\"http://www.tabtab.com/App_Themes/UserThemeNew/Img/loading.gif\" /></div>";
	}
	else
		alert("You have not defined loading container.");
};

// Show loader
Ajax.HideLoader = function()
{
	if(Ajax.LoaderContainer)
	{
		Ajax.LoaderContainer.style.display = "none";
		Ajax.LoaderContainer.innerHTML = "";
	}
	else
		alert("You have not defined loading container.");
};