define(function(require){
	var $ = require('jquery');
	require('uriTemplate')($);
	require('dialog')($);
	require('datepicker')($);
	require('../../../js/seaJsExtends/autoComplete.js')($);
	var IFA = require('util');
	require('../../../styles/cim/jquery-ui-1.8.14.custom.css');
	require('../../../styles/cim/autoComplete.css');
	$(function(){
		hotKeyCompany();
		$('#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();
		$("#searchForm").attr("action",url);
		$("#searchForm").submit();
	}
	$('#exportBtn').click(function(){
		var url = $('#exportUrl').val().trim();
		$('#searchForm').attr("action",url);
		$('#searchForm').submit();
	});
    $("#startDate").datepicker({
    	onSelect:function(date){    	
    		var date=date.split("-").join("/");
    		$("#endDate").datepicker("option","minDate",new Date(date));
    		//CalculateRentalDays();
    	}
	});
    $("#endDate").datepicker({
    	onSelect:function(date){    	
    		var date=date.split("-").join("/");
    		$("#startDate").datepicker("option","maxDate",new Date(date));
    		//CalculateRentalDays();
    	}
	});
	function hotKeyCompany(){
	  	$.ajax({
			type:"get",
			url:$("#companyUrl").val(),
			success:function(response){
			$("#cdpNo").autocomplete(response.company,
				      {
						caption: "&nbsp;&nbsp;"+i18ns("JS.districtHotKey.caption"),
						findCaption: "&nbsp;,"+i18ns("JS.districtHotKey.findCaption"),
						noFindCaption: "&nbsp;&nbsp;"+i18ns("JS.companyHotKey.noFindCaption")+":&nbsp; ",
						max: 5,
						width: 200,
						formatItem: function(row, i, max) {
				             var cpdNo="cdpNo:"+row.cdpNo+"&#13;";
				             var companyName=i18ns("JS.companyHotKey.coName")+":"+row.companyName+"&#13;";
				             var companyHotkey=i18ns("JS.companyHotKey")+":"+row.companyHotkey+"&#13;";
				             var companyAddress=i18ns("JS.companyHotKey.coAddr")+":"+row.companyAddress+"&#13;";
				             var companyContact1st=i18ns("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 CalculateRentalDays(){
//		var returnDate = $('#endDate').val();
//		var pickDate =  $('#startDate').val();
//		if(returnDate==""||pickDate==""){
//			$('#rentalDays').attr('value',"");
//			alert($('#rentalDays').val());
//		}else{
//			var rentalDays = getDaysBetween(pickDate,returnDate);
//			alert(rentalDays);
//		}
//	}
//	
//    function getDaysBetween(startDate,endDate) {
//    	var startDate = startDate.split("-");
//    	var startDate = new Date(startDate[1],startDate[2],startDate[0],0,0,0,0);
//    	var endDate  = endDate.split("-");
//    	var endDate  = new Date(endDate[1],endDate[2],endDate[0],0,0,0,0);
//    	var date  = Math.floor((endDate - startDate)/(1000*24*3600*30));
//    	return date+1; 
//    }
});