define(function(require) {
	//加载JS库
	var $ = require('jquery');
	require('grid')($);
	require('validation')($);
	require('dialog')($);
	require('uriTemplate')($);
	require('../../../styles/cim/jquery-ui-1.8.14.custom.css');
	require('../../../styles/cim/validation.css');
	var IFA = require('util');
	var validateList;
	//设置标志位，更新成功后自动跳转到Index页面（第一次进入编辑页面控制不跳转）
	var flag = false;
	var carTypeNameBefore;
	$(function() {
		carTypeNameBefore = $("#carTypeName").val();
		if(flag){
	    	window.location.href=$("#cancelUrl").val();
	    }
		flag == false?true:false;
		$("#save").bind("click", saveCarType);
		$("#cancel").bind("click", close);
		validateList = $("#editCarType").validate({
			rules: {
				carTypeName: {
					required:true,
					rangelength: [1, 20],
					regex:/^([a-zA-Z0-9]|[\u4e00-\u9fa5]|[_]|[-])*$/,
					checkExists:true
				},
				categoryId: {
					required:true
				},
				carTypeDescription: {
					required:true,
					rangelength: [5, 100],
					regex:/^([a-zA-Z0-9]|[\u4e00-\u9fa5]|[_]|[-]|[ ])*$/
				}
			},
			messages: {
				carTypeName: {
					required:i18n["Entity.carType.inputCarTypeName"],
					rangelength: i18n["Entity.carType.requireCarTypeName"],
					regex: i18n["Entity.carType.requireCarTypeNameDescription"]
				},
				carTypeDescription: {
					rangelength: i18n["Entity.carType.requireCarTypeDescription"],
					regex: i18n["Entity.carType.requireCarTypeDescriptionDescription"]
				},
				categoryId: {
					required: i18n["Entity.carType.pleaseSelectCategory"]
				}
			}
		});
		
	});
	//检查车型名称是否已经存在
	$.validator.addMethod("checkExists",function(value,element,param) {
		var flag = true;
		var urlStr = new $.Template($("#checkUrl").val());
 		var urlParam = {"carTypeName":$("#carTypeName").val()};
 		var urlStr = urlStr.expand(urlParam);
 		if(carTypeNameBefore == $("#carTypeName").val()) {
 			return true;
 		} else {
 			$.ajax({
 				url:urlStr,
 				async: false,
 				success:function(data) {
 					if(data.flag == "carTypeNameRepeat") {
 						flag = false;
 					} else {
 						flag = true;
 					}
 				}
 				
 			});
 			return flag;
 		}
	}, i18n["Entity.carType.carTypeNameExists"]);
	//点击触发，保存新更新的车型信息
	function saveCarType() {
		$("#saveHref").attr("href", "#");
		var urlStrUpdate = new $.Template($("#updateUrl").val());
 		var urlParamUpdate = {"carTypeId":$.trim($("#carTypeId").val())};
 		var updateUrl = urlStrUpdate.expand(urlParamUpdate);
		$("#editCarType").attr("action", updateUrl);
		$("#editCarType").submit();
		if(validateList.errorList.length>0) {
			$("saveHref").attr("href", "javascript:saveCarType();");;
		}
	}
	//点击取消按钮回到车型管理主页面
	function close() {
		var url = $("#cancelUrl").val();
		window.location.href = url;
	}
	$(function init() {
		if($("#message").val() == $("#I18nMsgKey").val()){
	    	window.location.href = $("#cancelUrl").val();
	    }
		
	});
	
	
});