/**
 * Class used to Validate on the Client-Side, before PRADO does its own validation
 * It transposes states to DOM objects accordingly -  in this case validation icons
 * copyright 2007 @ Aldratech SRL Romania
**/

RegisterClientValidator = new Object();

RegisterClientValidator.disabledIconPath = 'images/validation_icons/icon_validate_disabled.gif';
RegisterClientValidator.enabledIconPath = 'images/validation_icons/icon_validate_passed.gif';

// Keep STATES for AJAX Impl of Username DB Availability

RegisterClientValidator.USR_AVAILABLE = true;
RegisterClientValidator.EMAIL_AVAILABLE = true;


RegisterClientValidator.setIconState  = function(ico,state){
	document.getElementById(ico).src = (!state) ? this.disabledIconPath : this.enabledIconPath;
}

RegisterClientValidator.currencyHasHumanInput = 0;


RegisterClientValidator.parseInputField  = function(v, t, rel_field, ico, rrgxp ){

	//check for Browser DOM Support
	if(!this.checkBrowserCompatibility()){
		alert('Your Browser does not have JavaScript Enabled, or it does not support W3C DOM functions. The input check icons have been disabled!');
		return;
	}

	//override for fields with free inputs
	if (t == 0){
		ico.src = this.enabledIconPath;
		return;
	}

	var fval = v.toString();
	var matchValues = fval.match(rrgxp);

	//FIRST, check based on RegExp passed
	if (fval != undefined && fval != null && fval != "" && matchValues != null && matchValues[0].length == fval.length){

		//if this is a regular field check, or primary password field update the icon
		if (t <= 2){
			ico.src = this.enabledIconPath;
		}

	}
	else{
		ico.src = this.disabledIconPath;
		//if this is NOT a primary password field stop the checks and disable
		if (t != 2){
			return;
		}
	}

	//SECOND, check field using other algorythms based on t, alias vg_type of current field
	// t values:  0 - free field(no validation), 1 - basic validation, 2 - password, 3 - DOB
	switch(t){

		case 2: //master password typed  field correlate with re-type password icon
				var chkIcoState = ''+ ico.src;

				//we cannot check upon ico.src because PRADO uses friendly URLs and appends the domain to IMG src
				if (chkIcoState.search(this.disabledIconPath) != -1){
					rel_field.src = ico.src;
				}

				break;

		case 3: //password retyped checking, and password field correlate icon states

			if (fval != document.getElementById(rel_field).value){
				ico.src = this.disabledIconPath;
				return;
			}else{
				ico.src = this.enabledIconPath;
			}
			break;
		case 4: //Date object [12 months, 31 days max, year etc] checking
			if (new Date(fval) != null){
				ico.src = this.enabledIconPath;
			}
			else{
				ico.src = this.disabledIconPath;
			}
			break;
		default:
			//end of custom validations
	}
}


RegisterClientValidator.checkBrowserCompatibility = function(){
	if( document.getElementById && document.childNodes && document.createElement ) {
  		//the Browser supports advanced W3C DOM functions, let`s GO
  		return true;
	}
	else{
		return false;
	}
}


RegisterClientValidator.revalidate = function(){
	var reg_inputs = document.getElementsByTagName("input");
	for (var i=0; i < reg_inputs.length; i++){
		if (reg_inputs[i].onkeyup != null && reg_inputs[i].onblur != null)
			reg_inputs[i].onkeyup();
	}

}

