// JavaScript Document
	function stateChanged() 
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		 { 

			document.getElementById("fabrics_holder").innerHTML=xmlHttp.responseText ;
		 } 
	}
	
	function calculate_cushions()
	{

		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
			return;
		}


		//Call AJAX to calculate pricing + provide summary description
		var url="iwa/include/ajax/cushions.php?itemID=" + document.form1.thingID.value + "&cushion_size=" + document.form1.cushion_size.value + "&cushion_pad=" + document.form1.cushion_pad.value + "&edging=" + document.form1.edging.value + "&fabricCostPerM_cushion=" + document.form1.fabricCostPerM_cushion.value+ "&fabricName_cushion=" + document.form1.fabricName_cushion.value+ "&fabricRepeat_cushion=" + document.form1.fabricRepeat_cushion.value;



		xmlHttp.onreadystatechange=calculate_response_cushions;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);


		//Split size values
		var size_description = document.form1.cushion_size.value.split("@")[0];
		var pad_description = document.form1.cushion_pad.value.split("|")[0];
		var edging_description = document.form1.edging.value;

		//Store other settings in option variables
		MM_findObj("optionTypeID_1").value = document.form1.fabricName_cushion.value + ",0.00,Material,0";
		MM_findObj("optionTypeID_2").value = size_description + ",0.00,Size,0";
		MM_findObj("optionTypeID_3").value = pad_description + ",0.00,Pad,0";
		MM_findObj("optionTypeID_4").value = edging_description + ",0.00,Style,0";
	
	}


	function calculate_response_cushions()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			
			response = xmlHttp.responseText.split("@@IWA@@");
	//alert(response);
	
			if (response[0] == "FAILED")
			{
				alert("FAILED:" + response[1]);
				return;
			}
			else if (response[0] == "FAILED_SILENT")
			{
				return;	
			}
			
			var total_cost = response[1];
			var description = response[2];
			
			//document.getElementById("fabrics_holder").innerHTML=xmlHttp.responseText ;
			
			document.getElementById("build_summary_description").innerHTML = description;
		
			var obj_totalPrice = MM_findObj("build_total_price");
			obj_totalPrice.innerHTML = "£" + parseFloat(total_cost).toFixed(2);
			document.form1.rrp.value = total_cost;
		
			var obj_buildRight = MM_findObj("build_right");
			obj_buildRight.style.display = "block";
		
			var obj_buildBuy = MM_findObj("build_buy");
			if (total_cost > 0.00)
				obj_buildBuy.style.display = "block";
			else
				obj_buildBuy.style.display = "none";
	
			
		} 
		
	}



	function selectFabric(obj, ID, name, pricePerM, repeat, thumb_src)
	{
		



		var objInfoName = MM_findObj("build_panel_material_info_name_" + fabricForWhat);
		var objInfoPrice = MM_findObj("build_panel_material_info_price_" + fabricForWhat );

		eval("document.form1.fabricID_" + fabricForWhat + ".value = ID;");
		eval("document.form1.fabricName_" + fabricForWhat + ".value = name;");
		eval("document.form1.fabricCostPerM_" + fabricForWhat + ".value = pricePerM;");
		eval("document.form1.fabricRepeat_" + fabricForWhat + ".value = repeat;");

		eval("document.form1.fabricImage_" + fabricForWhat + ".src = thumb_src;");


		document.form1.thumbnailFile.value = thumb_src;

		objInfoName.innerHTML = name;
		objInfoPrice.innerHTML = "£" + pricePerM + " per metre";

		//Hide the fabric selection panel
		hideFabricSelection();

		//Calculate
		calculate_cushions();

	
	
	}


	function fillPadOptions(strSizeString)
	{
		//Remove current options
		for (i=0; i <= document.form1.cushion_pad.length; i++)
		{
			document.form1.cushion_pad.remove(0);
		//	document.form1.cushion_pad.removeChild();
		}
		
		//Get pad options section of string
		var sizeOptions = (strSizeString.split("@")[3]).split("|");
		

		//Add blank You choose... option
		var opt = document.createElement("option");
		opt.text = "No Pad";
		opt.value = "No Pad|0";
		document.form1.cushion_pad.options.add(opt);


		for (i=0; i < sizeOptions.length;  i++)
		{
			var thisPadOption = sizeOptions[i].split(",");	
			
			
			
			var opt = document.createElement("option");
			
			opt.text = thisPadOption[0];
			opt.value = thisPadOption[0] + "|" + thisPadOption[1];

			document.form1.cushion_pad.options.add(opt);

		}

		//alert(sizeOptions);
		
		calculate_cushions();
		
	}