/*
 * PureTrak Core Javascript File
 * Copyright(c) 2008-2009, Pure Voice Communictions, Inc.
 * http://www.puretrak.com
 * @author Patrick Hallinan
 *
 */

function update_top_info_cart_items() {
	// -- Get shopping cart item amount
	Ext.Ajax.request({
		url		: '/products/get_cart_item_count',
		method	: 'POST',
		success	: function(response,options) {
			var data = Ext.util.JSON.decode(response.responseText);
			if(parseInt(data.item_count) == 1) {
				Ext.get('top_info_cart_items').update(data.item_count+' ITEM');
			} else {
				Ext.get('top_info_cart_items').update(data.item_count+' ITEMS');
			}
		}
	});
}

function update_shipping_information(shipping_method) {
	
	if(shipping_method == 0) {
		Ext.Ajax.request({
			url: '/checkout/update_shipping_session_information',
			method: 'POST',
			params: {shipping_method: 0, shipping_amount: 0.00},
			failure:function(response,options){
				Ext.MessageBox.alert('Warning','Error connecting to server...');
			},
			success:function(response,options){
				var country = Ext.getCmp('shipping_calc_country_cb').getValue();
				var postal_code = Ext.getCmp('shipping_calc_postal_code').getValue();
				Ext.StoreMgr.get('order_totals_store').reload({params:{destination_country: country, destination_postal_code: postal_code}});
			}   
		});
	} else {
		var rec = Ext.getCmp('shipping_method_cb').getStore().getById(shipping_method);
	
		Ext.Ajax.request({
			url: '/checkout/update_shipping_session_information',
			method: 'POST',
			params: {shipping_method: shipping_method, shipping_amount: rec.data.rate},
			failure:function(response,options){
				Ext.MessageBox.alert('Warning','Error connecting to server...');
			},
			success:function(response,options){
				var country = Ext.getCmp('shipping_calc_country_cb').getValue();
				var postal_code = Ext.getCmp('shipping_calc_postal_code').getValue();
				Ext.StoreMgr.get('order_totals_store').reload({params:{destination_country: country, destination_postal_code: postal_code}});
			}   
		});
	}
}

Ext.onReady(function() {
	// -- Enable quick tips - feedback for any errors
	Ext.QuickTips.init();
	
	update_top_info_cart_items();
	
});