
	
	function getCatCost(btnName) {
	
		var btn = window.document.comp_configure[btnName];
		
		//if this length is null then there is only one radio button in the group
		if (btn.length == null) {
			if (btn.checked) {
				//alert('CAT: ' + btnName + ' VAL: ' +btn.value);
				if (btnName == "processors") {
					cost = parseFloat(btn.value.substring(6));
					max_cpu = document.getElementById('max_cpu').value;
					
					if (max_cpu == 1)
							var qty = 1;
					else {
						//work out the qty for the cpu/ram
						var qty_name = btnName + 'qty';
						var qty_select = document.comp_configure[qty_name];
						var qty = parseFloat(qty_select[qty_select.selectedIndex].value);
					}
					return parseFloat(cost*qty);
				}
				else if (btnName == "monitors" || btnName =="case_fans") {
					cost = parseFloat(btn.value.substring(6));
					var qty_name = btnName + 'qty';
					var qty_select = document.comp_configure[qty_name];
					var qty = parseFloat(qty_select[qty_select.selectedIndex].value);
					return parseFloat(cost*qty);
				}
				else {
					return  parseFloat(btn.value.substring(6));
				}
			}
			else {
				//alert('CAT: ' + btnName + ' VAL: ' +btn.value);
				return 0;
			}
		}
		else {
			for (k=0; k < btn.length; k++) {
				if (btn[k].checked)
				{
					if (btnName == "processors") {
						cost = parseFloat(btn[k].value.substring(6));
						max_cpu = document.getElementById('max_cpu').value;
						
						if (max_cpu == 1)
							var qty = 1;
						else {
							//work out the qty for the cpu/ram
							var qty_name = btnName + 'qty';
							var qty_select = document.comp_configure[qty_name];
							var qty = parseFloat(qty_select[qty_select.selectedIndex].value);
						}
						return parseFloat(cost*qty);
						//alert('TOTAL ' +subtotal);
					}
					else if (btnName == "monitors" || btnName=="case_fans") {
						cost = parseFloat(btn[k].value.substring(6));
						var qty_name = btnName + 'qty';
						var qty_select = document.comp_configure[qty_name];
						var qty = parseFloat(qty_select[qty_select.selectedIndex].value);
						return parseFloat(cost*qty);
					}
					else {
						return parseFloat(btn[k].value.substring(6));
					}
				}
			}
			return 0;
		}
	
	}
	
	function resetCat(btnName)
	{
		var btn = window.document.comp_configure[btnName];
	
		if (btn.length == null)
		{
			if(btn.defaultChecked) {
					if (btn.checked == false) {
						btn.checked = true;
					}
			}
		}
		else {
			for (j=0;j<btn.length;j++)
			{
				if (btn[j].defaultChecked) {
					if (btn[j].checked == false) {
						btn[j].checked=true;
					}
				}
				
			}
		}
		
	}
	
	function get_rental_rate(total) {
		for (var i in flexi_max_array) {
			var min_amt = parseFloat(i);
			if (total <= min_amt) {
					return parseFloat(flexi_max_array[i]);
			}
		}
		
	}
	
	function updatePrice (frm, btnName) {
		var btn = frm[btnName];
			
		//get the base cost first	
		for (i=0; i< btn.length; i++)
		{
			if (btn[i].checked) {
				var val = btn[i].value;
				var base_cost = val.substring(6);
				var base_sku = val.substring(0,6);
				//alert(base_sku);
			}
				
		}
		// go through the list again
		for (i=0; i< btn.length; i++)
		{
			diff = parseFloat(base_cost) - parseFloat(btn[i].value.substring(6));
			sku = btn[i].value.substring(0,6);
			if (sku == "000000") {
				var div_name = btnName + 'div';
			}
			else {
				var div_name = btnName+sku;
			}
			
			if (btnName == 'memory') {
				unit_cost = parseFloat(btn[i].value.substring(6));
				txt = '['+unit_cost+' per unit]';
			}			
			else if (diff == 0) {
				txt = '';
			}
			else if (diff < 0) {
				diff = diff*-1;
				txt = '[add '+diff+']';
			}
			else {
				txt = '[sub '+diff+']';
			}
			window.document.getElementById(div_name).innerHTML=txt;
				
		}
		//check if the btnName is base_unit, if so update the base unit info
		if (btnName=='base_unit') {
			if(base_sku!=null)
			{
				url = 'comp.edit.php?cmd=gbud&sku='+base_sku;
				$.get(url, function(j) {
					$("#base_unit_details").html(j);
				});
			}
		}
		//now we calc the new price
		calcPrice(frm);
		
	
	}
	

	
	function checkCat(frm)
	{
		var total = 0;
		
		for (i=0; i< cat_array.length; i++)
		{
			//var cat_name = cat_array[i];
			//alert('Total: ' +total + ' i='+i + ' Name: ' + cat_name);
			total += parseFloat(getCatCost(cat_array[i]));
		} 
		return total;
	
	}
	
	function resetSys()
	{
		var total = 0;
		
		for (i=0; i< cat_array.length; i++)
		{
			//var cat_name = cat_array[i];
			resetCat(cat_array[i]);
			
		} 
		return total;
	
	}
	
	function resetForm()
	{
		resetSys();
		resetQty();
		resetHDD();
		resetCheckBox('optical[]');
		resetCheckBox('software[]');
		calcPrice(document.comp_configure);
	}
	
	function resetQty()
	{
		//var def_mem_qty = document.comp_configure['default_mem_qty'].value;
		//var mem_qty = document.comp_configure['memoryqty'];
		var max_cpu = document.comp_configure['max_cpu'].value;
		
		//mem_qty.selectedIndex = def_mem_qty -1;
		
		if (max_cpu > 1) {
			var def_cpu_qty = document.comp_configure['default_cpu_qty'].value;
			var cpu_qty = document.comp_configure['processorsqty'];
			cpu_qty.selectedIndex = def_cpu_qty -1;
			
		}
		
	}
	
	//function to calc price
	function calcPrice(frm)
	{
		var subtotal = checkCat(frm);
		var base_unit = getCatCost('base_unit');
		subtotal += base_unit;
		
		//now we add in all the checkbox categories
		subtotal += getCheckBox('software[]');
		subtotal += getCheckBox('optical[]');
		subtotal += getHddTotal();
		document.getElementById('total_price').innerHTML='$' + subtotal+' AUD';
		document.getElementById('total_price_btm').innerHTML='$' + subtotal+' AUD';
		
		var rental = get_rental_rate(subtotal)/100;
		wkly_rental = (subtotal*rental*12)/52;
		show_amt = roundOff(wkly_rental, 2);
		document.getElementById('flexirent').innerHTML=' '+show_amt+' per week with Flexirent*';
		
	}
	
	function resetHDD()
	{
		chkbox = document.comp_configure['hdd[]'];
		
		//no hard drive options so return
		if (chkbox == null)
			return;
		if (chkbox.length == null)	{
			if (chkbox.defaultChecked) {
				var sku = chkbox.value.substring(0,6);
				var def_name = 'def_'+sku+'qty';
				var select_name = sku +'qty';
				document.comp_configure[select_name].selectedIndex = def_qty -1;
				if (chkbox.checked == false) {
					//this is a default hdd so get the default qty
					chkbox.checked = true;
					
					
				}
			}
		}
		for (j=0; j<chkbox.length; j++) {
			if (chkbox[j].defaultChecked) {
				var sku = chkbox[j].value.substring(0,6);
				var def_name = 'def_'+sku+'qty';
				var def_qty = document.getElementById(def_name).value;
				var select_name = sku +'qty';
				document.comp_configure[select_name].selectedIndex = def_qty -1;
				
				if(chkbox[j].checked == false) {
					chkbox[j].checked = true;
				}
			}
			else {
				chkbox[j].checked = false;
			}
			
		}
	}

	
	function getHddTotal()
	{
		chkbox = document.comp_configure['hdd[]'];
		max_hdd = document.comp_configure['max_hdd'].value;
		total = 0;
		used = 0;
		
		if (chkbox.length == null) {
			//there is only one hard drive option
			var sku = chkbox.value.substring(0,6);
			var div_name = sku + 'div';
			
			if (chkbox.checked) {
				document.getElementById(div_name).style.fontWeight = 'bold';
				var cost = chkbox.value.substring(6);
				qty_name = sku + 'qty';
				hdd_qty = document.comp_configure[qty_name];
				this_qty = parseFloat(hdd_qty[hdd_qty.selectedIndex].value);
				total = parseFloat(cost*this_qty);
			}
			else {
				document.getElementById(div_name).style.fontWeight = 'normal';
				return 0;
			}
		}
		
		for (l=0; l< chkbox.length; l++)
		{
			var sku = chkbox[l].value.substring(0,6);
			var div_name = sku + 'div';
			if (chkbox[l].checked) {
				document.getElementById(div_name).style.fontWeight = 'bold';
				
				var cost = chkbox[l].value.substring(6);
				qty_name = sku + 'qty';
				hdd_qty = document.comp_configure[qty_name];
				this_qty = parseFloat(hdd_qty[hdd_qty.selectedIndex].value);
				used += this_qty;
				if (used > max_hdd) {
					
					chkbox[l].checked=false;
					document.getElementById(div_name).style.fontWeight = 'normal';

					used -= this_qty;
					alert('No drive bays are available, please reduce the number of hard drives');
				}
				else {
					total += parseFloat(hdd_qty[hdd_qty.selectedIndex].value)*parseFloat(cost);
				}
			}
			else {
				document.getElementById(div_name).style.fontWeight = 'normal';
			}
		}
		//calc available drive bays
		available = max_hdd - used;
		if (available == 0) {
			document.getElementById('hdd_bays').innerHTML='<span class="warning_red">You have no more hard drive bays ' + available + ' </span>';
		}
		else {
			document.getElementById('hdd_bays').innerHTML='Available hard drive bays remaining: ' + available;
		}
		return total;
		
	}
	
	function resetCheckBox(name)
	{
		chkbox = document.comp_configure[name];
		if (chkbox == null)
			return;
		
		if (chkbox.length == null) {
			if (chkbox.defaultChecked) {
				if (chkbox.checked == false) {
					chkbox.checked = true;
				}
			}
		}
		else {
			for (j=0; j<chkbox.length; j++) {
				if (chkbox[j].defaultChecked) {
					if (chkbox[j].checked == false)
						chkbox[j].checked = true;
				}
				else {
					chkbox[j].checked = false;
				}
				
			}
			
		}
	}
	
	function getCheckBox(name)
	{
		chkbox = document.comp_configure[name];
		
		if (chkbox == null)
			return 0;
		
		total = 0;
		
		if (chkbox.length == null) {
			//there is only one item in the group
			if (chkbox.checked)
				return parseFloat(chkbox.value.substring(6));
			else
				return 0;
		}
		else 
		{
			for (j=0; j< chkbox.length; j++)
			{
				if (chkbox[j].checked) {
					//user has ticked this item
					total += parseFloat(chkbox[j].value.substring(6));
				}
			}
			return total;
		}
	}

	function roundOff(value, precision)
	{
			value = "" + value //convert value to string
			precision = parseInt(precision);
	
			var whole = "" + Math.round(value * Math.pow(10, precision));
	
			var decPoint = whole.length - precision;
	
			if(decPoint != 0)
			{
				result = whole.substring(0, decPoint);
				result += ".";
				result += whole.substring(decPoint, whole.length);
			}
			else
			{
				result = whole;
			}
			return result;
	}