define(function(require) {
	//加载JS库
	var $ = require('jquery');
	require('grid')($);
	require('validation')($);
	require('uriTemplate')($);
	require('../../../styles/cim/jquery-ui-1.8.14.custom.css');
	require('../../../styles/cim/validation.css');
	var IFA = require('util');
	var validateList;
	$(function() {
		
		validateList = $("#newCarType").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"]
				}
			}
		});
		$("#save").bind("click", saveCarType);
		$("#cancel").bind("click", close);
		//检查车型名称是否已经存在
		$.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);
			$.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 url = $("#createUrl").val();
		$("#newCarType").attr("action", url);
		$("#newCarType").submit();
		if(validateList.errorList.length>0) {
			$("saveHref").attr("href", "javascript:saveCarType();");;
		}
	}

	//点击取消按钮回到车型管理主页面
	function close() {
		var url = $("#cancelUrl").val();
		window.location.href = url;
	}
	$(function init() {
		if('<c:out value="${message}"/>'=='<ifa:constant namespace="I18nMsgKey" fieldName="ENTITY_SAVE_SUCCEED"/>'){
	    	$.msg.alert(i18n["Page.title.dialogshow.title"],"<fmt:message key='${message}'/>",{
	     		buttons: IFA.generateJson(i18n["Entity.Operation.ok"],function(){
		    		var message = '<c:out value="${message}"/>';
		    		if(message!=''){
		    			if(message=='<ifa:constant namespace="I18nMsgKey" fieldName="ENTITY_SAVE_SUCCEED"/>'){
		    				window.location="${contextPath}<ifa:constant namespace='cim' fieldName='HERTZ_VEHICLE_GROUP_PROFILE_URL'/>";
		    			}
		    		}$(this).dialog("close");
		    	})});
	    }
	});
	
	
});