function clsZFieldValidation(Form,Field,Caption){

	this.Field=Field;
	this.Form=Form;
	this.MustFill=true;
	this.PhoneNumber=false;
	this.Email=false;
	this.CheckBox=false;	
	this.Caption=Caption;
	this.FieldFilled=FieldFilled;
	this.Validate=Validate;
	this.EmailIsValid=EmailIsValid;
		
}
function Validate(ShowMessage){

	var Result=true;
	if (this.MustFill){
		Result=this.FieldFilled(ShowMessage);
	}
	
	if (!Result) return false;
	
	if (this.Email){
		ValidEmail=this.EmailIsValid();
		if (!ValidEmail){
			Result=false;			
			alert("You entered '" + this.Field.value + "' which is not a valid email address");
			this.Field.focus();
		}
	}
	
	if (!Result) return false;
	
	
	return Result;
}
function FieldFilled(ShowMessage){
	var Result=false;
	Form=this.Form;
	Field=this.Field;
	if (this.CheckBox){
		if (Field.checked){
			Result= true;
		}else{
			Result= false;
		}
	}else{
		if ((Field.value==null)||(Field.value==""))
		{
			Result=false;
		}else{
			Result=true;
		}
	}
	
	if ((!Result) && (ShowMessage)){
		alert("Please Enter your " + this.Caption)
		Field.focus();		
	}
	
	return Result

}

function clsZFormValidation(){
	
	this.Fields=new Array();	
	this.AddField=AddField;
	this.Validate=ZFormValidate;
	this.ChangeLastAddedProperty=ChangeLastAddedProperty;
}

function AddField(Form, Field, Caption){		
		var FieldToAdd = new clsZFieldValidation(Form,Field,Caption);
		this.Fields.push(FieldToAdd);
		
		
}
function ChangeLastAddedProperty(Property,NewValue){
	this.Fields[this.Fields.length-1][Property]=NewValue;
}

function ZFormValidate(){
	var FieldLoop;
	var FieldIndex;
	var ReturnValue;
	
	
	for (FieldIndex in this.Fields)	{		
		FieldLoop=this.Fields[FieldIndex];
		ReturnValue=FieldLoop.Validate(true);
		if (!ReturnValue){
			return false;
		}
	}
	return true;
}



function EmailIsValid() {

		str=this.Field.value;
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}
