/* ---------------------------------------------------------------- */
/* 																	*/
/*							xmlObj.js								*/
/*							---------								*/
/*																	*/
/* ---------------------------------------------------------------- */

/* ---------------------------------------------------------------- */
/* xmlObj constructor												*/
/* ---------------------------------------------------------------- */
function xmlObj (async) 
{
	// data memebers
	this.obj = new ActiveXObject("Microsoft.XMLDOM"); 	// the xml object to operate with
	this.async = async; 							  	// a boolean telling whether the process is async or not

	// methods
	this.init 		 = xmlObj_init;					  	// initializing the xml object
	this.sendRequest = xmlObj_sendRequest;				// sending a request to the server

	this.isEmpty	 = xmlObj_isEmpty;					// checking if we got any xml response

	this.isSuccess 	 = xmlObj_isSuccess;			  	// checking if we got information back 
	this.isError	 = xmlObj_isError;				  	// checking if we got an error back
	this.isInfo	 	 = xmlObj_isInfo;				  	// checking if we got an info back
	this.isRequire	 = xmlObj_isRequire;			  	// checking if we got a require command back
	this.isReLogin	 = xmlObj_isReLogin;				// checking if we got session expire back

	this.reLogin	 = xmlObj_reLogin;			
	this.getErrorMsg = xmlObj_getMessage;				// returning message text
	this.commandNode = xmlObj_commandNode; 				// getting the actual command node
	this.resultCode  = xmlObj_resultCode;				// parsing the result code

	this.getValue    = xmlObj_getValue;
	this.setValue    = xmlObj_setValue;

	this.countNodes	 = xmlObj_countNodes;
}

/* ---------------------------------------------------------------- */
/* xmlObj_init														*/
/*																	*/
/*		Initializing an xml object with the appropriate xml.		*/
/* ---------------------------------------------------------------- */
function xmlObj_init(xmlData) 
{
	this.obj.loadXML ("<?xml version='1.0' encoding='ISO-8859-8' ?>" + xmlData);
}

/* ---------------------------------------------------------------- */
/* xmlObj_sendRequest												*/
/*																	*/
/*		params : url - the url to send the request to				*/
/*				 requestObj - an XML object to be sent if needed	*/
/*																	*/
/* ---------------------------------------------------------------- */
function xmlObj_sendRequest(url, requestObj) 
{
	try 
	{
		var httpObj = new ActiveXObject("Microsoft.XMLHTTP");
		httpObj.open("POST", url, this.async);
		// The previous mime type used was "application/x-www-form-urlencoded", which is 
		// used to pass FORM DATA in HTTP. The correct content type for passing pure XML
		// data is "text/xml"
		httpObj.setRequestHeader("Content-type","text/xml");
		//httpObj.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		httpObj.send(requestObj);

		// Note that "200" means "good" and everything else generally means "bad"...
		// TODO: Change to standard dealing with HTTP statuses (i.e. create constants
		// for the statuses in use)
		if (httpObj.status == 200) {
			this.obj = httpObj.responseXML;
		}
		else {
			this.init (
				" <message> " +
					" <responseType>Error</responseType>" +
					" <message>" + httpObj.status + "</message>" +
				" </message> "
			);
		}
	}
	catch (error) 
	{
		this.init (	" <message> " +
							" <msg>Error In Communication. Please Try Again Later</msg> " +
					" </message> ");
	}
//alert (httpObj.statusText);
//	this.obj = httpObj.responseXML;
}

/* ---------------------------------------------------------------- */
/* xmlObj_isEmpty													*/
/* ---------------------------------------------------------------- */
function xmlObj_isEmpty ()
{
	if (this.obj == null || this.obj.xml == "") 
	{
		this.init(	" <local> " +
						" <response> "+
							" <responseType>Error</responseType> " +
							" <result>3000</result> " +
							" <message>No information received. please try again later</message> " +
						" </response> "+
					" </local> " );
		return true;
	}
	return false;
}

/* ---------------------------------------------------------------- */
/* xmlObj_isSuccess													*/
/* ---------------------------------------------------------------- */
function xmlObj_isSuccess (command)
{
	var isSuccess;

	if (this.isEmpty ())
		isSuccess = false;
	else
	{
		var responseType = this.getValue ("responseType");

		if (responseType == "Success")
		{
			var requestCommand = this.getValue ("command");
			
			isSuccess = (requestCommand == command);
		}
		else
			isSuccess = false;
	}
	return isSuccess;
}

/* ---------------------------------------------------------------- */
/* xmlObj_isError													*/
/* ---------------------------------------------------------------- */
function xmlObj_isError ()
{
	var isError;

	if (this.isEmpty ())
		isError = false;
	else
	{
		var responseType = this.getValue ("responseType");

		isError = (responseType == "Error");
	}
	return isError;
}

/* ---------------------------------------------------------------- */
/* xmlObj_isInfo													*/
/* ---------------------------------------------------------------- */
function xmlObj_isInfo ()
{
	var isInfo;

	if (this.isEmpty ())
		isInfo = false;
	else
	{
		var responseType = this.getValue ("responseType");

		isInfo = (responseType == "Info");
	}
	return isInfo;
}

/* ---------------------------------------------------------------- */
/* xmlObj_isRequire													*/
/* ---------------------------------------------------------------- */
function xmlObj_isRequire ()
{
	var isRequire;

	if (this.isEmpty ())
		isRequire = false;
	else
	{
		var responseType = this.getValue ("responseType");

		isRequire = (responseType == "Require");
	}
	return isRequire;
}

/* ---------------------------------------------------------------- */
/* xmlObj_isReLogin													*/
/* ---------------------------------------------------------------- */
function xmlObj_isReLogin ()
{
	var isReLogin;

	if (this.isEmpty ())
		isReLogin = false;
	else
	{
		var responseType = this.getValue ("responseType");

		isReLogin = (responseType == "SessionExpired");
	}
	return isReLogin;
}

/* ---------------------------------------------------------------- */
/* reLogin															*/
/* ---------------------------------------------------------------- */
function xmlObj_reLogin ()
{
	if (this.isReLogin ())
	{
		relog=commonRelogin();
//logout
	   if(relog==1)
			top.location.replace("login.html");
//relogin
		if(relog==0)
				alert("ok");
	    return true;
	}
    return false;
}

/* ---------------------------------------------------------------- */
/* xmlObj_getMessage												*/
/* ---------------------------------------------------------------- */
function xmlObj_getMessage() 
{
	var msgText = this.getValue ("message");

	if (msgText == "")
		msgText = "An internal error has occured. Please contact the support";

	if (msgText.indexOf ("500") != -1) return "Ashrait XMC service does not respond! Please contact the support";

	return msgText;
	
}

/* ---------------------------------------------------------------- */
/* xmlObj_commandNode												*/
/* ---------------------------------------------------------------- */
function xmlObj_commandNode() 
{
	var commandName = this.getValue("command");
	
	if (commandName == "") return;
	
	var commandNode = this.obj.getElementsByTagName(commandName).item(0);

	return commandNode;
}

/* ---------------------------------------------------------------- */
/* xmlObj_resultCode												*/
/* ---------------------------------------------------------------- */
function xmlObj_resultCode() 
{
	return this.getValue("result");
}

/* ---------------------------------------------------------------- */
/* xmlObj_getValue													*/
/* ---------------------------------------------------------------- */
function xmlObj_getValue (tagName)
{
	var theNode = this.obj.getElementsByTagName(tagName).item(0);

	if (theNode == null) 
		return "";
	else
		return theNode.text;
}

/* ---------------------------------------------------------------- */
/* xmlObj_setValue													*/
/* ---------------------------------------------------------------- */
function xmlObj_setValue (tagName,value)
{
	var theNode = this.obj.getElementsByTagName(tagName).item(0);
    if (theNode != null) 
		theNode.text = value;
}

/* ---------------------------------------------------------------- */
/* xmlObj_countNodes												*/
/* ---------------------------------------------------------------- */
function xmlObj_countNodes () 
{
    if (this.obj != null && this.obj.xml != "") 
		return this.obj.documentElement.childNodes.length;
	else
		return 0;
}
																		
