var shippingCost;
var subTotal;
var grandTotal;
var shippingMethod;
var shipMethodReg;

var tmpmethod;
var tmpcost;

function hideShippingTable(){
	jQuery("#shipping_processing_error").css('display','none');
	jQuery("#shipping_table").css('display','none');
	jQuery("#shipping_processing").css('display','block');
}

function showShippingTable(){
	jQuery("#shipping_processing").css('display','none');
	jQuery("#shipping_table").css('display','block');
}

var newcost;
var newmethod;

function changeShippingMethod(cost,method){
	newcost = cost;
	newmethod = method;
	tmpmethod = method;
	tmpcost = cost;
	hideShippingTable();
	setTimeout("saveChangeShippingMethod()",1500);
}

function changeShippingMethodBAK(cost,method){
	tmpmethod = method;
	tmpcost = cost;
	if(!method){
		return;
	}
	
	jQuery.ajax({
  		url: "/products/adjustShippingOptions.php?method=" + method,
  		cache: false
  	});
  	
	shippingCost = tmpcost;
	shippingMethod = tmpmethod;
	grandTotal = eval(subTotal) + eval(shippingCost);
	//alert(grandTotal);
	handleDisplayUpdates();
}

function saveChangeShippingMethod(){
	
	var url = "/products/adjustShippingOptions.php?method=" + newmethod;
	jQuery.ajax({
  		url: url,
  		cache: false,
  		success: function(html){
  			shippingCost = tmpcost;
			shippingMethod = tmpmethod;
			grandTotal = eval(subTotal) + eval(shippingCost);
			handleDisplayUpdates();
			showShippingTable();
			//alert(url + " " + cost + " " + grandTotal);
  		},
  		error: function(o,m,e){
     		//alert( "Data NOT Saved: " + m + " : " + e );
     		showShippingTable();
     		jQuery("#shipping_processing_error").css('display','block');
   		}
   	});
}




function handleDisplayUpdates() {
	if(document.getElementById('shipping_cost_display')){
	  document.getElementById('shipping_cost_display').innerHTML = "$" + CurrencyFormatted(shippingCost);
	} else {
		//alert("cant find ship cost");
	}		
	
	if(document.getElementById('grand_total_display')){
	  document.getElementById('grand_total_display').innerHTML = '$' + CurrencyFormatted(grandTotal);
	} else {
		//alert("cant find grand total");
	}		
	setSelectedShippingMethodClass();
}

function setSelectedShippingMethodClass(){
	var list;
	if(document.shipformname.shipMethod){
		list = document.shipformname.shipMethod;
	} else if(document.shipformname.userShippingMethod){
		list = document.shipformname.userShippingMethod;
	} else {
		return;
	}
	var id;
	for (var i=0; i < list.length; i++)  {
		id = "ship_method_" + list[i].value;
		if (list[i].checked)  {
			if(document.getElementById(id)){
				document.getElementById(id).className = "ship_selected";
			}	
		} else {
			// unselect it
			if(document.getElementById(id)){
				document.getElementById(id).className = "ship_not_selected";
			}
		}
	} 	}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}


function submitZip() {
	// change picture
	if( document.getElementById('submit_zip_button') ){
		document.getElementById('submit_zip_button').src = "/images/loadinfo.gif";
	}
	setTimeout("pause()",1500);
	return false;
}

function pause() {
	document.shipformname.submit();
	return false;
}



