var xmlHttp;
frmCnt = 0;

	function moveheadboardStyle()
	{  
		for (i=0; i<document.headboard.headboard_style.length; i++) {
		if (document.headboard.headboard_style[i].checked) {
		var t = document.headboard.headboard_style[i].value
		myOption = i;
		}
		}
		arrT = t.split('#')
		//alert(t)	
		var i = 1 ;
		var running = true ;
		while( running )
		{
		   var headboard_style = document.getElementById( 'y' + i ) ;
			if( headboard_style )
			{
				headboard_style.value = arrT[1];
			}
			else
			{
				running = false ;
			}
			i++ ;
		}	
	}
		
	function moveTheStorageOption()
	{  
		for (i=0; i<document.headboard.storage.length; i++) {
		if (document.headboard.storage[i].checked) {
		var t = document.headboard.storage[i].value
		myOption = i;
		}
		}
		arrT = t.split('#')
	
			
		var i = 1 ;
		var running = true ;
		while( running )
		{
			var storage_option = document.getElementById( 'f' + i ) ;
			var storage_option_id = document.getElementById( 'g' + i ) ;
			if( storage_option )
			{
				if ((arrT[1] != 0) && (arrT[2] == 'yes'))
				{
				storage_option.value = arrT[0];
				storage_option_id.value = arrT[1];
				document.getElementById( 'z' + i ).disabled  = false;
				}
				else if ((arrT[2] == 'no') && (arrT[1] != 0))
				{
				storage_option.value = arrT[0];
				storage_option_id.value = arrT[1];
				document.getElementById( 'z' + i ).disabled  = true;
				document.getElementById( 'z' + i ).value  = 2;
				//alert(arrT[2])
				}
				else if ((arrT[2] == 'no') && (arrT[1] == 0))
				{
				storage_option.value = arrT[0];
				storage_option_id.value = arrT[1];
				document.getElementById( 'z' + i ).disabled  = true;
				document.getElementById( 'z' + i ).value  = 0;
				}
			}
			else
			{
				
				running = false ;
			}
			i++ ;
		}
	
	}
	
	function moveheadboardFabric(value)
	{  
		for (i=0; i<document.headboard.headboardfabric.length; i++) {
		if (document.headboard.headboardfabric[i].checked) {
		var x = value//document.headboard.headboardfabric[i].value
		myOption = i;
		}
		}
		
		var i = 1 ;
		var running = true ;
		while( running )
		{
			//alert(t)
			var headboard_fabric = document.getElementById( 'm' + i ) ;
			if( headboard_fabric )
			{
				headboard_fabric.value = x;
			}
			else
			{
				running = false ;
			}
			i++ ;
		}
	}
	
	function check_addtocart(form) 
	{
		var formNum = form.formcounter.value;//var i = 1 ;
		var storage_type = document.getElementById( 'z' + formNum ) ;
		var storage_option_id = document.getElementById( 'g' + formNum ) ;
		
		var headboard_style = document.getElementById( 'y' + formNum ) ;
		var headboard_fabric = document.getElementById( 'm' + formNum ) ;
		
		if ((storage_option_id.value != 0) && (storage_type.value == 0))
		 {
		 alert("Please select storage type (Slide-store drawer or Pull out Drawer)...");
		 storage_type.focus();
		 return false;
		 }
		 
		
		 
		storage_type.disabled = false
	}
	
	function check_addtocart2(form) 
	{
		if (form.sizes.value == 0)
		 {
		 alert("Please select your mattress size!");
		 form.sizes.focus();
		 return false;
		 }
	}

	function showPrice(product_id, size_id, storage_option_id, storage_type_id, base_id, formcounter)
	{ 
	var url="getprice.asp?sid=" + Math.random() + "&o=" + product_id + "&p=" + size_id + "&q=" + storage_type_id + "&r=" + base_id + "&s=" + storage_option_id 
	frmCnt = formcounter;
	//alert(url)
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , false)
	xmlHttp.send(null)
	}
	
	function stateChanged() 
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			var t = xmlHttp.responseText;
			//arrtT = t.split('#');
			

			//GET TOTAL FOR BASKET
			var price = document.getElementById( 'sop' + frmCnt);
			//price.value = eval(price.value) + eval(arrtT[1])
			price.value = eval(t);
			var priceForHeadboards = document.getElementById( 'hp' + frmCnt);
			var totalForBasket = document.getElementById( 'price' + frmCnt);
			totalForBasket.value = CurrencyFormatted(eval(price.value) + eval(priceForHeadboards.value));
		} 
	} 
	
	function changePriceVal(formfrom,priceform){
		var myindex=formfrom.storage.selectedIndex;
		priceform.storage_option.value = formfrom.storage.options[myindex].value;
		var original = parseFloat(priceform.pricehidden.value);
		var addto = original;
        if(myindex == 1){
			addto = addto + 25.00;
			priceform.price.value = "£"+ addto;
		}
		else if(myindex ==2)
		{
			
			addto = addto + 30.00;
			priceform.price.value = "£" +addto;
		}
		else if(myindex ==3){
			addto = addto + 30.00;
			priceform.price.value = "£" +addto;
		}
		else if(myindex ==4){
			addto = addto + 60.00;
			priceform.price.value = "£" +addto;
		}
		else if(myindex == 0){
			addto = priceform.pricehidden.value;
			priceform.price.value = "£" +original;
		}
		priceform.pricehiddenafterchange.value = addto;
		
		for (var i=0; i < formfrom.selhead.length; i++)
   		{
  			 if (formfrom.selhead[i].checked)
      		{
      			AddHeadboard(formfrom.selhead[i],priceform);
      		}
   		}

	}
	
	function AddHeadboard(value,priceform){
			var x = value.value;
			arrX = x.split('#')
			var headboard_option_id = arrX[1];
			var original = parseFloat(priceform.pricehiddenafterchange.value);
			var addto = original;
			
				addto = addto + parseFloat(headboard_option_id);
				priceform.price.value = "£"+ addto;
				var style = arrX[2];
				
				if (style == "''"){
					priceform.headboard_style.value = null;
				}
				else{
					priceform.headboard_style.value = style;
				}
	}
	
	
	
	function getPrice(storage_option_id, product_id)
	{
	var x = storage_option_id;
	arrX = x.split('#')
	storage_option_id = arrX[1];	
	
	var i = 1 ;
    var running = true ;
    while( running )
    {
        var product_size_id = document.getElementById( 'psID' + i ) ;
		var storage_type_id = document.getElementById( 'z' + i );
		var base_id = document.getElementById( 'b' + i );
        if( product_size_id )
        {
            size_id = product_size_id.value;
			storage_type_id = storage_type_id.value;
			base_id = base_id.value;
			//proceed as normal
			var url="getprice.asp?sid=" + Math.random() + "&o=" + product_id + "&p=" + size_id + "&q=" + storage_type_id + "&r=" + base_id + "&s=" + storage_option_id + "&formCnt=" + i 
			//alert(url)
			xmlHttp=GetXmlHttpObject(stateChangedForOpt)
			xmlHttp.open("GET", url , false)
			xmlHttp.send(null)
        }
        else
        {
            running = false ;
        }
        i++ ;
    }
	}
	
	function stateChangedForOpt() 
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			var t = xmlHttp.responseText;
			arrtT = t.split('#')
			var i = 1 ;	
			//var price = document.getElementById( 'price' + arrtT[1]);
			var price = document.getElementById( 'sop' + arrtT[0]);
			price.value = eval(arrtT[1])
			
			//GET TOTAL FOR BASKET
			priceForHeadboards = document.getElementById( 'hp' + arrtT[0]);
			var totalForBasket = document.getElementById( 'price' + arrtT[0]);
			totalForBasket.value = CurrencyFormatted(eval(price.value) + eval(priceForHeadboards.value))
		} 
	} 
	
	function addHeadboardPrice(headboard_id)
	{
	//alert(headboard_id)
	var x = headboard_id;
	arrX = x.split('#')
	headboard_id = arrX[0];
	
	var i = 1 ;
    var running = true ;
    while( running )
    {
        var product_size_id = document.getElementById( 'psID' + i ) ;
        if( product_size_id )
        {
            size_id = product_size_id.value;
			//proceed as normal
			var url="getheadboardprice.asp?sid=" + Math.random() + "&o=" + headboard_id + "&p=" + size_id + "&formCnt=" + i  
			xmlHttp=GetXmlHttpObject(stateChangedForHeadboard)
			xmlHttp.open("GET", url , false)
			xmlHttp.send(null)
        }
        else
        {
            running = false ;
        }
        i++ ;
    }
	}
		
	function stateChangedForHeadboard() 
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			var t = xmlHttp.responseText;
			arrtT = t.split('#')
			//var price = document.getElementById( 'price' + arrtT[0]);
			var price = document.getElementById( 'hp' + arrtT[0]);
			price.value = eval(arrtT[1]);
			
			
			//GET TOTAL FOR BASKET
			priceForOptions = document.getElementById( 'sop' + arrtT[0]);
			var totalForBasket = document.getElementById( 'price' + arrtT[0]);
			totalForBasket.value = CurrencyFormatted(eval(price.value) + eval(priceForOptions.value))
			//alert(totalForBasket.value);
		} 
	} 
	
	function aidHeadboadSelection()
	{
		var selheadboard = document.getElementById('selheadboard') ;
		if (selheadboard.checked == true)
		{
			//alert(selheadboard.checked)
			document.getElementById('headboardfabric1').disabled = true;
			document.getElementById('headboardfabric2').disabled = true;
			document.cart1.y1.value = '';
			
			document.getElementById('headboardfabric1').checked = false;
			document.getElementById('headboardfabric2').checked = false;
			
			document.getElementById('hs1').disabled = true;
			document.getElementById('hs2').disabled = true;
			document.cart1.m1.value = '';
			//document.getElementById('hs3').disabled = true;
			
			document.getElementById('hs1').checked = false;
			document.getElementById('hs2').checked = false;
			//document.getElementById('hs3').checked = false;
			
			//REMOVE HEADBOARD PRICES FROM BASKET TOTAL
			//GET TOTAL FOR BASKET

			var i = 1 ;
			var running = true ;
			while( running )
			{
				var hp = document.getElementById( 'hp' + i ) ;
				if( hp )
				{
					priceForOptions = document.getElementById( 'sop' + i);
					HeadboardPrice = document.getElementById( 'hp' + i);
					HeadboardPrice.value = 0
					var totalForBasket = document.getElementById( 'price' + i);
					totalForBasket.value = CurrencyFormatted(0 + eval(priceForOptions.value))
				}
				else
				{
					running = false ;
				}
				i++ ;
			}
			//END
		}
		else
		{
			//alert(selheadboard.checked)
			document.getElementById('headboardfabric1').disabled = false;
			document.getElementById('headboardfabric2').disabled = false;
			document.getElementById('hs1').disabled = false;
			document.getElementById('hs2').disabled = false;
			//document.getElementById('hs3').disabled = false;
		}
	}

function GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null
	
	if (navigator.userAgent.indexOf("Opera")>=0)
	{
	alert("Scripts within this site don't work in Opera, please use Internet Explorer") 
	return 
	}
	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
	var strName="Msxml2.XMLHTTP"
	if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
	{
	strName="Microsoft.XMLHTTP"
	} 
	try
	{ 
	objXmlHttp=new ActiveXObject(strName)
	objXmlHttp.onreadystatechange=handler 
	return objXmlHttp
	} 
	catch(e)
	{ 
	alert("Error. Scripting for ActiveX might be disabled") 
	return 
	} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
	objXmlHttp=new XMLHttpRequest()
	objXmlHttp.onload=handler
	objXmlHttp.onerror=handler 
	return objXmlHttp
	}
} 

function poundsAsInt( textAmount )
{
    // Strip the "£" if it exists
    if( textAmount.charAt( 0 ) == '£' ) textAmount = textAmount.substring( 1 ) ;

    // return the number:
    return textAmount * 1.0 ;
}

function MattressPrice(strValue, frmCounter)
{ 
	var t = strValue;
	//alert(t)
	arrtT = t.split('#')
	//alert(arrtT[1])
	mattress_size = document.getElementById( 'ms' + frmCounter ) ;
	price = document.getElementById( 'mp' + frmCounter ) ;
	mattress_size.value = arrtT[0]
	price.value = arrtT[1]
}

function CurrencyFormatted(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '£' + dblValue + '.' + strCents);
}