Type.registerNamespace("USD.AuditAndDiagnostics");

var enableCacheAttrib = Xrm.Page.getAttribute("msdyusd_EnableCaching");
var cacheSizeAttrib = Xrm.Page.getAttribute("msdyusd_CacheSize");
var auditCacheDefaultSize = 10;
function PageOnLoad() {
	// For first load call the handler explicitly.
	EnableCachingOnChangeHandler();

    if (Xrm.Page.ui.getFormType() == 1) { //If form is in Create mode.
        OnActivityTrackingEnableDisable();
        OnDiagnosticsEnableDisable();
    }
}
	
function OnActivityTrackingEnableDisable() {
    if (getSelectedOptionValue("msdyusd_ATEnabled") === 1) {
        ResetValuesForActivityTracking(true);
        EnableDisableActivityTracking(false);
    }
    else {
        ResetValuesForActivityTracking(false);
        EnableDisableActivityTracking(true);
    }
	// Need to call it explicity since attribute.setValue doesn't fire onChange if attribute is changed via ClientAPI!
	if (Xrm.Page.ui.getFormType() != 1) {
		EnableCachingOnChangeHandler();
	}
}

function ResetValuesForActivityTracking(checked) {
    setAttributeValue("msdyusd_ATforAgentScripts", checked);
    setAttributeValue("msdyusd_ATforHostedControl", checked);
    setAttributeValue("msdyusd_ATforSubActionCalls", checked);
    setAttributeValue("msdyusd_ATforActionCalls", checked);
    setAttributeValue("msdyusd_ATforWindowsNavRules", checked);
    setAttributeValue("msdyusd_ATforEvents", checked);
    setAttributeValue("msdyusd_ATforAgentLogin", checked);
    setAttributeValue("msdyusd_ATforCustomerSession", checked);
    setAttributeValue("msdyusd_ATforUIIAction", checked);
    setAttributeValue("msdyusd_EnableCaching", checked);
}

function EnableDisableActivityTracking(disabled) {
    setDisabledProperty("msdyusd_ATforAgentScripts", disabled);
    setDisabledProperty("msdyusd_ATforHostedControl", disabled);
    setDisabledProperty("msdyusd_ATforSubActionCalls", disabled);
    setDisabledProperty("msdyusd_ATforActionCalls", disabled);
    setDisabledProperty("msdyusd_ATforWindowsNavRules", disabled);
    setDisabledProperty("msdyusd_ATforEvents", disabled);
    setDisabledProperty("msdyusd_ATforAgentLogin", disabled);
    setDisabledProperty("msdyusd_ATforCustomerSession", disabled);
    setDisabledProperty("msdyusd_ATforUIIAction", disabled);
    setDisabledProperty("msdyusd_EnableCaching", disabled);
}

function OnDiagnosticsEnableDisable() {
    if (getSelectedOptionValue("msdyusd_DGTEnabled") === 1) {
        setDisabledProperty("msdyusd_DGTVerbosityLevel", false);
        setAttributeValue("msdyusd_DGTVerbosityLevel", 100000000);
    }
    else {
        setDisabledProperty("msdyusd_DGTVerbosityLevel", true);
        setAttributeValue("msdyusd_DGTVerbosityLevel", null);
    }
}

//helper function
function setDisabledProperty(elementName, elementValue) {
    if (Xrm.Page.getControl(elementName) != null) {
        Xrm.Page.getControl(elementName).setDisabled(elementValue);
    }
}

function setAttributeValue(elementName, elementValue) {
    if (Xrm.Page.getAttribute(elementName) != null) {
        Xrm.Page.getAttribute(elementName).setValue(elementValue);
        Xrm.Page.getAttribute(elementName).setSubmitMode("always");
    }
}

function getSelectedOptionValue(elementName) {
    var selected = Xrm.Page.getAttribute(elementName).getSelectedOption();
    return selected.value;
}
function EnableCachingOnChangeHandler() {
    // If the value of cache size was set earlier use it else put a default value.
    if (cacheSizeAttrib != null && cacheSizeAttrib.getValue() == null) {
        cacheSizeAttrib.setValue(auditCacheDefaultSize);
    }
    setDisabledProperty("msdyusd_CacheSize", !enableCacheAttrib.getValue());
}
