// JavaScript Document

function checkZip() {	

	var ZipCode = document.ZipCodeForm.ZipCode.value;
	
	if(ZipCode == "") {		
		alert("Please enter a valid zip code to calculate your shipping costs.");
		return false;
	}
	else {
		getShippingRates(0);
	}
	
		
}

function getShippingRates(ShowMessage)	{				
	document.getElementById("ShipValueNotFound").style.display = "none";
	var ZipCode = document.ZipCodeForm.ZipCode.value;
	var Country = document.ZipCodeForm.ShipCountry.value;
	
	if(ZipCode == "") {		
		alert("A valid zip code is required to calculate the shipping cost.");
		return false;
	}
	else if(Country == "United States" && (isNaN(ZipCode) || ZipCode.length < 5)) {
		alert("A valid zip code is required to calculate the shipping cost.");
		return false;										
	}
	else if(Country == "") {
		alert("A country must be selected to calculate the shipping cost.");
		return false;
	}
	else {	
		var TotalWeight = document.ZipCodeForm.TotalWeight.value;		
		var shipmethod = document.ZipCodeForm.ShippingMethod.value;
		var shipcarrier = document.ZipCodeForm.ShippingCarrier.value;
		
		document.getElementById("ShippingMethod_load").style.display = "block";
		document.getElementById("ShippingMethod_1").style.display ="none";
		
		var s = new ship();
		s.setCallbackHandler(getShippingResult);	
		
		//Real-time Shipping
		if(shipmethod == 1) {
			
			//UPS
			if(shipcarrier == 1) {			
				s.CalcUPSShipping(ZipCode,TotalWeight,Country,1,1);	
			}
			//USPS
			else if(shipcarrier == 2) {
				s.CalcUSPSShipping(ZipCode,TotalWeight,Country,1,1);		
			}
			
		}
		//Custom Shipping
		else {
			s.CalcCustomShipping(TotalWeight);		
		}
		
		return false;
  }
}

function getShippingResult(ShippingRates) {		
	
	if(ShippingRates.length > 0 ) {
		with(document.ShipCalcForm){		
			//Need to remove all options
			document.ShipCalcForm.ShipMeth.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];
					ShipMeth.options[i] = option;		
			}
			document.getElementById("ShippingMethod_2").style.display ="block";
			document.getElementById("FreeShip").style.display ="none";
		}
	}
	else {
		//Need to remove all options
		document.ShipCalcForm.ShipMeth.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 ClearPromo() {
	var PrevPromo = document.CheckoutForm.PromoOfferType.value;
	var PrevCode = document.PromoCodeForm.PromoCode.value;
	document.PromoCodeForm.PromoCode.value = "";	
	document.getElementById("PromoCodeNotFound").style.display="none";
	document.getElementById("PromoCodeFound").style.display="none";
	document.CheckoutForm.PromoPercentOff.value = 0;
	document.CheckoutForm.PromoOfferType.value = 0;
	document.CheckoutForm.PromotionalCodeApplied.value = "";		
	if(PrevPromo == 1) {
		getShippingRates();
		CalcOrderTotal();
	}
	if(PrevCode != '') {
		CalcOrderTotal();
	}
	
}


function CalcOrderTotal() {
	
	if(document.CheckoutForm.TotalProducts.value > 0) {
		
		var ShipMethod = document.ShipCalcForm.ShipMeth.value
	
		var DashPos = ShipMethod.indexOf("-");
		var ArithVal = eval((ShipMethod.length*1)-(DashPos*1));
		var ArithVal2 = eval((ShipMethod.length*1)-(ArithVal*1)+3);	
	
		if(document.ZipCodeForm.ZipCode.value == ""){
			ShipPrice = 0;
		}
		else{
			ShipPrice = ShipMethod.substring(ArithVal2,ShipMethod.length);
		}
	}
	else {
		ShipPrice = 0;	
	}
	
	var SubTotal = eval(document.CheckoutForm.Subtotal.value*1);	
	
	
	//Check to see if there is a promotional code for a dollar discount
	if(document.CheckoutForm.PromoPercentOff.value > 0 && document.CheckoutForm.PromoOfferType.value == 0) {
		var PercentOff = document.CheckoutForm.PromoPercentOff.value;
		
		//Percent off
		if(document.CheckoutForm.PromoDiscountType.value == 0) {
			var TotalOff = eval(((PercentOff*1)/100)*SubTotal);
			var CodeFoundMessage = "Discount amount of "+PercentOff+"% off -$"+cent(TotalOff);
		}
		//Dollar value off
		else {
			var TotalOff = PercentOff;
			var CodeFoundMessage = "Discount amount of $"+PercentOff+" off";
		}
		
		if(SubTotal >= document.CheckoutForm.PromoMinTotal.value) {
			SubTotal = eval(SubTotal-(TotalOff*1));
			document.getElementById("PromoCodeFound").innerHTML = CodeFoundMessage;
			document.getElementById("PromoCodeFound").style.display="block";
		}		
		else { 
			document.getElementById("PromoCodeNotFound").innerHTML = "The promotional code entered requires a minumum order of $"+document.CheckoutForm.PromoMinTotal.value+".";
			document.getElementById("PromoCodeNotFound").style.display="block";
			document.getElementById("PromoCodeFound").style.display="none";
		}		
		
	}
	else if (document.CheckoutForm.PromoOfferType.value == 1) { 
		
		if(SubTotal >= document.CheckoutForm.PromoMinTotal.value ) {
			var CodeFoundMessage = "Free Shipping Promotion";
			document.getElementById("PromoCodeFound").innerHTML = CodeFoundMessage;
			document.getElementById("PromoCodeFound").style.display="block";			
			document.getElementById("ShippingMethod_2").style.display ="none";
			document.getElementById("FreeShip").style.display ="block";
			document.getElementById("FreeShip").innerHTML = "Free Shipping";
			document.CheckoutForm.SelectedShippingMethod.value = "";
			document.CheckoutForm.FreeShipping.value = 1;
			ShipPrice = 0;
		}
		else {
			document.getElementById("PromoCodeNotFound").innerHTML = "The promotional code entered requires a minumum order of $"+document.CheckoutForm.PromoMinTotal.value+".";
			document.getElementById("PromoCodeNotFound").style.display="block";
			document.getElementById("PromoCodeFound").style.display="none";
		}
	}
	else {	
		document.getElementById("PromoCodeFound").style.display="none";
	}
	
	
	var TotalPrice = eval((SubTotal*1)+(ShipPrice*1));	
	
	document.getElementById("OrderTotal").innerHTML = "Order Total: $"+cent(TotalPrice);
	document.CheckoutForm.OrderTotal.value = 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.ShipMeth.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";
		}			
	}	
	
}


//Function to validate the promo code entered 
function ValidatePromoCode() {
	var PromoCode = document.PromoCodeForm.PromoCode.value;
	
	if(PromoCode == '') {
		alert("Please enter a promotional code.");
		return false;
	}
	else {
		var p = new promo();
		p.setCallbackHandler(getPromoResult);	
		p.FindPromoCode(PromoCode);		
	}
		
	
}

function getPromoResult(PromoResults) {		
	
	if(PromoResults == 0) {
		document.getElementById("PromoCodeNotFound").innerHTML = "The promotional code entered does not exist or is no longer valid.";
		document.getElementById("PromoCodeNotFound").style.display="block";
		document.CheckoutForm.PromoPercentOff.value = 0;	
		document.CheckoutForm.PromotionalCodeApplied.value = "";	
		CalcOrderTotal();
	}
	else {		
		document.CheckoutForm.PromoPercentOff.value = PromoResults[3];	
		document.CheckoutForm.PromoOfferType.value = PromoResults[4];
		document.CheckoutForm.PromoDiscountType.value = PromoResults[1];
		document.CheckoutForm.PromoMinTotal.value = PromoResults[2];
		document.CheckoutForm.PromotionalCodeApplied.value = PromoResults[0];	
		document.CheckoutForm.AffiliateName.value = PromoResults[5];	
		document.getElementById("PromoCodeNotFound").style.display="none";		
		CalcOrderTotal();	
	}
}

