$(document).ready(function() {

	productID = $("input[name='productID']:checked").attr("value");
	
	if (isdefined("product" + productID + "minCM")) {
		defaultMinCM = eval("product" + productID + "minCM");
	}
	if (isdefined("product" + productID + "maxCM")) {
		defaultMaxCM = eval("product" + productID + "maxCM");
	}
	if (isdefined("product" + productID + "minInches")) {
		defaultMinInches = eval("product" + productID + "minInches");
	}
	if (isdefined("product" + productID + "maxInches")) {
		defaultMaxInches = eval("product" + productID + "maxInches");
	}
	if (isdefined("product" + productID + "baseprice")) {
		defaultBasePrice = eval("product" + productID + "baseprice");
	}
	if (isdefined("product" + productID + "inc")) {
		defaultIncrementCM = eval("product" + productID + "inc");
	}
	if (isdefined("product" + productID + "estart")) {
		defaultExtraStart = eval("product" + productID + "estart");
	}
	if (isdefined("product" + productID + "eprice")) {
		defaultExtraPerCM = eval("product" + productID + "eprice");
	}
	
	updateOptions();
	updateQuantityString();
	resetReview();

	$("input[name='productID']").change(function(){
		productID = $(this).attr("value");
		if (isdefined("product" + productID + "minCM")) {
			defaultMinCM = eval("product" + productID + "minCM");
		}
		if (isdefined("product" + productID + "maxCM")) {
			defaultMaxCM = eval("product" + productID + "maxCM");
		}
		if (isdefined("product" + productID + "minInches")) {
			defaultMinInches = eval("product" + productID + "minInches");
		}
		if (isdefined("product" + productID + "maxInches")) {
			defaultMaxInches = eval("product" + productID + "maxInches");
		}
		if (isdefined("product" + productID + "baseprice")) {
			defaultBasePrice = eval("product" + productID + "baseprice");
		}
		if (isdefined("product" + productID + "inc")) {
			defaultIncrementCM = eval("product" + productID + "inc");
		}
		if (isdefined("product" + productID + "estart")) {
			defaultExtraStart = eval("product" + productID + "estart");
		}
		if (isdefined("product" + productID + "eprice")) {
			defaultExtraPerCM = eval("product" + productID + "eprice");
		}
		
		updateOptions();
		updateQuantityString();
	});
	
	$("select#rodLengthCM,select#rodLengthCMDecimals").change(function(){
		selectedValue = $(this).attr("value");		
		if (selectedValue != "") {
			$("select#rodLengthInches,select#rodLengthInchDecimals").attr("value","");
		}
	});
	$("select#rodLengthInches,select#rodLengthInchDecimals").change(function(){
		selectedValue = $(this).attr("value");		
		if (selectedValue != "") {
			$("select#rodLengthCM,select#rodLengthCMDecimals").attr("value","");
		}
	});
	$("input#feet,input#in").change(function(){
		selectedValue = $(this).attr("value");		
		if (selectedValue != "") {
			$("input#centimetres").attr("value","0");
		}
	});
	$("input#centimetres").change(function(){
		selectedValue = $(this).attr("value");		
		if (selectedValue != "") {
			$("input#feet,input#in").attr("value","0");
		}
	});
	$("select#barinches").change(function(){
		selectedValue = $(this).attr("value");		
		if (selectedValue != "") {
			$("select#barcm").attr("selectedIndex","0");
		}
	});
	$("select#barcm").change(function(){
		selectedValue = $(this).attr("value");		
		if (selectedValue != "") {
			$("select#barinches").attr("selectedIndex","0");
		}
	});
	
	$("input#calculateprice").click(function(){
		errorArray = validatePage();
		if (errorArray.length > 0) {
			var alertText = "Before we can calculate the price:\n";
			for (i=0;i<errorArray.length;i++) {
				alertText = alertText + "\n* " + errorArray[i];
			}
			alert(alertText);
		} else {
			calculatePrice();
		}
	});
	
	$("input[name='productID']").change(function(){
		resetReview();
	});
	$("input[name='colour']").change(function(){
		resetReview();
	});
	$("input[name='finish']").change(function(){
		resetReview();
	});
	$("input[name='finial']").change(function(){
		resetReview();
	});
	$("input[name='rodstyle']").change(function(){
		resetReview();
	});
	$("select#rodLengthCM,select#rodLengthCMDecimals").change(function(){
		resetReview();
	});
	$("select#rodLengthInches,select#rodLengthInchDecimals").change(function(){
		resetReview();
	});
	$("select#quantity").change(function(){
		resetReview();
	});
	$("input#feet,input#in,input#centimetres").change(function(){
		resetReview();
	});
	$("select#aclength").change(function(){
		resetReview();
	});
	$("select#barinches,select#barcm").change(function(){
		resetReview();
	});
});

function isdefined(variable) {
    return eval('(typeof('+variable+') != "undefined");');
}


function validatePage() {
	var errorArray = new Array();
	
	if ($("input[name='colour']").length){
		if ($("input[name='colour']:checked").attr("value") === undefined) {
			errorArray.push("Please select a style");
		}
	}
	
	if ($("select#aclength").length){	
		if ($("select#aclength").attr("value") === "") {
			errorArray.push("Please select a length");
		}	
	}
	
	if ($("input[name='feet']").length && $("input[name='in']").length && $("input[name='centimetres']").length){	
		if (
			($("input[name='feet']").attr("value") === "" || $("input[name='feet']").attr("value") === "0") &&
			($("input[name='in']").attr("value") === "" || $("input[name='in']").attr("value") === "0") &&
			($("input[name='centimetres']").attr("value") === "" || $("input[name='centimetres']").attr("value") === "0")
			) {
				errorArray.push("Please choose a length");
		}
	}
	
	if ($("input[name='finish']").length){
		if ($("input[name='finish']:checked").attr("value") === undefined) {
			errorArray.push("Please select a finish");
		}
	}
	
	if ($("select#barinches").length && $("select#barcm").length){	
		if (($("select#barinches").attr("value") === "") && ($("select#barcm").attr("value") === "")) {
			errorArray.push("Please select a length");
		}	
	}
	
	if ($("input[name='finial']").length){
		if ($("input[name='finial']:checked").attr("value") === undefined) {
			errorArray.push("Please select a finial");
		}
	}
	
	if ($("input[name='rodstyle']").length){
		if ($("input[name='rodstyle']:checked").attr("value") === undefined) {
			errorArray.push("Please select a rod style");
		}
	}
	
	if ($("select#rodLengthInches").length){
		var rodlengtherror = false;
		if($("select#rodLengthCM").length) {
			if ($("select#rodLengthInches").attr("value") === "" && $("select#rodLengthCM").attr("value") === "") {
				var rodlengtherror = true;
			}
		} else {
			if ($("select#rodLengthInches").attr("value") === "") {
				var rodlengtherror = true;
			}
		}
		
		if (rodlengtherror == true) {
			errorArray.push("Please select a rod length");
		}	
	}
	
	if ($("input[name='bindingOption']").length){
		if ($("input[name='bindingOption']:checked").attr("value") === undefined) {
			errorArray.push("Please select your binding");
		}
	}
	
	return errorArray;
}

function updateOptions() {	
	if ($("select#rodLengthInches").length){	
		inchDDhtml = '<option value=""></option>';
		inchDecimalDDhtml = '<option value="0">.0</option><option value=".25">.25</option><option value=".5">.5</option><option value=".75">.75</option>';
		for (inch=defaultMinInches;inch<=defaultMaxInches;inch=inch+rodLengthInchStep) {
			inchDDhtml = inchDDhtml + '<option value="' + inch + '">' + inch + '</option>';
		}
		$("select#rodLengthInches").html(inchDDhtml);
		if(showIncrementDD == true) {
			$("select#rodLengthInchDecimals").html(inchDecimalDDhtml);
		}
		
		if(showCMDD) {
			cmDDhtml = '<option value=""></option>';
			cmDecimalDDhtml = '<option value="0">.0</option><option value=".5">.5</option>';
			
			for (cm=defaultMinCM;cm<=defaultMaxCM;cm=cm+rodLengthCMStep) {
				cmDDhtml = cmDDhtml + '<option value="' + roundNumber(cm,2) + '">' + roundNumber(cm,2) + '</option>';
			}
			$("select#rodLengthCM").html(cmDDhtml);
			
			if(showIncrementDD == true) {
				$("select#rodLengthCMDecimals").html(cmDecimalDDhtml);
			}
		}
	}
}

function updateQuantityString() {
		
	quantityString = "";
	
	if ($("select#rodLengthInches").length && $("select#rodLengthCM").length){	
		if (isdefined("defaultBasePrice")) {
			convertedBasePrice = defaultBasePrice * eval('conversionRate' + currencyCode);
			quantityString = quantityString + eval('currencySymbol' + currencyCode) + convertedBasePrice.toFixed(2) + " each for any length";
		}
		if (isdefined("defaultExtraStart")) {
			quantityString = quantityString + " up to " + defaultExtraStart + "cm (" + Math.round(defaultExtraStart/2.54) + '"' + ")";
		}
		if (isdefined("defaultExtraPerCM")) {
			convertedExtraPrice = (defaultExtraPerCM*2.54) * eval('conversionRate' + currencyCode);
			quantityString = quantityString + " and then " + eval('currencySymbol' + currencyCode) + convertedExtraPrice.toFixed(2) + " per additional 2.54cm (1" + '"' + ")";
		}
	} else if ($("select#barinches").length && $("select#barcm").length){	
		if (isdefined("defaultBasePrice")) {
			convertedBasePrice = defaultBasePrice * eval('conversionRate' + currencyCode);
			quantityString = quantityString + eval('currencySymbol' + currencyCode) + convertedBasePrice.toFixed(2) + " each";
		}
		if (isdefined("barMinCM") && isdefined("barextraprice") && isdefined("barIncrementCM")) {
			quantityString = quantityString + " for any length up to " + Math.round(barMinCM) + "cm (" + Math.round(barMinCM/2.54) + '"' + ")";		
			convertedExtraPrice = barextraprice * eval('conversionRate' + currencyCode);
			quantityString = quantityString + " and then " + eval('currencySymbol' + currencyCode) + convertedExtraPrice.toFixed(2) + " per additional " + Math.round(barIncrementCM) + "cm (" + Math.round(barIncrementCM/2.54) + '"' + ")";
		}	
	}
	
	$("label.quantitylabel").html(quantityString);
}

function decimaldisplay(thisdecimal) {
		
	if (Math.floor(thisdecimal) == thisdecimal) {
		var displaystring = parseFloat(thisdecimal).toFixed(0);
	} else {
		var displaystring = thisdecimal;
	}	
	
	return displaystring;
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function calculatePrice() {
	runningtotal = defaultBasePrice;
	runnerPricePerMetre = defaultBasePrice;
	
	productID = $("input[name='productID']:checked").attr("value");
	optionArray = new Array();
	optionArray.push($("h1.designTitle").html());
	
	if (isdefined("product" + productID + "name")) {
		optionArray.push(eval("product" + productID + "name"));
	}

	if ($("input[name='colour']").length){
		colourID = $("input[name='colour']:checked").attr("value");
		colourExtraPrice = eval("colour" + colourID + "price");
		runningtotal = runningtotal + colourExtraPrice;
		optionArray.push(eval("colour" + colourID + "name"));
	}
	
	if ($("select#aclength").length){
		if ($("select#aclength").attr("value") != "") {
			var metresAboveBase = $("select#aclength").attr("value") - defaultAcMinM;
			var amountToAdd = defaultAcExtraPrice * metresAboveBase;
			
			acLengthValue = "Length: " + $("select#aclength").attr("value") + " metres";
			optionArray.push(acLengthValue);
			runningtotal = runningtotal + amountToAdd;
		}
	}

	if ($("input[name='feet']").length && $("input[name='in']").length && $("input[name='centimetres']").length){	
		
		if (($("input[name='feet']").attr("value") != "" && $("input[name='feet']").attr("value") != "0") ||
			($("input[name='in']").attr("value") != "" && $("input[name='in']").attr("value") != "0")) {
			
			feetInchesFormat = "";
			if ($("input[name='feet']").attr("value") != "" && $("input[name='feet']").attr("value") != "0") {
				feetInchesFormat = feetInchesFormat + $("input[name='feet']").attr("value") + " feet ";
			}
			if ($("input[name='in']").attr("value") != "" && $("input[name='in']").attr("value") != "0") {
				feetInchesFormat = feetInchesFormat + $("input[name='in']").attr("value") + " inches";
			}
			
			runnerLengthValue = "Length: " + feetInchesFormat;
			var inchesfromfeet = parseFloat($("input[name='feet']").attr("value") * 12);
			var inchesfrominches = parseFloat($("input[name='in']").attr("value"));
			runnerLengthCM = Math.round((inchesfromfeet+inchesfrominches)*2.54);
		} else {
			runnerLengthValue = "Length: " + $("input[name='centimetres']").attr("value") + " centimetres";
			runnerLengthCM = $("input[name='centimetres']").attr("value");
		}
		
		runningtotal = (runnerPricePerMetre * (runnerLengthCM/100));
		optionArray.push(runnerLengthValue);
	}

	if ($("input[name='finish']").length){
		finishID = $("input[name='finish']:checked").attr("value");
		finishExtraPrice = eval("finish" + finishID + "price");
		runningtotal = runningtotal + (finishExtraPrice);
		optionArray.push(eval("finish" + finishID + "name"));
	}
	
	if ($("select#barinches").length && $("select#barcm").length){
		if ($("select#barinches").attr("value") != "") {
			barLengthValue = "Length: " + $("select#barinches").attr("value") + " inches";
			optionArray.push(barLengthValue);
			barlengthincs = $("select#barinches").attr("selectedIndex") - 1;
		} else if($("select#barcm").attr("value") != "") {
			barLengthValue = "Length: " + $("select#barcm").attr("value") + " centimetres";
			optionArray.push(barLengthValue);
			barlengthincs = $("select#barcm").attr("selectedIndex") - 1;
		}
		
		runningtotal = runningtotal + (barextraprice * barlengthincs);
	}
	
	if ($("input[name='finial']").length){
		finialID = $("input[name='finial']:checked").attr("value");
		finialExtraPrice = eval("finial" + finialID + "price");
		runningtotal = runningtotal + finialExtraPrice;
		optionArray.push(eval("finial" + finialID + "name"));
	}
	
	if ($("input[name='rodstyle']").length){
		rodstyleID = $("input[name='rodstyle']:checked").attr("value");
		rodstyleExtraPrice = eval("rodstyle" + rodstyleID + "price");
		runningtotal = runningtotal + rodstyleExtraPrice;
		optionArray.push(eval("rodstyle" + rodstyleID + "name"));
	}
	
	if ($("select#rodLengthInches").length){
		if ($("select#rodLengthInches").attr("value") != "") {
			trueInchLength = parseFloat($("select#rodLengthInches").attr("value"));
			if($("select#rodLengthInchDecimals").length && $("select#rodLengthInchDecimals").attr("value") != "") {
				trueInchLength = trueInchLength + parseFloat($("select#rodLengthInchDecimals").attr("value"));
			}
			trueCMLength = trueInchLength * 2.54;
			rodLengthValue = "Rod Length: " + trueInchLength + " inches";
			optionArray.push(rodLengthValue);
		} else if($("select#rodLengthCM").length && $("select#rodLengthCM").attr("value") != "") {
			trueCMLength = parseFloat($("select#rodLengthCM").attr("value"));
			if($("select#rodLengthCMDecimals").length && $("select#rodLengthCMDecimals").attr("value") != "") {
				trueCMLength = trueCMLength + parseFloat($("select#rodLengthCMDecimals").attr("value"));
			}
			rodLengthValue = "Rod Length: " + trueCMLength + " centimetres";
			optionArray.push(rodLengthValue);
		}
		
		if (typeof defaultExtraStart != "undefined") {
			if (defaultExtraStart < trueCMLength) {			
				extraToChargeFor = trueCMLength - defaultExtraStart;
				amountToAdd = defaultExtraPerCM * (Math.ceil(extraToChargeFor/defaultIncrementCM) * defaultIncrementCM);
				amountToAdd = Math.ceil(amountToAdd * 100) / 100;
				runningtotal = runningtotal + amountToAdd;
			}
		}			
	}
	
	if ($("select#quantity").length){
		quantity = $("select#quantity").attr("value");
		runningtotal = runningtotal * quantity;
		optionArray.push("Quantity: " + quantity);
	}
	
	if ($("input[name='bindingOption']").length) {
		binding = $("input[name='bindingOption']:checked").attr("value");
		optionArray.push("Binding: " + binding);
	}
	
	convertedPrice = runningtotal * eval('conversionRate' + currencyCode);
	displayPrice = eval('currencySymbol' + currencyCode) + convertedPrice.toFixed(2);
	
	htmlReviewCode = "<ul>";
	for (i=0;i<optionArray.length;i++) {
		htmlReviewCode = htmlReviewCode + "<li>" + optionArray[i] + "</li>";
	}
	htmlReviewCode = htmlReviewCode + "</ul>";
	
	$("div#reviewitem").html(htmlReviewCode);
	$("p#product_price").html(displayPrice);
	$("input#addToBasket").removeClass("disabled").removeAttr('disabled');
}

function resetReview() {
	htmlReviewCode = "<ul>";
	htmlReviewCode = htmlReviewCode + "<li>" + $("h1.designTitle").html() + "</li>";
	htmlReviewCode = htmlReviewCode + "</ul>";
	
	$("div#reviewitem").html(htmlReviewCode);
	$("p#product_price").html("&nbsp;");
	$("input#addToBasket").addClass("disabled").attr('disabled', 'disabled');
}
