// JavaScript Document

	jQuery.noConflict();
	
jQuery(document).ready(function()
{
	
//google.setOnLoadCallback(function() {

	
		jQuery("#spinner").bind("ajaxSend", function() {
		jQuery(this).show();
	}).bind("ajaxStop", function() {
		jQuery(this).hide();
	}).bind("ajaxError", function() {
		jQuery(this).hide();
	});


	var total = 0;
	
	function calcTotal()
	{
		
		//subtotal
		var currentTotal = jQuery("td.subTotal").text(); 
		currentTotal = currentTotal.replace('$', "").replace(',', "");
		
		//ship total
		
		var shiptotal1 = 0;
		
		shiptotal1 = jQuery("span.shiptotal").text();
		shiptotal1 = shiptotal1.replace('$', "");

		
		//tax total
		var taxTotal = jQuery('span.taxTotal').text();
		//alert (txTotal);
		if (taxTotal != ''){ 
		//alert('hi');
			taxTotal = taxTotal.replace('$', "");		
			var total1 ='$' + (parseFloat(shiptotal1) + parseFloat(currentTotal) + parseFloat(taxTotal)).toFixed(2); 
		
		} else {
		
			var total1 ='$' + (parseFloat(shiptotal1) + parseFloat(currentTotal)).toFixed(2); 
		}
		
		jQuery("strong.total").html("<strong>" + total1 + "</strong>");	
				
	}

	//This happens when the page loads
	//calcTotal();	
	

		
	jQuery("input[name='shipping_rate_id']").live('click' , function()
	{
		total = 0;

		var shiptotal = jQuery (this).attr('price');
		
		jQuery("span.shiptotal").html(shiptotal);
		
		calcTotal();

	});



	jQuery("input[name='ship_to_info_id']").live('click' , function()
	{
		
		var id = jQuery("input[name='ship_to_info_id']:checked").val()
				nocache = Math.random();
		jQuery('#result').load('administrator/components/com_virtuemart/classes/shipping/ajaxship.php?id=' + id + '&nocache=' + nocache + ' #shippingrates', function(){
																																									 																																						
			
			jQuery ("input'[name=shipping_rate_id]'").attr("checked", false);	
			
		});
		
		jQuery('span.taxTotal').load('administrator/components/com_virtuemart/classes/shipping/ajaxship.php?ship_to_info_id=' + id + ' #taxdisplay', function()
		{

			jQuery("span.shiptotal").html('$0.00');
			calcTotal();							 
									 
		});
		
		//jQuery('#result').load('components/com_virtuemart/themes/jxtc_kraftwerks/templates/checkout/list_shipping_methods.tpl.php?id=' + id);
		
	});
	


//for shipping estimater 
var zip;

jQuery ("input[name='zip']").keypress(function(e) {
if (e.keyCode == 13){

		jQuery ("input#calclick").focus();
		zip = ship_est();
	
}else return e.keyCode;

});

	jQuery ('.error').hide();
	
	//jQuery("input[name='submit_ship_est']").click(ship_est());
	jQuery ("input#calclick").click(function(){ship_est()});
	

function ship_est(){
	//jQuery("input[name='submit_ship_est']").live('click' , function()
	//{
		if (zip == jQuery("input[name='zip']").val()) return false; //if they put in the same zip as last time then dont do anything
		nocache = Math.random(); //i duno why i have to do this
		jQuery ('.error').hide();
		
		jQuery ("div#error_wrap").animate({
					//backgroundColor: "#f9f9f9",
					borderTopColor: "#f9f9f9",
					borderBottomColor: "#f9f9f9",
					borderLeftColor: "#f9f9f9",
					borderRightColor: "#f9f9f9"
					}, 'fast' );
		
		
		zip = jQuery("input[name='zip']").val();
		
		
		if (zip == "") {
			
			jQuery ('.error').show();
			
			//jQuery ("label#zip_error").show();
			
			//jQuery ("label#zip_error").fadeIn("slow");
//			jQuery ("div#error_wrap").animate({
//								//backgroundColor: "#c86161",
//								borderTopColor: "#eec5c5",
//								borderBottomColor: "#eec5c5",
//								borderLeftColor: "#eec5c5",
//								borderRightColor: "#eec5c5"
//								}, 100 );
			
			
			jQuery ("input#zip").focus();
			
			return false;
		}
		
		jQuery ('#ship_select').show();
		
		//alert (zip);
		jQuery('#shipest').load('administrator/components/com_virtuemart/classes/shipping/ajaxship.php?zip=' + zip + '&nocache=' + nocache + ' #shipestimates', function(){
			
			var shipest = jQuery ("#ship_select").val();

			if (shipest){
				jQuery("span.shiptotal").html(shipest);
				calcTotal();
			}
			
	
		});

	return zip;		
		
	//});
	
} //end function ship_est
	
    jQuery ("#ship_select").live('change',function () {
																																	  		  
		var shipest = jQuery (this).val();
		jQuery("span.shiptotal").html(shipest);
    		
		calcTotal();
    
	});
		
        

	
}); //end dom ready




function selectShippingRate()
{
	
   jQuery('input[name=shipping_rate_id]').each(
		function()
		{
			if (jQuery(this).val() == "standard_shipping%7CDHL%7CFREE+SHIPPING%7C0.00%7C22")
			{
				jQuery(this).attr('checked', true);
				calculatePrice(jQuery(this).val());
			}
			else
			{
				jQuery(this).removeAttr('checked');
			}	
		}
	)

	writeUserInfo();
}
