/**
* Shipping Quote Script
* @category Divinity.Apps
* @package App.Shipping
* @version SVN:$Id: quote.js 32505 2009-12-28 18:24:39Z tosho $
*/

///////////////////////////////////////////////////////////////////////////////

jQuery.extend({
	
	/** Shipping-related operations */
	"shipping": {
		
		/** Shipping Quote operations */
		"quote" : {
			
			/** Set the correct country and state */
			"states" : function(country_id, state_id, state) {
				var states_url = smarty_vars['rel_html_url']
						+ 'index.php?page=states&action=ajax_state_options'
					+ '&country_id=' + country_id
					+ '&state_id=' + state_id
					+ '&state=' + state
					+ '&select_name=state_id&text_name=state_name&' + Math.random();
					
				jQuery.ajax({
					"type": "GET",
					"url": states_url,
					"dataType": 'html',
					"success": function(o) {
						jQuery('#shipping #state_id').html(o);
						}
					});
				},
				
			/** Validate the data from the shipping quote form before submitting it */
			"validate" : function(frm) {

				if (!frm.zip.value) {
					alert('Please enter your zip code.');
					return false;
					}
				
				if (frm.state_id) {
					if (frm.state_name) {
						if (!frm.state_id.value && !frm.state_name) {
							alert('Please select or enter your state.');
							return;
							}
						} else {
						
						if (!frm.state_id.value) {
							alert('Please select your state.');
							return false;
							}
						}
					} else {
					
					if (!frm.state_name.value) {
						alert('Please enter your state.');
						return false;
						}
					}

				return true;
				},
				
			/** Submit the shipping quote request */
			"submit" : function(btn) {
				
				if (!jQuery.shipping.quote.validate(btn.form)) {
					return false;
					}

				var state_id = 0;
				if (btn.form.state_id) {
					state_id = btn.form.state_id.value;
					}
				var state = '';
				if (btn.form.state) {
					state = btn.form.state.value;
					}
				
				var quote_url = smarty_vars['rel_html_url']
					+ 'index.php?page=shipping_quote&id=' + shipping_quote_sku
					+ '&quantity=1&country=' + btn.form.country_id.value
					+ '&zip=' + btn.form.zip.value + '&state_id=' + state_id + '&state=' + state;

				jQuery.ajax({
					"type": "GET",
					"url": quote_url,
					"dataType": 'html',
					"success": function(o) {
						jQuery('#shipping_quote').html(o);
						jQuery(btn)
							.removeClass('disabledbtn')
							.removeAttr('disabled');
						}
					});
				jQuery('#shipping_quote').html('Calculating...');

				jQuery(btn)
					.addClass('disabledbtn')
					.attr('disabled', 'disabled');
				return true;
				}
			}
		}
	});

//////////////////////////////////////////////////////////////////////////////

jQuery(document).ready(
	function() {
		/** Disable the empty country options and attach the states handler */
		jQuery('#shipping select[name="country_id"]').change(
			function() {
				jQuery.shipping.quote.states(
					this.options[this.selectedIndex].value, '', '');
				}
			).change().find('option').each(
			function() {
				this.disabled = this.value == '';
				}
			);
		
		/** Disable the enter key for the zip text input */
		jQuery('#shipping input[name="zip"]').keypress(
			function (event) {
				if (event.keyCode == 13) {
					jQuery('#shipping input[name="shipquote"]')
						.focus().click();
					}
				return event.keyCode != 13;
				}
			);

		/** Attach the shipping quote handler */
		jQuery('#shipping input[name="shipquote"]').click(
			function () {
				if (!jQuery.shipping.quote.validate(this.form)) {
					return false;
					}
				return jQuery.shipping.quote.submit(this);
				}
			);

		}
	);