$(document).ready(function(){
	
	//validation
	
	var validator = $("#billingForm").validate({
		errorClass: "invalid",
		errorLabelContainer: "#error",
		highlight: function(element, errorClass) {
			$(element).addClass(errorClass);
			$(element.form).find("label[for=" + element.name + "]").addClass(errorClass);
			$("#warning").css({
				fontWeight: "bold",
				color: "rgb(172,38,27)"
			})
		},
		unhighlight: function(element, errorClass) {
			$(element).removeClass(errorClass);
			$(element.form).find("label[for=" + element.name + "]").removeClass(errorClass);
		},
		groups: {
		    phonenumber: "billing_phone1 billing_phone2 billing_phone3"
		},
		rules: {
			customer_fname: "required",
			customer_lname: "required",
			billing_address: "required",
			billing_city: "required",
			billing_country: "required",
			billing_state: {
				required: function(element){
					return $("#billing_country").val() == "Canada" || $("#billing_country").val() == "United States";
				}
			},
			billing_zip: {
				required: function(element){
					return $("#billing_country").val() == "Canada" || $("#billing_country").val() == "United States";
				}
			},
			billing_phone1: "required",
			billing_phone2: "required",
			billing_phone3: "required",
			billing_cardtype: "required",
			billing_cardnumber: {
				required: true,
				creditcard: true
			},
			billing_cardexpiry_month: "required",
			billing_cardexpiry_year: "required"
		}
	})
	
	//province/state swap
	$("#billing_country").change(function(){
		if( $(this).val() == "CA" || $(this).val() == "US" ){
			$.get('/checkout/inc.state_option1.asp', function(data){
				$('#selectState').html(data);
			});	
		}else{
			$.get('/checkout/inc.state_option2.asp', function(data){
				$('#selectState').html(data);
			});	
		}
	});
	
	//security popup
	
	var lmTimer;
	
	$("#learnMore,#onlineSecurityPopup").hover(
		function(){
			clearTimeout(lmTimer);
			$("#onlineSecurityPopup").css("display","block");
		},
		function(){
			lmTimer = setTimeout(function(){$("#onlineSecurityPopup").css("display","none");},500);
		}
	);
	
	//populate form
	if (useBillingAddress == 1){
	$("#useBillingAddress").attr("checked",true);
 }
 
	//$("#customer_fname").val(customer_fname);
	//$("#customer_lname").val(customer_lname);
	//$("#billing_address").val(billing_address);
	//$("#billing_address2").val(billing_address2);
	//$("#billing_city").val(billing_city);
	//$("#billing_country").val(billing_country);
	//$("#billing_state").val(billing_state);
   //$("#billing_zip").val(billing_zip);
   //$("#customer_email").val(customer_email);
  //$("#billing_phone1").val(billing_phone1);
	//$("#billing_phone2").val(billing_phone2);
	//$("#billing_phone3").val(billing_phone3);
	//$("#billing_cardtype").val(billing_cardtype);
	//$("#billing_cardnumber").val(billing_cardnumber);
	//$("#billing_cardexpiry_month").val(billing_cardexpiry_month);
	//$("#billing_cardexpiry_year").val(billing_cardexpiry_year);
	//$("#billing_cvn").val(billing_cvn);


});
