/***********************************************************/
/*			Form validation script v 1.2.2		              */
/***********************************************************/
/*	Author: Sergey Sushinsky / zuhar@tut.by /	              */
/***********************************************************/
/* Types available:									       		  */
/* notempty (returns true, when the field is not epmty)    */
/* isnumber	(returns true, when the field contains		     */
/*			 only numeric values)						   		  */
/* email (returns true when: a@b.c OR a.a@b.c)			     */
/* phone_fax (returns true when: ### ###-##-##)		        */
/* zip	(returns true when: ##### ,US format)             */
/* isSelected (returns true when: value!=null )            */
/* isValidCCN (returns true when Credit Card number exists)*/
/*	password (return true when password string is more then */
/* 2 characters length )											  */
/* confirm_password (compares 'Password'						  */
/* and 'Confirm Password' fields so appears to be equal;   */
/* if they don't - returns FALSE )								  */
/* isURL (returns true when http[ftp]://########.######)	  */
/***********************************************************/
function Validator(formObj) {
	//
	this.formObject = "document."+formObj || null;
	//Required fields array
	this.fieldsToValidate = null;
	//
	this.errors = "Please, check the fields below:\n";
	this.errorsLen = this.errors.length;
	//is self debug "on"
	this.selfDebug = false;
	//
	this.ver = "\n\n\nFVS ver 1.2.3 :: debug mode";
}
//
Validator.prototype.validate = function checkForm() {
		//Self debug block
		if(this.selfDebug) this.selfDebugger();
		//Validation block
		for(var i=0; i < this.fieldsToValidate.length; i++) {
				//Get field to validate and it's value
				//Debug
				//alert("Debug::Form alias:"+this.formObject);
				//var formItemObject = this.formObject+"."+getTechName(this.fieldsToValidate[i]);
				//Debug
				//alert("Debug::Form element alias:"+formItemObject);
				//var formItemValue = formItemObject.value;
				var formItemValue = eval(this.formObject+"."+getTechName(this.fieldsToValidate[i])).value;
				//Get type-specific function
				var formItemType = getType(this.fieldsToValidate[i]);
				//Type-specific function call
				var objRef = this;
				if( !eval( "objRef."+formItemType+"('"+formItemValue+"')" ) ) 
					this.errors += "- "+getRealName(this.fieldsToValidate[i])+"\n";
		}
		//Report errors block
		if(	this.errorsLen != this.errors.length ) {
			alert(this.errors);
			this.errors = "Please, check the fields below:\n";
			return false;
		}
		else return true;
	}
	//Private functions
	function getTechName(string) {
		var resString = string.split("|");
		return resString[0];
	}
	function getRealName(string) {
		var resString = string.split("|");
		if(resString.length < 3) return false;
		return resString[1];
	}
	function getType(string) {
		var resString = string.split("|");
		return resString[resString.length-1];
	}
	function isTypeExist(string) {	
		for(iterator in this)
			if(string == iterator) { 
				break;
				return true;
			}
		return false;
	}
	function trim(string) {
		if(string.length != 0) {
			var reRight=new RegExp(" +$");
			var reLeft=new RegExp("^ +");
			string=string.replace(reRight,"");
			string=string.replace(reLeft,"");
		}
		return string;
	}
	//Self debudder "tool"
	Validator.prototype.selfDebugger = function selfDebugger() {
		//
		objRef = this;
		//
		if( !this.fieldsToValidate ) { alert("Please, fill in Required fields array!\n\n [ fieldsToValidate property ] isn't defined."+this.ver); return false; }
		if( typeof(eval(this.formObject)) != "object" ) {
			alert("The form name you provide as the argument\n should be the same as the name of the form you wish to validate!\n See your HTML document <script> block to fix this error."+this.ver);
			return false;}
		//
		for(var i=0; i < this.fieldsToValidate.length; i++) {
			var testObjPrName = getTechName( this.fieldsToValidate[i] );
			//is Field Exists
			if( typeof( eval( this.formObject+"."+testObjPrName ) ) != "object" ) {
			alert("Can't get one of the fields by the following alias: "+testObjPrName+"\nSee the 'fieldsToValidate["+i+"]' property initialisation.\nThe name of the form element is incorrect."+this.ver);return false;}
			//is Element Title Exists
			if( !getRealName(this.fieldsToValidate[i]) ) {alert(" | | "+this.ver);return false;}
			//is Type Exists
			/*var testType = getType( this.fieldsToValidate[i] );
			var exFlag = false;
			for(iterator in this) if( testType == iterator ) { exFlag = true; break; }
			if( !exFlag ){ alert("The type you have specified doesn't exist!\nPlease, see the 'fieldsToValidate["+i+"]' property initialisation.\nThe type spelling is incorrect."+this.ver); return false; }
			*/
		}
		//
		alert("Script was implemented correctly :)\nDon't forget to set 'this.selfDebug' property into 'false',\n before this project will be shown to the customer."+this.ver);
		//
		return true;
	}
	//Type-specific check functions
	Validator.prototype.notempty = function notempty(string) {
			string = trim(string);
			//
			return (string.length == 0) ? false : true;
		}
	Validator.prototype.isnumber = function isnumber(string) {
			string = trim(string);
			//
			var testStr = "[0-9]+";
			var re = new RegExp(testStr);
			//
			return string.match(re) ? true : false;
	}
	Validator.prototype.email = function email(string) {
			string = trim(string);
			//
			var testStr = "^\\S+@\\S+\\.\\w{2,4}$";
			var re = new RegExp(testStr);
			//
			return string.match(re) ? true : false;
		}
	Validator.prototype.phone_fax = function phone_fax(string) {
			string = trim(string);
			//
			var testStr = "[0-9]{3} [0-9]{3}-[0-9]{4}";
			var re = new RegExp(testStr);
			//
			return string.match(re) ? true : false;
		}
	Validator.prototype.mm_dd_yyyy = function(string) {
			string = trim(string);
			//
			var testStr = "[0-9]{2}/[0-9]{2}/[0-9]{4}";
			var re = new RegExp(testStr);
			//
			return string.match(re) ? true : false;
		}
	Validator.prototype.zip = function zip(string) {
			string = trim(string);
			//
			var testStr = "[0-9]{5}";
			var re = new RegExp(testStr);
			//
			return string.match(re) ? true : false;
		}
	Validator.prototype.isSelected = function isSelected(string) {
			return (string == "") ? false : true;
		} 
	Validator.prototype.isValidCCN = function isValidCCN(string) {
			var string = trim(string);
			//
			var testStr = "^[0-9]{4}\s?[0-9]{4}\s?[0-9]{4}\s?[0-9]{1,4}$";
			var re = new RegExp(testStr);
			//
			return string.match(re) ? true : false;
		}
	Validator.prototype.isURL = function isURL(string) {
			var string = trim(string);
			//
			var pattern = "^[A-Za-z]+://[A-Za-z0-9-]+\.[A-Za-z0-9]+";
			var re = new RegExp(pattern);
			//
			return  string.match(re) ? true : false;
	}
	//Temp password string storage
	Validator.prototype.passwordStr = "";
	//Save initial password string into passwordStr variable
	Validator.prototype.password = function password(string) {
			this.passwordStr = trim(string);
			//
			return ( string.length < 2 ) ?  false : true;
		} 
	//Validates if initial and retyped passwords are the same
	Validator.prototype.confirm_password = function confirm_password(string) {
			var string = trim(string);
			( string != this.passwordStr ) ? false : true;
		} 
