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(){	
		getDistrictForArea();
		getDistrictForId();
//		 $("#area").one("click",function(){getDistrictForArea();});
		 $("#districtId").one("click",function(){getDistrictForId();});
		 $("#area").change(function(){getDistrictForId();});
		 $("#save").click(function(){submitForm();});
		 $("#cancel").click(function(){close();});
	});
	var validateList ;
	$(function(){
		// 验证用户正确性
	    validateList = $("#newPeopleProfile").validate({
	      // rules,用户定义键/值对规则;键为一个表单元素的 name属性（或是一组单选/复选按钮）、
		  // 值为一个简单的字符串或者由规则/参数对（rule/parameter）组成的一个对象
		  rules:{
		       cnName:{
	              required:true,
	              //regex: /^[\u4E00-\u9FA5\uF900-\uFA2D]+$/,  // 必须添加转义字符'/'--否则出错
	              rangelength:[0,20]
	           },
		       enName:{
	    	      required:true,
	    	      regex:/^[A-Za-z ]+$/,
	    	      rangelength:[0,20]
	    	   },
	    	   jobNumber:{
	    		  required:true,
	    		  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]
	    		},
	    		districtId:{
	    			required:true,
	    			checkDistrictId:true
	    		},
	    		groupId:{
	    			required:true
	    		},
		       email:{
	    		  required:true,
	    		  regex:/^(|\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+)$/,
	    		  rangelength:[0,50]
	    		},
	    		peopleRemarks:{
	      		  rangelength:[0,100]
	        	}
		  },
		  errorPlacement:function (error,element){		  
				  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:{
				  required:i18n["Validation.peopleProfile.jobNumber.notNull"],
				  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"]
			  },
	    	  groupId:{
	    		  required:i18n["Validation.peopleProfile.groupName.notNull"]
	    	  },
    		  districtId:{
	    		  required:i18n["Validation.peopleProfile.district.notNull"]
	    	  },
			  email:{
				  required:i18n["Validation.peopleProfile.email.notNull"],
				  regex:i18n["Validation.peopleProfile.email.regex"],
				  rangelength:i18n["Validation.peopleProfile.email.length"]
			  },
			  peopleRemarks:{
				  rangelength:i18n["Validation.peopleProfile.remarks.length"]
			  }
		   }
		});
	});
	$(function(){
		// 验证工号唯一性
		$.validator.addMethod("checkJobNumber",function(value,element,param){
			var jobNumber = $("#jobNumber").val().trim();
			var flag = false;
			if(jobNumber==""||jobNumber==undefined){
				flag = true;
			}else{
				var urlStr = new $.Template($("#validateUrl").val());
				var params = {"peopleId":-1,"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"]);
	});
	
	   // 获取区域级别的门店
	  function getDistrictForArea(){
		   var params ={"districtLevel":0,"parentCode":0};
		   getDistrict($("#area"),params);
		   getDistrictForId();
	   };
	   // 获取城市级别信息
	   function getDistrictForId(){
		   var parentCode = $("#area").val(); // 获取父节点编号
		   if(parentCode>0){
			   var params ={"districtLevel":1,"parentCode":parentCode};
			   getDistrict($("#districtId"),params);
		   }
	   };
	   function getDistrict($obj,params){
		   var getUrl = $("#getUrl").val()+".json";
		   $.ajax({
			   url:getUrl,
			   type:"POST",
			   data:params,
			   dataType:"json",
			   success:function(response,ioArgs){
			       //$obj.append("<option value='-1'>----请选择----</option>");
			       var list = response.districtList;
			       if(list!=null){
					   $obj.empty();
					   $obj.append("<option value='-1'>"+i18n['Title.peopleProfile.choose']+"</option>");
					   $.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);
					   });
			       }
		       }
		   });
	   };
		
		// 函数,提交表单
		function submitForm(){
			if($("#newPeopleProfile").valid()){
				var url = $("#saveUrl").val();
				$("#newPeopleProfile").attr("action",url);
				$("#newPeopleProfile").submit();
			}
		};
		// 关闭窗口
		function close(){
		    // 取不到值!须修改
			var indexUrl= $("#indexUrl").val();
		    window.location = indexUrl;
		};
});

