// -------------------------------------------------------------------
// FormValidation.js
//   encapsulates the FormValidation object, and contains all of the 
//   supprting validation methods
//
// Requirements:
//   PrestoCommon.js
//
// Version: 
//   $Id: $
// -------------------------------------------------------------------

var gBadCharsArray = new Array("<", ">", "{", "}", "&", "%", "$", "\\", ";", "/"); 
var gSkipValidation = false;

// -------------------------------------------------------------------
// ValidationObject()
// the basic validation object. 
// Arguments:
//   element:		 The object to validate							
//   message:		 The error message to report if validation fails
//   vType:			 (optional) The type of validation				
//   compareElement: (optional) The id of an element to compare to	
//	 tabToFocus:	 (optional) function to call to focus the element
// -------------------------------------------------------------------
//
function ValidationObject(element, message, vType, compareElement, tabToFocus) {
	if(element != "") {
		// if the arguments are being passed in, 
		// populate the object with them. 
		//
		this.HtmlElement	= element;
		this.ErrorMessage	= message;
		this.ValidationType	= vType;
		this.CompareElement = compareElement;
		this.Tab			= tabToFocus;
	} else {
		// otherwise, let's just create the empty object.
		//
		this.HtmlElement	= null;
		this.ErrorMessage	= null;
		this.ValidationType = null;
		this.CompareElement = null;
		this.Tab			= null;
	}
	
	_DEBUG("Completed creating "+ vType +" v.object for " + this.HtmlElement.id);
}

// -------------------------------------------------------------------
// Validate()
//    validates the form
// Arguments:
//    vo - an array of validationObjects to run through.	
// -------------------------------------------------------------------
//
function Validate(vo) {
	// if we can skip the valiation, let it pass. 
	//
	if (gSkipValidation) { return true; }
	
	_DEBUG("Begining Validation method with " + vo.length + " v.objects.");

	// loop through all the validationObjects
	//
	for(var i=0; i<vo.length; i++) {
	
		// now, let's determine the type of validation to use. 
		// if the ValidationType property is null, we'll just 
		// do the simple "IsRequired" validation. 
		//
		if(vo[i].ValidationType == "email") {
			// verify the field contains an email address
			//
			if(!IsEmailAddress(vo[i])) {
				AlertAndFocus(vo[i]);
				return false;
			}
		
		} else if(vo[i].ValidationType == "compare") {
			// verify the field, and the compareElement match
			//
			if(!CompareFields(vo[i].HtmlElement, vo[i].CompareElement)) {
				AlertAndFocus(vo[i]);
				return false;
			}
		
		} else if(vo[i].ValidationType == "notmatch") {
			// verify the field, and the compareElement match
			// however, if the values are both empty, that's OK. 
			//
			if(CompareFields(vo[i].HtmlElement, vo[i].CompareElement) && vo[i].HtmlElement.value!= "") {
				AlertAndFocus(vo[i]);
				return false;
			}
			
		} else {
			// perform basic "not empty" validation
			//
			if(!IsRequiredField(vo[i])) {
				AlertAndFocus(vo[i]);
				return false;
			}
		}
	}
}

// -------------------------------------------------------------------
// AlertAndFocus()
//    Generates the alert message, and focuses on the element. 
// Arguments:
//   vo:				 The Validation object that failed				
//   customFocusElement: (optional) A particular element to focus on (overrides vo).
// -------------------------------------------------------------------
//
function AlertAndFocus(vo, customFocusElement) {
	// focus the tab if needed
	//
	if (vo.Tab != null)	{
		eval(vo.Tab);
	}
	
	// focus the element
	//
	if (customFocusElement != null) {
		customFocusElement.focus();
	} else {
		vo.HtmlElement.focus();
	}
	
	// alert the message
	//
	alert(vo.ErrorMessage);
}

// -------------------------------------------------------------------
// IsRequiredField()
//    basic form field validation. Returns false if the string is empty, 
//    all spaces, or has any illegal chars. 
// -------------------------------------------------------------------
//
function IsRequiredField(vo) {
	var inputStr = Trim(vo.HtmlElement.value)
	if(inputStr == "" || inputStr == null) {
		_DEBUG("IsRequiredField(): input string is null");
		return false;
	}

	for(var i=0; i<gBadCharsArray.length; i++) {
		if(inputStr.indexOf(gBadCharsArray[i])!=-1) {
			_DEBUG("IsRequiredField(): Char " + gBadCharsArray[i] + " is reserved.");
			vo.ErrorMessage = "Sorry, you can't use the character '" + gBadCharsArray[i] + "'. Please try something else.";
			return false;
		}
	}
	var charRegxp = /^[ A-Za-z0-9\-\/\_\.\!\~\'\(\)\%\?]+$/;
	if (!charRegxp.test(inputStr)) {
		_DEBUG("IsRequiredField(): String " + inputStr + " failed regex.");
		return false;
	}
	return true;
}

// -------------------------------------------------------------------
// CompareFields()
//    basic comparision validation. Returns false if values don't match
// -------------------------------------------------------------------
//
function CompareFields(e1, e2) {
	if(e1.value == e2.value) {
		return true;
	}
	return false;
}

// -------------------------------------------------------------------
// IsEmailAddress()
//    email address field validation. 
// -------------------------------------------------------------------
//
function IsEmailAddress(vo) {
	// first let's do the easy work, and just look for 1 @ sign. 
	//
	var e	 = vo.HtmlElement;
	var bits = e.value.split("@");
	if (bits.length != 2) {return false;}
	if (Trim(e.value).indexOf(" ") != -1) {return false;}
	
	for(var i=0; i<gBadCharsArray.length; i++) {
		if(Trim(e.value).indexOf(gBadCharsArray[i])!=-1) {
			_DEBUG("IsEmailAddress(): Char " + gBadCharsArray[i] + " is reserved.");
			vo.ErrorMessage = "Sorry, you can't use the character '" + gBadCharsArray[i] + "' in an email address.";
			return false;
		}
	}
	
	// now do the regex.
	//
	var emailRegxp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!emailRegxp.test(Trim(e.value))) {
		_DEBUG("IsEmailAddress(): " + e.value + " is failing email regex.");
		vo.ErrorMessage = "Invalid email address. Please try again.";
		return false;
	}
	return true;
}


_DEBUG("Completed loading FormValidation.js...");