define(function(require){
	var $ = require('jquery');
	require('uriTemplate')($);
	require('dialog')($);
	require('datepicker')($);
	require('validation')($);
	require('grid')($);
	var IFA = require('util');
	require('../../../styles/cim/jquery-ui-1.8.14.custom.css');
	require('../../../styles/cim/cim-grid.css');
	$(function(){
		//searchResult();
		$("#saveRate").bind("click",saveRate);
		$("#cancel").bind("click",cancel);
		$("#selectall").toggle(function(){
			$("#selectall").attr("checked","checked");
			$("input[ name^='applicable']").attr("checked","checked");
		},function(){
			$("#selectall").removeAttr("checked");
			$("input[name^='applicable']").removeAttr("checked");
		});
	});
	//全选
	 function selectall(){
			var check = $(this).attr("checked");
			if(check){
				$("input[type='checkbox']").attr("checked","checked");
			}
	 }
	 //反选
	 function unselectall(){
		 $(this).removeAttr("checked");
		 $("input[type='checkbox']").removeAttr("checked");
	 }
	
	function saveRate(){
		var statue = $("#rateForm").valid();
		if(!statue){return false;}
		var checkbox=$("#checkboxs :checkbox");
		var checked=false;
		var len=checkbox.length;
		for(var i=0;i<len;i++){
			if($(checkbox[i]).attr("checked")){
				checked=true;
			}
		}
		
		if(!checked){
			$.msg.alert(i18ns("Rate.selectApplicable"));
			return;
		}
		
		$.msg.confirm(i18n['Page.Dialog.title'],i18n['Page.title.CreateConfirm'],function(result){
			if(result){
				$("#rateForm").submit();
				}
			
		});
	
	}
	
	function cancel(){
		var url = $("#cancelUrl").val();
		location.href=url;
	}
	
	$("#rateForm").validate({
		rules:{
			rateName:{
				required:true,
				VerifiRateName:true
			},
			ratePrice:{
				required:true,
				regex: /^((([0-9]|\,)*(\.[0-9]{1,2})?)|(([0-9]|\.)*(\,[0-9]{1,2})?)|(([0-9]|\')*((\.|\,)[0-9]{1,2})?))$/
			}
		},
		messages:{
			rateName:{
				required:i18n["Rate.Validate.rateName"]
			},
			ratePrice:{
				required:i18n["Rate.Validate.ratePrice"],
				regex:i18n["Rate.Validate.ratePrice.reg"]
			}
		}
	});
	
	$.validator.addMethod("VerifiRateName",function(value,element,param){
		var rateName = $('#rateName').val();
		var flag = "";
			var urlStr = $("#uniqueUrl").val();
		    $.ajax({
		    	type:"POST",
		    	url:urlStr+".json",
				async:false,
		    	data:{"rateName":rateName},
			    success: function(response,ioArgs){
		   		    flag = response.statue;
			   	},error:function(response,ioArgs){
			   		alert(ioArgs);
			   	}
		   	});
		return flag;
	},i18n["RateSheet.validate.unique.rateName"]);


});