define(function(require) {
	//加载JS库
	var $ = require('jquery');
	require('datepicker')($);
	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 = $("#newSaleGroup").validate({
			rules: {
			groupName: {
				required:true,
				rangelength: [1, 20],
				regex:/^([a-zA-Z0-9]|[\u4e00-\u9fa5]|[_]|[-]|[ ])*$/,
				checkExists:true
			},
			groupDescription: {
				rangelength: [1, 100],
				regex:/^([a-zA-Z0-9]|[\u4e00-\u9fa5]|[_]|[-]|[ ])*$/
			}
		},
		messages: {
			groupName: {
				required:i18n["Entity.saleGroup.inputSaleGroupName"],
				rangelength: i18n["Entity.vehicle.requireFuleConsumption"],
				regex: i18n["Entity.vehicle.requireGasTankDescription"]
			},
			groupDescription: {
				required:i18n["Entity.saleGroup.inputSaleGroupDescription"],
				rangelength: i18n["Entity.vehicle.requireReamrks"],
				regex: i18n["Entity.vehicle.requireGasTankDescription"]
			}
		}
	});
	$("#save").bind("click", saveSaleGroup);
	$("#cancel").bind("click", close);
});
	$.validator.addMethod("checkExists",function(value,element,param) {
		var flag = true;
		var urlStr = new $.Template($("#checkUrl").val());
 		var urlParam = {"groupName":$("#groupName").val()};
 		var urlStr = urlStr.expand(urlParam);
		$.ajax({
			url:urlStr,
			async: false,
			success:function(data) {
				if(data.flag == "groupNameRepeat") {
					flag = false;
				} else {
					flag = true;
				}
			}
		});
		return flag;
	}, i18n["Entity.saleGroup.saleGroupNameExists"]);
	//点击触发，保存新增车型信息
	function saveSaleGroup() {
		$("#saveHref").attr("href", "#");
		var url = $("#createUrl").val();
		$("#newSaleGroup").attr("action", url);
		$("#newSaleGroup").submit();
		if(validateList.errorList.length>0) {
			$("saveHref").attr("href", "javascript:saveVehicle();");;
		}
	}
	//点击取消按钮回到车型管理主页面
	function close() {
		var url = $("#cancelUrl").val();
		window.location.href = url;
	}
	
});