// JavaScript Document

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")
		   return false
		}

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

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

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

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

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true
}
function validate(){
    document.getElementById("fe_username").innerHTML = "";
	document.getElementById("fe_password").innerHTML = "";
	document.getElementById("fe_retype_password").innerHTML = "";
	document.getElementById("fe_email").innerHTML = "";
	document.getElementById("fe_code").innerHTML = "";
	
	if (!validate_string(document.register.username, "fe_username", "Username is required")){
		return false;
	}
	else if (!validate_string(document.register.password, "fe_password", "Password is required")){
		return false;
	}
	else if (!validate_string(document.register.retype_password, "fe_retype_password", "Retype your Password")){
		return false;
	}
	else if (document.register.retype_password.value != document.register.password.value){
		document.getElementById("fe_retype_password").innerHTML = "Password does not match. Try again";		
		document.register.retype_password.focus();
		return false;
	}	
	else if (!validate_string(document.register.email, "fe_email", "Email Address is required")){
		return false;
	}
	else if (echeck(document.register.email.value) == false){
		document.getElementById("fe_email").innerHTML = "Invalid Email. Try Again";
		document.register.email.focus();
		return false;
	}	
	else if (!validate_string(document.register.code, "fe_code", "Enter the letters shown in the image")){
		return false;
	}
	
	return true;
}

function validate_string(field, div, display_text){
	if (field.value == ""){
		document.getElementById(div).innerHTML = display_text;
		
		field.focus();
		return false;
	}
	return true;
}


