var valid_email=false;var valid_user=false;var valid_key=false;function validate(){var errors="";sigimage=/\.jpg$/i;zipcode=/^[0-9]{5}$/i;month=document.reg.month.value;day=document.reg.day.value;year=document.reg.year.value;document.reg.password2.value=document.reg.password.value;document.reg.confirm_email.value=document.reg.email.value;if(!validateScreenName(document.reg.username.value)){errors=errors+"- Create a valid screen name (letters, numbers and underscores only)\n";}if(document.reg.password.value==""||document.reg.password2.value==""){errors=errors+"- Enter and confirm your password\n";}if(document.reg.password2.value!=document.reg.password.value){errors=errors+"- Your passwords do not match please re-enter your password\n";}

if(!validateEmail(document.reg.email.value)){errors=errors+"- Enter a valid email\n";}

	var genderVal = getCheckedValue(document.reg.gender_);
        if (genderVal == '') {
                errors = errors + "- Provide your gender\n";
        }

var current_time=new Date();var curr_month=current_time.getMonth()+1;var curr_day=current_time.getDate();var curr_year=current_time.getFullYear();if(!(document.reg.email.value==""||document.reg.confirm_email.value=="")&&document.reg.confirm_email.value!=document.reg.email.value){errors=errors+"- Your confirmed email does not match your email\n";}if(document.reg.month.value==""){errors=errors+"- Enter the month of your birth\n";}if(document.reg.year.value==""){errors=errors+"- Enter the year of your birth\n";}if(document.reg.day.value==""){errors=errors+"- Enter the day of your birth\n";}if(month!=""&&(month<0||month>12)){errors=errors+"- Enter a valid month for your birthday.\n";}if(day!=""&&(day<0||day>31)){errors=errors+"- Enter a valid day for your birthday.\n";}if(year!=""&&(year>curr_year||year<(curr_year-120))){errors=errors+"- Enter a valid year for your birthday.\n";}if(!document.reg.accept.value=="true"){errors=errors+"- You have not accepted our Terms and Conditions of Use\n";}if((document.reg.month.value=="")||(document.reg.year.value=="")||(document.reg.day.value=="")){}else{var age_min=18;var age_max=100;var today=new Date();if(year%1!=0){year=today.getFullYear();}if(month%1!=0){month=today.getMonth()+1;}if(day%1!=0){month=today.getDate();}var age=today.getFullYear()-document.reg.year.value;if(today.getMonth()+1<document.reg.month.value){age=age-1;}else{if(today.getMonth()+1==document.reg.month.value){if(today.getDate()<document.reg.day.value){age=age-1;}}}if((age<age_min)||(age>150)){errors=errors+"- We're sorry, but you must be at least 18 years old to register for eHarmony Advice per the minimum age requirements in our Terms & Conditions of Service.\n";}else{if(age>age_max){errors=errors+"- We're sorry, but you must be at least 18 years old to register for eHarmony Advice per the minimum age requirements in our Terms & Conditions of Service.\n";}}}if(errors!=""){alert("Please fix the following errors:\n"+errors);return"failed";}}function validateEmail(email){var reg=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;if(reg.test(email)==false){return false;}return true;}function validateScreenName(screenName){var reg=/^([A-Za-z0-9_])+$/;if(reg.test(screenName)==false){return false;}return true;}

function validate_signup(form) 
{
	var errors = "";
	sigimage = /\.jpg$/i;
	zipcode = /^[0-9]{5}$/i;

	month = form.month.value;
	day = form.day.value;
	year = form.year.value;

	//Set hidden confirm password field to the same as password field
	form.password2.value = form.password.value;
	form.confirm_email.value = form.email.value;

	if (!validateScreenName(form.username.value)) 
	{
		errors = errors + "- Create a valid screen name (letters, numbers and underscores only)\n";
	}
		
	if(form.password.value == '' || form.password2.value == '') 
	{
		errors = errors + "- Enter and confirm your password\n";
	}
		
	if(form.password2.value != form.password.value) 
	{
		errors = errors + "- Your passwords do not match please re-enter your password\n";
	}
		
	if (!validateEmail(form.email.value)) 
	{
		errors = errors + "- Enter a valid email\n";
	}

	var genderVal = getCheckedValue(document.reg.gender_);
       	if (genderVal == '') {
       		errors = errors + "- Provide your gender\n";
        }

	var current_time = new Date();
	var curr_month = current_time.getMonth()+1;
	var curr_day = current_time.getDate();
	var curr_year = current_time.getFullYear();
					
	if (!(form.email.value == '' || form.confirm_email.value == '')  && form.confirm_email.value !=  form.email.value) 
	{
		errors = errors + "- Your confirmed email does not match your email\n";
	}

	if (form.month.value == '') { errors = errors + "- Enter the month of your birth\n"; } 
	if (form.year.value == '') { errors = errors + "- Enter the year of your birth\n"; }
	if (form.day.value == '') { errors = errors + "- Enter the day of your birth\n";} 
	if(month != '' && (month < 0 || month > 12)) { errors = errors+ "- Enter a valid month for your birthday.\n"; }
	if(day != '' && (day < 0 || day > 31)) { errors = errors + "- Enter a valid day for your birthday.\n"; }
	if(year != '' && (year > curr_year || year < (curr_year-120))) { errors = errors + "- Enter a valid year for your birthday.\n";}
	if (!form.accept.value == 'true') { errors = errors + "- You have not accepted our Terms and Conditions of Use\n"; }
	if ((form.month.value == '') || (form.year.value == '') || (form.day.value == '')) { }
	else	{
		var age_min = 18;
		var age_max = 100;
		var today = new Date();
		if(year%1 != 0){year = today.getFullYear();} if(month%1 != 0){month = today.getMonth() + 1;} if(day%1 != 0){month = today.getDate();}
		var age = today.getFullYear() - form.year.value;
		if(today.getMonth()+ 1 < form.month.value)
		{ age = age - 1;}
		else if(today.getMonth() + 1 == form.month.value)
		{ if(today.getDate() < form.day.value) { age = age - 1;}}


		if((age < age_min) || (age > 150)) { errors = errors + "- We're sorry, but you must be at least 18 years old to register for eHarmony Advice per the minimum age requirements in our Terms & Conditions of Service.";}
		else if(age > age_max) { errors = errors + "- We're sorry, but you must be at least 18 years old to register for eHarmony Advice per the minimum age requirements in our Terms & Conditions of Service.";} 					 
	}
	if (errors != "") {
			alert("Please fix the following errors:\n" + errors);
			return false;
	}
	return true;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
