// JavaScript Document

// Should be reset "GlobalCategoryName"  and "GlobalAttributeName" every time when Category name is change (Option button click time)
//var GlobalDomainName = ""; 
//var GlobalCategoryName = ""; 
//var GlobalAttributeName = ""; 
var item_cdLIST = ""; // if attribute is uncheck then previous value is collected from here.
var item_cd_NO = "";// if attribute is uncheck then previous value is collected from here.
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	  {
		  // Firefox, Opera 8.0+, Safari
		  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
		  // Internet Explorer
		  try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
		  catch (e)
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
	  }
	return xmlHttp;
}
/*
function f_sub()
{
	//document.getElementById("UrlValue").value = "?d="+GlobalDomainName;
	//document.getElementById("UrlValue").value += "?c1="+GlobalCategoryName;
	//document.getElementById("UrlValue").value += "?a="+GlobalAttributeName;
	
	document.main.submit();
}

$GlobalDomainName = ltrim(rtrim($_POST['GlobalDomainName']));  // "http://www.kagoo-diningroom.com/";
$GlobalCategoryName = ltrim(rtrim($_POST['GlobalCategoryName']));
$GlobalAttributeName = ltrim(rtrim($_POST['GlobalAttributeName']));
if (empty($GlobalAttributeName)) {
	header("Location: http://www.kagoo-".$GlobalDomainName.".com/search/index.php?c1=".$GlobalCategoryName);
	exit();
} else {
	header("Location: ./search/index.php?d=".$GlobalDomainName."&c1=".$GlobalCategoryName."&a=".$GlobalAttributeName);
	exit();
}

*/
/*
function urlRedirect() {
	if (document.getElementById("GlobalAttributeName").value == ""){
		if (document.getElementById("GlobalAttributeName").value == ""){
			
		} else {
		document.main.action="http://www.kagoo-"+ document.getElementById("GlobalDomainName").value +".com/search/index.php?c1="+ document.getElementById("GlobalCategoryName").value ; 
		document.main.submit();
		}
	} else {
		document.main.action="./search/index.php"; 
		document.main.submit();
	}
		
}
*/
function ResetHtml(ToCloseHtml) {
	/*
	function where new or ajax code is updating html is same.
	ie if function name is "Diningroom" then html tag DIV id is also should be "Diningroom" 
	otherwise it will not work
	
	because previous opened "html tag DIV name" is save in the variable "document.getElementById("GlobalDomainName").value"
	now just get the value and set as empty and draw new one..
	*/
	if (ToCloseHtml =="diningroom") { 
		document.getElementById("Diningroom").innerHTML = "";//document.getElementById("BlankHTML").value;
		document.getElementById("DiningDetails_Button").innerHTML = "";
	}else if (ToCloseHtml =="forchildren") {
		document.getElementById("Forchildren").innerHTML = "";
		document.getElementById("Forchildren_Button").innerHTML= ""; 
	}else if (ToCloseHtml =="exterior") {
		document.getElementById("Exteria").innerHTML = "";
		document.getElementById("Exteria_Button").innerHTML= ""; 
	}else if (ToCloseHtml =="rattan") {
		document.getElementById("Rattan").innerHTML = "";
		document.getElementById("Rattan_Button").innerHTML= ""; 
	}else if (ToCloseHtml =="oa") {
		document.getElementById("Oa").innerHTML = "";
		document.getElementById("Oa_Button").innerHTML= ""; 
	}else if (ToCloseHtml =="bedroom") {
		document.getElementById("Bedroom").innerHTML = "";
		document.getElementById("Bedroom_Button").innerHTML= ""; 
	}else if (ToCloseHtml =="livingroom") {
		document.getElementById("Livingroom").innerHTML = "";
		document.getElementById("Livingroom_Button").innerHTML= ""; 
	}else if (ToCloseHtml =="cabinet") {
		document.getElementById("Cabinet").innerHTML = "";
		document.getElementById("Cabinet_Button").innerHTML= ""; 
	}
	
	/*
	// to unset the count portion div id = (domainname+item_cd_Display)
	if (ToCloseHtml != "") {
		divName = ToCloseHtml+"item_cd_Display";
		document.getElementById(divName).innerHTML = "";
	}
	*/
}
function ClearAll(ToCloseHtml) {
	for (var i=0; i<document.main.category_d.length; i++)
		document.main.category_d[i].checked = false;
	UnsetRoomNew();	
	ResetHtml(ToCloseHtml);
}

function Diningroom (CategoryName) {
	
	if(document.getElementById("GlobalDomainName").value != "diningroom") {
		ResetHtml(document.getElementById("GlobalDomainName").value);
	}
	UnsetRoomNew();
	document.getElementById("GlobalDomainName").value="diningroom";
	document.getElementById("GlobalCategoryName").value = CategoryName;
	document.getElementById("GlobalAttributeName").value = "";
	
	if (CategoryName.length==0)
		  { 
			  document.getElementById("Diningroom").innerHTML="";
			  return;
		  }
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  } 
	var url="http://www.kagoo-diningroom.com/MainSearchPage/Template/Dining/dining.php";
	url=url+"?c1="+CategoryName;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange = function () { 
		if (xmlHttp.readyState==4)
		{ 
			document.getElementById("Diningroom").innerHTML=xmlHttp.responseText;
			document.getElementById("DiningDetails_Button").innerHTML='<div class="search_button"><p><INPUT TYPE="image" SRC="images/search_button2.jpg" ALT="検索" WIDTH="126" HEIGHT="29"></p></div>';
		}
		
	};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function CheckRoom() {
	//alert("sdfsd");
	if (document.getElementById("CheckboxRoom").value == "Y")
		document.getElementById("CheckboxRoom").value = "N";
	else
		document.getElementById("CheckboxRoom").value = "Y";
}
function CheckNewItem() {
	if (document.getElementById("CheckboxNewItem").value == "New")
		document.getElementById("CheckboxNewItem").value = "ALL";
	else
		document.getElementById("CheckboxNewItem").value = "New";
//	alert(document.getElementById("CheckboxNewItem").value );
}
function UnsetRoomNew() {
	
	// whenever Category is selected reset the newItem and room check box.
	document.getElementById("CheckboxRoom").value = "N";
	for (var i=0; i<document.main.room.length; i++)
		document.main.room[i].checked = false;
	
	document.getElementById("CheckboxNewItem").value = "ALL";
	for (var i=0; i<document.main.newItem.length; i++)
		document.main.newItem[i].checked = false;
	/*
	// to unset the count portion div id = (domainname+item_cd_Display)
	if (document.getElementById("GlobalDomainName").value != "") {
		divName = document.getElementById("GlobalDomainName").value+"item_cd_Display";
		document.getElementById(divName).innerHTML = "";
	}
	*/
}


function Checkbox(AttributeName){
	// AttributeNO is check and it 'Two' then send all attribute list by URL
	var AttributeNO = "";
	//'-sp' is seperator string and in php by this char , array is generated.
	AttributeName = "-sp"+AttributeName; 
	if (document.getElementById("GlobalAttributeName").value.match(AttributeName)) {
		document.getElementById("GlobalAttributeName").value = document.getElementById("GlobalAttributeName").value.replace(AttributeName, "");
		AttributeNO = "Two"; // when UNcheck then need to compare with all.
	}	
	else {
		// For 1st 2 attribute we will send array 
		if (document.getElementById("GlobalAttributeName").value == "")
			AttributeNO = "Two";
			
		document.getElementById("GlobalAttributeName").value+=AttributeName;
	
	}
	//alert (document.getElementById("GlobalAttributeName").value);
	// for count item no 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		  alert ("Your browser does not support AJAX!");
		  return;
	} 
	var url="http://www.kagoo-diningroom.com/MainSearchPage/attributeList.php";
	
	var postValue = "c1=" + escape(encodeURI(document.getElementById("GlobalCategoryName").value ));

//	+"&mytextarea2=" +escape(encodeURI( document.getElementById("mytextarea2").value ));


	//url=url+"?c1="+document.getElementById("GlobalCategoryName").value;
	//alert (xmlHttp.responseText);
	//alert(document.getElementById(DivName).value);
	//For 1st 2 attribute we will send array or when uncheck the check box.
	if ((AttributeNO == "Two") || (item_cd_NO == "NotFound")) {
	//	alert(document.getElementById("GlobalAttributeName").value);
		postValue += "&a=" + escape(encodeURI(document.getElementById("GlobalAttributeName").value )); 
		//url=url+"&a="+document.getElementById("GlobalAttributeName").value;
	//	item_cd_NO ="";
	}
	else {
		//url=url+"&a="+AttributeName;
		postValue += "&a=" + escape(encodeURI(AttributeName)); 
		postValue += "&item_cd=" + escape(encodeURI(document.getElementById("item_cd").value )); 
		//url=url+"&item_cd="+document.getElementById("item_cd").value;
	}
	postValue += "&GlobalDomainName=" + escape(encodeURI(document.getElementById("GlobalDomainName").value )); 
	//url=url+"&GlobalDomainName="+document.getElementById("GlobalDomainName").value;
	postValue += "&CheckboxRoom=" + escape(encodeURI(document.getElementById("CheckboxRoom").value )); 
	//url=url+"&CheckboxRoom="+document.getElementById("CheckboxRoom").value;
	postValue += "&CheckboxNewItem=" + escape(encodeURI(document.getElementById("CheckboxNewItem").value )); 

	//url=url+"&CheckboxNewItem="+document.getElementById("CheckboxNewItem").value;
	
	//url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange = function () { 
		if (xmlHttp.readyState==4)
		{ 
			//alert(xmlHttp.responseText);
			DivName =document.getElementById("GlobalDomainName").value+"item_cd_Display";// "forchildrenitem_cd_Display" ;
			if (xmlHttp.responseText.length==0)	{  
				document.getElementById(DivName).innerHTML='<p style="color:#FF0000">商品が登録されていません</p>';
				item_cd_NO = "NotFound";
				document.getElementById("item_cd").value = "";
				// to send attribute array by URL
				//AttributeNO == "Two"
				// submit button have to disable
				
				return;
			} else { 
				var item_cdArray = xmlHttp.responseText.split("__Item_Total_NO__"); 
				item_cdLIST = item_cdArray[0]; 
				item_cd_NO = item_cdArray[1]; 
				if ((item_cdLIST != null)||(item_cd_NO!=null)) {
					document.getElementById("item_cd").value = item_cdLIST;
					document.getElementById(DivName).innerHTML = '<p>(全 '+ item_cd_NO +' 商品)</p>';
				} else {
					document.getElementById(DivName).innerHTML='<p style="color:#FF0000">商品が登録されていません</p>';
					item_cd_NO = "NotFound";
					document.getElementById("item_cd").value = "";
				}
				
			}
		}                                                          
		
	};
//	alert (postValue);
//	xmlHttp.open("GET",url,true);
//	xmlHttp.send(null);
	xmlHttp.open('POST', url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", postValue.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(postValue);

}
function Forchildren(CategoryName) {
	if(document.getElementById("GlobalDomainName").value != "forchildren") {
		ResetHtml(document.getElementById("GlobalDomainName").value);
	}
	document.getElementById("GlobalDomainName").value="forchildren";
	document.getElementById("GlobalCategoryName").value = CategoryName;
	document.getElementById("GlobalAttributeName").value = "";
	UnsetRoomNew();
	if (CategoryName.length==0)
		  { 
			  document.getElementById("Forchildren").innerHTML="";
			  return;
		  }
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  } 

	var url="http://www.kagoo-diningroom.com/MainSearchPage/Template/forchildren/forchildren.php";
	url=url+"?c1="+CategoryName;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange = function () { 
		if (xmlHttp.readyState==4) { 
			document.getElementById("Forchildren").innerHTML=xmlHttp.responseText;
			
			document.getElementById("Forchildren_Button").innerHTML='<div class="search_button"><p><INPUT TYPE="image" SRC="images/search_button2.jpg" ALT="検索" WIDTH="126" HEIGHT="29"></p></div>';
		}
	};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}
/*
c1=chair&a=-spad0-color-spad2-wood-spad21-fabric-spad21-leather&GlobalDomainName=diningroom&CheckboxRoom=N&CheckboxNewItem=ALL

c1=chair&a=-spad21-leather&item_cd=%273741%27%2C%273793%27%2C%273896%27%2C%273953%27%2C%273954%27%2C%274140%27%2C%274203%27%2C%274206%27%2C%274251%27&GlobalDomainName=diningroom&CheckboxRoom=N&CheckboxNewItem=ALL


-spad0-color-spad2-wood-spad21-fabric-spad21-leather
c1=chair&a=-spad0-color-spad2-wood-spad21-fabric-spad21-leather&GlobalDomainName=diningroom&CheckboxRoom=N&CheckboxNewItem=ALL
*/
function Exteria(CategoryName) {
	if(document.getElementById("GlobalDomainName").value != "exterior") {
		ResetHtml(document.getElementById("GlobalDomainName").value);
	}
	document.getElementById("GlobalDomainName").value="exterior";
	document.getElementById("GlobalCategoryName").value = CategoryName;
	document.getElementById("GlobalAttributeName").value = "";
	UnsetRoomNew();
	if (CategoryName.length==0)
		  { 
			  document.getElementById("Exteria").innerHTML="";
			  return;
		  }
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  } 

	var url="http://www.kagoo-diningroom.com/MainSearchPage/Template/Exteria/exteria.php";
	url=url+"?c1="+CategoryName;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange = function () { 
		if (xmlHttp.readyState==4) { 
			document.getElementById("Exteria").innerHTML=xmlHttp.responseText;
			
			document.getElementById("Exteria_Button").innerHTML='<div class="search_button"><p><INPUT TYPE="image" SRC="images/search_button2.jpg" ALT="検索" WIDTH="126" HEIGHT="29"></p></div>';
			
		}
	};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function Rattan(CategoryName) {
	if(document.getElementById("GlobalDomainName").value != "rattan") {
		ResetHtml(document.getElementById("GlobalDomainName").value);
	}
	document.getElementById("GlobalDomainName").value="rattan";
	document.getElementById("GlobalCategoryName").value = CategoryName;
	document.getElementById("GlobalAttributeName").value = "";
	UnsetRoomNew();
	if (CategoryName.length==0)
		  { 
			  document.getElementById("Rattan").innerHTML="";
			  return;
		  }
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  } 

	var url="http://www.kagoo-diningroom.com/MainSearchPage/Template/Rattan/rattan.php";
	url=url+"?c1="+CategoryName;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange = function () { 
		if (xmlHttp.readyState==4) { 
			document.getElementById("Rattan").innerHTML=xmlHttp.responseText;
			
			document.getElementById("Rattan_Button").innerHTML='<div class="search_button"><p><INPUT TYPE="image" SRC="images/search_button2.jpg" ALT="検索" WIDTH="126" HEIGHT="29"></p></div>';
		}
	};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}
function Oa(CategoryName) {
	if(document.getElementById("GlobalDomainName").value != "oa") {
		ResetHtml(document.getElementById("GlobalDomainName").value);
	}
	document.getElementById("GlobalDomainName").value="oa";
	document.getElementById("GlobalCategoryName").value = CategoryName;
	document.getElementById("GlobalAttributeName").value = "";
	UnsetRoomNew();
	if (CategoryName.length==0)
		  { 
			  document.getElementById("Oa").innerHTML="";
			  return;
		  }
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  } 

	var url="http://www.kagoo-diningroom.com/MainSearchPage/Template/Oa/oa.php";
	url=url+"?c1="+CategoryName;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange = function () { 
		if (xmlHttp.readyState==4) { 
			document.getElementById("Oa").innerHTML=xmlHttp.responseText;
			
			document.getElementById("Oa_Button").innerHTML='<div class="search_button"><p><INPUT TYPE="image" SRC="images/search_button2.jpg" ALT="検索" WIDTH="126" HEIGHT="29"></p></div>';
		}
	};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function Bedroom(CategoryName) {
	if(document.getElementById("GlobalDomainName").value != "bedroom") {
		ResetHtml(document.getElementById("GlobalDomainName").value);
	}
	document.getElementById("GlobalDomainName").value="bedroom";
	document.getElementById("GlobalCategoryName").value = CategoryName;
	document.getElementById("GlobalAttributeName").value = "";
	UnsetRoomNew();
	if (CategoryName.length==0)
		  { 
			  document.getElementById("Bedroom").innerHTML="";
			  return;
		  }
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  } 

	var url="http://www.kagoo-diningroom.com/MainSearchPage/Template/Bedroom/bedroom.php";
	url=url+"?c1="+CategoryName;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange = function () { 
		if (xmlHttp.readyState==4) { 
			document.getElementById("Bedroom").innerHTML=xmlHttp.responseText;
			
			document.getElementById("Bedroom_Button").innerHTML='<div class="search_button"><p><INPUT TYPE="image" SRC="images/search_button2.jpg" ALT="検索" WIDTH="126" HEIGHT="29"></p></div>';
		}
	};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function Livingroom(CategoryName) {
	if(document.getElementById("GlobalDomainName").value != "livingroom") {
		ResetHtml(document.getElementById("GlobalDomainName").value);
	}
	document.getElementById("GlobalDomainName").value="livingroom";
	document.getElementById("GlobalCategoryName").value = CategoryName;
	document.getElementById("GlobalAttributeName").value = "";
	UnsetRoomNew();
	if (CategoryName.length==0)
		  { 
			  document.getElementById("Livingroom").innerHTML="";
			  return;
		  }
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  } 

	var url="http://www.kagoo-diningroom.com/MainSearchPage/Template/Livingroom/livingroom.php";
	url=url+"?c1="+CategoryName;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange = function () { 
		if (xmlHttp.readyState==4) { 
			document.getElementById("Livingroom").innerHTML=xmlHttp.responseText;
			
			document.getElementById("Livingroom_Button").innerHTML='<div class="search_button"><p><INPUT TYPE="image" SRC="images/search_button2.jpg" ALT="検索" WIDTH="126" HEIGHT="29"></p></div>';
		}
	};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function Cabinet(CategoryName) {
	if(document.getElementById("GlobalDomainName").value != "cabinet") {
		ResetHtml(document.getElementById("GlobalDomainName").value);
	}
	document.getElementById("GlobalDomainName").value="cabinet";
	document.getElementById("GlobalCategoryName").value = CategoryName;
	document.getElementById("GlobalAttributeName").value = "";
	UnsetRoomNew();
	if (CategoryName.length==0)
		  { 
			  document.getElementById("Cabinet").innerHTML="";
			  return;
		  }
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  } 

	var url="http://www.kagoo-diningroom.com/MainSearchPage/Template/Cabinet/cabinet.php";
	url=url+"?c1="+CategoryName;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange = function () { 
		if (xmlHttp.readyState==4) { 
			document.getElementById("Cabinet").innerHTML=xmlHttp.responseText;
			
			document.getElementById("Cabinet_Button").innerHTML='<div class="search_button"><p><INPUT TYPE="image" SRC="images/search_button2.jpg" ALT="検索" WIDTH="126" HEIGHT="29"></p></div>';
		}
	};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}


