var select_products_res;
var select_product_pageSize = 20;
var select_product_count;
var select_product_currentPage = 1;
var select_product_pageCount;
var select_product_provinceID = "0";
var select_product_districtID = "0";
var select_product_cityID = "0";
var select_product_shopName = "";
var select_product_firstProductType = "";
var select_product_secondProductType = "";
var select_product_thirdProductType = "";
select_product_productName = "";
var select_product_retailPrice = "";
var select_product_enableGroupBuy = "";
var select_product_groupBuyPrice = "";
var select_product_groupBuyQuantity = "";
var select_product_groupBuyCredit = "";
var select_product_groupBuySubscription = "";

//搜索商品
function select_selectProduct(pageNum){
	
	var url = "selectShopOrProduct_user.aspx";
	var param = "method=product&m=product";
	param = param + "&provinceID=" + select_product_provinceID;
	param = param + "&districtID=" + select_product_districtID;
	param = param + "&cityID=" + select_product_cityID;
	param = param + "&shopName=" + select_product_shopName;
	param = param + "&firstTypeID=" + select_product_firstProductType;
	param = param + "&secondTypeID=" + select_product_secondProductType;
	param = param + "&thirdTypeID=" + select_product_thirdProductType;
	param = param + "&productName=" + select_product_productName;
	param = param + "&retailPrice=" + select_product_retailPrice;
	param = param + "&enableGroupBuy=" + select_product_enableGroupBuy;
	if(select_product_enableGroupBuy == "Y"){
		param = param + "&groupBuyPrice=" + select_product_groupBuyPrice;
		param = param + "&groupBuyQuantity=" + select_product_groupBuyQuantity;
		param = param + "&groupBuyCredit=" + select_product_groupBuyCredit;
		param = param + "&groupBuySubscription=" + select_product_groupBuySubscription;
	}
	param = param + "&pageNum=" + pageNum;
	select_product_currentPage = pageNum;
	
//	alert(param);
	sendRequest(url, param, select_selectProduct_processResponse);
}
function select_selectProduct_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();
				}
				updateCheckCode();
				break;
			  default:
				select_products_res = res;
				select_showSelecteProducts();
			}
		} 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_showSelecteProducts(){
	document.getElementById("productresTR").style.display = "block";
	//清空信息列表
	cleanList("productList");
	select_product_count = select_products_res.getElementsByTagName("count")[0].firstChild.nodeValue;
	if(select_product_count == 0){
		//没有符合条件的商店
		document.getElementById("productPageControl").innerHTML = "<font color='blue'>没有找到符合条件的商品</font>";
		return false;
	}
	//获得商店信息
	var products = select_products_res.getElementsByTagName("products")[0].childNodes;
	//显示的总页数
	select_product_pageCount = Math.ceil(select_product_count/select_product_pageSize);

	//更新当前页数
	if (select_product_currentPage > select_product_pageCount) {
		select_product_currentPage = select_product_pageCount;
	}
	
//	alert("总记录数"+select_product_count);
//	alert("每页记录数"+select_product_pageSize);
//	alert("当前页" + select_product_currentPage);
//	alert("总页数"+select_product_pageCount);
//	alert("总记录数"+select_product_pageSize);
//	alert("每页记录数"+select_product_pageSize);
	//显示商店列表
	var tbody = document.getElementById("productList");
	
	for(var i=0;i<products.length; i++){
		
		var tr = document.createElement("tr");
		if(i%2 == 0){
			tr.style.backgroundColor = "#FFF2E8";
		}
		tbody.appendChild(tr);
		
		var product = products.item(i).childNodes;
		var shopLID = product.item(0).firstChild.nodeValue;
		var shopBID = product.item(1).firstChild.nodeValue;
		var shopName = product.item(2).firstChild.nodeValue;
		var address = product.item(3).firstChild.nodeValue;
		var shopType = product.item(4).firstChild.nodeValue;
		var monthCredit = product.item(5).firstChild.nodeValue;
		var maxCredit = product.item(6).firstChild.nodeValue;
		var startDate = product.item(7).firstChild.nodeValue;
		var productLID = product.item(8).firstChild.nodeValue;
		var productBID = product.item(9).firstChild.nodeValue;
		var productName = product.item(10).firstChild.nodeValue;
		var productType = product.item(11).firstChild.nodeValue;
		var retailPrice = product.item(12).firstChild.nodeValue;
		var noChaffer = product.item(13).firstChild.nodeValue;
		var enableGroupBuy = product.item(14).firstChild.nodeValue;
		var monthAccessedNum = product.item(15).firstChild.nodeValue;
		var defaultImg = product.item(16).firstChild.nodeValue;
		var width = product.item(17).firstChild.nodeValue;
		var height = product.item(18).firstChild.nodeValue;
		
		
		//显示商品图片列
		var productImg = document.createElement("img");
		productImg.setAttribute("productLID", productLID);
		var imgSpan = document.createElement("span");
		imgSpan.style.border = "1px solid #808080";
		imgSpan.style.width = "100px";
		imgSpan.style.height = "75px";
		imgSpan.style.cursor = "hand";
		imgSpan.appendChild(productImg);
		if(defaultImg != "null"){
			productImg.setAttribute("src","images/shop/"+shopLID+"/products/"+productLID+"/"+defaultImg+".jpg");
			resetImgSize(productImg,100,75,width,height);
			productImg.setAttribute("alt","查看商品");
			productImg.onclick = function(){window.open("product.jsp?productID=" + this.productLID); return false};
			productImg.onmouseover = function(){showMouseInfo_product(this.src);}
			productImg.onmouseout = function(){
				hideMouseInfo();
			}
		}else{
			productImg.setAttribute("src","images/other/default.gif");
			productImg.setAttribute("width","100");
			productImg.setAttribute("height","75");
		}
		
		var a = document.createElement("a");
		a.setAttribute("href","product.jsp?productID=" + productLID);
		a.setAttribute("target","_blank");
		a.appendChild(document.createTextNode(productName));
		var td = document.createElement("td");
		td.setAttribute("align", "center");
		td.appendChild(imgSpan);
		td.appendChild(document.createElement("<br>"));
		td.appendChild(a);
		tr.appendChild(td);
		
		//所属商店列
		var shopLOGO = document.createElement("img");
		shopLOGO.setAttribute("src","images/shop/"+shopLID+"/logo.gif");
		shopLOGO.setAttribute("width","70");
		shopLOGO.setAttribute("height","40");
		var a = document.createElement("a");
		a.setAttribute("href","shop.jsp?shop=" + shopBID);
		a.setAttribute("href","shop.jsp?shop=" + shopBID);
		a.setAttribute("shopLID",shopLID);
		a.setAttribute("shopBID",shopBID);
		a.setAttribute("shopName",shopName);
		a.setAttribute("address",address);
		a.setAttribute("shopType",shopType);
		a.setAttribute("monthCredit",monthCredit);
		a.setAttribute("maxCredit",maxCredit);
		a.setAttribute("startDate",startDate);
		a.onmousemove = function(){
			showMouseInfo(this.shopName,this.address,this.shopType,this.monthCredit,this.maxCredit,this.startDate);
		}
		a.onmouseout = function(){
			hideMouseInfo();
		}
		var td = document.createElement("td");
		td.setAttribute("align", "center");
		a.appendChild(shopLOGO);
		a.appendChild(document.createElement("<br>"));
		a.appendChild(document.createTextNode(shopName));
		td.appendChild(a);
		tr.appendChild(td);
		
		//后序列
		for(var j=11; j<16; j++){
			var text = product.item(j).firstChild.nodeValue
			var td = document.createElement("td");
			td.setAttribute("align", "center");
			if(j == 12){
				td.innerHTML = "<font color='red'>￥"+text+"元</font>";
				tr.appendChild(td);
				continue;
			}
			if(j==13){
				if(text == "N"){
					text = "<font color='red'>一口价</font>";
				}else{
					text = "<font color='red'>可议价</font>";
				}
			}
			if(j==14){
				if(text == "N"){
					text = "<font color='red'>不可团购</font>";
				}else{
					text = "<font color='red'>可团购</font>";
				}
			}
			td.innerHTML = text;
			tr.appendChild(td);
		}
	}
	
	//显示页面控制
	var control = "";
	document.getElementById("productPageControl").innerHTML = "";
	//显示页面控制链接
	if (select_product_currentPage <= 1) {
		control = "<font color='808080'>上一页</font>&nbsp;&nbsp;<a href='#productList' onclick='select_showProduct_nextPage();'>\u4e0b\u4e00\u9875</a>";
	}
	if (select_product_currentPage > 1 && select_product_currentPage < select_product_pageCount) {
		control = "<a href='#productList' onclick='select_showProduct_prePage();'>\u4e0a\u4e00\u9875</a>&nbsp;&nbsp;<a href='#productList' onclick='select_showProduct_nextPage();'>\u4e0b\u4e00\u9875</a>";
	}
	if (select_product_currentPage >= select_product_pageCount) {
		control = "<a href='#productList' onclick='select_showProduct_prePage();'>\u4e0a\u4e00\u9875</a>&nbsp;&nbsp;<font color='808080'>下一页</font>";
	}
	if (select_product_pageCount == 1) {
		control = "";
	}
	
	control = control + "<br>\u5171 <font color='blue'>" + select_product_pageCount + "</font> \u9875";
	control = control + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	control = control + "\u5f53\u524d\u4e3a\u7b2c <font color='blue'>" + select_product_currentPage + "</font> \u9875 <br>";
	
	//页数选择列表
	if(select_product_currentPage != 1){
		control = control + "<a href='#productList' onclick='select_selectProduct(1)'>回第一页</a>";
	}else{
		control = control + "<font color='808080'>回第一页</font>";
	}
	for (var i = select_product_currentPage-5; i <= (select_product_currentPage+5) && i <= select_product_pageCount; i++) {
		if(i<=0){
			i = 1;
		}
		if (i == select_product_currentPage) {
			control = control + "<font color='red'>【" + i + "】</font>&nbsp;";
			continue;
		}
		control = control + "<a href='#productList' onclick='select_selectProduct(" + i + ")'>【" + i + "】</a>&nbsp;";
	}
	if(select_product_currentPage != select_product_pageCount){
		control = control + "<a href='#productList' onclick='select_selectProduct(" + select_product_pageCount + ")'>最后一页</a>";
	}else{
		control = control + "<font color='808080'>最后一页</font>";
	}
	document.getElementById("productPageControl").innerHTML = control;
	
}

//商品下一页
function select_showProduct_nextPage(){
	select_product_currentPage = select_product_currentPage + 1;
	if (select_product_currentPage >= select_product_pageCount) {
		select_product_currentPage = select_product_pageCount;
	}
	select_selectProduct(select_product_currentPage);
}
//商品上一页
function select_showProduct_prePage(){
	select_product_currentPage = select_product_currentPage - 1;
	if (select_product_currentPage <= 0) {
		select_product_currentPage = 1;
	}
	select_selectProduct(select_product_currentPage);
}

//显示鼠标停留的信息(商品图片信息)
function showMouseInfo_product(src) {
	var divMouse = document.getElementById("mouseinfo_select");
	divMouse.innerHTML = "";
	var productImg = document.createElement("img");
	productImg.setAttribute("src",src);
	divMouse.appendChild(productImg);
	divMouse.style.visibility = "visible";
	divMouse.style.left = document.body.scrollLeft + event.clientX + 5;
	divMouse.style.top = document.body.scrollTop;
}
//显示鼠标停留的信息(商店信息)
function showMouseInfo(shopName, address, shopType, monthCredit, maxCredit, startDate) {
	var divMouse = document.getElementById("mouseinfo_select");
	var shopInfo = new StringBuffer();
	shopInfo.append("商店名称：");shopInfo.append(shopName);shopInfo.append("<br>");
	shopInfo.append("商店类型：");shopInfo.append(shopType);shopInfo.append("<br>");
	shopInfo.append("商店地址：");shopInfo.append(address);shopInfo.append("<br>");
	shopInfo.append("本月诚信：");
	if(monthCredit >= 0){
		shopInfo.append("<font color='red'>");
		shopInfo.append(monthCredit);
		shopInfo.append("</font>");
	}else{
		shopInfo.append(monthCredit);
	}
	shopInfo.append("<br>");
	
	shopInfo.append("最大诚信：");
	if(maxCredit >= 0){
		shopInfo.append("<font color='red'>");
		shopInfo.append(maxCredit);
		shopInfo.append("</font>");
	}else{
		shopInfo.append(maxCredit);
	}
	shopInfo.append("<br>");
	
	shopInfo.append("加盟时间：");shopInfo.append(startDate);shopInfo.append("<br>");
	
	divMouse.innerHTML = shopInfo.toString();
	divMouse.style.visibility = "visible";
	divMouse.style.left = document.body.scrollLeft + event.clientX + 50;
	divMouse.style.top = document.body.scrollTop + event.clientY;
}
//隐藏鼠标停留的信息
function hideMouseInfo() {
	var divMouse = document.getElementById("mouseinfo_select");
	divMouse.style.visibility = "hidden";
}
//检查输入的搜索商品条件
function select_checkInputForSelectProduct(){
	var shopName = document.getElementById("shopName").value;
	var firstProductType = document.getElementById("firstProductType").value;
	var productName = document.getElementById("productName").value;
	var retailPrice = document.getElementById("retailPrice").value;
	var groupBuyPrice = document.getElementById("groupBuyPrice").value;
	var groupBuyQuantity = document.getElementById("groupBuyQuantity").value;
	var groupBuyCredit = document.getElementById("groupBuyCredit").value;
	var groupBuySubscription = document.getElementById("groupBuySubscription").value;
	var shopType = document.getElementById("shopType").value;
	
	//判断是否选择了商品类型条件
	if(firstProductType == ""){
		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("firstProductType").focus();
		}
		return false;
	}
	if(!select_checkProductName(productName)){
		document.getElementById("productresTR").style.display = "block";
		//清空信息列表
		cleanList("productList");
		document.getElementById("productPageControl").innerHTML = "<font color='blue'>没有找到符合条件的商品</font>";
		return false;
	}
	if(!select_checkShopName(shopName)){
		document.getElementById("productresTR").style.display = "block";
		//清空信息列表
		cleanList("productList");
		document.getElementById("productPageControl").innerHTML = "<font color='blue'>没有找到符合条件的商品</font>";
		return false;
	}
	if(!select_checkRetailPrice(retailPrice)){
		document.getElementById("productresTR").style.display = "block";
		//清空信息列表
		cleanList("productList");
		document.getElementById("productPageControl").innerHTML = "<font color='blue'>没有找到符合条件的商品</font>";
		return false;
	}
	//判断是否选择了团购条件
	if(document.getElementById("enableGroupBuy").value == "Y"){
		if(!select_checkGroupBuyPrice(groupBuyPrice)){
			document.getElementById("productresTR").style.display = "block";
			//清空信息列表
			cleanList("productList");
			document.getElementById("productPageControl").innerHTML = "<font color='blue'>没有找到符合条件的商品</font>";
		return false;
		}
		if(!select_checkGroupBuyQuantity(groupBuyQuantity)){
			document.getElementById("productresTR").style.display = "block";
			//清空信息列表
			cleanList("productList");
			document.getElementById("productPageControl").innerHTML = "<font color='blue'>没有找到符合条件的商品</font>";
			return false;
		}
		if(!select_checkGroupBuyCredit(groupBuyCredit)){
			document.getElementById("productresTR").style.display = "block";
			//清空信息列表
			cleanList("productList");
			document.getElementById("productPageControl").innerHTML = "<font color='blue'>没有找到符合条件的商品</font>";
			return false;
		}
		if(!select_checkGroupBuySubscription(groupBuySubscription)){
			document.getElementById("productresTR").style.display = "block";
			//清空信息列表
			cleanList("productList");
			document.getElementById("productPageControl").innerHTML = "<font color='blue'>没有找到符合条件的商品</font>";
			return false;
		}
	}
	return true;
}

function  select_checkProductName(value) {
	if(value.length > 50){
		return false;
	}else{
		return true;
	}
}
function  select_checkRetailPrice(value) {
	reg = /^[0-9]*\.?[0-9]{0,2}$/;
	if (!reg.test(value) || value < 0 || value >= 10000000) {
		return false;
	} else {
		return true;
	}
}
function  select_checkGroupBuyPrice(value) {
	reg = /^[0-9]*\.?[0-9]{0,2}$/;
	if (!reg.test(value) || value < 0 || value >= 10000000) {
		return false;
	} else {
		return true;
	}
}
function  select_checkGroupBuyQuantity(value) {
	reg = /^[0-9]{0,3}$/;
	if (!reg.test(value) || value < 0 || value >= 1000) {
		return false;
	} else {
		return true;
	}
}
function  select_checkGroupBuyCredit(value) {
	reg = /^[0-9]{0,3}$/;
	if (!reg.test(value) || value < 0 || value >= 1000) {
		return false;
	} else {
		return true;
	}
}
function  select_checkGroupBuySubscription(value) {
	reg = /^[0-9]{0,6}$/;
	if (!reg.test(value) || value < 0 || value >= 1000000) {
		return false;
	} else {
		return true;
	}
}
