define(function(require){
	var $ = require('jquery');
	var IFA = require('util');
	require('validation')($);
	require('dialog')($);
	require('uriTemplate')($);
	require("../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../styles/cim/validation.css');
	$(function(){	
		 $("#rankLevel").change(function(){showDistrict();});
		 $("#save").click(function(){submitForm();});
		 $("#cancel").click(function(){close();});
	});
	$(function(){
		// 验证用户正确性
	    $("#cityForm").validate({
		  rules:{
	    	name:{
	              required:true,
	              //regex: /^[\u4E00-\u9FA5\uF900-\uFA2D]+|[a-zA-Z]+$/,  // 必须添加转义字符'/'--否则出错
	              regex:/^[^\|"'<>:;\?\\\\\/]*$/,//不能输入|"'<>:;?\/
	              rangelength:[0,20],
	              checkName:true
	           },
	           hotkey:{
	    	      required:true,
	    	      regex:/^[A-Za-z0-9]+$/,
	    	      rangelength:[0,16]
	    	   }
		  },
		  errorPlacement:function (error,element){
				error.insertAfter(element);
		   },
		
		  // 用户自定义的键/值对消息;键为一个表单元素的name属性，值为该表单元素将要显示的消息。该消息覆盖元素的title属性或者默认消息
		  messages:{
			   name:{
			     required:i18n["Entity.city.name.notNull"],
			     regex:i18n["Entity.city.name.regex"],
			     rangelength:i18n["Entity.city.name.length"]   
			  },
			  hotkey:{
				  required:i18n["Entity.city.hotkey.notNull"] ,
				  regex:i18n["Entity.city.hotkey.regex"],
				  rangelength:i18n["Entity.city.hotkey.length"]   
			  }
		   }
		});

		$.validator.addMethod("checkName",function(value,element,param){
			var parentCode = $("#parentId").val();
			var districtName = value;
			var urlStr = $("#checkNameUrl").val();
			var params = {"parentId":parentCode,"name":districtName,"id":-1};
			var flag = false;
			 $.ajax({
			    	url:urlStr,
			    	dataType:'json',
			    	type:"POST",
			    	data:params,
					async: false,
				    success: function(data){
			    	   flag = data.flag;
				   	}
			   	});
			   	 return flag;
		},i18n["Validation.district.name.exist"]);
	});
	
	   // 获取城市信息
	   function getDistrict(params){
		   $("#parentId").empty(); //清空原数据
		   var getUrl = $("#getUrl").val()+".json";
		   $.ajax({
			   url:getUrl,
			   type:"POST",
			   data:params,
			   dataType:"json",
			   timeout:5000,
			   success:function(response,ioArgs){
			       var list = response.cityList;
			       if(list!=null){
			    	   $.each(list,function(index,item){
						      // 在此处同时保存门店的id和名字,显示门店名称,但保存门店id
						   $("<option></option>").val(item.id).text(item.name).appendTo($("#parentId"));
			    		
					   });
			       }
		       }
		   });
	   }

	   // 获取城市信息
	   function showDistrict(){
		   var level = $("#rankLevel").val();
		   if(level==0){
			   $("#parentId").hide();
		   }
		   else if(level==1){
			   $("#parentId").show();
			   var params ={"rankLevel":level - 1};
			   getDistrict(params);
		   }
		   else{
			   $("#parentId").show();
			   var params ={"rankLevel":level - 1};
			   getDistrict(params);
		   }
	   }

		// 函数,提交表单
		function submitForm(){
			
			/*
			if($("#newDistrict").valid()){  
				setParentCode();
				//$("#save").attr("href","#"); // 改变href的值
				var url = $("#saveUrl").val();
				//$("#newDistrict").attr("action",url);
				//$("#newDistrict").submit();
				$.post(url,$("#newDistrict").serialize(),function(data){
					if(data.error){
						$("#newDistrict").valid();
					}else{
						$.msg.confirm(i18n['Page.Dialog.title'],i18n["Entity.save.succeed"],function(){
						},{buttons:IFA.generateJson(i18n['Page.Dialog.ok'],function(){
							var indexUrl= $("#indexUrl").val();
						    window.location = indexUrl;
						})});
					}
				});
			}*/
			if($("#cityForm").valid()){ 
				$("#cityForm").submit();
			}
		};
		// 关闭窗口
		function close(){
			// 进入查询页面URL
		      var indexUrl= $("#indexUrl").val();
		      window.location = indexUrl;
		};
});

