// JavaScript Document
	function decimalplace(value){
		if(!isNaN(value) & value <1)
		{
			value = value * 100;
			value = Math.round(value);
			//value = value/100;			
			
			value = value+'';
			value = value.substr(0, value.length - 2) + "0." + value.substr(value.length - 2, value.length);
			
			return value;		
		}
		else if(!isNaN(value)){
			value = value * 100;
			value = Math.round(value);
			//value = value/100;			
			
			value = value+'';
			value = value.substr(0, value.length - 2) + "." + value.substr(value.length - 2, value.length);
			
			return value;
		}

		 else {
			return "0.00";
		}
	}

	function calculateprice(id,cheapestprice){
		
		var currentprice = eval("price_" + id);
		var currentmainid = "";
		currentprice = cheapestprice;

		if(document.getElementById("mainid_" + id)){
			selectedmainid = document.getElementById("mainid_" + id).value;
			priceArr = eval("mainid_price_" + id);

			for(a=0; a<priceArr.length; a++){
				if(priceArr[a][0] == selectedmainid){
					currentprice = priceArr[a][1];
				}
			}
			currentmainid = eval("currentmainid_" + id);

			if(currentmainid != selectedmainid){
				currentmainid = eval("currentmainid_" + id + " = '" + selectedmainid + "'");

				if(currentmainid != ""){
					for(a=1;a<5;a++){
						if(document.getElementById("option"+a+"_coderef_"+id)){
							var currCodeRef = eval("option" + a + "_" + currentmainid + "_" + id + "_CodeRef");
							document.getElementById("option"+a+"_coderef_"+id).value = currCodeRef;
						}
						
						var currArray = eval("option" + a + "_" + currentmainid + "_" + id + "_Array");
					
						$("#option" + a + "_" + id).removeOption(/./);
					
						for(b=0; b<currArray.length; b++){
							$("#option" + a + "_" + id).addOption(currArray[b][0], currArray[b][2]);
						}
						
						if(currArray.length != 1){
							$("#option" + a + "_" + id).selectOptions("");
						} else {
							$("#option" + a + "_" + id).selectOptions(currArray[0][0]);
						}
					}
				} else {
					for(a=1;a<5;a++){
						//$("#option" + a + "_" + id).removeOption(/./);
						//$("#option" + a + "_" + id).addOption("", "Select");
					}
				}
			} else {
				if(currentmainid == ""){
					for(a=1;a<5;a++){
					    /*optval = 'Please Select'
						$("#option" + a + "_" + id).removeOption(/./);
						$("#option" + a + "_" + id).addOption("", optval);*/
					}
				}
			}
			
			if(parseFloat(getOptionPriceByMainID(1, id, currentmainid))) currentprice = parseFloat(currentprice) + parseFloat(getOptionPriceByMainID(1, id, currentmainid));
			if(parseFloat(getOptionPriceByMainID(2, id, currentmainid))) currentprice = parseFloat(currentprice) + parseFloat(getOptionPriceByMainID(2, id, currentmainid));
			if(parseFloat(getOptionPriceByMainID(3, id, currentmainid))) currentprice = parseFloat(currentprice) + parseFloat(getOptionPriceByMainID(3, id, currentmainid));
			if(parseFloat(getOptionPriceByMainID(4, id, currentmainid))) currentprice = parseFloat(currentprice) + parseFloat(getOptionPriceByMainID(4, id, currentmainid));
		} else {
			if(parseFloat(getOptionPrice(1, id))) currentprice = parseFloat(currentprice) + parseFloat(getOptionPrice(1, id));
			if(parseFloat(getOptionPrice(2, id))) currentprice = parseFloat(currentprice) + parseFloat(getOptionPrice(2, id));
			if(parseFloat(getOptionPrice(3, id))) currentprice = parseFloat(currentprice) + parseFloat(getOptionPrice(3, id));
			if(parseFloat(getOptionPrice(4, id))) currentprice = parseFloat(currentprice) + parseFloat(getOptionPrice(4, id));
		}

		if(isNaN(document.getElementById("quantity_" + id).value)){
			currentprice = 0;
		} else {
			minimumquantity = eval("minimumquantity_" + id);

			if(document.getElementById("quantity_" + id).value < minimumquantity){
				document.getElementById("quantity_" + id).value = minimumquantity;
			}
			
			if(document.getElementById("unitprice_" + id)){
				document.getElementById("unitprice_" + id).innerHTML = decimalplace(currentprice);
			}
			currentprice = currentprice * document.getElementById("quantity_" + id).value;
		}
	
	
		if(document.getElementById("mainid_" + id)){
			if(document.getElementById("mainid_" + id).value == ""){
				document.getElementById("price_" + id).innerHTML = decimalplace(currentprice);
				//document.getElementById("price_" + id).innerHTML = "0.00";
				//document.getElementById("price_" + id).innerHTML = decimalplace(currentprice);
			} else {
				document.getElementById("price_" + id).innerHTML = decimalplace(currentprice);
			
				if (document.getElementById("size1")){document.getElementById("size1").innerHTML = "<p>&nbsp;</p>";}
				if (document.getElementById("size2")){document.getElementById("size2").innerHTML = "<p>&nbsp;</p>";}
			}
		} else {
			document.getElementById("price_" + id).innerHTML = decimalplace(currentprice);
		}
	}
	
	function getOptionPrice(option, id){
		var retVar = 0;

		try{
			var currArray = eval("option" + option + "_" + id + "_Array");
			var currValue = document.getElementById("option" + option + "_" + id).value;

			for(a = 0; a<currArray.length;a++){
				if(currArray[a][0] == currValue){
					retVar = currArray[a][1];
				}
			}
		} catch (err) {
		}
		
		return retVar;
	}
	
	function getOptionPriceByMainID(option, id, mainid){
		var retVar = 0;

		try{
			var currArray = eval("option" + option + "_" + mainid + "_" + id + "_Array");
			var currValue = document.getElementById("option" + option + "_" + id).value;

			for(a = 0; a<currArray.length;a++){
				if(currArray[a][0] == currValue){
					retVar = currArray[a][1];
				}
			}
		} catch (err) {
		}
		
		return retVar;
	}
	
	function validateProductList(id){
		//alert('validateProductList')
		var valid = true;
		var errArr = new Array();

		if(document.getElementById("mainid_" + id)){
			if(document.getElementById("mainid_" + id).value == ""){
				errArr.push("Please select Size.");
				valid = false;
			}
		}

		for(a=1; a < 5; a++){
			if(document.getElementById("option" + a + "_" + id)){
				if(document.getElementById("option" + a + "_" + id).value == ""){
					errArr.push("Please select " + document.getElementById("option" + a + "_type_" + id).value + ".");
					valid = false;
				}
			}
		}

		if(isNaN(document.getElementById("quantity_" + id).value) || document.getElementById("quantity_" + id).value.length == 0){
			errArr.push("Please enter a valid quantity");
			valid = false;
		} else {
			minimumquantity = eval("minimumquantity_" + id);

			if(parseInt(document.getElementById("quantity_" + id).value) < minimumquantity){
				errArr.push("The quantity selected is less than the minimum order allowed (" + minimumquantity + " items)");
				valid = false;
			}			
		}

		if(errArr.length > 0){
			var errText = "The following errors have occurred: \n\n";
			
			for(a=0; a<errArr.length; a++){
				errText += "- " + errArr[a] + "\n"
			}
			
			errText += "\nPlease correct and try again.";
			
			alert(errText);
		} else {
						
			target = document.getElementById("f_" + id).action;
			postvalues = "dotdotpath=" + dotdotpath + "&listingarea=" + document.getElementById("f_" + id).listingarea.value;
			postvaluesjoin = "&";

			if(document.getElementById("productrangeid_" + id)){
					postvalues += postvaluesjoin + "productrangeid=" + document.getElementById("productrangeid_" + id).value;
					postvaluesjoin="&";
			}

			if(document.getElementById("mainid_" + id)){
					postvalues += postvaluesjoin + "mainid_" + id + "=" + document.getElementById("mainid_" + id).value;
					postvaluesjoin="&";
			}

			for(a=1; a < 5; a++){
				if(document.getElementById("option" + a + "_" + id)){
					postvalues += postvaluesjoin + "option" + a + "_coderef_" + id + "=" + document.getElementById("option" + a + "_coderef_" + id).value;
					postvalues += postvaluesjoin + "option" + a + "_type_" + id + "=" + document.getElementById("option" + a + "_type_" + id).value;
					postvalues += postvaluesjoin + "option" + a + "_" + id + "=" + document.getElementById("option" + a + "_" + id).value;
					postvaluesjoin="&";
				}
			}
			
			if(document.getElementById("quantity_" + id)){
					postvalues += postvaluesjoin + "quantity_" + id + "=" + document.getElementById("quantity_" + id).value;
					postvaluesjoin="&";
			}
			document.getElementById('floatbox_position').offsetHeight;
			document.getElementById('floatbox_position').style.visibility = "visible";
						
			$.ajax({
				type: "POST",
				url: target,
				data: postvalues,
				success: function(msg){
					//alert('success')
					document.getElementById('floatingbasket').innerHTML = msg;
					$('#floatingbasket').slideDown("fast");
					//alert( document.getElementById('basketcount').innerHTML);
					document.getElementById('basketcount_header').innerHTML = document.getElementById('basketcount').innerHTML + " items ";
					document.getElementById('baskettotal_header').innerHTML = '&pound;'+ document.getElementById('baskettotal').innerHTML;
					//document.getElementById('basketcount').innerHTML + " items <span class=\"price\">£81.36</span>";	
					/*if(document.getElementById("i_basket") != null){
						var el = document.getElementById("floatbox");
						var el2 = document.getElementById("i_basket");
						el2.style.height = el.clientHeight +20;
						el2.style.visibility="visible";
					}*/
					timerID = setTimeout("hideBasket()",5000);
					
					
				}
			});
									
			//window.scroll(0,0);
			
			//document.getElementsByTagName("select").style.display = "none";
			//get browser type
			var browser=navigator.appName;
			var b_version=navigator.appVersion;
				
				if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
					{ 
					//test for MSIE x.x;
				 	 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
					 if (ieversion>=7)
					   {
					   }
					 else if (ieversion>=6)
					 	{
		 			//if browser IE < 7 then hide select box
							var x = document.getElementsByTagName("select");
							for (i = 0; i < x.length; i++) 
							{
							x[i].style.display = "none";
							}
						}
					}
		}
	}
	
	function specialoffer_buyitnow(specialofferid, dotdotpath, quantity){
		document.getElementById('floatbox_position').style.visibility = "visible";
		$.ajax({
			type: "POST",
			url: dotdotpath + "special_offers/submit.cfm",
			data: "specialofferid=" + specialofferid + "&dotdotpath=" + dotdotpath + "&quant_" + specialofferid + "=" + quantity,
			success: function(msg){
				document.getElementById('floatingbasket').innerHTML = msg;
				$('#floatingbasket').slideDown("fast");

				document.getElementById('basketcount_header').innerHTML = document.getElementById('basketcount').innerHTML + " items "		
				document.getElementById('baskettotal_header').innerHTML = '&pound;'+ document.getElementById('baskettotal').innerHTML;
				timerID = setTimeout("hideBasket()",5000)
			}
		});
	}
	
	function validatespecialofferform(specialofferid, dotdotpath){
		quantobj = document.getElementById("quant_" + specialofferid);
		
		if(isNaN(quantobj.value) || parseInt(quantobj.value) <= 0 || quantobj.value.length == 0){
			alert("Please select a valid quantity");
		} else {
			document.getElementById('floatbox_position').style.visibility = "visible";
			$.ajax({
				type: "POST",
				url: dotdotpath + "special_offers/submit.cfm",
				data: "specialofferid=" + specialofferid + "&dotdotpath=" + dotdotpath + "&quant_" + specialofferid + "=" + quantobj.value,
				success: function(msg){
					document.getElementById('floatingbasket').innerHTML = msg;
					$('#floatingbasket').slideDown("fast");
					
					document.getElementById('basketcount_header').innerHTML = document.getElementById('basketcount').innerHTML + " items "
					document.getElementById('baskettotal_header').innerHTML = '&pound;'+ document.getElementById('baskettotal').innerHTML;
					timerID = setTimeout("hideBasket()",5000)
				}
			});
		}
	}	
	
	function validatebestsellingform(bestsellingid, dotdotpath, minquan){
		quantobj = document.getElementById("quant_" + bestsellingid);
		
		if(isNaN(quantobj.value) || parseInt(quantobj.value) <= 0 || quantobj.value.length == 0 || parseInt(quantobj.value) < parseInt(minquan)){
			alert("Please select a valid quantity");
		} else {
			document.getElementById('floatbox_position').style.visibility = "visible";
			$.ajax({
				type: "POST",
				url: dotdotpath + "best_buys/submit.cfm",
				data: "bestsellingid=" + bestsellingid + "&dotdotpath=" + dotdotpath + "&quant_" + bestsellingid + "=" + quantobj.value,
				success: function(msg){
					document.getElementById('floatingbasket').innerHTML = msg;
					$('#floatingbasket').slideDown("fast");
					
					document.getElementById('basketcount_header').innerHTML = document.getElementById('basketcount').innerHTML + " items "
					document.getElementById('baskettotal_header').innerHTML = '&pound;'+ document.getElementById('baskettotal').innerHTML;
					timerID = setTimeout("hideBasket()",5000)
				}
			});
		}
	}	
	
	function validatestockitemform(stockitemid, dotdotpath){
		//document.getElementByID("floatingbasket").style.visibility = "visible";
		quantobj = document.getElementById("quant_" + stockitemid);
		minobj = document.getElementById("minimumstockitems_" + stockitemid);

		if(isNaN(quantobj.value) || parseInt(quantobj.value) <= 0 || quantobj.value.length == 0){
			alert("Please select a valid quantity");
		} else {
			if(parseInt(quantobj.value) < parseInt(minobj.value)){
				alert("The quantity selected is less than the minimum order allowed (" + minobj.value + " items)");
				
			} else {
				document.getElementById('floatbox_position').style.visibility = "visible";
				$.ajax({
					type: "POST",
					url: dotdotpath + "stockitems/submit.cfm",
					data: "stockitemid=" + stockitemid + "&dotdotpath=" + dotdotpath + "&quant_" + stockitemid + "=" + quantobj.value,
					success: function(msg){
						document.getElementById('floatingbasket').innerHTML = msg;
						$('#floatingbasket').slideDown("fast");
						
						document.getElementById('basketcount_header').innerHTML = document.getElementById('basketcount').innerHTML + " items "	
						document.getElementById('baskettotal_header').innerHTML = '&pound;'+ document.getElementById('baskettotal').innerHTML;
						timerID = setTimeout("hideBasket()",5000)
					}
				});
			}
		}
	}	
	
	function calculatestockitemprice(stockitemid){
		quantobj = document.getElementById("quant_" + stockitemid);
		priceobj = document.getElementById("price_" + stockitemid);

		if(isNaN(quantobj.value) || parseInt(quantobj.value) <= 0 || quantobj.value.length == 0){
			document.getElementById("price").innerHTML = "&pound; Please select valid quantity";
		} else {
			document.getElementById("price").innerHTML = "&pound; " + decimalplace(parseInt(quantobj.value) * parseInt(priceobj.value));
		}
	}
	function calculatespecialofferitemprice(specialofferid){
		quantobj = document.getElementById("quant_" + specialofferid);
		listpriceobj = document.getElementById("listprice_" + specialofferid);
		priceobj = document.getElementById("price_" + specialofferid);

		if(isNaN(quantobj.value) || parseInt(quantobj.value) <= 0 || quantobj.value.length == 0){
			document.getElementById("priceout_" + specialofferid).innerHTML = "&pound; Please select valid quantity";
			document.getElementById("discountedpriceout_" + specialofferid).innerHTML = "&nbsp;";
		} else {
			document.getElementById("priceout_" + specialofferid).innerHTML = "List Price: <strong style=\"text-decoration:line-through\">&pound;" + decimalplace(parseInt(quantobj.value) * parseFloat(listpriceobj.value)) + "</strong> ";
			document.getElementById("discountedpriceout_" + specialofferid).innerHTML = "Special Offer Price: <strong>&pound;" + decimalplace(parseInt(quantobj.value) * parseFloat(priceobj.value)) + "</strong> ";
		}
	}

	var timerID;
	
	function hideBasket(){
		/*var el2 = document.getElementById("i_basket");
		el2.style.height = 10;
		el2.style.display="none";*/
		$('#floatingbasket').slideUp("slow");
		document.getElementById('floatbox_position').style.visibility = "hidden";
		//document.getElementById("quickselector").style.display = "";
		//reshow select
		var x = document.getElementsByTagName("select");
			for (i = 0; i < x.length; i++) {
			x[i].style.display = "block";
			// or
			// x[i].style.visibility = "hidden"
			}
		
	}
	
	function updatequant(id, amount){
		minquantobj = document.getElementById("minquantity_" + id);
		currentquantobj = document.getElementById("quant_" + id);
		
		newvalue = parseInt(currentquantobj.value) + parseInt(amount);
		
		if(parseInt(minquantobj.value) > newvalue){
			if(amount > 0){
				currentquantobj.value = minquantobj.value;
			} else {
				currentquantobj.value = 0;
			}
		} else {
			currentquantobj.value = newvalue;
		}
	}
	
	function validatequant(id){
		minquantobj = document.getElementById("minquantity_" + id);
		currentquantobj = document.getElementById("quant_" + id);
		
		if(!isNaN(minquantobj.value)){
			if(parseInt(minquantobj.value) > parseInt(currentquantobj.value)){
				currentquantobj.value = 0;
			}
		} else {
			currentquantobj.value = 0;
		}
	}

	function updatebasket(id)
	{	
		document.getElementById("ubasket"+id).submit();
	}

	/*function updatebasket(){
		document.getElementById("ubasket").submit();
	}*/
			function updateprizebasket(){
		document.getElementById("prizebasket").submit();
	}

	
