// Modular Form Validation v0.2
// by Llorenç Herrera
// Copyright (c) Hexoplastia 2000

function ValidateForm( wich ){

	DisableForm( wich );
	
	SubmitButtonValueChange = wich.SubmitButtonValueChange.value.split('&');

	ChangeButtonCaption( eval('wich.'+SubmitButtonValueChange[0]), SubmitButtonValueChange[2] );
	
	ErrorList = "";

	if( wich.RequiredFields ){
	
		RequiredFieldsArray = wich.RequiredFields.value.split('&');
		
		for( i=0; i<RequiredFieldsArray.length; i++){
		
			RequiredFieldData = RequiredFieldsArray[i].split(':');
			FieldValue = eval('wich.'+RequiredFieldData[0]+'.value');
			
			switch( RequiredFieldData[2] ){
			
				case "0": // Must be filled with anything
					if( FieldValue == "" ){
						ErrorList = ErrorList+RequiredFieldData[1]+"\n";
					}
					break;
					
				case "1": // EMail validation
					if( !emailCheck( FieldValue ) && FieldValue != "" ){
						ErrorList = ErrorList+RequiredFieldData[1]+"\n";
					}
					break;
					
				case "50": // If the specified CheckBox is checked, checks for requiring the specified field to be filled
					FirstFieldValue = eval('wich.'+RequiredFieldData[0]+'.checked');
					SecondFieldValue = eval('wich.'+RequiredFieldData[3]+'.value');
					if( FirstFieldValue && SecondFieldValue == "" ){
						ErrorList = ErrorList+RequiredFieldData[1]+"\n";
					}
					break;
			}
		}
		
		if( ErrorList != "" ){
			ErrorList = wich.Text_ErrorHeader.value+"\n\n"+ErrorList;
			alert( ErrorList );
			EnableForm( wich );
			ChangeButtonCaption( eval('wich.'+SubmitButtonValueChange[0]), SubmitButtonValueChange[1] );
			return false;
		}
		
	}
	
	return true;
	
}

function ChangeButtonCaption( Button, Caption )
{
	Button.value = Caption;
}

function DisableForm( wich ){
	if( document.all || document.getElementById ){
		for( i=0; i<wich.length; i++ ){
			var tempobj = wich.elements[i];
			var type = tempobj.type.toLowerCase();
			if( type == "submit" || type == "reset" ){
				tempobj.disabled = true;
			}
		}
	}
}

function EnableForm(wich){
	if( document.all || document.getElementById ){
		for( i=0; i<wich.length; i++ ){
			var tempobj = wich.elements[i];
			var type = tempobj.type.toLowerCase();
			if( type == "submit" || type == "reset" ){
				tempobj.disabled = false;
			}
		}
	}
}

function emailCheck (emailStr) {
	// EMail validation function
	// Changes:  Sandeep V. Tamhankar (stamhankar@hotmail.com)
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		//alert("Email address seems incorrect (check @ and .'s)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	if (user.match(userPat)==null) {
	    //alert("The username doesn't seem to be valid.")
	    return false
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
		        //alert("Destination IP address is invalid!")
				return false
		    }
	    }
	    return true
	}

	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		//alert("The domain name doesn't seem to be valid.")
	    return false
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
	   //alert("The address must end in a three-letter domain, or two letter country.")
	   return false
	}

	if (len<2) {
	   //var errStr="This address is missing a hostname!"
	   //alert(errStr)
	   return false
	}

	return true;
}

