function readMore(id) {
	document.getElementById('productDescription_'+id).style.height = 'auto';
	document.getElementById('productDescriptionLink_'+id).innerHTML = '<a href="javascript:readLess(\''+id+'\');">Read less</a>';
}

function readLess(id) {
	document.getElementById('productDescription_'+id).style.height = '135px';
	document.getElementById('productDescriptionLink_'+id).innerHTML = '<a href="javascript:readMore(\''+id+'\');">Read more</a>';
	window.location.hash = 'product_'+id;
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 
}

function validateNewCustomerForm() {
	var errorMsg = '';
	if(!isValidEmail(document.getElementById('newCustomerEmail').value))
		errorMsg = 'Please enter a valid email address\n';
	if(errorMsg != '') {
		alert('Please correct the following errors;\n\n'+errorMsg+' ');
		return false;
	}
	return true;
}

function validateDeliveryForm() {
	var errorMsg = '';
	if(document.getElementById('delivery_fullname').value == '')
		errorMsg = 'Please enter your full name\n';
	if(document.getElementById('delivery_addr1').value == '')
		errorMsg = 'Please enter your address\n';
	if(errorMsg != '') {
		alert('Please correct the following errors;\n\n'+errorMsg+' ');
		return false;
	}
	return true;
}

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
	try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	  try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
		xmlhttp = false;
	  }
	}
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	try {
	  xmlhttp = new XMLHttpRequest();
	} catch (e) {
	  xmlhttp = false;
	}
  }
  return xmlhttp;
}

var http = getHTTPObject();

var isWorking = false;

function qtyCount(id,action) {
	var qtyObj = document.getElementById(id);
	var curVal = qtyObj.innerHTML;
	if(action == 'add') {
		var newVal = parseInt(curVal)+1;
	}
	if(action == 'sub') {
		if(curVal > 1) {
			var newVal = parseInt(curVal)-1;
		}
		else {
			newVal = curVal;
		}
	}
	qtyObj.innerHTML = newVal;
}

function removeBasketItem(id) {
	if(!isWorking && http) {
		document.getElementById('basketItem_'+id).style.display = 'none';

		var itemPrice = parseFloat(document.getElementById('basketItemPrice_'+id).innerHTML);
		var basketSubtotal = parseFloat(document.getElementById('basketSubtotal').innerHTML);

		var newSubTotal = parseFloat(basketSubtotal-itemPrice).toFixed(2);
		var newVatTotal = parseFloat((basketSubtotal-itemPrice)*0.15).toFixed(2);
		var newBasketTotal = parseFloat(4.99+parseFloat(newSubTotal)+parseFloat(newVatTotal)).toFixed(2);

		var newPrice = parseFloat(parseFloat(newSubTotal)+parseFloat(newVatTotal)).toFixed(2);

		document.getElementById('basketSubtotal').innerHTML = newSubTotal;
		document.getElementById('basketVat').innerHTML = newVatTotal;
		document.getElementById('basketTotal').innerHTML = newBasketTotal;

		http.open("GET", "/modules/shop/ajax.php?removeFromBasket=1&id=" + id + "&newPrice=" + newPrice, true);

		isWorking = true;
		http.send(null);
	}
}

function handleUpdateProductOptions2HttpResponse() {
	if(http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			res = http.responseText.split("|TS|");

			id = res[0];
			results = res[1].split("*TS*");
			
			options2SelObj = document.getElementById("productOptions2_"+id);

			var c = 1;
			for(i=0;i<results.length;i+=2) {
				options2SelObj.options[c] = new Option(results[i+1],results[i]);
				c+=1;
			}
			
			options2SelObj.disabled = false;
			
			isWorking = false;
		}
	}
}

function handleUpdateProductOptions3HttpResponse() {
	if(http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			res = http.responseText.split("|TS|");

			id = res[0];
			results = res[1].split("*TS*");
			
			options3SelObj = document.getElementById("productOptions3_"+id);

			var c = 1;
			for(i=0;i<results.length;i+=2) {
				options3SelObj.options[c] = new Option(results[i+1],results[i]);
				c+=1;
			}
			
			options3SelObj.disabled = false;
			
			isWorking = false;
		}
	}
}

function handleUpdateProductPriceHttpResponse() {
	if(http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			results = http.responseText.split("*TS*");

			var priceExVat = results[0];
			var prod_id = results[1];
			var specialPriceExVat = results[2];
			
			var priceExVatObj = document.getElementById("productPriceExVat_" + prod_id);
			var priceIncVatObj = document.getElementById("productPriceIncVat_" + prod_id);

			var priceSpecialExVatObj = document.getElementById("productSpecialPriceExVat_" + prod_id);
			var priceSpecialIncVatObj = document.getElementById("productSpecialPriceIncVat_" + prod_id);
			
			priceExVatObj.innerHTML = parseFloat(priceExVat).toFixed(2);
			priceIncVatObj.innerHTML = (Math.round((parseFloat(priceExVat)+(parseFloat(priceExVat)*0.15))*100)/100).toFixed(2);
			
			priceSpecialExVatObj.innerHTML = parseFloat(specialPriceExVat).toFixed(2);
			priceSpecialIncVatObj.innerHTML = (Math.round((parseFloat(specialPriceExVat)+(parseFloat(specialPriceExVat)*0.15))*100)/100).toFixed(2);
			
			if(specialPriceExVat != 0) {
				document.getElementById("specialOfferPriceDisplay_" + prod_id).style.display = 'block';
				document.getElementById("fullPriceDisplay_" + prod_id).style.display = 'none';
			}
			
			isWorking = false;
		}
	}
}

function handleAddToBasketHttpResponse() {
	if(http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			results = http.responseText.split(",");
			
			document.getElementById('basketNumItems').innerHTML = results[1];
			document.getElementById('basketShippingPrice').innerHTML = '&pound;' + results[3];
			document.getElementById('basketTotalPrice').innerHTML = '&pound;' + results[0];
			
			document.getElementById('qty_' + results[2]).innerHTML = "1";
			
			document.getElementById('itemAddedMsg_' + results[2]).style.display = 'block';
			
			document.getElementById('itemAddedMsg_' + results[2]).innerHTML = "Item has been added to basket";
			
			isWorking = false;
		}
	}
}

function handleCreateAccountHttpResponse() {
	if(http.readyState == 4) {
		if(http.responseText.indexOf('invalid') == -1) {
			result = http.responseText;
			if(result == 'OK') {
				var html = '<p>Your account has been created!</p><p>Your login details have been sent to your email address.</p>';
			}
			else {
				var html = '<p>An error occurred. Your account could not be created.</p>';
			}
			document.getElementById('createAccountDiv').innerHTML = html;
			isWorking = false;
		}
	}
}

function handleShowSpecialOfferPricesHttpResponse() {
	if(http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			results = http.responseText.split("|");
			if(results.length > 1) {
				titles = results[0].split("*TS2*");
				prod_options = results[1].split("*TS3*");
	
				html = '<table cellspacing="0" cellpadding="0" border="1"><tr><th>'+titles[0]+'</th><th>'+titles[1]+'</th><th>'+titles[2]+'</th><th>Full Price (&pound;)</th><th>Offer Price (&pound;)</th></tr>';
				for(i=0;i<prod_options.length;i+=1) {
					opts = prod_options[i].split("*TS1*");
					html += '<tr><td>'+opts[0]+'</td><td>'+opts[1]+'</td><td>'+opts[2]+'</td><td>&pound;'+opts[3]+'</td><td><input style="margin-top:0;width:50px;" type="text" name="option_offer_price['+opts[5]+']" value="'+opts[4]+'" /></td></tr>';
				}
				html += '</table>';
			}
			else {
				document.getElementById('offerDetails').style.textAlign = 'left';
				prices = results[0].split(",");
				html = '<label>Full price (&pound;)</label><input type="text" style="background-color:#fff;border:0;" readonly="readonly" value="' + prices[0] + '" /><br />';
				html += '<label>Offer price (&pound;)</label><input type="text" name="specialOfferPrice" value="' + prices[1] + '" /><br />';
			}
			document.getElementById('offerDetails').innerHTML = html;

			isWorking = false;
		}
	}
}

function addToBasket(id) {
	if(!isWorking && http) {
		var qty = document.getElementById('qty_' + id).innerHTML;
		var price = document.getElementById('productSpecialPriceIncVat_' + id).innerHTML;
		if(price == '0.00')
			price = document.getElementById('productPriceIncVat_' + id).innerHTML;
		if(document.getElementById('productOptions1_'+id))
			var option1 = document.getElementById('productOptions1_'+id).options[document.getElementById('productOptions1_'+id).selectedIndex].value;
		else
			var option1 = '';
		if(document.getElementById('productOptions2_'+id))
			var option2 = document.getElementById('productOptions2_'+id).options[document.getElementById('productOptions2_'+id).selectedIndex].value;
		else
			var option2 = '';
		if(document.getElementById('productOptions3_'+id))
			var option3 = document.getElementById('productOptions3_'+id).options[document.getElementById('productOptions3_'+id).selectedIndex].value;
		else
			var option3 = '';
		if(price != '0.00') {
			http.open("GET", "/modules/shop/ajax.php?addToBasket=1&price=" + price + "&qty=" + qty + "&id=" + id + "&option1=" + option1 + "&option2=" + option2 + "&option3=" + option3, true);
			http.onreadystatechange = handleAddToBasketHttpResponse;
			isWorking = true;
			http.send(null);
		}
	}
}

function removeProductOptions(obj) {
	var delRow = obj.parentNode.parentNode;
	var tbl = delRow.parentNode.parentNode;
	var rIndex = delRow.sectionRowIndex;
	tbl.deleteRow(rIndex);
}

function addProductOptions() {
	
	var tbl = document.getElementById('productOptionsTable');
	var lastRow = tbl.rows.length;
	var iteration = lastRow;
	var row = tbl.insertRow(lastRow);
	
	var cell = row.insertCell(0);
	var el = document.createElement('input');
	el.type = 'text';
	el.name = 'option1_value[]';
	el.style.width = '120px';
	cell.align = 'center';
	cell.appendChild(el);
	
	var cell = row.insertCell(1);
	var el = document.createElement('input');
	el.type = 'text';
	el.name = 'option2_value[]';
	el.style.width = '120px';
	cell.align = 'center';
	cell.appendChild(el);
	
	var cell = row.insertCell(2);
	var el = document.createElement('input');
	el.type = 'text';
	el.name = 'option3_value[]';
	el.style.width = '120px';
	cell.align = 'center';
	cell.appendChild(el);

	var cell = row.insertCell(3);
	var el = document.createElement('input');
	el.type = 'text';
	el.name = 'option_price[]';
	el.style.width = '40px';
	cell.align = 'center';
	cell.appendChild(el);
}

function updateProductOptions2(id) {
	if(!isWorking && http) {
		var option1 = document.getElementById('productOptions1_'+id).options[document.getElementById('productOptions1_'+id).selectedIndex].value;
		if(document.getElementById('productOptions2_'+id)) {
			http.open("GET", "/modules/shop/ajax.php?updateProductOptions2=1&option1=" + option1 + "&prod_id=" + id, true);
			http.onreadystatechange = handleUpdateProductOptions2HttpResponse;
		}
		else {
			http.open("GET", "/modules/shop/ajax.php?updateProductPrice=1&option1=" + option1 + "&prod_id=" + id, true);
			http.onreadystatechange = handleUpdateProductPriceHttpResponse;
		}
		isWorking = true;
		http.send(null);
	}
}

function updateProductOptions3(id) {
	if(!isWorking && http) {
		var option1 = document.getElementById('productOptions1_'+id).options[document.getElementById('productOptions1_'+id).selectedIndex].value;
		var option2 = document.getElementById('productOptions2_'+id).options[document.getElementById('productOptions2_'+id).selectedIndex].value;
		if(document.getElementById('productOptions3_'+id)) {
			http.open("GET", "/modules/shop/ajax.php?updateProductOptions3=1&option1=" + option1 + "&option2=" + option2 + "&prod_id=" + id, true);
			http.onreadystatechange = handleUpdateProductOptions3HttpResponse;
		}
		else {
			http.open("GET", "/modules/shop/ajax.php?updateProductPrice=1&option1=" + option1 + "&option2=" + option2 + "&prod_id=" + id, true);
			http.onreadystatechange = handleUpdateProductPriceHttpResponse;
		}
		isWorking = true;
		http.send(null);
	}
}

function updateProductPrice(id) {
	if(!isWorking && http) {
		var option1 = document.getElementById('productOptions1_'+id).options[document.getElementById('productOptions1_'+id).selectedIndex].value;
		var option2 = document.getElementById('productOptions2_'+id).options[document.getElementById('productOptions2_'+id).selectedIndex].value;
		var option3 = document.getElementById('productOptions3_'+id).options[document.getElementById('productOptions3_'+id).selectedIndex].value;
		http.open("GET", "/modules/shop/ajax.php?updateProductPrice=1&option1=" + option1 + "&option2=" + option2 + "&option3=" + option3 + "&prod_id=" + id, true);
		http.onreadystatechange = handleUpdateProductPriceHttpResponse;
		isWorking = true;
		http.send(null);
	}
}

function admin_showSpecialOfferPrices() {
	if(!isWorking && http) {
		document.getElementById('offerDetails').style.display = 'block';
		var prod = document.getElementById('product').options[document.getElementById('product').selectedIndex].value;
		http.open("GET", "/modules/shop/ajax.php?showSpecialOfferPrices=1&prod_id=" + prod, true);
		http.onreadystatechange = handleShowSpecialOfferPricesHttpResponse;
		isWorking = true;
		http.send(null);
	}
}

function createAccount() {
	if(!isWorking && http) {
		http.open("GET", "/modules/shop/ajax.php?createAccount=1", true);
		http.onreadystatechange = handleCreateAccountHttpResponse;
		isWorking = true;
		http.send(null);
	}
}