/******************************************************************************
* Global variables required to setup the customer field. You have to replace
* the values of AccountFieldName and ContactFieldName when using attribute
* names different than the one used in this sample.
*****************************************************************************/
AccountFieldName = "hud_accountid";
ContactFieldName = "hud_contactid";
CustomerFieldName = "hud_customerid";
CreatedByName = "createdby";
AccountLookup = crmForm.all.item(AccountFieldName);
ContactLookup = crmForm.all.item(ContactFieldName);
CreatedByLookup = crmForm.all.item(CreatedByName);
CustomerLookup = null;
CreatedByInnerHTML = null;
AllFieldsAvailable = (AccountLookup != null) && (ContactLookup != null);
/******************************************************************************
* GetLookupFieldHtml builds the inner HTML of a lookup control. Used to
* dynamically create the customer lookup.
*****************************************************************************/
function GetLookupFieldHtml(name, tabIndex, lookupTypes, lookupTypeNames, lookupTypeIcons, reqLevel) {
var html = "<table class=\"ms-crm-Lookup\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" style=\"table-layout:fixed;\">" +
"<tr>" +
"<td>" +
"<div ime-mode=\"auto\" class=\"ms-crm-Lookup\" tabindex=\"" + (parseInt(tabIndex) + 1) + "\"></div>" +
"<label class=\"ms-crm-Hidden-NoBehavior\" for=\"" + name + "_ledit\"></label>" +
"<input class=\"ms-crm-Hidden-NoBehavior\" ime-mode=\"auto\" type=\"text\" tabindex=\"" + tabIndex + "\" id=\"" + name + "_ledit\" maxlength=\"1000\"/>" +
"</td>" +
"<td width=\"25\" class=\"Lookup_RenderButton_td\">" +
"<img src=\"/_imgs/btn_off_lookup.gif\" id=\"" + name + "\" class=\"ms-crm-Lookup\" req=\"" + reqLevel + "\" style=\"ime-mode:auto\" lookuptypes=\"" + lookupTypes + "\" lookuptypenames=\"" + lookupTypeNames + "\" lookuptypeIcons=\"" + lookupTypeIcons + "\" lookupclass=\"BasicCustomer\" lookupbrowse=\"0\" lookupstyle=\"single\" defaulttype=\"0\" autoresolve=\"1\" showproperty=\"1\" resolveemailaddress=\"0\">" +
"<a href=\"#\" onclick=\"previousSibling.click();\" tabindex=\"-1\"></a>" +
"</td>" +
"</tr>" +
"</table>";
return html;
}
/******************************************************************************
* A sample event handler. As the customer lookup does not exist on the form,
* you cannot add an OnChange event handler in the CRM client. This method is
* used as an alternative.
*****************************************************************************/
function Customer_OnChange() { alert("Customer changed"); }
/******************************************************************************
* Helper function. Retrieves a field by its name.
*****************************************************************************/
GetField = function(name) { return crmForm.all.item(name);
}
/******************************************************************************
* Helper function. Retrieves the id of the TD element hosting a lookup control.
*****************************************************************************/
GetLookupCellId = function(name) { return name + "_d";
}
/******************************************************************************
* Helper function. Retrieves the TD element hosting a lookup control.
*****************************************************************************/
GetLookupCell = function(name) { return GetField(GetLookupCellId(name));
}
/******************************************************************************
* ExchangeLookups replaces an existing lookup control with another one. Used
* to replace the account lookup with the customer lookup and vice versa.
*****************************************************************************/
ExchangeLookups = function(lookupToShowId, lookupToHideId, newLookupHtml) { var prevCell = GetLookupCell(lookupToHideId);
var prevCellIndex = prevCell.cellIndex;
var row = prevCell.parentNode;
row.deleteCell(prevCellIndex);
var newCell = row.insertCell(prevCellIndex);
newCell.id = GetLookupCellId(lookupToShowId);
newCell.innerHTML = newLookupHtml;
}
/******************************************************************************
* CreateCustomerField creates the customer lookup and replaces the account
* lookup on the form.
*****************************************************************************/
function CreateCustomerField() { var customerValue;
if (AccountLookup.DataValue != null) { customerValue = AccountLookup.DataValue;
}
else { customerValue = ContactLookup.DataValue;
}
var accountLookupEditField = GetField(AccountFieldName + "_ledit");
var tabIndex;
//If the account field supports the auto-resolve feature, then the tab-index is stored on the text field
//of the lookup control
if (accountLookupEditField == null) { tabIndex = AccountLookup.getAttribute("tabIndex"); }
//otherwise we use the same tabIndex as in the 3.0 implementation.
else { tabIndex = accountLookupEditField.getAttribute("tabIndex"); }
//Standard values for a customer field
var lookupTypes = "1,2";
var lookupTypeNames = "account:1,contact:2";
var lookupTypeIcons = "/_imgs/ico_16_1.gif:/_imgs/ico_16_2.gif";
var reqLevel = AccountLookup.getAttribute("req"); var customerIdHtml = GetLookupFieldHtml(CustomerFieldName, tabIndex, lookupTypes, lookupTypeNames, lookupTypeIcons, reqLevel);
// ExchangeLookups(CustomerFieldName, AccountFieldName, customerIdHtml);
ExchangeLookups(CustomerFieldName, CreatedByName, customerIdHtml);
CustomerLookup = GetField(CustomerFieldName);
CustomerLookup.DataValue = customerValue;
// Here we are able to attach events to the customer field
// CustomerLookup.onchange = Customer_OnChange;
}
/******************************************************************************
* Main code: If the required fields are avaialable on the form, the account
* lookup is replaced with a customer lookup. The CreatedByInnerHTML is saved to
* restore the original state later (see OnSave).
*****************************************************************************/
if (AllFieldsAvailable) { CreatedByInnerHTML = GetLookupCell(CreatedByName).innerHTML;
CreateCustomerField();
}