﻿var menu,model=0;
var ajax=(window.ActiveXObject)?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();
var profile,Flag,Menu;

function getMenu(p) {
    if (profile!=p) {
        Flag=p.split("@")[1];
        Menu=p.split("@")[0];
        ajax.open("POST", "xmlmenu.asp?flag="+Flag+"&menu="+Menu, true);
        ajax.send();
        profile=p;
    }
}

function blockExpandCollapse(o,n) {
	if (n==1) {
		otr=o.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.nextSibling.nextSibling;
		oImg=o;
	} else {
		otr=o.parentElement.parentElement.nextSibling;
		oImg=o.parentElement.parentElement.previousSibling.childNodes(0).childNodes(0).childNodes(0).childNodes(0).childNodes(0).childNodes(0);
	}
	if (otr.style.display=="none") {
		otr.style.display="block";
		oImg.src="http://imgs.bees.com.tw/product/collapse.gif"; oImg.title="閉合";
	}
	else {
		otr.style.display="none";
		oImg.src="http://imgs.bees.com.tw/product/expand.gif"; oImg.title="展開";
	}
}

function ResizeImage(img, style){
    var image=new Image();
    var mWidth,mHeight;
    switch (style) {
        case 1: mWidth=mHeight=566; break;
        case 2: mWidth=mHeight=272; break;
        case 3: mWidth=mHeight=174; break;
        case 4: mWidth=mHeight=125; break;
    }
    image.src=img.src;
    if(image.width > 0 && image.height > 0){
        if(image.width/image.height >= mWidth/mHeight){
            if(image.width > mWidth){ 
                img.width = mWidth;
                img.height = (image.height * mWidth)/image.width;
            }else{
                img.width=image.width; 
                img.height=image.height;
            }
        }else{
            if(image.height>mHeight){ 
                img.height = mHeight;
                img.width = (image.width * mHeight)/image.height; 
            }else{
                img.width=image.width; 
                img.height = image.height;
            }
        }
    }
}

function showResult(req) {
	var document=window.frames[0].document;
	var oMenu=document.createElement("div");
	var xmlDOM=req.responseXML;
	var ndRoot=xmlDOM.documentElement;
	
	for (var i=0; i<ndRoot.childNodes.length; i++) {
		var ndCategory=ndRoot.childNodes(i);
		var aryProperties=(ndCategory.getAttribute("properties")=="")?new Array():ndCategory.getAttribute("properties").split(",");
		
		//category
		var oTABLE=document.createElement("table"); oTABLE.width="100%"; oTABLE.border="0"; oTABLE.cellSpacing="5"; oTABLE.cellPadding="0";
		var oTR=oTABLE.insertRow();
		var oTD=oTR.insertCell();
		
			var oT=document.createElement("table"); oT.className="menu"; oT.width="100%"; oT.border="0"; oT.cellSpacing="2"; oT.cellPadding="0";
			var oTr=oT.insertRow();
			var oTd=oTr.insertCell();
				var ot=document.createElement("table"); ot.width="100%"; ot.border="0"; ot.cellSpacing="0"; ot.cellPadding="0";
				var otr=ot.insertRow();
				var otd=otr.insertCell(); otd.className="menu-categoryname"; otd.noWrap=true;
					var oInput=document.createElement("input"); oInput.type="image"; oInput.src=((ndCategory.getAttribute("expand")=="1")?"http://imgs.bees.com.tw/product/collapse.gif":"http://imgs.bees.com.tw/product/expand.gif"); oInput.title=((ndCategory.getAttribute("expand")=="1")?"閉合":"展開"); oInput.onclick=new Function("blockExpandCollapse(this,1)");
					otd.appendChild(oInput);
					var oSpan=document.createElement("span"); oSpan.Properties=ndCategory.getAttribute("properties"); oSpan.innerText=ndCategory.getAttribute("name");
					otd.appendChild(oSpan);
				var otd=otr.insertCell(); otd.width="2";
				var otd=otr.insertCell(); otd.className="menu-categorydesc"; otd.innerText=ndCategory.getAttribute("desc");
					var oImg=document.createElement("img"); oImg.src="http://imgs.bees.com.tw/product/blank.gif"; oImg.width="8";
					otd.appendChild(oImg);
				oTd.appendChild(ot);
				
			var oTr=oT.insertRow(); oTr.style.display=((ndCategory.getAttribute("show")=="1")?"block":"none");
			var oTd=oTr.insertCell(); oTd.className="menu-categorypicture";
				var oImg=document.createElement("img");	oImg.src=((ndCategory.getAttribute("upload")=="1")?"http://imgs.bees.com.tw/product/"+Flag+"/"+Menu+"/"+ndCategory.getAttribute("id")+".gif":"http://imgs.bees.com.tw/product/category.gif"); oImg.title="展開／閉合"; oImg.onclick=new Function("blockExpandCollapse(this,2)");
				oTd.appendChild(oImg);
			
			switch (model*2+((ndCategory.getAttribute("style")=="0")?0:1)) { //model+style
				case 0:
					var oTr=oT.insertRow(); oTr.style.display=((ndCategory.getAttribute("expand")=="1")?"block":"none");
					var oTd=oTr.insertCell(); oTd.className="menu-content";
						var ot=document.createElement("table"); ot.width="100%"; ot.border="0"; ot.cellSpacing="0"; ot.cellPadding="2";
						var otr=ot.insertRow();
						var oth=document.createElement("th"); oth.width="20"; oth.innerText="☆"; otr.appendChild(oth);
						var oth=document.createElement("th"); oth.align="left"; oth.innerText="產品名稱"; otr.appendChild(oth);
						var oth=document.createElement("th"); oth.innerText="金額"; otr.appendChild(oth);
						//product
						for (var ii=0; ii<ndCategory.childNodes.length; ii++) {
							var ndProduct=ndCategory.childNodes(ii);
							var otr=ot.insertRow();
							var otd=otr.insertCell(); otd.align="center"; //推薦商品
								if (ndProduct.getAttribute("commend")=="1") {var oImg=document.createElement("img"); oImg.src="http://imgs.bees.com.tw/product/star.gif"; otd.appendChild(oImg);} else {otd.innerText=" ";}
							var otd=otr.insertCell(); otd.innerText=ndProduct.getAttribute("name"); //產品名稱
								var discount=parseInt(ndProduct.getAttribute("discount")); if (discount!=0) otd.style.color="red"; //~~特價商品
								if (ndProduct.getAttribute("new")=="1") {var oImg=document.createElement("img"); oImg.src="http://imgs.bees.com.tw/product/new.gif"; otd.appendChild(oImg);} //~~新品上架
							var otd=otr.insertCell(); otd.align="center"; if (discount!=0) otd.style.color="red"; //金額
							    var price=ndProduct.getAttribute("price"); if (price.indexOf("*")==-1 && price.split(",").length!=1) price="*"+price; //給預設值
							    otd.innerText=tPRICE(price,discount);
						}
						oTd.appendChild(ot);
					break;
				case 1:
					var oTr=oT.insertRow(); oTr.style.display=((ndCategory.getAttribute("expand")=="1")?"block":"none");
					var oTd=oTr.insertCell();
						//product
						for (var ii=0; ii<ndCategory.childNodes.length; ii++) {
							var ndProduct=ndCategory.childNodes(ii);
							var oDiv=document.createElement("div"); oDiv.className="float";
							    var discount=parseInt(ndProduct.getAttribute("discount")); oDiv.style.border=((discount!=0)?"1px solid red":"1px solid gray"); //~~特價商品
								var price=ndProduct.getAttribute("price"); if (price.indexOf("*")==-1 && price.split(",").length!=1) price="*"+price; //給預設值
								var odiv=document.createElement("div"); odiv.className="style"+ndCategory.getAttribute("style")+"img";
									var ot=document.createElement("table"); ot.border="0"; ot.cellSpacing="0"; ot.cellPadding="0";
									var otr=ot.insertRow();
									var otd=otr.insertCell();
								        var oA=document.createElement("a"); oA.href=((ndProduct.getAttribute("upload")=="1")?"http://imgs.bees.com.tw/product/"+Flag+"/"+Menu+"/"+ndProduct.getAttribute("id")+".gif":"http://imgs.bees.com.tw/product/product.gif"); oA.rel="lightbox["+ndCategory.getAttribute("name")+"]"; oA.title=ndProduct.getAttribute("name")+" $ "+tPRICE(price,discount); oA.content=ndProduct.getAttribute("desc");
									        var oImg=document.createElement("img");
									            oImg.src=((ndProduct.getAttribute("upload")=="1")?"http://imgs.bees.com.tw/product/"+Flag+"/"+Menu+"/"+ndProduct.getAttribute("id")+"s.gif?"+Math.random():"http://imgs.bees.com.tw/product/products.gif");
									            oImg.alt=ndProduct.getAttribute("name")+" $ "+tPRICE(price,discount);
									            oImg.onload=new Function("ResizeImage(this,"+ndCategory.getAttribute("style")+")");
									        oA.appendChild(oImg);
									        var oBr=document.createElement("br");
									        oA.appendChild(oBr);
										otd.appendChild(oA);
									odiv.appendChild(ot);
								oDiv.appendChild(odiv);
								var odiv=document.createElement("div"); odiv.className="style"+ndCategory.getAttribute("style")+"txt";
									var ot=document.createElement("table"); ot.border="0"; ot.cellSpacing="0"; ot.cellPadding="0";
									var otr=ot.insertRow();
									if (ndProduct.getAttribute("commend")=="1") {var otd=otr.insertCell(); var oImg=document.createElement("img"); oImg.src="http://imgs.bees.com.tw/product/star.gif"; otd.appendChild(oImg);} //推薦商品
									if (ndProduct.getAttribute("new")=="1") {var otd=otr.insertCell(); var oImg=document.createElement("img"); oImg.src="http://imgs.bees.com.tw/product/new.gif"; otd.appendChild(oImg);} //~~新品上架
									var otd=otr.insertCell(); otd.noWrap=true;
										var oSpan=document.createElement("span"); oSpan.title=oSpan.innerText=ndProduct.getAttribute("name")+" $ "+tPRICE(price,discount);
										otd.appendChild(oSpan);
									odiv.appendChild(ot);
								oDiv.appendChild(odiv);
							oTd.appendChild(oDiv);
						}
					break;
				case 2:
					var oTr=oT.insertRow(); oTr.style.display=((ndCategory.getAttribute("expand")=="1")?"block":"none");
					var oTd=oTr.insertCell(); oTd.className="menu-content";
						var ot=document.createElement("table"); ot.width="100%"; ot.border="0"; ot.cellSpacing="0"; ot.cellPadding="2";
						var otr=ot.insertRow();
						var oth=document.createElement("th"); oth.width="20"; oth.innerText="☆"; otr.appendChild(oth);
						var oth=document.createElement("th"); oth.align="left"; oth.innerText="產品名稱"; otr.appendChild(oth);
						var oth=document.createElement("th"); oth.innerText="金額"; otr.appendChild(oth);
						for (var j=0; j<aryProperties.length; j++) {var oth=document.createElement("th"); oth.innerText=aryProperties[j]; otr.appendChild(oth);} //properties
						/*var oth=document.createElement("th"); oth.width="40"; oth.innerText="數量"; otr.appendChild(oth);
						var oth=document.createElement("th"); oth.width="40"; oth.innerText="訂購"; otr.appendChild(oth);*/
						//product
						for (var ii=0; ii<ndCategory.childNodes.length; ii++) {
							var ndProduct=ndCategory.childNodes(ii);
							var otr=ot.insertRow();
							var otd=otr.insertCell(); otd.align="center"; //推薦商品
								if (ndProduct.getAttribute("commend")=="1") {var oImg=document.createElement("img"); oImg.src="http://imgs.bees.com.tw/product/star.gif"; otd.appendChild(oImg);} else {otd.innerText=" ";}
							var otd=otr.insertCell(); otd.innerText=ndProduct.getAttribute("name"); //產品名稱
								var discount=parseInt(ndProduct.getAttribute("discount")); if (discount!=0) otd.style.color="red"; //~~特價商品
								if (ndProduct.getAttribute("new")=="1") {var oImg=document.createElement("img"); oImg.src="http://imgs.bees.com.tw/product/new.gif"; otd.appendChild(oImg);} //~~新品上架
							var otd=otr.insertCell(); otd.align="center"; if (discount!=0) otd.style.color="red"; //金額
							    var price=ndProduct.getAttribute("price"); if (price.indexOf("*")==-1 && price.split(",").length!=1) price="*"+price; //給預設值
							    var aryprice=price.split(",");
							    if (aryprice.length==1) { //單一價格 20
								    if (discount!=0) { //~~特價商品
									    otd.innerText=price-discount;
								    } else {
									    otd.innerText=price;
								    }
							    } else { //多重價格 *大:30,中:25,小:20
							        for (j=0; j<aryprice.length; j++) {
						                if (aryprice[j].split(":")[0].substr(0,1)=="*") {
							                var oInput=document.createElement("<input type='radio' name='"+ndProduct.getAttribute("name")+"' checked>");
						                    oInput.id=aryprice[j].split(":")[0].substr(1);
						                } else {
							                var oInput=document.createElement("<input type='radio' name='"+ndProduct.getAttribute("name")+"'>");
						                    oInput.id=aryprice[j].split(":")[0];
						                }
							                oInput.value=((discount!=0)?parseInt(aryprice[j].split(":")[1])-discount:parseInt(aryprice[j].split(":")[1]));
							                otd.appendChild(oInput);
							            var oText=document.createTextNode(oInput.id+":"+oInput.value);
							                otd.appendChild(oText);
							        }
							    }
							//properties
							for (var jj=0; jj<aryProperties.length; jj++) {
								var ndProperty=ndProduct.selectSingleNode("property[@name='"+aryProperties[jj]+"']")
								var otd=otr.insertCell(); otd.align="center";
								if (ndProperty) {
									var oSelect=document.createElement("select"); oSelect.Name=ndProperty.getAttribute("name"); oSelect.Type=ndProperty.getAttribute("type");
										if (ndProperty.getAttribute("type")=="addition") {var oOption=document.createElement("option"); oOption.value="- - ,+0"; oOption.innerText="- - ,+0"; oSelect.appendChild(oOption);}
									for (var k=0; k<ndProperty.childNodes.length; k++) {
										var ndItem=ndProperty.childNodes(k);
										var oOption=document.createElement("option"); oOption.innerText=((ndProperty.getAttribute("type")=="addition")?oOption.value=ndItem.text+",+"+ndItem.getAttribute("price"):oOption.value=ndItem.text); oSelect.appendChild(oOption);
									}
									otd.appendChild(oSelect);
								} else {
									var oImg=document.createElement("img"); oImg.src="http://imgs.bees.com.tw/product/blank.gif";
									otd.appendChild(oImg);
								}
							}
							/*var otd=otr.insertCell(); otd.align="center"; //數量
								var oInput=document.createElement("input"); oInput.type="text"; oInput.className="input-amount"; oInput.size="4"; oInput.value="1"; oInput.maxLength="2"; oInput.onfocus=new Function("this.select()");
								otd.appendChild(oInput);
							var otd=otr.insertCell(); otd.align="center"; //訂購
								var oInput=document.createElement("input"); oInput.type="image"; oInput.src="http://imgs.bees.com.tw/product/cart.gif"; oInput.onclick=new Function("window.top.buy(this,2)");
								otd.appendChild(oInput);*/
						}
						oTd.appendChild(ot);
					break;
				case 3:
					var oTr=oT.insertRow(); oTr.style.display=((ndCategory.getAttribute("expand")=="1")?"block":"none");
					var oTd=oTr.insertCell();
						var ot=document.createElement("table"); ot.width="100%"; ot.border="0"; ot.cellSpacing="0"; ot.cellPadding="2";
						//product
						for (var ii=0; ii<ndCategory.childNodes.length; ii++) {
							var ndProduct=ndCategory.childNodes(ii);
							var otr=ot.insertRow();
							var otd=otr.insertCell(); otd.className="menu-item";
								var oDiv=document.createElement("div"); oDiv.className="float";
								    var discount=parseInt(ndProduct.getAttribute("discount")); oDiv.style.border=((discount!=0)?"1px solid red":"1px solid gray"); //~~特價商品
								    var price=ndProduct.getAttribute("price"); if (price.indexOf("*")==-1 && price.split(",").length!=1) price="*"+price; //給預設值
									var oA=document.createElement("a"); oA.href=((ndProduct.getAttribute("upload")=="1")?"http://imgs.bees.com.tw/product/"+Flag+"/"+Menu+"/"+ndProduct.getAttribute("id")+".gif":"http://imgs.bees.com.tw/product/product.gif"); oA.rel="lightbox["+ndCategory.getAttribute("name")+"]"; oA.title=ndProduct.getAttribute("name")+" $ "+tPRICE(price,discount); oA.content=ndProduct.getAttribute("desc");
										var oImg=document.createElement("img"); oImg.src=((ndProduct.getAttribute("upload")=="1")?"http://imgs.bees.com.tw/product/"+Flag+"/"+Menu+"/"+ndProduct.getAttribute("id")+"s.gif":"http://imgs.bees.com.tw/product/products.gif"); oImg.alt=ndProduct.getAttribute("name")+" $ "+tPRICE(price,discount);
										oA.appendChild(oImg);
									oDiv.appendChild(oA);
								otd.appendChild(oDiv);
								var oDiv=document.createElement("div"); oDiv.className="float";
									if (ndProduct.getAttribute("commend")=="1") {var oImg=document.createElement("img"); oImg.src="http://imgs.bees.com.tw/product/star.gif"; oDiv.appendChild(oImg);} //推薦商品
									if (ndProduct.getAttribute("new")=="1") {var oImg=document.createElement("img"); oImg.src="http://imgs.bees.com.tw/product/new.gif"; oDiv.appendChild(oImg);} //~~新品上架
								otd.appendChild(oDiv);
								var oDiv=document.createElement("div"); oDiv.className="container";
									var otable=document.createElement("table"); otable.width="40%"; otable.border="0"; otable.cellSpacing="4"; otable.cellPadding="0";
									var otR=otable.insertRow();
									var otD=otR.insertCell(); otD.colSpan="2";
										var oSpan=document.createElement("span"); oSpan.innerText=ndProduct.getAttribute("name");
										otD.appendChild(oSpan);
										var oSpan=document.createElement("span"); oSpan.innerText=" $ ";
										otD.appendChild(oSpan);
										var oSpan=document.createElement("span");
							                var aryprice=price.split(",");
							                if (aryprice.length==1) { //單一價格 20
								                if (discount!=0) { //~~特價商品
									                oSpan.innerText=price-discount;
								                } else {
									                oSpan.innerText=price;
								                }
							                } else { //多重價格 *大:30,中:25,小:20
							                    for (j=0; j<aryprice.length; j++) {
						                            if (aryprice[j].split(":")[0].substr(0,1)=="*") {
							                            var oInput=document.createElement("<input type='radio' name='"+ndProduct.getAttribute("name")+"' checked>");
						                                oInput.id=aryprice[j].split(":")[0].substr(1);
						                            } else {
							                            var oInput=document.createElement("<input type='radio' name='"+ndProduct.getAttribute("name")+"'>");
						                                oInput.id=aryprice[j].split(":")[0];
						                            }
							                            oInput.value=((discount!=0)?parseInt(aryprice[j].split(":")[1])-discount:parseInt(aryprice[j].split(":")[1]));
							                            oSpan.appendChild(oInput);
							                        var oText=document.createTextNode(oInput.id+":"+oInput.value);
							                            oSpan.appendChild(oText);
							                    }
							                }
										otD.appendChild(oSpan);
										var odiv=document.createElement("div"); odiv.innerText=ndProduct.getAttribute("desc");
										otD.appendChild(odiv);
									//properties
									for (var jj=0; jj<aryProperties.length; jj++) {
										var ndProperty=ndProduct.selectSingleNode("property[@name='"+aryProperties[jj]+"']")
										if (ndProperty==null) {
											var otR=otable.insertRow(); otR.style.display="none";
											var otD=otR.insertCell();
											var otD=otR.insertCell();
										} else {
											var otR=otable.insertRow();
											var otD=otR.insertCell(); otD.className="menu-title"; otD.width="20%"; otD.noWrap=true; otD.innerText=ndProperty.getAttribute("name");
											var otD=otR.insertCell();
												var oSelect=document.createElement("select"); oSelect.Name=ndProperty.getAttribute("name"); oSelect.Type=ndProperty.getAttribute("type");
													if (ndProperty.getAttribute("type")=="addition") {var oOption=document.createElement("option"); oOption.value="- - ,+0"; oOption.innerText="- - ,+0"; oSelect.appendChild(oOption);}
												for (var k=0; k<ndProperty.childNodes.length; k++) {
													var ndItem=ndProperty.childNodes(k);
													var oOption=document.createElement("option"); oOption.innerText=((ndProperty.getAttribute("type")=="addition")?oOption.value=ndItem.text+",+"+ndItem.getAttribute("price"):oOption.value=ndItem.text); oSelect.appendChild(oOption);
												}
												otD.appendChild(oSelect);
										}
									}
									/*var otR=otable.insertRow();
									var otD=otR.insertCell(); otD.colSpan="2"; otD.align="right";
										var ott=document.createElement("table"); ott.border="0"; ott.cellSpacing="4"; ott.cellPadding="0";
										var ottr=ott.insertRow();
										var ottd=ottr.insertCell(); ottd.innerText="數量";
										var ottd=ottr.insertCell();
											var oInput=document.createElement("input"); oInput.type="text"; oInput.className="input-amount"; oInput.size="4"; oInput.value="1"; oInput.maxLength="2"; oInput.onfocus=new Function("this.select()");
											ottd.appendChild(oInput);
										var ottd=ottr.insertCell();
											var oInput=document.createElement("input"); oInput.type="image"; oInput.src="http://imgs.bees.com.tw/product/cart.gif"; oInput.onclick=new Function("window.top.buy(this,3)");
											ottd.appendChild(oInput);
										otD.appendChild(ott);*/
									oDiv.appendChild(otable);
								otd.appendChild(oDiv);
						}
						oTd.appendChild(ot);
					break;
			}
			
			oTD.appendChild(oT);
			
		oMenu.appendChild(oTABLE);
	}
	menu=oMenu;
}
function tPRICE(price,discount) {
    var aryprice=price.split(",");
    if (aryprice.length==1) { //單一價格 20
	    if (discount!=0) { //~~特價商品
		    return price-discount;
	    } else {
		    return price;
	    }
    } else { //多重價格 *大:30,中:25,小:20
	    if (discount!=0) { //~~特價商品
	        for (j=0; j<aryprice.length; j++) {
		        aryprice[j]=aryprice[j].split(":")[0]+":"+(parseInt(aryprice[j].split(":")[1])-discount);
	        }
	        return aryprice.join(",");
	    } else {
		    return price;
	    }
    }
}