
$(document).ready(function(){	
	$(document).ready(function() {		
		$('#id_country').change(function() {
			resetAjaxQueries();
			updateStateByIdCountry();
		});

		if (SE_RefreshMethod == 0)
		{
			$('#id_state').change(function() {
				resetAjaxQueries();
				updateCarriersList();
			});

			$('#zipcode').bind('keyup',function(e) {
				if (e.keyCode == '13')
				{		
					resetAjaxQueries();
					updateCarriersList();
				}												
			});
		}

		$('#update_carriers_list').click(function() {
			updateCarriersList();
		});

		$('#carriercompare_submit').click(function() {
			resetAjaxQueries();
			simulateSelection();
			return false;
		});

		$("input[name='carrier_price_value']").live('change', function() {
			disableUpdateCart();
		});

		updateStateByIdCountry();
		disableUpdateCart();
	});
});

var ajaxQueries = new Array();

function displayWaitingAjax(type, message)
{
	$('#SE_AjaxDisplay').find('p').html(message);
	$('#SE_AjaxDisplay').css('display', type);
	
	if(type == "block")
		$('#update_carriers_list').attr("disabled", "disabled");
	else if(type == "none")
		$('#update_carriers_list').removeAttr("disabled");
	
	disableUpdateCart();
}

function disableUpdateCart()
{
	var checked = $('input[name="carrier_price_value"]:checked').val()
	if(typeof checked == "undefined")
		$('#carriercompare_submit').attr("disabled", "disabled");
	else
		$('#carriercompare_submit').removeAttr("disabled");
}

function updateStateByIdCountry()
{
	$('#id_state').children().remove();
	$('#availableCarriers').slideUp('fast');
	$('#states').slideUp('fast');
	displayWaitingAjax('block', SE_RefreshStateTS);

	var query = $.ajax({
		type: 'POST',
		headers: { "cache-control": "no-cache" },
		url: baseDir + 'modules/carriercompare/ajax.php' + '?rand=' + new Date().getTime(),
		data: 'method=getStates&id_country=' + $('#id_country').val(),
		dataType: 'json',
		success: function(json) {
			if (json.length)
			{
				for (state in json)
				{
					$('#id_state').append('<option value=\''+json[state].id_state+'\' '+(id_state == json[state].id_state ? 'selected="selected"' : '')+'>'+json[state].name+'</option>');
				}
				$('#states').slideDown('fast');
			}
			if (SE_RefreshMethod == 0)
				updateCarriersList();
			displayWaitingAjax('none', '');
		}
	});
	ajaxQueries.push(query);
}

function updateCarriersList()
{
	$('#carriercompare_errors_list').children().remove();	
	$('#availableCarriers').slideUp('fast', function(){		
		$(this).find(('tbody')).children().remove();				
		$('#noCarrier').slideUp('fast');
		displayWaitingAjax('block', SE_RetrievingInfoTS);

		var query = $.ajax({
			type: 'POST',
			headers: { "cache-control": "no-cache" },
			url: baseDir + 'modules/carriercompare/ajax.php' + '?rand=' + new Date().getTime(),
			data: 'method=getCarriers&id_country=' + $('#id_country').val() + '&id_state=' + $('#id_state').val() + '&zipcode=' + $('#zipcode').val(),
			dataType: 'json',
			success: function(json) {
				if (json.length)
				{
					var html  = '';
					$.each(json, function(index, carrier)
					{
						html += '<tr class="'+(index % 2 ? 'alternate_' : '')+'item">'+
									'<td class="" width="64px">'+
										'&nbsp; <input type="radio" name="carrier_price_value" value="'+ ((displayPrice == 1) ? carrier.price_tax_exc : carrier.price) +'" id="id_carrier'+carrier.id_carrier+'" '+(id_carrier == carrier.id_carrier ? 'checked="checked"' : '')+'/>'+
									'</td>'+
									'<td class="carrier_name">'+
										'<label for="id_carrier'+carrier.id_carrier+'">'+
										(carrier.img ? '<img src="'+carrier.img+'" alt="'+carrier.name+'" />' : carrier.name)+
										'</label>'+
									'</td>'+
									'<td class="carrier_infos">'+
										((carrier.delay != null) ? carrier.delay : '') +
									'</td>'+
									'<td class="carrier_price">';

										if (carrier.price)
										{
											html += '<span class="price">'+(displayPrice == 1 ? formatCurrency(carrier.price_tax_exc, currencyFormat, currencySign, currencyBlank) : formatCurrency(carrier.price, currencyFormat, currencySign, currencyBlank))+'</span>';
										}
										else
										{
											html += txtFree;
										}

						html += 	'</td>'+
								'</tr>';
					});
					$('#carriers_list').append(html);

					displayWaitingAjax('none', '');
					$('#availableCarriers').slideDown();
				}
				else
				{
					displayWaitingAjax('none', '');
					$('#noCarrier').slideDown();
				}
			}
		});
		ajaxQueries.push(query);
	});

}
                               
function simulateSelection()
{
	$('#carriercompare_errors').slideUp();
	$('#carriercompare_errors_list').children().remove();

	var query = $.ajax({
		type: 'POST',
		headers: { "cache-control": "no-cache" },
		url: baseDir + 'modules/carriercompare/ajax.php' + '?rand=' + new Date().getTime(),
		data: 'method=simulateSelection&' + $('#compare_shipping_form').serialize(),
		dataType: 'json',
		success: function(json) {
			if (json.price != 0)
			{
				var price = formatCurrency(json.price, currencyFormat, currencySign, currencyBlank);
				$('#total_shipping').html(price);
				var total = formatCurrency(json.order_total + json.price, currencyFormat, currencySign, currencyBlank);
				$('#total_price').html(total);
			}
			else
			{
				$('#total_shipping').html(txtFree);
				var total = formatCurrency(json.order_total, currencyFormat, currencySign, currencyBlank);
				$('#total_price').html(total);
			}
			$('tr.cart_total_delivery').show();
		}
	});
	ajaxQueries.push(query);
}

function resetAjaxQueries()
{
	for (i = 0; i < ajaxQueries.length; ++i)
		ajaxQueries[i].abort();
	ajaxQueries = new Array();
}