/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID! 0");
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail! 1");
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail! 2");
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail! 3");
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail! 4");
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail! 5");
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail! 6");
		    return false;
		 }

 		 return true;				
	}


function validate_required(field,alerttxt){
	with (field){
		 if (value==null||value==""){
		    alert(alerttxt);
		    return false;
		 }else {
		    return true;
		 }
	}
}

function verify_fields(field1, field2, alerttxt){
	if(field1.value != field2.value){
		alert(alerttxt);
		
		return false;
	}else{
		return true;
	}
}

function validate_form(thisform){
	document.getElementById(thisform.email.name).style.color = "#444";
	document.getElementById(thisform.cemail.name).style.color = "#444";

	with (thisform){
		if (validate_required(email,"E-mail must be filled out!")==false){
			document.getElementById(email.name).style.color = "Red";
			return false;
		}else if(validate_required(cemail, "Please verify e-mail!")==false){
			document.getElementById(cemail.name).style.color = "Red";
			return false;
		}else if(!echeck(email.value)){
			document.getElementById(email.name).style.color = "Red";
			//email.focus();
			return false;
		}else if(!echeck(cemail.value)){
			document.getElementById(cemail.name).style.color = "Red";
			return false;
		}else if(!verify_fields(email, cemail, "E-mails do not match!")){
			document.getElementById(email.name).style.color = "Red";
			document.getElementById(cemail.name).style.color = "Red";
			//email.focus();
			return false;
		}else{
			return true;
		}
		
		
	}
}

