
//the ajax class
//main function is CallServer - this function get the url to retrieve the data from
//the class expects that the server will return xml and send it to the user defined 
//function "handleServerXmlData(xmlDoc)"
var EBAjax = 
{	
	//function that get url and send method and expect to get
	//xml source to load in the xml document
	ServerRequest : function(strMethod,strUrl,strPostParams,objCallback)
	{
		var xmlHttpObj = null;
		try
		{
			
			xmlHttpObj =	this.GetXmlHttpRequest();
			if(xmlHttpObj != null)
			{
				xmlHttpObj.open(strMethod,strUrl,true);
				xmlHttpObj.onreadystatechange = function()
				{
					
					if(xmlHttpObj.readyState == 4)
					{
						var strSourceXml = xmlHttpObj.responseText;
						var xmlDoc = null;
						xmlDoc = EBXmlHandler.GetXmlDocument(strSourceXml);
						
						if (typeof(objCallback) != 'function')
						{
							ServerResponse(xmlDoc);
						}
						else
						{
							objCallback(xmlDoc);
						}
					}
				};
				
				if(strMethod.toUpperCase() == "POST")
				{
					//check if the postparams are null(solves firefox-windows 2003 bug!)
					if(strPostParams == null)
					{
						strPostParams = 'post=get';
					}
					xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				}
				xmlHttpObj.send(strPostParams);
			}
			else
			{
				this.ExecuteError("Xml Http Request object could not be created!");
			}	
		}
		catch(err)
		{
			this.ExecuteError(err.message);
		}
	},
	
	ServerRequestJson : function(strMethod,strUrl,strPostParams,objCallback)
	{
		var xmlHttpObj = null;
		try
		{
			
			xmlHttpObj =	this.GetXmlHttpRequest();
			if(xmlHttpObj != null)
			{
				xmlHttpObj.open(strMethod,strUrl,true);
				xmlHttpObj.onreadystatechange = function()
				{
					
					if(xmlHttpObj.readyState == 4)
					{
						var strSourceXml = xmlHttpObj.responseText;
						objCallback(strSourceXml);
					}
				};
				
				if(strMethod.toUpperCase() == "POST")
				{
					//check if the postparams are null(solves firefox-windows 2003 bug!)
					if(strPostParams == null)
					{
						strPostParams = 'post=get';
					}
					xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				}
				xmlHttpObj.send(strPostParams);
			}
			else
			{
				this.ExecuteError("Xml Http Request object could not be created!");
			}	
		}
		catch(err)
		{
			this.ExecuteError(err.message);
		}
	},
	
	//function that create the xml http object
	//if the function is not succeed to create null is returned
	GetXmlHttpRequest : function()
	{
		var xmlHttpObj = null;
		//check for evil Microsoft
		try 
		{
			xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch(errMain) 
		{
			try 
			{
				xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (err) 
			{
				xmlHttpObj = null;
			}
		}
		//check for mozilla suit:
		if(xmlHttpObj == null && typeof XMLHttpRequest != 'undefined') 
		{
			xmlHttpObj = new XMLHttpRequest();
		}
		
		return xmlHttpObj;
	},
	
	ExecuteError : function(strErrMsg)
	{
		/*
		try
		{
			ServerError(strErrMsg);
		}
		catch(err)
		{
			window.status = strErrMsg;
		}*/
	}
}


//Xml handler class - the GetXmlDocument is calling from the xmlHttpRequest object
//when it synchronic finishing to load the page from the server
var EBXmlHandler = 
{
	//get the xml document object according to the browser!
	GetXmlDocument : function(strXml)
	{
		var xmlDoc = null;
		var strXmlError = "";
		
		if(document.implementation.createDocument)
		{ 
			// Mozilla, create a new DOMParser 
			var domParser = new DOMParser(); 
			
			xmlDoc = domParser.parseFromString(strXml, "text/xml"); 
			if(xmlDoc.documentElement.tagName == "parsererror")// xml error
			{
				xmlDoc = null
			}
			else
			{
				return xmlDoc;
			}
		} 
		else if(window.ActiveXObject)
		{
			// Internet Explorer, create a new XML document using ActiveX 
			// and use loadXML as a DOM parser. 
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM") 
			xmlDoc.async = false; 
			xmlDoc.resolveExternals = false;
			xmlDoc.loadXML(strXml);
			var xmlError = xmlDoc.parseError;
			if (xmlError.errorCode == 0)
			{
				return xmlDoc;
			}
			else
			{
				xmlDoc = null
				strXmlError = xmlError.reason;
			}
		}
		
		if(xmlDoc == null)
		{
			EBAjax.ExecuteError("The xml data could not be parsed! " + strXmlError);
			return null;
		}
	},
	
	GetStringXml : function(xmlNode)
	{
		var strXml			= "";
		
		if(document.implementation && document.implementation.createDocument)//Mozilla
		{ 
			var objSerializer = new XMLSerializer();
			strXml = objSerializer.serializeToString(xmlNode);
		}
		else if(window.ActiveXObject)//IE
		{
			strXml = xmlNode.xml
		}
		
		return strXml;
	},
	
	GetXmlNodeValue : function(xmlNode)
	{
		if(xmlNode.childNodes.length == 1)
		{
			return xmlNode.childNodes[0].nodeValue;
		}
		
		return "";
	},
	
	ToLegalXml : function(strXml)
	{
		strXml = strXml.replace(/&/g, '&amp;');
		strXml = strXml.replace(/</g, '&lt;');
		strXml = strXml.replace(/>/g, '&gt;');
		strXml = strXml.replace(/\"/g, '&quot;');
		strXml = strXml.replace(/\'/g, '&apos;');
		return strXml;
	},
	
	FromLegalXml : function(strXml)
	{
		strXml = strXml.replace(/&amp;/g, '&');
		strXml = strXml.replace(/&lt;/g, '<');
		strXml = strXml.replace(/&gt;/g, '>');
		strXml = strXml.replace(/&quot;/g, '\"');
		strXml = strXml.replace(/&apos;/g, '\'');
		return strXml;
	},
	
	CreateXmlTextNode : function(xmlDoc,strTagName,strValue)
	{
		if (strValue == null) {strValue = '';}
		
		var XML_NODE = xmlDoc.createElement(strTagName);
		var XML_VALUE = this.CreateXmlTextValue(xmlDoc,strValue);
		XML_NODE.appendChild(XML_VALUE);
		return XML_NODE;
	},
	
	CreateXmlTextValue : function(xmlDoc,strValue)
	{ 
		var XML_VALUE = xmlDoc.createTextNode(strValue);
		return XML_VALUE;
	}

}


