// JavaScript Document


	var previousSelection = null;
	var fabricForWhat = "curtain";




	function orderSample(name, thumb_src)
	{
		document.form1.doWhat.value = "add_sample";
		document.form1.thingName.value = name;
		document.form1.action='kidscurtaincompany_shop.php';
		document.form1.thumbnail_src.value = thumb_src;
		document.form1.submit();
	}


	function ajax_loadFabrics(searchText, NO_LONGER_USED, pageNum, mode, isWood)
	{
	//	alert("load fabrics");
		selectedColour = "";
		selectedTopic = "";
		selectedCollection = "";

		if (typeof(mode) == "undefined" ||  mode == "" ||  mode == "sample")
		{

			selectedColour = document.form1.cmbColour.value;
			selectedTopic = document.form1.cmbTopic.value;
		}
		else if (mode == "collection")
		{
			selectedCollection = document.form1.collectionName.value;
		}
		
//		alert("mode is " + mode);
	//	alert("selectedColour is " + selectedColour);
		
		
		xmlHttp=GetXmlHttpObject();
		
		if (xmlHttp==null)
		{
			return;
		}
		
		if (pageNum == null)
			pageNum = 1;
		var results_per_page = -1;
		if (typeof(document.form1.results_per_page) != "undefined")
		{
			results_per_page = document.form1.results_per_page.value;
//			alert(document.form1.results_per_page.value);
			
		}


		var url="iwa/include/ajax/fabric.php?isWood=" + isWood + "&mode=" + mode +"&selectedCollection=" + selectedCollection + "&searchText=" + searchText + "&selectedColour=" + selectedColour + "&selectedTopic=" +  selectedTopic + "&pageNum=" + pageNum + "&user_chosen_results_per_page=" + results_per_page;
	
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	
	}













	function displayValance(obj)
	{

		var valance_edit = MM_findObj("valance_show_if_adding");

		if (obj.checked)
		{
			valance_edit.style.display = "block";

			copyFabric("curtain", "valance");

		}
		else
			valance_edit.style.display = "none";
			
			
		curtain("form1");
			
	}
	function displayTiebacks(obj)
	{

		var tiebacks_edit = MM_findObj("tiebacks_show_if_adding");

		if (obj.checked)
		{
			tiebacks_edit.style.display = "block";
		
			copyFabric("curtain", "tiebacks");
		
		}
		else
			tiebacks_edit.style.display = "none";
			
			
		curtain("form1");
			
	}




	function displayFabricSelection(fabricFor)
	{
		fabricForWhat = fabricFor;

		var fabric_selection = MM_findObj("fabric_selection");

		fabric_selection.style.display = "block";
		
		showHideComboBoxes("none");
		
		
			
		
	}
	
	function showHideComboBoxes(showOrHide)
	{
		//Hide any combo boxes, depending on what product we are on
		switch (fabricForWhat)
		{
			case "curtain":
				document.form1.liningc.style.display = showOrHide;
				break;
			case "romanblind":
			case "rollerblind":
				document.form1.blind_position.style.display = showOrHide;
				document.form1.lining.style.display = showOrHide;
				document.form1.chain.style.display = showOrHide;
				document.form1.chain_position.style.display = showOrHide;
				break;
			case "duvet":
				document.form1.duvet_size.style.display = showOrHide;
				break;
			
			
			
		}
		
	}

	function copyFabric(sourceFabricForWhat, destinationFabricForWhat)
	{
		var objInfoName_Source = MM_findObj("build_panel_material_info_name_" + sourceFabricForWhat);
		var objInfoPrice_Source = MM_findObj("build_panel_material_info_price_" + sourceFabricForWhat );

		var objInfoName_Dest = MM_findObj("build_panel_material_info_name_" + destinationFabricForWhat);
		var objInfoPrice_Dest = MM_findObj("build_panel_material_info_price_" + destinationFabricForWhat );

		objInfoName_Dest.innerHTML = objInfoName_Source.innerHTML;
		objInfoPrice_Dest.innerHTML = objInfoPrice_Source.innerHTML;

		eval("document.form1.fabricID_" + destinationFabricForWhat + ".value = document.form1.fabricID_" + sourceFabricForWhat + ".value;");
		eval("document.form1.fabricName_" + destinationFabricForWhat + ".value = document.form1.fabricName_" + sourceFabricForWhat + ".value;");
		eval("document.form1.fabricCostPerM_" + destinationFabricForWhat + ".value = document.form1.fabricCostPerM_" + sourceFabricForWhat + ".value;");
		eval("document.form1.fabricRepeat_" + destinationFabricForWhat + ".value = document.form1.fabricRepeat_" + sourceFabricForWhat + ".value;");

		eval("document.form1.fabricImage_" + destinationFabricForWhat + ".src = document.form1.fabricImage_" + sourceFabricForWhat + ".src;");


	}



	function hideFabricSelection(fabricForWhat)
	{
//		alert("hide");
		//Hide the fabric selection panel
		var fabric_selection = MM_findObj("fabric_selection");
		fabric_selection.style.display = "none";
		
		showHideComboBoxes("block");
			
		
	}

	function selectHeading(obj, code, name, priceUnlined, priceLined)
	{

		//Store values for use by curtain calculator
		document.form1.headingCode.value = code;
		document.form1.headingName.value = name;
		document.form1.headingPriceUnlined.value = priceUnlined;
		document.form1.headingPriceLined.value = priceLined;

		var objInfo = MM_findObj("build_panel_heading_info");
		objInfo.innerHTML = name;		


		//Calculate
		curtain("form1");
	}



	
	
	function enlargeFabric(obj, title, src, pricePerM, specNames, specValues)
	{
		
		var enlargePanel = MM_findObj("largefabric");
		var enlargePanelTitle = MM_findObj("largeFabric_title");
		var enlargePanelImage = MM_findObj("largeFabric_image");
		var enlargePanelSpec = MM_findObj("largeFabric_spec");
		var enlargePanelOrderSample = MM_findObj("largeFabric_orderSample");


		//Get page top of fabric to be enlarged and set enlargement to be near it
		var objPos = findPos(obj);
		var verticalPos = objPos[0] - 380;
		
		enlargePanel.style.top = (verticalPos + "px");

		enlargePanel.style.display = "block";
		enlargePanelTitle.innerHTML = title;
		enlargePanelImage.src = src;



		
		specText = "<table border='0'>";
		specText += "  <tr><td>Price:</td><td>&pound;" + pricePerM + " per metre</td></tr>";
		
		specNames = specNames.split("|");
		specValues = specValues.split("|");
		
		collectionName = "";
		hasCollection = false;

//alert(specValues);

		for (i=0; i < specNames.length; i++)
		{
			
			if ( specValues[i] != "")
			{
				specText += "  <tr><td>" + specNames[i] + ":</td><td>" + specValues[i] + "</td></tr>";
				
				if (specNames[i] == "Collection" && specValues[i] != "")
				{
					collectionName = specValues[i];
					hasCollection = true;
				}
			}
			
		}

//collectionName = "Orchard Farm";
		
		specText += "</table>";

		enlargePanelSpec.innerHTML = specText;

		enlargePanelOrderSample.innerHTML = "<img onclick='orderSample(\"" + title + "\", \"" + src + "\");' class='button_image'  src='iwa/images/buttons/order_sample_of_this_fabric.png' />";
		
		if (hasCollection)
			enlargePanelOrderSample.innerHTML += "<br/><br/><a href='fabric_collections.php?collectionName=" + collectionName + "'><img class='button_image'  src='iwa/images/buttons/view_collection.png' /></a>";
		
		
//		alert(specNames);

	}
	function hideLargeFabric()
	{
		var enlargePanel = MM_findObj("largefabric");
		enlargePanel.style.display = "none";
		
	}



	function displayHelpAjax(objHelpButton, helpCode)
	{

		//Position help box
		pos = findPos(objHelpButton);
		newTop = pos[0] + 10;
		newLeft = pos[1] + 30;
		
		var help_obj = MM_findObj("help");
		help_obj.style.top = parseInt(newTop) + "px"
		help_obj.style.left = parseInt(newLeft) + "px"
		help_obj.style.display = "block";				



		xmlHttp=GetXmlHttpObject();
		
		if (xmlHttp==null)
			return;
		
		var url="iwa/include/ajax/displayHelp.php?itemID=" + document.form1.thingID.value + "&helpCode=" + helpCode;
//alert(url);
		xmlHttp.onreadystatechange=ajax_response_displayHelp;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);		
		
	}

	function ajax_response_displayHelp() 
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			var help_obj = MM_findObj("help");
			var helptext_obj = MM_findObj("buildhelp_text");
			
			helptext_obj.innerHTML = xmlHttp.responseText;

			
		} 
	}




	function findPos(obj)
	{
		var atop = 0;
		var aleft = 0
	
		do 
		{
			atop += obj.offsetTop;
			aleft += obj.offsetLeft;
			
			
		}
		while (obj = obj.offsetParent)
		
		return [atop, aleft]
		
		
	}


	function displayHelp(obj, topOffset, helpText)
	{
		var help_obj = MM_findObj("help");
		var helptext_obj = MM_findObj("buildhelp_text");
		
		
		helptext_obj.innerHTML = helpText;
		
		help_obj.style.top = parseInt(topOffset) + "px"
		
/*		help_obj.style.top = (parseInt(obj.style.top) + 20) + "px";
		help_obj.style.left = (parseInt(obj.style.left) + 20) + "px";
*/
		help_obj.style.display = "block";
		

	}
	
	function hideHelp(obj)
	{
		var help = MM_findObj("help");
		help.style.display = "none";
	}
	