/**********************************************************
	Project Name			: ProactiveFamilySolutions
	Site URL				: http://www.proactivefamilysolutions.com
	Project Phase			: User Phase
	Module Name				: Egg Donor Guide Request Form JS file
	Database Name			: proactiv_proact
	Page Name				: requestform.js
	Purpose of this page	: This page is used for validation of Egg Donor Guide Request 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 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 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 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 to disable textbox starts here **********************/
function disableother()
{
	document.getElementById("othertd").innerHTML="<input name=\"contact_time_text\" type=\"text\" value=\"\" disabled=\"true\"/>";
}
/*********************** Function to enable textbox starts here **********************/
function enableother()
{
	document.getElementById("othertd").innerHTML="<input name=\"contact_time_text\" type=\"text\" value=\"\"/>";
}

var dat = new Date();
var curday = dat.getDate();
var curmon = dat.getMonth()+1;
var curyear = dat.getFullYear();

function checkleapyear(datea)
{
	if(datea.getYear()%4 == 0)
	{
		if(datea.getYear()% 10 != 0)
		{
			return true;
		}
		else
		{
			if(datea.getYear()% 400 == 0)
				return true;
			else
				return false;
		}
	}
return false;
}
function DaysInMonth(Y, M) {
    with (new Date(Y, M, 1, 12)) {
        setDate(0);
        return getDate();
    }
}
function datediff(date1, date2) {
    var y1 = date1.getFullYear(), m1 = date1.getMonth(), d1 = date1.getDate(),
	 y2 = date2.getFullYear(), m2 = date2.getMonth(), d2 = date2.getDate();

    if (d1 < d2) {
        m1--;
        d1 += DaysInMonth(y2, m2);
    }
    if (m1 < m2) {
        y1--;
        m1 += 12;
    }
    return [y1 - y2, m1 - m2, d1 - d2];
}


function validate()
{
	/*---------------------Request person name - start here---------------------*/	
	/*First Name*/
	if (trimString(document.requestform.firstName.value)=="")
	
	{
		alert( "First Name should not be blank" );
		document.requestform.firstName.focus();
		  return false;
	}
	document.requestform.firstName.value=trimString(document.requestform.firstName.value);
	/*---------------------If any special character---------------------*/	
	if (!isCharsInBag(document.requestform.firstName.value, ".abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ))
	{
	alert("First Name should be in characters" );
	document.requestform.firstName.focus();
	return false;
	}
		/*----------------------If length of name if greater than 25 --------------------*/	
	if (document.requestform.firstName.value.length > 25)
	
	{
		alert( "Maximum character length should be 25  in the \"first name\" field." );
		document.requestform.firstName.focus();
		  return false;
	}
	
	/*middle name*/
   document.requestform.middleName.value=trimString(document.requestform.middleName.value);
   /*---------------------If any special character---------------------*/	
	if (!isCharsInBag(document.requestform.middleName.value, ".abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ))
	{
	alert("Middle Name should be in characters" );
	document.requestform.middleName.focus();
	return false;
	}
		/*----------------------If length of name if greater than 25 --------------------*/	
	if (document.requestform.middleName.value.length > 25)
	
	{
		alert( "Maximum character length should be 25  in the \"middle name\" field." );
		document.requestform.middleName.focus();
		  return false;
	}
   
   /*last name*/
	if (trimString(document.requestform.lastName.value)=="")
	
	{
		alert( "Last Name should not be blank" );
		document.requestform.lastName.focus();
		  return false;
	}
	document.requestform.lastName.value=trimString(document.requestform.lastName.value);
		/*---------------------If any special character---------------------*/	
	if (!isCharsInBag(document.requestform.lastName.value, ".abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ))
	{
	alert("Last Name should be in characters" );
	document.requestform.lastName.focus();
	return(false);
	}
		/*----------------------If length of name if greater than 25 --------------------*/	
	if (document.requestform.lastName.value.length > 25)
	
	{
		alert( "Maximum character length should be 25  in the \"last name\" field." );
		document.requestform.lastName.focus();
		  return false;
	}
	/*---------------------Request person name - end here---------------------*/	
	/*********************** If address is equal to null validation starts here **********************/
	if (trimString(document.requestform.address.value)==""  )

	{ 
			alert( "Address should not be blank" );
			document.requestform.address.focus();
			return false;
	}
	/*if (!isCharsInBag(document.requestform.address.value, " #/-1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ))
	{
		alert("Invalid characters found in address" );
		document.requestform.address.focus();
		return false;
	}*/
	/*********************** If cityname is equal to null validation starts here **********************/
	if (trimString(document.requestform.city.value)==""  )

	{
			alert( "City should not be blank" );
			document.requestform.city.focus();
			 return false;
	}
	/*********************** If cityname is not equal to null validation starts here **********************/
	if (!isCharsInBag(document.requestform.city.value, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ))
	{
		alert("Your city should be in characters" );
		document.requestform.city.focus();
		return false;
	}
	/*********************** If cityname length is greater than 25 validation starts here **********************/
	if (document.requestform.city.value.length > 25)
	
	{
		alert( "Maximum character length should be 25  in the \"city\" field." );
		document.requestform.city.focus();
		  return false;
	}
	/*********************** If state ID is equal to null validation starts here **********************/
	if (trimString( document.requestform.intStateId.value)==""  )
	
	{
			alert( "State should be  selected " );
			document.requestform.intStateId.focus();
			  return false;
	}
	/*********************** If zipcode is equal to null validation starts here **********************/
	if (trimString( document.requestform.zip.value)==""  )
	
	{
		alert( "Zip/Postal code should not be blank" );
		document.requestform.zip.focus();
		return false;
	}
	/*********************** If zipcode is not equal to null validation starts here **********************/
	/*if (!isCharsInBag(document.requestform.zip.value, "0123456789" ))
	{
		alert("Invalid characters in Zip (or)Postal code  field" );
		document.requestform.zip.focus();
		return(false);
	}
	if (!isFloatValue(document.requestform.zip.value))
	{
		alert("Invalid characters in \" Zip/Postal code  \" field " );
		document.requestform.zip.focus();
		return false;
	}*/
	/*********************** If zipcode length is greater than 10 validation starts here **********************/
	if (document.requestform.zip.value.length > 10)
	
	{
		alert( "Maximum character length should be 10  in the \"zip\" field." );
		document.requestform.zip.focus();
		  return false;
	}
	/*if (trimString( document.requestform.zip.value)==("000000"|"00000"|"0000"|"000"|"00"|"0"))
	
	{
		alert( "Zip/Postal code should not be zero format" );
		document.requestform.zip.focus();
		return false;
	}*/
	/*********************** If country ID is equal to null validation starts here **********************/
	if (trimString(document.requestform.IntCountryId.value)==""  )
	
	{
			alert( "Country should be selected" );
			document.requestform.IntCountryId.focus();
			 return false;
	}
	/*********************** If Email ID is equal to null validation starts here **********************/
	if(trimString(document.requestform.email.value)=="")
	{
		alert( "E-mail Id should not be blank" );
		document.requestform.email.focus();
		return false;
	}
	/*********************** If Email ID is not equal to null validation starts here **********************/
	if ((document.requestform.email.value!=""))
	{
		/*********************** If Email ID length is greater than 50 validation starts here **********************/
		if(document.requestform.email.value.length>50)
		{
			alert("Maximum character length should be 50 in the \"  E-mail Id \" field.");
			document.requestform.email.focus();
			return(false);
		}
		/*if ((document.requestform.email.value=="")||((isCharsInBag(document.requestform.email.value, "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@.0123456789"))&&(document.requestform.email.value.length>9)&&(document.requestform.email.value.length<50)&&(isEmail(document.requestform.email.value)))) advemail=0;
		else advemail=1;
		if (advemail==1) 
		{
			alert("Enter valid E-mail Id.");
			document.requestform.email.focus();
			return(false);
		}*/
		if (echeck(document.requestform.email.value)==false)
			{
				document.requestform.email.focus()
				return false
			}
	}
	/*********************** If phone number 1 is equal to null validation starts here **********************/
	if (trimString(document.requestform.ph1.value)==""  )
	
	{
			alert( "Phone should not be blank" );
			document.requestform.ph1.focus();
			return false;
	}
	/*********************** If phone number 1 is not equal to null validation starts here **********************/
	if (trimString(document.requestform.ph1.value)!="")
	{
		if (document.requestform.ph1.value.length < 3)
		{
			alert( "Character length should be 3 in the \"phone\" field." );
			document.requestform.ph1.focus();
			return false;
		}
		if (!isCharsInBag(document.requestform.ph1.value, " -()1234567890" ))
		{
			alert("Invalid characters found in phone" );
			document.requestform.ph1.focus();
			return false;
		}
	}
	/*********************** If phone number 2 is equal to null validation starts here **********************/
	if (trimString(document.requestform.ph2.value)==""  )
	
	{
			alert( "Phone should not be blank" );
			document.requestform.ph2.focus();
			return false;
	}
	/*********************** If phone number 2 is not equal to null validation starts here **********************/
	if (trimString(document.requestform.ph2.value)!="")
	{
		if (document.requestform.ph2.value.length < 3)
		{
			alert( "Character length should be 3 in the \"phone\" field." );
			document.requestform.ph2.focus();
			return false;
		}
		if (!isCharsInBag(document.requestform.ph2.value, " -()1234567890" ))
		{
			alert("Invalid characters found in phone" );
			document.requestform.ph2.focus();
			return false;
		}
	}
	/*********************** If phone number 3 is equal to null validation starts here **********************/
	if (trimString(document.requestform.ph3.value)==""  )
	
	{
			alert( "Phone should not be blank" );
			document.requestform.ph3.focus();
			return false;
	}
	/*********************** If phone number 3 is not equal to null validation starts here **********************/
	if (trimString(document.requestform.ph3.value)!="")
	{
		if (document.requestform.ph3.value.length < 4)
		{
			alert( "Character length should be 4 in the \"phone\" field." );
			document.requestform.ph3.focus();
			return false;
		}
		if (!isCharsInBag(document.requestform.ph3.value, " -()1234567890" ))
		{
			alert("Invalid characters found in phone" );
			document.requestform.ph3.focus();
			return false;
		}
	}
	
	
	
//------ Date of birth-------/
if (trimString(document.requestform.txtdate.value)=="")
	{
		alert("Please select your date of birth");
		document.requestform.txtdate.focus();
		return false;
		
	}

if (trimString(document.requestform.txtdate.value)!="")
	{
		var dt=trimString(document.requestform.txtdate.value);
		var ex=dt.split("/");
		var dt_txt=ex[2]+""+ex[0]+""+ex[1];
		
		var cur_date=new Date();
		
		var cru_mn=cur_date.getMonth()+1;
		var cru_dt=cur_date.getDate();
		if (cru_mn<10)
		{
			var cru_mn="0"+cru_mn;
		}
		if (cru_dt<10)
		{
			var cru_dt="0"+cru_dt;
		}
		var cru_yr=cur_date.getFullYear();
		var cur_date_txt=cru_yr+""+cru_mn+""+cru_dt;
		//alert(dt_txt);
		//alert(cur_date_txt);
		if (dt_txt>cur_date_txt)
		{
			alert("Date of birth should be lesser than current date");
			document.requestform.txtdate.focus();
			return false;
		}
		if (dt_txt==cur_date_txt)
		{
			alert("Date of birth should be lesser than current date");
			document.requestform.txtdate.focus();
			return false;
		}
		
/*************************************************/
		var calday =ex[1]; 
		var calmon = ex[0];
		var calyear =ex[2];
		var curd = new Date(curyear,curmon-1,curday);
		var cald = new Date(calyear,calmon-1,calday);
		var diff =  Date.UTC(curyear,curmon,curday,0,0,0) - Date.UTC(calyear,calmon,calday,0,0,0);
		var dife = datediff(curd,cald);
		var age=dife[0]; //" years, "+dife[1]+" months, and "+dife[2]+" days");
		if(age<15)
		{
			alert("Age should be greater than 15 years");
			document.requestform.txtdate.focus();
			return false;	
		}
/**************************************************/
	}
//---------------/


/***************************Password**************************************/
if (trimString(document.requestform.donor_pass.value)=="")
                    {
                                alert( "Password should not be blank" );
                                document.requestform.donor_pass.focus();
                                  return false;
                       }
if (trimString(document.requestform.donor_pass.value)!="")					
{
	
		if(document.requestform.donor_pass.value.length<=4)
						{
                                alert( "Password should be more than 4 characters");
                                document.requestform.donor_pass.focus();
                                  return false;
                        }

	if(!isNaN(trimString(document.requestform.donor_pass.value)))
						  {
                                alert( "Password should be alpha numeric");
                                document.requestform.donor_pass.focus();
                                  return false;
                        }
if (!isCharsInBag(document.requestform.donor_pass.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ))
				{
					alert("Invalid characters found in password" );
					document.requestform.donor_pass.focus();
					return(false);
				}

		   
					   


}

if (trimString(document.requestform.cdonor_pass.value)=="")
                    {
                                alert( "Confirm password should not be blank" );
                                document.requestform.cdonor_pass.focus();
                                  return false;
                       }
					   
   if (trimString(document.requestform.donor_pass.value)!="")					
	{
		if(trimString(document.requestform.donor_pass.value)!=trimString(document.requestform.cdonor_pass.value))
		{
					alert("Password and Confirm password should be same" );
					document.requestform.cdonor_pass.focus();
					return(false);
		}
		
	}

		/******************* End of password********************/

	
	/*********************** If contact time validation starts here **********************/
	var len=document.requestform.contact_time.length;
	for (var k=len-1; k>=0; k--)
		{
			/*********************** If contact time is selected validation starts here **********************/
			if (document.requestform.contact_time[k].checked)
			{
				/*********************** If contact time selected is equal to "other" validation starts here **********************/
				if (document.requestform.contact_time[k].value=="other")
				{
					var checkval=2;
					break;
				}
				/*********************** If contact time selected is not equal to "other" validation starts here **********************/
				if (document.requestform.contact_time[k].value!="other")
				{
					var checkval=1;
					break;
				}
			}
			/*********************** If contact time is not selected validation starts here **********************/
			else
			{
				var checkval=0;
			}
		}
		/*********************** If contact time text is equal to null validation starts here **********************/
	if (checkval==2 && trimString(document.requestform.contact_time_text.value)=="" )
	{
		alert("Contact time should not be empty" );
		document.requestform.contact_time_text.focus();
		return false;
	}
	/*********************** If contact time is not selecte and contact text is equal to null validation starts here **********************/
	if (checkval==0 && trimString(document.requestform.contact_time_text.value)=="" )
	{
		alert("Contact time should be selected" );
		document.requestform.contact_time[0].focus();
		return false;
	}
	/*********************** If how you know is equal to null validation starts here **********************/
	if (trimString(document.requestform.how_you_know.value)=="")
	{
		alert("Please select \"How did you hear about us?\"" );
		document.requestform.how_you_know.focus();
		return false;
	}
	
	/*********************** CAPTCHA validation**********************/
	if (trimString(document.requestform.captcha.value)=="")

	{
		alert( "Please enter the Word Verification" );
		document.requestform.captcha.focus();
		return false;
	}
	//document.requestform.submit();
	return true;
}