// JavaScript Document
function valid()
{
   var f = document.appl_form;

  if(!empty_yname(f.yname,"Your Name")||!yemail(f.yemail,"Your Email ID")||!empty_yemail(f.yemail,"Your Email ID")||!empty_fname(f.fname,"Your Friends Name")||!femail(f.femail,"Email ID")||!empty_femail(f.femail,"Email ID")||!empty_message(f.message,"Your message"))
   {

   return false;              
  }
  return true; 
}


function empty_yname(name,id)
{ 

   field=name.value;
  if(field == "")
  { 
     alert("Please enter " + id);
     name.focus();
     return false;  
  }
  var digits=new Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',                       'w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',' ');
  for(var i=0; i<field.length; i++)
    {	
	var cc=field.charAt(i);
  	if(!(cc==digits[0]|| cc==digits[1]|| cc==digits[2]|| cc==digits[3]||cc==digits[4]|| cc==digits[5]|| cc==digits[6]||cc==digits[7]
  	    || cc==digits[8]|| cc==digits[9]|| cc==digits[10]|| cc==digits[11]|| cc==digits[12]|| cc==digits[13]|| cc==digits[14]||
		cc==digits[15]|| cc==digits[16]|| cc==digits[17]|| cc==digits[18]|| cc==digits[19]|| cc==digits[20]|| cc==digits[21]|| cc==
		digits[22]|| cc==digits[23]|| cc==digits[24]|| cc==digits[25]|| cc==digits[26]|| cc==digits[27]|| cc==digits[28]|| cc==
		digits[29]|| cc==digits[30]|| cc==digits[31]|| cc==digits[32]|| cc==digits[33]|| cc==digits[34]|| cc==digits[35]|| cc==
		digits[36]|| cc==digits[37]|| cc==digits[38]|| cc==digits[39]|| cc==digits[40]|| cc==digits[41]|| cc==digits[42]|| cc==
		digits[43]|| cc==digits[44]|| cc==digits[45]|| cc==digits[46]|| cc==digits[47]|| cc==digits[48]|| cc==digits[49]|| cc==
		digits[50]|| cc==digits[51]|| cc==digits[52]|| cc==digits[53]))
   {
   	alert("Name should contain only Characters");
   	name.focus();
   	return false;
   }
} 
   if(field.indexOf(" ")==0)
  {
   alert("Spaces are not allowed before the name");
    name.value=""; 
   name.focus();
   return false;     
  }
   if(field.indexOf(" ")==field.length-1)
  {
   alert("Spaces are not allowed after the name");
   name.value=""; 
   name.focus();
   return false;     
  }
  var temp = field;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ") { temp = ""; }
   name.value=temp;
 return true;
 }
 

function yemail(name,id)
{
  
  field=name.value;
  if(field == "")
  { 
     alert("Please enter " + id);
     name.focus();
     return false;  
  }
  for(i=0;i<=field.length;i++)
  {
   if((field.indexOf(" ")==i)) 
   {
	alert("Spaces are not allowed in "+ id); 
	 name.focus();
	return false;
   }
   if(field.indexOf(" ")==0)
  {
   alert("Please enter valid email without spaces");
   x.value=""
   x.focus();
   return false;     
  }
 }
   return true;
}




function empty_yemail(name,id)
 {
		x=name.value;
  if(x.charAt(0)=="@")
  {
    alert(  x + " is not a valid "  + id);
    name.focus();
    return false;
  }
    var a,b,i,count=0;
   a=x.indexOf("@");
   b=x.lastIndexOf(".");
  for(i=0;i<x.length;i++)
  {
    if(x.charAt(i)=="@")
	count++;
  }
  if(count!=1)
  {
    alert( x + " is not a valid "  + id);
    name.focus();
   return false;
  }
  if(b-a<2)
  {
    alert( x + " is not a valid " + id);
    name.focus();
   return false;
  }
 var len = (x.length)-1;
  c=len-b;
  if(c>3 || c<2 )
  {   
   alert(x + " is not a valid "  +id);
   name.focus();
   return false;
  }
  
  
  
   return true;
		
}
function empty_fname(name,id)
{ 

  field=name.value;
  if(field == "")
  { 
     alert("Please enter Receivers Name");
     name.focus();
     return false;  
  }
  var digits=new Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',                       'w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',' ');
  for(var i=0; i<field.length; i++)
    {	
	var cc=field.charAt(i);
  	if(!(cc==digits[0]|| cc==digits[1]|| cc==digits[2]|| cc==digits[3]||cc==digits[4]|| cc==digits[5]|| cc==digits[6]||cc==digits[7]
  	    || cc==digits[8]|| cc==digits[9]|| cc==digits[10]|| cc==digits[11]|| cc==digits[12]|| cc==digits[13]|| cc==digits[14]||
		cc==digits[15]|| cc==digits[16]|| cc==digits[17]|| cc==digits[18]|| cc==digits[19]|| cc==digits[20]|| cc==digits[21]|| cc==
		digits[22]|| cc==digits[23]|| cc==digits[24]|| cc==digits[25]|| cc==digits[26]|| cc==digits[27]|| cc==digits[28]|| cc==
		digits[29]|| cc==digits[30]|| cc==digits[31]|| cc==digits[32]|| cc==digits[33]|| cc==digits[34]|| cc==digits[35]|| cc==
		digits[36]|| cc==digits[37]|| cc==digits[38]|| cc==digits[39]|| cc==digits[40]|| cc==digits[41]|| cc==digits[42]|| cc==
		digits[43]|| cc==digits[44]|| cc==digits[45]|| cc==digits[46]|| cc==digits[47]|| cc==digits[48]|| cc==digits[49]|| cc==
		digits[50]|| cc==digits[51]|| cc==digits[52]|| cc==digits[53]))
   {
   	alert("Name should contain only Characters");
   	name.focus();
   	return false;
   }
} 
   if(field.indexOf(" ")==0)
  {
   alert("Spaces are not allowed before the name");
    name.value=""; 
   name.focus();
   return false;     
  }
   if(field.indexOf(" ")==field.length-1)
  {
   alert("Spaces are not allowed after the name");
   name.value=""; 
   name.focus();
   return false;     
  }
  var temp = field;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ") { temp = ""; }
   name.value=temp;
 return true;
 }
 
 

function femail(name,id)
{
  
  field=name.value;
  if(field == "")
  { 
     alert("Please enter Receivers valid Email Id");
     name.focus();
     return false;  
  }
  for(i=0;i<=field.length;i++)
  {
   if((field.indexOf(" ")==i)) 
   {
	alert("Spaces are not allowed in" + id); 
	 name.focus();
	return false;
   }
   if(field.indexOf(" ")==0)
  {
   alert("Please enter valid email without spaces");
   x.value=""
   x.focus();
   return false;     
  }
 }
   return true;
}




function empty_femail(name,id)
 {
		x=name.value;
  if(x.charAt(0)=="@")
  {
    alert(  x + " is not a valid "  + id);
    name.focus();
    return false;
  }
    var a,b,i,count=0;
   a=x.indexOf("@");
   b=x.lastIndexOf(".");
  for(i=0;i<x.length;i++)
  {
    if(x.charAt(i)=="@")
	count++;
  }
  if(count!=1)
  {
    alert( x + " is not a valid "  + id);
    name.focus();
   return false;
  }
  if(b-a<2)
  {
    alert( x + " is not a valid " + id);
    name.focus();
   return false;
  }
 var len = (x.length)-1;
  c=len-b;
  if(c>3 || c<2 )
  {   
   alert(x + " is not a valid "  + id);
   name.focus();
   return false;
  }
  
  
  
   return true;
		
}
function empty_message(name,id)
{ 

  field=name.value;
  if(field == "Please Leave your Message Here..")
  { 
     alert("Please leave some message to your friend " + id);
     name.focus();
     return false;  
  }
 return true;
 }
 
