define(function(require){
	var $ = require('jquery');
	require('validation')($);
	require('dialog')($);
	require('uriTemplate')($);
	var IFA = require('util'); 
	require("../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../styles/cim/validation.css');  

	$(function(){
//		 initShow(); 
		 getDistrictForArea();
//		 $("#area").one("click",function(){getDistrictForArea();});
		 $("#area").change(function(){getDistrictForId();});
		 $("#districtId").change(function(){getRealDistrict();});
		 $("#save").click(function(){submitForm();});
		 $("#cancel").click(function(){
			 // 进入查询页面URL
		      var indexUrl= $("#indexUrl").val();
		      window.location = indexUrl;
		 });
	});
	$(function(){
		// 验证用户正确性
	    $("#updateChauffeur").validate({
	    	 rules:{
		       cnName:{
	              required:true,
//	              regex: /^[\u4E00-\u9FA5\uF900-\uFA2D]+$/,  // 必须添加转义字符'/'--否则出错
	              rangelength:[0,20]
	           },
		       enName:{
	    	      required:true,
	    	      regex:/^[A-Za-z ]+$/,
	    	      rangelength:[0,20]
	    	   },
	    	   jobNumber:{
	    		  rangelength:[0,20],
	    		  regex: /^[0-9a-zA-Z]{0,20}$/,
	    		  checkJobNumber:true
	    		},
		       mobileTel:{
	    		  required:true,
//	    		  regex:/^(?:\(?[0\+]?\d{1,3}\)?)[\s-]?(?:0|\d{1,4})[\s-]?(?:(?:13\d{9})|(?:\d{7,8}))$/,
	    		  rangelength:[0,20]
	    		},
		       rentalType:{
	    		  required:true
	    		},
	    		districtId:{
	    			required:true,
	    			checkDistrictId:true
	    		},
	    		realDistrict:{
	    			required:true,
	    			checkRealDistrict:true
	    		},
		       email:{
	    		  regex:/^(|\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+)$/,
	    		  rangelength:[0,50]
	    		},
	    		language:{
	    		  rangelength:[0,20]
		  		},
	    		peopleRemarks:{
	      		  rangelength:[0,100]
	        	}
		  },
		  errorPlacement:function (error,element){
			  if(element.attr("name")=="rentalType"){
				  error.insertAfter("#radio");
			  }else{
				  error.insertAfter(element);
			  }
		   },
		
		  // 用户自定义的键/值对消息;键为一个表单元素的name属性，值为该表单元素将要显示的消息。该消息覆盖元素的title属性或者默认消息
		   messages:{
			  cnName:{
			     required:i18n["Validation.peopleProfile.cnName.notNull"],
//			     regex:i18n["Validation.peopleProfile.cnName.regex"],
			     rangelength:i18n["Validation.peopleProfile.cnName.length"]   
			  },
			  enName:{
				  required:i18n["Validation.peopleProfile.enName.notNull"],
				  regex:i18n["Validation.peopleProfile.enName.regex"],
				  rangelength:i18n["Validation.peopleProfile.enName.length"]   
			  },
			  jobNumber:{
				  regex:i18n["Validation.peopleProfile.jobNumber.regex"],
				  rangelength:i18n["Validation.peopleProfile.jobNumber.length"]
			  },
			  mobileTel:{
				  required:i18n["Validation.peopleProfile.mobileTel.notNull"],
//				  regex:i18n["Validation.peopleProfile.mobileTel.regex"],
				  rangelength:i18n["Validation.peopleProfile.mobileTel.length"]
			  },
			  rentalType:{
	    		  required:i18n["Validation.peopleProfile.rentalType.notNull"]
	    	  },
			  districtId:{
	    		  required:i18n["Validation.peopleProfile.district.notNull"]
	    	  },
			  email:{
				  regex:i18n["Validation.peopleProfile.email.regex"],
				  rangelength:i18n["Validation.peopleProfile.email.length"]
			  },
			  language:{
				  rangelength:i18n["Validation.peopleProfile.language.length"]
			  },
			  peopleRemarks:{
				  rangelength:i18n["Validation.peopleProfile.remarks.length"]
			  }
		   }
		});
	});
	$(function(){
		// 验证工号唯一性
		$.validator.addMethod("checkJobNumber",function(value,element,param){
			var jobNumber = $("#jobNumber").val().trim();
			var peopleId = $("#peopleId").val().trim();
			var flag = false;
			if(jobNumber==""||jobNumber==undefined){
				flag = true;
			}else{
				var urlStr = new $.Template($("#validateUrl").val());
				var params = {"peopleId":peopleId,"jobNumber":jobNumber};
			    $.ajax({
			    	url:urlStr.expand(params)+".json",
			    	dataType:'json',
					async: false,
				    success: function(data){
			   		    flag = data.flag;
				   	}
			   	});
			}
			return flag;
		},i18n["Validation.peopleProfile.jobNumber.exist"]);
		// 验证城市不能为空
		$.validator.addMethod("checkDistrictId",function(value,element,param){
			var districtId=$("#districtId").val();
			if(districtId<0 || districtId==null){
				return false;
			}else{
				return true;
			}
		},i18n["Validation.peopleProfile.district.notNull"]);
	});
	
	// 验证门店不能为空
	$.validator.addMethod("checkRealDistrict",function(value,element,param){
		var districtId=$("#realDistrict").val();
		if(districtId<0 || districtId==null){
			return false;
		}else{
			return true;
		}
	},i18n["Validation.peopleProfile.district.notNull"]);
		
	   // 获取区域级别的门店
	  function getDistrictForArea(){
		   var params ={"districtLevel":0,"parentCode":0};
		   getDistrict($("#area"),params);
		   $("#area").find("option[value="+$("#parentCode").val()+"]").attr("selected",true);
		   getDistrictForId();
	   };
	   // 获取城市级别信息
	   function getDistrictForId(){
		   var parentCode = $("#area").val(); // 获取父节点编号
		   if(parentCode>0){
			   var params ={"districtLevel":1,"parentCode":parentCode};
			   getDistrict($("#districtId"),params);
			   $("#districtId").find("option[value="+$("#showDistrictId").val()+"]").attr("selected",true);
			   getRealDistrict();
			   $("#realDistrict").find("option[value="+$("#showRealDistrict").val()+"]").attr("selected",true);
		   }else{
			   $("#districtId").empty();			  
			   $("#realDistrict").empty();			
		   }
	   };
	   // 获取门店级别信息
	   function getRealDistrict(){
		   var parentCode = $("#districtId").val(); // 获取父节点编号
		   if(parentCode>0){
			   var params ={"districtLevel":2,"parentCode":parentCode};
			   getDistrict($("#realDistrict"),params);
		   }else{
			   $("#realDistrict").empty();			  
		   }
	   };
	   function getDistrict($obj,params){
		   var getUrl = $("#getUrl").val()+".json";
		   $.ajax({
			   url:getUrl,
			   type:"POST",
			   data:params,
			   dataType:"json",
			   async:false,
			   success:function(response,ioArgs){
			       //$obj.append("<option value='-1'>----请选择----</option>");
			       var list = response.districtList;
			       if(list!=null){
					   $obj.empty();
					   $.each(list,function(index,item){
						    // 在此处同时保存门店的id和名字,显示门店名称,但保存门店id
						    //$obj.append("<option value="+item.districtId+">"+item.districtName+"</option>");
						    $("<option></option>").val(item.districtId).text(item.districtName).appendTo($obj);
					   });
					  // $obj.first().attr("selected","selected");
			       }
		       }
		   });
	   };
	// 用户点击"编辑"后,页面初始化,加载原来的用户信息   
	function initShow(){
		// 获取父节点编号,然后根据父节点编号和门店层级获取父节点的名称并显示
		var parentCode = $("#parentCode").val();
		if(parentCode>0){
			 var params ={"districtLevel":0,"parentCode":parentCode};
			 getDistrict($("#area"),params);
		}
	};
	
	// 函数,提交表单
	function submitForm(){
		if($("#updateChauffeur").valid()){
			$("#save").attr("href","#"); // 改变href的值
			var urlStr = $("#updateUrl").val();
			$("#updateChauffeur").attr("action",urlStr);
			$("#updateChauffeur").submit();
		}
	};
});

