/**
 * validation configuration object
 */
var errors = false;
jsLabel.jsSignUpValidation = {
	fullName : {
		required: 'Please enter your full name',
		minlength: 'Full Name must be at least 6 characters',
		maxlength: 'Full Name must be less than 40 characters'
	},
	email: {
		required: 'Please enter your email address',
		email: 'Please enter a valid email address'
	},
	password : {
		required: 'Please enter a password',
		minlength: 'Password must be at least 6 characters',
		maxlength: 'Password must be less than 40 characters'
	},
	gender : 'Please choose a gender',
	birthday : {
		required: 'Please enter your birthday',
		date : 'Please enter a valid date',
		minAge : 'You must be 14 or older to sign up'
	}
};

var validator = {}
validator = {
	focusInvalid 	: false,		//	focuses the first invalid field (default is true)
	focusCleanup 	: false,	//	removes the error message once the field is focused (defaults to false)
	onkeyup 		: false,	//	Revalidates on key up
	errorElement	: 'span', 
	errorPlacement	: function(err, elm) {
     if (elm.attr('name') == 'gender' || elm.attr('name') == 'birthday')
       err.insertAfter(elm.parent());
	 else
       err.insertAfter(elm)		
	},
	rules : {
		fullName : {
			required : true,
			minlength : 6,
			maxlength : 40
		},
		email : {
			required : true,
			email : true
		},
		password : {
			required : true,
			minlength : 6,
			maxlength : 30
		},
		gender : 'required',
		birthday : {
			required : true,
			date : true,
			minAge : true
		}
	},
	messages :{
		fullName : {
			required: jsLabel.jsSignUpValidation.fullName.required,
			minlength: jsLabel.jsSignUpValidation.fullName.minlength,
			maxlength: jsLabel.jsSignUpValidation.fullName.maxlength
		},
		email: {
			required: jsLabel.jsSignUpValidation.email.required,
			email: jsLabel.jsSignUpValidation.email.email
		},
		password : {
			required: jsLabel.jsSignUpValidation.password.required,
			minlength: jsLabel.jsSignUpValidation.password.minlength,
			maxlength: jsLabel.jsSignUpValidation.password.maxlength
		},
		gender : jsLabel.jsSignUpValidation.gender,
		birthday : {
			required: jsLabel.jsSignUpValidation.birthday.required,
			date : jsLabel.jsSignUpValidation.birthday.date,
			minAge : jsLabel.jsSignUpValidation.birthday.minAge
		},
		recaptcha_response_field : 'Please enter the text above'
	}
}

$j(function() {


	/**
	 * adds a minimum age validation method for birthday field
	 * @param {Object} value 
	 * @param {Object} element
	 */
	jQuery.validator.addMethod("minAge", function(value, element) {
		var minAge = 14
		var birthdate = new Date(value);
		var today = new Date();
		var difference = today - birthdate.getTime();  
		var days = Math.round(difference/(1000*60*60*24))
	  return this.optional(element) || days >= (minAge*365); 
	}, "You must be 14 or older to sign up");
	/**
	* initializes the signUpForm and its events for use on either home or signup page
	*/
	//$j('#signUpForm').show();
	//$j('#signUpForm button').attr('disabled', false);
	$j('#signUpForm').validate(validator);
	
	/*$j('.cancelAction').click(function(){
		clearFieldValues('#signUpForm');
		clearValidationErrorMessages('#signUpForm');
	})*/
	
	/*if ($j('#homepage').length > 0) {
		$j('#signUpForm button').bind('click', swapSignUpSteps);
	}*/
	
	if(errors) {
		//if there are errors, reset the date selectors to previous values
		var monthList = document.getElementById("month");
		var dateList = document.getElementById("day");
		var yearList = document.getElementById("year");
		var birthDate = document.getElementById("birthday");
		monthList.value = birthMonth;
		dateList.value = birthDay;
		if (parseInt(birthYear) < 87) birthYear = parseInt('19' + birthYear);
		yearList.value = birthYear;
		birthDate.value = birthMonth + '/' + birthDay + '/' + birthYear;
	}
	
	$j("#birthdayFields").dateSelect();

    if($j("#whyBirthdayAction").length > 0) {
        $j("#whyBirthdayAction").click(function(e) {
            e.preventDefault();
            alertWithTitle($j("#whyBirthdayCtn p").html(), "Why provide my birthday?");
        });

    };
    if($j("#whatIsSearchPrivacy").length > 0) {
		$j("#whatIsSearchPrivacy").click(function(e) {
			e.preventDefault();
			alertWithTitle($j("#whatIsSearchPrivacy p").text(), "What is search privacy?");
		});

	};
	
	if ($j('#captchaForm').length > 0) {
		$j('#recaptcha_response_field').focus();
	}
	
	//var birthday = new dateSelector('#day', '#month', '#year', '#birthday');
	
/*	if ($j('#signUp').length > 0) {
		$j('#registerSecurity h2').remove();
		$j('#registerSecurity p').remove();
		$j('#recaptcha_response_field').addClass('required');
		$j('#registerSecurity').show();
		$j('#country').change(function(){
			if ($j('#country').val() == 2) {
				$j('#postalCode').prev().text('Postal Code:');
			}
			if ($j('#country').val() == 1 || $j('#country').val() == 0) {
				$j('#postalCode').prev().text('Zip Code:');
			}
		});
	}*/
});
