function jField(qfield,qparent){
	this.parent=qparent;
	this.id=qfield;
	this.displayError=null;
	this.fieldName='This field';
		
	this.setParams=function(qparams){
		for(var param in qparams){
			this[param]=qparams[param];
		}
	}
	this.validate=function(){return true;}
	this.conditionalvalidate=function(){return true;}
	this._validate=function(){
		this.valid=this.validate();
		if(this.valid) clearError(this.id);
		return this.valid;
	}
}
var FieldFunctions=new function(){
	this.focusField=function(){
		this.className=jlib.String.addOnce(this.className, "fhilite");
	}
	
	this.blurField=function(){
		if(vFormat[this.id]){
			vFormat[this.id](this);
		}
		this.className=jlib.String.remove(this.className, "fhilite");
	}
}

//!----- Validation methods --------!

var jValidation=new function(){
	
	//for name and last name
	this.vName=function(){
		var qel=jlib.$(this.id);
		qel.value=Trim(qel.value);
		var qvalue= qel.value;
		if(isEmpty(qvalue)){
			if(this.errorStr==undefined) errorStr=this.fieldName+' cannot be empty';
			displayError(qel,errorStr,this.displayError);
			return false;
		}else{
			return true;
		}
		qel=null;
	}
	
	this.vEmail=function (){
		var qel=jlib.$(this.id);
		qel.value=Trim(qel.value);
		var qvalue=qel.value;
		if(isEmpty(qvalue)){
			if(this.errorStr==undefined) errorStr=this.fieldName+' cannot be empty';
			displayError(qel,errorStr,this.displayError);
			return false;
		}else if(!checkRegEx(qvalue, /^[a-zA-Z_0-9-'\+~]+(\.[a-zA-Z_0-9-'\+~]+)*@([a-zA-Z_0-9-]+\.)+[a-zA-Z]{2,7}$/)){
			displayError(qel,'Please enter a valid email address',this.displayError);
			return false;
		}else{
			return true;
		}
		qel=null;
	}
		
	this.vSelection=function(){
		var qel=jlib.$(this.id);
		var qvalue=qel.value;
		if(isEmpty(qvalue)){
			if(this.errorStr==undefined) errorStr='Please select a '+this.fieldName;
			displayError(qel,errorStr,this.displayError);
			return false;
		}else{
			return true;
		}
		qel=null;
	}
	
	this.vPhone=function(){
		var qel=jlib.$(this.id);
		var qvalue=qel.value;
		var qfieldName=(this.fieldName=='This field')? 'Phone number':this.fieldName;
	    if( this.required ){
	        if( qvalue.length == 0 ){
				displayError(qel,qfieldName+" cannot be empty",this.displayError);
	            return false;
	        }
	    }else{
	        if( qvalue.length == 0 ) return true;
	    }
	
		if(!checkRegEx(qvalue, /^(((1))?[ ,\-,\.]?([\\(]?([1-9][0-9]{2})[\\)]?))?[ ,\-,\.]?([^0-1]){1}([0-9]){2}[ ,\-,\.]?([0-9]){4}(( )((x){0,1}([0-9]){1,5}){0,1})?$/)){
			displayError(qel,"Invalid "+qfieldName,this.displayError);
			return false;
		}else{
			return true;
		}
		qel=null;
	}
	
	this.vZip=function(){
 		var qel=jlib.$(this.id);
		qel.value=Trim(qel.value);
		var qvalue= qel.value;
		
		if(isEmpty(qvalue)){
			displayError(qel,this.fieldName+' cannot be empty',this.displayError);
			return false;
		}else if(!checkRegEx(qvalue, /(^\d{5}$)|(^\d{5}[ ,\-,\.]?\d{4}$)/)){
			displayError(qel,this.fieldName+' is not valid',this.displayError);
			return false;
		}else{
			return true;
		}
		qel=null;
	}
	
	this.vConditional=function(qfield){
		return function(){
			var qel=jlib.$(qfield);
			qel.value=Trim(qel.value);
			var qvalue= qel.value;
			if(isEmpty(qvalue)){
				return true;
			}else{
				return this.conditionalvalidate();
			}
			qel=null;
		}
	}
	
	this.vConditionalChGroup=function(qfield){
		return function(){
			var qel=(qfield);
			var checkval='';
			var search=true;
			for(i=0;i<qel.length && search==true;i++){
				if(qel[i].checked){
					search=false;
					checkval=qel[i].value;
				}
			}
			
			if(isEmpty(checkval)){
				this.required=true;
				return this.conditionalvalidate();
			}else{
				this.required=false;
				return true;	
			}
			qel=null;
		}
	}
}

function clearError(qel){
	jlib.$DE(qel+"_err");
	jlib.removeClassName(qel+'_l',"fieldError");	
}

function displayError(qel,qerr,qdisplayError){
	var el=jlib.$(qel.id+"_err");
	
	if(el==null){
		el=document.createElement('div');
		el.id=qel.id+"_err";
		el.className="ferror";
		el.innerHTML='* '+qerr;
		if(qdisplayError!=null){
			jlib.$(qdisplayError).appendChild(el);
		}else{
			qel.parentNode.insertBefore(el,qel);
		}
		jlib.addClassName(qel.id+'_l',"fieldError");		
	}else{
		el.innerHTML='* '+qerr;
	}
	el=null;
}
function Trim(str){
	str = LTrim(str);
	return RTrim(str);
}

function RTrim(str){
	while(str.charAt((str.length -1))==" "){
		str = str.substring(0,str.length-1);
	}
	return str;
}
function LTrim(str){
	while(str.charAt(0)==" "){
		str = str.replace(str.charAt(0),"");
	}
	return str;
}
function isEmpty(str){
	if(str==""){ return true; }else{ return false;}
}
function checkRegEx(qstr,qexp){
	return qexp.test(qstr);
}