var $l = function(id) {
  return document.getElementById(id);
};

var selectedRadio = "3 Inch Curtain Tape";

function setSelectedRadio(rad, ap,productID,taxHandling) {
  selectedRadio = rad;
  calculatePrice(ap,productID,taxHandling);
}

function getHeadingPrice(width) {
  if (selectedRadio == '3 Inch Curtain Tape') {
	 checkRadioOptions(1);
    return 0;
  }
  
  if (selectedRadio.indexOf('Goblet Pleats') != -1) {
	width <= 115 ? 	checkRadioOptions(2) : 	checkRadioOptions(3);
    return width <= 115 ? 15 : 25;
  }
  
  if (selectedRadio.indexOf('Eyelets') != -1) {
	width <= 115 ? 	checkRadioOptions(4) : 	checkRadioOptions(5);	  
    return width <= 115 ? 10 : 20;
  }
}

function calculatePrice(areaPrice,productID,taxHandling) {
  var taxRate = taxHandling == "Inclusive" ? 1.15 : 1;
  var widthBox = $l('CurtainWidth');
  var lengthBox = $l('CurtainLength');
  if (widthBox && lengthBox) {
    var area = widthBox.value * lengthBox.value;
	var p = ((area * areaPrice * taxRate) * 100) / 100;
	var qtyID = "Q_" + productID;
	var qtyElem = $l(qtyID);
	
	var headingPrice = getHeadingPrice(widthBox.value);
	p += ((headingPrice * taxRate) * 100) / 100;
	
	p = Math.round(p * 100) / 100;
	p = p.toString();

    if (p.indexOf(".") == -1) p += ".00";
    else if (p.length == 3 && p.indexOf(".") != -1) p += "0";
	if (p.indexOf(".") == p.length - 2) p += "0";

	if (isNaN(p)) {
	  $l('GenPrice').innerHTML = "Invalid number entered";
	  qtyElem.value = 0;
	}
	else {
	  $l('GenPrice').innerHTML = "£" + p;
	  qtyElem.value = area;
	}
  }
}

function checkRadioOptions(headingOption) {
	var rb = document.form.addonRadio;
	var len = rb.length;
	
	for (i = 0;i<len;i++) {
		if (parseInt(rb[i].value) == parseInt(headingOption)) {
			rb[i].checked = true;
		}
	}
	
}
function checkCurtainOptions() {
  
}
