$(document).ready(function(){
	var $this;
	
	// Ajax Start/Stop indicator
	$('#loading').ajaxStart(function() { $(this).show(); }).ajaxStop(function() { $(this).hide(); }).hide();

	
	// Insurance Calculator Form Validation
	$('#insuranceValue').validate({
		rules : { number : {required:true, number:true}},
		messages : {
			insureValue: "Not a valid amount.",
			trackingNum: "Tracking number is required.",
			insuranceType: "Please select an insurance type.",
			shipType: "Please select a shipping method."
		}
	});

	$('#skipToCheckout').click(function(){
		return true;
	});
				
	// Hide update button from JS users
	$('input[name=formUpdate]').hide();
	
	// Update total on item type change
	$('#insuranceType').change(function(){
		if ($('#insuranceType').val() == "Jewelry") {
			if ($('#shipType').val() == 'cod') { $('#shipType').val(''); }
			$("#shipType option[value='cod']").hide();
			$("#shipType option[value='secured']").show();
		}
		else if ($('#insuranceType').val() == "General Cargo") {
			if ($('#shipType').val() == 'secured') { $('#shipType').val(''); }
			$("#shipType option[value='secured']").hide();
			$("#shipType option[value='cod']").show();
		}
		if ( $('#insuranceType').val() != '' && $('#shipType').val() != '' ) { updateTotal(); }
		else { clearTotal(); }
	});
	
	// Update total on shipping change
	$('#shipType').change(function() { 
		$this = $(this).val();
		if ( $('#insuranceType').val() != '' && $this != '' ) { updateTotal(); }
		else { 
			$('#shipNotes').load('/pages/purchaseinsurance_rates.php?notes='+$this);
			clearTotal(); 
		}
	});
	
	// Set timeout for keypress ajax calls
	var search_timeout = undefined;
	
	// Update total when user keys amount in value input
	$('#insureValue').bind('keyup', function(){
		if( search_timeout !== undefined ) {
			clearTimeout(search_timeout);
		}
		search_timeout = setTimeout(function() {
			search_timeout = undefined;	
			if ( $('#insuranceType').val() != '' && $('#shipType').val() != '' ) { updateTotal(); }
			else { clearTotal(); }
		}, 1000);
	}).bind('keypress', function(e) {
		return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
	});
});	
	
// Function to update premium total
function updateTotal() {
	var item_calc = 0;
	var item_cost = 0;
	var item_max = 0;
	
	// Get insurance value
	var item_value = $('#insureValue').val();
	item_value = stripCommas(item_value);
	
	var ship_type = $('#shipType').val();
	var item_type = $('#insuranceType').val();
	
	$.getJSON('/pages/purchaseinsurance_rates.php?item='+item_type+'&ship='+ship_type+'&json', function(data){
		
		// Ensure Limits
		item_calc = Math.min(item_value, data.limit);

		// Calculate it
		item_cost = (item_calc*data.rate).toFixed(2);
		
		// Update value on site and in form 
		$('span.totalCost').html(item_cost);
		$('input.totalCost').val(item_cost);
		
		// Set limits
		$('#insureValue').attr('max',data.limit);
		$('#insureValueMsg').html(data.limit_txt);
		if ( parseInt(item_value) > parseInt(data.limit) ) {
			$('#insureValue').val(data.limit);
		}
	});
}
function clearTotal() {
	$('span.totalCost').html('');
	$('input.totalCost').val('');
}
function stripCommas(numString) {
	var re = /,/g;
	return numString.replace(re,"");
}
function hideLimits() {
	// Reset limits
	$('#insureValue').attr('max','100000');
	$('#insureValueMsg').html('');
}