define(function(require) {
	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');
	$(function(){
		$("#save").bind("click",save);
		$("#newPayMethodAttribute").validate({
			rules:{
				modelName:{
					required:true,
					checkExisting:true
				},
				showName:{
					required:true
				}
				
			}
		});
		
		$.validator.addMethod("checkExisting",function(value,element,param){
			var urlStr = new $.Template($("#checkingUrl").val());
			var name = $(element).attr("name");
			var params = {itemName:name,itemValue:value,id:$("#id").val()};
			var url = urlStr.expand(params);
			var result = false;
			$.ajax({
				url:url,
				async:false,
				success:function(response){
					if(response.checkResult){
						result= true;
					}
				}
			});
			return result;
		},i18ns("Entity.payMethod.beenUsed"));
	});
	
	function save(){
		if(!$("#newPayMethodAttribute").valid())
			return;
		var creationUrl = $("#updateUrl").val();
		$.msg.confirm(i18n['Page.Dialog.title'],i18n['Page.title.updateSure'],function(){},{buttons:
			IFA.generateJson(i18n['ConditionMap.ok'],function(){
				$.post(creationUrl,$("#newPayMethodAttribute").serialize()+"&_method=put",function(){
					$.msg.alert(i18n['Page.Dialog.title'],i18n['Entity.Operation.Save.Success'],function(){},{buttons:IFA.generateJson(i18n['ConditionMap.ok'],
					function(){
						$(this).dialog('close');
						location.href=$("#indexUrl").val();
					}
					)});
				});
				$(this).dialog('close');
			},
			i18n['ConditionMap.cancel'],function(){
				$(this).dialog('close');
			}
			)});
	}
	
});