/**********************************************************
	Project Name			: ProactiveFamilySolutions
	Site URL				: http://www.proactivefamilysolutions.com
	Project Phase			: User Phase
	Module Name				: Parents registration Form JS file
	Database Name			: proactiv_proact
	Page Name				: parents_register.js
	Purpose of this page	: This page is used for validation of Parents registration Form.
	Last Modified By		: Srinivasn.R.R
	Last Modified Date		: 10 July 2008
	Last Modified Purpose	: For writting Code Comments
 *********************************************************/
 /**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

/*function defaultfocus()
{
alert("hi");
document.getElementById("username").focus();
}*/

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Please enter valid E-mail Id")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Please enter valid E-mail Id")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Please enter valid E-mail Id")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Please enter valid E-mail Id")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Please enter valid E-mail Id")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Please enter valid E-mail Id")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Please enter valid E-mail Id")
		    return false
		 }

 		 return true					
	}

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.username.value)=="")

	{
		alert( "Username should not be blank" );
		document.register.username.focus();
		return false;
	}
	document.register.username.value=trimString(document.register.username.value);
	/*********************** If username is not equal to null validation starts here **********************/
	if (trimString(document.register.username.value)!="")
	{
		var uname=trimString(document.register.username.value);
		var numval=isNaN(uname);
		if (numval==false)
		{
			alert("Username should be alphanumeric");
			document.register.username.focus();
			return false;
		}
		if (!isCharsInBag(uname, "_.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" ))
		{
			alert("Invalid characters found in Username" );
			document.register.username.focus();
			return false;
		}
		var unamelen=document.register.username.value.length;
		if (unamelen<6 || unamelen>15)
		{
		alert("Username should be 6 to 15 characters" );
		document.register.username.focus();
		return false;
		}
	}
	/*********************** If firstname is equal to null validation starts here **********************/
	if (trimString(document.register.fname.value)=="")
	
	{
		alert( "First Name should not be blank" );
		document.register.fname.focus();
		return false;
	}
	document.register.fname.value=trimString(document.register.fname.value);
	/*********************** If firstname is not equal to null validation starts here **********************/
	if (!isCharsInBag(document.register.fname.value, ".abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ))
	{
		alert("First Name should be in characters" );
		document.register.fname.focus();
		return(false);
	}
	
	if (document.register.fname.value.length > 25)
	
	{
		alert( "Maximum character length should be 25  in the \"First Name\" field." );
		document.register.fname.focus();
		return false;
	}
	/*********************** If middlename is not equal to null validation starts here **********************/
	document.register.mname.value=trimString(document.register.mname.value);
	if (!isCharsInBag(document.register.mname.value, ".abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ))
	{
		alert("Middle Name should be in characters" );
		document.register.mname.focus();
		return(false);
	}
	if (document.register.mname.value.length > 25)
	
	{
		alert( "Maximum character length should be 25  in the \"Middle Name\" field." );
		document.register.mname.focus();
		return false;
	}
	/*********************** If lastname is equal to null validation starts here **********************/
	if (trimString(document.register.lname.value)=="")
	
	{
		alert( "Last Name should not be blank" );
		document.register.lname.focus();
		return false;
	}
	document.register.lname.value=trimString(document.register.lname.value);
	/*********************** If lastname is not equal to null validation starts here **********************/
	if (!isCharsInBag(document.register.lname.value, ".abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ))
	{
		alert("Last Name should be in characters" );
		document.register.lname.focus();
		return(false);
	}
	if (document.register.lname.value.length > 25)
	
	{
		alert( "Maximum character length should be 25  in the \"Last Name\" field." );
		document.register.lname.focus();
		return false;
	}
	
	/******************Wife / partner name**************************/
   document.register.partnername.value=trimString(document.register.partnername.value);
   /*---------------------If any special character---------------------*/	
	if (!isCharsInBag(document.register.partnername.value, " &.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ))
	{
	alert("Wife/Partner Name should be in characters" );
	document.register.partnername.focus();
	return false;
	}
		/*----------------------If length of name if greater than 25 --------------------*/	
	if (document.register.partnername.value.length > 100)
	
	{
		alert( "Maximum character length should be 100  in the \"Wife/Partner Name\" field." );
		document.register.partnername.focus();
		return false;
	}
	/*********************** If password is not equal to null validation starts here **********************/
	/*if(!isCharsInBag(document.register.password.value,"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ))
	{
			alert( "Invalid characters in  Password" );
			document.register.password.focus();
			  return false;
	}
	if (document.register.password.value.length<6)

	{
			alert( "Passwords must be at least six (6) characters" );
			document.register.password.focus();
			  return false;
	}
	if (document.register.password.value.length>12)

	{
			alert( "Passwords must be maximum of twelve (12) characters" );
			document.register.password.focus();
			  return false;
	}*/
	/*********************** If confirm pasword is equal to null validation starts here **********************/
	/*if (trimString(document.register.cpassword.value)==""  )

	{
			alert( "Confirm password should not be blank" );
			document.register.cpassword.focus();
			  return false;
	}*/
/*********************** If password and confirm password is not equal to null validation starts here **********************/
	/*if (document.register.password.value!=document.register.cpassword.value)
	{
		alert( "Password and confirm password must be same" );
		document.register.password.focus();
		 return false;
	}*/
	/*********************** If Email ID is equal to null validation starts here **********************/
	if (document.register.email.value=="")
	{
		alert( "E-mail Id should not be blank" );
		document.register.email.focus();
		 return false;
	}
	/*********************** If Email ID is not equal to null validation starts here **********************/
	 if ((document.register.email.value!=""))
					
		{
		if(document.register.email.value.length>50)
		{
			alert("Maximum character length should be 50 in the \"  E-mail Id \" field.");
			document.register.email.focus();
			return(false);
		}
		/*if ((document.register.email.value=="")||((isCharsInBag(document.register.email.value, "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@.0123456789"))&&(document.register.email.value.length>9)&&(document.register.email.value.length<50)&&(isEmail(document.register.email.value)))) advemail=0;
		else advemail=1;
		if (advemail==1) 
		{
			alert("Enter valid E-mail Id.");
			document.register.email.focus();
			return(false);
		}*/
		if (echeck(document.register.email.value)==false)
			{
				document.register.email.focus()
				return false
			}
	}
	/*********************** If Address is equal to null validation starts here **********************/
	if (trimString(document.register.address1.value)==""  )
	
	{ 
		alert( "Address1 should not be blank" );
		document.register.address1.focus();
		return false;
	}
	/*********************** If city is equal to null validation starts here **********************/
	if (trimString(document.register.city.value)==""  )
	
	{
		alert( "City should not be blank" );
		document.register.city.focus();
		return false;
	}
	/*********************** If cityname is not equal to null validation starts here **********************/
	if (!isCharsInBag(document.register.city.value, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ))
	{
		alert("Your city should be in characters" );
		document.register.city.focus();
		return false;
	}
	
	/*********************** If state is equal to null validation starts here **********************/
	/*if (trimString( document.register.state.value)==""  )
	
	{
		alert( "State should be  selected " );
		document.register.state.focus();
		return false;
	}*/
	
	
	/*********************** If country is equal to null validation starts here **********************/
	if (trimString(document.register.country.value)==""  )
	
	{
		alert( "Country should not be blank" );
		document.register.country.focus();
		return false;
	}
	/*********************** If zipcode is equal to null validation starts here **********************/
	if (trimString( document.register.zipcode.value)==""  )
	
	{
		alert( "Zip/Postal code should not be blank" );
		document.register.zipcode.focus();
		return false;
	}
	/*********************** If zipcode is not equal to null validation starts here **********************/
	/*if (trimString( document.register.zipcode.value)==("000000"|"00000"|"0000"|"000"|"00"|"0"))
	
	{
		alert( "Zip/Postal code should not be zero format" );
		document.register.zipcode.focus();
		return false;
		}
	if (!isCharsInBag(document.register.zipcode.value, "0123456789" ))
	{
		alert("Invalid characters in Zip (or)Postal code  field" );
		document.register.zipcode.focus();
		return(false);
	
	
		if (!isFloatValue(document.register.zipcode.value))
		{
		alert("Invalid characters in \" Zip/Postal code  \" field " );
		document.register.zipcode.focus();
		return(false);
		}		
	
	}*/
	var ziptot=document.getElementById("zipcode").value.length;
	
	/*if(ziptot<6)
	{
		alert("Zip code must be 6 digit");
		document.getElementById("zipcode").focus();
		return false;
	
	}*/
	if(ziptot>10)
	{
		alert("Zip code length must be 1 to 10 digits");
		document.getElementById("zipcode").focus();
		return false;
	
	}
	/*********************** If phone number 1 is equal to null validation starts here **********************/
	/*if (document.register.home_phone1.value=="")
	{
		alert("Please enter the home phone number");
		document.register.home_phone1.focus();
		return false;
	}*/
	/*********************** If phone number 1 is not equal to null validation starts here **********************/
	/*var phone1=trimString(document.register.home_phone1.value);
	var numval_phone1=isNaN(phone1);
	if (numval_phone1==true)
	{
		alert("Home phone number should be numeric");
		document.register.home_phone1.focus();
		return false;
	}
	var fnamelen_phone1=document.register.home_phone1.value.length;
	if (fnamelen_phone1<3 || fnamelen_phone1>3)
	{
		alert("Home phone number should be 3 characters" );
		document.register.home_phone1.focus();
		return false;
	}*/
	
	
/*********************** If phone number 2 is equal to null validation starts here **********************/	
	/*if (document.register.home_phone2.value=="")
	{
		alert("Please enter the home phone number");
		document.register.home_phone2.focus();
		return false;
	}*/
	/*********************** If phone number 2 is not equal to null validation starts here **********************/
	/*var phone2=trimString(document.register.home_phone2.value);
	var numval_phone2=isNaN(phone2);
	if (numval_phone2==true)
	{
		alert("Home phone number should be numeric");
		document.register.home_phone2.focus();
		return false;
	}
	var fnamelen_phone2=document.register.home_phone2.value.length;
	if (fnamelen_phone2<3 || fnamelen_phone2>3)
	{
		alert("Home phone number should be 3 characters" );
		document.register.home_phone2.focus();
		return false;
	}*/
	
	
/*********************** If phone number 3 is equal to null validation starts here **********************/
	
	/*if (document.register.home_phone3.value=="")
	{
		alert("Please enter the home phone number");
		document.register.home_phone3.focus();
		return false;
	}*/
	/*********************** If phone number 3 is not equal to null validation starts here **********************/
	/*var phone3=trimString(document.register.home_phone3.value);
	var numval_phone3=isNaN(phone3);
	if (numval_phone3==true)
	{
		alert("Home phone number should be numeric");
		document.register.home_phone3.focus();
		return false;
	}
	var fnamelen_phone3=document.register.home_phone3.value.length;
	if (fnamelen_phone3<4 || fnamelen_phone3>4)
	{
		alert("Home phone number should be 4 characters" );
		document.register.home_phone3.focus();
		return false;
	}*/

	/*********************** If Home Phone number is equal to null validation starts here **********************/

	if (document.register.home_phone.value=="" && document.register.work_phone.value=="" && document.register.mobile.value=="")
	{
		alert("Please enter the Home phone number or Work phone number or Mobile phone number");
		document.register.home_phone.focus();
		return false;
	}
	
	if (document.register.home_phone.value!="")
	{
		var home_phone_len=document.register.home_phone.value.length;
		if (home_phone_len>50)
		{
			alert("Home phone number should be less than 50 characters" );
			document.register.home_phone.focus();
			return false;
		}	
	}
	
	/*********************** If Home Phone number is equal to null validation ends here **********************/
	
		
		/************************ dob and wedding date validation **************/
		/*if dob year null*/
		if(document.register.dobdate.value!="" && document.register.dobmonth.value=="")
		{
			alert("Please select birth month");
			document.register.dobmonth.focus();
			return false;
		}
		
		/*if dob month null*/
		if(document.register.dobdate.value=="" && document.register.dobmonth.value!="")
		{
			alert("Please select birth date");
			document.register.dobdate.focus();
			return false;
		}
		
		/*if wedding year null*/
		if(document.register.weddingdate.value!="" && document.register.weddingmonth.value=="")
		{
			alert("Please select anniversary month");
			document.register.weddingmonth.focus();
			return false;
		}
		
		/*if wedding month null*/
		if(document.register.weddingdate.value=="" && document.register.weddingmonth.value!="")
		{
			alert("Please select anniversary date");
			document.register.weddingdate.focus();
			return false;
		}
		
	/*	if(document.register.weddingmonth.value!="" && document.register.weddingyear.value!="" && document.register.dobmonth.value!="" && document.register.dobyear.value!="")
		{
		/*if wedding year less than birth year*/
			/*if(document.register.weddingyear.value < document.register.dobyear.value)
			{
				alert("Anniversary year should not less than birth year");
				document.register.weddingyear.focus();
				return false;
			}
		}*/
	
	/*********************** If Work Phone number is equal to null validation starts here **********************/

	if (document.register.work_phone.value!="")
	{
		var work_phone_len=document.register.work_phone.value.length;
		if (work_phone_len>50)
		{
			alert("Work phone number should be less than 50 characters" );
			document.register.work_phone.focus();
			return false;
		}	
	}
	
	/*********************** If Work Phone number is equal to null validation ends here **********************/
	
	/*********************** If Mobile number is equal to null validation starts here **********************/

	if (document.register.mobile.value!="")
	{
		var mobile_len=document.register.mobile.value.length;
		if (mobile_len>50)
		{
			alert("Work phone number should be less than 50 characters" );
			document.register.mobile.focus();
			return false;
		}	
	}
	
	/*********************** If Mobile number is equal to null validation ends here **********************/
	
	/*********************** If occupation is equal to null validation starts here **********************/
	if (trimString(document.register.occupation.value)=="")

	{
		alert( "Occupation should not be blank" );
		document.register.occupation.focus();
		return false;
	}
	/*********************** If occupation is not equal to null validation starts here **********************/
	if (trimString(document.register.occupation.value)!="")
	{
		var occupation1=trimString(document.register.occupation.value);
		var numval=isNaN(occupation1);
		if (numval==false)
		{
			alert("Occupation should be alphanumeric");
			document.register.occupation.focus();
			return false;
		}
		if (!isCharsInBag(occupation1, " _.&,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890/-" ))
		{
			alert("Invalid characters found in Occupation" );
			document.register.occupation.focus();
			return false;
		}
		var occupationlen=document.register.occupation.value.length;
		if (occupationlen<2 || occupationlen>200)
		{
		alert("Occupation should be 2 to 200 characters" );
		document.register.UsrName.focus();
		return false;
		}
	}
	/*********************** If reason is equal to null validation starts here **********************/
	if (trimString(document.register.reason.value)=="")

	{
		alert( "Reason should not be blank" );
		document.register.reason.focus();
		return false;
	}
	/*********************** If date is equal to null validation starts here **********************/
	/*if (trimString(document.register.dates.value)=="")

	{
		alert( "Dates should not be blank" );
		document.register.dates.focus();
		return false;
	}*/
	/*********************** If notes is equal to null validation starts here **********************/
	document.register.notes.value=trimString(document.register.notes.value);
	/*if (trimString(document.register.notes.value)=="")

	{
		alert( "Notes should not be blank" );
		document.register.notes.focus();
		return false;
	}*/
	
	/*********************** If Researching Foreign Surrogacy is equal to null validation starts here **********************/
	if (trimString(document.register.researchFoSurrogacy.value)=="")

	{
		alert( "Please select \"Are you researching foreign surrogacy for\"" );
		document.register.researchFoSurrogacy.focus();
		return false;
	}
	
	/*********************** If How Long Foreign Surrogacy is equal to null validation starts here **********************/
	if (trimString(document.register.howLongFoSurrogacy.value)=="")

	{
		alert( "Please select \"How long have you been considering foreign surrogacy?\"" );
		document.register.howLongFoSurrogacy.focus();
		return false;
	}
	
	/*********************** If Reason for Foreign Surrogacy is equal to null validation starts here **********************/
	if (trimString(document.register.reasonFoSurrogacy.value)=="")

	{
		alert( "Please select \"Reason you might consider foreign surrogacy\"" );
		document.register.reasonFoSurrogacy.focus();
		return false;
	}
	
	/*********************** If Other Surrogacy Organizations is equal to null validation starts here **********************/
	var otherResOrg_len=document.register.otherResOrg.length; // Number of options
	// If number of options is greater than 1, then code starts here
	if (otherResOrg_len>1)
	{
		// For k = 0 to number of options,code starts here
		for (var k=otherResOrg_len-1; k>=0; k--)
		{
			// If options is checked, then
			if (document.register.otherResOrg[k].checked)
			{
				var otherResOrg_checkval=1;
				break;
			}
			// If options is not checked, then
			else
			{
				var otherResOrg_checkval=0;
			}
		}
		// For k = 0 to number of options,code ends here
		
		// If no options are checked, then code starts here
		if (otherResOrg_checkval==0)
		{
			alert( "Please select \"Other surrogacy organization\"" );
			return false;
		}
	}
	
	
	/*********************** If Previously explored adoption is equal to null validation starts here **********************/
	var preExploredAdoption_len=document.register.preExploredAdoption.length; // Number of options
	// If number of options is greater than 1, then code starts here
	if (preExploredAdoption_len>1)
	{
		// For k = 0 to number of options,code starts here
		for (var k=preExploredAdoption_len-1; k>=0; k--)
		{
			// If options is checked, then
			if (document.register.preExploredAdoption[k].checked)
			{
				var preExploredAdoption_checkval=1;
				break;
			}
			// If options is not checked, then
			else
			{
				var preExploredAdoption_checkval=0;
			}
		}
		// For k = 0 to number of options,code ends here
		
		// If no options are checked, then code starts here
		if (preExploredAdoption_checkval==0)
		{
			alert( "Please select \"Previously explored adoption\"" );
			return false;
		}
	}
	
	
	
	/*********************** If how you know is equal to null validation starts here **********************/
	if (trimString(document.register.how_you_know.value)=="")

	{
		alert( "Please select \"How did you hear about us?\"" );
		document.register.how_you_know.focus();
		return false;
	}
	
	/*********************** CAPTCHA validation**********************/
	if (trimString(document.register.captcha.value)=="")

	{
		alert( "Please enter the Word Verification" );
		document.register.captcha.focus();
		return false;
	}

}