//Class jjValidator

//requires prototype.js
//requires jjFormElement.js


var jjValidator = Class.create();

jjValidator.prototype = 
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



// === CONSTRUCTOR ===
	initialize: function(inElementList)
	{
		this.requiredFieldList = inElementList;
		//this is a field type as key, regex string to fail test as value
		this.fieldTypesValidators =
		$H({
			oneline: $H({pattern: /[\r\n]/, successIf: false, errStr: 'may not have newline characters'}),
			numeric: $H({pattern: /\D/, successIf: false, errStr: 'must only contain digits'}),
			email: $H({pattern: /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/, successIf: true, errStr: 'must be a valid email'}),
			nowhitespace: $H({pattern: /[\s]/, successIf: false, errStr: 'may not have whitespace (spaces, tabs, etc)'})

		});
				
	}, //end jjValidator constructor



// === METHODS ===	
	
	
	//given a jjFormElement, returns true if the value is empty	
	elementEmpty: function (inElement)
	{
		if ( $(inElement.id).value == "")
		{
			return true;
		}
		else
		{
			return false;
		}

	}, //end jjValidator.elementEmpty()
	
	
	//given 2 jjFormElements, returns true if the values are the same
	elementsSame: function(inElement1, inElement2)
	{
		if ($(inElement1.id).value == $(inElement2.id).value)
		{
			return true;
		}
		else
		{
			return false;
		}

	}, //end jjValidator.elementsSame()
	
	
	//sets focus on the given object if it is not hidden
	setFocusOnElement: function(inElement)
	{
		var e = $(inElement.id);
		if ( e.visible() )
		{
			e.scrollTo();
			e.focus();
		}
	}, //end jjValidator.setFocusOnElement()
	
	
	
	//returns a message if a field is wrong type, null if it is ok
	elementWrongType: function(inElement)
	{
		var val = $(inElement.id).value;
		var type = inElement.type;
		
		var typelist = this.fieldTypesValidators.keys();
		
		//only have to test if there is a type that we know of
		if ( (type != null) && (typelist.indexOf(type) != -1) )
		{

			//test using the regex, compare result against "successIf"
			var answer = (this.fieldTypesValidators[type].pattern.test(val)) ? 'Y' : 'N';
			var correctAnswer = (this.fieldTypesValidators[type].successIf) ? 'Y' : 'N';
			if (answer != correctAnswer)
			{
				return this.fieldTypesValidators[type].errStr;
			}
		}
		
		return null;
		
	}, //end jjValidator.elementWrongType()
	
	
	//reports error with given text on optionally given object,
	//option to set the focus to the object
	reportErrorForElementSetFocus: function(inErrText, inElement, inFocusTF)
	{	
		var alertText = "" + inErrText;

		if (inElement != null)
		{
			alertText = inElement.desc + " " + alertText;
		}

		if (alertText != "")
		{
			alert( alertText );
		}

		if ( inFocusTF == true )
		{
			this.setFocusOnElement( inElement );
		}


	}, //end jjValidator.reportErrorForElementSetFocus()
	
	
	//true/false are all of the required fields given filled with correct val
	requiredFieldsAreFilled: function(inShowErrorTF, inSetFocusTF)
	{
		var showErrorTF = false;
		if (inShowErrorTF == true)
		{
			showErrorTF = true;
		}

		var setFocusTF = false;
		if (inSetFocusTF == true)
		{
			setFocusTF = true;
		}

		//loop over required fields, fail out if any are empty
		for (var i = 0; i < this.requiredFieldList.length; i++)
		{
			var f = this.requiredFieldList[i];

			if (this.elementEmpty(f))
			{
				//show error
				if (showErrorTF == true)
				{	
					this.reportErrorForElementSetFocus("is required.", f, setFocusTF);
				}

				//exit now
				return false;

			} //end if this element is empty
			
			var typeErrorMsg = this.elementWrongType(f);
			if (typeErrorMsg)
			{
				//show error
				if (showErrorTF == true)
				{	
					this.reportErrorForElementSetFocus(typeErrorMsg, f, setFocusTF);
				}
				
				//exit now
				return false;
				
			}

		}


		//success if we make it to here
		return true;

	} //end jjValidator.requiredFieldsAreFilled()
	


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} //end class jjValidator


