//global variable
var prices = new Array();

function addOptionHandler(){
	if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("optArea")) return false;
  if (!document.getElementById("totalPrice")) return false;

  var oa = document.getElementById("optArea");
  var selectOpts = oa.getElementsByTagName("select");
  
  var debug = "";
  
  if(selectOpts){
  	for (var y=0; y<selectOpts.length; y++) {
		  selectOpts[y].onchange = function(){
				//Option label, do nothing
				if(this.value == "-1") { return false; }
				
				//Set: passID, optName, pricing
				var passID = this.getAttribute("id").substring(3);
				var optName = this.options[this.selectedIndex].firstChild.nodeValue;
				var pricing = this.options[this.selectedIndex].getAttribute("name");
				
				//debug = debug + "passID: " + passID + "optName: " + optName + "pricing: " + pricing +  "<br />";
				
				if(this.selectedIndex == 0){
					optName = "";
					pricing = 0;
				}
				//alert('id: '+passID+' name: '+optName+' price: '+pricing);
				//updateChosenArea(passID, optName, pricing);
				updatePriceTotal(passID, pricing);
		  }
		}
	}
}

function updateChosenArea(passID, optName, pricing){
	if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("optArea")) return false;  
  if (!document.getElementById("totalPrice")) return false;
  
	var dis = document.getElementById("choice"+passID);
	
	//document.write("choice"+passID);
	
	if(dis){
		if(dis.firstChild.nodeType == 3){
				// commented out as do not want to show totals on this shop
				dis.firstChild.nodeValue = optName + " + " + "\u00A3" + pricing;
		}
	}
	document.write(dis.getAttribute("id"));
}
function updatePriceTotal(passID, pricing){
	if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("totalPrice")) return false;
  if (!document.getElementById("the_product_price")) return false;
  
  var tpp = document.getElementById("the_product_price");
	prices[passID] = parseFloat(pricing);
	
	//document.write(pricing);
	var total = 0;
	for (var y=0; y<prices.length; y++) {
		if(prices[y]){
			total = total + prices[y];
		}
	}
	//add options' prices and product's price all together
	//NOTE: pound sign occupies one character only, need to change below for other currencies
	total = total + parseFloat(tpp.getAttribute("value"));
	
	//document.write(total);
	
	var tp = document.getElementById("totalPrice");
	if(tp.firstChild.nodeType == 3){
		// first line commented out as do not want to show the total
		tp.firstChild.nodeValue = "\u00A3" + total;
		//tp.firstChild.nodeValue = "\u00A3" + ;
		//tp.firstChild.nodeValue = "\u00A3" + tpp.getAttribute("value");
	}
}

function checkOptionSelection(){
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
    if (!document.getElementById("optArea")) return false;
    if (!document.getElementById("totalPrice")) return false;

  var oa = document.getElementById("optArea");
  var selectOpts = oa.getElementsByTagName("select");
  
  if(selectOpts){
  	for (var y=0; y<selectOpts.length; y++) {
  		if(selectOpts[y].selectedIndex == 0){
  			alert("Please select product option(s)");
  			return false;
  		}
		}
	}
	return true;
}
addLoadEvent(addOptionHandler);