// ====================  For the Delivery Time Estimator =========================

function deliveryEstimateError(errorCode, errorMessage){
	document.getElementById("deliveryDateBox").value = "Error: " + errorCode + " : " + errorMessage;	
}


	function changeShippingOption(){
		var deliveryOptionsListMenu = document.getElementById("deliveryOptionsSelect");
		var selectedShippingChoice = deliveryOptionsListMenu.value;
		
		for(var i=0; i<deliveryDestObj.shippingOptionsArr.length; i++){
			
			var apiShippingDesc = deliveryDestObj.shippingOptionsArr[i].shippingOptionName;
	
			if(apiShippingDesc == selectedShippingChoice){
				document.getElementById("deliveryDateBox").value = deliveryDestObj.shippingOptionsArr[i].arrivalDesc;
				
				// Set the new shipping method in our deliveryEstimateObject, it may affect the countdown timer.
				deliveryDestObj.selectNewShippingMethod(apiShippingDesc);
			}
		}
	}
 	function buildDeliveryWidget(){
		var deliveryOptionsListMenu = document.getElementById("deliveryOptionsSelect");
		for(var i=0; i<deliveryDestObj.shippingOptionsArr.length; i++){
			var shippingDescription = deliveryDestObj.shippingOptionsArr[i].shippingOptionName;
			// Add to the end of the existing options
			deliveryOptionsListMenu.options[deliveryOptionsListMenu.length] = new Option(shippingDescription, shippingDescription);
		}
		changeShippingOption();
		
		// Record the earliest delivery date in a global var.
		earliestDeliveryDateObj.setTime(Date.parse(deliveryDestObj.getEarliestDeliveryDate()));
	}
	
	function countdownTimerEvent(hours, minutes, seconds){
		
		var hoursDesc = "Hours";
		if(hours == "1")
			hoursDesc = "Hour";
		
		document.getElementById("countdownTimer").innerHTML = hours + " " + hoursDesc + ", " + minutes + " Min, " + seconds + " sec";
	}
	


	
	
var earliestDeliveryDateObj = new Date();
var deliveryDestObj = new DeliveryEstimates();
deliveryDestObj.attachResponseEvent(buildDeliveryWidget, this);
deliveryDestObj.attachErrorEvent(deliveryEstimateError, this);
deliveryDestObj.attachCountdownTimerEvent(countdownTimerEvent, this);


if(productIdOnPage != ""){
	deliveryDestObj.setProductID(productIdOnPage);
	deliveryDestObj.fireRequest();
}



