// JavaScript Document

//var timemenu;
var subname;
var toolname;
var menutags;
var subtags;
var subbid;
var sublastflag;
var subflag;
var flag;
var viewname;
var xmlHttpT;
var styleInput;
var styleListDiv;
var styleFlag = false;
var page = 0;
var tid = "";

window.onload = function() {
	document.stylesearch.keywords.focus();
}
/************************************************************************************* SHOW SUB LIST FUNCTION **/
function showlist(menu, bid) {
	subname = document.getElementById(bid);
	var pos = getXY(menu);
	//alert(subname);
	//alert("left:"+pos.left+"|top:"+pos.top);
	if(subname != null) {
		subname.style.display = "block";
		if(pos.left > 790) {
			//alert(subname.offsetWidth);
			subname.style.left = document.getElementById("menu").offsetWidth - subname.offsetWidth + "px";
		} else {
			subname.style.left = pos.left + "px";
		}
		subname.style.top = pos.top + 33 + "px";
	}
}
function hidelist(bid) {
	//timemenu = setTimeout("hidesublist()", 300);
	if(subname != null) {
		subname.style.display = "none";
	}
	subbid = bid;
}
function getXY(Obj) {
	for (var sumTop=0, sumLeft=0; Obj!=document.body; sumTop+=Obj.offsetTop, sumLeft+=Obj.offsetLeft, Obj=Obj.offsetParent);
	return {left:sumLeft, top:sumTop}
}
/************************************************************************************* MOUSE ON SUB LIST FUNCTION **/
function showsublist() {
	//clearTimeout(timemenu);
	subname.style.display = "block";
	menutags = document.getElementById("menutable").getElementsByTagName("td");
	if(subbid.className == "menucurrent menuline") {
		subflag = subbid;
	}
	if(subbid.className == "menucurrent menulast") {
		sublastflag = subbid;
	}
	if(document.getElementById("menutable").rows[0].cells[menutags.length].className == "menucurrent menulast"){
		flag = true;
	}
	if(subbid.cellIndex == menutags.length) {
		subbid.className = "subcurrent menulast";
	} else {
		subbid.className = "subcurrent menuline";
	}
}
function hidesublist(obj, e) {
	//if (e.currentTarget) {
	//	if (obj.contains(e.relatedTarget)) {
	//		return;
	//	}
	//} else {
	//	if (obj.contains(e.toElement)) {
	//		return;
	//	}
	//}
	for(i=0; i<menutags.length; i++) {
		menutags[i].className = "menuline";
	}
	if(subflag != null) {
		subflag.className = "menucurrent menuline";
	}
	if(sublastflag != null) {
		sublastflag.className = "menucurrent menulast";
	}
	if(!flag) {
		menutags[menutags.length-1].className = "menulast";
	}
	
	subname.style.display = "none";
}
/************************************************************************************* SHOW DETAIL OPTIONS FUNCTION **/
function showdetail(thisdiv, thisli) {
	var divDetailinfo = document.getElementById("detailinfo");
	var divPacking = document.getElementById("packing");
	var divChartsize = document.getElementById("chartsize");
	var divCreateflyer = document.getElementById("createflyer");
	var divGetsample = document.getElementById("getsample");
	var divEmailfriend = document.getElementById("emailfriend");
	var divThis = document.getElementById(thisdiv);
	if(divDetailinfo && divPacking && divChartsize && divCreateflyer && divGetsample && divEmailfriend && divThis) {
		toolname = thisdiv;
		var tags = document.getElementById("detailtool").getElementsByTagName("li");
		for(i=0; i<tags.length; i++) {
			tags[i].className = "mainrightline";
		}
		//tags[tags.length-1].className = "mainrightlast";
		//if(tags[tags.length-1] == thisli) {
		//	thisli.className = "mainrightcurlast";
		//} else {
			thisli.className = "mainrightcurrent";
		//}
		
		divDetailinfo.style.display = "none";
		divPacking.style.display = "none";
		divChartsize.style.display = "none";
		divCreateflyer.style.display = "none";
		divGetsample.style.display = "none";
		divEmailfriend.style.display = "none";
		divThis.style.display = "block";
		
		if(toolname == "createflyer" && document.getElementById("contact")) {
			document.flyerform.contact.focus();
		} else if(toolname == "getsample" && document.getElementById("o_quantity")) {
			//document.sampleform.o_quantity.focus();
		} else if(toolname == "emailfriend" && document.getElementById("mailto")) {
			document.sentmailform.mailto.focus();
		}
	}
}
/************************************************************************************* SET SAMPLE FORM FROM FLYER FORM **/
function getflyerform() {
	//alert(document.readyState);
	if(document.readyState == "complete") {
		var flyerform = document.flyerform;
		var sampleform = document.sampleform;
		for(i=0; i<9; i++) {
			if(sampleform.elements[i+10].value == ""){
				sampleform.elements[i+10].value = flyerform.elements[i].value;
			}
		}
	}
}
function getsampleform() {
	if(document.readyState == "complete") {
		var sampleform = document.sampleform;
		var flyerform = document.flyerform;
		for(i=0; i<9; i++) {
			if(flyerform.elements[i].value == "" && sampleform.elements[i+10]){
				flyerform.elements[i].value = sampleform.elements[i+10].value;
			}
		}
	}
}
/************************************************************************************* DW FUNCTION **/
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  var url1 = (selObj.options[selObj.selectedIndex].value).indexOf(getURL(1));
  //alert((selObj.options[selObj.selectedIndex].value));
  var url2 = "";
  if(url1 > 0) {
	  url2 = getURL(1) + "/" + getURL(2);
  }
  //alert(url1);
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+url2+"'");
  if (restore) selObj.selectedIndex=0;
}
function getURL(n) {
	var name = document.location.pathname.split("/");
	return name[n];
}

/************************************************************************************* OPEN WINDOW FUNCTION **/
function openEmailWindow(lName) {
	window.open(lName, 'email', 'width=600, height=280, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');
}
function openFlyerWindow(lName) {
	window.open(lName, 'flyer', 'width=600, height=280, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');
}
function openECatalog(lName) {
	window.open(lName, 'ecatalog', 'width=950, height=665, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');
}
function openFlierWindow(lName) {
	window.open(lName, 'flier', 'width=840, height=665, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no');
}
function openZoomWindow(lName) {
	window.open(lName, 'zoom', 'width=470, height=405, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');
}
/************************************************************************************* ADD FAVORITE FUNCTION **/
function addlove() {
    if(document.all) {
        try {
            window.external.addFavorite(window.location.href, document.title);
        } catch(e) {
            alert( "Press Ctrl + D add to favorite." );
        }
    } else if(window.sidebar) {
        window.sidebar.addPanel(document.title, window.location.href, "");
    } else {
        alert( "Press Ctrl + D add to favorite." );
    }
}
/************************************************************************************* CHANGE MAIN IMAGE FROM COLOR **/
function changeColorImage(styleNo, colorName) {
	document.getElementById("mainImage").src = "/images/products/" + styleNo + "/" + styleNo + "-" + colorName + ".jpg";
	document.getElementById("zoom").href = "/images/products/" + styleNo + "/" + styleNo + "-" + colorName + "-b.jpg";
	//MagicZoom.update(zoom, "/images/products/" + styleNo + "/" + styleNo + "-" + colorName + "-b.jpg", "/images/products/" + styleNo + "/" + styleNo + "-" + colorName + ".jpg", "show-title: false");
	MagicZoom.refresh();

	if(document.getElementById("downImage")) {
	    document.getElementById("downImage").href = "/home/saveimage.asp?filename=" + styleNo + "/" + styleNo + "-" + colorName + "-b.jpg";
	}
	if(document.getElementById("addLogo")) {
	    document.getElementById("addLogo").onclick = function() {openZoomWindow('/zoomimage/zoom.html?imgfile=' + styleNo + '/' + styleNo + '-' + colorName + '-b.jpg');};
	}
}
/************************************************************************************* STYLE SEARCH FUNCTION **/
function getStyle(t){
	tid = t;
	styleListDiv = document.getElementById("stylelist");
	styleInput = document.stylesearch.keywords;
	if(styleInput.value == "Enter style #") {
		styleInput.value = "";
	}
	styleInput.onblur = function(){clearStyleList();}
	window.onblur = function(){clearStyleList();}
	//if(styleInput.value.length > 0) {
		//timestyle = setTimeout("clearStyleList()", 2000);
		if (window.ActiveXObject) {
			xmlHttpT = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		else if (window.XMLHttpRequest) {
			xmlHttpT = new XMLHttpRequest();
		}
		var url = "/search/styletoxml.asp?t_id=" + tid + "&offset=" + page + "&p_style=" + escape(styleInput.value);

		xmlHttpT.onreadystatechange = callback;
		xmlHttpT.open("GET", url, true);
		xmlHttpT.send(null);
	//}
}
function callback() {
	if(xmlHttpT.readyState == 4) {
        if(xmlHttpT.status == 200) {
			//alert(xmlHttpT.responseText);
			setStyleList(xmlHttpT.responseXML);
        } else if(xmlHttpT.status == 204) {
			clearStyleList();
		}
    }
}
function setStyleList(xmlData) {
	var offset = xmlData.getElementsByTagName("products").item(0).getAttribute("offset");
	var first = xmlData.getElementsByTagName("products").item(0).getAttribute("first");
	var last = xmlData.getElementsByTagName("products").item(0).getAttribute("last");
	var total = xmlData.getElementsByTagName("products").item(0).getAttribute("total");
	var styles = xmlData.getElementsByTagName("pstyle");
	var names = xmlData.getElementsByTagName("pname");
	var brands = xmlData.getElementsByTagName("tdescription");
	var tempstyle = "<ul onmouseover='clearBlur();' onmouseout='resetBlur();'>"
	//alert(totals);
	if(styles.length > 0) {
		tempstyle = tempstyle + "<li class='nonestyle'>Please choose your style.</li>";
		for(var i=0; i<styles.length; i++) {
			tempstyle = tempstyle + "<li><a href='/" + brands[i].firstChild.nodeValue + "/" + styles[i].firstChild.nodeValue + ".shtml'>" + styles[i].firstChild.nodeValue.replace(eval("/"+styleInput.value+"/i"), "<span>"+styleInput.value+"</span>") + "&nbsp;&nbsp;" + names[i].firstChild.nodeValue.substring(0, 30) + "</a></li>";
		}
		if(styles.length == 1) {
			if(styleFlag) {
				window.location.href('/' + brands[0].firstChild.nodeValue + '/' + styles[0].firstChild.nodeValue + '.shtml');
			}
		}
		tempstyle = tempstyle + "<p><a href='javascript:prevPage(" + offset + ");'>Previous</a>&nbsp;&nbsp;<a href='javascript:nextPage(" + offset + ");'>Next</a>&nbsp;&nbsp;|&nbsp;&nbsp;Find style <strong>" + total + "</strong>, " + first + "-" + last + ".</p>";
	} else {
		tempstyle = tempstyle + "<li class='nonestyle'>" + styleInput.value + "&nbsp;&nbsp;None style find.</li>";
	}
	tempstyle = tempstyle + "</ul>";
	styleListDiv.innerHTML = tempstyle;
	styleFlag = false;
	styleListDiv.style.display = "block";
	styleInput.focus();
}
function clearStyleList() {
	styleListDiv.style.display = "none";
	styleListDiv.innerHTML = "";
	//clearTimeout(timestyle);
}
function setStyle(stylename) {
	styleInput.value = stylename;
}
function onEnterStyle(e) {
	if(!e) e = window.event;
	if((e.keyCode || e.which) == 13) {
		styleFlag = true;
	}
}
function onKeyStyle() {
	page = 0;
	getStyle(tid);
}
function onGoStyle() {
	//stylesearchCheck();
	styleFlag = true;
	getStyle(tid);
}
function clearBlur() {
	styleInput.onblur = null;
}
function resetBlur() {
	styleInput.onblur = function(){clearStyleList();}
}
function nextPage(offset) {
	page = Number(offset) + 20;
	//alert(page);
	if(page > -1) {
		getStyle(tid);
	}
}
function prevPage(offset) {
	page = Number(offset) - 20;
	//alert(page);
	if(page >= 0) {
		getStyle(tid);
	}
}
