//<script language="JavaScript" src="xmlhttprequest.js"></script>
 //<script language="JavaScript">
/*---------------------------------------------------------------------------
/ © Copyright 2001 Pentamation Enterprises, Inc., All Rights Reserved.
/ This program is PROPRIETARY and CONFIDENTIAL information of Pentamation
/ Enterprises, Inc., and may not be disclosed or used except as expressly
/ authorized in a license agreement controlling such use and disclosure.
/ Unauthorized use of this program will result in legal proceedings, civil
/ damages, and possible criminal prosecution.
/----------------------------------------------------------------------------
/ System Name: WebSMS
/----------------------------------------------------------------------------
/ Page Name: ValidCommon.js
/----------------------------------------------------------------------------
/ Page Description: Contains client side common functions used for validation
/----------------------------------------------------------------------------
/  REV #      DATE     BY   COMMENTS
/ 1.0.000  02/09/2000  TMH  Initial write.
/--------------------------------------------------------------------------*/

// Method:       BuildRequestDOM
// Description:  This method takes an object, and for each property of the
//				 object, creates a node in an XMLDOM object.
// Parameters:   pobjParams.  An object whose properties are parameters to an ASP page.
// Return Value: XMLDOM Object

// document.prototype.loadXML2 = function(str) {
//	var aDOMParser = new DOMParser();
//	var DOMDocumnt = aDOMParser.parseFromString(str,"text/xml"); 
//	return DOMDocumnt;
//}

function BuildRequestDOM(pobjParams)
{
	var domParams;
	var objParam;
	var strXML;
	var intCtr;
	var strEncodedValue;

	strXML = "<DATA>";
	for (objParam in pobjParams) {
		
		strEncodedValue = pobjParams[objParam];
		strEncodedValue = strEncodedValue.replace(/&/g, "&amp;");
		strEncodedValue = strEncodedValue.replace(/</g, "&lt;");
		strEncodedValue = strEncodedValue.replace(/>/g, "&gt;");
		strEncodedValue = strEncodedValue.replace(/"/g, "&quot;");
		strEncodedValue = strEncodedValue.replace(/'/g, "&apos;");
		strXML += "<" + objParam.toUpperCase() + ">" +
				strEncodedValue + "</" +
				objParam.toUpperCase() + ">";
	}	// End-For: each key
	strXML += "</DATA>";
    if (document.implementation && document.implementation.createDocument && !(window.opera)){ 
		domParams = document.implementation.createDocument("","doc",null);
		var aDOMParser = new DOMParser();
		var DOMDocumnt = aDOMParser.parseFromString(strXML,"text/xml"); 
		domParams.load(DOMDocumnt);
	}
    else if (window.ActiveXObject) {
		domParams = new ActiveXObject("Microsoft.XMLDOM");
		domParams.async = false;
		domParams.loadXML(strXML);
	}
	return domParams
}

// Method:       BuildXMLErrorDoc
// Description:  This method takes a string and builds an XML document from it.
// Parameters:   pstrError.  Text to be displayed as the error message.
// Return Value: XMLDOM Object
function BuildXMLErrorDoc(pstrError)
{
	var domReturn;
	// Create an XMLDOM object
	
    if (document.implementation && document.implementation.createDocument)
	{  var domParams= document.implementation.createDocument("","doc",null);
		domReturn.load("<error>" + pstrError + "</error>");
	}
    else if (window.ActiveXObject) {
	domReturn = new ActiveXObject("Microsoft.XMLDOM");
	domReturn.async = false;
	// Create an entry for the error string
	domReturn.loadXML("<error>" + pstrError + "</error>");
	}
	
	// return the object
	return domReturn;
}

// Method:       RequestValidation
// Description:  This method opens a connection to the server and sends an XML
//				 document object as parameters to the given page.  It expects an
//				 XML DOM Object returned.
// Parameters:   pobjParams.  An object containing the parameters for the page
//				 pstrURL.  The URL of the validation page to be opened.  This method
//					will add the 'http://{server}/{Package}/content' to the URL given.
// Return Value: XMLDOM Object
function RequestValidation(pobjParams,pstrURL,pValue)
{
	var domParams;
	var domReturnValue;
	var objRequest;
	var strCommand;
	var strBase;
	var regContent = /.*\/content/i;
	var winOut;
    domReturnValue = "";
	// Retrieve the current URL
	strCommand = location.href;
	// Strip off anything after /content
	strCommand = strCommand.match(regContent);
	strBase = strCommand;

	// If the remaning string is not 'null'
	if (!(strCommand == "null")) {
		// Determine if URL was passed in
		if (pstrURL == null) {
			// Use standard URL
			strCommand += "/common/FieldValidation.asp";
		} else {
			// Use passed in URL
			strCommand = pstrURL; //AJP test
		}
		// Build Parameters XML DOM object
		domParams = BuildRequestDOM(pobjParams);

		// Check if an error occured parsing the parameters object
		if (1==1) {   
		//AJPif (domParams.parseError.errorCode == 0) {  < fix this
			// Create an XML Request object, Open a connection to server, and send parameters
            
   
    if (document.implementation && document.implementation.createDocument){
		 	objRequest = new XMLHttpRequest(); 
		 	if (objRequest) {
       		objRequest.open("POST",strCommand,false); 
       		if (!(window.opera)){
      	    objRequest.setRequestHeader('Content-Type', 'text/xml');
      	    }
		    objRequest.send("<DATA><DENY>" + pValue + "</DENY></DATA>");
		    if (objRequest.responseText == "") 
		    { 
		    domReturnValue = "NOTHING";
		    }
		    else
		    {
		    domReturnValue = objRequest.responseText;
		    }
		    }
	}else if (window.ActiveXObject) {
			objRequest = new ActiveXObject("Microsoft.XMLHTTP");
			objRequest.open("POST",strCommand,false);
			objRequest.send(domParams);
    }

			// If the server says everything is OK
			if (domReturnValue != "")
			{
			return domReturnValue;
			}
			else
			{
			if (objRequest.status == 200) { 
				// Get the returned XML from the Request object
 			domReturnValue = objRequest.responseText;
				// If there was an error parsing the XML
			} else {	// Server said there was a problem
				winOut = window.open();
				winOut.document.write(objRequest.responseText);
				winOut.document.close();
				domReturnValue = BuildXMLErrorDoc("An error occured on the server: " + location.host + " Status: " + objRequest.status + "  Description: " + objRequest.statusText);
			}	// End-If: Server said everything was OK
			}
		} else {	// An error occured parsing the parameters object
     		 //AJP domReturnValue = BuildXMLErrorDoc("Error building PARAM list.  Error: " + domParams.parseError.errorCode + "  Reason: " + domParams.parseError.reason);
		}	// End-If: Error occured parsing the paramters object
	} else {	// Remaining string was 'null'
		domReturnValue = BuildXMLErrorDoc("Cannot build URL for validation page.");
	}	// End-If: Remaining string was not 'null'
	return domReturnValue;
}

// Method:       CheckXMLDOMforError
// Description:  This method take an XMLDOM object and determines if an ERROR node exists.
//				 If so, it displays the error text to the user.  This method returns true
//				 if an ERROR node exists, and false otherwise.
// Parameters:   pdomDocument.  The XMLDOM to be checked
// Return Value: true/false.
function CheckXMLDOMforError(pdomDocument)
{
	var nlError;
	var blnReturnValue = false;
	var lngErrNumber = null;
	var strErrSource = null;
	var strErrorHTML = "";
	if (document.implementation && document.implementation.createDocument)
     {
	nlError = pdomDocument.getElementsByTagName("error"); 
	} 
    else if (window.ActiveXObject) {
	nlError = pdomDocument.selectSingleNode("error");
	}

	if (!(nlError == null)) {
		blnReturnValue = true;
		 if (document.implementation && document.implementation.createDocument)
        {		
        lngErrNumber = nlError.getElementsByTagName("NUMBER")[0].firstChild.nodeValue;
		strErrSource = nlError.getElementsByTagName("SOURCE")[0].firstChild.nodeValue;
        }
         else if (window.ActiveXObject) {
		lngErrNumber = nlError.getAttribute("NUMBER");
		strErrSource = nlError.getAttribute("SOURCE");
        }

		strErrorHTML = "<TABLE>" +
				"<TR>" +
				"<TD colspan=\"2\">An error has occurred in this application</TD>" +
				"</TR>";
		if (lngErrNumber != null) {
			strErrorHTML += "<TR>" +
					"<TD valign=\"top\"><LABEL>Number:</LABEL></TD>" +
					"<TD>" + lngErrNumber + "</TD>" +
					"</TR>";
		}
		if (strErrSource != null) {
			strErrSource = strErrSource.replace(/->/g, "<BR>&rarr;");
			strErrorHTML += "<TR>" +
					"<TD valign=\"top\"><LABEL>Source:</LABEL></TD>" +
					"<TD>" + strErrSource + "</TD>" +
					"</TR>";
		}
		strErrorHTML += "<TR>" +
				"<TD valign=\"top\"><LABEL>Description:</LABEL></TD>" +
				"<TD>" + nlError.text + "</TD>" +
				"</TR>" +
				"<TR>" +
				"<TD colspan=\"2\">The error was not caused by anything that you did.  " +
				"Please copy the information on this screen and give it to your " +
				"system administrator.</TD>" +
				"</TR>" +
				"</TABLE>";
		SPIConfirm(strErrorHTML);
	}
	return blnReturnValue;
}

// Method:       FieldMayBeNull
// Description:  This method takes an XML Node and assumes it is from the schema section
//				 of an ADO Generated XML document.  It determines if the nullable or
//				 maybenull attributes are set.  It returns true if they are set to true, and
//				 false if they don't exist or are set to false.
// Parameters:   pstrError.  Text to be displayed as the error message.
// Return Value: XMLDOM Object
function FieldMayBeNull(pobjDataTypeNode)
{
	var strFldIsNullable;
	var strFldMayBeNull;
	var blnRetVal = true;
    if (document.implementation && document.implementation.createDocument)
	{
	strFldIsNullable = pobjDataTypeNode.getElementsByTagName("rs:nullable")[0].firstChild.nodeValue;
	strFldMayBeNull = pobjDataTypeNode.getElementsByTagName("rs:maybenull")[0].firstChild.nodeValue;
	}
    else if (window.ActiveXObject) {
	strFldIsNullable = pobjDataTypeNode.getAttribute("rs:nullable");
	strFldMayBeNull = pobjDataTypeNode.firstChild.getAttribute("rs:maybenull");
	}

	if ((strFldIsNullable == "false" || strFldIsNullable == null) &&
		(strFldMayBeNull == "false" || strFldMayBeNull == null)) {
		blnRetVal = false;
	}
	return blnRetVal;
}

//</script>