var isIE = true;
var errorMessage = "";

//Determine browser
if (window.ActiveXObject) {
	isIE = true;
}
else {
	isIE = false;
}

//Load the validation XML
var validateXml = loadXmlFile(g_address + "includes/validate.xml");

function validate(text, type) {
//Takes in a string and type and returns whether or not the string is properly formatted according to the
//properties and the regular expression associated with that type (found in the validate.xml configuration file).
	var isValid;
	var regExp;
	var pattern = readNode(validateXml, "/Types/Type[@Name='" + type + "']/Pattern");
	var ignoreCase = readNode(validateXml, "/Types/Type[@Name='" + type + "']/@IgnoreCase");
	var blackListing = readNode(validateXml, "/Types/Type[@Name='" + type + "']/@BlackList");
	
	//Declare the regular expression with the appropriate ignoreCase argument.
	if (ignoreCase.toUpperCase() == "TRUE") {
		regExp = new RegExp(pattern, "i");
	}
	else {
		regExp = new RegExp(pattern);
	}
	
	//Test the string against the regular expression, taking black-listing vs. white-listing into account for the result.
	if (blackListing.toUpperCase() == "TRUE") {
		isValid = !(regExp.test(text));
	}
	else {
		isValid = regExp.test(text);
	}
	
	return isValid;
}

function checkLength(text, maxLength) {
//Takes in a string and maximum length and returns whether or not the string is a valid
//length.  True = length is ok; false = text is too long.
	var isValid;
	
	if (maxLength == "") {
		isValid = true;
	}
	else {
		maxLength = parseInt(maxLength);
		
		if (text.length > maxLength) {
			isValid = false;
		}
		else {
			isValid = true;
		}
	}

	return isValid;
}

function imposeMaxLength(object, maxLength) {
//Simulates an input box's maxLength property for use with a textArea.  Call it on a textArea
//like so:  onKeyUp="return imposeMaxLength(this, 5);"
	if (object.value.length > maxLength) {
		object.value = object.value.substring(0, maxLength);	//Keep only the required number or characters in the textarea
		object.scrollTop = object.scrollHeight;	//Keep the textarea scrolled to the bottom
	}
}

function loadXmlFile(xmlFile) {
//Load an XML file from the specified path, using the appropriate technique for the browser.
	var xmlDoc = null;	
	
	if (isIE == true) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	    xmlDoc.load(xmlFile);		
	}
	else {	   
        xmlDoc = new XMLHttpRequest();            
		xmlDoc.open("GET", xmlFile, true);
		xmlDoc.send("");
	}
	
	return xmlDoc;
}


function readNode(xmlDoc, xPath) {
//Read an XML node from the XML document with the given xPath, using the appropriate technique for the brower.
	var nodeContents = "";
	
	if (isIE == true) {
		nodeContents = xmlDoc.selectSingleNode(xPath).text;
	}
	else {	    
	    xmlDoc = xmlDoc.responseXML;	        	
	    nodeContents = xmlDoc.evaluate(xPath, xmlDoc, null, XPathResult.STRING_TYPE, null).stringValue;
	}
	return nodeContents;
}

function addError(errorText) {
//Takes in some custom error text and adds it as a formatted line to the overall error message.
	errorMessage += "-- " + errorText + "\n";
}

function attemptSubmit() {
//If no validation errors occurred, this returns true (allowing form submittal). If validation errors
//occurred, this returns false (stopping form submittal) and displays the alert box with error messaging.
	if (errorMessage != "") {
		alert("Please correct the following errors:\n\n" + errorMessage);
		return false;
	}
	else {
		return true;
	}
}