var currency = 'GBP';	// currency which the php page defaults to

function ajaxFunction(id, urlstring) {
	var xmlHttp;
	try { // Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch(e) { // Internet Explorer 6.0+
		try {
			xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) { // Internet Explorer 5.5+
			try {
				xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e) { // Unsupported
				alert('Your browser does not support AJAX!');
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			var responseText = xmlHttp.responseText;
			document.getElementById(id).innerHTML = responseText;
			document.getElementById(id).value = responseText;
			updateTotalPrice();
		}
	}
	xmlHttp.open('get', '/content/products/webshopAJAX.php?' + urlstring , true);
	xmlHttp.send(null);
}

function changeQuantity(field) {
	var product = field.id.split('-')[0];

	field.value = validateQty(field.value);
	updateTotalQty();

	var productPrice = document.getElementById(product + '-price');
	if(field.value == '') {
		productPrice.innerHTML = '0.00';
		productPrice.value = 0;
		updateTotalPrice();
	} else {
		productPrice.innerHTML = 'pending...';
		document.getElementById('total-price').innerHTML = '<b>pending...</b>';
		ajaxFunction(product + '-price', 'action=getPrice&product=' + product + '&quantity=' + field.value + '&currency=' + currency);
	}
}

function changeCurrency(select) {
	currency = select[select.selectedIndex].text;
	for(var i = 0; i < products.length; i++) {
		if(document.getElementById(products[i] + '-qty').value != '') {
			document.getElementById('total-price').innerHTML = '<b>pending...</b>';
			document.getElementById(products[i] + '-price').innerHTML = 'pending...';
			ajaxFunction(products[i] + '-price', 'action=getPrice&product=' + products[i] + '&quantity=' + document.getElementById(products[i] + '-qty').value + '&currency=' + currency);
		}
	}
}

function validateQty(value) {
	if(!isNaN(value)) {
		if(value == null || value == 0) {
			value = '';
		} else if(value < 0 || value > 20) {
			value = validateQty(prompt(value + ' is an invalid number, please enter a number between 0 and 20: '));
		}
	} else {
		value = '';
	}
	return value;
}

function updateTotalQty() {
	var totalQty = 0;
	for(var i = 0; i < products.length; i++) {
		if(document.getElementById(products[i] + '-qty').value != '') {
			totalQty += parseInt(document.getElementById(products[i] + '-qty').value);
		}
	}
	document.getElementById('total-qty').innerHTML = '<b>' + totalQty + '</b>';
}

function updateTotalPrice() {
	var totalPrice = 0;
	var i = 0;
	while(i < products.length && totalPrice != 'pending...') {
		var productPrice = document.getElementById(products[i] + '-price');
		if(productPrice.innerHTML == 'pending...') {
			totalPrice = -1;
			i = products.length;
		} else if(productPrice.innerHTML != '') {
			totalPrice += parseFloat(productPrice.innerHTML);
		}
		i++;
	}

	if(totalPrice == -1) {
		document.getElementById('total-price').innerHTML = '<b>pending...</b>';
	} else {
		document.getElementById('total-price').innerHTML = '<b>$' + totalPrice.toFixed(2) + '</b>';
	}
}
