/**********************************************************
	Project Name			: ProactiveFamilySolutions
	Site URL				: http://www.proactivefamilysolutions.com
	Project Phase			: User Phase
	Module Name				: User Registration JS file
	Database Name			: proactiv_proact
	Page Name				: register.js
	Purpose of this page	: This page is used for validation of User Registration Form.
	Last Modified By		: Srinivasn.R.R
	Last Modified Date		: 10 July 2008
	Last Modified Purpose	: For writting Code Comments
 *********************************************************/
function isCharsInBag (s, bag)
{  
    var i;
    for (i = 0; i < s.length; i++)
    {   
       
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) return false;
    }
    return true;
}


function isEmail (s)
	{
	    var i = 1;
	    var j=0;
	    var ar=0;
	    var m=0;
	    var sLength = s.length;
	    
	    while (i < sLength)
	    {
			if (s.charAt(i) == "@")j++;			
			i++;
	    }
	    i=1;
	    while (i < sLength)
	    {
			if (s.charAt(i) == ".")ar++;			
			i++;
	    }
		m=sLength-1;
	    if (j==0) return false;
	    if (ar==0) return false;
	    if (j>1) return false;
	    if (s.charAt(1) == "@")  return false;
	    if (s.charAt(1) == ".")  return false;
	    if (s.charAt(m) == "@") return false;
	    if (s.charAt(m) == ".") return false;
	 		return true;
	 }
function ext(s)
{
        var i,pos=0;
        for (i = 0; i < s.length; i++)
        {
                var c = s.charAt(i);
                if (c==".") pos=i;
        }
        var extValue="";
        for (i=pos+1;i<=s.length;i++)
        {
                extValue =extValue+s.charAt(i);
        }

        if (extValue != 'jpeg' && extValue != 'JPEG' && extValue != 'jpg' && extValue != 'JPG' && extValue != 'png' && extValue != 'PNG')
        return false;
    else
  		document.register.ftype.value=extValue;
	return true;
}
function trimString (str)
{
	str = this != window? this : str;
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
function isCharsInBag (s, bag)
{
    var i;
    for (i = 0; i < s.length; i++)
    {

        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) return false;
    }
    return true;
}

function isFloatValue(s)
  {
            var i,counter=0;
            for (i = 0; i < s.length; i++)
            {
                var c = s.charAt(i);
                if (c==".") counter++;
            }
            if (counter>1)
            return false;
            else
            return true;
  }
function chkadd()
{


						


/*------------------------------------*/		
				
/*-----------------------------------------*/

/*********************** If username is equal to null validation starts here **********************/
if (trimString(document.register.UsrName.value)=="")

                        {
                                alert( "User Name should not be blank" );
                                document.register.UsrName.focus();
                                  return false;
                        }
/*********************** If username is not equal to null validation starts here **********************/
if (trimString(document.register.UsrName.value)!="")
{
	var uname=trimString(document.register.UsrName.value);
	var numval=isNaN(uname);
	if (numval==false)
	{
		alert("User Name should be alphanumeric");
		document.register.UsrName.focus();
		return false;
	}
	if (!isCharsInBag(uname, "_.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" ))
	{
		alert("Invalid characters found in User Name" );
		document.register.UsrName.focus();
		return false;
	}
	var unamelen=document.register.UsrName.value.length;
	if (unamelen<6 || unamelen>15)
	{
		alert("User Name should be 6 to 15 characters" );
		document.register.UsrName.focus();
		return false;
	}
}
/*********************** If firstname is equal to null validation starts here **********************/
if (trimString(document.register.ChvFName.value)=="")

                        {
                                alert( "First Name should not be blank" );
                                document.register.ChvFName.focus();
                                  return false;
                        }
/*********************** If firstname is not equal to null validation starts here **********************/
if (!isCharsInBag(document.register.ChvFName.value, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ))
				{
					alert("Your First Name should be in characters" );
					document.register.ChvFName.focus();
					return(false);
				}

if (document.register.ChvFName.value.length > 25)

                        {
                                alert( "Maximum character length should be 25  in the \"First Name\" field." );
                                document.register.ChvFName.focus();
                                  return false;
                        }
/*********************** If middlename is equal to null validation starts here **********************/
if (!isCharsInBag(document.register.ChvMName.value, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ))
				{
					alert("Your Middle Name should be in characters" );
					document.register.ChvMName.focus();
					return(false);
				}
/*********************** If middlename is not equal to null validation starts here **********************/
if (document.register.ChvMName.value.length > 25)

                        {
                                alert( "Maximum character length should be 25  in the \"Middle Name\" field." );
                                document.register.ChvMName.focus();
                                 return false;
                        }
/*********************** If lastname is equal to null validation starts here **********************/
if (trimString(document.register.ChvLName.value)==""  )

                        {
                                alert( "Last Name should not be blank" );
                                document.register.ChvLName.focus();
                                return false;
                        }
/*********************** If lastname is not equal to null validation starts here **********************/
if (!isCharsInBag(document.register.ChvLName.value, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ))
				{
					alert("Your Last Name should be in characters" );
					document.register.ChvLName.focus();
					return(false);
				}
if (document.register.ChvLName.value.length > 25)

                        {
                                alert( "Maximum character length should be 25  in the \"Last Name\" field." );
                                document.register.ChvLName.focus();
                                return false;
                        }
/*********************** If address is equal to null validation starts here **********************/
if (trimString(document.register.chvAdd1.value)==""  )

                        { 
                                alert( "Address1 should not be blank" );
                                document.register.chvAdd1.focus();
                                return false;
                        }
/*if (!isCharsInBag(document.register.chvAdd1.value, " _()./-#&',0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ))
				{
					alert("Only letters (a-z), numbers (0-9), and #-_/&(). are allowed" );
					document.register.chvAdd1.focus();
					return false;
				}
*/
/*********************** If cityname is equal to null validation starts here **********************/
if (trimString(document.register.chvCity.value)==""  )

                        {
                                alert( "City should not be blank" );
                                document.register.chvCity.focus();
                                 return false;
                        }
/*********************** If cityname is not equal to null validation starts here **********************/
if (!isCharsInBag(document.register.chvCity.value, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ))
				{
					alert("Your city should be in characters" );
					document.register.chvCity.focus();
					return false;
				}

/*********************** If stat ID is equal to null validation starts here **********************/
if (trimString( document.register.intStateId.value)==""  )

                        {
                                alert( "State should be  selected " );
                                document.register.intStateId.focus();
                                  return false;
                        }


/*********************** If country ID is equal to null validation starts here **********************/
if (trimString(document.register.IntCountryId.value)==""  )

                        {
                                alert( "Country should not be blank" );
                                document.register.IntCountryId.focus();
                                 return false;
                      }




/*********************** If zipcode is equal to null validation starts here **********************/

if (trimString( document.register.intZip.value)==""  )

                        {
                                alert( "Zip/Postal code should not be blank" );
                                document.register.intZip.focus();
                                  return false;
                        }
	/*********************** If zipcode is not equal to null validation starts here **********************/					
                        if (trimString( document.register.intZip.value)==("000000"|"00000"|"0000"|"000"|"00"|"0"))

                        {
                                alert( "Zip/Postal code should not be zero format" );
                                document.register.intZip.focus();
                                  return false;
                        }
          if (!isCharsInBag(document.register.intZip.value, "0123456789" ))
                                {
                                        alert("Invalid characters in Zip (or)Postal code  field" );
                                        document.register.intZip.focus();
                                        return(false);


                        if (!isFloatValue(document.register.intZip.value))
                                {
                                        alert("Invalid characters in \" Zip/Postal code  \" field " );
                                        document.register.intZip.focus();
                                        return(false);
                                }		
								
                                }
	//var ziptot=document.getElementById("intZip").value.length;
	

/*********************** If phone number 1 is equal to null validation starts here **********************/
var ph1=document.getElementById("ph1").value; 
	if(ph1=="")
			{
			alert("Phone number should not be empty");
//			document.register.ph1.focus();
			document.getElementById("ph1").focus();
			return false;
			}
/*********************** If phone number 1 is not equal to null validation starts here **********************/		
		if (!isCharsInBag(document.getElementById("ph1").value,"0123456789" ))
                                {
                                        alert("Phone numbers must be numeric" );
                                     document.register.ph1.focus();
                                        return false;
								}
			var l1=document.getElementById("ph1").value.length;
	
			if(l1<3)
			{
			alert("Enter 3 digit code");
			document.register.ph1.focus();
			return false;
			}						
/*********************** If phone number 2 is equal to null validation starts here **********************/
			if(document.register.ph2.value=="")
			{
			alert("Phone number should not be empty");
			document.register.ph2.focus();
			return false;
			}
/*********************** If phone number 2 is not equal to null validation starts here **********************/
			if (!isCharsInBag(document.register.ph2.value, "0123456789" ))
                                {
                                        alert("Phone numbers must be numeric" );
                                      document.register.ph2.focus();
                                        return false;
									}
								var l2=document.getElementById("ph2").value.length;
	
			if(l2<3)
			{
			alert("Enter 3 digit code");
			document.register.ph2.focus();
			return false;
			}						

/*********************** If phone number 3 is equal to null validation starts here **********************/					
									
									
			if(document.register.ph3.value=="")
			{
			alert("Phone number should not be empty");
		document.register.ph3.focus();
			return false;
			}
/*********************** If phone number 3 is not equal to null validation starts here **********************/
			if (!isCharsInBag(document.register.ph3.value, "0123456789" ))
                                {
                                        alert("Phone numbers must be numeric" );
                                      document.register.ph3.focus();
                                        return false;
									}
						var l3=document.getElementById("ph3").value.length;
	
			if(l3<4)
			{
			alert("Enter 4 digit code");
			document.register.ph3.focus();
			return false;
			}							
/*********************** If fax number 1 is not equal to null validation starts here **********************/
			
		if(document.register.fax1.value!="")
			{
						
						if (!isCharsInBag(document.register.fax1.value, "0123456789" ))
											{
													alert("Fax numbers must be numeric" );
												  document.register.fax1.focus()
													return(false);
											}
						var l1=document.getElementById("fax1").value.length;
				
						if(l1<3)
						{
						alert("Enter 3 digit code");
						document.register.fax1.focus();
						return false;
						}						
			
						if(document.register.fax2.value=="")
						{
						alert("Fax number should not be empty");
						document.register.fax2.focus();
						return false;
						}
						if (!isCharsInBag(document.register.fax2.value, "0123456789" ))
											{
													alert("Fax numbers must be numeric" );
												  document.register.fax2.focus()
													return(false);
												}
											var l2=document.getElementById("fax2").value.length;
				
						if(l2<3)
						{
						alert("Enter 3 digit code");
						document.register.fax2.focus();
						return false;
						}						
			
												
												
												
						if(document.register.fax3.value=="")
						{
						alert("Fax number should not be empty");
						document.register.fax3.focus();
						return false;
						}
						if (!isCharsInBag(document.register.fax3.value, "0123456789" ))
											{
													alert("Fax numbers must be numeric" );
												  document.register.fax3.focus()
													return(false);
												}
									var l3=document.getElementById("fax3").value.length;
				
						if(l3<4)
						{
						alert("Enter 4 digit code");
						document.register.fax3.focus();
						return false;
						}	
			}
			/*------------------------- End of Fax----------------------------------*/  
						
		/*----------------------------  End of phone validation -------------------------------*/

/*

if(trimString(document.register.chvRefEmail.value)=="")
                        {
                                alert( " Referrers E-mail should not be blank" );
                                document.register.chvRefEmail.focus();
                                  return false;
                        }
						*/

/*********************** If Email ID is equal to null validation starts here **********************/
if(trimString(document.register.chvEmail.value)=="")
                        {
                                alert( "E-mail should not be blank" );
                                document.register.chvEmail.focus();
                                  return false;
                        }
/*********************** If Email ID is not equal to null validation starts here **********************/
                if ((document.register.chvEmail.value!=""))
					
					{
					if(document.register.chvEmail.value.length>50)
					{
					alert("Maximum character length should be 50 in the \"  Email Id \" field.");
					document.register.chvEmail.focus();
					return(false);
					}
					if ((document.register.chvEmail.value=="")||((isCharsInBag(document.register.chvEmail.value, "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@.0123456789"))&&(document.register.chvEmail.value.length>9)&&(document.register.chvEmail.value.length<50)&&(isEmail(document.register.chvEmail.value)))) advemail=0;
					else advemail=1;
					if (advemail==1) 
					{
					alert("Enter valid E-mail Id");
					document.register.chvEmail.focus();
					return(false);
					}
					}
/*********************** If confirm Email ID is equal to null validation starts here **********************/
if(trimString(document.register.chvREmail.value)=="")
                        {
                                alert( " Confirm E-mail should not be blank" );
                                document.register.chvREmail.focus();
                                  return false;
                        }
/*********************** If confirm Email ID is not equal to null validation starts here **********************/
                if ((document.register.chvREmail.value!=""))
					
					{
					if(document.register.chvREmail.value.length>50)
					{
					alert("Maximum character length should be 50 in the \"Confirm  Email Id \" field.");
					document.register.chvREmail.focus();
					return(false);
					}
					if ((document.register.chvREmail.value=="")||((isCharsInBag(document.register.chvREmail.value, "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@.0123456789"))&&(document.register.chvREmail.value.length>9)&&(document.register.chvREmail.value.length<50)&&(isEmail(document.register.chvREmail.value)))) advemail=0;
					else advemail=1;
					if (advemail==1) 
					{
					alert("Enter the valid Confirm E-mail Id.");
					document.register.chvREmail.focus();
					return(false);
					}
					}

					

/*********************** If Email ID and confirm Email ID is not equal to null validation starts here **********************/		   
if (document.register.chvEmail.value!=document.register.chvREmail.value)
						{
							alert("E-mail Address and  Confirm E-mail Address do not match");
							document.register.chvREmail.focus();
							return false;
						}  


/*********************** If password is equal to null validation starts here **********************/
if (trimString(document.register.chvUsrPwd.value)==""  )

                        {
                                alert( "Password should not be blank" );
                                document.register.chvUsrPwd.focus();
                                  return false;
                        }
/*********************** If password is not equal to null validation starts here **********************/
if(!isCharsInBag(document.register.chvUsrPwd.value,"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ))
                        {
                                alert( "Invalid characters in  Password" );
                                document.register.chvUsrPwd.focus();
                                  return false;
                        }
if (document.register.chvUsrPwd.value.length<6)

                        {
                                alert( "Passwords must be at least six (6) characters" );
                                document.register.chvUsrPwd.focus();
                                  return false;
                        }
if (document.register.chvUsrPwd.value.length>12)

                        {
                                alert( "Passwords must be maximum of twelve (12) characters" );
                                document.register.chvUsrPwd.focus();
                                  return false;
                        }
/*********************** If confirm password is equal to null validation starts here **********************/
if (trimString(document.register.chvRUsrPwd.value)==""  )

                        {
                                alert( "Confirm Password should not be blank" );
                                document.register.chvRUsrPwd.focus();
                                  return false;
                        }

/*********************** If confirm password is not equal to null validation starts here **********************/
if(!isCharsInBag(document.register.chvRUsrPwd.value,"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ))
                        {
                                alert( "Invalid characters in Confirm Password" );
                                document.register.chvRUsrPwd.focus();
                                  return false;
                        }
if (document.register.chvRUsrPwd.value.length<6)

                        {
                                alert( "Re-Passwords must be at least six (6) characters" );
                                document.register.chvRUsrPwd.focus();
                                  return false;
                        }

	  
if (document.register.chvUsrPwd.value!=document.register.chvRUsrPwd.value)
						{
							alert("Password and Confirm password do not match");
							document.register.chvRUsrPwd.focus();
							return false;
						}


/*if (trimString( document.register.ChvquestId.value)==""  )

                        {
                                alert( "Secret Question should not be blank" );
                                document.register.ChvquestId.focus();
                                  return false;

						} 

						if (trimString( document.register.ChvAnswer.value)==""  )

                        {
                                alert( "Secret Answer should not be blank" );
                                document.register.ChvAnswer.focus();
                                  return false;

						} */
/*********************** Newsletter validation starts here **********************/
var chkcount=parseInt(document.register.txtchk.value)

if (chkcount > 0)
{

var checkbox3_len=document.register.checkbox3.value;

if (document.register.checkbox3[0].checked)
{
var  count=0;
	var chkcount=parseInt(document.register.txtchk.value)
     firstcheck=0;
	
	if (chkcount > 1)
	{
	
	
	for(var i=0;i<chkcount;i++)
		{
			 
             
		if(document.register.mail_checkbox[i].checked)
			{
				if( firstcheck==0)
				{
					document.register.passChk.value=document.register.mail_checkbox[i].value
						 firstcheck=1;
				}
				else
				{
					document.register.passChk.value=document.register.passChk.value+"/" + document.register.mail_checkbox[i].value
				}
					 count=1
			}
					
		}
	}			
	 if (chkcount == 1)
		{
			if(document.register.mail_checkbox.checked)
			{
				document.register.passChk.value=document.register.mail_checkbox.value;
			count=1;
							
			}
		}

	if(count==1)
						{
						return true;
						}
						else
						{
					//alert("To help us serve you better, please select the product category that best describes your interest");
					alert("You will need to check a box listed below to subscribe your newsletter subscription");
					return false;
						}

}
}
//return true;
//document.register.submit();

}


	




