/* Enable/Disable form fields based on Payment Type */
function set_paymenttype()
{
switch (crmForm.all.new_paymenttype_optionset.DataValue)
{
case "100000000" /* Lease */:
trigger_deal_economics(true);
trigger_lease_economics(true);
trigger_thirdpartyfinancing(false);
crmForm.all.new_feevolume.disabled=true;
break;
case "100000001" /* Sale */:
trigger_deal_economics(true);
trigger_lease_economics(false);
trigger_thirdpartyfinancing(false);
crmForm.all.new_feevolume.disabled=true;
break;
case "100000002" /* Direct Lease-3rd Party */:
trigger_deal_economics(true);
trigger_lease_economics(false);
trigger_thirdpartyfinancing(true);
crmForm.all.new_feevolume.disabled=true;
break;
case "100000003" /* Fee-Third Party Financing */:
trigger_deal_economics(false);
trigger_lease_economics(false);
trigger_thirdpartyfinancing(false);
crmForm.all.new_salerevenue.disabled=false;
crmForm.all.new_feevolume.disabled=false;
break;
default:
trigger_deal_economics(false);
trigger_lease_economics(false);
trigger_thirdpartyfinancing(false);
crmForm.all.new_feevolume.disabled=true;
}
}
function trigger_thirdpartyfinancing(valid)
{
if (valid==true)
{
crmForm.all.new_thirdpartyleaseno.disabled=false;
crmForm.all.new_thirdpartyleaseterm.disabled=false;
crmForm.all.new_nbvremarketed.disabled=false;
}
else
{
crmForm.all.new_thirdpartyleaseno.disabled=true;
crmForm.all.new_thirdpartyleaseterm.disabled=true;
}
}
function trigger_lease_economics(valid)
{
if (valid==true)
{
crmForm.all.new_leasepymt.disabled=false;
crmForm.all.new_leasepymtother.disabled=false;
crmForm.all.new_leaseterm.disabled=false;
crmForm.all.new_newcapx.disabled=false;
crmForm.all.new_nbvremarketed.disabled=false;
crmForm.all.new_finsoftcosts.disabled=false;
}
else
{
crmForm.all.new_leasepymt.disabled=true;
crmForm.all.new_leasepymtother.disabled=true;
crmForm.all.new_leaseterm.disabled=true;
crmForm.all.new_newcapx.disabled=true;
crmForm.all.new_nbvremarketed.disabled=true;
crmForm.all.new_finsoftcosts.disabled=true;
}
}
function trigger_deal_economics(valid)
{
if (valid==true)
{
crmForm.all.new_salerevenue.disabled=false;
crmForm.all.new_estcosts.disabled=false;
}
else
{
crmForm.all.new_salerevenue.disabled=true;
crmForm.all.new_estcosts.disabled=true;
}
}
function Form_onsave()
{
if (crmForm.all.new_totalleasepayment.DataValue!=null) {
crmForm.all.new_totalleasepayment.ForceSubmit = true;
}
if (crmForm.all.new_estprofit.DataValue!=null) {
crmForm.all.new_estprofit.ForceSubmit = true;
}
if (crmForm.all.new_estprofitmargin.DataValue!=null) {
crmForm.all.new_estprofitmargin.ForceSubmit = true;
}
}
function new_leadsource_onchange()
{
crmSDKSample.filterChildField("new_leadsource", "new_leadsourcedetail");
}
function new_profit_onchange()
{
var field1 = crmForm.all.new_salerevenue.DataValue;
field1 = (field1==null ? 0 : field1);
var field2 = crmForm.all.new_estcosts.DataValue;
field2 = (field2==null ? 0 : field2);
crmForm.all.new_estprofit.DataValue = field1 - field2;
if (field1 != 0)
{
crmForm.all.new_estprofitmargin.DataValue = (field1-field2)/field1;
}
else
{
crmForm.all.new_estprofitmargin.DataValue = 0;
}
}
function new_payment_onchange()
{
var field1 = crmForm.all.new_leasepymt.DataValue;
field1 = (field1==null ? 0 : field1);
var field2 = crmForm.all.new_leasepymtother.DataValue;
field2 = (field2==null ? 0 : field2);
crmForm.all.new_totalleasepayment.DataValue = field1 + field2;
}
function Form_onload()
{
set_paymenttype();
var CRM_FORM_TYPE_CREATE = 1;
var CRM_FORM_TYPE_UPDATE = 2;
/* Only enable the dependent picklist on a Create or Update form.
Disabled and read-only forms are not editable and so do not require dependent picklists. */
switch (crmForm.FormType)
{
case CRM_FORM_TYPE_CREATE:
case CRM_FORM_TYPE_UPDATE:
var crmSDKSample = new Object();
/* Map the dependencies START*/
/*
This sample maps 1 dependent picklist for this form.
* new_leadsource > new_leadsourcedetail
*/
var gArrDependentPicklists =
[{
"ParentFieldId": "new_leadsource",
"ChildFieldId": "new_leadsourcedetail",
"ChildFieldLabel": "Lead Source Detail",
"OptionsGroup":
[
{"ParentValue": "200001", /* Trade Show */
"ChildOptionValues": ["", "1001", /* ACA */
"1002", /* TASBO */
"1003", /* CASH */
"1004", /* CLCS */
"1005", /* CACSC */
"1006", /* GASCA */
"1007", /* MASBO */
"1008", /* NJCSC */
"1009", /* SCAPCS */
"1010", /* Region 5 */
"1011", /* OSFMA */
"1012", /* AACC */
"1013", /* CTASBO */
"1014", /* COASBO */
"1015", /* WASBO */
"1016", /* UAPCS */
"1017", /* FADSS */
"1018", /* FSBA */
"1019", /* SCASC */
"1020", /* NCSC */
"1021", /* NSA */
"1022", /* OKSCL */
"1023", /* FEPPA */
"1024", /* HFSE */
"1025", /* FSPMA */
"1026", /* LACSA */
"1027", /* GSNC */
"1028", /* IFMA */
"1029", /* LANIGP */
"1030", /* NYSCA */
"1031", /* ILASB */
"1032", /* OSBA */
"1033", /* AZCSA */
"1034", /* FCSC */
"1035", /* TACNIGP */
"1036", /* OCCA */
"1037", /* TASA-TASB */
"1038", /* FEFPA */
"1039", /* Green CA Schools Summit */
"1040", /* CA Charter School Show */
"1041", /* CASBO */
"100001042", /* CEFPI */
"100001043", /* GSA EXPO */
"100001044", /* NCAPCS */
"100001045", /* NCPSMA */
"100001046" /* NE Build */ ]
},
{"ParentValue": "200002", /* Referral */
"ChildOptionValues": ["", "2001", /* Manufacturer */
"2002", /* Cooperative Purchasing */
"2003", /* Vendor */
"2004", /* Customer */
"2005" /* MBI */ ]
},
{"ParentValue": "200003", /* Internet-Website */
"ChildOptionValues": ["", "3001", /* Search Engine */
"3002", /* Link */
"3003", /* Click Thru */
"3004", /* Hubspot Form */
"3005", /* Request A Quote */
"3006", /* Education Presentation */
"3007", /* Corrections Presentation */
"3008", /* Healthcare Presentation */
"3009", /* P.IQ Presentation */
"3010", /* Green Resource Library */
"3011" /* Web Sessions */ ]
},
{"ParentValue": "200004", /* Printed Materials */
"ChildOptionValues": ["", "4001", /* Brochure */
"4002", /* Flyer */
"4004", /* Postcard */
"4005" /* Advertisement */ ]
},
{"ParentValue": "200005", /* Bid-Lead Services */
"ChildOptionValues": ["", "5001", /* Govdirections */
"5002", /* RFP Schoolwatch */
"5003", /* 360 Mobile */
"5004", /* Bid Clerk */
"5005", /* FindRfp */
"5006", /* CDC News */
"5007", /* FBO.gov */
"5008", /* Public Notice Ads */
"5009", /* Reed */
"5010", /* Mypublicnotices */
"5011", /* Bid Ocean */
"5012", /* NECO */
"5013", /* RFP Depot */
"5014", /* Onvia */
"5015", /* Ebuy */
"100005016" /* Central Bidding */ ]
},
{"ParentValue": "200008", /* Building Decal */
"ChildOptionValues": [""]
},
{"ParentValue": "200009", /* Other */
"ChildOptionValues": ["", "9001" /* 800 Call */]
},
{"ParentValue": "200010", /* News Article-Business Development Team */
"ChildOptionValues": [""]
},
{"ParentValue": "200011", /* Repeat Customer */
"ChildOptionValues": [""]
},
{"ParentValue": "200012", /* Email Blast */
"ChildOptionValues": [""]
},
{"ParentValue": "200013", /* Third Party */
"ChildOptionValues": ["", "100001301", /* Dealer Calls */
"100001302", /* New Dealer */
"100001303" /* Repeat Dealer */ ]
},
{"ParentValue": "100000000", /* Marketing Blitz Phone Calls */
"ChildOptionValues": [""]
}
]
}
]; /* Map the dependancies END*/
/* Add Child Field Options to the global dependancy array START*/
for (var depPickList in gArrDependentPicklists)
{
var DependentPicklist = gArrDependentPicklists[depPickList];
/*Get a reference to the child field*/
var ChildField = crmForm.all[DependentPicklist.ChildFieldId];
/* Use ExpectedChildOptions to compare the actual child field options to the
mapped child field options */
var ExpectedChildOptions = 1; //The empty default field.
for (var og in DependentPicklist.OptionsGroup)
{
var OptionsGroup = DependentPicklist.OptionsGroup[og];
/* Initialize an array to hold valid options */
var aOptionGroup = new Array();
/* Loop through the mapped options and compare to valid options.
If the options match, add the option to aOptionGroup array. */
for (var value in OptionsGroup.ChildOptionValues)
{
for (var opt in ChildField.Options)
{
if (OptionsGroup.ChildOptionValues[value] == ChildField.Options[opt].DataValue)
{
aOptionGroup.push(ChildField.Options[opt]);
}
}
/* Count the number of mapped child options */
if (OptionsGroup.ChildOptionValues[value] != "")
{ ExpectedChildOptions++ }
}
/*Add the valid options to the gArrDependentPicklists[depPickList].OptionsGroup[og].Options */
OptionsGroup.Options = aOptionGroup;
}
/*Display a message if the expected options do not match the available options.*/
if (ChildField.Options.length != ExpectedChildOptions)
{
var sUnavailableOptionsErrorMessage = "Some " + DependentPicklist.ChildFieldLabel + " options are not being displayed.\n";
sUnavailableOptionsErrorMessage += "The form Onload event script is not synchronized \n";
sUnavailableOptionsErrorMessage += "for the current " + DependentPicklist.ChildFieldLabel + " options.";
//sUnavailableOptionsErrorMessage += "Expected: " + ExpectedChildOptions + " Counted: " + ChildField.Options.length;
alert(sUnavailableOptionsErrorMessage);
}
}
/* Add Child Field Options to the global dependancy array END*/
/*Attach the global dependancy array to the crmSdkSample object */
crmSDKSample.gArrDependentPicklists = gArrDependentPicklists;
/* Define a function that can be called from the Parent field OnChange Event START*/
var filterChildFieldFunction = function filterChildField(paramParentFieldId, paramChildFieldId)
{
for (var depPickList in crmSDKSample.gArrDependentPicklists)
{
var DependentPicklist = crmSDKSample.gArrDependentPicklists[depPickList];
/* Match the parameters to the correct dependent picklist mapping*/
if ((DependentPicklist.ParentFieldId == paramParentFieldId) && (DependentPicklist.ChildFieldId == paramChildFieldId))
{
/* Get references to the related fields*/
var ParentField = crmForm.all[paramParentFieldId];
var ChildField = crmForm.all[DependentPicklist.ChildFieldId];
/* Capture the current value of the child field*/
var CurrentChildFieldValue = ChildField.DataValue;
/* If the parent field is null the Child field can be set to null */
if (ParentField.DataValue == null)
{
ChildField.DataValue = null;
ChildField.ForceSubmit = true;
ChildField.FireOnChange();
ChildField.Disabled = true;
return;
}
for (var og in DependentPicklist.OptionsGroup)
{
var OptionsGroup = DependentPicklist.OptionsGroup[og];
/* Find the Options group that corresponds to the value of the parent field. */
if (ParentField.DataValue == OptionsGroup.ParentValue)
{
/*Enable the field and set the options*/
ChildField.Disabled = false;
ChildField.Options = OptionsGroup.Options;
/*Check whether the current value is valid*/
var bCurrentValueIsValid = false;
for (var validOptionIndex in ChildField.Options)
{
var OptionDataValue = ChildField.Options[validOptionIndex].DataValue;
if (CurrentChildFieldValue == OptionDataValue)
{
bCurrentValueIsValid = true;
break;
}
}
/*
If the value is valid, set it.
If not, set the child field to null
*/
if (bCurrentValueIsValid)
{
ChildField.DataValue = CurrentChildFieldValue;
}
else
{
ChildField.DataValue = null;
ChildField.ForceSubmit = true;
ChildField.FireOnChange();
}
break;
}
}
}
}
}
/* Define a function that can be called from the Parent field OnChange Event END*/
/*Attach the function to the crmSDKSample*/
crmSDKSample.filterChildField = filterChildFieldFunction;
/*Attach the crmSDKSample to the window so it is globally available*/
window.crmSDKSample = crmSDKSample;
/*Call the OnChange event for any Parent Fields*/
for (var depPickList in crmSDKSample.gArrDependentPicklists)
{
var DependentPicklist = crmSDKSample.gArrDependentPicklists[depPickList];
crmForm.all[DependentPicklist.ParentFieldId].FireOnChange();
}
break;
} // END switch (crmForm.FormType)
}