var myIndex = 10000;

function AddProduct(intProdId, strProdName, strPrice1, intNoOfImages, strProductType) {
    document.getElementById('instructions').style.display = 'none';
	var elemid
	var adding = false

	// show the image(s)
	for(i=0;i<intNoOfImages;i++){
		elemid = 'p' + intProdId + '_' + ((i*1) + 1)
		if(document.getElementById(elemid).style.display == 'none'){
			document.getElementById(elemid).style.display = 'block';

			if(getImageWidth(elemid)>600){
				document.getElementById(elemid).style.zIndex = 10;
				}
			else {
				document.getElementById(elemid).style.zIndex = myIndex;
				myIndex = myIndex + 10;
				}
			adding = true;
			}
		}

	// update info panel & delete/info input
	UpdateSelectedProduct(strProdName, intProdId, intNoOfImages, strProductType, strPrice1);

	if(adding==true){
		// add to room contents
		var strRoomContents = document.getElementById(strProductType + 'Items');
		strRoomContents.innerHTML = strRoomContents.innerHTML + '<div class="item" id="Item' + intProdId + '"><img src="images/roomdesigner/bullet-2.gif" alt="" class="bullet" /><div class="description">' + strProdName.replace(/%20/gi, ' ') + '</div><div class="quantity"><input onchange="UpdatePrices()" type="text" value="1" id="qty' + intProdId + '" name="qty' + intProdId + '"/></div><div class="price" id="linePrice'+ intProdId +'">&pound;' + (strPrice1 * 1).toFixed(2) + '</div></div><input type="hidden" id="price' + intProdId + '" name="price' + intProdId + '" value="'+ (strPrice1 * 1).toFixed(2) +'"/>';

		// add to addbasket input
		var strBasketAdd = document.getElementById('basketadd').value;
		document.getElementById('basketadd').value = strBasketAdd + intProdId + ',';
		
		UpdatePrices()
		}
	}
	
function RemoveProduct() {
	var intProdId = document.getElementById('selectedproduct').value;
	var intNoOfImages = document.getElementById('selectedproductimages').value;
	var strProductType = document.getElementById('selectedproducttype').value;
	var strPrice1 = document.getElementById('selectedproductprice').value;
	
	// hide the images
	for(i=0;i<intNoOfImages;i++){
		elemid = 'p' + intProdId + '_' + ((i*1) + 1);
		document.getElementById(elemid).style.display = 'none';
		}
		
	// update the product list
	var strRoomContents = document.getElementById(strProductType + 'Items');
	strRoomContents.removeChild(document.getElementById('Item'+ intProdId));

	// remove from addbasket input
	var strBasketAdd = document.getElementById('basketadd').value;
	document.getElementById('basketadd').value = strBasketAdd.replace(',' + intProdId + ',', ',');
	
	UpdatePrices()
	}

function UpdateSelectedProduct(strProdName, intProdId, intNoOfImages, strProductType, strPrice1) {
	document.getElementById('selecteditemupdate').innerHTML = strProdName.replace(/%20/gi,' ');
	
	document.getElementById('selectedproduct').value = intProdId;
	document.getElementById('selectedproductimages').value = intNoOfImages;
	document.getElementById('selectedproducttype').value = strProductType;
	document.getElementById('selectedproductprice').value = strPrice1;
	}

function SubmitRoom() {
	if(document.getElementById('roomtotalitems').value*1 > 0){
		document.getElementById('rd_form').submit();
		} 
	else {
		alert('No products chosen')	
		}
	}

function SetZIndex(id) {
	if(document.getElementById(id).style.zIndex>10){
		document.getElementById(id).style.zIndex=myIndex;
		myIndex = myIndex + 10;
		}
	}

function ViewProduct() {
	window.open('product-popup.html?-pid'+ document.getElementById('selectedproduct').value, 'productPop', "width=700,height=500,scrollbars=yes");
	}

function UpdatePrices() {
	var strBasketAdd = document.getElementById('basketadd').value;
	var decTotalPrice = 0;
	var intTotalItems = 0;
	var qtyInput;
	var priceInput;
	var i;
	
	var arrBasketAdd=strBasketAdd.split(",");

	for (i=0;i<arrBasketAdd.length;i++) {
		if(arrBasketAdd[i]!='') {
			qtyInput = document.getElementById('qty' + arrBasketAdd[i]).value;
			priceInput = document.getElementById('price' + arrBasketAdd[i]).value;
			
			linePrice = (qtyInput * 1) * (priceInput * 1)
			document.getElementById('linePrice'+ arrBasketAdd[i]).innerHTML = '&pound;' + linePrice.toFixed(2)
			
			decTotalPrice = (decTotalPrice * 1) + (linePrice.toFixed(2) * 1)
			intTotalItems = intTotalItems + (qtyInput * 1)
			}
		}
	
	var decTotalPriceVAT = decTotalPrice * 1.175
	decTotalPriceVAT = decTotalPriceVAT.toFixed(2);
	
	document.getElementById('totalprice').innerHTML = '&pound;' + decTotalPrice.toFixed(2);
	document.getElementById('roomcontentstotalprice').innerHTML = '&pound;' + decTotalPrice.toFixed(2);
	document.getElementById('totalpricevat').innerHTML = '(&pound;'+ decTotalPriceVAT +' inc.VAT)';
	document.getElementById('roomcontentstotalpricevat').innerHTML = '(&pound;'+ decTotalPriceVAT +' inc.VAT)';
	
	document.getElementById('roomtotalitems').value = intTotalItems;
	document.getElementById('totalitems').innerHTML = intTotalItems;	
	}