var MAX_INT = 2147483648;//2,147,483,648
var MAX_MONEY = 922337203685476;//922,337,203,685,477.5807

//-----------------------------------------------------------------------------------------------------------------
function getXMLObject(xmlString)
{
    var xml;
    // code for IE
    if (window.ActiveXObject)
    {
        xml = new ActiveXObject("Microsoft.XMLDOM");
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument)
    {
        xml = document.implementation.createDocument("","",null);
    }

    // code for IE
    if (window.ActiveXObject)
      {
      var doc=new ActiveXObject("Microsoft.XMLDOM");
      doc.async="false";
      xml.loadXML(xmlString);
      }
    // code for Mozilla, Firefox, Opera, etc.
    else
      {
      var parser=new DOMParser();
      xml=parser.parseFromString(xmlString,"text/xml");
      }            
    
    
    return xml;
}

//-----------------------------------------------------------------------------------------------------------------
// Will call LoadFunction once the entire page is loaded
// - LoadFunction - string - function to be called when the page is loaded
//-----------------------------------------------------------------------------------------------------------------
function CrossBrowser_AttachLoadEvent(LoadFunction)
{
	try
	{
		if (window.addEventListener)
			eval("window.addEventListener('load', " + LoadFunction + ", false)");
		else
			eval("window.attachEvent('onload', " + LoadFunction + ")");
	}
	catch(e)
	{ ; }	
}
//-----------------------------------------------------------------------------------------------------------------
// Will call LoadFunction once the entire page is loaded
// - LoadFunction - string - function to be called when the page is loaded
//-----------------------------------------------------------------------------------------------------------------
function CrossBrowser_AttachUnLoadEvent(UnLoadFunction)
{
	try
	{
		if (window.addEventListener)
		{
			eval("window.addEventListener('beforeunload', " + UnLoadFunction + ", false)");
		}
		else
		{
			eval("window.attachEvent('onbeforeunload', " + UnLoadFunction + ")");
		}
	}
	catch(e)
	{ ; }	
}

//-----------------------------------------------------------------------------------------------------------------
// The array to hold all XMLHTTP Requests
// Exclusively used by CrossBrowser_GetAsyncXML function
//-----------------------------------------------------------------------------------------------------------------
var CrossBrowser_XMLRequests = new Array();

//-----------------------------------------------------------------------------------------------------------------
// Custom XML Request object to hold other data
// Exclusively used by CrossBrowser_GetAsyncXML function
// - XMLRequest - object = the xmlhttp request object
// - SuccessFunction - string = the function to call when the request is complete
// - SuccessFunctionParam - object = the parameter to pass to the SuccessFunction (can be null or an array)
//-----------------------------------------------------------------------------------------------------------------
function CrossBrowser_XMLRequest(XMLRequest, SuccessFunction, SuccessFunctionParam)
{
	this.XMLRequest = XMLRequest;
	this.SuccessFunction = SuccessFunction;
	this.SuccessFunctionParam = SuccessFunctionParam;
	this.SuccessFunctionCalled = false;	
}

//-----------------------------------------------------------------------------------------------------------------
// Send a cross browser, asynchronous, xmlhttp call.
// URL - string = the url of the xml to fetch
// SuccessFunction - string = the funciton to call when the xml data is ready
// SuccessFunctionParam - object = the paramater to pass to the SuccessFunction - optional
//-----------------------------------------------------------------------------------------------------------------
function CrossBrowser_GetAsyncXML(URL, SuccessFunction, SuccessFunctionParam) 
{
	try
	{
		var XMLRequest = false;
		// branch for native XMLHttpRequest object
		if (window.XMLHttpRequest)
		{
			XMLRequest = new XMLHttpRequest();
			XMLRequest.onreadystatechange = CrossBrowser_XMLRequestReady;
			XMLRequest.open("GET", URL, true);
            XMLRequest.setRequestHeader("If-None-Match",Date());
            XMLRequest.setRequestHeader("Cache-Control","no-cache,max-age=0");
            XMLRequest.setRequestHeader("Pragma","no-cache");
			XMLRequest.send(null);  
		}
		// branch for IE/Windows ActiveX version
		else if (window.ActiveXObject)
		{
			XMLRequest = new ActiveXObject("Msxml2.XMLHTTP.3.0");
			if (XMLRequest) {
				XMLRequest.onreadystatechange = CrossBrowser_XMLRequestReady;
				XMLRequest.open("GET", URL, true);
                XMLRequest.setRequestHeader("If-None-Match",Date());
                XMLRequest.setRequestHeader("Cache-Control","no-cache,max-age=0");
                XMLRequest.setRequestHeader("Pragma","no-cache");
				XMLRequest.send();
			}
		}
		if (XMLRequest)
		{
			var oCrossBrowser_XMLRequest = new CrossBrowser_XMLRequest(XMLRequest, SuccessFunction, SuccessFunctionParam);
			CrossBrowser_XMLRequests.push(oCrossBrowser_XMLRequest);
		}
	}
	catch(e)
	{; }	
}

//-----------------------------------------------------------------------------------------------------------------
// Called when an XMLHTTP request has had a state change
// Exclusively used by CrossBrowser_GetAsyncXML function
// When ready, calls the SuccessFunction with the SuccessFunctionParam
//-----------------------------------------------------------------------------------------------------------------
function CrossBrowser_XMLRequestReady() 
{
	try
	{
		for (var i = 0; i < CrossBrowser_XMLRequests.length; i++)
		{	
			if (CrossBrowser_XMLRequests[i].XMLRequest.readyState == 4) // ready
			{
				if (CrossBrowser_XMLRequests[i].XMLRequest.status == 200) // no error
				{
					if (!CrossBrowser_XMLRequests[i].SuccessFunctionCalled)
					{
						CrossBrowser_XMLRequests[i].SuccessFunctionCalled = true;
						eval(CrossBrowser_XMLRequests[i].SuccessFunction + "(CrossBrowser_XMLRequests[i].XMLRequest, CrossBrowser_XMLRequests[i].SuccessFunctionParam)");
					}
				}
			}
		}
	}
	catch(e)
	{ ; }	
}


//-----------------------------------------------------------------------------------------------------------------
function CrossBrowser_GetXML(URL, PostXML) 
{
	var xmlhttp=CrossBrowser_GetXMLHTTPRequest(URL, PostXML);
	if(xmlhttp)
		return xmlhttp.responseXML;
	else
		return null;
}
//-----------------------------------------------------------------------------------------------------------------
function CrossBrowser_GetHTML(URL, PostXML) 
{
	var xmlhttp=CrossBrowser_GetXMLHTTPRequest(URL, PostXML);

	if(xmlhttp&&xmlhttp.status=="200")
		return xmlhttp.responseText;
	else
		return null;
}
//-----------------------------------------------------------------------------------------------------------------
function CrossBrowser_GetXMLHTTPRequest(URL, PostXML) 
{
	try
	{
		var XMLRequest = false;
		var PostGet="Get";
		if(PostXML!=null)
			PostGet="POST";

		// branch for native XMLHttpRequest object
		if (window.XMLHttpRequest)
		{
			XMLRequest = new XMLHttpRequest();
			//XMLRequest.onreadystatechange = CrossBrowser_XMLRequestReady;
			XMLRequest.open(PostGet, URL, false);
            XMLRequest.setRequestHeader("If-None-Match",Date());
            XMLRequest.setRequestHeader("Cache-Control","no-cache,max-age=0");
            XMLRequest.setRequestHeader("Pragma","no-cache");
			XMLRequest.send(PostXML);  
		}
		// branch for IE/Windows ActiveX version
		else if (window.ActiveXObject)
		{
			XMLRequest = new ActiveXObject("Msxml2.XMLHTTP.3.0");
			if (XMLRequest) {
				//XMLRequest.onreadystatechange = CrossBrowser_XMLRequestReady;
				XMLRequest.open(PostGet, URL, false);
                XMLRequest.setRequestHeader("If-None-Match",Date());
                XMLRequest.setRequestHeader("Cache-Control","no-cache,max-age=0");
                XMLRequest.setRequestHeader("Pragma","no-cache");
				XMLRequest.send(PostXML);
			}
		}
		return XMLRequest;
	}
	catch(e)
	{ return null; }	
}

//-----------------------------------------------------------------------------------------------------------------
function toBit(p_value)
{
    var ReturnValue=0;

    try{
        switch(p_value)
        {
            case true:
            case "true":
            case "1":
                ReturnValue=1;
                break;
        }
    }catch(e){;}
    
    return ReturnValue;
}

//-----------------------------------------------------------------------------------------------------------------
function isblank(s) {
	for(var i=0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

//-----------------------------------------------------------------------------------------------------------------
function trim(s) {
    return s.replace(/^\s+|\s+$/g,"");
}

//-----------------------------------------------------------------------------------------------------------------
function maxLength(field) {
    var maxlength = field.getAttribute("maxlength");
    if (field.value.length >= maxlength) {
        event.returnValue = false;
        return false;
    }
}

//-----------------------------------------------------------------------------------------------------------------
function maxLengthPaste(field) {
    var maxlength = field.getAttribute("maxlength");
    event.returnValue = false;
    if ((field.value.length + window.clipboardData.getData("Text").length) > maxlength) {
        return false;
    }
    event.returnValue = true;
}

//-----------------------------------------------------------------------------------------------------------------
function cookie_create(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

//-----------------------------------------------------------------------------------------------------------------
function cookie_read(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//-----------------------------------------------------------------------------------------------------------------
function cookie_erase(name) {
	cookie_create(name,"",-1);
}

//-----------------------------------------------------------------------------------------------------------------
function xml_setAttribute (xml, node, name, value)
{
    var attribute = xml.createAttribute(name);
    attribute.value=value;
    node.attributes.setNamedItem(attribute);
}

//-----------------------------------------------------------------------------------------------------------------
function xml_setCDATA (xml, node, value)
{
    node.appendChild(xml.createCDATASection(value));
}

//-----------------------------------------------------------------------------------------------------------------
function setAttribute(obj, name, value)
{
    if(!isblank(value))
        obj.setAttribute(name,value);
}

//-----------------------------------------------------------------------------------------------------------------
String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g,"");}
//-----------------------------------------------------------------------------------------------------------------
String.prototype.ltrim = function() {return this.replace(/^\s+/,"");}
//-----------------------------------------------------------------------------------------------------------------
String.prototype.rtrim = function() {return this.replace(/\s+$/,"");}

if( document.implementation.hasFeature("XPath", "3.0") )
{
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++)
		{
			aResult[i] =  aItems.snapshotItem(i);
		}
		
		return aResult;
	}
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 )
		{
			return xItems[0];
		}
		else
		{
			return null;
		}
	}

	Element.prototype.selectNodes = function(cXPathString)
	{
		if(this.ownerDocument.selectNodes)
		{
			return this.ownerDocument.selectNodes(cXPathString, this);
		}
	}

	Element.prototype.selectSingleNode = function(cXPathString)
	{	
		if(this.ownerDocument.selectSingleNode)
		{
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		}
	}

}

