function updateShippingSelect() {
  var city = jQuery('#city').val();
  var state = jQuery('#state').val();
  var zip = jQuery('#zip').val();
  var country = jQuery('#country').val();
  jQuery.ajax({
    url: "/ssl-only/checkout/shipping",
    data: { 
      rm: 'methods', 
      city: city, 
      state: state, 
      zip: zip,
      country: country },
    dataType: 'text',
    type: 'GET',
    success: shippingMethodsCallBack,
  });
}

var shippingMethodsCallBack = function(response) {
	var alert_text = extractAlert(response);
	if(alert_text) {
		alert(alert_text);
	}
	response = response.replace(/\<alert\>.*\<\/alert\>/, '');
	jQuery("#ship_method").html(response);
}

function extractAlert(response) {
	var alert_pattern = /\<alert\>.*\<\/alert\>/;
	var alert_text = alert_pattern.exec(response);
	if(alert_text) {
		alert_text = new String(alert_text);
		alert_text = alert_text.replace(/\<alert\>/, '');
		alert_text = alert_text.replace(/\<\/alert\>/, '');
		return alert_text;
	} else {
		return null;
	}
}

function checkPO(addr) {
  if (addr.match(/^po|po box|p\.o\. box|post office box|post box|box \d+/i)) {
		var shippingSelect = document.getElementById('ship_method');
		if ( shippingSelect[0].value != 99 ) {
			alert("We're sorry but we can't ship to PO Boxes at this time. Please enter a street address.");
     	return false;
		}
	}
}

function counter(name, max_length) {
	var text = document.getElementById(name).value;
	var current_length = text.length;
	if (current_length > max_length) {
		alert ("Sorry, you've exceeded the maximum number of characters allowed.");
		text = text.substring(0, current_length-1);
		document.getElementById(name).value = text;
		return false;
	} else {
		document.getElementById(name + "-counter").value = max_length - current_length;
		return;
	}
}

function get_estimate() {
	var element = document.getElementById('ship_method');
	var link0 = element.value == 99 ? 'service=usps' : 'service=ups';
	var link = '/ssl-only/checkout/shipping/estimate/?' 
		+ link0
		+ '&city='+document.getElementById('city').value
		+ '&state='+document.getElementById('state').value
		+ '&zip='+document.getElementById('zip').value
		+ '&country='+document.getElementById('country').value;
	// The window name (in this case, 'ShippingEstimate') canNOT have a
	// space. Otherwise it will break in IE7.
	window.open(link, 'ShippingEstimate','scrollbars=1,status=0,width=900,height=500,location=0,directories=0,menubar=0,toolbar=0,resizable=0');
	return false;
}

jQuery(document).ready(function() {
	jQuery('#ship').keyup(function() {
		counter('ship', 30);
	});

	jQuery('#gift').keyup(function() {
		counter('gift', 250);
	});

	jQuery('#addr').blur(function() { 
		checkPO(this.value);
	});

	jQuery('#addr2').blur(function() { 
		checkPO(this.value);
	});

	jQuery("#city").change(function() {
		updateShippingSelect();
	})

	jQuery("#state").change(function() {
		updateShippingSelect();
	})

	jQuery("#zip").blur(function() {
		updateShippingSelect();
	});

	jQuery("#country").change(function() {
		 updateShippingSelect();
	});

	jQuery("#estimate").click(function() {
		 get_estimate();
	});
	
	jQuery(".phone").keyup(function() {
		var phone_part = jQuery(this)
		if(phone_part.val().length == phone_part.attr("maxlength")) {
			phone_part.next().focus();
		}
	})
});

