var err = [];

function validate_paypal() {
	if( $("#checkout_form").validate().form() ) {
		window.location.href = '/includes/paypal_redirect.php';	
	}
}

$(document).ready(function() {
	jQuery.validator.messages.required = "";
	
	/* password policy */
	jQuery.validator.addMethod("password", function( value, element ) {
		return (this.optional(element) || value.length >= 6) ; // && /\d/.test(value) && /[a-z]/i.test(value);
	}, "Your password must be at least 6 characters long");
	
	jQuery.validator.addMethod("repeat_password", function( value, element ) {
		return (value == $("input.password").val() ) ; // && /\d/.test(value) && /[a-z]/i.test(value);
	}, "Your passwords do not match");
	
	jQuery.validator.addMethod("cart_ship_choose", function( value, element ) {
		return !(value=='-1'); // && /\d/.test(value) && /[a-z]/i.test(value);
	}, "You must choose a shipping option");
	
	jQuery.validator.addMethod("agree", function( value, element ) {
		return !(value=='yes'); // && /\d/.test(value) && /[a-z]/i.test(value);
	}, "You must agree to the policy");
	
});

function return_dependency(class_name) {
	return { depends: function(element) { return $(class_name).is(":visible");  } }	;
}

function bindAddProductValidator(id) {
	return $("#" + id ).validate({
				onkeyup: false,
				messages: {
					ProductModel: { 
						required: " ",
						remote: jQuery.validator.format("{0} is already in use.") 
					}
				}	
			});

}

function bindAddressValidator(id) {	
	var test_arr = new Array("creditcard", "email", "password", "repeat_password", "cart_ship_choose", "agree");
	var elements_with_errors = new Array();
	
	v = $("#" + id ).validate({
		 rules: {
			 /* ccn fields */
			 payment_CCN: {
			 		required: return_dependency(".pm_pane.pm_1:first")
			 },
			 payment_CVV2: {
			 		required: return_dependency(".pm_pane.pm_1:first")
			 },
			 /* check fields */
			payment_CheckNumber: {
					required: return_dependency(".pm_pane.pm_2:first")
			},
			payment_RoutingNumber: {
					required: return_dependency(".pm_pane.pm_2:first")
			},
			payment_AccountNumber: {
					required: return_dependency(".pm_pane.pm_2:first")
			},
			payment_AccountName: {
					required: return_dependency(".pm_pane.pm_2:first")
			},
			payment_BankName: {
					required: return_dependency(".pm_pane.pm_2:first")
			},
			/* cash/quote fields 
			 payment_BillingNotes: {
			 		required: return_dependency(".pm_6")
			 },*/
			/* paypal fields */
			 payment_PayPalEmailAddr: {
			 		required: return_dependency(".pm_pane.pm_7:first")
			 },
			 payment_PayPalConf: {
			 		required: return_dependency(".pm_pane.pm_7:first")
			 }
		},
		messages: {
			bill_EmailAddr: { 
				email: "Please enter a valid email address",
                remote: jQuery.validator.format("{0} is already in use. <a href='/forgot.php'>Click here if you forgot your password</a>.")
            }
		},
		onkeyup:false,
		errorPlacement: function(error, element) { 	
			for (var i in test_arr) { // call out custom errors
			 	if (element.hasClass(test_arr[i])) {
					// if a div with the class test-arr[i] doesn't exist inside div.warning, create it
					if($('div.warning2 div.' + test_arr[i]).length == 0) {
						$('div.warning2').append("<div class=\"" + test_arr[i] + "\"></div>");
					}
					
					if(!isEmpty(error.html())) { // fill that div with the error if error is nonblank
						$('div.warning2 div.' + test_arr[i]).html(error.html() + "<br/>");
					}
				}	// fi element hasClass
			}  // end for i in test_arr
		},
		showErrors: function(errorMap, errorList) {
			var errors = this.numberOfInvalids();
			//alert(errorList.length);
			if (errorList.length > 0) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted below'
					: 'You missed ' + errors + ' fields.  They have been highlighted below';
				
				show_error(message);
			} else {
				
				hide_error();
			}
			this.defaultShowErrors();
		},
		unhighlight: function(element, errorClass) {

			 $(element).removeClass(errorClass);
			 var class_arr = $(element).attr("class").split(" ");
			 $('div.warning2 div.' + class_arr[0]).html('');
			 
		 }
	}); 

	return v;
} 
