/*
全日本雅虎中文化搜尋AJAX版 首頁分類版
*/
/*--------取得頁面大小---------*/
function get_size(){
	body_width		=	document.body.clientWidth;
	body_height		=	document.body.clientHeight;
	window_width	=	document.documentElement.clientWidth;
	window_height	=	document.documentElement.clientHeight;
	body_height 	< 	window_height 	? 	pageHeight 	= window_height		: 	pageHeight 	= body_height;	
	body_width 		< 	window_width	?	pageWidth 	= window_width		:	pageWidth 	= body_width;
	arrayPageSize	= 	new Array(pageWidth,pageHeight,window_width,window_height);
	return arrayPageSize;
}
/*--------圖層遮蔽開啟---------*/
function div_open(){
	if(document.getElementById('bg')!=null){
		var PageSize		=	get_size();
		var bg 				=	document.getElementById('bg');
		bg.style.width		=	PageSize[0]+"px";
		bg.style.height		=	(PageSize[1]*1.1)+"px";
		bg.style.display	=	'block';
	}
	if(document.getElementById('waitting')!=null){
		var PageSize			=	get_size();
		var waitting 			=	document.getElementById('waitting');
		waitting.style.display	=	'block';
	}	
}
/*--------圖層遮蔽關閉---------*/
function div_close(){
if(document.getElementById('bg')!=null){
		var PageSize		=	get_size();
		var bg 				=	document.getElementById('bg');
		bg.style.display	=	'none';
	}
	if(document.getElementById('waitting')!=null){
		var PageSize			=	get_size();
		var waitting 			=	document.getElementById('waitting');
		waitting.style.display	=	'none';
	}	
}

function HttpResquest(method,ref){
	div_open();
	if(!method){
		return false;	
	}else{
		switch(method){
			//選擇分類處理
			case 'choose_categorydemo' :
				xmlHttp			=	createXHR();							//初始化AJAX物件
				var showurl		=	"../html/categorydemo_get.php";					//後端check之程式檔路徑
				window.status	=	'資料取得中...';								
				strXML			=	'index=true&categoryId='+ref;
				//要傳給後端的一些資訊
				xmlHttp.open('POST',showurl,true);							//用POST方式傳遞
				xmlHttp.setRequestHeader( "Content-Type", "text/xml" );		//傳回的文本為XML以便取得XML物件
				xmlHttp.onreadystatechange=choose_categorydemo_result;		//指定取得回傳結果的function name
				xmlHttp.send(strXML);//送出AJAX請求
			break;
		}	
	}
}

function choose_categorydemo_result(){
	if (xmlHttp.readyState==4 && xmlHttp.status==200){	//如果請求處理完成而且回傳狀態為ok  PS:200=ok(無誤)
		var xmlResult 	= 	xmlHttp.responseXML;//取得XML回傳物件的結果
		if(xmlResult != null){
			if(xmlResult.getElementsByTagName('error_msg') && xmlResult.getElementsByTagName('error_msg').length > 0){
				div_close();
				document.getElementById('error_msg').innerHTML		=	xmlResult.getElementsByTagName('error_msg')[0].firstChild.data;//回傳錯誤並顯示錯誤訊息				
			}
			else if(xmlResult.getElementsByTagName('category_list_not') && xmlResult.getElementsByTagName('category_list_not').length > 0){
				div_close();
				return false;	
			}
			else if(xmlResult.getElementsByTagName('only_categoryId') && xmlResult.getElementsByTagName('only_categoryId').length > 0 && xmlResult.getElementsByTagName('only_categoryId_prev') && xmlResult.getElementsByTagName('only_categoryId_prev').length > 0){
				var only_categoryId = xmlResult.getElementsByTagName('only_categoryId')[0].firstChild.data;
				var categoryId_prev = xmlResult.getElementsByTagName('only_categoryId_prev')[0].firstChild.data;
				location.href	=	'http://www.mogan.com.tw/html/categorydemo.php?category='+only_categoryId+'&categoryId_prev='+categoryId_prev;
				return false;	
			}
			else{
				return change_process(xmlResult);
			}
		}
	}
}        

function change_process(xmlResult){
	var object = new Object;	
	/*---------取得並更換分類---------*/
	if(xmlResult.getElementsByTagName('category_list') && xmlResult.getElementsByTagName('category_list').length > 0){
		var newcategry;
		var category_one = xmlResult.getElementsByTagName('category_one');
		for(i = 0 ; i < category_one.length ; i++){
			if(category_one[i].hasChildNodes()){
				for(j = 0 ; j<category_one[i].childNodes.length ; j++){
					category_one[i].childNodes[j].nodeName == 'xml_categoryid' 		? object.Id 	= category_one[i].childNodes[j].firstChild.data : '';
					category_one[i].childNodes[j].nodeName == 'xml_categoryname' 	? object.Name = category_one[i].childNodes[j].firstChild.data : '';
					category_one[i].childNodes[j].nodeName == 'xml_categorynum' 	? object.Num 	= category_one[i].childNodes[j].firstChild.data : '';	
				}
				if(newcategry==null)	newcategry='';
				newcategry += '<img src="../../images/w_link.gif" width="14" height="14" /><a name="#" onclick="HttpResquest(\'choose_categorydemo\',\''+object.Id+'\')" class="w_link" style="cursor:pointer">'+object.Name+'</a><span class="id_hi2">('+object.Num+')</span><br />\n'						
			}
		}
		if(newcategry != null){
			document.getElementById('categry_main').innerHTML = '<a href="../../html/categorydemo.php"><img src="../../images/w_yahoo.gif" alt="日本雅虎中文分類" border="0" /></a><br />' + newcategry;
		}	
	}
	div_close();
}

