/**
 *定义命名空间 电子围栏
 */
var electronicFence = {};

var fenceloadcount = 0;

var fenceEventTree = null;

var fenceType_ = 2;

var isEnableFence = false;

electronicFence.tree = {
	treeObject : null,
	fenceJson : null,
	curNodeId : -1,
	isShow : false,
	init : function(){
	
		if(map == undefined ){
		
			fenceloadcount ++;

				setTimeout("electronicFence.tree.init()",1000);

			return;
		}
		
		fenceloadcount = 0;
		
		this.initGroup();
		electronicFence.tree.menu.init();
		electronicFence.win.init();
		electronicFence.tree.addAllelectronicFenceNode();
		electronicFence.draw.init();
		initFenceEventTree();
		/*
		$(document).click(function(event){
			electronicFence.draw.clearFence();
			$("#fence-msg").hide();	
										  
		});*/
	},
	initGroup : function(){
	
		var groupNodeTreeStr="";
		
		if(fenceType_ == 2){
		
			groupNodeTreeStr="[{\"id\":\"fence_g_"+0+"\",\"open\":\"true\",\"title\":\""+electronicFenceLanguage.sywl+"\",\"name\":\""+electronicFenceLanguage.sywl+"\",\"icon\":\""+electronicFenceUrl.allDom+"\",\"pId\": \"-1\","+
								"\"children\": [{\"id\":\"fence_g_"+1+"\",\"title\":\""+electronicFenceLanguage.dbx+"\",\"name\":\""+electronicFenceLanguage.dbx+"\",\"icon\":\""+markerUrl.groupNodeicon+"\",\"count\":\"0\",\"pId\": \"0\"}"+
								//"{\"id\":\"fence_g_"+2+"\",\"title\":\""+electronicFenceLanguage.ghlx+"\",\"name\":\""+electronicFenceLanguage.ghlx+"\",\"icon\":\""+markerUrl.groupNodeicon+"\",\"count\":\"0\",\"pId\": \"0\"}"+
							"]}]";
		}else{
		
			groupNodeTreeStr="[{\"id\":\"fence_g_"+0+"\",\"open\":\"true\",\"title\":\""+electronicFenceLanguage.sywl+"\",\"name\":\""+electronicFenceLanguage.sywl+"\",\"icon\":\""+electronicFenceUrl.allDom+"\",\"pId\": \"-1\","+
								"\"children\": [{\"id\":\"fence_g_"+1+"\",\"title\":\""+electronicFenceLanguage.dbx+"\",\"name\":\""+electronicFenceLanguage.dbx+"\",\"icon\":\""+markerUrl.groupNodeicon+"\",\"count\":\"0\",\"pId\": \"0\"},"+
								"{\"id\":\"fence_g_"+2+"\",\"title\":\""+electronicFenceLanguage.ghlx+"\",\"name\":\""+electronicFenceLanguage.ghlx+"\",\"icon\":\""+markerUrl.groupNodeicon+"\",\"count\":\"0\",\"pId\": \"0\"}"+
							"]}]";
		}
	    
	    groupNodeTreeStr=$.parseJSON(groupNodeTreeStr);
		
		var setting={
	    		callback: {
					onClick:electronicFence.tree.OnClick,
					onRightClick: electronicFence.tree.OnRightClick
				},
				data: {
					simpleData: {
						enable: true,
						idKey: "id"
					}
				}		
	    }
	    
		$.fn.zTree.init($("#electronicFence_tree"),setting,groupNodeTreeStr);
		
		
		electronicFence.tree.treeObject = $.fn.zTree.getZTreeObj("electronicFence_tree");
	},
	addNode : function(type,name,id){//2对划线,1多边形
		if(type == fenceType_){
			return;
		}
		var curNode = electronicFence.tree.treeObject.getNodeByParam("id",id);
		
		if(curNode){
			
			curNode.name = name;
			
			electronicFence.tree.treeObject.updateNode(curNode);		
			
			return;
			
		}
		
		var newNode = [{'id':id,'name':name,"icon":type==1?electronicFenceUrl.polygon:electronicFenceUrl.line}];
		
		var parentNode=electronicFence.tree.treeObject.getNodeByParam("id","fence_g_"+(type==1?1:2));
		
		if(parentNode!=null){
		
			newNode=electronicFence.tree.treeObject.addNodes(parentNode,newNode,true);
			
			parentNode.count=parseInt(parentNode.count)+1;
			
			parentNode.name =parentNode.title+"("+parentNode.count+")";
			
	        electronicFence.tree.treeObject.updateNode(parentNode);		
	        
	        return newNode;
	        
		}else{
			return null;
		}	
		
	},
	deleteNode : function(id){
	
		var curnode=electronicFence.tree.treeObject.getNodeByParam("id",id);
		
		if(!curnode){
			return;
		}
		
		var parentNode=curnode.getParentNode();
		
	    electronicFence.tree.treeObject.removeNode(curnode);  
	    
	    parentNode.count=parseInt(parentNode.count)-1;
	    
	    if(parentNode.count==0){
	    
	    	parentNode.name =parentNode.title;
	    	
	    }else{
	    
			parentNode.name =parentNode.title+"("+parentNode.count+")";
			
	    }
	    
	    electronicFence.tree.treeObject.updateNode(parentNode);	
	
	},
	updateNode : function(name,id){//2对划线,1多边形		
		
		 var curnode=electronicFence.tree.treeObject.getNodeByParam("id",id);
			  	 
	  	 if (curnode!=null) {
	  	 
			curnode.name = name;
			
			electronicFence.tree.treeObject.updateNode(curnode);
			
			return curnode;
			
		 }else{
		 	return null;
		 }
		
		
	},
	addAllelectronicFenceNode : function (){
	
		isEnableFence = modelPremiss();
		
		if(!isEnableFence){
		
			return;	
			
		}
		
		$.ajax({
	        type: "POST",
	        dataType: "json",
	        async : false,
	        url: "json/fenceAction_getUserFence.action",
	        success: function(json) {	
	        	         
		         if(json.flag==true) {
		         
		         		electronicFence.tree.fenceJson = json;
		         		
		         		for(var i=0;i<json.totalcount;i++){
							if(json.data[i].fenceType != fenceType_){
     							electronicFence.tree.addNode(json.data[i].fenceType,json.data[i].name,json.data[i].id);//2对划线,1多边形
							}						
                 		}
             	}
                 
	        },
	        error: function() {}
	   });	
	   
	},
	show: function(){
		$("#marker_pane").hide();
		$("#electronicFence_pane").show();
		
		$("#marker_dot").css("left","75px");
		
		electronicFence.tree.isShow = true;
	},
	hide : function(){
		$("#electronicFence_pane").hide();
		$("#marker_pane").show();
		
		$("#marker_dot").css("left","35px");
		
		electronicFence.tree.isShow = false;
	},
	OnRightClick : function(event, treeId, treeNode){	
	
		if(!treeNode){
			return;
		}
		if(!isEnableFence){
			alert(electronicFenceLanguage.wkqgfw);
			return;
		}
		if(treeNode.id == "fence_g_1"){
			electronicFence.tree.menu.showRMenu("group", event.clientX, event.clientY);
			$("#electronicFence_type").val(1);
		}else if(treeNode.id == "fence_g_2"){
			electronicFence.tree.menu.showRMenu("group", event.clientX, event.clientY);
			$("#electronicFence_type").val(2);
		}else if(treeNode.id == "fence_g_0"){
			electronicFence.tree.menu.showRMenu("root", event.clientX, event.clientY);
		}else{
			
			electronicFence.draw.clearFence();
			
			var parentNodeId = treeNode.getParentNode().id;
			
			if($("#fence-msg").is(":hidden")){
     				
     			$("#fence-msg").show();
     					
     		}
			
			if(parentNodeId == "fence_g_1"){
				
				beforeCloseSave();
				
				$("#electronicFence_type").val(1);
				
				$("#fence_lien").hide();
				
				$("#fence_poly").show();
				
			}else if(parentNodeId == "fence_g_2"){
			
				$("#electronicFence_type").val(2);	
				
				$("#fence_poly").hide();
				
				$("#fence_lien").show();										
				
			}
			
			for(var i=0;i<electronicFence.tree.fenceJson.totalcount;i++){
				
				if(electronicFence.tree.fenceJson.data[i].id == treeNode.id){
				
					electronicFence.tree.curNodeId = treeNode.id;
					
					break;
					
				}
			
			}
		
			electronicFence.tree.menu.showRMenu("childsNode", event.clientX, event.clientY);
			
			electronicFence.draw.showFence(treeNode.id);
			
		}
		
	},
	OnClick : function (event, treeId, treeNode){
			if(!isEnableFence){
				alert(electronicFenceLanguage.wkqgfw);
				return;
			}
			if(treeNode.id == "fence_g_1" || treeNode.id == "fence_g_2" || treeNode.id == "fence_g_0"){
			
				if(treeNode.id == "fence_g_1"){
				
					$("#electronicFence_type_polyArea").attr("checked","checked");
					
					if(!$("#electronicFence_vehicle_binding_ContentWin").is(":hidden")){
					
						changefenceTypeRadio(1);
						
					}
					
				}else if(treeNode.id == "fence_g_2"){
				
					$("#electronicFence_type_line").attr("checked","checked");
					
					if(!$("#electronicFence_vehicle_binding_ContentWin").is(":hidden")){
					
						changefenceTypeRadio(2);
						
					}
					
				}
				
			}else{
			
				beforeCloseSave();
				
				if($("#fence-msg").is(":hidden")){
    				
    					$("#fence-msg").show();
    					
    			}
			
				electronicFence.tree.treeObject.selectNode(treeNode);
				

				
				electronicFence.tree.curNodeId = treeNode.id;	
				
				
				
				
				if(treeNode.getParentNode().id == "fence_g_1"){		
									
						
						$("#electronicFence_type").val(1);
						
						if(!$("#electronicFence_vehicle_binding_ContentWin").is(":hidden")){
					
							$("#electronicFence_type_polyArea").attr("checked","checked");
							
							
						}
						
				}else if(treeNode.getParentNode().id == "fence_g_2"){
						
						$("#electronicFence_type").val(2);
						
						
						if(!$("#electronicFence_vehicle_binding_ContentWin").is(":hidden")){
					
							$("#electronicFence_type_line").attr("checked","checked");
							
						}
						
				}
				
				electronicFence.draw.showFence(treeNode.id);
				
				
				
				if(!$("#electronicFence_vehicle_binding_ContentWin").is(":hidden")){
					showFenceSelect(treeNode.id);
					showVechiletoFenceTable();
				}
							
			}
					
	}
};

electronicFence.tree.menu = {
		 electronicFence_tree_Menu : null,
		 init : function(){
		 	this.electronicFence_tree_Menu = $("#electronicFence_tree_Menu");
		 },
		 showRMenu : function(type, x, y) {
		 							
						$("#electronicFence_tree_Menu_del").hide();
						$("#electronicFence_tree_Menu_add").hide();
						$("#electronicFence_tree_Menu_update").hide();
						$("#electronicFence_tree_Menu_bingding").hide();
						$("#electronicFence_tree_Menu_event").hide();
						$("#electronicFence_tree_Menu_refresh").hide();
						$("#electronicFence_tree_Menu").hide();
						//$("#he_1").hide();
						//$("#he_2").hide();
						//$("#he_3").hide();
						
						
						
						if (type=="root") {
							//$("#electronicFence_tree_Menu_add").show();
							$("#electronicFence_tree_Menu").show();
							$("#electronicFence_tree_Menu ul").show();
							$("#electronicFence_tree_Menu_refresh").show();
						}else if(type == "childsNode"){
						
							$("#electronicFence_tree_Menu").show();
							$("#electronicFence_tree_Menu ul").show();
							
							for(var i=0;i<electronicFence.tree.fenceJson.totalcount;i++){
				
								if(electronicFence.tree.fenceJson.data[i].id == electronicFence.tree.curNodeId){
									if(electronicFence.tree.fenceJson.data[i].competence == 1){
										$("#electronicFence_tree_Menu_del").show();
										//$("#electronicFence_tree_Menu_add").show();
										$("#electronicFence_tree_Menu_update").show();
										
									//$("#electronicFence_tree_Menu_event").show();
									}
									break;								
								}
							
							}
							
							$("#electronicFence_tree_Menu_bingding").show();
							//$("#he_2").show();
							//$("#he_3").show();
						}else if(type == "group"){
							$("#electronicFence_tree_Menu").show();
							$("#electronicFence_tree_Menu ul").show();
							$("#electronicFence_tree_Menu_add").show();
						}
						
						if((y + $("#electronicFence_tree_Menu").height())<$(window).height()){
						
							this.electronicFence_tree_Menu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});
						
						}else{
						
							this.electronicFence_tree_Menu.css({"top":(y-$("#electronicFence_tree_Menu").height())+"px", "left":x+"px", "visibility":"visible"});
						}
						
						$("body").bind("mousedown", electronicFence.tree.menu.onBodyMouseDown);
					},
		 hideRMenu : function() {
			if (this.electronicFence_tree_Menu) this.electronicFence_tree_Menu.css({"visibility": "hidden"});
			$("body").unbind("mousedown", electronicFence.tree.menu.onBodyMouseDown);
		 },
		 onBodyMouseDown : function(event){
			if (!(event.target.id == "electronicFence_tree_Menu" || $(event.target).parents("#electronicFence_tree_Menu").length>0)) {
				electronicFence.tree.menu.electronicFence_tree_Menu.css({"visibility" : "hidden"});
			}
			
		},
		add : function(){
			electronicFence.tree.menu.hideRMenu();
			electronicFence.win.openAddWin();
			electronicFence.draw.clearFence();
		},
		del : function(){
			
			$("#close_electronicFence_Win").click();
		
			$("#close_electronicFence_vehicle_binding_Win").click();
		
			electronicFence.tree.menu.hideRMenu();
			
			var id = electronicFence.tree.curNodeId,type = $("#electronicFence_type").val();
			
			if(confirm("Do you want to delete?")){
				
					$.ajax({
			        type: "POST",
			        dataType: "json",
			        url: "json/fenceAction_delFenceName.action",
			        data: {"fenceType":type,"fenceId":id},
			        success: function(json) {	
			        	         
				         
				         	for(var i=0;i<electronicFence.tree.fenceJson.totalcount;i++){
				
								if(electronicFence.tree.fenceJson.data[i].id == id){
								
									electronicFence.tree.fenceJson.data.remove(i);
									
									electronicFence.tree.fenceJson.totalcount--;
									
									electronicFence.tree.deleteNode(id);
									
									
									electronicFence.draw.clearFence();
									
									$("#fence-msg").hide();
									
									break;
									
								}
							
							}

		                 
			        },
			        error: function() {}
		   	     });	
			}
						
		},
		update : function(){
		
			electronicFence.tree.menu.hideRMenu();
			
			electronicFence.win.openUpdateWin();
			
		},
		bingding : function(){
			
			electronicFence.tree.menu.hideRMenu();
			
			electronicFence.win.openbingdingWin();
			
			
		},
		editEvent : function(){
			electronicFence.tree.menu.hideRMenu();
			electronicFence.win.openeventWin();
			
		},
		refresh : function(){
			
			electronicFence.tree.menu.hideRMenu();
			
			$("#close_electronicFence_Win").click();
		
			$("#close_electronicFence_vehicle_binding_Win").click();
			
			var curfenceId =electronicFence.tree.curNodeId;
			
			
			electronicFence.draw.clearFence();
			
			electronicFence.tree.addAllelectronicFenceNode();
			
			var json = electronicFence.tree.fenceJson;
			
			var ids=new Array();
			
			for(var i=0;i<json.data.length;i++){
			
				ids.push(json.data[i].id);
				
			}
			
			
			var treeObj = electronicFence.tree.treeObject;
			
			
			var polynodes = treeObj.getNodeByParam("id","fence_g_1").children;
			
			var linenodes = treeObj.getNodeByParam("id","fence_g_2");
			
			if(linenodes){
				linenodes = linenodes.children;
			}else{
				linenodes = new Array();
			}
			
			var nodes = polynodes.concat(linenodes)
			
			var deleteedId = new Array();
			
			for(var i=0 ; i < nodes.length ; i++){
			
				if(nodes[i].id == "fence_g_1" || nodes[i].id == "fence_g_2" || nodes[i].id == "fence_g_0" ){
				
				}else{
				
					if( ids.indexOf(nodes[i].id) < 0 ){
					
						electronicFence.tree.deleteNode(nodes[i].id);
						
						deleteedId.push(nodes[i].id);
						
					}
					
				}
			
			}
			
			
			if(curfenceId != -1 && (deleteedId.indexOf(curfenceId) < 0)){
			
				
				if(!$("#fence-msg").is(":hidden")){
     				
     				electronicFence.draw.showFence(curfenceId);
     					
     			}
			}else{
				
				$("#fence-msg").hide();	
							
			}
			
		}
}


electronicFence.win = {
	init : function(){
		  //添加电子围栏窗口开始
		   frame("electronicFence_ContentWin","move_electronicFence_Win","close_electronicFence_Win");
		  //添加电子围栏窗口结束
		  
		   //添加电子围栏窗口开始
		   frame("electronicFence_vehicle_binding_ContentWin","move_electronicFence_vehicle_binding_Win","close_electronicFence_vehicle_binding_Win");
		  //添加电子围栏窗口结束
		  
		  
		   //添加电子围栏窗口开始
		   frame("electronicFence_event_ContentWin","move_electronicFence_event_Win","close_electronicFence_event_Win");
		  //添加电子围栏窗口结束
	},
	openbingdingWin : function(){
		
		electronicFence.win.closeeventWin();
		
		electronicFence.win.closeWin();
		
		$("#saveFenceBtn").removeAttr("disabled");//去掉button的disabled属性，使button可用
		$("#cancleFenceBtn").removeAttr("disabled");//去掉button的disabled属性，使button可用
		
		
		$("#electronicFence_vehicle_binding_ContentWin").center();
		
		$("#electronicFence_vehicle_binding_ContentWin").show();
		
		$("#fencesaveMsg").text("");
		
		showFenceSelect(electronicFence.tree.curNodeId);
		
		showVechileGrouptoFenceSelect();
		
		showVechiletoFenceTable();
				
		
		if($("#electronicFence_type").val() == 2){
		
			$("#electronicFence_type_line").attr("checked","checked");
			
		}else{
		
			$("#electronicFence_type_polyArea").attr("checked","checked");
		}
						
	},
	openAddWin : function(){
		
		$("#saveFenceBtn_").removeAttr("disabled");//去掉button的disabled属性，使button可用
	
		electronicFence.win.closeeventWin();
		$("#close_electronicFence_vehicle_binding_Win").click();
		
		$("#fenceAddNameMsg").css("color","rgb(153, 153, 153)");
		$("#fenceAddNameMsg").text(language.zmbnwk);
		
		$("#saveFenceBtn_").removeAttr("disabled");//去掉button的disabled属性，使button可用
		
		$("#saveFenceBtn_").show();
		$("#updateFenceBtn_").hide();
		
		$("#electronicFence_name").val("");
		
		$("#move_electronicFence_Win").find("span").text(electronicFenceLanguage.dzwlsx);
		$("#electronicFence_ContentWin").center();
		$("#electronicFence_ContentWin").show();
	},
	closeWin : function(){
		$("#close_electronicFence_Win").click();
		$("#saveFenceBtn_").show();
		$("#updateFenceBtn_").hide();
		
		$("#electronicFence_name").val("");
	},
	add : function(){
		
		$("#saveFenceBtn_").attr("disabled","disabled");//设置所有的button不可用 
		
		
		var name = $("#electronicFence_name").val(),
			type = $("#electronicFence_type").val();
			
		
			//判断是否为空
		if(name.length == 0){
		
			$("#fenceAddNameMsg").css("color","red");
			$("#fenceAddNameMsg").text(language.zmbnwk);
			$("#saveFenceBtn_").removeAttr("disabled");//去掉button的disabled属性，使button可用
			return;
			
		}else{
			
			$("#fenceAddNameMsg").css("color","rgb(153, 153, 153)");
			$("#fenceAddNameMsg").text(language.zmbnwk);
		
		}
			//请求ajax
				//树形添加节点 or 响应
			$.ajax({
		        type: "POST",
		        dataType: "json",
		        url: "json/fenceAction_addOrUpdateFenceName.action",
		        data: {"fenceName":name,"fenceType":type},
		        success: function(json) {	
		        	         
			         if(json.flag==true) {
			         
			         	electronicFence.tree.fenceJson.data.push(json.data[0]);	
			         	
			         	electronicFence.tree.curNodeId = json.data[0].id;
			         	
			         	electronicFence.tree.fenceJson.totalcount +=1;		         		
						
	     				electronicFence.tree.addNode(json.data[0].fenceType,json.data[0].name,json.data[0].id);//2对划线,1多边形
						
						$("#electronicFence_type").val("");
						
						if(json.data[0].fenceType ==1){
						
							electronicFence.draw.drawingManager.setMap(map);
							
							$("#fence-msg").show();
							$("#fence_lien").hide();
							$("#fence_poly").show();
							

						}else{
						
							electronicFence.draw.openDrawLine();
							
							$("#fence-msg").show();
							$("#fence_poly").hide();				
							$("#fence_lien").show();	
							
						}
												
						//关闭
						electronicFence.win.closeWin();
						
	             	}else{
	             	
	             		if(json.msg == 10001){
	             		
	             			$("#fenceAddNameMsg").css("color","red");
	             			
	             			$("#fenceAddNameMsg").text(electronicFenceLanguage.ycz);
	             			
	             			$("#saveFenceBtn_").removeAttr("disabled");//去掉button的disabled属性，使button可用
	             			
	             		}
	             		
	             	}
	                 
		        },
		        error: function() {}
	   	     });	

			
	},
	openUpdateWin : function(){
	
		$("#updateFenceBtn_").removeAttr("disabled");//去掉button的disabled属性，使button可用
	
		electronicFence.win.closeeventWin();
		
		$("#fenceAddNameMsg").css("color","rgb(153, 153, 153)");
		
		$("#fenceAddNameMsg").text(language.zmbnwk);
		
		$("#updateFenceBtn_").removeAttr("disabled");//去掉button的disabled属性，使button可用
		
		$("#updateFenceBtn_").show();
		$("#saveFenceBtn_").hide();
		
		$("#electronicFence_ContentWin").center();
		
		$("#move_electronicFence_Win").find("span").text(electronicFenceLanguage.xgdzwlsx);
		
		$("#electronicFence_ContentWin").show();
		
		var id = electronicFence.tree.curNodeId;
		
		for(var i=0;i<electronicFence.tree.fenceJson.totalcount;i++){
				
			if(electronicFence.tree.fenceJson.data[i].id == id){
			
				$("#electronicFence_name").val(electronicFence.tree.fenceJson.data[i].name);
				
				break;
				
			}
		
		}
		
		
	},
	update : function(){
	
		$("#updateFenceBtn_").attr("disabled","disabled");//设置所有的button不可用
		
		$("#fenceAddNameMsg").text("");
		var name = $("#electronicFence_name").val(),
			type = $("#electronicFence_type").val();
			id = electronicFence.tree.curNodeId;
			//判断是否为空
			
			if(name.length == 0){
		
			$("#fenceAddNameMsg").css("color","red");
			$("#fenceAddNameMsg").text(language.zmbnwk);
			$("#updateFenceBtn_").removeAttr("disabled");//去掉button的disabled属性，使button可用
			
			return;
			
		}else{
			
			$("#fenceAddNameMsg").css("color","rgb(153, 153, 153)");
			$("#fenceAddNameMsg").text(language.zmbnwk);
		
		}
			
			
			//请求ajax
				//树形添加节点 or 响应
			$.ajax({
		        type: "POST",
		        dataType: "json",
		        url: "json/fenceAction_addOrUpdateFenceName.action",
		        data: {"fenceName":name,"fenceType":type,"fenceId":id},
		        success: function(json) {	
		        	         
			         if(json.flag==true) {

			         	for(var i=0;i<electronicFence.tree.fenceJson.totalcount;i++){
				
							if(electronicFence.tree.fenceJson.data[i].id == id){
							
								electronicFence.tree.fenceJson.data[i].name = json.data[0].name;
								
								break;
								
							}
						
						}
			         	
			         	
			         	electronicFence.tree.curNodeId = json.data[0].id;
			         		         		
						
	     				electronicFence.tree.updateNode(json.data[0].name,json.data[0].id);//2对划线,1多边形
						
						$("#electronicFence_type").val("");
						
									//关闭
						electronicFence.win.closeWin();
						
	             	}else{
	             			$("#fenceAddNameMsg").css("color","red");
	             			
	             			$("#fenceAddNameMsg").text(electronicFenceLanguage.ycz);
	             			
	             			$("#updateFenceBtn_").removeAttr("disabled");//去掉button的disabled属性，使button可用
	             	}
	                 
		        },
		        error: function() {}
	   	     });	
			
	},
	openeventWin : function(){
		electronicFence.win.closeWin();
		$("#close_electronicFence_vehicle_binding_Win").click();
		
		$("#electronicFence_event_ContentWin").center();
		$("#electronicFence_event_ContentWin").show();
	},
	closeeventWin : function(){
		$("#electronicFence_event_ContentWin").hide();
	}
}


electronicFence.draw = {

	directionsDisplay : null,
	
	directionsService : null,
	
	drawingManager : null,
	
	curPolygon : null,	
	
	Linelistener : null,
	
	startFlagLine : false,
	
	init : function (){
		//多边形
		electronicFence.draw.drawingManager = new google.maps.drawing.DrawingManager({
			 drawingControl: false, 
			 drawingMode:  google.maps.drawing.OverlayType.POLYGON,	 
			 //设置图形显示样式
			 polygonOptions: {
				strokeColor: "#00EE00",
			    strokeOpacity: 0.8,
			    strokeWeight: 3,
			    fillColor: "#0000ee",
			    fillOpacity: 0.35,
				editable: true
		     },markerOptions : {
		     	draggable : true,
		     	visible:false
		     }
		     
	      });	   
		   
	    google.maps.event.addListener(electronicFence.draw.drawingManager, 'polygoncomplete', function(polygon) {
		 
		   polygon.setVisible(true);
		   
		   electronicFence.draw.curPolygon = polygon;
		   
		   electronicFence.draw.drawingManager.setMap(null);
		   
		   var id = electronicFence.tree.curNodeId;		   
		   
		   		var latlngarr = polygon.getPath().getArray();
		   		
		   		var parmsArr = new Array();
		   		
		   		for(var i=0 ;i <latlngarr.length;i++ ){		   			
		   			
		   			parmsArr.push({"lat":latlngarr[i].lat(),"lng":latlngarr[i].lng(),"pointOrder":i});
		   				   					   			
		   		}
		   		
		   		var fenceJson = {};
				
				fenceJson.latlngs = parmsArr;
				
				fenceJson.fenceType = 1;
				
				fenceJson.fenceId = id;
				
	   			$.ajax({
			        type: "POST",
			        dataType: "json",
			        url: "json/fenceAction_addFencePolygonOrFenceRoute.action",
			        data: {"fenceJson":JSON.stringify(fenceJson)},
			        success: function(json) {	
						
								if(json.flag == true){
											for(var i=0 ; i < electronicFence.tree.fenceJson.data.length ; i++){
												
												if(electronicFence.tree.fenceJson.data[i].id == electronicFence.tree.curNodeId){
													
													for(var j =0 ; j < latlngarr.length ; j++){
																						
														electronicFence.tree.fenceJson.data[i].fencePolygon.push({lat : latlngarr[j].lat(),lng : latlngarr[j].lng(),fenceId : id ,pointOrder : j});
													}
													
													break;
												}
											}
										}
						
		                 
			        },
			        error: function() {}
		   	     });

		   
		 google.maps.event.addListener(polygon, 'mouseup', function(PolyMouseEvent) {
			     isSaveFlag = false;
			    //alert(2);
				/*
				var img = $("img[src='http://maps.gstatic.cn/mapfiles/undo_poly.png']");
				var img2 = $("img[src='http://maps.gstatic.com/mapfiles/undo_poly.png']");
				
				if(img){
				
					img.parent().parent().parent().remove();
					
				}
				*/
				if(img2){
				
					img2.parent().parent().parent().remove();
					
				}
				
				//alert(PolyMouseEvent.vertex+","+PolyMouseEvent.path+","+PolyMouseEvent.edge);
				
				if(PolyMouseEvent.path != undefined){//The index of the vertex beneath the cursor when the event occurred, if the event occurred on a vertex and the polyline or polygon is editable. If the event does not occur on a vertex, the value is undefined.					
					
					
					electronicFence.draw.curTime = time2stamp();
					
					setTimeout(function(){
		   					
		   					if(time2stamp() - electronicFence.draw.curTime >= 2000){
		   						if( !isSaveFlag){
							   		var latlngarr = polygon.getPath().getArray();
							   		
							   		var parmsArr = new Array();
							   		
							   		for(var i=0 ;i <latlngarr.length;i++ ){		   			
							   			
							   			parmsArr.push({"lat":latlngarr[i].lat(),"lng":latlngarr[i].lng(),"pointOrder":i});
							   				   					   			
							   		}
							   		
							   		var fenceJson = {};
									
									fenceJson.latlngs = parmsArr;
									
									fenceJson.fenceType = 1;
									
									fenceJson.fenceId = id;
									
						   			$.ajax({
								        type: "POST",
								        dataType: "json",
								        url: "json/fenceAction_upDateFencePolygonOrFenceRoute.action",
								        data: {"fenceJson":JSON.stringify(fenceJson)},
								        success: function(json) {	
											
											if(json.flag == true){
												for(var i=0 ; i < electronicFence.tree.fenceJson.data.length ; i++){
													
													if(electronicFence.tree.fenceJson.data[i].id == electronicFence.tree.curNodeId){
														electronicFence.tree.fenceJson.data[i].fencePolygon = new Array();
														for(var j =0 ; j < latlngarr.length ; j++){
																							
															electronicFence.tree.fenceJson.data[i].fencePolygon.push({lat : latlngarr[j].lat(),lng : latlngarr[j].lng(),fenceId : id ,pointOrder : j});
														}
														
														break;
													}
												}
											}
							                 
								        },
								        error: function() {}
							   	     });
							   	      isSaveFlag = true;
						   	     }
					   		}
					   },2000);
				}
				
  		  });
		   
		});	
		
		google.maps.event.addListener(map, 'click', function(e) {		
			
			if(electronicFence.draw.iscrtlDown && electronicFence.draw.startFlagLine){
					
					electronicFence.draw.startDrawLine(e.latLng);
			
			}
		
		});
		
		

    	document.onkeydown=function(event){
    	
            var e = event || window.event || arguments.callee.caller.arguments[0];                      
            
            if(e && e.keyCode==17){
               
                if(!electronicFence.draw.iscrtlDown){
                	 
                	 electronicFence.draw.iscrtlDown = true;
                	
                }
          
             }
             
             if(e && e.keyCode==27){
             
             	beforeCloseSave();
             	
             	electronicFence.draw.clearFence();
             	
             	$("#fence-msg").hide();
             	
             	
             }
   
        };  
            
        document.onkeyup = function(event){  

            var e = event || window.event || arguments.callee.caller.arguments[0];                          
            
            if(e && e.keyCode==17){ // 按 Esc 
                
                 electronicFence.draw.iscrtlDown = false;
             } 

        } ; 


		
		
	},
	iscrtlDown : false,
	
	curTime : false,
	drawPolygon : function(latlngArr,id){	
	
		if(latlngArr.length <3){
			return;
		}			
		
		var triangleCoords = new Array();
		
		var editable = false;
		
		for(var i=0 ; i<latlngArr.length ; i++){
			
			for(var j=0 ; j<latlngArr.length ; j++ ){
				
				if(latlngArr[j].pointOrder == i){
				
					if(i>0){
					
						if( latlngArr[j].lat != triangleCoords[i-1].lat() || latlngArr[j].lng != triangleCoords[i-1].lng()){
							triangleCoords.push(new google.maps.LatLng(latlngArr[j].lat, latlngArr[j].lng));
						}
						
					}else{
						triangleCoords.push(new google.maps.LatLng(latlngArr[j].lat, latlngArr[j].lng));
					}
					
					break;
				}
			}
			//triangleCoords.push(new google.maps.LatLng(latlngArr[i].lat, latlngArr[i].lng));
 			//triangleCoords.splice(latlngArr[i].pointOrder,0,new google.maps.LatLng(latlngArr[i].lat, latlngArr[i].lng));
		}
		
		for(var j=0;j<electronicFence.tree.fenceJson.data.length;j++){
				
			if(electronicFence.tree.fenceJson.data[j].id == id){
				if(electronicFence.tree.fenceJson.data[j].competence==1){
					editable = true;
				}else{
					editable = false;
				}
				
				break;								
			}
							
		}
		
		electronicFence.draw.curPolygon = new google.maps.Polygon({
			    paths: triangleCoords,
			    strokeColor: "#00EE00",
			    strokeOpacity: 0.8,
			    strokeWeight: 3,
			    fillColor: "#0000ee",
			    fillOpacity: 0.35,
				editable: editable
		 	});
	  
		  
		 electronicFence.draw.curPolygon.setMap(map);

	  	  
		  google.maps.event.addListener(electronicFence.draw.curPolygon, 'mouseup', function(PolyMouseEvent) {
			    isSaveFlag = false;
			    //alert(2);
				/*
				var img = $("img[src='http://maps.gstatic.cn/mapfiles/undo_poly.png']");
				var img2 = $("img[src='http://maps.gstatic.com/mapfiles/undo_poly.png']");
				
				if(img){
				
					img.parent().parent().parent().remove();
					
				}
				
				if(img2){
				
					img2.parent().parent().parent().remove();
					
				}
				*/
				//alert(PolyMouseEvent.vertex+","+PolyMouseEvent.path+","+PolyMouseEvent.edge);
				
				if(PolyMouseEvent.path != undefined){//The index of the vertex beneath the cursor when the event occurred, if the event occurred on a vertex and the polyline or polygon is editable. If the event does not occur on a vertex, the value is undefined.					
					
					
					electronicFence.draw.curTime = time2stamp();
					
					setTimeout(function(){
		   					
		   					if(time2stamp() - electronicFence.draw.curTime >= 2000){
		   						if(!isSaveFlag){
							   		var latlngarr = electronicFence.draw.curPolygon.getPath().getArray();
							   		
							   		var parmsArr = new Array();
							   		
							   		for(var i=0 ;i <latlngarr.length;i++ ){		   			
							   			
							   			parmsArr.push({"lat":latlngarr[i].lat(),"lng":latlngarr[i].lng(),"pointOrder":i});
							   				   					   			
							   		}
							   		
							   		var fenceJson = {};
									
									fenceJson.latlngs = parmsArr;
									
									fenceJson.fenceType = 1;
									
									fenceJson.fenceId = id;
									
						   			$.ajax({
								        type: "POST",
								        dataType: "json",
								        url: "json/fenceAction_upDateFencePolygonOrFenceRoute.action",
								        data: {"fenceJson":JSON.stringify(fenceJson)},
								        success: function(json) {	
											if(json.flag == true){
												for(var i=0 ; i < electronicFence.tree.fenceJson.data.length ; i++){
													
													if(electronicFence.tree.fenceJson.data[i].id == electronicFence.tree.curNodeId){
														
														electronicFence.tree.fenceJson.data[i].fencePolygon = new Array();
														
														for(var j =0 ; j < latlngarr.length ; j++){
																							
															electronicFence.tree.fenceJson.data[i].fencePolygon.push({lat : latlngarr[j].lat(),lng : latlngarr[j].lng(),fenceId : id ,pointOrder : j});
														}
														
														break;
													}
												}
											}
							                 
								        },
								        error: function() {}
							   	     });
						   	     }
						   	     isSaveFlag = true;
					   		}
					   },2000);
				}
				
  		  });
	
	},
	drawline : function(latlngArr){	
		  	  
		 electronicFence.draw.clearFence();
		 
		 for(var i=0 ; i<latlngArr.length ; i++){
			
			for(var j=0 ; j<latlngArr.length ; j++ ){
				
				if(latlngArr[j].pointOrder == i){
				
					electronicFence.draw.lineLatLng.push(new google.maps.LatLng(latlngArr[j].lat, latlngArr[j].lng));
					
					break;
				}
			}
			
		}
		 
		 
		 electronicFence.draw.setDirections();
		 
	},
	
	linemarkerArr : new Array (),
	
	lineLatLng : new Array(),
	
	openDrawLine : function (){
		
		electronicFence.draw.clearFence();
		
		electronicFence.draw.startFlagLine = true;
		
	},
	
	startDrawLine : function(latLng){
		
		if(electronicFence.draw.lineLatLng.length<=20){
		
			electronicFence.draw.lineLatLng.push(latLng);
			
		}else{
			return;
		}
		
		if(electronicFence.draw.lineLatLng.length == 1){
		   		
		   		var gicon = {url: electronicFenceUrl.linefirstMarker,
   					size: new google.maps.Size(24, 38),
   					origin: new google.maps.Point(0,0),
   					anchor: new google.maps.Point(12, 38)
  				};
  				
  				var marker = new google.maps.Marker({position: latLng, map: map, icon: gicon, draggable: true});				   		
		   		
		   		google.maps.event.addListener(marker, "dragend", function(){
						
						try{
						
						   var marker = electronicFence.draw.linemarkerArr[idx];
						   
						   electronicFence.draw.lineLatLng[idx] = marker.getPosition();
						   
						   electronicFence.draw.setDirections();
						   
						}catch(ex){}
						
				});
		   		
		   		electronicFence.draw.linemarkerArr.push(marker);
		   		
		   		
		 }
			
		electronicFence.draw.setDirections();
		
	},
	showFence : function(id){
	
		var json = electronicFence.tree.fenceJson;
		
		if(json == null){
		
			return;
			
		}
		
		for(var i=0;i<json.totalcount;i++){
		
			if(id == json.data[i].id){
				
				electronicFence.draw.clearFence();
     			
     			if(json.data[i].fenceType == 1){
     			
     				
     				electronicFence.draw.drawPolygon(json.data[i].fencePolygon,id);    				
     				
     				if(json.data[i].fencePolygon.length > 0){
     				
	     				map.setCenter(new google.maps.LatLng(json.data[i].fencePolygon[0].lat, json.data[i].fencePolygon[0].lng));
	     				
	     				map.setZoom(13);
     				
     				}else{
     					electronicFence.draw.drawingManager.setMap(map);
     				}   
     				  	
     				$("#fence_lien").hide();
				
					$("#fence_poly").show();	

     				
     			}else{
   				
     				electronicFence.draw.drawline(json.data[i].renceRoute);
     				
     				electronicFence.draw.startFlagLine = true;	
     				
     				$("#fence_poly").hide();
				
					$("#fence_lien").show();	
     				    				
     			}
     			
     			break;
     			
     		}
     		
    	}
     		
     		
	},
	clearFence : function(){
				
				electronicFence.draw.lineLatLng = new Array();
				
				if(electronicFence.draw.linemarkerArr[0]){
					electronicFence.draw.linemarkerArr[0].setMap(null);
				}
				electronicFence.draw.linemarkerArr = new Array();
				
				electronicFence.draw.startFlagLine = false;
			
				google.maps.event.removeListener(electronicFence.draw.Linelistener);
				
				if(electronicFence.draw.curPolygon){
				
					electronicFence.draw.curPolygon.setMap(null);
					
				}
				
				if(electronicFence.draw.directionsDisplay){
     				
     					electronicFence.draw.directionsDisplay.setMap(null);
     			}
     			
     			electronicFence.draw.drawingManager.setMap(null);
     			
     			electronicFence.draw.drawingManager = new google.maps.drawing.DrawingManager({
			 drawingControl: false, 
			 drawingMode:  google.maps.drawing.OverlayType.POLYGON,	 
			 //设置图形显示样式
			 polygonOptions: {
				strokeColor: "#00EE00",
			    strokeOpacity: 0.8,
			    strokeWeight: 3,
			    fillColor: "#0000ee",
			    fillOpacity: 0.35,
				editable: true
		     },markerOptions : {
		     	draggable : true,
		     	visible:false
		     }
		     
	      });	   
		   
	    google.maps.event.addListener(electronicFence.draw.drawingManager, 'polygoncomplete', function(polygon) {
		 
		   polygon.setVisible(true);
		   
		   electronicFence.draw.curPolygon = polygon;
		   
		   electronicFence.draw.drawingManager.setMap(null);
		   
		   var id = electronicFence.tree.curNodeId;		   
		   
		   		var latlngarr = polygon.getPath().getArray();
		   		
		   		var parmsArr = new Array();
		   		
		   		for(var i=0 ;i <latlngarr.length;i++ ){		   			
		   			
		   			parmsArr.push({"lat":latlngarr[i].lat(),"lng":latlngarr[i].lng(),"pointOrder":i});
		   				   					   			
		   		}
		   		
		   		var fenceJson = {};
				
				fenceJson.latlngs = parmsArr;
				
				fenceJson.fenceType = 1;
				
				fenceJson.fenceId = id;
				
	   			$.ajax({
			        type: "POST",
			        dataType: "json",
			        url: "json/fenceAction_addFencePolygonOrFenceRoute.action",
			        data: {"fenceJson":JSON.stringify(fenceJson)},
			        success: function(json) {	
						
								if(json.flag == true){
											for(var i=0 ; i < electronicFence.tree.fenceJson.data.length ; i++){
												
												if(electronicFence.tree.fenceJson.data[i].id == electronicFence.tree.curNodeId){
													
													for(var j =0 ; j < latlngarr.length ; j++){
																						
														electronicFence.tree.fenceJson.data[i].fencePolygon.push({lat : latlngarr[j].lat(),lng : latlngarr[j].lng(),fenceId : id ,pointOrder : j});
													}
													
													break;
												}
											}
										}
						
		                 
			        },
			        error: function() {}
		   	     });

		   
		 google.maps.event.addListener(polygon, 'mouseup', function(PolyMouseEvent) {
			     isSaveFlag = false;
			    //alert(2);
				/*
				var img = $("img[src='http://maps.gstatic.cn/mapfiles/undo_poly.png']");
				var img2 = $("img[src='http://maps.gstatic.com/mapfiles/undo_poly.png']");
				
				if(img){
				
					img.parent().parent().parent().remove();
					
				}
				
				if(img2){
				
					img2.parent().parent().parent().remove();
					
				}
				*/
				//alert(PolyMouseEvent.vertex+","+PolyMouseEvent.path+","+PolyMouseEvent.edge);
				
				if(PolyMouseEvent.path != undefined){//The index of the vertex beneath the cursor when the event occurred, if the event occurred on a vertex and the polyline or polygon is editable. If the event does not occur on a vertex, the value is undefined.					
					
					
					electronicFence.draw.curTime = time2stamp();
					
					setTimeout(function(){
		   					
		   					if(time2stamp() - electronicFence.draw.curTime >= 2000){
		   						if( !isSaveFlag){
							   		var latlngarr = polygon.getPath().getArray();
							   		
							   		var parmsArr = new Array();
							   		
							   		for(var i=0 ;i <latlngarr.length;i++ ){		   			
							   			
							   			parmsArr.push({"lat":latlngarr[i].lat(),"lng":latlngarr[i].lng(),"pointOrder":i});
							   				   					   			
							   		}
							   		
							   		var fenceJson = {};
									
									fenceJson.latlngs = parmsArr;
									
									fenceJson.fenceType = 1;
									
									fenceJson.fenceId = id;
									
						   			$.ajax({
								        type: "POST",
								        dataType: "json",
								        url: "json/fenceAction_upDateFencePolygonOrFenceRoute.action",
								        data: {"fenceJson":JSON.stringify(fenceJson)},
								        success: function(json) {	
											
											if(json.flag == true){
												for(var i=0 ; i < electronicFence.tree.fenceJson.data.length ; i++){
													
													if(electronicFence.tree.fenceJson.data[i].id == electronicFence.tree.curNodeId){
														electronicFence.tree.fenceJson.data[i].fencePolygon = new Array();
														for(var j =0 ; j < latlngarr.length ; j++){
																							
															electronicFence.tree.fenceJson.data[i].fencePolygon.push({lat : latlngarr[j].lat(),lng : latlngarr[j].lng(),fenceId : id ,pointOrder : j});
														}
														
														break;
													}
												}
											}
							                 
								        },
								        error: function() {}
							   	     });
							   	      isSaveFlag = true;
						   	     }
					   		}
					   },2000);
				}
				
  		  });
		   
		});	
     			
	},
	setDirections : function (){
		 
		if(electronicFence.draw.lineLatLng.length < 2) return;
		
		if(electronicFence.draw.linemarkerArr[0] && electronicFence.draw.linemarkerArr[0].getMap()){
			
			electronicFence.draw.linemarkerArr[0].setMap(null);
			
		}
		if(electronicFence.draw.directionsService == null)
			electronicFence.draw.directionsService = new google.maps.DirectionsService();
		if(electronicFence.draw.directionsDisplay == null)
		{
			electronicFence.draw.directionsDisplay = new google.maps.DirectionsRenderer({draggable:true});
  			electronicFence.draw.directionsDisplay.setMap(map);
  			//electronicFence.draw.directionsDisplay.setPanel(document.getElementById('dirDivExlive'));
			google.maps.event.addListener(electronicFence.draw.directionsDisplay, "directions_changed",electronicFence.draw.lienChange);
		}
		
		var wayPoints = new Array();
			
			for(var i=1;i<electronicFence.draw.lineLatLng.length-1;i++)
			{
				if(i>1){
		 	
			 		if(wayPoints[wayPoints.length-1].location.lat() != electronicFence.draw.lineLatLng[i].lat() || wayPoints[wayPoints.length-1].location.lng() != electronicFence.draw.lineLatLng[i].lng()){
			 					
			 					if(wayPoints.length >5 || ((i+1) == electronicFence.draw.lineLatLng.length && ((i+2)< 7))) break;
				
								wayPoints.push({location: electronicFence.draw.lineLatLng[i],stopover:true}); 			
			 			
			 		}
		 		}else{
		 			wayPoints.push({location: electronicFence.draw.lineLatLng[i],stopover:true}); 		
		 		}

			}

		
		if(electronicFence.draw.lineLatLng.length>1)
		{ 
			var request = {
				origin: electronicFence.draw.lineLatLng[0],
				destination: electronicFence.draw.lineLatLng[electronicFence.draw.lineLatLng.length-1],
				waypoints: wayPoints,
				travelMode: google.maps.TravelMode.DRIVING
			};
			
			electronicFence.draw.directionsService.route(request, function(response, status) {
				
				if (status == google.maps.DirectionsStatus.OK) {					
					electronicFence.draw.directionsDisplay.setMap(null);
					electronicFence.draw.directionsDisplay.setDirections(response);
					electronicFence.draw.directionsDisplay.setMap(map);
				}			
					
			});
		}
	},
	
	lienChange : function(){
			 
			 electronicFence.draw.curTime = time2stamp();
			 
        	 isSaveLineFlag = false;
        	 
        	 setTimeout(function(){
        	 			  if(!isSaveLineFlag){
		        	 			 if(time2stamp() - electronicFence.draw.curTime >= 2000){				        	 				        	
						        	 
						        	 var fenceJson = {};
						        	 
						        	 var parmsArr = new Array();
						        	 
						        	 for(var i=0;i<electronicFence.draw.lineLatLng.length;i++){
						        	 
						        	 	parmsArr.push({"lat":electronicFence.draw.lineLatLng[i].lat(),"lng":electronicFence.draw.lineLatLng[i].lng(),"pointOrder":i});
						        	
						        	 }
				   		
				   		
						
									 fenceJson.latlngs = parmsArr;
									
									 fenceJson.fenceType = 2;
									 
									 fenceJson.fenceId = electronicFence.tree.curNodeId;
									
						   			 $.ajax({
								        type: "POST",
								        dataType: "json",
								        url: "json/fenceAction_upDateFencePolygonOrFenceRoute.action",
								        data: {"fenceJson":JSON.stringify(fenceJson)},
								        success: function(json) {	
											
											if(json.flag == true){
											
												for(var i=0 ; i < electronicFence.tree.fenceJson.data.length ; i++){
													
													if(electronicFence.tree.fenceJson.data[i].id == electronicFence.tree.curNodeId){
														
														for(var j =0 ; j < electronicFence.draw.lineLatLng.length ; j++){
																							
															electronicFence.tree.fenceJson.data[i].renceRoute.push({lat : electronicFence.draw.lineLatLng[j].lat(),lng : electronicFence.draw.lineLatLng[j].lng(),fenceId : electronicFence.tree.curNodeId ,pointOrder : j});
														}
														
														break;
													}
												}
												
											}
											
											isSaveLineFlag = true;
							                 
								        },
								        error: function() {}
							   	     });
									        	 
									        	 
					        	 }
				        	 }
			        	 
			        	 
        	 },2000);

	}

}

function showVechiletoFenceTable(){
	 
	$("#fence_poly_table_title").hide();
	$("#fence_lien_table_title").hide();
	
	var fenceType = $("#electronicFence_type").val(); 
	
	if(fenceType == 2){
	 	
	 	$("#fence_lien_table_title").show();
	 	
	}else{
	
	 	$("#fence_poly_table_title").show();
	}
	 
	$("#electronicFence_vehicle_table_content tbody").html("");
	
	 
	 
	 var groupId= $("#fence_vehicle_group").val();
	 
	 if(groupId.length == 0){
	 	groupId = -1;
	 }else{
	 	groupId = groupId.split("_")[2];
	 }
	 
	 

	 if(cartIds.length == 0) return;
	 
	 
	 var curGroupChildsIds = equipmentMap[groupId] ? (equipmentMap[groupId].getChildsIds()) : (new Array());
	 var curGroupChildsIdsLength = curGroupChildsIds.length;
	 
	 var id,curVehicle;
	 
	 var rows="",options="";
	 
	 var fenceId = $("#fence_selecte_id").val();
	 
	 if(fenceId.length > 0 ){
	 
	 	fenceId=fenceId.split("_")[1];
	 	
	 }else{
	 
	 	fenceId=-1;
	 	
	 }
	 
	 
	 
	 var result = getBingdingVehicleFromFenceData(curGroupChildsIds.toString());
	 var isBingdinged = false;
	 
	 for(i=0;i<curGroupChildsIdsLength;i++) {
	 		
	 		id=curGroupChildsIds[i];	
			
			isBingdinged = false;
			
			for(var j=0;j<result.length;j++){
				
				if(result[j].fenceId == fenceId && result[j].vhcId == id){
				
					isBingdinged = true;
					
					break;
				}
			}
			
			
			
			curVehicle=equipmentObject[id];
			
			if(fenceType == 2){	
				
					if(isBingdinged){
					
						rows += "<tr name='fence_vechileId_"+curVehicle.id+"'>" +//onclick='clearCurSelect(this)'
												    "<td align='center' style='width:100px;' name='"+result[j].id+"'>"+$.trim(curVehicle.vehicleName)+"</td>"+
												    "<td align='center' style='width:100px;'><input type='text' class='electronicFence_vehicle_table_input'  value='"+(result[j].deviateDis == 500 ? "" : result[j].deviateDis)+"'></input></td>"+
												    "<td align='center' style='width:100px;'><input type='text' class='electronicFence_vehicle_table_input'  value='"+(result[j].stopAlarmMin == 30 ? "" : result[j].stopAlarmMin)+"'></input></td>"+
												    "<td align='center' style='width:100px;'><input type='text' class='electronicFence_vehicle_table_input'  value='"+(result[j].maxVeo == 0 ? "" : result[j].maxVeo)+"'></input></td>"+
												   "<td align='center' style='width:100px;'><input type='text' class='electronicFence_vehicle_table_input' name='time' onkeydown='return false' onkeyup='return false' oncontextmenu='return false' value='"+(result[j].fromtime2.length == 0 ? ""  : (result[j].fromtime2.split(" ")[1] == "00:00:00" ? "" : result[j].fromtime2.split(" ")[1]))+"' onclick=\"WdatePicker({autoShowQS:false,el:$dp.$('d12'),skin:'simple',dateFmt:'HH:mm:ss'})\"></input></td>"+
												    "<td align='center'><input type='text' class='electronicFence_vehicle_table_input' name='time' onkeydown='return false' onkeyup='return false' oncontextmenu='return false' value='"+(result[j].totime2.length == 0 ? ""  :(result[j].totime2.split(" ")[1] == "00:00:00" ? "" : result[j].totime2.split(" ")[1]))+"' onclick=\"WdatePicker({autoShowQS:false,el:$dp.$('d12'),skin:'simple',dateFmt:'HH:mm:ss'})\"></input></td>"+
												 "</tr>"
												 
					 }else{
					 
					 	rows += "<tr name='fence_vechileId_"+curVehicle.id+"'>" +//onclick='clearCurSelect(this)'
												    "<td align='center' style='width:100px;' name=''>"+$.trim(curVehicle.vehicleName)+"</td>"+
												    "<td align='center' style='width:100px;'><input type='text' class='electronicFence_vehicle_table_input'></input></td>"+
												    "<td align='center' style='width:100px;'><input type='text' class='electronicFence_vehicle_table_input' ></input></td>"+
												    "<td align='center' style='width:100px;'><input type='text' class='electronicFence_vehicle_table_input' ></input></td>"+
												    "<td align='center' style='width:100px;'><input type='text' class='electronicFence_vehicle_table_input' name='time' onkeydown='return false' onkeyup='return false' oncontextmenu='return false' onclick=\"WdatePicker({el:$dp.$('d12'),skin:'simple',dateFmt:'HH:mm:ss'})\"></input></td>"+
												    "<td align='center'><input type='text' class='electronicFence_vehicle_table_input' name='time' onkeydown='return false' onkeyup='return false' oncontextmenu='return false' onclick=\"WdatePicker({el:$dp.$('d12'),skin:'simple',dateFmt:'HH:mm:ss'})\"></input></td>"+
												 "</tr>"
												 
					 }
					 
			}else{
				   if(isBingdinged){

				   		options = "<option value='0' "+(result[j].regionAlarmType==0 ? "selected ='selected'" : "")+" >"+electronicFenceLanguage.bbj+"</option><option value='1' "+(result[j].regionAlarmType==1 ? "selected ='selected'" : "")+" >"+electronicFenceLanguage.jrcxbj+"</option><option value='2'  "+(result[j].regionAlarmType==2 ? "selected ='selected'" : "")+" >"+electronicFenceLanguage.sccxbj+"</option><option value='4'  "+(result[j].regionAlarmType==4 ? "selected ='selected'" : "")+" >"+electronicFenceLanguage.jrbjyc+"</option><option value='8' "+(result[j].regionAlarmType==8 ? "selected ='selected'" : "")+" >"+electronicFenceLanguage.scbjyc+"</option><option value='16'  "+(result[j].regionAlarmType==16 ? "selected ='selected'" : "")+" >"+electronicFenceLanguage.jcqydbj+"</option>";
					  				  	
					  	rows += "<tr name='fence_vechileId_"+curVehicle.id+"'>" +// 
												    "<td align='center' style='width:100px;' name='"+result[j].id+"'>"+$.trim(curVehicle.vehicleName)+"</td>"+
												    "<td align='center' style='width:100px;'><select style='width:100%;border : 0px;'>"+options+"</select></td>"+
												    "<td align='center' style='width:100px;'><input type='text' class='electronicFence_vehicle_table_input' value='"+(result[j].maxVeo == 0 ? "" : result[j].maxVeo)+"'></input></td>"+
												    "<td align='center' style='width:100px;'><input type='text' class='electronicFence_vehicle_table_input' name='time' onkeydown='return false' onkeyup='return false' oncontextmenu='return false' value='"+(result[j].fromtime2.length == 0 ? ""  : (result[j].fromtime2.split(" ")[1] == "00:00:00" ? "" : result[j].fromtime2.split(" ")[1]))+"' onclick=\"WdatePicker({autoShowQS:false,el:$dp.$('d12'),skin:'simple',dateFmt:'HH:mm:ss'})\"></input></td>"+
												    "<td align='center'><input type='text' class='electronicFence_vehicle_table_input' name='time' onkeydown='return false' onkeyup='return false' oncontextmenu='return false' value='"+(result[j].totime2.length == 0 ? ""  :(result[j].totime2.split(" ")[1] == "00:00:00" ? "" : result[j].totime2.split(" ")[1]))+"' onclick=\"WdatePicker({autoShowQS:false,el:$dp.$('d12'),skin:'simple',dateFmt:'HH:mm:ss'})\"></input></td>"+
												 "</tr>"
					}else{
						
						rows += "<tr name='fence_vechileId_"+curVehicle.id+"'>" +// onclick='clearCurSelect(this)'
												    "<td align='center' style='width:100px;' name=''>"+$.trim(curVehicle.vehicleName)+"</td>"+
												    "<td align='center' style='width:100px;' ><select style='width:100%;border : 0px;'><option value='0'>"+electronicFenceLanguage.bbj+"</option><option value='1'>"+electronicFenceLanguage.jrcxbj+"</option><option value='2'>"+electronicFenceLanguage.sccxbj+"</option><option value='4'>"+electronicFenceLanguage.jrbjyc+"</option><option value='8'>"+electronicFenceLanguage.scbjyc+"</option><option value='16'>"+electronicFenceLanguage.jcqydbj+"</option></select></td>"+
												    "<td align='center' style='width:100px;'><input type='text' class='electronicFence_vehicle_table_input'></input></td>"+
												    "<td align='center' style='width:100px;'><input type='text' class='electronicFence_vehicle_table_input' name='time' onkeydown='return false' onkeyup='return false' oncontextmenu='return false' onclick=\"WdatePicker({qsEnabled:false,autoShowQS:false,el:$dp.$('d12'),skin:'simple',dateFmt:'HH:mm:ss'})\"></input></td>"+
												    "<td align='center'><input type='text' class='electronicFence_vehicle_table_input' name='time' onkeydown='return false' onkeyup='return false' oncontextmenu='return false' onclick=\"WdatePicker({qsEnabled:false,autoShowQS:false,el:$dp.$('d12'),skin:'simple',dateFmt:'HH:mm:ss'})\"></input></td>"+
												 "</tr>"
					 
					}
			
			}
			
	 }
	 
	 $("#electronicFence_vehicle_table_content tbody").append(rows);
	 
	 rows="";
	 
}

function showVechileGrouptoFenceSelect(){
	
	 var options ="";
	 var i=0;
	 $.each(equipmentMap, function (k, v) {//迭代每一个组
	 		
	 		if(i<1){
	 			options += "<option value='fence_group_"+k+"' selected='selected' >"+v.getName()+"</option>"
	 			i++;
	 		}else{
	 			options += "<option value='fence_group_"+k+"'>"+v.getName()+"</option>"
	 		}	
			
	
	 });
	 
	 $("#fence_vehicle_group").append(options);
	 
	 options="";
	 
}

function showFenceSelect(id){
	
	var options ="";
	
	$("#fence_selecte_id").html("");
	
	var fenceType =$("#electronicFence_type").val();
	 
	for(var i=0 ; i< electronicFence.tree.fenceJson.data.length ; i++){
	
				if(electronicFence.tree.fenceJson.data[i].fenceType == fenceType ){
				
			 		if(electronicFence.tree.fenceJson.data[i].id == id){
			 			options += "<option id='fence_"+electronicFence.tree.fenceJson.data[i].id+"' value='fence_"+electronicFence.tree.fenceJson.data[i].id+"' selected='selected' >"+electronicFence.tree.fenceJson.data[i].name+"</option>"
			 		}else{
			 			options += "<option id='fence_"+electronicFence.tree.fenceJson.data[i].id+"' value='fence_"+electronicFence.tree.fenceJson.data[i].id+"'>"+electronicFence.tree.fenceJson.data[i].name+"</option>"
			 		}
			 		
		 		} 		
	 }
	 
	 $("#fence_selecte_id").append(options);
	 
	 options="";
	 
}

function showFenceSelectbyType(type){
	
	var options ="";
	
	$("#fence_selecte_id").html("");
	 
	for(var i=0 ; i< electronicFence.tree.fenceJson.data.length ; i++){
	 		
	 		if(electronicFence.tree.fenceJson.data[i].fenceType == type){
	 			options += "<option id='fence_"+electronicFence.tree.fenceJson.data[i].id+"' value='fence_"+electronicFence.tree.fenceJson.data[i].id+"'>"+electronicFence.tree.fenceJson.data[i].name+"</option>"
	 		}	 		
	 }
	 
	 $("#fence_selecte_id").append(options);
	 
	 options="";
	 
}

var curTr = null;
function clearCurSelect(element){

			if(curTr != null){
				curTr.css("background","");
				curTr.css("color","#676767");
			}
			
			curTr=$(element);
			curTr.css("background","#A0B0C7");
			
}

function changefenceTypeRadio(type){
		
		$("#electronicFence_type").val(type);
		
		showFenceSelectbyType(type);
		
		showVechiletoFenceTable();
	
}

function changefence(){
		
		showVechiletoFenceTable();
	
}

function savebingdingVehicletoFence(){

	  $("#saveFenceBtn").attr("disabled","disabled");//设置所有的button不可用 
	  
	  if($("#fence_selecte_id").html().length == 0){
	  	
	  	$("#fencesaveMsg").text(electronicFenceLanguage.qxzwl);
	  	
	  	$("#saveFenceBtn").removeAttr("disabled");//去掉button的disabled属性，使button可用
	  	
	  	return ;
	  	
	  }else{
	  	$("#fencesaveMsg").text("");
	  }
	  
	  if($("#electronicFence_vehicle_table_content tbody").html().length == 0){
	  	
	  	$("#fencesaveMsg").text(electronicFenceLanguage.qxzybddsb);
	  	
	  	$("#saveFenceBtn").removeAttr("disabled");//去掉button的disabled属性，使button可用
	  	
	  	return ;
	  }else{
	  	$("#fencesaveMsg").text("");
	  }
	  
	  var fenceType = $("input[type='radio']:checked").val();
	  
	  var fenceid = $("#fence_selecte_id").val().split("_")[1];
	  
	  var tds = null,vechileId="",warmtype=-1,maxVeo = 0,fromtime="",totime="",id="";
	  
	  var parmjson = new Array(),fenceJson = null;
	  
	   $("#electronicFence_vehicle_table_content tr").each(function(){
	   		
	   		vechileId=$(this).attr("name").split("_")[2];
	   		    
	   		tds =$(this).find("td");
	   		id = $(tds[0]).attr("name");	
	   		if(fenceType  ==1 ){
	   		
	   		   
	   			
	   			warmtype=$(tds[1]).find("select").val();
	   			maxVeo=$(tds[2]).find("input").val();
	   			fromtime=$(tds[3]).find("input").val();
	   			totime=$(tds[4]).find("input").val();
	   			
	   			if(id.length != 0 || warmtype != 0 || maxVeo.length !=0 || fromtime.length !=0 || totime.length !=0 ){	   				
	   				
	   				if(maxVeo.length ==0 ){
	   					maxVeo = 0;
	   				}
	   				
	   				
	   				if(fromtime.length == 0){
	   				
	   					fromtime="1899-12-30 00:00:00.000";
	   					
	   				}else{
	   					fromtime="1899-12-30 " + fromtime+".000";
	   				}
	   				
	   				if(totime.length == 0){
	   				
	   					totime="1899-12-30 00:00:00.000";
	   					
	   				}else{
	   					totime="1899-12-30 " + totime+".000";
	   				}
	   				
	   				if(id.length>0){
	   					parmjson.push({id:id,fenceId : fenceid,vhcId : vechileId,fromtime : fromtime,totime : totime,regionAlarmType : warmtype,deviateDis : 500,stopAlarmMin : 30,minVeo : 0,maxVeo :maxVeo});
	   				}else{
	   					parmjson.push({fenceId : fenceid,vhcId : vechileId,fromtime : fromtime,totime : totime,regionAlarmType : warmtype,deviateDis : 500,stopAlarmMin : 30,minVeo : 0,maxVeo :maxVeo});
	   				}
	   			}
	   		
	   		}else if(fenceType == 2){
	   		
	   			deviateDis=$(tds[1]).find("input").val();
	   			stopAlarmMin=$(tds[2]).find("input").val();
	   			maxVeo=$(tds[3]).find("input").val();
	   			fromtime=$(tds[4]).find("input").val();
	   			totime=$(tds[5]).find("input").val();
	   			
	   			if(id.length != 0 || deviateDis.length != 0 || maxVeo.length !=0 || stopAlarmMin.length !=0 || totime.length !=0  || fromtime.length !=0){
	   				
	   				if(deviateDis.length == 0){
	   				
	   					deviateDis = 500;
	   					
	   				}
	   				
	   				if(maxVeo.length == 0){
	   				
	   					maxVeo = 0;
	   					
	   				}
	   				
	   				if(stopAlarmMin.length == 0){
	   				
	   					stopAlarmMin = 30;
	   					
	   				}
	   				
	   				if(fromtime.length == 0){
	   				
	   					fromtime="1899-12-30 00:00:00.000";
	   					
	   				}else{
	   					fromtime="1899-12-30 " + fromtime+".000";
	   				}
	   				
	   				if(totime.length == 0){
	   				
	   					totime="1899-12-30 00:00:00.000";
	   					
	   				}else{
	   					totime="1899-12-30 " + totime+".000";
	   				}
	   				
	   				if(id.length>0){
	   					parmjson.push({id:id,fenceId : fenceid,vhcId : vechileId,fromtime : fromtime,totime : totime,regionAlarmType : 0,deviateDis : deviateDis,stopAlarmMin : stopAlarmMin,minVeo : 0,maxVeo :maxVeo});
	   				}else{
	   					parmjson.push({fenceId : fenceid,vhcId : vechileId,fromtime : fromtime,totime : totime,regionAlarmType : 0,deviateDis : deviateDis,stopAlarmMin : stopAlarmMin,minVeo : 0,maxVeo :maxVeo});
	   				}
	   			}	
	   			
	   		
	   		}	
	   		
	   });
	   
	   if(parmjson.length == 0){
	   		$("#saveFenceBtn").removeAttr("disabled");//去掉button的disabled属性，使button可用
	   		return;
	   }else{
	   		fenceJson ={};
	   		fenceJson.fenceVehicle=parmjson;
	   }
	   
	   $("#fencesaveMsg").text(manager.zztj);
	   
	   $.ajax({
	        type: "POST",
	        dataType: "json",
	        url: "json/fenceAction_addOrUpdateFenceVehicle.action",
	        data: {fenceJson:JSON.stringify(fenceJson)},
	        async : false,
	        success: function(json) {
	        		
	        		if(json.flag == true){
	        		
	        			$("#fencesaveMsg").text(electronicFenceLanguage.bdcg);
	        
	        	    	$("#saveFenceBtn").removeAttr("disabled");//去掉button的disabled属性，使button可用
	        	    	
	        	    	showVechiletoFenceTable();
	        	    	
	        	    }else{
	        	    	
	        	    	$("#fencesaveMsg").text(json.msg);
	        	    	
	        	    	
	        
	        	    	$("#saveFenceBtn").removeAttr("disabled");//去掉button的disabled属性，使button可用
	        	    	
	        	    }
	        },
	        error: function() {}
	   });	

}

function getBingdingVehicleFromFenceData(ids){
	var result = null;
	$.ajax({
	        type: "POST",
	        dataType: "json",
	        url: "json/fenceAction_getFenceVehicle.action",
	        data: {fenceVehicleIds:ids},
	        async : false,
	        success: function(json) {	
	        	    
	        	    if(json.flag == true && json.data.length > 0){
	        	    
	        	    	result = json.data;
	        	    	
	        	    	/*for(var i=0 ; i< json.data.length ;i++){
	        	    	
	        	    		result[json.data[i].vhcId] = json.data[i];
	        	    		
	        	    	}*/
	        	    	
	        	    }      
	        },
	        error: function() {}
	   });	
	   
	   return result != null ? result : new Array();
}

function showborder (element){
	element.style.border="1px solid";
}

function hideborder (element){
	element.style.border="0px solid";
}
var isSaveFlag = false;
var isSaveLineFlag = false;
function beforeCloseSave(){		

						if($("#electronicFence_type").val() == 1){
						
								if(!electronicFence.draw.curPolygon){
									
									return;
									
								}
								
								if(!electronicFence.draw.curPolygon.getMap()){
									return;
								}
		   						
						   		var latlngarr = electronicFence.draw.curPolygon.getPath().getArray();
						   		
						   		var parmsArr = new Array();
						   		
						   		for(var i=0 ;i <latlngarr.length;i++ ){		   			
						   			
						   			parmsArr.push({"lat":latlngarr[i].lat(),"lng":latlngarr[i].lng(),"pointOrder":i});
						   				   					   			
						   		}
						   		
						   		var fenceJson = {};
								
								fenceJson.latlngs = parmsArr;
								
								fenceJson.fenceType = 1;
								
								fenceJson.fenceId = electronicFence.tree.curNodeId;
								
					   			$.ajax({
							        type: "POST",
							        dataType: "json",
							        url: "json/fenceAction_upDateFencePolygonOrFenceRoute.action",
							        data: {"fenceJson":JSON.stringify(fenceJson)},
							        async : false,
							        success: function(json) {	
										if(json.flag == true){
											for(var i=0 ; i < electronicFence.tree.fenceJson.data.length ; i++){
												
												if(electronicFence.tree.fenceJson.data[i].id == electronicFence.tree.curNodeId){
													
													electronicFence.tree.fenceJson.data[i].fencePolygon = new Array();
													
													for(var j =0 ; j < latlngarr.length ; j++){
																						
														electronicFence.tree.fenceJson.data[i].fencePolygon.push({lat : latlngarr[j].lat(),lng : latlngarr[j].lng(),fenceId : id ,pointOrder : j});
													}
													
													break;
												}
											}
										}
						                 
							        },
							        error: function() {}
						   	     });
						   	     
						   	     isSaveFlag = true;
						   	     
						   	}else if($("#electronicFence_type").val() == 2){
						   				        	 				        	
				        	 
						        	 var fenceJson = {};
						        	 
						        	 var parmsArr = new Array();
						        	 
						        	 for(var i=0;i<electronicFence.draw.lineLatLng.length;i++){
						        	 
						        	 	parmsArr.push({"lat":electronicFence.draw.lineLatLng[i].lat(),"lng":electronicFence.draw.lineLatLng[i].lng(),"pointOrder":i});
						        	
						        	 }
				   					
				   					 if(parmsArr.length == 0){
				   					  
				   					  	return;
				   					 	
				   					 }	
				   		
						
									 fenceJson.latlngs = parmsArr;
									
									 fenceJson.fenceType = 2;
									 
									 fenceJson.fenceId = electronicFence.tree.curNodeId;
									
						   			 $.ajax({
								        type: "POST",
								        dataType: "json",
								        url: "json/fenceAction_upDateFencePolygonOrFenceRoute.action",
								        data: {"fenceJson":JSON.stringify(fenceJson)},
								        async : false,
								        success: function(json) {	
											
											if(json.flag == true){
											
												for(var i=0 ; i < electronicFence.tree.fenceJson.data.length ; i++){
													
													if(electronicFence.tree.fenceJson.data[i].id == electronicFence.tree.curNodeId){
														
														for(var j =0 ; j < electronicFence.draw.lineLatLng.length ; j++){
																							
															electronicFence.tree.fenceJson.data[i].renceRoute.push({lat : electronicFence.draw.lineLatLng[j].lat(),lng : electronicFence.draw.lineLatLng[j].lng(),fenceId : electronicFence.tree.curNodeId ,pointOrder : j});
														}
														
														break;
													}
												}
												
											}
							                 
								        },
								        error: function() {}
							   	     });
									 isSaveLineFlag = false;       	 
					        	 
						   	}

}

function shieldFence(){
	javascript:electronicFence.tree.hide();
	$(".ico_weilan").hide();
	$("#fence-msg").hide();
	$("#fencePermissionsPanelBtn").hide();
}

function openFence(){
	if(usertype=="-1000"||usertype=="-100"){
		$(".ico_weilan").hide();
		$("#fencePermissionsPanelBtn").hide();
	}else{
		$(".ico_weilan").show();
		$("#fencePermissionsPanelBtn").show();
	}
}


function initFenceEventTree(){
	var groupNodeTreeStr="[{\"id\":\"fence_e_"+0+"\",\"open\":\"true\",\"title\":\""+electronicFenceLanguage.sysj+"\",\"name\":\""+electronicFenceLanguage.sysj+"\",\"icon\":\""+markerUrl.groupNodeicon+"\",\"pId\": \"-1\","+
								"\"children\": [{\"id\":\"fence_e_"+1+"\",\"title\":\""+electronicFenceLanguage.paizhao+"\",\"name\":\""+electronicFenceLanguage.paizhao+"\",\"icon\":\""+electronicFenceUrl.eventPhoto+"\",\"count\":\"0\",\"pId\": \"fence_e_0\"}"+							
							"]}]";

	    
	    groupNodeTreeStr=$.parseJSON(groupNodeTreeStr);
		
		var setting={
	    		callback: {
					onClick:null,
					onRightClick:null
				},
				data: {
					simpleData: {
						enable: true,
						idKey: "id"
					}
				}		
	    }
	    
		$.fn.zTree.init($("#electronicFence_event_tree"),setting,groupNodeTreeStr);
		
		
		fenceEventTree = $.fn.zTree.getZTreeObj("electronicFence_event_tree");
}

function saveFenceEvent(){

	var time = $("#photo_time").val();
	var times = $("#photo_times").val();
	
	 var typeValue = new Array();
	
	 var types = $("input[name='triggeringCondition']:checked");  

      types.each(function() {   
          	typeValue.push(this.value);
      });  
	
	var starttime = $("#fence_start").val();
	var endtime = $("#fence_end").val();
	
	var fenceId = electronicFence.tree.curNodeId;
	
	alert(time+","+times+","+typeValue.toString()+","+starttime+","+endtime);
	
	$.ajax({
	        type: "POST",
	        dataType: "json",
	        url: "",
	        data: {},
	        success: function(json) {
	        
				if(json.flag == true){

				}
                 
	        },
	        error: function() {}
   	     });
	
}

function cancleFencebingdingVehicletoFence(){
	
	$("#fencesaveMsg").text("");
	
	$("#cancleFenceBtn").attr("disabled","disabled");//设置所有的button不可用 
	
	var groupId = $("#fence_vehicle_group").val();
	
	if(groupId.length <= 0){
	
		$("#cancleFenceBtn").removeAttr("disabled");//去掉button的disabled属性，使button可用
		
		return;
		
	}
	
	var fenceId = $("#fence_selecte_id").val();
	
	if(groupId.length == 0){
	 	groupId = -1;
	}else{
	 	groupId = groupId.split("_")[2];
	}
	
	var curGroupChildsIds = equipmentMap[groupId] ? (equipmentMap[groupId].getChildsIds()) : (new Array()); 
	
	if( curGroupChildsIds.length <= 0 ){
	
		$("#cancleFenceBtn").removeAttr("disabled");//去掉button的disabled属性，使button可用
		
		return;
		
	}
	
	if(fenceId.length <= 0 ){
	
		$("#cancleFenceBtn").removeAttr("disabled");//去掉button的disabled属性，使button可用
		return;
		
	}else{
		fenceId = fenceId.split("_")[1];
	}
	
	
	 
	 
	$.ajax({
	        type: "POST",
	        dataType: "json",
	        url: "json/fenceAction_delFenceVehicle.action",
	        data: {fenceVehicleIds:curGroupChildsIds.toString(),fenceId:fenceId},
	        success: function(json) {
	        
				if(json.flag == true){
				
					showVechiletoFenceTable();
					
					$("#cancleFenceBtn").removeAttr("disabled");//去掉button的disabled属性，使button可用
					
				}else{
					
					$("#fencesaveMsg").text("*"+json.msg);
					
					$("#cancleFenceBtn").removeAttr("disabled");//去掉button的disabled属性，使button可用
				}
                 
	        },
	        error: function() {$("#cancleFenceBtn").removeAttr("disabled");}//去掉button的disabled属性，使button可用
   	     });
}


function modelPremiss(){

	var flag = false;
	
	$.ajax({
	        type: "POST",
	        dataType: "json",
	        async : false,
	        url: "manager/loginAction_modelPpermission.action",
	        success: function(json) {      
	         	
	         	flag = json.flag;
	         	
	        },
	        error: function() {}
   	     });
   	 
   	 return flag;   
}