/**
 * @requires kg.js
 **/

kg.signInForm = (function() {
	function _init() {
		_addButtonHandlers();
		_signInValidation();
	}
	function _addButtonHandlers() {
		$('#signInLink').click(function(e){
			e.preventDefault();
			$('#signInBox').show();
		});
		
		$('#pwdResetLink').click(function(e){		 
			e.preventDefault();
			$('#signInBox').hide();
			$('#pwdResetBox').show();
		});
		$('#signInBox .closeBox').click(function(e) {
			$('#signInBox').hide();
		});
		$('#pwdResetBox .closeBox').click(function(e) {
			$('#pwdResetBox').hide();
		});
		
		$('#signInBox .cancel').click(function(e) {
			e.preventDefault();
			$('#signInBox').hide();
		});
		$('.signInBtn').click(function(e) {
			e.preventDefault();
			$('#signIn').submit();
		});
	}
	function _signInValidation() {
		if ($('#signInForm').length) {
	        var email_confirm_error = 'E-mail address does not match';
	        var password_confirm_error = 'Password does not match';
			var please_certify_18 = 'Please certify that you are 18 years old or older.';
			var email_validation_error = 'Please enter valid email address.';

	        $('#signInForm').validate({
				errorClass: "formError",
	            ignoreTitle: true,
				highlight: function(element, errorClass) {
					$(element).addClass(errorClass);
					$(element).prev().addClass('error');
					if ($(element).parent().find('.helperText').length) {
						$(element).parent().find('.helperText').css('top','35px');
					}
					if (!$(element).parent().find('div.clearError').length) {
						$(element).prev().before("<div class='clearError'><!-- --></div>");
					}
				},
				unhighlight: function(element, errorClass) {
					$(element).removeClass(errorClass);
					$(element).prev().removeClass('error');
					if ($(element).parent().find('.helperText').length) {
						$(element).parent().find('.helperText').css('top','20px');
					}
					$(element).parent().find('div.clearError').remove();
				},
				invalidHandler: function(form, validator) {
					var errors = validator.numberOfInvalids();
				    if (errors) {
				        $("div.formErrorNotice div").html("Please address the errors below");
				        $("div.formErrorNotice").show();
				      } else {
				        $("div.formErrorNotice").hide();
				      }
				},
	            errorPlacement: function(e, el) {
	                e.prependTo(el.parent());//.next().addClass('error');
	            },
	            rules: {
	            	//Commented out the call to blankEMail() validations..
	                //logonId: { blankEMail: true, required: true, email: true },
	                logonId: {required: true, validateEmail: true},
	              	logonPassword: { required: true, minlength: 6 }
	            },
	            messages: {
	                emailConfirm: { equalTo: email_confirm_error },
					passwordConfirm: { equalTo: password_confirm_error },
					certify18: { required: please_certify_18 },
					logonId: {validateEmail: email_validation_error }
	            }
	        });
		}
	}
	
	return {
		init: function(){
			_init();
		}
	}
})();


$(document).ready(function(){
	kg.signInForm.init();
});

jQuery.validator.addMethod("validateEmail", function()
{
	var email=$('#email').val();
	var filter=/^[0-9a-zA-Z]+[-\.\w]{0,1}[0-9a-zA-Z]*@[0-9a-zA-Z]+\.[a-zA-Z]{2,9}$/;
	if(filter.test(email)){
		return true;
	}
	else{
		return false;
	}
});
