define(function(require){
	var $ = require('jquery');
    require('uriTemplate')($);
    require('validation')($);
    require('dialog')($);
	var IFA = require('util');
	require("../../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../../styles/cim/validation.css');
	$(function(){
//		var reStr = '<label id="Receiver"><span style="color:#d3d3d3; font-style: italic;text-decoration:underline">请输入收件人名称   </span></label>';
//		var timeStr = '<label id="Time"><span style="color:#d3d3d3;text-decoration:underline;font-style: italic">请输入时间</span></label>';
//		var dayStr = '<label id="day"><span style="color:#d3d3d3;text-decoration:underline;font-style: italic">请输入日期</span></label>';
//		var placeStr =  '<label id="Place"><span style="color:#d3d3d3;text-decoration:underline;font-style: italic">请输入地点</span></label>';
//		var airportStr = '<label id="Place"><span style="color:#d3d3d3;text-decoration:underline;font-style: italic">请输入机场名</span></label>';
//		var voucherStr = '<label id="Place"><span style="color:#d3d3d3;text-decoration:underline;font-style: italic">请输入礼卷编号</span></label>';
//		var carStr = '<label id="Place"><span style="color:#d3d3d3;text-decoration:underline;font-style: italic">请输入车型</span></label>';
//		var licenseStr= '<label id="Place"><span style="color:#d3d3d3;text-decoration:underline;font-style: italic">请输入车牌号</span></label>';
//		var colorStr = '<label id="Place"><span style="color:#d3d3d3;text-decoration:underline;font-style: italic">请输入车的颜色</span></label>';
//		var conciergeStr = '<label id="Place"><span style="color:#d3d3d3;text-decoration:underline;font-style: italic">请输入礼宾小姐姓名</span></label>';
//		var driverStr = '<label id="Place"><span style="color:#d3d3d3;text-decoration:underline;font-style: italic">请输入司机姓名</span></label>';
//		var phoneStr = '<label id="Place"><span style="color:#d3d3d3;text-decoration:underline;font-style: italic">请输入手机号</span></label>';
//		var cedata = CKEDITOR.replace('smsTemplateContent',{skin:'kama',language:'zh-cn'});
		$('#receiver').bind("click",function(){
			str="{personName}";
			fillTemplate(str);
		});
		$('#time').bind("click",function(){
			str="{time}";
			fillTemplate(str);
		});
		$('#day').bind("click",function(){
			str="{day}";
			fillTemplate(str);
		});
		$('#place').bind("click",function(){
			str="{pickupLocation}";
			fillTemplate(str);
		});
		$('#airport').bind("click",function(){
			str="{arrivalAirport}";
			fillTemplate(str);
		});
		$('#destination').bind("click",function(){
			str="{returnLocation}";
			fillTemplate(str);
		});
		$('#voucher').bind("click",function(){
			str="{voucherNo}";
			fillTemplate(str);
		});
		$('#car').bind("click",function(){
			str="{carTypeName}";
			fillTemplate(str);
		});
		$('#license').bind("click",function(){
			str="{plateNumber}";
			fillTemplate(str);
		});
		$('#color').bind("click",function(){
			str="{color}";
			fillTemplate(str);
		});
		$('#concierge').bind("click",function(){
			str="{conciergeName}";
			fillTemplate(str);
		});
		$('#driver').bind("click",function(){
			str="{chauffeurName}";
			fillTemplate(str);
		});
		$('#cphone').bind("click",function(){
			str="{conciergePhone}";
			fillTemplate(str);
		});
		$('#dphone').bind("click",function(){
			str="{chauffeurPhone}";
			fillTemplate(str);
		});
		$('#resRmarks').bind("click",function(){
			str="{resRemarks}";
			fillTemplate(str);
		});
		$('#taskUrl').bind("click",function(){
			str="{taskUrl}";
			fillTemplate(str);
		});
		function fillTemplate(str){
		    var tc = document.getElementById("smsTemplateContent");
		    var tclen = tc.value.length;
		    tc.focus();
		    if(typeof document.selection != "undefined")
		    {
		        document.selection.createRange().text = str;  
		    }
		    else
		    {
		        tc.value = tc.value.substr(0,tc.selectionStart)+str+tc.value.substring(tc.selectionStart,tclen);
		    }
		    return false;
		}
		$("#savaHref").click(function(){
			if($('#smsTemplate').valid()){
				var url=$("#saveUrl").val().trim();
				$("#smsTemplate").attr('action',url);
				$("#smsTemplate").submit();		
			}else{
				$.msg.alert(i18n["Page.Dialog.content.inputError"]);
			}
			 return false;
		});
		$("#cancel").click(function(){
         	  var urlstr = $("#cancelUrl").val().trim();
           	  location.href = urlstr;
           	  return false;
		});
	});
	
	$("#smsTemplate").validate({
		rules:{
			smsTemplateTitle:{
			    required:true,
			    maxlength:200,
			    VerificationSmsTemplateTitle:true
			},
			smsTemplateRemarks:{
				maxlength:100
			},
			smsTemplateContent:{
				maxlength:1000
			}
		}
	});

	$.validator.addMethod("VerificationSmsTemplateTitle",function(value,element,param){
		var smsTemplateTitle = $.trim($("#smsTemplateTitle").val());
		var smsTemplateId = $.trim($('#smsTemplateId').val());
		var flag = "";
			var urlStr = $("#uniqueUrl").val();
		    $.ajax({
		    	type:"POST",
		    	url:urlStr+".json",
				async:false,
		    	data:{"smsTemplateId":smsTemplateId,"smsTemplateTitle":smsTemplateTitle},
			    success: function(response,ioArgs){
		   		    flag = response.status;
			   	},error:function(response,ioArgs){
			   		alert(ioArgs);
			   	}
		   	});
		return flag;
	},i18n["Validation.smstemplate.norepeat"]);
});