// JavaScript Document


function validate()
{

		var name = document.forms[0].donor_name.value;
		var group_name = document.forms[0].donor_group.value;
		var dist = document.forms[0].donor_dist.value;
		var city = document.forms[0].donor_city.value;
		var password = document.forms[0].password.value;
		var email = document.forms[0].email.value;
		var no_of_donations  = document.forms[0].no_of_donations.value;

		if(trim(name)=="")
		{
		  alert("Please enter your NAME");
		  document.forms[0].donor_name.focus();
		  return false;
		}
		else if(group_name=="")
		{
			alert("Please select your BLOOD GROUP");
			document.forms[0].donor_group.focus();
			return false;
		
		}
		else if(dist=="")
		{
			alert("Please select your District");
			document.forms[0].donor_dist.focus();
			return false;
		}
		
		else if(trim(city)=="")
		{
			alert("Please enter your CITY");
			document.forms[0].donor_city.focus();
			return false;
		}
		else if((trim(email)!="")&& (!echeck(email)))
		{
		  alert("Invalid E-MAIL");
		  document.forms[0].email.focus();
		  return false;
		}
		else if(isNaN(trim(no_of_donations)))
		{
		   alert("Please Enter a valid input");
		   document.forms[0].no_of_donations.focus();
		   return false;
		}
		else if(trim(password)=="")
		{
			alert("Please enter your PASSWORD");
			document.forms[0].password.focus();
			return false;
		}
		else
		  return true;
			
	
}

function validate_edit()
{

		
		var name = document.forms[0].donor_name.value;
		var group_name = document.forms[0].donor_group.value;
		var dist = document.forms[0].donor_dist.value;
		var city = document.forms[0].donor_city.value;
		var email = document.forms[0].email.value;

		if(trim(name)=="")
		{
		  alert("Please enter your NAME");
		  document.forms[0].donor_name.focus();
		  return false;
		}
		else if(group_name=="")
		{
			alert("Please select your BLOOD GROUP");
			document.forms[0].donor_group.focus();
			return false;
		
		}
		else if(dist=="")
		{
			alert("Please select your District");
			document.forms[0].donor_dist.focus();
			return false;
		}
		
		else if(trim(city)=="")
		{
			alert("Please enter your CITY");
			document.forms[0].donor_city.focus();
			return false;
		}
		
		else if((trim(email)!="")&& (!echeck(email)))
		{
		  alert("Invalid E-MAIL");
		  document.forms[0].email.focus();
		  return false;
		}
		else
		  return true;
			
	
}


function trim(inputString) {
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { 
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { 
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for 
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
   }
   return retValue;
} 


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){
		   
		   return false
		}
         if(str.indexOf("'")!=-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					
}
