define(function(require){
	var $ = require('jquery');
    require('uriTemplate')($);
    require('validation')($);
    require('dialog')($);
    require('util');
	require("../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../styles/cim/validation.css');
	require('../../../js/seaJsExtends/autoComplete.js')($);
	require('../../../styles/cim/autoComplete.css');
	$(function(){	
		//公司HotKey
		 hotKeyCompany();
		$('#billForm').validate({
			rules:{
				billingTitle:{
					required:true,
					maxlength:20
					//VerificationBillName:true
				},
				companyCdp:{
					required:true
				},
				billingDescription:{
					maxlength:50
				}
			}
		});
		
//      //账单标题唯一性验证
//		$.validator.addMethod("VerificationBillName:true",function(value,element,param){
//			var billingTitleVal = $('input[name=billingTitle]').val();
//			var flag = "";
//				var urlStr = $("#uniqueUrl").val();
//			    $.ajax({
//			    	type:"POST",
//			    	url:urlStr+".json",
//					async:false,
//			    	data:{"billingTitle":billingTitleVal},
//				    success: function(response,ioArgs){
//			   		    flag = response.status;
//				   	},error:function(response,ioArgs){
//				   		alert(ioArgs);
//				   	}
//			   	});
//			return flag;
//		},"账单标题不能重复");
	
		$("#saveBt").click(function(){
			if($('#billForm').valid()){	
				$("#billForm").submit();
			}else{
				$.msg.alert(i18n["Page.Dialog.content.inputError"]);
			}
			 return false;
		});
	});
	//公司热键
	function hotKeyCompany(){
	  	$.ajax({
			type:"get",
			url:$("#companyUrl").val(),
			success:function(response){
			$("#companyCdp").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);
						$("#companyName").val(data.companyName);
						//showContact(data.companyId);
					});
		}
		});
	}
		
	$("#addcontact").bind("click",function(){
		if($("#companyCdp").val()==""){
			$("#companyName").val("");
		}
		var companyName = $("#companyName").val();
	    var url = $('#addContactUrl').val();
	    var data = {"companyName":companyName};
		var personId = window.showModalDialog(url,data,"dialogWidth=1000px;dialogHeight=600px");
		showContact(personId);
	});
	
	function showContact(personId){
		var url = $('#showContactUrl').val();
		$.get(url,{"personId":personId},function(data){
			var personProfile = data.personProfile;
			$("#contactTips").remove();
			$("#personId").attr("value",personId);
			if(personProfile.cnName!=null&&personProfile.cnName!=""){				
				$("#contact1Info").text(i18ns("Entity.billing.contact")+"："+personProfile.en1stName+personProfile.enLstName+"("+personProfile.cnName+")");
			}else{
				$("#contact1Info").text(i18ns("Entity.billing.contact")+"："+personProfile.en1stName+personProfile.enLstName);
			}
			
			$("#contact1PhoneInfo").text(i18ns("Entity.dealer.tel")+"："+personProfile.mobileTelephone);
			$("#contact1EmailInfo").text("Email："+personProfile.email);
		});
	}
//	function showContact(companyId){
//		var url = $('#showContactUrl').val();
//		$.get(url,{"companyId":companyId},function(data){
//			var companyProfile = data.companyProfile;
//			$("#contact1").css("display","inline");
//			$("#contact1").val(companyProfile.companyContact1st);
//			$("#contact1Info").text("联系人1："+companyProfile.companyContact1st);
//			$("#contact1PhoneInfo").text("电话："+companyProfile.contact1stPhone);
//			$("#contact1EmailInfo").text("Email："+companyProfile.contact1stEmail);
//			if(companyProfile.companyContact2nd!=null&&companyProfile.companyContact2nd!=""){
//				$("#contact2").css("display","inline");
//				$("#contact2").val(companyProfile.companyContact2nd);
//				$("#contact2Info").text("联系人2："+companyProfile.companyContact2nd);
//				$("#contact2PhoneInfo").text("电话："+companyProfile.contact2ndPhone);
//				$("#contact2EmailInfo").text("Email："+companyProfile.contact2ndEmail);
//			}
//			if(companyProfile.companyContact3rd!=null&&companyProfile.companyContact3rd!=""){
//				$("#contact3").css("display","inline");
//				$("#contact3").val(companyProfile.companyContact3rd);
//				$("#contact3Info").text("联系人3："+companyProfile.companyContact3rd);
//				$("#contact2PhoneInfo").text("电话："+companyProfile.contact3rdPhone);
//				$("#contact2EmailInfo").text("Email："+companyProfile.contact3rdEmail);
//			}
//		});
//	}
});