var kindFile = "xml/kind.xml";
var scopeFile = "xml/scope.xml";
var scaleFile = "xml/scale.xml";
var searchFile = "xml/search.xml";
var kindXmldom;
var scopeXmldom;
var scaleXmldom;
var searchXmldom;

function checkCorporation() {
	try {
		var temp = "错误信息：\r\n\r\n";
		var  i = 0;
		if($F("name").trim( ) == "") {
			i++;
			temp += i + ". 企业名称不能输入空字符串\r\n";
			if(i == 1)
				$("name").focus();
		}
		
		if($F("categoryid").trim( ) == "0") {
			i++;
			temp += i + ". 请选择企业行业\r\n";
			if(i == 1)
				$("categoryid").focus();
		}
		
		if($F("groupid").trim( ) == "0") {
			i++;
			temp += i + ". 请选择企业所在地区\r\n";
			if(i == 1)
				$("groupid").focus();
		}
		/*
		if($F("address").trim( ) == "") {
			i++;
			temp += i + ". 详细地址不能输入空字符串\r\n";
			if(i == 1)
				$("address").focus();
		}
		
		if($F("kind").trim( ) == "") {
			i++;
			temp += i + ". 请输入企业性质\r\n";
			if(i == 1)
				$("kind").focus();
		}
		
		if($F("scope").trim( ) == "") {
			i++;
			temp += i + ". 请输入经营范围\r\n";
			if(i == 1)
				$("scope").focus();
		}
		
		if($F("scale").trim( ) == "") {
			i++;
			temp += i + ". 请选择企业规模\r\n";
			if(i == 1)
				$("scale").focus();
		}
		
		var regx = /^\([\+\d]+?\)\d+?[-]\d+$/gi
		var p = "(" + $F("phone1").trim() + ")" + $F("phone2").trim() + "-" + $F("phone3").trim();
		if(!regx.test(p)) {
			i++;
			temp += i + ". 请输入正确的联系电话\r\n";
			if(i == 1) {
				$("phone1").focus();
				$("phone1").select();
			}
		}
		*/
		
		if($("ctype") && $F("ctype")=="0") {
			i++;
			temp+=i+". 请选择企业类型\r\n";
			if(i==1)
				$("ctype").focus();
		}
		
		if($F("detail").trim( ) == "") {
			i++;
			temp += i + ". 企业描述不能输入空字符串\r\n";
			if(i == 1)
				$("detail").focus();
		}
		
		if($("checkCode") && $F("checkCode")=="") {
			i++;
			temp+=i+". 必须输入验证码\r\n";
			if(i==1)
				$("checkCode").focus();
		}
		
		if($("checkCorporationComment") && $("checkCorporationComment").checked) {
			if($F("username").trim( ) == "") {
				i++;
				temp += i + ". 用户名不能输入空字符串\r\n";
				if(i == 1)
					$("username").focus();
			}
			
			if($F("content").trim( ) == "") {
				i++;
				temp += i + ". 评论留言不能输入空字符串\r\n";
				if(i == 1)
					$("content").focus();
			} else if($F("content").trim().length < 20) {
				i++;
				temp += i + ". 评论留言必须输入超过20字\r\n";
				if(i == 1)
					$("content").focus();
			}
			
			if(!$("permitButton").checked) {
				i++;
				temp += i + ". 请确定已经阅读注意事项\r\n";
			}
		}
	
		if(i != 0) {
			alert(temp);
			return false;
		}
		
		$("submitButton").disabled = true;
		$("resetButton").disabled = true;
		return true;
	} catch(e) {
		alert(e.message);
		return false;
	}
}

function checkDeleteCorporation() {
	with(document.all) {
		if(!document.all.ids) {
			alert("你没有选择要删除的企业");
			return false;
		}
		var isChecked=false;
		if(!ids.length){
			if(!ids.checked) {
				alert("你没有选择要删除的企业");
				return false;
			}
		}else{
			for(var i=0;i<ids.length;i++) {
				if(ids[i].checked)break;
				if(i==ids.length-1) {
					alert("你没有选择要删除的企业");
					return false;
				}
			}
		}
	}
	
	var flag = confirm("确定要删除选择的企业吗？")?true:false;
	if(flag) {
		$("action").value = "delete";
	}
	return flag;
}

function checkTidyCorporation() {
	with(document.all) {
		if(!document.all.ids) {
			alert("你没有选择要整理的企业");
			return false;
		}
		var isChecked=false;
		if(!ids.length){
			if(!ids.checked) {
				alert("你没有选择要整理的企业");
				return false;
			}
		}else{
			for(var i=0;i<ids.length;i++) {
				if(ids[i].checked)break;
				if(i==ids.length-1) {
					alert("你没有选择要整理的企业");
					return false;
				}
			}
		}
	}
	
	var flag = confirm("确定要整理选择的企业吗？")?true:false;
	if(flag) {
		$("action").value = "tidy";
	}
	return flag;
}

function getKindSelectInput(sId, sName, sKind, desc, requireText) {
	if(!kindXmldom || kindXmldom == null) {
		kindXmldom = Try.these(
						function() {
							var dom = new ActiveXObject("Microsoft.XMLDOM");
							dom.setProperty("SelectionLanguage", "XPath");
							return dom;
						},
						function() {return document.implementation.createDocument("", "", null)}
					);
		kindXmldom.async = false;
		kindXmldom.load(kindFile);
	}
	
	var temp = '<select ' + (requireText ? '' : (' id="' + sId + '" name="' + sName + '" ')) + ' onchange="$(\'' + sId + '\').value = this.value;" style="width: 150px;">';
	temp += '<option value="" selected="selected">' + (desc ? desc : '不分公司性质') + '</option>';
	if(kindXmldom) {
		var root = kindXmldom.documentElement;
		var kindArr = $A(root.childNodes);
		var id, name;
		kindArr.each(function(kind, index) {
						   if(kind.attributes) {
							   id = getNodeAttribute(kind, "id");
							   name = getNodeAttribute(kind, "name");
							   temp += '<option value="' + name + '" ' + (sKind == name ? 'selected="selected"' : '') + '>' + name + '</option>';
						   }
						});
	}
	temp += '</select>';
	if(requireText) {
		temp += '&nbsp;&nbsp;&nbsp;<input type="text" id="' + sId + '" name="' + sName + '" class="ipttxt1" size="30" maxLength="50" value="' + sKind + '" />';
	}
	
	return temp;
}

function getScopeSelectInput(sId, sName, sScope, desc, requireText) {
	if(!scopeXmldom || scopeXmldom == null) {
		scopeXmldom = Try.these(
						function() {
							var dom = new ActiveXObject("Microsoft.XMLDOM");
							dom.setProperty("SelectionLanguage", "XPath");
							return dom;
						},
						function() {return document.implementation.createDocument("", "", null)}
					);
		scopeXmldom.async = false;
		scopeXmldom.load(scopeFile);
	}
	
	var temp = '<select ' + (requireText ? '' : (' id="' + sId + '" name="' + sName + '" ')) + ' onchange="$(\'' + sId + '\').value = this.value;" style="width: 150px;">';
	temp += '<option value="" selected="selected">' + (desc ? desc : '所有经营范围') + '</option>';
	if(scopeXmldom) {
		var root = scopeXmldom.documentElement;
		var scopeArr = $A(root.childNodes);
		var id, name;
		scopeArr.each(function(scope, index) {
						   if(scope.attributes) {
							   id = getNodeAttribute(scope, "id");
							   name = getNodeAttribute(scope, "name");
							   temp += '<option value="' + name + '" ' + (sScope == name ? 'selected="selected"' : '') + '>' + name + '</option>';
						   }
						});
	}
	temp += '</select>';
	if(requireText) {
		temp += '&nbsp;&nbsp;&nbsp;<input type="text" id="' + sId + '" name="' + sName + '" class="ipttxt1" size="30" maxLength="50" value="' + sScope + '" />';
	}
	
	return temp;
}

function getScaleSelectInput(sId, sName, sScale, desc) {
	if(!scaleXmldom || scaleXmldom == null) {
		scaleXmldom = Try.these(
						function() {
							var dom = new ActiveXObject("Microsoft.XMLDOM");
							dom.setProperty("SelectionLanguage", "XPath");
							return dom;
						},
						function() {return document.implementation.createDocument("", "", null)}
					);
		scaleXmldom.async = false;
		scaleXmldom.load(scaleFile);
	}
	
	var temp = '<select id="' + sId + '" name="' + sName + '" style="width: 150px;">';
	temp += '<option value="" selected="selected">' + (desc ? desc : '不论公司规模') + '</option>';
	if(scaleXmldom) {
		var root = scaleXmldom.documentElement;
		var scaleArr = $A(root.childNodes);
		var id, name;
		scaleArr.each(function(scale, index) {
						   if(scale.attributes) {
							   id = getNodeAttribute(scale, "id");
							   name = getNodeAttribute(scale, "name");
							   temp += '<option value="' + name + '" ' + (sScale == name ? 'selected="selected"' : '') + '>' + name + '</option>';
						   }
						});
	}
	temp += '</select>';
	
	return temp;
}

function doError(request) {
	$("rspMsg").innerHTML = "读取数据失败(code=" + request.status + ")，请重试……";
}

function createNodataRow(table, residualRow, colspan){
	if(table) {
		while(table.rows.length > residualRow) {
			table.deleteRow(residualRow);
		}
		var row = table.insertRow(-1);
		var cell = row.insertCell(-1);
		with(cell) {
			align = "center";
			colSpan = colspan;
			//style.paddingTop = "10px";
			style.color = "#FF0000";
			innerHTML = '不存在任何企业信息';
		}
	}
}

function createLoadingRow(table, residualRow, colspan){
	if(table) {
		while(table.rows.length > residualRow) {
			table.deleteRow(residualRow);
		}
		var row = table.insertRow(-1);
		var cell = row.insertCell(-1);
		with(cell) {
			align = "center";
			colSpan = colspan;
			style.paddingTop = "10px";
			innerHTML = '<img src="images/loading.gif" border="0" align="absmiddle" />&nbsp;正在加载数据，请稍侯......';
		}
	}
}

function loadIndexRanking() {
	var paras = "showCount=10&showPage=index";
	loadRanking(paras);
}

function clickSearchRanking() {
	var paras = "temp=" + Math.random();
	if($F("categoryid") != "0") {
		paras += "&categoryid=" + $F("categoryid");
	}
	if($F("groupid") != "0") {
		paras += "&groupid=" + $F("groupid");
	}
	if($F("kind") != "") {
		paras += "&kind=" + $F("kind");
	}
	if($F("scope") != "") {
		paras += "&scope=" + $F("scope");
	}
	if($F("scale") != "") {
		paras += "&scale=" + $F("scale");
	}
	loadRanking(paras);
}

function disabledButton(flag) {
	if($("searchButton")) {
		$("searchButton").disabled = flag;
	}
}

function loadRanking(paras) {
	try {
		disabledButton(true);
		createLoadingRow($("redRanking"), 1, 3);
		createLoadingRow($("blackRanking"), 1, 3);
		createLoadingRow($("hotRanking"), 1, 3);
		createLoadingRow($("famousRanking"), 0, 2);
		createLoadingRow($("newRanking"), 0, 1);
		createLoadingRow($("commendRanking"), 0, 1);
		var request_url = "loadRanking.asp";        // 需要获取内容的url
		var myAjax = new Ajax.Request(request_url,{ // 将request_url返回内容绑定到id为''的容器中
			method       : 'get', //HTTP请求的方法,get or post
			encoding     : "GBK",
			parameters   : paras,
			asynchronous : true, 
			contentType  : "application/x-www-form-urlencoded",
			onFailure    : doError, //失败的时候调用 reportError 函数
			onComplete   : loadRankingCallBack     //内容获取完毕的时候
		});
	
	}catch(e){
		disabledButton(false);
		doError(e.message);
	}
}

function loadRankingCallBack(request) {
	try {
		if(request.status == 200) { // 加载成功  
			var dom = request.responseXML;
			var root = dom.documentElement;
			if(!root || root == null) {
				dom = new ActiveXObject("Microsoft.XMLDOM");
				dom.loadXML(request.responseText);
				root = dom.documentElement;
			}
			
			if(!root || root == null) {
				$("rspMsg").innerHTML = "读取数据失败，请重试……";
			} else {
				createRankingList(root);
			}
		} else {
			$("rspMsg").innerHTML = "读取数据失败(code=" + request.status + ")，请重试……";
		}
	} catch(e) {
		$("rspMsg").innerHTML = "读取数据失败(" + e.message + ")，请重试……";
	}
	
	disabledButton(false);
}

function createRankingList(root) {
	var table = $("redRanking");
	var rankingNode;
	if(table) {
		rankingNode = root.selectSingleNode("ranking[@type='red']");
		createCorporationRankingNode(table, rankingNode, "#FF0000", true);
	}
	
	table = $("blackRanking");
	if(table) {
		rankingNode = root.selectSingleNode("ranking[@type='black']");
		createCorporationRankingNode(table, rankingNode, "#000000", true);
	}
	
	table = $("hotRanking");
	if(table) {
		rankingNode = root.selectSingleNode("ranking[@type='hot']");
		createCorporationRankingNode(table, rankingNode, "#6600CC", false);
	}
	
	table = $("newRanking");
	if(table) {
		rankingNode = root.selectSingleNode("ranking[@type='new']");
		try {
			createCorporationNode(table, rankingNode, "listCorporation.asp?o=8");
		} catch(e) {
			alert(e.message);
		}
	}
	
	table = $("commendRanking");
	if(table) {
		rankingNode = root.selectSingleNode("ranking[@type='commend']");
		try {
			createCorporationNode(table, rankingNode, "listCorporation.asp?ctype=3&o=8");
		} catch(e) {
			alert(e.message);
		}
	}
	
	table = $("famousRanking");
	if(table) {
		rankingNode = root.selectSingleNode("ranking[@type='famous']");
		try {
			createFamousCorporationNode(table, rankingNode, "listCorporation.asp?ctype=2&o=8");
		} catch(e) {
			alert(e.message);
		}
	}
}

function createFamousCorporationNode(table, rankingNode, morePage) {
	table.deleteRow(0);
	if(rankingNode) {
		var nodeArr = $A(rankingNode.childNodes);
		var cid, cname, point, comments;
		var row, cell;
		if(nodeArr.length == 0) {
			createNodataRow(table, 0, 2);
		} else {
			var len = 0;
			var index = 0;
			nodeArr.each(function(node, i) {
							   if(node.attributes) {
								   cid = getNodeAttribute(node, "id");
								   cname = getNodeAttribute(node, "name");
								   point = getNodeAttribute(node, "point");
								   comments = getNodeAttribute(node, "comments");
								   len = cname.length;
								   if(index % 2 == 0) {
								   	  row = table.insertRow(-1);
								   }
								   cell = row.insertCell(-1);
								   with(cell) {
									   width = 360;
									   align = "left";
									   bgColor = "#FFFFFF";
									   innerHTML = "· <a href='readCorporation.asp?id=" + cid + "' target='_blank' title='" + cname + "'>" + 
									   				(len > 30 ? (cname.substring(0, 27) + "...") : cname) + "</a>";
								   }
								   index++;
							   }
							});
		}
		
	    row = table.insertRow(-1);
	    cell = row.insertCell(-1);
	    with(cell) {
		   width = "100%";
		   colSpan = 2;
		   align = "right";
		   bgColor = "#FFFFFF";
		   style.paddintRight = 3;
		   innerHTML = '<a href="' + morePage + '"><img src="images/more.gif" border="0" align="absmiddle" />' +
						'&nbsp;更多...</a>';
	    }
	}
}

function createCorporationNode(table, rankingNode, morePage) {
	table.deleteRow(0);
	if(rankingNode) {
		var nodeArr = $A(rankingNode.childNodes);
		var cid, cname, point, comments;
		var row, cell;
		if(nodeArr.length == 0) {
			createNodataRow(table, 0, 1);
		} else {
			var len = 0;
			nodeArr.each(function(node, index) {
							   if(node.attributes) {
								   cid = getNodeAttribute(node, "id");
								   cname = getNodeAttribute(node, "name");
								   point = getNodeAttribute(node, "point");
								   comments = getNodeAttribute(node, "comments");
								   len = cname.length;
								   row = table.insertRow(-1);
								   cell = row.insertCell(-1);
								   with(cell) {
									   width = 250;
									   align = "left";
									   bgColor = "#FFFFFF";
									   innerHTML = "· <a href='readCorporation.asp?id=" + cid + "' target='_blank' title='" + cname + "'>" + 
									   				(len > 18 ? (cname.substring(0, 15) + "...") : cname) + "</a>";
								   }
							   }
							});
		}
		
	    row = table.insertRow(-1);
	    cell = row.insertCell(-1);
	    with(cell) {
		   width = "100%";
		   align = "right";
		   bgColor = "#FFFFFF";
		   style.paddintRight = 3;
		   innerHTML = '<a href="' + morePage + '"><img src="images/more.gif" border="0" align="absmiddle" />' +
						'&nbsp;更多...</a>';
	    }
	}
}

function createCorporationRankingNode(table, rankingNode, color, isPoint) {
	table.deleteRow(1);
	if(rankingNode) {
		var nodeArr = $A(rankingNode.childNodes);
		var cid, cname, point, comments;
		var row, cell;
		if(nodeArr.length == 0) {
			createNodataRow(table, 1, 3);
		} else {
			var index = 0;
			nodeArr.each(function(node, i) {
							   if(node.attributes) {
								   index++;
								   cid = getNodeAttribute(node, "id");
								   cname = getNodeAttribute(node, "name");
								   point = parseFloat(getNodeAttribute(node, "point"));
								   comments = getNodeAttribute(node, "comments");
							       point = Math.round(point * 10) / 10;
								   row = table.insertRow(-1);
								   cell = row.insertCell(-1);
								   with(cell) {
									   width = 30;
									   noWrap = "nowrap";
									   align = "left";
									   style.color = color;
									   innerHTML = index;
								   }
								   cell = row.insertCell(-1);
								   with(cell) {
									   align = "left";
									   innerHTML = '<a href="readCorporation.asp?id=' + cid + '" target="_blank">' + cname + '</a>';
								   }
								   cell = row.insertCell(-1);
								   with(cell) {
									   align = "left";
									   width = 40;
									   noWrap = "nowrap";
									   style.color = color;
									   innerHTML = isPoint ? point : comments;
								   }
							   }
							});
		}
	}
}

function printSearchInCommonUse() {
	if(!searchXmldom || searchXmldom == null) {
		searchXmldom = Try.these(
						function() {
							var dom = new ActiveXObject("Microsoft.XMLDOM");
							dom.setProperty("SelectionLanguage", "XPath");
							return dom;
						},
						function() {return document.implementation.createDocument("", "", null)}
					);
		searchXmldom.async = false;
		searchXmldom.load(searchFile);
	}
	
	var temp = "<strong>常用搜索: </strong>&nbsp;";
	if(searchXmldom) {
		var root = searchXmldom.documentElement;
		var nodeArr = $A(root.childNodes);
		var id, name, queryString;
		nodeArr.each(function(node, index) {
						   if(node.attributes) {
							   id = getNodeAttribute(node, "id");
							   name = getNodeAttribute(node, "name");
							   queryString = getNodeAttribute(node, "queryString");
							   temp += '<a href="listCorporation.asp?' + queryString + '">[' + name + ']</a>&nbsp;';
						   }
						});
	}
	
	document.write(temp);
}

function orderColumn(colNum) {
	var o1 = parseInt($F("o"))
	var o2 = 0;
	switch(colNum) {
		default:
		case 1:
			o2 = o1 == 0 ? 1 : 0;
			break;
		case 2:
			o2 = o1 == 2 ? 3 : 2;
			break;
		case 3:
			o2 = o1 == 4 ? 5 : 4;
			break;
		case 4:
			o2 = o1 == 6 ? 7 : 6;
			break;
		case 5:
			o2 = o1 == 8 ? 9 : 8;
			break;
	}

	$("o").value = o2;
	$("action").disabled = true;
	document.theForm.submit();
}

function expandCorporation(cid) {
	var table = $("brief_" + cid);
	if(table) {
		table.style.display = table.style.display != "none" ? "none" : "";
	}
}

function corporationComment(flag) {
	$('corporationCommentRow').style.display = flag ? "" : "none";
}