var canSubmit = true;
$(function () {
	// clear search default text on focus
	$(".clearDefault").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	$("#save_order").click(function(){
		if(!$("#agree_tc").is(':checked')){
			alert('You must agree to the terms and conditions to continue.');
			canSubmit = false;
		}else{
			canSubmit = true;
		}
	});
	// submit forms with links
	$("a.submit").click(function(){	
		if(canSubmit){
			$(this).closest("form").submit();
		}
		return false;	
	});
	$('form input').keypress(function(e){
		var c = e.which ? e.which : e.keyCode;
		if(c == 13){
		    $(this).closest('form').submit();
		} 
	});
	$('a.expand').click(function(){
		$more = $(this).closest('li').children('div.more');
		$('p', $more).slideToggle();
		return false;
	});
	$("#billing_equals_shipping").click(function(){
		if($(this).is(':checked')){
			$('input[name="shipping_first_name"]').val($('input[name="first_name"]').val());
			$('input[name="shipping_last_name"]').val($('input[name="last_name"]').val());
			$('input[name="shipping_address"]').val($('input[name="address"]').val());
			$('input[name="shipping_address2"]').val($('input[name="address2"]').val());
			$('input[name="shipping_city"]').val($('input[name="city"]').val());
			$('input[name="shipping_zip"]').val($('input[name="zip"]').val());
			$('#shipping_country_code').val($('#country_code').val());
			
			$("#shipping_zip").trigger('change');
			
		}else{
			$("input,select").attr('disabled', '');
		}
	});
	// Datepicker
	$('.datepicker').datepicker({
		dateFormat: 'd MM yy'
	});
});
