define(function(require) {
	//加载JS库
	var $ = require('jquery');
	require('grid')($);
	require('validation')($);
	require('uriTemplate')($);
	require('datepicker')($);
	require('../../../styles/cim/jquery-ui-1.8.14.custom.css');
	require('../../../styles/cim/validation.css');
	var IFA = require('util');
	var validateList;
	//控制页面第一次进入编辑页面不跳转的标志位
	var flag = false;
	var groupNameBefore;
	$(function() {
		groupNameBefore = $("#groupName").val();
		if(flag){
	    	window.location.href=$("#cancelUrl").val();
	    }
		flag == false?true:false;
		$("#save").bind("click", saveSaleGroup);
		$("#cancel").bind("click", close);
		validateList = $("#editSaleGroup").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.requireGasTank"],
				regex: i18n["Entity.vehicle.requireGasTankDescription"]
			},
			groupDescription: {
				rangelength: i18n["Entity.vehicle.requireReamrks"],
				regex: i18n["Entity.vehicle.requireGasTankDescription"]
			}
		}
	});
	});
	$.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);
 		if(groupNameBefore == $("#groupName").val()) {
 			return true;
 		} else {
 			$.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 urlStrUpdate = new $.Template($("#updateUrl").val());
 		var urlParamUpdate = {"groupId":$.trim($("#groupId").val())};
 		var updateUrl = urlStrUpdate.expand(urlParamUpdate);
		$("#editSaleGroup").attr("action", updateUrl);
		$("#editSaleGroup").submit();
		if(validateList.errorList.length>0) {
			$("saveHref").attr("href", "javascript:saveSaleGroup();");;
		}
	}
	//点击取消按钮回到管理主页面
	function close() {
		var url = $("#cancelUrl").val();
		window.location.href = url;
	}
	$(function init() {
		if($("#messageUpdate").val() == "success"){
	    	window.location = $("#cancelUrl").val();
	    }
	});
	
	
});