//Pricing calculator
var currentProductType = "standard";
var currentProductSize= "4x6";
var currentProductQuantity = 0;

function fetchCurrentProductIdentifier(){

	if(currentProductType == "standard"){
		if(currentProductSize == "4x6")
			return 211;
		else if(currentProductSize == "55x85")
			return 212;
		else if(currentProductSize == "6x11")
			return 213;
		else
			alert("Product Config Error");
	}
	else if(currentProductType == "personalized"){
		if(currentProductSize == "4x6")
			return 214;
		else if(currentProductSize == "55x85")
			return 215;
		else if(currentProductSize == "6x11")
			return 216;
		else
			alert("Product Config Error");
	}
	else if(currentProductType == "mailed"){
		if(currentProductSize == "4x6")
			return 217;
		else if(currentProductSize == "55x85")
			return 218;
		else if(currentProductSize == "6x11")
			return 219;
		else
			alert("Product Config Error");
	}
	else{
		alert("Product Type Error.");
	}
}

function changeProductType(productType){
	currentProductType = productType;

	if(checkIfProductLoaded(fetchCurrentProductIdentifier()))
		buildQuantityChoices();

	updateProductSubtotal();
}
function changeSize(productSize){
	currentProductSize = productSize;
	updateProductSubtotal();
}
function changeQuantity(productQuantity){
	currentProductQuantity = productQuantity;
	updateProductSubtotal();
}

function buildQuantityChoices(){

	if(!checkIfProductLoaded(fetchCurrentProductIdentifier()))
		return;

	var productObj = productLoaderObj.getProductObj(fetchCurrentProductIdentifier());

	var quantityChoicesArr = productObj.getQuantityChoicesArr();

	// default to the first quantity choice when the list is built, if 5,000 quantity is not a choice
	var fiveThousandQuantityFound = false;
	for(var i=0; i<quantityChoicesArr.length; i++){
		if(quantityChoicesArr[i] == 5000){
			fiveThousandQuantityFound = true;
			break;
		}
	}

	if(fiveThousandQuantityFound)
		currentProductQuantity = 5000;
	else
		currentProductQuantity = quantityChoicesArr[0];


	var quantityListMenu = document.getElementById("productQuantity");

	quantityListMenu.options.length = 0;

	for(var i=0; i<quantityChoicesArr.length; i++){
		quantityListMenu.options[quantityListMenu.length] = new Option(addCommas(quantityChoicesArr[i]), quantityChoicesArr[i]);
		if(quantityChoicesArr[i] == currentProductQuantity)
			quantityListMenu.options[quantityListMenu.length - 1].selected = true;
	}
}

var productIDsLoadedArr = new Array();

function checkIfProductLoaded(prodId){

	for(var i=0; i<productIDsLoadedArr.length; i++){
		if(productIDsLoadedArr[i] == prodId)
			return true;
	}

	productLoaderObj.loadProductID(prodId);

	return false;
}

function productLoaderErrorEvent(statusCode, statusDesc){
	alert("Error loading Pricing Info.\n\nError Code: " + statusCode + "\nError Description: " + statusDesc);
}

function newQuantityChoice(newQuantity){
	currentProductQuantity = newQuantity;
}

function productLoadedEvent(productIDloaded){
	productIDsLoadedArr.push(productIDloaded);

	var productObj = productLoaderObj.getProductObj(fetchCurrentProductIdentifier());

	buildQuantityChoices();
	updateProductSubtotal();
}

var productLoaderObj = new ProductLoader();
productLoaderObj.attatchProductLoadingErrorEvent(productLoaderErrorEvent, this);
productLoaderObj.attachProductLoadedGlobalEvent(productLoadedEvent, this);

function updateProductSubtotal(){
	if(!checkIfProductLoaded(fetchCurrentProductIdentifier())){
		$("#pricePerPostcard").html("-- Loading --");
	}
	else{
		var productObj = productLoaderObj.getProductObj(fetchCurrentProductIdentifier());
		productObj.selectedOptionsObj.setQuantity(currentProductQuantity);

		var pricePerPostcard = productObj.getSubtotal() / productObj.getSelectedQuantity();
		pricePerPostcard = (Math.round(pricePerPostcard * 1000) / 1000);

		if(pricePerPostcard < 1){
			$("#pricePerPostcard").html((Math.round((pricePerPostcard * 100) * 10) / 10) + "&cent;");
		}
		else{
			$("#pricePerPostcard").html("$" + addCommas(Math.round(pricePerPostcard * 100) / 100));
		}
	}
}


if(checkIfProductLoaded(fetchCurrentProductIdentifier())){
	updateProductSubtotal();
}
