function clearBox(box) {
	 if(box.value == box.defaultValue) {
	 	 box.value = "";
	 }
}

sfHover = function() {
	var sfEls = document.getElementById("main-nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function getValue(id) {
    var select = document.getElementById(id);
    var option = select.options[select.selectedIndex];
    var price = option.value.split(', ')[1];
	var input = document.getElementById(id + '_quantity');
	if(input) {
		var quantity = document.getElementById(id + '_quantity').value;
	}
	else {
		var quantity = 1;
	}
	var totalprice = price * quantity;
	
    document.getElementById(id + '_price_p').value = totalprice.toFixed(2);
	var originalprice = document.getElementById(id + '_base_price').value;
	var partsgp = document.getElementById('parts_gp').value;
	var difference = (totalprice - originalprice);
	var diffmarkedup = difference / (1 - (partsgp / 100));
	document.getElementById(id + '_diff_price').value = diffmarkedup.toFixed(2);
	updateTotal();
}

function getOriginalValue(id) {
	if(document.getElementById(id)) {
		var select = document.getElementById(id);
		var option = select.options[select.selectedIndex];
		var price = option.value.split(', ')[1];
		var input = document.getElementById(id + '_quantity');
		if(input) {
			var quantity = document.getElementById(id + '_quantity').value;
		}
		else {
			var quantity = 1;
		}
		var totalprice = price * quantity;
		if(!totalprice) {
			totalprice = 0.00
		};
		document.getElementById(id + '_price_p').value = totalprice.toFixed(2);
		document.getElementById(id + '_base_price').value = totalprice.toFixed(2);
		updateTotal();
	}
}

function getValueSoft(id) {
    var select = document.getElementById(id);
    var option = select.options[select.selectedIndex];
    var price = option.value.split(', ')[1];
	var input = document.getElementById(id + '_quantity');
	if(input) {
		var quantity = document.getElementById(id + '_quantity').value;
	}
	else {
		var quantity = 1;
	}
	var totalprice = price * quantity;
	
    document.getElementById(id + '_price_s').value = totalprice.toFixed(2);
	var originalprice = document.getElementById(id + '_base_price').value;
	var difference = totalprice - originalprice;
	document.getElementById(id + '_diff_price').value = difference.toFixed(2);
	updateTotal();
}

function getOriginalValueSoft(id) {
	if(document.getElementById(id)) {
		var select = document.getElementById(id);
		var option = select.options[select.selectedIndex];
		var price = option.value.split(', ')[1];
		var input = document.getElementById(id + '_quantity');
		if(input) {
			var quantity = document.getElementById(id + '_quantity').value;
		}
		else {
			var quantity = 1;
		}
		var totalprice = price * quantity;
		document.getElementById(id + '_price_s').value = totalprice.toFixed(2);
		document.getElementById(id + '_base_price').value = totalprice.toFixed(2);
		updateTotal();
	}
}

function updateTotal(){
	if(document.getElementById('core_system_builder')) {
		var inputs = document.getElementById('core_system_builder').getElementsByTagName("input");
		var parts = 0.00;
		for (var i = 0; i < inputs.length; i++) {
			var curID = inputs[i].id;
			parts += (parseFloat(inputs[i].value) || 0) * (curID.substr(curID.length-8) == "_price_p" ? 1 : 0);
		}
		document.getElementById('parts_total').value = parts.toFixed(2); // Drops parts total in to input box
		var soft = 0.00;
		for(var i = 0; i < inputs.length; i++) {
			var curID = inputs[i].id;
			soft += (parseFloat(inputs[i].value) || 0) * (curID.substr(curID.length-8) == "_price_s" ? 1 : 0);
		document.getElementById('soft_total').value = soft.toFixed(2); // Drops software total in to input box
		}
		var costtotal = 0.00;
			costtotal = parseFloat(parts) + parseFloat(soft);
		var costvat = parseFloat(costtotal * 1.175);
		var partsgp = document.getElementById('parts_gp').value; // Gets parts markup
		var softgp = document.getElementById('soft_gp').value; // Gets software markup
		var partsmarkup = parts / (1 - (partsgp / 100)); //  Works markup from 25 to 1.25 and multiplies parts total
		var softwaremarkup = soft / (1 - (softgp / 100)); //  Works markup from 25 to 1.25 and multiplies parts total
		var subtotal = (partsmarkup + softwaremarkup);
		var vat = (subtotal  * 1.175) - subtotal; // Calculates VAT
		var total = subtotal * 1.175; // Calculates total
		var grossexvat = 0.00;
			grossexvat = parseFloat(subtotal) - parseFloat(costtotal);
		var gross = 0.00;
			gross = parseFloat(total) - parseFloat(costvat);
		var retailexvat = document.getElementById('retailexvat').value;
		var retail = parseFloat(retailexvat * 1.175);
		document.getElementById('costexvat').value = costtotal.toFixed(2); // appends costprice
		document.getElementById('cost').value = costvat.toFixed(2);
		document.getElementById('grossexvat').value = grossexvat.toFixed(2);
		document.getElementById('gross').value = gross.toFixed(2);
		document.getElementById('totalexvat').value = subtotal.toFixed(2); // Appends VAT
		document.getElementById('total').value = total.toFixed(2); // Appends Total Value
		document.getElementById('retail').value = retail.toFixed(2); // Appends Total Value
	}
}

function recalculateOrder() {
	var systemTotal = document.getElementById('system_total').value;
	var monitor = document.getElementById('monitors');
    var monitorOption = monitor.options[monitor.selectedIndex];
    var monitorPrice = monitorOption.value.split(', ')[1];
	var keyboard = document.getElementById('keyboards_mice');
    var keyboardOption = keyboard.options[keyboard.selectedIndex];
    var keyboardPrice = keyboardOption.value.split(', ')[1];
	var speakers = document.getElementById('keyboards_mice');
    var speakersOption = speakers.options[speakers.selectedIndex];
    var speakersPrice = speakersOption.value.split(', ')[1];
	var totalExcVat = (parseFloat(systemTotal) || 0) + (parseFloat(monitorPrice) || 0) + (parseFloat(keyboardPrice) || 0) + (parseFloat(speakersPrice) || 0);
	document.getElementById('total').value = totalExcVat.toFixed(2);
}

function updateProductPrice() {
	if(document.getElementById('configurator')) {
		var selects = document.getElementById('configurator').getElementsByTagName('select'); // grabs all selects from form #configurator
		var total = parseFloat(document.getElementById('retail').value); // retail exc vat
		for(var i = 0; i < selects.length; i++) {
			var curID = selects[i].id;
			var curSelect = document.getElementById(curID);
			var option = curSelect.options[curSelect.selectedIndex];
			var price = parseFloat(option.value.split(', ')[1]) || 0; // price of current item minus price of default item
			if(curID === 'ram' || curID === 'harddisk3' || curID === 'harddisk2') {
				var quantitySelect = document.getElementById(curID + '_quantity');
				var quantity = quantitySelect.options[quantitySelect.selectedIndex].value;
				quantity = parseInt(quantity);
				var defQuantity = document.getElementById(curID + '_default_quantity').value;
				defQuantity = parseInt(defQuantity);
				if(quantity != defQuantity) {
					quantity -= 1;
					pricemod = option.value.split(', ')[2];
					pricemod = parseFloat(pricemod) || 0;
					if(quantity > 0) {
						pricemod *= quantity;
					}
					else {
						quantity = -1;
						pricemod *= quantity;
					}
					price += pricemod;
				}
				else {
					price *= quantity;
				}
			}
			total += price;
		};
		total = total.toFixed(2);
		var vattotal = total * 1.175;
		vattotal = vattotal.toFixed(2);
		document.getElementById('exvat').innerHTML = total;
		document.getElementById('bottom-exvat').innerHTML = total;
		document.getElementById('incvat').innerHTML = vattotal;
		document.getElementById('bottom-incvat').innerHTML = vattotal;
		document.getElementById('totalexvat').value = total;
	}
}

function checkForFixed() {
	var select = document.getElementById('time');
    var option = select.options[select.selectedIndex];
    var value = option.value;
	if(value == "fixed") {
		document.getElementById('fixed').style.visibility="visible";
	}
	else {
		document.getElementById('fixed').style.visibility="hidden";
	}
}


$(document).ready(function() {
						   
	if($('#info').length > 0) {
		$('#info table tr:even').addClass('alt');
		$('#info table tr td:first-child').addClass('heading');
	}
	
	var latestHeight = $('#latest').height();
	$('#news').css('min-height', latestHeight);
	
	if($("#admin-tabs").length > 0) {
		$(function() {
			$("#admin-tabs").tabs();
		})
	};
	$('select#harddisk3').change(function() {
		var id = $(this).val();
		if(id != null) {
			$.get('/includes/ajax.php?action=getdiskinfo&disk=' + id, function(xml){
				
				$(xml).find("disk").each(function(index) {
					var rpm = $(this).find("rpm").attr('value');
					var cache = $(this).find("cache").attr("value");
					$('#hdd3rpm').text(rpm);
					$('#hdd3cache').text(cache);
				});
			});
		};
	});
	$('select#harddisk2').change(function() {
		var id = $(this).val();
		if(id != null) {
			$.get('/includes/ajax.php?action=getdiskinfo&disk=' + id, function(xml){
				
				$(xml).find("disk").each(function(index) {
					var rpm = $(this).find("rpm").attr('value');
					var cache = $(this).find("cache").attr("value");
					$('#hdd2rpm').text(rpm);
					$('#hdd2cache').text(cache);
				});
			});
		};
	});
	$('select#gpu').change(function() {
		var id = $(this).val();
		if(id != null) {
			$.get('/includes/ajax.php?action=getgpuinfo&gpu=' + id, function(xml){
				
				$(xml).find("gpu").each(function(index) {
					var ram = $(this).find("ram").attr('value');
					$('#gpuram').text(ram);
				});
			});
		};
	});
	var mBoard = $('#mboard');
	$('select#case').change(function() {
		var id = $(this).val();
		if(id != null) {
			$.get('/admin/includes/ajax.php?action=getMotherboards&case=' + id, function(xml){
				mBoard.find("option").each(function(index) {
					$(this).remove();
				 });
				$(xml).find('motherboard').each(function(index) {
					var mBoardId = $(this).find("id").attr('value');
					var mBoardName = $(this).find("name").attr('value');
					$('<option></option>')
					.attr('value', mBoardId)
					.text(mBoardName)
					.appendTo(mBoard);
				});
			});
		};
	});
	$('a[rel*="external"]').click(function() { 
		window.open($(this).attr('href')); 
		return false; 
	});
	
	$('li.fmqli input').keyup(function() {
		if($(this).attr('maxlength' ) == $(this).val().length) {
			$(this).next('input').focus(); // move to next input element
		}
	});
	if($('#warrantyform').length > 0) {
		//$('#warrantyform').hide();
		$('#warrantyform').validate();
	}
	
	if($('#warrantyform2').length > 0) {
		$('#warrantyform2').hide();
		$('#warrantyform2').validate();
		$('#warrantytype').change(function() {
			$('#warrantyform2').hide();							   
			if($(this).val() == '5 Year RTB Warranty') {
				$('#warrantyform2').show('slow');
			}
			else if($(this).val() == 'Choose') {
			// do nothing
			}
			else {
				alert('foo');
			}
		});
	}
});


