﻿function addLoadEvent(func){
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function show(element){
	if (!document.getElementById) return false;
	
	document.getElementById(element).style.display = "block";
}

function hide(element){
	if (!document.getElementById) return false;

	document.getElementById(element).style.display = "none";
}

var boxPrice=0;
var paintPrice=0;
var photoPrice=0;
var calligraphyPrice = 0;
var flowerPrice = 0;
var fabricPrice = 0;
var morePrice = 0;
var shippingPrice = 0;
var insurancePrice = 0;

function setup(){
	if (!document.getElementById("PaintOptions")) return false;
	if (!document.getElementById("PhotoPrintingOptions")) return false;
	if (!document.getElementById("CalligraphyOptions")) return false;
	if (!document.getElementById("GiftOptions")) return false;
	
	hide("PaintOptions");
	hide("PhotoPrintingOptions");
	hide("CalligraphyOptions");
	hide("GiftOptions");
}

function priceSelect(element){
	if (!document.getElementById) return false;
	if (!document.getElementById(element)) return false;
	
	document.getElementById(element).className = "price";
}

function priceDeselect(element){
	if (!document.getElementById) return false;
	if (!document.getElementById(element)) return false;
	
	document.getElementById(element).className = "pricedim";
}

function updatePrice(){ 
	if (!document.getElementById("totalprice")) return false;
	if (!document.getElementById("EstimatedTotal")) return false;

	total = boxPrice + photoPrice + calligraphyPrice + flowerPrice + fabricPrice + morePrice + shippingPrice + insurancePrice;
		
	priceString = "$" + total + ".00";

	document.getElementById("totalprice").firstChild.nodeValue = priceString;
	document.getElementById("EstimatedTotal").value = "$" + total + ".00";
}

function setboxclick(section, price, priceobject){
	var box = document.getElementById(section);
	var inputs = box.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		inputs[i].onclick = function(){
			document.getElementById("BoxTypeSmallPrice").className = "pricedim";
			document.getElementById("BoxTypeLargePrice").className = "pricedim";
			if(!notCustom) document.getElementById("BoxTypeOtherPrice").className = "pricedim";
			priceSelect(priceobject);
			boxPrice = price;
			document.getElementById("ShippingUSPS").disabled=false;
			document.getElementById("ShippingOther").disabled=false;
			/* recalculate shipping */
			if (document.getElementById("ShippingUSPS").checked==true) {
				if (price==80) {
					shippingPrice = 8;
					priceDeselect("USPSPriceBig");
					priceDeselect("ShippingOtherPrice");
					priceSelect("UPSSPrice");
				}
				else if (price==100) {
					shippingPrice = 16;
					priceDeselect("USPSPrice");
					priceDeselect("ShippingOtherPrice");
					priceSelect("USPSPriceBig");
				}
			}

			updatePrice();
		}
	}
}

function formclicks(){
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	
	/* box types */
	if (!document.getElementById("BoxTypeSmall")) return false;
	if (!document.getElementById("BoxTypeLarge")) return false;
	if (!document.getElementById("BoxTypeOther") || !document.getElementById("BoxTypeOtherPrice") || !document.getElementById("BoxTypeOtherText")) {
		notCustom = true;
	} else {
		notCustom = false;
	}
	if (!document.getElementById("BoxTypeSmallPrice")) return false;
	if (!document.getElementById("BoxTypeLargePrice")) return false;

	setboxclick("BoxTypeSmall",80,"BoxTypeSmallPrice");
	setboxclick("BoxTypeLarge",100,"BoxTypeLargePrice");
	if(!notCustom) {
		setboxclick("BoxTypeOther",100,"BoxTypeOtherPrice");
		var BoxTypeOtherText = document.getElementById("BoxTypeOtherText");
		BoxTypeOtherText.onfocus = function(){
			priceDeselect("BoxTypeSmallPrice");
			priceDeselect("BoxTypeLargePrice");
			priceSelect("BoxTypeOtherPrice");
			document.getElementById("BoxTypeOtherField").checked=true;
			boxPrice = 100;
			updatePrice();
		}
	}
	
	/* paint options */
	if (!document.getElementById("PaintNo")) return false;
	if (!document.getElementById("PaintYes")) return false;
	if (!document.getElementById("PaintPrice")) return false;
	if (!document.getElementById("PaintOptions")) return false;

	var PaintNo = document.getElementById("PaintNo");
	PaintNo.onclick = function(){
		priceDeselect("PaintPrice");
		paintPrice = 0;
		updatePrice();
		hide("PaintOptions");
	}
	
	var PaintNo = document.getElementById("PaintYes");
	PaintNo.onclick = function(){
		priceSelect("PaintPrice");
		photoPrice = 30;
		updatePrice();
		show("PaintOptions");
	}


	/* photo options */
	if (!document.getElementById("PhotoPrintingNo")) return false;
	if (!document.getElementById("PhotoPrintingYes")) return false;
	if (!document.getElementById("PhotoPrice")) return false;
	if (!document.getElementById("PhotoPrintingOptions")) return false;

	var PhotoPrintingNo = document.getElementById("PhotoPrintingNo");
	PhotoPrintingNo.onclick = function(){
		priceDeselect("PhotoPrice");
		photoPrice = 0;
		updatePrice();
		hide("PhotoPrintingOptions");
	}
	
	var PhotoPrintingYes = document.getElementById("PhotoPrintingYes");
	PhotoPrintingYes.onclick = function(){
		priceSelect("PhotoPrice");
		photoPrice = 15;
		updatePrice();
		show("PhotoPrintingOptions");
	}

	/* calligraphy options */
	if (!document.getElementById("CalligraphyNo")) return false;
	if (!document.getElementById("CalligraphyYes")) return false;
	if (!document.getElementById("CalligraphyPrice")) return false;
	if (!document.getElementById("CalligraphyOptions")) return false;

	var CalligraphyNo = document.getElementById("CalligraphyNo");
	CalligraphyNo.onclick = function(){
		priceDeselect("CalligraphyPrice");
		calligraphyPrice = 0;
		updatePrice();
		hide("CalligraphyOptions");
	}
	
	var CalligraphyYes = document.getElementById("CalligraphyYes");
	CalligraphyYes.onclick = function(){
		priceSelect("CalligraphyPrice");
		calligraphyPrice = 25;
		updatePrice();
		show("CalligraphyOptions");
		document.getElementById("CalligraphyText").focus();
	}

	/* flower options */
	if (!document.getElementById("FlowersNo")) return false;
	if (!document.getElementById("FlowersSmall")) return false;
	if (!document.getElementById("FlowersPetals")) return false;
	if (!document.getElementById("FlowersLayer")) return false;
	if (!document.getElementById("FlowersSmallPrice")) return false;
	if (!document.getElementById("FlowersLayerPrice")) return false;

	var FlowersNo = document.getElementById("FlowersNo");
	FlowersNo.onclick = function(){
		priceDeselect("FlowersSmallPrice");
		priceDeselect("FlowersLayerPrice");
		flowerPrice = 0;
		updatePrice();
	}
	
	var FlowersSmall = document.getElementById("FlowersSmall");
	FlowersSmall.onclick = function(){
		priceSelect("FlowersSmallPrice");
		priceDeselect("FlowersLayerPrice");
		flowerPrice = 30;
		updatePrice();
	}

	var FlowersPetals = document.getElementById("FlowersPetals");
	FlowersPetals.onclick = function(){
		priceSelect("FlowersSmallPrice");
		priceDeselect("FlowersLayerPrice");
		flowerPrice = 30;
		updatePrice();
	}

	var FlowersLayer = document.getElementById("FlowersLayer");
	FlowersLayer.onclick = function(){
		priceSelect("FlowersLayerPrice");
		priceDeselect("FlowersSmallPrice");
		flowerPrice = 50;
		updatePrice();
	}

	/* fabric options */
	if (!document.getElementById("FabricNo")) return false;
	if (!document.getElementById("FabricYes")) return false;
	if (!document.getElementById("FabricPrice")) return false;

	var FabricNo = document.getElementById("FabricNo");
	FabricNo.onclick = function(){
		priceDeselect("FabricPrice");
		fabricPrice = 0;
		updatePrice();
	}
	
	var FabricYes = document.getElementById("FabricYes");
	FabricYes.onclick = function(){
		priceSelect("FabricPrice");
		fabricPrice = 25;
		updatePrice();
	}
	
	/* memorabilia options */
	if (!document.getElementById("MemorabiliaNo")) return false;
	if (!document.getElementById("MemorabiliaYes")) return false;
	if (!document.getElementById("MorePrice")) return false;

	var MemorabiliaNo = document.getElementById("MemorabiliaNo");
	MemorabiliaNo.onclick = function(){
		priceDeselect("MorePrice");
		morePrice = 0;
		updatePrice();
	}
	
	var MemorabiliaYes = document.getElementById("MemorabiliaYes");
	MemorabiliaYes.onclick = function(){
		priceSelect("MorePrice");
		morePrice = 35;
		updatePrice();
	}

	/* shipping options */
	if (!document.getElementById("ShippingUSPS")) return false;
	if (!document.getElementById("ShippingInsurance")) return false;
	if (!document.getElementById("ShippingOther")) return false;
	if (!document.getElementById("USPSPrice")) return false;
	if (!document.getElementById("USPSPriceBig")) return false;
	if (!document.getElementById("ShippingOtherPrice")) return false;

	var ShippingUSPS = document.getElementById("ShippingUSPS");
	ShippingUSPS.onclick = function(){
		priceDeselect("ShippingOtherPrice");
		priceDeselect("USPSPrice");
		priceDeselect("USPSPriceBig");
		document.getElementById("ShippingInsurance").disabled=false;
		if (boxPrice==80) {
			shippingPrice = 8;
			priceSelect("USPSPrice");
		} else if (boxPrice==100) {
			shippingPrice = 18;
			priceSelect("USPSPriceBig");
		}
		updatePrice();
	}
	
	var ShippingInsurance = document.getElementById("ShippingInsurance");
	ShippingInsurance.onclick = function(){
		if (ShippingInsurance.checked==true){
			priceSelect("USPSPriceInsurance");
			insurancePrice = 3;
			updatePrice();
		} else {
			priceDeselect("USPSPriceInsurance");
			insurancePrice = 0;
			updatePrice();
		}
	}

	var ShippingOther = document.getElementById("ShippingOther");
	ShippingOther.onclick = function(){
		priceDeselect("USPSPrice");
		priceDeselect("USPSPriceBig");
		priceDeselect("USPSPriceInsurance");
		priceSelect("ShippingOtherPrice");
		document.getElementById("ShippingInsurance").checked=false;
		document.getElementById("ShippingInsurance").disabled=true;
		insurancePrice = 0;
		shippingPrice = 8;
		updatePrice();
	}
	
	/* gift options */
	if (!document.getElementById("GiftNo")) return false;
	if (!document.getElementById("GiftYes")) return false;
	if (!document.getElementById("GiftOptions")) return false;
	
	var GiftNo = document.getElementById("GiftNo");
	GiftNo.onclick = function(){
		hide("GiftOptions");
	}
	
	var GiftYes = document.getElementById("GiftYes");
	GiftYes.onclick = function(){
		show("GiftOptions");
	}
}

function prepareTotal(){
	if (!document.createElement) return false;
	if (!document.createTextNode) return false;
	if (!document.getElementById) return false;
	
	if (!document.getElementById("totalpricecell")) return false;
	if (!document.getElementById("totalnote")) return false;
	if (!document.getElementById("EstimatedTotal")) return false;
	if (notCustom == null) return false;
	
	//shows the estimated total note
	notepara = document.getElementById("totalnote");
	notepara.firstChild.nodeValue = "The actual cost of this custom shadow box may vary from the estimate if you have custom requests. You'll receive an email with the final quote. Also, frame types and other materials are subject to availability. I'll let you know if something is unavailable. Residents of California will be charged sales tax.";
	
	//shows the initial estimated total
	var para = document.createElement("p");
	if(!notCustom){
		var t1 = document.createTextNode("Starting at ");
		para.appendChild(t1);
	}
		var price = document.createElement("span");
	price.className = "pricetotal";
	price.setAttribute("id","totalprice");
	var pricestring = "$0.00";
	var t2 = document.createTextNode(pricestring);
	price.appendChild(t2);
	para.appendChild(price);
		
	pricecell = document.getElementById("totalpricecell");
	
	pricecell.appendChild(para);

	//sets the form input total
	var forminput = document.getElementById("EstimatedTotal");
	forminput.value = price;
}

addLoadEvent(setup);
addLoadEvent(formclicks);
addLoadEvent(prepareTotal);
