// JScript File

var __emailPattern = "email";
var __emailRegEx = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

//**************************************************************************************
// Object:		Project
// Inherits:	None
Form = function() {
	
	frm = document.getElementsByTagName("form")[0];
	if (frm) {
		this.id = frm.id;
		this.elem = frm;
	} // end if	
} // end object

	//**************************************************************************************
	// Method:		validate().  
	// Returns:		Alerts errors and changes field border colors
	Form.prototype.validate = function(actionQs) {
		
		var outMsg = new Array();
		for (var i=0; i<this.elem.elements.length; i++) {
			var frmElement = this.elem.elements[i];
			var reqErrMsg = this.checkData(frmElement);
			
			// values found, check pattern
			if (reqErrMsg!="") {	
				outMsg[i] = reqErrMsg;			// no value found, store error msg
				frmElement.className = "field_error";
			} else {
				if (frmElement.type=="text") { frmElement.className = "field";  } // end if
			} // end if 	
		} // end for
		
		// No Required or Pattern error message
		if (outMsg.length==0) {
			// Perform confirmation validation.  Compare values with each 
			// other to see if they are equal
			var confirmMsg = this.confirm();
			if (confirmMsg!="") {
				alert(confirmMsg)
				return;
			} // end if
			if (actionQs) {
				var qsSuffix = "?";
				lastChar = this.elem.action.charAt(this.elem.action.length-1);
				qsPrefix = (lastChar == "?") ? "&" : "?"; 
				this.elem.action = this.elem.action + qsPrefix + actionQs;				
			} // end if
			this.elem.submit();
			return;		
		} // end if
		
		// Loop through err messages and alert user. 
		var formatted="";
		for (var errorMsg in outMsg) {
			if (typeof outMsg[errorMsg]=="string") {formatted += "-- " + outMsg[errorMsg] + "\n";} // end if
			 
		} // end for
		alert("Please correct the following field errors and try again:\n\n"+formatted);
		
	} // end method 
	
	
	//**************************************************************************************
	// Method:		checkRequired().  Checks all input fields with custom attribute 'Required' set to "true" 
	// Returns:		object type in string form
	Form.prototype.checkData = function(frmElement) {
	
		var isRequired = frmElement.getAttribute("Required");
		var fldname = frmElement.getAttribute("FieldName");
		if (isRequired=="true") {
			switch (frmElement.type) {
			case "select-one" :
			case "radio" :
			case "textarea" :
			case "text" :				
				// the following is used for both 'text' and 'textarea' input types
				if (frmElement.value.trim().length==0) {			
					return "'" + fldname + "' requires a value.";
				} else {
					var patternMsg = this.checkPattern(frmElement, fldname);
					if (patternMsg!="") { 
						return patternMsg;
					} // end if
			} // end if	
				break;
			case "hidden" :		
				if (frmElement.value.length==0) {
					msg = "UNEXPECTED: Method: checkRequired(). Project code was expected in hidden input field but not supplied.";					
					return msg;
				} // end if
				break;
			default:
				alert(frmElement.type);
				break;
			
			} // end switch
		} // end if
		return "";
		
	} // end method 
	
	//**************************************************************************************
	// Method:		checkPattern().  
	// Returns:		err message 
	Form.prototype.checkPattern = function(frmElement, fldname) {
	
		
		var pat = frmElement.getAttribute("Pattern");
		if (pat) {
			switch (frmElement.type) {
			case "textarea":
			case "text" :
				// the following is used for both 'text' and 'textarea' input types
				if (pat==__emailPattern) {
					if (!frmElement.value.match( __emailRegEx )) {
						return this.getPatternMsg(pat, fldname);
					} // end if
				} // end if
				break;
			
			default: 
				break;		
			} // end switch
		} // end if
		return "";
	} // end method
	
	//**************************************************************************************
	// Method:		getPatternMsg().  Return appropriate message based on type of pattern
	// Returns:		object type in string form
	Form.prototype.getPatternMsg = function(patType, fldname) {
		switch (patType) {
			case __emailPattern :
				return "'" + fldname + "' has an invalid email address."
				
			default:
				break;
		} // end switch
		return ""
	} // end method
	
		
	//**************************************************************************************
	// Method:		confirm().  Meant to be overridden locally.  Matches to fields to see
	//				if values are equal.  If no confirmation validation is needed, 
	//				simply return empty string in overridden method.
	// Returns:		Message if error occurs
	Form.prototype.confirm = function() {
		alert("UNEXPECTED: Method confirm() is meant to be overridden locally.");
	} // end method


	Form.prototype.confirmDelete = function(delUrl) {
		if (confirm("Are you sure you want to delete this row?")) {
			document.location = delUrl;
		} // end if	
	} // end method