<!--

function isNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function isPrice(strString)
	// check for valid Prices 56.87 or 56 but not 6.000 or 6,32
	{
	var blnResult = true;
		//check if it's numeric.
		if ( !isNumeric(strString) ) {
			blnResult = false;
		} else {
		//check if there's a ".".
			//split string by "." create array.
			arrayS = strString.split(".");
			//check wether arrayS has more than 2 elements. then it's wrong.
			if (arrayS.length > 2)	{
				blnResult = false;
			} else {
				//if it has 2 elements then its a.b
				if (arrayS.length == 2) {
					//if there's more than 2 numbers on b.
					if ( arrayS[1].length > 2) blnResult = false;
				}
			}
		}
	return blnResult;
}
			
	
//PF_Container Definition
function PF_Container() {
	this.elements = new Array();
}

//addElement Function Prototype for PF_Container Class.
function addElementProt(the_object) {
	newKey = this.elements.length;
	this.elements[newKey] = the_object;

}
PF_Container.prototype.addElement = addElementProt; 

//getErrors Prototype for PF_Container Class.
function getErrorsProt(the_form) {
	errors = '';
	the_length = this.elements.length;
	for (x=0; x < the_length; x++) {
		errors += this.elements[x].getError(the_form);
	}
	return errors;
}
PF_Container.prototype.getErrors = getErrorsProt;

//Element Definition.
function PF_Element(type, name, real_name, required, val_rule, val_message, val_arg) {
	this.type = type;
	this.name = name;
	this.real_name = real_name;
	this.required = required;
	this.val_rule = val_rule;
	this.val_message = val_message;
	this.val_arg = val_arg;
}

//getError Prototype for PF_Element Class.
function getErrorProt(the_form) {
	errors = '';
	form_element = the_form.elements[this.name];
	the_value = getElementValue(the_form, this.name, this.type);		
	if (this.required && !the_value) { 
		if (this.val_message) {
			 errors = ' - ' + this.val_message + '\n';
		 } else {
	 		 errors = ' - ' + this.real_name + PFRequired + '\n';
		}
	}

	if (!errors && this.val_rule) {
		switch (this.val_rule) {
			case 'ValueDepends':
			//Compare this value against the Depending value.
			if (the_value != the_form.elements[this.val_arg].value) errors += ' - ' + this.val_message + ' \n';
			break;

			case 'Email':
			//check if it's a valid email address.
			var filter =/^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;
			if (!filter.test(the_value)) errors += ' - ' + this.val_message + '\n';
			break;
			
			case 'DependsOn':
			//we check only if this element is filled.. :);
			if (the_value) {
				check = true;
				if (this.val_arg.constructor == window.Array) {
					//it's an array.
					length = this.val_arg.length;
					for (x = 0; x < length; x++) {
					if (! the_form.elements[this.val_arg[x]].value) check = false;
					}
				} else {
					//it's not an array
					if (! the_form.elements[this.val_arg].value) check = false;
				}
				if (! check ) errors += ' - ' + this.val_message + ' .\n';
			}
			break;
			
			case 'Numeric':
				if (! isNumeric(the_value)) {
					errors += ' - ' + this.val_message + '\n';
				}
			break;
			
			case 'Price':
				if (! isPrice(the_value)) {
					errors += ' - ' + this.val_message + '\n';
				}
			break;
			case 'Username':							
			var filter =/^[a-z0-9A-Z_-]{6,32}$/;
			if (!filter.test(the_value)) errors += ' - ' + this.val_message + '\n';
			break;
			case 'Pass':
			var filter =/^.{6,16}$/;
			if (!filter.test(the_value)) errors += ' - ' + this.val_message + '\n';
			break;
			default:
			break;

		}
	}	
	return errors;
}
PF_Element.prototype.getError = getErrorProt;	

function getElementValue(the_form, name, type) {
	form_element = the_form.elements[name];
	switch (type) {
	case 'Select':
	
	//check whether it's selected in case it's a multiple line select.
	isSelected = (form_element.selectedIndex >= 0);
	
		if (isSelected) { 
			//it is selected so there is a value.
			the_value = form_element.options[form_element.selectedIndex].value;
			//Because of problems with selectIndex (returning only the first selected index).. if it's multiple there shouldn't be a no use value.

		} else {
			//it's not selected set value to '';
			the_value = '';
		}
	break;
	case 'CheckboxGroup':
	the_value = '';
	if (form_element.length) {
		cg_size = form_element.length-1;
		//iterate through all the elements and check whether one is filled.
		for (i=0; i<= cg_size; i++) {
 			if (form_element[i].checked) the_value = '1';
 		}
	} else {
		if (form_element.checked) the_value = '1';
	}
	break;
	case 'Checkbox':
		if (form_element.checked) {
		//it's checked.
		the_value = 1;
		} else {
		the_value = '';
		}
	break;

	default :
	the_value = form_element.value; 
	break;
	}
	return the_value;
}
	

//Element Definition.
function PF_Date(name, real_name, required, val_rule, val_message, check_year) {
	this.name = name;
	this.real_name = real_name;
	this.required = required;
	this.val_rule = val_rule;
	this.val_message = val_message;
	this.check_year = check_year;
}

function dateGetErrorProt(the_form){
	var tr = '';
	var dday = getElementValue(the_form, this.name + 'Day', 'Select');
	var dmonth = getElementValue(the_form, this.name + 'Month', 'Select');

	if (this.check_year) {
		var dyear = getElementValue(the_form, this.name + 'Year', 'Select');
	} else {
		if (!this.val_rule) {
		var dyear = 4;
		}
	} 
	
if (this.required || (this.val_rule && (dday || dmonth || (this.check_year && dyear)))) {
	var err=0;
	//are they filled?
	if (!dday || !dmonth || !dyear) err = 1;
	//are they numbers?
	if (isNaN(dday) || isNaN(dmonth) || isNaN(dyear)) err = 1;
	//day bigger than 31?
	if (dday > 31) err = 1;
	//month bigger than 12?
	if (dmonth > 12) err = 1;
	// months with 30 days
	if ((dmonth==4 || dmonth==6 || dmonth==9 || dmonth==11) && (dday==31)) err = 1;
	// february, leap year
	if (dmonth==2){
		// feb
		var g = parseInt( dyear/4 )
		if ( isNaN(g) ) err= this.val_message;
		if (dday > 29) err= this.val_message;
		if (dday == 29 && ( (dyear/4) != parseInt(dyear/4)) ) err= this.val_message;
	}
	if (err) tr = ' - ' + this.val_message + '\n';
}
	return tr;

}
PF_Date.prototype.getError = dateGetErrorProt;	

function validate(the_form) {

	the_object_name = the_form.name + '_Obj';
	errors = document[the_object_name].getErrors(the_form);
	if (errors) { alert(PFErrors + '\n' + errors); }
	return (errors == '');

}
// -->	
