/***********************************************************************
	Validator Class  ver. 2.50 (CrossBrowser Version)
	Created by		:		Lothar James Foss (CNP Solutions)
	Creation date	:		05.11.2003
 ***********************************************************************/
function Validator(strName,strFormName){
	//Purpose : Constructor function for the validator.
	//Accepts : strName     - The validators name. Is used for all internal/external method calls.
	//          strFormName - The name of the form to be validated.
	//Returns :	Nothing.

	//Private Properties
	this.name           =  strName;
	this.formName       =  strFormName;
	this.passed         =  1;

	//Public Methods
	this.addItem        =  validatorAddItem;
	this.submit         =  validatorSubmit;
	this.isValid        =  validatorIsValid;

	//Protected Methods
	this.getPassed      =  validatorGetPassed;
	this.setPassed      =  validatorSetPassed;
}


function validatorAddItem(strChildName,strType,strItemName,vntParam1,vntParam2,vntParam3) {
	//Purpose : Creates a validation object of the correct type.
	//Accepts : strChildName - The name of the created validation object.
	//          strType      - The type of object to be created. Can be "text" or "select".
	//          strItemName  - The name of the form element to be validated.
	//          vntParam1    - Object specific parameter.
	//          vntParam2    - Object specific parameter.
	//          vntParam3    - Object specific parameter.
	//Returns :	Nothing.
	switch(strType.toLowerCase()) {
		case 'text':
			this[strChildName]   =  new TextObj(this,strChildName,strItemName,vntParam1,vntParam2,vntParam3);
			break;

		case 'select':
			this[strChildName]   =  new SelectObj(this,strChildName,strItemName,vntParam1,vntParam2,vntParam3);
			break;
	}
}


function validatorGetPassed() {
	//Purpose :	Returns the validation objects passed parameter.
	//Accepts : Nothing.
	//Returns :	blnPassed - False if a validation has failed for this form, else true.
	blnPassed = this.passed;
	return blnPassed;
}


function validatorSetPassed(blnPassed) {
	//Purpose : Sets the validation objects passed parameter.
	//Accepts : blnPassed - Set to false if validation has failed.
	//Returns :	Nothing.
	this.passed = blnPassed;
}


function validatorIsValid() {
	//Purpose : Returns the validation objects passed parameter and resets the object.
	//Accepts : Nothing.
	//Returns :	blnPassed - False if a validation has failed for this form, else true.
	blnPassed = this.passed;

	if(!this.passed) this.setPassed(1);
	return blnPassed;
}


function validatorSubmit(){
	//Purpose : If validation has passed, submit the form else reset object.
	//Accepts : Nothing.
	//Returns :	Nothing.
	if(this.passed) { document.forms[this.formName].submit(); }
	else { this.setPassed(1); }
}




/***********************************************************************
	Textfield Class  ver. 2.00 (CrossBrowser Version)
	Created by		:		Lothar James Foss (CNP Solutions)
	Creation date	:		05.11.2003
 ***********************************************************************/
function TextObj(objParent,strName,strItemName,blnDelete,blnFocus,strFocusName){
	//Purpose : Constructor function for the text object.
	//Accepts : objParent    - Object reference to this objects superclass.
	//          strName      - The validators name. Is used for all internal/external method calls.
	//          strItemName  - The name of the form element to be validated.
	//          blnDelete    - Set to true if this element should be cleared if validation has failed.
	//          blnFocus     - Set to true if this element should receive focus if validation has failed.
	//          strFocusName - The name of the form element to receive focus on error.
	//Returns :	Nothing
	this.name           =  strName;
	this.parent         =  objParent;
	this.itemName       =  strItemName;
	this.item           =  document.forms[this.parent.formName][this.itemName];
	this.delContent     =  blnDelete;
	this.getFocus       =  blnFocus;
	this.focus          =  (strFocusName)? document.forms[this.parent.formName][strFocusName] : document.forms[this.parent.formName][this.itemName];

	this.isEmpty        =  textIsEmpty;
	this.isDate         =  textIsDate;
	this.afterDate      =  textAfterDate;
	this.beforeDate     =  textBeforeDate;
	this.invalidChars   =  textInvalidChars;
	this.validChars     =  textValidChars;
	this.withinRange    =  textWithinRange;
	this.withinValue    =  textWithinValue;
	this.regularExp     =  textRegularExp;
	this.showError      =  textShowError;
}


function textIsEmpty(strErrorText) {
	//Purpose : Checks if the inputfield is empty.
	//Accepts : strErrorText - The text to be displayed if validation fails.
	//Returns :	Nothing
	var objItem         =  this.item;
	var strValue        =  "";

	//Strip spaces from the inputfields value.
	for(var count = 0; count < objItem.value.length; count++) {
		strValue += (objItem.value.charAt(count) == ' ') ? "" : objItem.value.charAt(count);
	}

	//If nothing is left after spaces are removed then the field is empty.
	if(strValue == "") {
		this.showError(strErrorText);
	}
}


function textIsDate(strErrorText) {
	//Purpose : Checks if the date in the inputfield is a valid date.
	//          The date must conform to the format DDMMYYYY, DD/MM/YYYY or DD-MM-YYYY.
	//Accepts : strErrorText  - The text to be displayed if validation fails.
	//Returns :	Nothing
	var objItem         =  this.item;
	var strValue        =  objItem.value;
	var blnValidDate    =  false;

	//Remove seperators
	if(strValue.indexOf("-") != -1 || strValue.indexOf("/") != -1)
		strValue = strValue.substring(0,2) + strValue.substring(3,5) + strValue.substring(6,10);

	//Format dates for check
	var intDay   = parseInt(strValue.substring(0,2),10);
	var intMonth = parseInt(strValue.substring(2,4),10);
	var strYear  = strValue.substring(4,8);

	//Check date validity
	if(intDay >= 1 && intMonth >= 1 && intMonth <= 12 && strYear.length == 4){
		if((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && intDay <= 31)
			blnValidDate    =  true;

		else if((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && intDay <= 30)
			blnValidDate    =  true;

		else if(intMonth == 2 && intDay <= 28)
			blnValidDate    =  true;

		else if(intMonth == 2 && intDay <= 29)
			if(strYear % 4 == 0)
				if(strYear % 100 != 0 || strYear % 400 == 0)
					blnValidDate    =  true;
	}

	if(!blnValidDate)
		this.showError(strErrorText);
}


function textAfterDate(strDate,strErrorText) {
	//Purpose : Checks if the date in the inputfield is more recent than the passed date.
	//          The inputfields value must conform to the format accepted by the isDate validation.
	//Accepts : strDate       - The date to be validated against in the format DDMMYYYY.
	//          strErrorText  - The text to be displayed if validation fails.
	//Returns :	Nothing
	var objItem         =  this.item;
	var strValue        =  objItem.value;

	//Remove seperators
	if(strValue.indexOf("-") != -1 || strValue.indexOf("/") != -1)
		strValue = strValue.substring(0,2) + strValue.substring(3,5) + strValue.substring(6,10);

	//Format dates for compare
	strDate  =  strDate.substring(4,8) + strDate.substring(2,4) + strDate.substring(0,2);
	strValue =  strValue.substring(4,8) + strValue.substring(2,4) + strValue.substring(0,2);

	if(strValue !="" && strDate >= strValue)
		this.showError(strErrorText);
}


function textBeforeDate(strDate,strErrorText) {
	//Purpose : Checks if the passed date is more recent than the date in the inputfield.
	//          The inputfields value must conform to the format accepted by the isDate validation.
	//Accepts : strDate       - The date to be validated against in the format DDMMYYYY.
	//          strErrorText  - The text to be displayed if validation fails.
	//Returns :	Nothing
	var objItem         =  this.item;
	var strValue        =  objItem.value;

	//Remove seperators
	if(strValue.indexOf("-") != -1 || strValue.indexOf("/") != -1)
		strValue = strValue.substring(0,2) + strValue.substring(3,5) + strValue.substring(6,10);

	//Format dates for compare
	strDate  =  strDate.substring(4,8) + strDate.substring(2,4) + strDate.substring(0,2);
	strValue =  strValue.substring(4,8) + strValue.substring(2,4) + strValue.substring(0,2);

	if(strValue !="" && strDate <= strValue)
		this.showError(strErrorText);
}


function textInvalidChars(strInvalid,strErrorText) {
	//Purpose : Checks if the inputfield contains invalid characters.
	//Accepts : strInvalid   - A string containing all invalid characters.
	//          strErrorText - The text to be displayed if validation fails.
	//Returns :	Nothing
	var objItem         =  this.item;

	//Loop through the inputfields value one character at a time
	for(var count = 0; count < objItem.value.length; count++)
		if(strInvalid.indexOf(objItem.value.charAt(count)) != -1) this.showError(strErrorText);
}


function textValidChars(strValid,strErrorText) {
	//Purpose : Checks that the inputfield only contains valid characters.
	//Accepts : strValid     - A string containing all valid characters.
	//          strErrorText - The text to be displayed if validation fails.
	//Returns :	Nothing
	var objItem         =  this.item;

	//Loop through the inputfields value one character at a time
	for(var count = 0; count < objItem.value.length; count++)
		if(strValid.indexOf(objItem.value.charAt(count)) == -1) this.showError(strErrorText);
}


function textWithinRange(intMin,intMax,strErrorText) {
	//Purpose : Checks if the inputfield contains the correct number of characters.
	//Accepts : intMin       - The minimum number of characters allowed.
	//          intMax       - The maximum number of characters allowed.
	//          strErrorText - The text to be displayed if validation fails.
	//Returns :	Nothing
	var objItem         =  this.item;

	if(objItem.value.length < intMin || objItem.value.length > intMax) this.showError(strErrorText);
}


function textWithinValue(intMin,intMax,strErrorText) {
	//Purpose : Checks if the inputfield contains a numeric value that is within the given range.
	//Accepts : intMin       - The minimum value.
	//          intMax       - The maximum value.
	//          strErrorText - The text to be displayed if validation fails.
	//Returns :	Nothing
	var objItem         =  this.item;
	var intValue        =  parseInt(objItem.value,10);

	if(intValue < intMin || intValue > intMax)
		this.showError(strErrorText);
}


function textRegularExp(strExpression,strErrorText) {
	//Purpose : Checks the inputfields value against a regular expression.
	//Accepts : strExpression - The regular expression.
	//          strErrorText  - The text to be displayed if validation fails.
	//Returns :	Nothing
	var objItem         =  this.item;
	var regExpression   =  strExpression;

	if(!regExpression.test(objItem.value)) this.showError(strErrorText);
}


function textShowError(strErrorText){
	//Purpose : Displays an error message and gives the failed inputfield focus.
	//Accepts : strErrorText - The text to be displayed if validation fails.
	//Returns :	Nothing
	var objItem         =  this.focus;
	var objParent       =  this.parent;

	if(objParent.getPassed()) {
		//Alert error message
		alert(strErrorText);

		//Delete content
		if(this.delContent) objItem.value = "";

		//Give field focus
		if(this.getFocus) objItem.focus();

		objParent.setPassed(0);
	}
}

/***********************************************************************
	SEB Select Class  ver. 1.00 (CrossBrowser Version)
	Created by		:		Lothar James Foss (CNP Solutions)
	Creation date	:		13.06.2001
 ***********************************************************************/
function SelectObj(objParent,strName,strItemName,blnFocus,strFocusName){
	//Purpose : Constructor function for the select object.
	//Accepts : objParent    - Object reference to this objects superclass.
	//          strName      - The validators name. Is used for all internal/external method calls.
	//          strItemName  - The name of the form element to be validated.
	//          blnFocus     - Set to true if this element should receive focus if validation has failed.
	//          strFocusName - The name of the form element to receive focus on error.
	//Returns :	Nothing
	this.name           =  strName;
	this.parent         =  objParent;
	this.itemName       =  strItemName;
	this.item           =  document.forms[this.parent.formName][this.itemName];
	this.getFocus       =  blnFocus;
	this.focus          =  (strFocusName)? document.forms[this.parent.formName][strFocusName] : document.forms[this.parent.formName][this.itemName];

	this.hasValue       =  selectHasValue;
	this.showError      =  selectShowError;
}

function selectHasValue(strValue,strErrorText) {
	//Purpose : Checks the inputfields value against the passed value.
	//Accepts : strValue      - The value to be validated against.
	//          strErrorText  - The text to be displayed if validation fails.
	//Returns :	Nothing
	var objItem         =  this.item;
	var selectedValue   =  objItem.options[objItem.selectedIndex].value;

	if(selectedValue == strValue)
		this.showError(strErrorText);
}

function selectShowError(strErrorText){
	//Purpose : Displays an error message and gives the failed inputfield focus.
	//Accepts : strErrorText - The text to be displayed if validation fails.
	//Returns :	Nothing
	var objItem         =  this.focus;
	var objParent       =  this.parent;

	if(objParent.getPassed()) {
		//Alert error message
		alert(strErrorText);

		//Give field focus
		if(this.getFocus)
			objItem.focus();

		objParent.setPassed(0);
	}
}
