// Form Validator
// Created on : 12/03/2004
// Created by : Deepak S.
// Last Modified on : 13/03/2004

function isFormClear()
{	
	document.form1.name.value="";
	document.form1.email.value="";
	document.form1.tele.value="";
	document.form1.comment.value="";
	//alert(window.location.href);
}

// returns true if the string is a valid email

function isEmail(str){
	if(isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}

// returns true if the string only contains characters A-Z or a-z

function isAlpha(str){
	var re = /[^a-z A-Z]/g
	if (re.test(str)) return false;
	return true;
}

// returns true if the string only contains characters 0-9

function isNumeric(str){
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}

// returns true if the string only contains characters A-Z, a-z or 0-9

function isAlphaNumeric(str){
	var re = /[^a-z A-Z 0-9]/g
	if (re.test(str)) return false;
	return true;
}

// returns true if the string is empty

function isEmpty(str){
	return (str == null) || (str.length == 0);
}

// returns true if the string is a phone number formatted as number.

function isPhoneNumber(str){	
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}
// validate the form
function validateForm(f, preCheck){
	var errors = '';
	if(preCheck != null) errors += preCheck;
	var i,e,t,n,v;
	for(i=0; i < f.elements.length; i++){
		e = f.elements[i];
		if(e.optional) continue;
		t = e.type;
		n = e.name;
		v = e.value;
		if(t == 'text' || t == 'password' || t == 'textarea'){
			if(isEmpty(v)){
				errors += 'Please Enter Valid - '+n;
				errors += '\n'; continue;
				
			}
			if(e.isAlpha){
				if(!isAlpha(v)){
					errors += n+' can only contain characters A-Z a-z.\n'; continue;
				}
			}
			if(e.isNumeric){
				if(!isNumeric(v)){
					errors += n+' can only contain characters 0-9.\n'; continue;
				}
			}
			if(e.isAlphaNumeric){
				if(!isAlphaNumeric(v)){
					errors += n+' can only contain characters A-Z a-z 0-9.\n'; continue;
				}
			}
			if(e.isEmail){
				if(!isEmail(v)){
					errors += v+' Invalid Email.\n'; continue;
				}
			}			
			if(e.isPhoneNumber){
				if(!isPhoneNumber(v)){
					errors += v+' Invalid phone number.\n'; continue;
				}
			}			
		}		
	}
	if(errors != '') alert(errors);
	return errors == '';
}

// This function configures the previous form validation code for this form.

function configureValidation(f){
	f.name.isAlphaNumeric = true;	
	f.email.isEmail = true;
	f.tele.isPhoneNumber = true;	
	f.comment.isEmpty = true;
	var preCheck =null;
	return validateForm(f, preCheck);
}

