// SHOW / HIDE PASSWORD FIELDS

function show_hide_password(elem) {
	var eData={v:elem.value, t:elem.type, s:elem.size, n:elem.name, c:elem.className, i:elem.id},
	newElem, newType=(eData.t=='password'?'text':'password');
	try {
		newElem=document.createElement("<input type='"+newType+"' name='"+eData.n+"' value='"+eData.v+"' size='"+eData.s+"' class='"+eData.c+"' id='"+eData.i+"'>");
	}
	catch(e) {
		newElem=document.createElement('input');
		newElem.name=eData.n;
		newElem.type=newType;
		newElem.size=eData.s;
		newElem.value=eData.v;
		newElem.className=eData.c;
		newElem.id=eData.i;
	}
	elem.parentNode.replaceChild(newElem, elem);
	return newElem.type;
}

// AGE VERIFICATION

function age_verification(age,year,month,day) {
	var mydate = new Date();
	mydate.setFullYear(year, month-1, day);

	var currdate = new Date();
	currdate.setFullYear(currdate.getFullYear() - age);
	if ((currdate - mydate) < 0){
		return false;
	}
	return true;
}

function removeFrame(){
	if (top.location != self.location) top.location = self.location;
}

function country_check(country) {
	if (country == "US" || country == "CA") {
		$("#a_altstate").hide();
		$("#a_altzip").hide();
		$("#a_state").show();
		$("#a_zip").show();
		$('label[for="state"]').html("State");
		$('label[for="zip"]').html("Zip Code");
	} else {
		$("#a_altstate").show();
		$("#a_altzip").show();
		$("#a_state").hide();
		$("#a_zip").hide();
		$('label[for="state"]').html("State / Province");
		$('label[for="zip"]').html("Postal Code");
	}
}

$(document).ready(function() { 

	removeFrame();

	$('#facebox').livequery(function() {
    	$(this).bgiframe();
	});
	$('a[rel*=facebox]').livequery(function() {
    	$(this).facebox();
		return false;
	});
	
	$("#content").corner("5px");
	
	$('.fields').tooltip({ 
	    track: true, 
	    delay: 0, 
	    showURL: false, 
	    showBody: " - ", 
	    fade: 250,
		top:-20
	});

	$(".close_errors").click(function() {
		$("#errors").hide();
	});
	
	$("#c_number").keyup(function() {
		var new_c_number = $("#c_number").val().replace(/ /gi, '').replace(/-/gi, '');
		$("#c_number").val(new_c_number);
	});
	
	$("#a_country,#a_state").addClass("field").addClass("full");
	$("#a_dob_ts_mo,#a_dob_ts_da,#a_dob_ts_yr,#c_expmo,#c_expyr").addClass("field");

	$("#a_country").bind('change', function() {
		var country = $(this).val();
		country_check(country);
	});

	country_check($("#a_country").val());

// VALIDATOR RULES


$.validator.addMethod("age_verification", function(value, element) { 
	return this.optional(element) || age_verification(18,$("#a_dob_ts_yr").val(),$("#a_dob_ts_mo").val(),$("#a_dob_ts_da").val());
}, "You must be 18+ years old");

$.validator.addMethod("password", function(value, element) { 
	return this.optional(element) || /\d/.test(value) && /[a-z]/i.test(value);
}, "Your password must contain letters and at least a number");

$.validator.addMethod("alphanumeric", function(value, element) { 
	return this.optional(element) || /^[A-Za-z0-9]+$/.test(value);
}, "Please enter an alphanumeric value");

$.validator.addMethod("alpha", function(value, element) { 
	return this.optional(element) || /^[A-Za-z' ']+$/.test(value);
}, "Please enter an alphabetic value");

$.validator.addMethod("valid_cc", function(value, element) { 
	return this.optional(element) || ccCheck({popup:false});
}, "There's a problem with your card");


});