$(document).ready(function(){
    jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");});

var Pyra={
	getHttpServer:function(url){
		var url=typeof(url)!='undefined'?url:'';
		return 'http://'+document.domain+url;
	},
	getURL:function(url){
		location.href=url;
	},
	getBrand:function(f){
		location.href=Pyra.getHttpServer()+'/brand/'+f.value;
	},
	fScreen:function(u){
		window.open(u, 'lgview', 'fullscreen=1,dependent=1,location=0,menubar=0,resizable=0,scrollbars=1,status=0,toolbar=0');
	},
	cart:function(){
		$('#cartFM').submit(function(){
			if($('#atn').val()==''){
				return false;
			}
			return true;
		});
		$('.checkout').click(function(){
			$('#atn').val('checkout');
			$('#cartFM').submit();
		});
		$('#updateCart').click(function(){
			$('#atn').val('update');
			$('#cartFM').submit();
		});
		$('#goBack').click(function(){
			
		});
	},
	cartDelete:function(code){
		if(confirm('Are you sure you want to remove this item from your shopping cart?')){
			$('#code').val(code);
			$('#atn').val('delete');
			$('#cartFM').submit();
		}
	},
	checkout:function(step){
		if(step==1){
			$('#checkoutRegisterFM').validate({
				rules: {
					email : {
						required: true,
						email: true
					},
					passwd:{
						required:true,
						rangelength:[4, 16]
					},
					passwd_c: {
						equalTo: "#passwd"
					}
				},
				messages: {
					email: "Please enter your email address",
					passwd: "Please enter the same password twice"
				},
				errorClass: "coolBoxError",
				errorLabelContainer: "#messageBox ul",
				wrapper: "li",
				highlight: function(element, errorClass){
					$(element).addClass(errorClass);
				}
			});
			$('#checkoutLoginFM').validate({
				rules: {
					email : {
						required: true,
						email: true
					},
					password: "required"
				},
				messages: {
					email: "Please enter your email",
					password: "Please enter your password"
				},
				errorClass: "coolBoxError",
				errorLabelContainer: "#messageBox2 ul",
				wrapper: "li",
				highlight: function(element, errorClass){
					$(element).addClass(errorClass);
				}
			});
		}else if(step==2){
			$('#billing_country').change(function(){
				if($(this).val()=='JM'){
					$('#billing_stateProvince').hide();
					$('#billing_usStateProvince').hide();
					$('#billing_jmStateProvince').show();
				}else if($(this).val()=='US'){
					$('#billing_stateProvince').hide();
					$('#billing_usStateProvince').show();
					$('#billing_jmStateProvince').hide();
					$('#billing_city_content').html('<input type="text" id="billing_city" name="billing_city" class="coolBox" />');
				}else{
					$('#billing_stateProvince').show();
					$('#billing_usStateProvince').hide();
					$('#billing_jmStateProvince').hide();
					$('#billing_city_content').html('<input type="text" id="billing_city" name="billing_city" class="coolBox" />');
				}
			});
			$('#billing_user_state_province').change(function(){
				$('#billing_state_province').val($(this).val());
			});
			$('#billing_jm_state_province').change(function(){
				$('#billing_state_province').val($(this).val());
			});
			$('#billing_us_state_province').change(function(){
				$('#billing_state_province').val($(this).val());
			});
			$('#shipping_country').change(function(){
				if($(this).val()=='JM'){
					$('#shipping_stateProvince').hide();
					$('#shipping_usStateProvince').hide();
					$('#shipping_jmStateProvince').show();
				}else if($(this).val()=='US'){
					$('#shipping_stateProvince').hide();
					$('#shipping_usStateProvince').show();
					$('#shipping_jmStateProvince').hide();
					$('#shipping_city_content').html('<input type="text" id="shipping_city" name="shipping_city" class="coolBox" />');
				}else{
					$('#shipping_stateProvince').show();
					$('#shipping_usStateProvince').hide();
					$('#shipping_jmStateProvince').hide();
					$('#shipping_city_content').html('<input type="text" id="shipping_city" name="shipping_city" class="coolBox" />');
				}
			});
			$('#shipping_user_state_province').change(function(){
				$('#shipping_state_province').val($(this).val());
			});
			$('#shipping_jm_state_province').change(function(){
				$('#shipping_state_province').val($(this).val());
			});
			$('#shipping_us_state_province').change(function(){
				$('#shipping_state_province').val($(this).val());
			});
			$('#billing_jm_state_province').change(function(){
				var b=$(this);
				$.get('/xml/getJMRegionCity.xml.php',{v:b.val(),t:1},
					function(data){
						var xmlObj=data.documentElement;
						$('#billing_city_content').html(xmlObj.childNodes[0].firstChild.nodeValue);
					}
				);
			});
			$('#shipping_jm_state_province').change(function(){
				var b=$(this);
				$.get('/xml/getJMRegionCity.xml.php',{v:b.val(),t:2},
					function(data){
						var xmlObj=data.documentElement;
						$('#shipping_city_content').html(xmlObj.childNodes[0].firstChild.nodeValue);
					}
				);
			});
			$('#coNewAddressFM').validate({
				rules: {
					billing_firstname : "required",
					shipping_firstname: {
						required : {
							depends: function(element){return $('#enter_shipping').attr('checked');}
						}
					},
					billing_lastname : "required",
					shipping_lastname: {
						required : {
							depends: function(element){return $('#enter_shipping').attr('checked');}
						}
					},
					billing_address1 : "required",
					shipping_address1: {
						required : {
							depends: function(element){return $('#enter_shipping').attr('checked');}
						}
					},
					billing_state_province : "required",
					shipping_state_province: {
						required : {
							depends: function(element){return $('#enter_shipping').attr('checked');}
						}
					},
					billing_city : {
						required : {
							depends : function(element){if(element.form.billing_country.value=='JM'){return true;}else{return false;}}
						}
					},
					shipping_city : {
						required : {
							depends : function(element){if(element.form.shipping_country.value=='JM'){return true;}else{return false;}}
						}
					},
					billing_zip_postal_code : "required",
					shipping_zip_postal_code: {
						required : {
							depends: function(element){return $('#enter_shipping').attr('checked');}
						}
					},
					billing_country : "required",
					shipping_country: {
						required : {
							depends: function(element){return $('#enter_shipping').attr('checked');}
						}
					},
					billing_telephone_1:{
						required: true,
						phoneUS: true
					},
					shipping_telephone_1: {
						required : {
							depends: function(element){return $('#enter_shipping').attr('checked');}
						},
						phoneUS: true
					},
					billing_telephone_2:{
						required: false,
						phoneUS: true
					},
					shipping_telephone_2: {
						required : false,
						phoneUS: true
					},
					billing_mobile:{
						required: false,
						phoneUS: true
					},
					shipping_mobile: {
						required : false,
						phoneUS: true
					}
				},
				messages: {
					billing_firstname: "Please enter your firstname",
					billing_lastname : "Please enter your lastname",
					billing_address1 : "Please enter an address",
					billing_state_province : "Please enter a state or province",
					billing_city : "Please select a city",
					billing_zip_postal_code : "PLease enter a zip or postal code",
					billing_country : "Please select a country",
					billing_telephone_1 : "Please enter a least one contact number",
					shipping_firstname: "Please enter a shipping firstname",
					shipping_lastname : "Please enter your shipping lastname",
					shipping_address1 : "Please enter a shipping address",
					shipping_state_province : "Please enter a shipping state or province",
					shipping_city  : "Please select a city",
					shipping_zip_postal_code : "PLease enter a shipping zip or postal code",
					shipping_country : "Please select a shipping country",
					shipping_telephone_1 : "Please enter a least one contact number for your shipping address"
				},
				errorClass: "coolBoxError",
				errorLabelContainer: "#messageBox ul",
				wrapper: "li",
				highlight: function(element, errorClass){
					$(element).addClass(errorClass);
				}
			});
			$('#enter_shipping').click(function(){
				if($(this).attr('checked')){
					$('#shippingAddress').show();
				}else{
					$('#shippingAddress').hide();
				}
			});
		}else if(step==3){
			$('.checkout').click(function(){
				$('#action').val('checkout');
				if($('.paymentMethods:checked').attr('checked')){
					$('#checkoutFM').submit();
				}else{
					alert('Please choose a payment method');
				}
			});
			$('.cartEdit').click(function(){
				window.location=Pyra.getHttpServer('/cart');
			});
			$('#checkoutFM').submit(function(){
				
			});
		}
	},
	validLogin:function(){
		$('#checkoutRegisterFM').validate({
			rules: {
				email : {
					required: true,
					email: true
				},
				passwd:{
					required:true,
					rangelength:[4, 16]
				},
				passwd_c: {
					equalTo: "#passwd"
				}
			},
			messages: {
				email: "Please enter your email address",
				passwd: "Please enter the same password twice"
			},
			errorClass: "coolBoxError",
			errorLabelContainer: "#messageBox ul",
			wrapper: "li",
			highlight: function(element, errorClass){
				$(element).addClass(errorClass);
			}
		});
		$('#checkoutLoginFM').validate({
			rules: {
				email : {
					required: true,
					email: true
				},
				password: "required"
			},
			messages: {
				email: "Please enter your email",
				password: "Please enter your password"
			},
			errorClass: "coolBoxError",
			errorLabelContainer: "#messageBox2 ul",
			wrapper: "li",
			highlight: function(element, errorClass){
				$(element).addClass(errorClass);
			}
		});
	},
	initOrders:function(){
		$('#orderMonth').change(function(){
			location.href=Pyra.getHttpServer('/myaccount/viewOrders/'+$(this).val());
		});
	}
};