function validate() 
{
  var f = document.frm_key_fill; 
  var fname=f.txt_name.value // Name Validation
   if (trim(fname) == "") { 
      alert("Name should not be empty !!");
	  f.fname.value = "";
      f.txt_name.focus();
      return false;
    }
	flen	= fname.length;
	for (i=0; i<flen; i++) {
	  schar = fname.charAt(i);
	  if ( schar >= '0' && schar <= '9' ) {
		  alert("Numbers not allowed in Name");
		  f.txt_name.focus();
		  return false;
	   }
	  var valid_chars =  new RegExp ("[a-zA-Z ]", "g");
	  if( !valid_chars.test(schar) ) {
		 alert("Special characters like " + schar + " are not allowed in Name ");
		 f.txt_name.focus();
		 return false;
	  }
	  var valid_chars = null;
	}//Name Validation ends
	 var val=f.mob_no.value; // Mobile Validation
		if(trim(val) =="")
		{
		  alert("Please Enter Mobile No");
		  f.mob_no.focus();
		  return false;	
		}
		if (isNaN(val)){
          alert("Please Enter Mobile No");
		  f.mob_no.focus();
		  return false;	
		} 
		if(val.length <10 || val.length >10 )
		{
          alert("Please Enter 10 digit Mobile No");
		  f.mob_no.focus();
		  return false;	
		}
                for (i=0; i<val.length; i++) {
                 schar_mob = val.charAt(i);
                 var valid_chars =  new RegExp ("[0-9]", "g");
                 if( !valid_chars.test(schar_mob) ) {
                  alert("Character " + schar_mob + " not allowed in Mobile No");
                  f.mob_no.focus();
                  return false;
                 }
                 var valid_chars = null;
               } //mobile Validation Ends
		 var em 	= f.txt_email_id.value; //email Validation
     if (trim(em)!="") {
	   if((f.txt_email_id.value.indexOf('@')=="-1")||(f.txt_email_id.value.indexOf('.')=="-1")) {
			alert("Please Enter EMail ID properly");
			f.txt_email_id.focus();
			return false;
		  }
	   if (!isEmailAddr1(em)) {
			alert("Please Enter EMail ID properly");
			document.frm.txt_email_id.focus();
			return false;
		}
		if(!isEmailAddr(em,"Email Id")){
			alert("Please Enter EMail ID properly");
			document.frm.txt_email_id.focus();
			return false;
		}
	 }
	 else {
		  alert("EMail ID should not be empty !! ");
		  f.txt_email_id.focus();
		  return false;
	 }
	 if(!charvalidation("email",f.txt_email_id.value)){
	 f.txt_email_id.focus();
	  return false;
	} //Email validation Ends
	
     var des_key=f.txt_dkey.value; // Desired validation
	 if(trim(des_key) =="")
		{
		  alert("Please Enter Desired Keyword");
		  f.txt_dkey.focus();
		  return false;	
		}
	     for(i=0; i<des_key.length; i++)
	      {
	        schar = des_key.charAt(i);
		valid_chars_dkey = new RegExp ("[a-zA-Z0-9, ]","g");
	       if( !valid_chars_dkey.test(schar) )
				{
				alert("Special characters like " + schar + " are not allowed in Desired Keyword ");
				f.txt_dkey.focus();
				return false;
				}
			var valid_chars = null;
		}// Desired validation Ends
		document.frm_key_fill.submit();
		//window.location.href="index.php";
	 
  }
function trim(inputString)  // To validate the spaces 
{
		if (typeof inputString != "string") { return inputString; }
			var retValue = inputString;
			var ch = retValue.substring(0, 1);
			while (ch == " ") { // Check for spaces at the beginning of the string
				retValue = retValue.substring(1, retValue.length);
				ch = retValue.substring(0, 1);
			}
			ch = retValue.substring(retValue.length-1, retValue.length);
			while (ch == " ") { // Check for spaces at the end of the string
				retValue = retValue.substring(0, retValue.length-1);
				ch = retValue.substring(retValue.length-1, retValue.length);
			}
			while (retValue.indexOf("  ") != -1) { 
						retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
			}
			return retValue; // Return the trimmed string back to the user
} 
function isEmailAddr1(email)
{
		  var result = false;
		  var theStr = new String(email);
		  var index = theStr.indexOf("@");
		  if (index > 0)
		  {
			var pindex = theStr.indexOf(".",index);
			if ((pindex > index+1) && (theStr.length > pindex+1))
			result = true;
		  }
		  return result;
       }
		function isEmailAddr(email,Label) 
	    {
			if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
			return (true)
			}
			var valid_alter_email = new RegExp ("-", "g");
			 if(!valid_alter_email.test(email)){
				return (true);
			}
			alert("Invalid "+Label+" Address! Please re-enter.")
			return (false)
}
function charvalidation(regexp,fvalue) 
{
   var valid_characters;
	var invalid_characters='';
    var	lastchar;
	var setcomma;
	for (var i = 0; i < fvalue.length; i++)
	{
	   if(regexp=="email"){
		// valid_characters = new RegExp (\"[a-zA-Z0-9_]\", \"g\");\n";
		 valid_characters = new RegExp ("[a-zA-Z0-9_.@]", "g");
		}
        part_user_name = fvalue.charAt (i);
		if (!valid_characters.test(part_user_name))
		{
				if(invalid_characters=='' || invalid_characters==null){
				invalid_characters=part_user_name;
				}else{
				if(invalid_characters.indexOf(part_user_name)==-1)
				invalid_characters=invalid_characters+" "+part_user_name;
				}
		}
		valid_characters = null;
	}

	if(invalid_characters!='' ){
		if(invalid_characters.length>1){
			if(invalid_characters=='')
 			 alert("Empty Space are not allowed in Email Address");
			else
			 alert("Special characters like '" + invalid_characters + "' are not allowed");
		}else{
			if(invalid_characters=='')
 			 alert("Empty Space are not allowed in Email Address");
			else
			 alert("Special characters like '" + invalid_characters + "' are not allowed");
		}
		return false;
	}
	return true;
}
