//************************************************
//checking for null
//************************************************

function checkNull(val,msg){
	flag=true;
	s=val.value
	if(s==''){
		alert(msg);
		val.focus();
		flag=false;
		return false;
		}
	else{
		flag=true;
		//return true;
		}
	if(s.charAt(0)==' '){
		alert("Blank spaces not allowed");
		val.focus();
		flag=false;
		return false;
	}else{
	flag=true;
	return true;
	}	
	}

//************************************************

//************************************************
//checking for Telephone
//************************************************

function NumCheckphone(val,msg)
{
	s=val.value;
  if (s!=''){
  
  len=s.length;
  flag=true;
  for (i=0;i<len;i++) 
  {
     if(s.charAt(i)==' ')
        {
          alert("Spaces are not allowed");
          val.select();
          val.focus();
          return false;
          break;
        }
     var ch = s.charAt(i);
                						
                 if(ch=="!"||ch=="#"||ch=="$"||ch=="%"||ch=="^"||ch=="&"||ch=="*"|| ch=="'" ||ch=='"' ||
 					ch=="("||ch==")"||ch=="=" || ch== ">" ||ch=="<"||ch=="?"
					||ch==":"||ch==";"||ch=="["||ch=="]"||ch=="{"||ch=="}"||ch=="\\"||ch=="," ||ch=="'"||ch=="~"||ch=="`"||ch=="_"||ch=="."||ch=="-")
					{
				 		alert("Special Characters are not allowed...\n\n Enter numbers only");
				 		val.select();
                        val.focus();
						return false;
						break;
					}    
     if ((s.charAt(i)<'0') || (s.charAt(i)>'9') )  
     {
        flag=false;
        break;
      }
       
     
  }
  if (flag) 
  {
     return true;
  }
  else
  {
     alert(msg);
	 val.select();
     val.focus();
     return false;
  }
  }
  return true;
}
//************************************************


//************************************************
//checking for Null and special characters
//************************************************
function checkForNull(val,msg)
{
  s=val.value;
  len=s.length;
  if (s=='')
  {
     alert(msg);
     val.focus();
     return false;
  }
  
  flag=true;
  for (i=0;i<len;i++) 
  {
     if (s.charAt(i)!= ' ') 
     {
        flag=false;
        break;
     }
  }
  if (flag)
  {
     alert(msg);
     val.value="";
     val.focus();
     return false;
  }
    	len=val.value.length
			
				for(i=0;i<=len;i++)
                {       
                	var ch = val.value.substring(i,i+1);
                						
                 if(ch=="!"||ch=="#"||ch=="$"||ch=="%"||ch=="^"||ch=="&"||ch=="*"|| ch=="'" ||ch=='"' ||
 					ch=="("||ch==")"||ch=="=" || ch== ">" ||ch=="<"||ch=="?"
					||ch==":"||ch==";"||ch=="["||ch=="]"||ch=="{"||ch=="}"||ch=="\\"||ch=="," ||ch=="'"||ch=="~"||ch=="`"||ch=="_")
					{
				 		alert("Special Characters are not allowed");
				 		val.select();
                        val.focus();
						return false;
					}
				
				}		
  return true;
}

//************************************************
//Validation for E-Mail
//************************************************
function email (val)
	{
          str=val;
	   	//flag = false;
	   if(str.value.length==0)
	   	  return true;
		if(str.value.charAt(0) ==" " || str.value.charAt(0)=="_" || str.value.charAt(0)=="-" ||str.value.charAt(0)==".")
    			{
				alert("First character should not be a blank or - in Email");
				str.select();
				str.focus();
				return false;
				}
		else
        {
			if(str != "")
        	{     
				var len = str.value.length;
                var i;
                for(i=0;i<=len;i++)
					{
								if(str.value.substring(i,i+1)==" ")
								{
									alert("Blank space not allowed in Email");
									str.select();
								    str.focus();
									return false;
								}
					}
                var a1 = str.value.indexOf("@",0);
			    var a2 = str.value.indexOf(".",0);
				
				if(a1==0 && a2==1)
				{
				return false;
				}
				if(a1==-1 )
				{
					alert("Please Enter proper Email-id");
					str.select();
                    str.focus();
					return false;
                }  
				if(a1==0)
				{
					alert(" @ cannot be first character or u may have missed an @.");
					str.select();
                    str.focus();
					return false;
                }  

	
				if(a1==a1 && a2==a1+1)
				{
					alert("dot cannot be after @");
					str.select();
                    str.focus();
					return false;
				}	
				if(a1==str.value.length-1)
				{
					alert("@ cannot be last character");
					str.select();
                    str.focus();
					return false;
				}
				if(a2==-1)
				{
					alert("You are missed dot(.) in Email");
					str.select();
                    str.focus();
					return false;
				}
				if(a2==0)
				{
					alert("Dot (.) cannot be first character Email");
					str.select();
                    str.focus();
					return false;
				}
				
				if(a2 == len-1)
				{
					alert(" Last Character should not be a Dot");
					str.select();
                    str.focus();
					return false;
				}
				if(a1 == len-1)
				{
					alert(" Last Character should not be a @");
					str.select();
                    str.focus();
					return false;
				}
				var ats=0;
				for(i=0;i<=len;i++)
                {       
                	var ch = str.value.substring(i,i+1);
                	str.select();
					if(ch=="@")
					ats++;
					if(ats>1)
					{
						alert("More than one @ not allowed in Email");
						str.select();
                        str.focus();
						return false;
					}					
                 if(ch=="!"||ch=="#"||ch=="$"||ch=="%"||ch=="^"||ch=="&"||ch=="*"|| ch=="'" ||ch=='"' ||
 					ch=="("||ch==")"||ch=="+"||ch=="="||ch=="|" || ch== ">" ||ch=="<"||ch=="?"||ch=="/"
					||ch==":"||ch==";"||ch=="["||ch=="]"||ch=="{"||ch=="}"||ch=="\\"||ch==",")
					{
				 		alert("Invalid Character in your Email id " );
				 		str.select();
                        str.focus();
						return false;
					}
				
				}
			}		
		}
	
   return(true);
   
     }

//************************************************



//************************************************
//Numbers check only
//*************************************************

function NumCheckOnly(val,msg)
{
  s=val.value;
  flag=true;
  Len=s.length;
 
  for (i=0;i<Len;i++) 
  {
     if ((s.charAt(i)<'0') || (s.charAt(i)>'9') )  
     {
        flag=true;
      }
     else
       {
         flag=false;
         break;
       }  
    
  }
  if (flag) 
  {
     return true;
  }
  else
  {
     alert(msg);
	 val.select();
     val.focus();
     return false;
  }
}
//*************************************************

//************************************************
function chkmin(val,msg,intMax)
{
	
   if(val.value.length<intMax)
     {
       alert(msg);
       val.select();
       val.focus();
       return false;
     }
   else
      return true;
} 
//********************************************************************
function chkmin1(val,msg,intMax)
{
if(val.value == "")
{
	return true;
}	
else if((val.value.length>0)&&(val.value.length<intMax))
     {
       alert(msg);
       val.select();
       val.focus();
       return false;
     }
 } 


//*********************************************************
//VALIDATION CHECK
//*********************************************************
//*****************************************************

function check()
{
flag=true;
flag=flag&&checkNull(document.frmContactUs.txtCName,"Please Enter CompanyName.....");
flag=flag&&checkNull(document.frmContactUs.txtFName,"Please Enter FirstName.....")&&NumCheckOnly(document.frmContactUs.txtFName,"Numbers are not allowed....");
flag=flag&&checkNull(document.frmContactUs.txtLName,"Please Enter LastName......")&&NumCheckOnly(document.frmContactUs.txtLName,"Numbers are not allowed....");
flag=flag&&checkNull(document.frmContactUs.txtadd,"Please Enter Address.......");
flag=flag&&checkNull(document.frmContactUs.txtCity,"Please Enter City......")&&NumCheckOnly(document.frmContactUs.txtCity,"Numbers are not allowed....");
flag=flag&&checkNull(document.frmContactUs.txtState,"Please Enter State......")&&NumCheckOnly(document.frmContactUs.txtState,"Numbers are not allowed....");
flag=flag&&checkForNull(document.frmContactUs.txtzip,"Zip code is empty.....\n\nPlease Re-Enter Zipcode")&&NumCheckphone(document.frmContactUs.txtzip,"Characters are not allowed....");
flag=flag&&NumCheckphone(document.frmContactUs.txtPhone,"Phone Number should be Numeric")&&chkmin1(document.frmContactUs.txtPhone,"Enter valid Phonenumber,\n\n Please include STD Code also \n\n if outsider  include ISDCode",9);
flag=flag&&NumCheckphone(document.frmContactUs.txtfax,"Fax Number should be numeric")&&chkmin1(document.frmContactUs.txtfax,"Enter valid Faxnumber,\n\n Please include STD Code also \n\n if outsider  include ISDCode",9);
flag=flag&&checkNull(document.frmContactUs.txtemail ,"Email cannot be empty....\n\nPlease Re-enter the email address")&&email(document.frmContactUs.txtemail);
return flag;
}