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

var tmpmethod;
var tmpcost;

function changeShippingMethod(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 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;
}


