function CheckEmail(stremail)
{ 
  var err;
  var str=stremail;
  var ValidChars = "0123456789.";
  var validdom   = "0123456789";
  var illegalchars="#$!%^&*();:<>+/\?|";
  var i;
  var c;
  var l;
  var n;
  var at="@";
  var dot=".";
  var lat=str.indexOf(at);
  var lstr=str.length;
  var ldot=str.indexOf(dot);
  


    i=0;
   c = str.charAt(i); 
  
   if (ValidChars.indexOf(c) !=-1) 
   {   return false; }

   l=str.indexOf(dot)+1;
   n=str.charAt(l);
  
   if(n=="")
   { return false; }
   
   for(i=l;i<lstr;i++)
   {
     l=str.charAt(i); 
		 
	 if (validdom.indexOf(l)!=-1) 
	 { return false; }
   }
   
   	clen=illegalchars.length;
 	for(j=0;j<clen;j++)
	{
		c=illegalchars.charAt(j);
		if(str.indexOf(c)>0)
		{
		 return false;	
		}
	}
   
   
   
  if (str.indexOf(at)==-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;
   }
   
     //alert(c);
   
   if (str.indexOf(dot,(lat+2))==-1){
      return false;
   }
   
   if (str.indexOf(" ")!=-1){
      return false;
     
   }
    
      return true ;        
}



//----------------------------------Start AJAX code-----------------------------
/******************************************************************************/
//function name :createRequestObject
/******************************************************************************/    
   function createRequestObject(){//function start 
      var xmlhttp=false;      
      try{ xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");  }
      catch(e){//first catch start brace
          try{
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }catch(e){//start sec catch
              xmlhttp=false;
          }    //end sec catch                    
      }//first catch end brace  
      if(!xmlhttp && typeof XMLHttpRequest !='undefined'){
        xmlhttp=new XMLHttpRequest();      
      }return xmlhttp;  
    }//function end 
//----------------------------------End AJAX code-----------------------------


function validate_user(DOB){  
    document.forms[0].dob.value=DOB; 
    /*if(document.getElementById('chkbulkpurchase').checked){
      document.forms[0].bdate.value=bdate;  
     // alert(bdate);
      } */     
    var bln_validate=false;
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
       var radio = document.getElementById("rdogender").value;   
      //  alert(radio);
    //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&        
            var strmssg1="Fields marked with asterisk cannot be left blank.";
            var strmssg2="";      
            var objerrdiv=document.getElementById('errdiv');
			objerrdiv.style.display= "block";

			//objerrdiv.setAttribute("style", "error_message");
			
            //checking for empty textboxes
            if(document.forms[0].firstname.value==""){          
                bln_validate=true;          
            }else if(document.forms[0].lastname.value==""){         
                bln_validate=true;          
            }else if(document.getElementById("rdogender").value==""){         
                bln_validate=true;  
            }else if(document.forms[0].dob.value==""){         
                bln_validate=true;  
            }else if(document.forms[0].emailid.value==""){         
                bln_validate=true;  
            }else if(document.forms[0].permntaddress.value==""){         
                bln_validate=true;  
            }else if(document.forms[0].postcode.value==""){         
                bln_validate=true;  
            }else if(document.forms[0].city.value==""){         
                bln_validate=true;  
            }else if(document.forms[0].cmbcountry.value==""){         
                bln_validate=true;  
            }else if(document.forms[0].zip.value==""){         
                bln_validate=true;  
            }else if(document.forms[0].telephone.value==""){         
                bln_validate=true;  
            }else if(document.forms[0].password1.value==""){         
                bln_validate=true;  
            } 
            
            if(!bln_validate){
             if((document.forms[0].password1.value.length <6) ||(document.forms[0].password1.value.length>10)){
                 strmssg1="";              
                 strmssg2="Password should be more than 6 characters"
                 strmssg2+=" and less then 10 characters"
                 window.scroll(0,0)   ; 
                 document.forms[0].password1.focus();
                 bln_validate=true;
             }else if(document.forms[0].password1.value!=document.forms[0].password2.value){
                window.scroll(0,0)  ;  
                document.forms[0].password2.focus();
                strmssg1="";
                strmssg2="Passwords not matching";             
                bln_validate=true;
              }            
            }
            
            if(!bln_validate){          
               if((DOB>document.forms[0].dttoday.value)){            
                  window.scroll(0,0)    ;               
                  strmssg1="";
                  strmssg2=" Birth date is invalid ";             
                  bln_validate=true;             
               }
             }
            
             if(!bln_validate){
               if(CheckEmail(document.forms[0].emailid.value)==false){  
                    window.scroll(0,0)    
                    document.forms[0].emailid.focus();
                    strmssg1="";
                    strmssg2="Please enter a valid email id.";              
                    bln_validate=true;
               }
             }
              if(!bln_validate)
			  { 
			  
               	//check if email id is unique            
                chkValueExists(document.forms[0].username.value);              
              }
			  	else
				{
					if(navigator.appName=='Microsoft Internet Explorer')
					{
					  objerrdiv.innerText=strmssg1+strmssg2;
					}
					else if(navigator.appName=='Netscape')
					{
					  objerrdiv.textContent=strmssg1+strmssg2;     
					}
			
              
	              }             
             
              //if all textboxes are not empty, check for valid email id
              
 /**********************************************************************************************************************/             
          /* if(bln_validate){   
                if(navigator.appName=='Microsoft Internet Explorer'){
                  objerrdiv.innerText=strmssg1+strmssg2;
                }else if(navigator.appName=='Netscape'){
                  objerrdiv.textContent=strmssg1+strmssg2;     
                }                  
                  return false;                
              }else {    
                alert("submitting");            
                document.getElementById('save_mode').value='save';
                //document.frmcreateaccount.submit();                             
              } */ 
            
    }// end validate()
    
       
    
  function chkValueExists(username){
      var xmlHttp;
      var ch=0; 
      var objerrdiv=document.getElementById('errdiv');
      var num=0;                                       
      var strUrl= "userFunctions.php?validate_username=1&username="+username;
     // alert(strUrl); 
      xmlHttp=createRequestObject();    
      xmlHttp.open("GET",strUrl,true);       
      xmlHttp.onreadystatechange=function() {
              if(xmlHttp.readyState==4) {         
                      if(xmlHttp.status==200){  
                          eval("var response=("+xmlHttp.responseText+")");
                          num="";
                          errmsg=response.Match;
                          num=errmsg;
                         // alert("num"+num);    
                          if(num==1){
                            window.scroll(0,0)    
                            document.forms[0].username.focus();
                            strmssg1="";
                            strmssg2="Username is already in use";
                            if(navigator.appName=='Microsoft Internet Explorer'){
                              objerrdiv.innerText=strmssg1+strmssg2;
                            }else if(navigator.appName=='Netscape'){
                              objerrdiv.textContent=strmssg1+strmssg2;     
                            }  
                            //alert("not submitting");  
                         }
                         else{                         
                              //alert("submitting");            
                              document.getElementById('save_mode').value='save';
                              document.frmcreateaccount.submit();
                              
                         }
                                                      
                      }
              }
      }
      xmlHttp.send(null); 

}  
    
    
      function  showbulkdate(id){
        var ni = document.getElementById(id);
  		
  		
  			if (ni.style.display === 'none')
  				ni.style.display = 'block';
        else
          	ni.style.display = 'none';
      }
      
        
    
    
    
/*==========================================================================# 
# * Function for adding a Filter to an Input Field                          # 
# * @param  : [filterType  ] Type of filter 0=>Alpha, 1=>Num, 2=>AlphaNum   # 
# * @param  : [evt         ] The Event Object                               # 
# * @param  : [allowDecimal] To allow Decimal Point set this to true        # 
# * @param  : [allowCustom ] Custom Characters that are to be allowed       # 
#==========================================================================*/ 
function filterInput(filterType, evt, allowDecimal, allowCustom){ 
    var keyCode, Char, inputField, filter = ''; 
    var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
    var num   = '0123456789'; 
    // Get the Key Code of the Key pressed if possible else - allow 
    if(window.event){ 
        keyCode = window.event.keyCode; 
        evt = window.event; 
    }else if (evt)keyCode = evt.which; 
    else return true; 
    // Setup the allowed Character Set 
    if(filterType == 0) filter = alpha; 
    else if(filterType == 1) filter = num; 
    else if(filterType == 2) filter = alpha + num; 
    if(allowCustom)filter += allowCustom; 
    if(filter == '')return true; 
    // Get the Element that triggered the Event 
    inputField = evt.srcElement ? evt.srcElement : evt.target || evt.currentTarget; 
    // If the Key Pressed is a CTRL key like Esc, Enter etc - allow 
    if((keyCode==null) || (keyCode==0) || (keyCode==8) || (keyCode==9) || (keyCode==13) || (keyCode==27) )return true; 
    // Get the Pressed Character 
    Char = String.fromCharCode(keyCode); 
    // If the Character is a number - allow 
    if((filter.indexOf(Char) > -1)) return true; 
    // Else if Decimal Point is allowed and the Character is '.' - allow 
    else if(filterType == 1 && allowDecimal && (Char == '.') && inputField.value.indexOf('.') == -1)return true; 
    else return false; 
}
    
    
    
    
    