define(function(require){
	var $ = require('jquery');
	require('uriTemplate')($);
	require('dialog')($);
	require('datepicker')($);
	require('grid')($);
	require('../../../js/seaJsExtends/autoComplete.js')($);
	var IFA = require('util');
	require('../../../styles/cim/jquery-ui-1.8.14.custom.css');
	require('../../../styles/cim/cim-grid.css');
	require('../../../styles/cim/autoComplete.css');
	$(function(){
		hotKeyCompany();
		hotKeyDistrict();
//		search();
//		$('#searchBtn').click(function(){
//			if($('#cdpNo').val()==""){
//				$('#companyId').attr("value","");
//			}
//			search();
//		});
//		$("#searchForm :input").bind("keydown",function(e){
//			var e = e ? e : window.event;
//			if(e.keyCode==13){
//			   search();
//			}
//		});
	});
	
//	function search(){
//		var url = $('#searchUrl').val();
//		$('#OCRRList').grid({
//			url:url,
//			root:"openContractRevenueList",
//			pageTheme:'Theme1',
//			isREST :false,
//			method:"POST",
//			paging:false,
//			defaultParams:IFA.convertQueryStringToJson($("#searchForm").serialize()),
//			fields:[{name:i18n["Entity.report.seq"],type:'seq'},
//			        {name:i18n["Entity.report.rentalno"],id:"rentalNo",style:{width:'10%'}},
//			        {name:"CDP#",id:"cdpNo",style:{width:'10%'}},
//			        {name:i18n["Entity.report.companyname"],id:"companyName",style:{width:'10%'}},
//			        {name:i18n["Entity.report.districtname"],id:"districtName"},
//			        {name:i18n["Entity.report.pickupdate"],id:"pickupDate",style:{width:'10%'},render:function(data){
//	  				  	var timeCheck = /^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}$/;
//	  				  	if(!timeCheck.exec(data.pickupDate)){
//	  				  		 data.pickupDate = new Date(data.pickupDate).format("yyyy-MM-dd hh:mm");
//	  				  	}
//	  				  	return data.pickupDate;
//			        }},
//			        {name:i18n["Entity.report.returndate"],id:"returnDate",render:function(data){
//	  				  	var timeCheck = /^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}$/;
//	  				  	if(!timeCheck.exec(data.returnDate)){
//	  				  		 data.returnDate = new Date(data.returnDate).format("yyyy-MM-dd hh:mm");
//	  				  	}
//	  				  	return data.returnDate;
//			        }},
//			        {name:i18n["Entity.report.noshowfee"],id:"noShowFee"},
//			        {name:i18n["Entity.report.contractrevenue"],id:"openContractRevenue"},
//			        {name:i18n["Entity.report.totalincome"],id:"totalIncome"}
//			        ]
//		});
//	}
	$('#exportBtn').click(function(){
		var url = $('#exportUrl').val().trim();
		$('#searchForm').attr("action",url);
		$('#searchForm').submit();
//		$.ajax({
//			type:"POST",
//			url:url,
//			data:{companyId:$.trim($('#companyId').val()),districtId:$.trim($('#districtId').val()),endDate:$.trim($('#endDate').val())},
//			success:function(response,ioArgs){
//				
//			}
//		});
	});
    $("#endDate").datepicker({
	});
	function hotKeyCompany(){
	  	$.ajax({
			type:"get",
			url:$("#companyUrl").val(),
			success:function(response){
			$("#cdpNo").autocomplete(response.company,
				      {
						caption: "&nbsp;&nbsp;"+i18n["JS.districtHotKey.caption"],
						findCaption: "&nbsp;,"+i18n["JS.districtHotKey.findCaption"],
						noFindCaption: "&nbsp;&nbsp;"+i18n["JS.companyHotKey.noFindCaption"]+":&nbsp; ",
						max: 5,
						width: 200,
						formatItem: function(row, i, max) {
				             var cpdNo="cdpNo:"+row.cdpNo+"&#13;";
				             var companyName=i18n["JS.companyHotKey.coName"]+":"+row.companyName+"&#13;";
				             var companyHotkey=i18n["JS.companyHotKey"]+":"+row.companyHotkey+"&#13;";
				             var companyAddress=i18n["JS.companyHotKey.coAddr"]+":"+row.companyAddress+"&#13;";
				             var companyContact1st=i18n["JS.companyHotKey.coContact"]+":"+row.companyContact1st+"&#13;";
				             var title=cpdNo+companyName+companyHotkey+companyAddress+companyContact1st;
				             var companyName=row.companyName+"("+row.companyHotkey+")";
							return '<span title="{2}"><span class="ac_code" >{0}</span><span class="ac_Name">{1}</span></span>'.patchContent(row.cdpNo,companyName,title);
						},
						formatMatch: function(row, i, max) {
							return "{0} {1} {2}".patchContent(row.cdpNo,row.companyHotkey,row.companyName);
						},
						formatResult: function(row) {
							return "{0}".patchContent(row.cdpNo);
						}
					}).result(function(event, data, formatted) {
						$("#companyId").val(data.companyId);
					});
		}
		});
	}
	function hotKeyDistrict(){
	  	$.ajax({
			type:"get",
			url:$("#hotkeyDistrictUrl").val(),
			success:function(response){
			var acdata=response.district;
			$("#districtHokKey").autocomplete(acdata,
				      {
						caption: "&nbsp;&nbsp;"+i18n["JS.districtHotKey.caption"],
						findCaption: "&nbsp;,"+i18n["JS.districtHotKey.findCaption"],
						noFindCaption: "&nbsp;&nbsp;"+i18n["JS.districtHotKey.noFindCaption"]+":&nbsp; ",
						width: 200,
						max: 5,
						formatItem: function(row, i, max) {
							return '<span class="ac_code">{0}</span><span class="ac_Name">{1}</span>'.patchContent(row.districtHotkey, row.districtName);
						},
						formatMatch: function(row, i, max) {
							return "{0} {1}".patchContent(row.districtHotkey, row.districtName);
						},
						formatResult: function(row) {
							return "{0}".patchContent(row.districtName);
						}
					}).result(function(event, data, formatted) {
						//$(this).attr("code", data.districtId);
						$("#districtId").val(data.districtId);
						//$("#reservationForm").validate().element("#districtId");
					});
		}
		});
	}
});