// ====================  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").value = 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);
deliveryDestObj.setProductID(productIdOnPage);
deliveryDestObj.fireRequest();















// ====================  For the Price calculator =========================



function productLoaderErrorEvent(statusCode, statusDesc){
	alert("Error loading Pricing Info.\n\nError Code: " + statusCode + "\nError Description: " + statusDesc);
}


function newQuantityChoice(newQuantity){
	productObj.selectedOptionsObj.setQuantity(newQuantity);
	updateProductSubtotal();

}

function SelectChoice(optionName, newChoice){
	productObj.selectedOptionsObj.setOptionChoice(optionName, newChoice);
	updateProductSubtotal();
}




var productLoaderObj = new ProductLoader();
var productObj = new Product();
productLoaderObj.attatchProductLoadingErrorEvent(productLoaderErrorEvent, this);

function buildPriceWidget(){
	// Set product object within a global variable
	productObj = productLoaderObj.getProductObj(productIdOnPage);
	
	var quantitiesArr = productObj.getQuantityChoicesArr();

	var priceCalc = "";
	
	var optNames = productObj.getOptionNames();
	for(var i=0; i<optNames.length; i++)
	{
		var optionName = optNames[i];

		if(productObj.checkIfOptionIsEmpty(optionName))
			continue;
		if(productObj.isOptionForAdministrators(optionName))
			continue;
		if(productObj.checkIfOptionHasSingleChoice(optionName))
			continue;

		var selectedChoiceName = productObj.getChoiceSelected(optionName);
		
		// If choice is hidden on the selected Option, then dont display any of the option choices.
		if(productObj.isChoiceIsHidden(optionName, selectedChoiceName))
			continue;

		// Figure out if the Option Description should be converted for HTML special characters.
		if(productObj.isOptionDescriptionHTMLformat(optionName))
			priceCalc +=  '<p class="CalculatorTextField">' + productObj.getOptionDescription(optionName) + "</p>";
		else
			priceCalc += '<p class="CalculatorTextField">' +  htmlize(productObj.getOptionDescription(optionName)) + "</p>";
			
			
		
		var choicesArr = productObj.getChoiceNamesForOption(optionName);
		
		priceCalc += "<select class='CalculatorTextField' name='option_" + i + "' id='option_" + i + "' onChange='SelectChoice(\""+addslashes(optionName)+"\",this.value)'> ";
		for(var x=0; x < choicesArr.length; x++){
			
			var choiceSelected = "";
			if(selectedChoiceName == choicesArr[x])
				choiceSelected = " selected='selected'";
				
			priceCalc += "<option value='"+ addslashes(choicesArr[x]) + "'" + choiceSelected + ">";
			priceCalc += htmlize(productObj.getChoiceDescription(optionName, choicesArr[x]));
			priceCalc += "</option>\n";
		}
		
		priceCalc += "</select><br />";

			
	}
		
	
	priceCalc += '<p class="CalculatorTextField">Quantity</p>';
	priceCalc += '<select name="quantityPick" id="quantityPick" class="dataDeliveryDrop" onchange="newQuantityChoice(this.value);">';
	
	for(var i=0; i<quantitiesArr.length; i++){
		if(quantitiesArr[i]==productObj.getSelectedQuantity())
			var selectedText = " selected='selected'";
		else
			var selectedText = "";
			
		priceCalc += "<option value='"+quantitiesArr[i]+"' "+selectedText+">"+addCommas(quantitiesArr[i])+"</option>";
	}
	priceCalc += "</select><br />";
	
	document.getElementById('priceCalculatorWidget').innerHTML = priceCalc;
	
	updateProductSubtotal();
	
	// This will set the radio buttons for offset/digitial
	newQuantityChoice(productObj.getSelectedQuantity());
	
}

function updateProductSubtotal(){
	document.getElementById('subtotalPrice').innerHTML = "$" + addCommas(productObj.getSubtotal());
}


productLoaderObj.attachProductLoadedEvent(productIdOnPage, buildPriceWidget);






