// JavaScript Document
	
	var pillow_chosen_manually = false;
	var suppress_onchange = false;

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

			document.getElementById("fabrics_holder").innerHTML=xmlHttp.responseText ;
			$("#loading").css("display", "none");
		 } 
	}

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

		//Split size values
		var size_detail = document.form1.duvet_size.value.split(",");
		var size_code = size_detail[1];
		var size_description = size_detail[2];

//alert(pillow_chosen_manually);

		if (pillow_chosen_manually == false && suppress_onchange == false)
		{
	//		alert("a");
	
			document.form1.pillow_size[3].disabled = "";

			suppress_onchange = true;
			if (size_code == "toddler")
			{
				document.form1.pillow_size.selectedIndex = 1;		
				document.form1.pillow_size.disabled = "";
				document.form1.pillow_size[3].disabled = "disabled";
			}
			else if (size_code == "throw")
			{
				document.form1.pillow_size.selectedIndex = 0;		
				document.form1.pillow_size.disabled = "disabled";
			}
			else if (size_code == "single")
			{
				document.form1.pillow_size.selectedIndex = 2;		
				document.form1.pillow_size.disabled = "disabled";
			}
			else if (size_code == "double")
			{
				document.form1.pillow_size.selectedIndex = 3;		
				document.form1.pillow_size.disabled = "disabled";
			}
			else if (size_code == "doublethrow")
			{
				document.form1.pillow_size.selectedIndex = 0;		
				document.form1.pillow_size.disabled = "disabled";
			}
			
		}
		
		pillow_chosen_manually = false;
		suppress_onchange = false;
		
		//Split pillow values
		var pillow_detail = document.form1.pillow_size.value.split(",");
		var pillow_code = pillow_detail[1];
		var pillow_description = pillow_detail[2];

		document.form1.technical_options.value = "&duvet_size=" + size_code + "&pillow_size=" + pillow_code;


		if (document.form1.fabricCostPerM_duvet.value == "")
			return;
			


	
		//Call AJAX to calculate pricing + provide summary description
		var url="iwa/include/ajax/duvet.php?itemID=" + document.form1.thingID.value + "&sizeDetail=" + document.form1.duvet_size.value + "&fabricCostPerM_duvet=" + document.form1.fabricCostPerM_duvet.value+ "&fabricName_duvet=" + document.form1.fabricName_duvet.value+ "&fabricRepeat_duvet=" + document.form1.fabricRepeat_duvet.value  + "&pillow_description=" + pillow_description;
		xmlHttp.onreadystatechange=calculate_response_duvet;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);



		//Store other settings in option variables
		MM_findObj("optionTypeID_1").value = document.form1.fabricName_duvet.value + ",0.00,Material,0";
		MM_findObj("optionTypeID_2").value = size_description + ",0.00,Duvet Size,0";
		MM_findObj("optionTypeID_3").value = pillow_description + ",0.00,Pillow,0";

	
	}
	
	function calculate_response_duvet()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			
			response = xmlHttp.responseText.split("@@IWA@@");
	
	
			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_duvets();

	
	
	}
