// JavaScript Document
$.validator.addMethod("creditCard", function(value, element) {  
return this.optional(element) ||  /^[0-9]{16,}$/i.test(value);  
}, "<strong>CREDIT CARD</strong> requires 16 Digits");	
$.validator.addMethod("cvv", function(value, element) {  
return this.optional(element) ||  /^[0-9]{3,4}$/i.test(value);  
}, "<strong>CVV</strong>  Minimum of 3 Digits");
$.validator.addMethod("cvv2", function(value, element) {  
return this.optional(element) ||  /^[0-9]{4,}$/i.test(value);  
}, "<strong>CVV</strong> Requires 4 Digits");
$.validator.addMethod("fullname", function(value, element) {  
return this.optional(element) ||  /^[a-zA-Z]{2,}[ a-zA-Z]{1,}?$/i.test(value);  
	}, "The <strong>FIRSTNAME</strong> is invalid");
/*$.validator.addMethod("fullname2", function(value, element) {  
return this.optional(element) ||  /^[a-zA-Z]{1,}?[ ][a-zA-Z]{1,}?$/i.test(value);  
	}, "Please enter your <strong>LASTNAME</strong>");	*/
$.validator.addMethod("phone", function(value, element) {  
	return this.optional(element) ||  /^[(][0-9]{3,}[)][ ][0-9]{3,}[-][0-9]{4,}$/i.test(value);  
	}, "Phone Requires 10 Digits");
$.validator.addMethod("phone2", function (value, element) {
    return this.optional(element) || /^[(][0-9]{3,}[)][ ][0-9]{3,}[-][0-9]{4,}$/i.test(value);
}, "Phone Requires 10 Digits");
$.validator.addMethod("zipcode", function (value, element) {  
	return this.optional(element) ||  /^[0-9]{5,}$/i.test(value);  
	}, "<strong>ZIP</strong> requires 5 digits");	

$("#checkoutForm1").validate({
	groups: {
    ExpDate: "fields_expmonth fields_expyear"
  },
	rules: {
	cc_number: "required creditCard",
	fields_expyear: "required",
	fields_expmonth: "required",
	cc_cvv: "required cvv",
	country: "required"
},	
	errorElement: "em",
		errorPlacement: function(error, element) {
		error.appendTo(element.parent());
	},
	messages: {
	cc_number: {required: "<strong>CARD NUMBER</strong> is required", creditCard: "<strong>CREDIT CARD</strong> requires 16 Digits"},
	fields_expmonth: { required: "<strong>EXPIRATION MONTH</strong> is required"},
	fields_expyear: { required: "<strong>EXPIRATION YEAR</strong> is required"},
	cc_cvv: { required: "<strong>CVV</strong> is required" },
	country: { required: "<strong>Country</strong> is required" }
},
	submitHandler: function(form) {
		form.submit();
   	internalLink = true
   }
		});
		


/*$("input[name=cc_number]", "#checkoutForm1")
.focus(function() {
var foo = $(this).val();
foo = foo.charAt(0);
if (foo=='3') { var mask = "?999999999999999"; } else { var mask = "?9999999999999999";}
//$(this).unmask();
//$(this).mask(mask);
//$(this).unmask(mask).select().mask(mask,{completed:function(){$('input[name=cc_cvv]').focus();}});
});

$("input[name=cc_number]", "#checkoutForm1").keyup(function() {
	var foo = $(this).val();
	var foo2 = $("input[name=cc_cvv]", "#checkoutForm1");

		foo = foo.charAt(0);
	if (foo=='3') {
		var mask = "?9999";
		var mask2 = "?999999999999999";
		foo2.addClass('isAmex');
		foo2.rules("add", { required:true, cvv: false, cvv2: true });
		//$(this).unmask();
		//$(this).mask(mask2);
		foo2.validate();
		foo2.unmask();
		foo2.mask(mask);
	} else {
		var mask = "?999";
		var mask2 = "?9999999999999999";
		foo2.removeClass('isAmex');
		foo2.rules("add", { required:true, cvv: true, cvv2: false });
		//$(this).unmask();
		//$(this).mask(mask2);
		foo2.validate();
		foo2.unmask();
		foo2.mask(mask);
	}
});

$("input[name=cc_cvv]", "#checkoutForm1").focus(function() {
	var foo = $("input[name=cc_number]", "#checkoutForm1").val();
	foo = foo.charAt(0);
	foo2 = $(this);
	if (foo=='3') {
		var mask = "?9999";
		foo2.addClass('isAmex');
		foo2.rules("add", { required:true, cvv: false, cvv2: true });
		foo2.validate();
		foo2.unmask();
		foo2.mask(mask);
	} else {
		var mask = "?999";
		foo2.removeClass('isAmex');
		foo2.rules("add", { required:true, cvv: true, cvv2: false });
		foo2.validate();
		foo2.unmask();
		foo2.mask(mask);
	}
});
$("input[name=cc_cvv]", "#checkoutForm2").focus(function() {
	var foo = $("input[name=cc_number]", "#checkoutForm2").val();
	foo = foo.charAt(0);
	foo2 = $(this);
	if (foo=='3') {
		var mask = "?9999";
		foo2.addClass('isAmex');
		foo2.rules("add", { required:true, cvv: false, cvv2: true });
		foo2.validate();
		foo2.unmask();
		foo2.mask(mask);
	} else {
		var mask = "?999";
		foo2.removeClass('isAmex');
		foo2.rules("add", { required:true, cvv: true, cvv2: false });
		foo2.validate();
		foo2.unmask();
		foo2.mask(mask);
	}
});*/
$("select[name=fields_expmonth]", "#checkoutForm1").focus(function() {
	$("input[name=cc_number]", "#checkoutForm1").validate();
});

var ajaxText = ['.', '..', '...', '....', '.....']
var ajaxI = 0;
function showAjax() {
	$('#ajaxLoader p:eq(1)').html(ajaxText[ajaxI++%5]);
}

$("#checkoutForm2").validate({
	groups: {
    ExpDate: "fields_expmonth fields_expyear"
  },
	rules: {
	fullName: "required",
	lastName: "required",
	email: "required email",
	dayPhone: {
			required: true
			,phone: true
			,remote: {
				type: "POST"
				,url: 'cdyne_jquery_validate.php'
			}
		}
		,zip: {
			required:true
			,zipcode: true
			/*,remotezip: {
				type: "POST"
				,url: 'validate_zip.php'
			}*/

    },
    alt_phone: {required: false, phone2: false},
	cc_number: "required creditCard",
	fields_expmonth: "required",
	fields_expyear: "required",
	cc_cvv: "required cvv",
    country: "required"
	},	
	errorElement: "em",
		errorPlacement: function(error, element) {
		error.appendTo(element.parent());
	},
	messages: {
	fullName: {required: "<strong>Your FIRST NAME</strong> is required"},
	lastName: {required: "<strong>Your LAST NAME</strong> is required"},
	email: {required: "<strong>A VALID EMAIL ADDRES</strong> is required", email: "Please enter a <strong>VALID EMAIL ADDRESS</strong>"},
	dayPhone: {required: "<strong>A VALID DAYTIME PHONE NUMBER</strong> is required",phone: "Phone Requires <strong>10 Digits</strong>", remote:"Please verify if your phone number is correct."},	
	zip: {required: "<strong>A 5 DIGIT ZIP CODE</strong> is required", remotezip:"The <strong>ZIP CODE</strong> entered is invalid"},	
	cc_number: {required: "<strong>CARD NUMBER</strong> is required", creditCard: "<strong>CREDIT CARD</strong> requires 16 Digits"},
	fields_expmonth: { required: "<strong>EXPIRATION MONTH</strong> is required"},
	fields_expyear: { required: "<strong>EXPIRATION YEAR</strong> is required"},
	cc_cvv: {required: "<strong>CVV</strong> is required"},
	country: {required: "<strong>Country</strong> is required"}
	},
	submitHandler: function(form) {
			$('#ajaxLoader').overlay({ mask: { color: '#000', loadSpeed: 0, opacity: 0.85 }, closeOnClick: false, oneInstance: false, top: '30%', onLoad: function() {
			form.submit(); 
			setInterval("showAjax()",500) }});
      $('#ajaxLoader').overlay().load();
            internalLink = true
   	}
});


/*$("input[name=cc_number]", "#checkoutForm2")
.focus(function() {
var foo = $(this).val();
foo = foo.charAt(0);
if (foo=='3') { var mask = "?999999999999999"; } else { var mask = "?9999999999999999";}
//$(this).unmask();
//$(this).mask(mask);
});

$("input[name=cc_number]", "#checkoutForm2").keyup(function() {
	var foo = $(this).val();
	var foo2 = $("input[name=cc_cvv]", "#checkoutForm2");

		foo = foo.charAt(0);
	if (foo=='3') {
		var mask = "?9999";
		foo2.addClass('isAmex');
		foo2.rules("add", { required:true, cvv: false, cvv2: true });
		foo2.validate();
		foo2.unmask();
		foo2.mask(mask);
	} else {
		var mask = "?999";
		foo2.removeClass('isAmex');
		foo2.rules("add", { required:true, cvv: true, cvv2: false });
		foo2.validate();
		foo2.unmask();
		foo2.mask(mask);
	}
});
*/
$("input[name=dayPhone]", "#checkoutForm2")
.focus(function() {
var mask = "?(999) 999-9999";
$(this).unmask(mask).select().mask(mask,{completed:function(){$('input[name=Cardcvv]').focus();}});
});
$("input[name=dayPhoneIndex]", "#checkoutForm2")
.focus(function() {
var mask = "?(999) 999-9999";
$(this).unmask(mask).select().mask(mask,{completed:function(){$('input[name=Cardcvv]').focus();}});
});
$("input[name=nightPhone]", "#checkoutForm2")
.focus(function() {
var mask = "?(999) 999-9999";
$(this).unmask(mask).select().mask(mask,{completed:function(){$('input[name=Cardcvv]').focus();}});
});
$("input[name=zip]", "#checkoutForm2")
.focus(function() {
var mask = "?99999";
$(this).unmask(mask).select().mask(mask,{completed:function(){$('input[name=cc_number]').focus();}});
});	
$("input[name=zipNOI]", "#checkoutForm2")
.focus(function() {
var mask = "?99999";
$(this).unmask(mask).select().mask(mask,{completed:function(){$('input[name=cc_number]').focus();}});
});	
$("input[name=cc_number]", "#checkoutForm2")
.focus(function() {
var mask = "?9999999999999999";
$(this).unmask(mask).select().mask(mask,{completed:function(){$('input[name=Cardcvv]').focus();}});
});	
$("input[name=cc_cvv]", "#checkoutForm2")
.focus(function() {
var mask = "?999";
$(this).unmask(mask).select().mask(mask,{completed:function(){$('input[name=Cardcvv]').focus();}});
});

$(function () {
    var $form2 = $("#checkoutForm2"), $zip = $("input[name=alt_phone]", "#checkoutForm2");
    $form2.validate();
    $zip.mask("?(999) 999-9999");
});


$(".button").hover(function() {
	$('#tooltip').hide();
});
