var xmlaction=null;
var xmlaction2=null;
var xmlFile="product_category.xml";
var picPreURL="../image/product/";


function loadListTable(){
	xmlaction=getRequest();
	xmlaction.onreadystatechange = loadListCallBack;
	var date=new Date().getTime();
	xmlaction.open("GET", xmlFile+"?random="+date, true);
	xmlaction.send(null);
}

function loadListCallBack(){
	if (xmlaction.readyState == 4) {
	 // only if "OK"
		if (xmlaction.status == 200) {
		    var _node = document.getElementById("loadTR");
		    try{
			    _node.removeNode(true);
			}catch(ex){
			    _node.parentNode.removeChild(_node);
			    }
			buildListTable(xmlaction.responseXML);
		} else {
			alert("Load Album List Error!"+xmlaction.status);
			document.getElementById("loadTD").innerHTML="<span style='color: red;font-weight: bold;'>Load category list failed, please <a href='javascript:window.location.reload();'>try again</a> later</span>";
		}
		
		xmlaction=null;
	}
}

function buildListTable(_xml){
	var list=_xml.getElementsByTagName("category");

	var _tr=document.createElement("tr");
	var _td1=document.createElement("td");
	_td1.setAttribute("className","leftTD");
	_td1.setAttribute("class","leftTD");
	_td1.innerHTML="&nbsp;"
	var _td2=document.createElement("td");
	_td2.setAttribute("className","rightTD");
	_td2.setAttribute("class","rightTD");
	_td2.innerHTML="&nbsp;"
	var _td=document.createElement("td");
	_td.setAttribute("className","decUnitBorder middleTD mainText");
	_td.setAttribute("class","decUnitBorder middleTD mainText");
	_td.setAttribute("align","left");
	_tr.appendChild(_td1);
	_tr.appendChild(_td);
	_tr.appendChild(_td2);
	document.getElementById("listTable").appendChild(_tr);

	var html="";
	for(var i=1;i<=list.length;i++){
		var _name=getPropertyByName(list[i-1],"name");
		var _id=getPropertyByName(list[i-1],"id");
		html+="<img src='../image/dec_dotted_2.gif' class='decDottedPic'> <a href='product_family.html?id="+_id+"'>"+i+") "+_name+"</span></a><br>";
	}
	_td.innerHTML=html;
}

var productCategoryId=0;

function receiveProductDoOnLoad(){
	var params=parseParams(window.location.href);
	productCategoryId=getParamValue(params,"id");
	var _xmlfile=productCategoryId+".xml";

	xmlaction=getRequest();
	xmlaction.onreadystatechange = loadProductInfoCallBack;
	var date=new Date().getTime();
	xmlaction.open("GET", _xmlfile+"?random="+date, true);
	xmlaction.send(null);
}

function loadProductInfoCallBack(){
	if (xmlaction.readyState == 4) {
	 // only if "OK"
		if (xmlaction.status == 200) {
			buildProductInfoList(xmlaction.responseXML);
		
			xmlaction2=getRequest();
			xmlaction2.onreadystatechange = loadSelectCallBack;
			var date=new Date().getTime();
			xmlaction2.open("GET", xmlFile+"?random="+date, true);
			xmlaction2.send(null);
		} else {
			//alert("Load Album List Error!"+xmlaction.status);
			document.getElementById("categoryTitle").innerHTML="<span style='color: red;font-weight: bold;'>Load product list failed, please <a href='javascript:window.location.reload();'>try again</a> later</span>";
		}
	}
}

function buildProductInfoList(_xml){
	var _list=_xml.getElementsByTagName("product");
	var _table=document.getElementById("productListTable");
	for(var i=0;i<_list.length;i++){
		var _tr=document.createElement("tr");
		var _td1=document.createElement("td");
		_td1.setAttribute("width","40%");
		_td1.setAttribute("className","productListRow"+(i%2+1));
		_td1.setAttribute("class","productListRow"+(i%2+1));
		_td1.innerHTML=getPropertyByNameForHTML(_list[i],"name");
		var _td2=document.createElement("td");
		_td2.setAttribute("width","20%");
		_td2.setAttribute("className","productListRow"+(i%2+1));
		_td2.setAttribute("class","productListRow"+(i%2+1));
		_td2.innerHTML=getPropertyByNameForHTML(_list[i],"cas");
		var _td3=document.createElement("td");
		_td3.setAttribute("width","10%");
		_td3.setAttribute("className","productListRow"+(i%2+1));
		_td3.setAttribute("class","productListRow"+(i%2+1));
		var pure=getPropertyByNameForHTML(_list[i],"pure");
		if(pure=="0"){
			pure=" "
		}else{
			pure=pure+" %";
		}
		_td3.innerHTML=pure;
		var _td4=document.createElement("td");
		_td3.setAttribute("className","productListRow"+(i%2+1));
		_td3.setAttribute("class","productListRow"+(i%2+1));
		var _pic=getPropertyByName(_list[i],"pic");

		if(_pic=="" || _pic==null){
			_pic=" ";
			_td4.innerHTML=_pic;
		}else{
			_pic="<img alt='"+getPropertyByName(_list[i],"name")+"' style='filter:chroma(color = #FFFFFF);' src='"+picPreURL+_pic+"'/> "
			_td4.innerHTML=_pic;
		}
		
		
		_tr.appendChild(_td1);
		_tr.appendChild(_td2);
		_tr.appendChild(_td3);
		_tr.appendChild(_td4);
		_table.appendChild(_tr);
	}
}

function loadSelectCallBack(){
	if (xmlaction2.readyState == 4) {
	 // only if "OK"
		if (xmlaction2.status == 200) {
			var _ele=xmlaction2.responseXML.getElementsByTagName("category");
			var _select=document.getElementById("family_select");
			var selectindex=-1;
			for(var i=0;i<_ele.length;i++){
				var _option=document.createElement("option");
				_option.setAttribute("value",getPropertyByName(_ele[i],"id"));
				_option.innerHTML=getPropertyByName(_ele[i],"name");
				_select.appendChild(_option);
				
				if(productCategoryId==getPropertyByName(_ele[i],"id")){
					document.getElementById("categoryTitle").innerHTML=getPropertyByName(_ele[i],"name");
					selectindex=i;
				}
			}
			
			setTimeout("document.getElementById('family_select').selectedIndex="+selectindex,0);
		} else {
			//alert("Load Album List Error!"+xmlaction.status);
			document.getElementById("categoryTitle").innerHTML="<span style='color: red;font-weight: bold;'>Load product list failed, please <a href='javascript:window.location.reload();'>try again</a> later</span>";
		}
	}
}

function onFamilySelectChange(){
	var sel=document.all.family_select;
	var value=sel.options[sel.selectedIndex].value;
	if(value==0)
		return;
	
	var t_url="product_family.html?id="+value;
	document.URL=t_url;
}

function Param(_name,_value){
	this.name=_name;
	this.value=_value;
}

function parseParams(_url){
	var index=_url.indexOf('?',0);
	var result=new Array();
	if(index<0)
		return result;
	_url=_url.substring(index+1);

	while((index=_url.indexOf('&'))>0){
		var unit=_url.substring(0,index);
		_url=_url.substring(index+1);
		var i=unit.indexOf('=');
		if(i<=0)
			continue;
		var _name=unescape(unit.substring(0,i));
		var _value=unescape(unit.substring(i+1));
		result.push(new Param(_name,_value));
	}
	
	if(_url.length>0){
		var i=_url.indexOf('=');
		if(i<=0)
			return result;
		var _name=unescape(_url.substring(0,i));
		var _value=unescape(_url.substring(i+1));
		result.push(new Param(_name,_value));
	}
	return result;
}

function getPropertyByNameForHTML(_params,_name){
	var _value=getPropertyByName(_params,_name);
	if(_value==null || _value=="")
		return " ";
	else return _value;
}

function getParamValue(_params,_name){
	if(_params==undefined || _params==null || _params.length==0)
		return null;
	
	for(var i=0;i<_params.length;i++){
		if(_params[i].name==_name)
			return _params[i].value;
	}
	return null;
}

function getRequest(){
  if (window.XMLHttpRequest) {
    return new XMLHttpRequest();
  } // branch for IE/Windows ActiveX version
  else if (window.ActiveXObject) {
    return  new ActiveXObject("Microsoft.XMLHTTP");
  }
}

function getPropertyByName(_ele,_name){
	var list=_ele.childNodes;
	for(var i=0;i<list.length;i++){
		if(list[i].tagName==_name){
			try{
				return list[i].firstChild.data;
			}catch(e){
				return null;
			}
		}
	}
}