autocomplete
var globalname;
var data = Array();
var loadflag=false;
$(document).ready(function () {
data.length = 0;
if ($("input[name*='txt_FirstName']").html() != null) {
globalname = 'txt_FirstName'
if(loadflag==false)
{
loadflag=true;
getdata("");
}
BindHandle();
}
});
function BindHandle()
{
if ($("input[name*='" + globalname + "']").html() != null) {
$("input[name*='" + globalname + "']").result(function (event, data, formatted) {
var id = formatted.substring(formatted.indexOf('<span style="display:none">') + 27, formatted.indexOf('</span>'));
$.ajax({
type: "POST",
contentType: "application/json",
url: "CHService.asmx/GetContactById",
dataType: 'json',
data: "{contactid:'" + id + "'}",
success: function (jsonStr) {
var result = eval('(' + jsonStr + ')');
if (result != null) {
if ($("input[name*='" + globalname + "']").html() != null) {
$("input[name*='" + globalname + "']").val(result.FirstName);
}
if ($("input[name*='" + globalname.replace('txt_FirstName', 'txt_LastName') + "']").html() != null) {
$("input[name*='" + globalname.replace('txt_FirstName', 'txt_LastName') + "']").val(result.LastName);
}
if ($("input[name*='" + globalname.replace('txt_FirstName', 'txt_Email') + "']").html() != null) {
$("input[name*='" + globalname.replace('txt_FirstName', 'txt_Email') + "']").val(result.Email);
}
if ($("input[name*='" + globalname.replace('txt_FirstName', 'txt_Compnay') + "']").html() != null) {
$("input[name*='" + globalname.replace('txt_FirstName', 'txt_Compnay') + "']").val(result.Company);
}
if ($("input[name*='" + globalname.replace('txt_FirstName', 'txt_PhoneNumber1') + "']").html() != null) {
$("input[name*='" + globalname.replace('txt_FirstName', 'txt_PhoneNumber1') + "']").val(result.Phone1);
}
if ($("input[name*='" + globalname.replace('txt_FirstName', 'txt_PhoneNumber2') + "']").html() != null) {
$("input[name*='" + globalname.replace('txt_FirstName', 'txt_PhoneNumber2') + "']").val(result.Phone2);
}
if ($("input[name*='" + globalname.replace('txt_FirstName', 'txt_Address1') + "']").html() != null) {
$("input[name*='" + globalname.replace('txt_FirstName', 'txt_Address1') + "']").val(result.Address1);
}
if ($("input[name*='" + globalname.replace('txt_FirstName', 'txt_Address2') + "']").html() != null) {
$("input[name*='" + globalname.replace('txt_FirstName', 'txt_Address2') + "']").val(result.Address2);
}
if ($("input[name*='" + globalname.replace('txt_FirstName', 'txt_City') + "']").html() != null) {
$("input[name*='" + globalname.replace('txt_FirstName', 'txt_City') + "']").val(result.City);
}
if ($("input[name*='" + globalname.replace('txt_FirstName', 'txt_ZipCode') + "']").html() != null) {
$("input[name*='" + globalname.replace('txt_FirstName', 'txt_ZipCode') + "']").val(result.PostalCode);
}
if ($("input[name*='" + globalname.replace('txt_FirstName', 'txtBDate') + "']").html() != null) {
if (result.BirthDate != "") {
$("input[name*='" + globalname.replace('txt_FirstName', 'txtBDate') + "']").val(result.BirthDate);
}
else {
$("input[name*='" + globalname.replace('txt_FirstName', 'txtBDate') + "']").val("");
}
}
if ($("select[name*='" + globalname.replace('txt_FirstName', 'ddl_State') + "']").html() != null) {
if (result.State == "") {
$("select[name*='" + globalname.replace('txt_FirstName', 'ddl_State') + "']")[0].options(0).selected = true;
}
else {
$("select[name*='" + globalname.replace('txt_FirstName', 'ddl_State') + "']").val(result.State);
}
}
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
}
});
});
}
}
function getSource(obj) {
globalname = obj.name;
if(loadflag==false)
{
loadflag=true;
getdata("");
}
}
function getdata(id) {
if(data.length == 0)
{
$.ajax({
type: "POST",
contentType: "application/json",
url: "CHService.asmx/GetAutoCompleteContacts",
dataType: 'json',
data: "{firstname:'" + id + "'}",
success: function (jsonStr) {
var result = eval('(' + jsonStr + ')');
$.each(result, function (i, item) {
var s = item.FirstName + " " + item.LastName + " " + item.Address1 + " " + item.Email + '<span style="display:none">' + item.ContactId + '</span>';
data.push(s);
}
)
$("input[name*='" + globalname + "']").autocomplete(data, { scroll: true, scrollHeight: 300, width: 400,cacheLength:1 });
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
loadflag=false;
}
});
}
}