// JavaScript Document


function getShippingRates(ShowMessage)	{				
	document.getElementById("ShipValueNotFound").style.display = "none";
	var ZipCode = document.ZipCodeForm.ZipCode.value;
	
	if(ZipCode == "" || isNaN(ZipCode)) {		
		alert("A valid zip code is required to calculate the shipping cost.");
		return false;
	}
	else {	
		var TotalWeight = document.ZipCodeForm.TotalWeight.value;
		var shipperzip = document.ZipCodeForm.shipperzip.value;
	
		document.getElementById("ShippingMethod_load").style.display = "block";
		document.getElementById("ShippingMethod_1").style.display ="none";
		
		var s = new ship();
		s.setCallbackHandler(getShippingResult);		
		s.CalcShipping(TotalWeight,shipperzip,ZipCode);		
		
		return false;
  }
}

function getShippingResult(ShippingRates)
{		
	if(ShippingRates.length > 0 ) {
		with(document.ShipCalcForm){		
			//Need to remove all options
			document.ShipCalcForm.ShippingMethod.options.length = 0;
			
			for(i=0;i<ShippingRates.length;i++){			
				var RateValues = ShippingRates[i].split(",");			
				var option = new Option();
					option.text=RateValues[1];
					option.value=RateValues[0];
					ShippingMethod.options[i] = option;		
			}
		}
	}
	else {
		//Need to remove all options
		document.ShipCalcForm.ShippingMethod.options.length = 0;
		document.getElementById("ShipValueNotFound").innerHTML = "<br>Shipping rates are not available for the zip code entered.";
		document.getElementById("ShipValueNotFound").style.display = "block";
	}
	
	CalcOrderTotal();		
	toggleShipMethod();
}


function CalcOrderTotal() {
	
	var ShipMethod = document.ShipCalcForm.ShippingMethod.value
			
	var DashPos = ShipMethod.indexOf("-");
	var ArithVal = eval((ShipMethod.length*1)-(DashPos*1));
	var ArithVal2 = eval((ShipMethod.length*1)-(ArithVal*1)+1);	
	if(document.ZipCodeForm.ZipCode.value == ""){
		ShipPrice = 0;
	}
	else{
		ShipPrice = ShipMethod.substring(ArithVal2,ShipMethod.length);
	}
	
	var SubTotal = eval(document.ShipCalcForm.Subtotal.value*1);
		
	var TotalPrice = eval((SubTotal*1)+(ShipPrice*1));	
	
	document.getElementById("OrderTotal").innerHTML = "Order Total: $"+cent(TotalPrice);
	document.CheckoutForm.SelectedShippingMethod.value = ShipMethod;
}

function cent(amount) {
// returns the amount in the .99 format 
	amount -= 0;
	amount = (Math.round(amount*100))/100;
	return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}

function toggleShipMethod() {		
	document.getElementById("ShippingMethod_load").style.display = "none";
	
	if(document.ZipCodeForm.ZipCode.value != '' && document.ShipCalcForm.ShippingMethod.length > 0) {
		if(document.getElementById("ShippingMethod_1").style.display=="none") {
			document.getElementById("ShippingMethod_1").style.display="block";
		}						
	}
	else {
		if(document.getElementById("ShippingMethod_1").style.display=="block") {
			document.getElementById("ShippingMethod_1").style.display="none";
		}			
	}		
}
