//Author: Pratik.Gohil

// wraps an XML request so it works in IE and Mozilla/Firefox
// sends data to url with fnc as the callback
// can be async or not
// if o is valid then the xmlHTTPReq is set to the request created in the function
// used by callers who need access to the xmlHTTPReq after the call

function XMLRequest( url, data, fnc, o, async )
{
	 async = async == undefined ? true : async;
	 var xmlHTTPReq;
 	
	 if( BarTab.IE )
		 xmlHTTPReq = new ActiveXObject( "Microsoft.XMLHTTP" );
	 else
		 xmlHTTPReq = new XMLHttpRequest();
 	
	 if( xmlHTTPReq )
	 {
		 if( o ) o.xmlHTTPReq = xmlHTTPReq;
		 if( fnc ) xmlHTTPReq.onreadystatechange = fnc;
		 xmlHTTPReq.open( "POST", url, async );
		 xmlHTTPReq.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		 xmlHTTPReq.send( data );
 		
		 if( fnc && !async && !BarTab.IE )//In IE the fnc() will automatically be invoked in case async = false
			 fnc();		
	 }
 	
	 return xmlHTTPReq;
}

function GetResponseText(errorMessage)
{
    var response = "";
    if( o.xmlHTTPReq.readyState == 4 )
	     {
	        if( o.xmlHTTPReq.status == 200 )
	        {
	          response = o.xmlHTTPReq.responseText.decodeHTML();
	        }	
	        else //error
	        {
	          if(errorMessage)
	          {
	            response = errorMessage //for temporary
	          }
	          else
	          {
	            response = "Error occurred while retrieving data.";
	          }
	        }
	     }
	  else
	  {
	    response = "";
	  }
	  return response;
}

function GetResponseDocument()
{
	var response = null;
	if( o.xmlHTTPReq.readyState == 4 )
	{
		if( o.xmlHTTPReq.status == 200 )
		{
			response = o.xmlHTTPReq.responseXML.documentElement;
		}					        
	}	
	return response;
}

// Create posted data
CreateParams = function( nodes )
{		
	var postedParams = "";	
	for( theNode in nodes )
	{
		postedParams += "&" + theNode + "=" + nodes[theNode];
	}
	postedParams = encodeURI(postedParams);	
	return postedParams.substring(1);
}