function calculateText(qtyElement,index) {
	
	getValues(index);
	if (qtyElement.value < minqty) {
		alert('Minimum quantity is '+minqty);
		qtyElement.value = minqty;
	}
	quantity = qtyElement.value;
	calculateQuotation(qtyElement,quantity,initial,monthly,annual);
}

function calculateCheckbox(qtyElement,index) {
	getValues(index);
	quantity = 0;
	if (qtyElement.checked) quantity = 1;
	calculateQuotation(qtyElement,quantity,initial,monthly,annual);
}

function changeOption (selectElement,group) {
	qtyElement = document.getElementById("qty"+selectElement.name);
	index = selectElement.options[selectElement.selectedIndex].value;
	calculateText(qtyElement,index);
}

function calculateSelect(qtyElement,componentGroup) {
	selectElement = document.getElementById("sel"+componentGroup);
	index = selectElement.options[selectElement.selectedIndex].value;
	calculateText(qtyElement,index);
}

function getValues(index) {
	minqty = minqtyArray[index];
	initial = initialArray[index];
	monthly = monthlyArray[index];
	elem = document.getElementById("contractPeriod");
	contractPeriod = elem.selectedIndex;
	surcharge = 1 + (contractPeriod / 10);
	monthly *= surcharge;
	annual = annualArray[index];
}

function calculateQuotation(qtyElement,quantity,initial,monthly,annual) {	
	rowIndex = qtyElement.name.substr(10,13);
	totalElement = document.getElementById('initialTotal');
	totalValue = totalElement.value;
	chargeElement = document.getElementById('initial'+rowIndex);
	chargeValue = chargeElement.value;
	totalValue -= chargeValue;
	chargeValue = quantity*initial;
	totalValue += chargeValue;
	chargeElement.value = chargeValue.toFixed(2);
	totalElement.value = totalValue.toFixed(2);
	
	totalElement = document.getElementById('monthlyTotal');
	totalValue = totalElement.value;
	chargeElement = document.getElementById('monthly'+rowIndex);
	chargeValue = chargeElement.value;
	totalValue -= chargeValue;
	chargeValue = quantity*monthly;
	totalValue += chargeValue;
	chargeElement.value = chargeValue.toFixed(2);
	totalElement.value = totalValue.toFixed(2);
	
	totalElement = document.getElementById('annualTotal');
	totalValue = totalElement.value;
	chargeElement = document.getElementById('annual'+rowIndex);
	chargeValue = chargeElement.value;
	totalValue -= chargeValue;
	chargeValue = quantity*annual;
	totalValue += chargeValue;
	chargeElement.value = chargeValue.toFixed(2);
	totalElement.value = totalValue.toFixed(2);
}