var select_shops_res;
var select_shop_pageSize = 20;
var select_shop_count;
var select_shop_currentPage = 1;
var select_shop_pageCount;
var select_shop_sequence;
var select_shop_provinceID;
var select_shop_districtID;
var select_shop_cityID;
var select_shop_shopType;
var select_shop_shopName;

//搜索商店
function select_selectShop(pageNum){

	var url = "selectShopOrProduct_user.aspx";
	var param = "method=shop&m=shop";
	param = param + "&provinceID=" + select_shop_provinceID;
	param = param + "&districtID=" + select_shop_districtID;
	param = param + "&cityID=" + select_shop_cityID;
	param = param + "&shopType=" + select_shop_shopType;
	param = param + "&shopName=" + select_shop_shopName;
	param = param + "&pageNum=" + pageNum;
	select_shop_currentPage = pageNum;
	select_shop_sequence = (select_shop_currentPage-1)*select_shop_pageSize;
//	alert(param);
	sendRequest(url, param, select_selectShop_processResponse);
}
function select_selectShop_processResponse(){
	if (XMLHttpReq.readyState == 4) {
		if (XMLHttpReq.status == 200) {
			var res = XMLHttpReq.responseXML;
			msgBox_alert.close();
			var text = res.getElementsByTagName("response")[0].firstChild.nodeValue;
			switch (text) {
			   case "exception":
				msgBox_alert = new miniWindow({obj:"msgBox_alert",windowType:'alert',title:'系统提示：',hint:"<font color=red>系统繁忙，请稍后再试！</font>"});
				msgBox_alert.open();
				msgBox_alert.callback=function(args){
					msgBox_alert.close();
				}
				break;
			  default:
				select_shops_res = res;
				select_showSelecteShops();
			}
		} else {
			msgBox_alert = new miniWindow({obj:"msgBox_alert",windowType:'alert',title:'系统提示：',hint:"<font color=red>系统繁忙，请稍后再试！</font>"});
			msgBox_alert.open();
			msgBox_alert.callback=function(args){
				msgBox_alert.close();
			}
		}
	}
}
function select_showSelecteShops(){
//	alert("显示");
	document.getElementById("shopresTR").style.display = "block";
	//清空信息列表
	cleanList("shopList");
	select_shop_count = select_shops_res.getElementsByTagName("count")[0].firstChild.nodeValue;
	if(select_shop_count == 0){
		//没有符合条件的商店
		document.getElementById("shopPageControl").innerHTML = "<font color='blue'>没有找到符合条件的商店</font>";
		return false;
	}
	//获得商店信息
	var shops = select_shops_res.getElementsByTagName("shops")[0].childNodes;
	
	//显示的总页数
	select_shop_pageCount = Math.ceil(select_shop_count/select_shop_pageSize);
	//更新当前页数
	if (select_shop_currentPage > select_shop_pageCount) {
		select_shop_currentPage = select_shop_pageCount;
	}
	//显示商店列表
	var tbody = document.getElementById("shopList");
	var tr = document.createElement("tr");
	tbody.appendChild(tr);
	
	for(var i = 0;i<shops.length; i++){
		if(i%2 == 0){
			tr.style.backgroundColor = "#FFF2E8";
		}
		var shop = shops.item(i).childNodes;
		var shopLID = shop.item(0).firstChild.nodeValue;
		var shopBID = shop.item(1).firstChild.nodeValue;
		var shopName = shop.item(2).firstChild.nodeValue;
		var enableShop = shop.item(9).firstChild.nodeValue;
		
		//排名
		select_shop_sequence += 1;
		var td = document.createElement("td");
		td.setAttribute("align", "center");
		if(select_shop_sequence <= 10){
			td.innerHTML = "<font color='red'>№" + select_shop_sequence + "</font>";
		}else{
			td.innerHTML = "№" + select_shop_sequence;
		}
		
		tr.appendChild(td);
		
		//显示商店logo图片及名称
		var logo = document.createElement("img");
		logo.setAttribute("src","images/shop/"+shopLID+"/logo.gif");
		logo.setAttribute("width","70");
		logo.setAttribute("height","40");
		var a = document.createElement("a");
		if(enableShop == 'Y'){
			a.setAttribute("href","shop.jsp?shop=" + shopBID);
			a.setAttribute("target","_blank");
		}else{
			a.setAttribute("href","#");
			a.onclick = function(){
				alert("本店装修中，暂未上传任何商品或服务信息，敬请期待！");
			}
		}
		var td = document.createElement("td");
		td.setAttribute("align", "center");
		a.appendChild(logo);
		a.appendChild(document.createElement("<br>"));
		a.appendChild(document.createTextNode(shopName));
		td.appendChild(a);
		tr.appendChild(td);
		
		//显示后序列
		for(var j=3; j<shop.length-1; j++){
			var text = shop.item(j).firstChild.nodeValue
			var td = document.createElement("td");
			td.setAttribute("align", "center");
			//以图片形式显示本月诚信
			if(j == 5){
				var font = document.createElement("font");
				if(text >= 0){
					font.setAttribute("color","red");
				}else{
					font.setAttribute("color","black");
				}
				font.appendChild(document.createTextNode(text));
				td.appendChild(font);
				if(text >= 0){
					//小心数量(总分除100的余数每20分一颗)
					var smoNum = Math.floor((text%100)/20);
					if(smoNum > 0){
						td.appendChild(document.createElement("<br>"));
					}
					for(var n=0; n<smoNum; n++){
						var smoCreditImg = document.createElement("img");
						smoCreditImg.setAttribute("src","images/other/credit.gif");
						smoCreditImg.setAttribute("width","10");
						smoCreditImg.setAttribute("height","10");
						smoCreditImg.setAttribute("alt","20分/颗");
						td.appendChild(smoCreditImg);
					}
					
					//大心数量(总分除500的余数每满100分一颗)
					var bigNum = Math.floor((text%500)/100);
					if(bigNum > 0){
						td.appendChild(document.createElement("<br>"));
					}
					for(var n=0; n<bigNum; n++){
						var bigCreditImg = document.createElement("img");
						bigCreditImg.setAttribute("src","images/other/credit.gif");
						bigCreditImg.setAttribute("width","15");
						bigCreditImg.setAttribute("height","15");
						bigCreditImg.setAttribute("alt","100分/颗");
						td.appendChild(bigCreditImg);
					}
					
					//白钻数量(总分除2000的余数每满500分一颗)
					var wightNum = Math.floor((text%2000)/500);
					if(wightNum > 0){
						td.appendChild(document.createElement("<br>"));
					}
					for(var n=0; n<wightNum; n++){
						var bigCreditImg = document.createElement("img");
						bigCreditImg.setAttribute("src","images/other/diamond1.gif");
						bigCreditImg.setAttribute("width","20");
						bigCreditImg.setAttribute("height","20");
						bigCreditImg.setAttribute("alt","500分/颗");
						td.appendChild(bigCreditImg);
					}
					
					//红钻数量(总分每满2000分一颗)
					var redNum = Math.floor(text/2000);
					if(redNum > 0){
						td.appendChild(document.createElement("<br>"));
					}
					for(var n=0; n<redNum; n++){
						var bigCreditImg = document.createElement("img");
						bigCreditImg.setAttribute("src","images/other/diamond2.gif");
						bigCreditImg.setAttribute("width","20");
						bigCreditImg.setAttribute("height","20");
						bigCreditImg.setAttribute("alt","2000分/颗");
						td.appendChild(bigCreditImg);
					}
				}
				tr.appendChild(td);
				continue;
			}
			//以图片形式显示最大诚信
			if(j == 6){
				var font = document.createElement("font");
				if(text >= 0){
					font.setAttribute("color","red");
				}else{
					font.setAttribute("color","black");
				}
				font.appendChild(document.createTextNode(text));
				td.appendChild(font);
				if(text >= 0){
					//小图数量(总分除500的余数每100分一颗)
					var smoNum = Math.floor((text%500)/100);
					if(smoNum > 0){
						td.appendChild(document.createElement("<br>"));
					}
					for(var n=0; n<smoNum; n++){
						var smoCreditImg = document.createElement("img");
						smoCreditImg.setAttribute("src","images/other/credit.gif");
						smoCreditImg.setAttribute("width","10");
						smoCreditImg.setAttribute("height","10");
						smoCreditImg.setAttribute("alt","100分/颗");
						td.appendChild(smoCreditImg);
					}
								
					//大图数量(总分除2000的余数每满500分一颗)
					var bigNum = Math.floor((text%2000)/500);
					if(bigNum > 0){
						td.appendChild(document.createElement("<br>"));
					}
					for(var n=0; n<bigNum; n++){
						var bigCreditImg = document.createElement("img");
						bigCreditImg.setAttribute("src","images/other/credit.gif");
						bigCreditImg.setAttribute("width","15");
						bigCreditImg.setAttribute("height","15");
						bigCreditImg.setAttribute("alt","500分/颗");
						td.appendChild(bigCreditImg);
					}
					
					//白钻数量(总分除10000的余数每满2000分一颗)
					var wightNum = Math.floor((text%10000)/2000);
					if(wightNum > 0){
						td.appendChild(document.createElement("<br>"));
					}
					for(var n=0; n<wightNum; n++){
						var bigCreditImg = document.createElement("img");
						bigCreditImg.setAttribute("src","images/other/diamond1.gif");
						bigCreditImg.setAttribute("width","20");
						bigCreditImg.setAttribute("height","20");
						bigCreditImg.setAttribute("alt","2000分/颗");
						td.appendChild(bigCreditImg);
					}
					
					//红钻数量(总分每满10000分一颗)
					var redNum = Math.floor(text/10000);
					if(redNum > 0){
						td.appendChild(document.createElement("<br>"));
					}
					for(var n=0; n<redNum; n++){
						if(n!=0 && n%6 == 0){
							td.appendChild(document.createElement("<br>"));
						}
						var bigCreditImg = document.createElement("img");
						bigCreditImg.setAttribute("src","images/other/diamond2.gif");
						bigCreditImg.setAttribute("width","20");
						bigCreditImg.setAttribute("height","20");
						bigCreditImg.setAttribute("alt","10000分/颗");
						td.appendChild(bigCreditImg);
					}
				}
				tr.appendChild(td);
				continue;
			}
			if(j == 7){
				td.innerHTML = "<font color='red'>"+text+"</font>";
				tr.appendChild(td);
				continue;
			}
			td.innerHTML = text;
			tr.appendChild(td);
		}
		var tr = document.createElement("tr");
		tbody.appendChild(tr);
	}

	var control = "";
	document.getElementById("shopPageControl").innerHTML = "";
	//显示页面控制链接
	if (select_shop_currentPage <= 1) {
		control = "<font color='808080'>上一页</font>&nbsp;&nbsp;<a href='#shopList' onclick='select_showShop_nextPage();'>\u4e0b\u4e00\u9875</a>";
	}
	if (select_shop_currentPage > 1 && select_shop_currentPage < select_shop_pageCount) {
		control = "<a href='#shopList' onclick='select_showShop_prePage();'>\u4e0a\u4e00\u9875</a>&nbsp;&nbsp;<a href='#shopList' onclick='select_showShop_nextPage();'>\u4e0b\u4e00\u9875</a>";
	}
	if (select_shop_currentPage >= select_shop_pageCount) {
		control = "<a href='#shopList' onclick='select_showShop_prePage();'>\u4e0a\u4e00\u9875</a>&nbsp;&nbsp;<font color='808080'>下一页</font>";
	}
	if (select_shop_pageCount == 1) {
		control = "";
	}
	
	control = control + "<br>\u5171 <font color='blue'>" + select_shop_pageCount + "</font> \u9875";
	control = control + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	control = control + "\u5f53\u524d\u4e3a\u7b2c <font color='blue'>" + select_shop_currentPage + "</font> \u9875 <br>";
	//页数选择列表
//	alert(select_shop_currentPage);

	if(select_shop_currentPage != 1){
		control = control + "<a href='#shopList' onclick='select_selectShop(1)'>回第一页</a>";
	}else{
		control = control + "<font color='808080'>回第一页</font>";
	}
	for (var i = select_shop_currentPage-5; i <= (select_shop_currentPage+5) && i <= select_shop_pageCount; i++) {
		if(i<=0){
			i = 1;
		}
		if (i == select_shop_currentPage) {
			control = control + "<font color='red'>【" + i + "】</font>";
			continue;
		}
		control = control + "<a href='#shopList' onclick='select_selectShop(" + i + ")'>【" + i + "】</a>";
		
	}
	if(select_shop_currentPage != select_shop_pageCount){
		control = control + "<a href='#shopList' onclick='select_selectShop(" + select_shop_pageCount + ")'>最后一页</a>";
	}else{
		control = control + "<font color='808080'>最后一页</font>";
	}
	
	document.getElementById("shopPageControl").innerHTML = control;
}

//商店下一页
function select_showShop_nextPage(){
	select_shop_currentPage = select_shop_currentPage + 1;
	if (select_shop_currentPage >= select_shop_pageCount) {
		select_shop_currentPage = select_shop_pageCount;
	}
	select_selectShop(select_shop_currentPage);
}
//商店上一页
function select_showShop_prePage(){
	select_shop_currentPage = select_shop_currentPage - 1;
	if (select_shop_currentPage <= 0) {
		select_shop_currentPage = 1;
	}
	select_selectShop(select_shop_currentPage);
}

//检查输入的搜索商店条件
function select_checkInputForSelectShop(){
	var shopName = myReplaceChar(trimString(document.getElementById("shopName").value));
//	alert(shopName);
	var shopType = document.getElementById("shopType").value;
	if(!select_checkShopName(shopName)){
		document.getElementById("shopresTR").style.display = "block";
		//清空信息列表
		cleanList("shopList");
		document.getElementById("shopPageControl").innerHTML = "<font color='blue'>没有找到符合条件的商店</font>";
		return false;
	}
	if(shopType==0){
		msgBox_alert = new miniWindow({obj:"msgBox_alert",windowType:'alert',title:'系统提示：',hint:"<font color=red>最少选择一种商店类型！</font>"});
		msgBox_alert.open();
		msgBox_alert.callback=function(args){
			msgBox_alert.close();
			document.getElementById("shopType").focus();
		}
		return false;
	}
	return true;
}
function select_checkShopName(value){
	if(value.length > 50){
		return false;
	}else{
		return true;
	}
}
