﻿// 行业层
function CallingLayer() {

	var opts = new Array();
	
	var finish = false;
	
	this.form = "";
						
	this.getCallingList = function() {	
	
		var ajax = new ajaxRequest();
		
		ajax.url = url + "/Tools/Ajax.aspx?t=calling";
		
		ajax.callback = function(xmlobj) {
		
			opts = new Array();
			
			var node = null;
			
			var xpath = "//data/calling";
			
			var DOMDocument = xmlobj.responseXML;
			
			if( isIE() ) {	//IE
				node = DOMDocument.selectNodes(xpath);
			}
			else {	// Mozilla FireFox
			
				var NodeArray = new Array();
				
				var xPathResult = DOMDocument.evaluate(xpath, DOMDocument, DOMDocument.createNSResolver(DOMDocument.documentElement), XPathResult.ORDER_NODE_ITERATOR_TYPE, null);
				
				if(xPathResult) {
				
					var oNode = xPathResult.iterateNext();
					
					while(oNode) {
					
						NodeArray[NodeArray.length] = oNode;
						
						oNode = xPathResult.iterateNext();
					
					}
				
				}
				
				node = NodeArray;			
			}
						
			var patn = new RegExp("\\d{3}", "ig");
						
			for(var i=0; i<node.length; i++) {
				
				var opt = document.createElement("option");		
				
				opt.value = node[i].getAttribute("callid");
				
				opt.text = node[i].getAttribute("name");

				opts[i] = opt;				
			}
			
			finish = true;
						
		}
		
		ajax.send();

	}
	

	// 初始化区域层
	this.initialize = function() {
	
		this.getCallingList("");
		
		var html = "";
		
		html += '<div class=\"popupLayer\">';
		html += '<div class="popupTitle"><h2>请选择公司所属行业 (您最多可以选择1个行业)</h2><a onclick="javascript:filterLayer.cancel();filterLayer.hideLayer(\'popupCalling\');">[确定]</a></div>';
		html += '<div class="popupContent">';
		html += '<div class="popupCategory">';
		html += '请选择公司所属行业';
		html += '</div>';
		html += '<div class="popupList">'
		html += '<dl id="popupCallingContent">';
		html += '</dl>';
		html += '</div>';
		html += '</div>';
		html += '<dl id="popupCallingSelected" class="popupSelected">';
		html += '<dt>您已经选择的行业是：<a href="javascript:;" onclick="javascript:CallingLayer.Clear();">清空所有选项</a></dt>';
		html += '</dl>';
		html += '<a class="layerok" href="javascript:CallingLayer.enter();filterLayer.cancel();filterLayer.hideLayer(\'popupCalling\');">确定</a>';
		html += '</div>';
		
		setTimeout("popupCalling.GetCalling()", 1000);		
		
		return html;
	};	
	
	this.GetCalling = function() {
		if( !finish )
		{
			setTimeout("popupCalling.GetCalling()", 1000);
			return;
		}
				
		CallingLayer.SelectedChangedCalling();
	}
	
	// 选择框变换
	CallingLayer.SelectedChangedCalling = function() {

		var ajax = new ajaxRequest();
		
		ajax.url = url + "/Tools/Ajax.aspx?t=calling";
														
		ajax.callback = function(xmlobj) {
					
			var node = null;
			
			var xpath = "//data/calling";
			
			var DOMDocument = xmlobj.responseXML;
			
			if( isIE() ) {	//IE
				node = DOMDocument.selectNodes(xpath);
			}
			else {	// Mozilla FireFox
			
				var NodeArray = new Array();
				
				var xPathResult = DOMDocument.evaluate(xpath, DOMDocument, DOMDocument.createNSResolver(DOMDocument.documentElement), XPathResult.ORDER_NODE_ITERATOR_TYPE, null);
				
				if(xPathResult) {
				
					var oNode = xPathResult.iterateNext();
					
					while(oNode) {
					
						NodeArray[NodeArray.length] = oNode;
						
						oNode = xPathResult.iterateNext();
					
					}
				
				}
				
				node = NodeArray;			
			}
			
			var lstCalling = $("popupCallingContent");
			var innerHTML = ""
			
			var patn = new RegExp("\\d{3}", "ig");
									
			for(var i=0; i<node.length; i++) {
				
				innerHTML += '<dd><input type="checkbox" id="cb_calling_' + node[i].getAttribute("callid") + '" name="cb_calling_' + node[i].getAttribute("callid") + '" value="' + node[i].getAttribute("callid") + '" onclick="javascript:CallingLayer.ItemClick(this);" /><label id="lbl_calling_' + node[i].getAttribute("callid") + '" for="cb_calling_' + node[i].getAttribute("callid") + '">' + node[i].getAttribute("name") + '</label></dd>';
				
			}
			lstCalling.innerHTML = innerHTML;
			
			for(var i=0; i<popupCalling.Value.length; i++) {
				if( $("cb_calling_" + popupCalling.Value[i])!=null ) {
					$("cb_calling_" + popupCalling.Value[i]).checked = true;
				}
			}
			
		}
		
		ajax.send();
	}
		
	// 单项选择
	CallingLayer.ItemClick = function(obj) {
		var nItem = 0;
		
		if( obj.checked ) {
			if( popupCalling.Value.length>0 ) {
				obj.checked = false;
				alert("您最多可以选择1个行业");
				return;				
			}
		}
		
		var nIndex = -1;
		for(var i=0; i<popupCalling.Value.length; i++) {
			if( popupCalling.Value[i]==obj.value ) {
				nIndex = i;
				break;
			}
		}
		
		if( obj.checked ) {
			// 不存在该选择，则添加
			if( nIndex==-1 ) {
				popupCalling.Value.push(obj.value);
				popupCalling.Text.push($("lbl_calling_" + obj.value).innerHTML);
			}
		}
		else {
			// 存在则删除
			if( nIndex!=-1 ) {
				popupCalling.Value.splice(nIndex, 1);
				popupCalling.Text.splice(nIndex, 1);
				if( $("cb_calling_" + obj.value)!=null ) {
					$("cb_calling_" + obj.value).checked = false;
				}
			}
		}
				
		var innerHTML = "";
		innerHTML += '<dt>您已经选择的行业是：<a href="javascript:;" onclick="javascript:CallingLayer.Clear();">清空所有选项</a></dt>';
		
		for(var i=0; i<popupCalling.Value.length; i++) {
			innerHTML += '<dd><input type="checkbox" id="cb_calling_slt_' + popupCalling.Value[i] + '" name="cb_calling_slt_' + popupCalling.Value[i] + '" value="' + popupCalling.Value[i] + '" checked="checked" onclick="javascript:CallingLayer.ItemClick(this);" /><label id="lbl_calling_slt_' + popupCalling.Value[i] + '" for="cb_calling_slt_' + popupCalling.Value[i] + '">' + popupCalling.Text[i] + '</label></dd>';
		}
		$("popupCallingSelected").innerHTML = innerHTML;
	}
	
	// 清空所有选择
	CallingLayer.Clear = function() {
		popupCalling.Value.length = popupCalling.Text.length = 0;
		
		var objs = $("popupCallingContent").getElementsByTagName("input");
		for(var i=0; i<objs.length; i++) {
			objs[i].disabled = false;
			objs[i].checked = false;
		}
		
		var innerHTML = "";
		innerHTML += '<dt>您已经选择的行业是：<a href="javascript:;" onclick="javascript:CallingLayer.Clear();">清空所有选项</a></dt>';
		
		for(var i=0; i<popupCalling.Value.length; i++) {
			innerHTML += '<dd><input type="checkbox" id="cb_calling_slt_' + popupCalling.Value[i] + '" name="cb_calling_slt_' + popupCalling.Value[i] + '" value="' + popupCalling.Value[i] + '" checked="checked" onclick="javascript:CallingLayer.ItemClick(this);" /><label id="lbl_calling_slt_' + popupCalling.Value[i] + '" for="cb_calling_slt_' + popupCalling.Value[i] + '">' + popupCalling.Text[i] + '</label></dd>';
		}
		$("popupCallingSelected").innerHTML = innerHTML;
	}
	
	CallingLayer.ok = function(obj, form) {	
		if( $(form)==null ) return false;
		if( form=="" ) return false;
		
		var callingInput;
		if( $("calling")==null ) {
			callingInput = document.createElement("input");
			callingInput.setAttribute("type", "hidden");
			callingInput.id = "calling";
			callingInput.name = "calling";
			$(form).appendChild(callingInput);
		}
		else {
			callingInput = $("calling");
		}
		
		callingInput.value = "";
		$(obj).value = "";
		for(var i=0; i<popupCalling.Value.length; i++) {
			callingInput.value += popupCalling.Value[i] + "-";
			$(obj).value += popupCalling.Text[i] + ",";
		}
		callingInput.value = callingInput.value.substring(0, callingInput.value.length-1);
		$(obj).value = $(obj).value.substring(0, $(obj).value.length-1);
		if( $(obj).value.length==0 ) {
			$(obj).value = "请选择";
		}
	}
	
	// 来自哪个表格
	CallingLayer.form = function(str) {
		popupCalling.from = str;
	}
	
	// 当点击确定的时候
	CallingLayer.enter = function() {
		if( popupCalling.from == "form_search" )
		{
			CallingLayer.ok("btnSelectCode", "form_search");
		}
		else
		{			
			CallingLayer.ok("btnComCode", "form_search1");
		}
	}	
	
	this.Text = new Array();
	this.Value = new Array();
}

var popupCalling = new CallingLayer();