var _usedOutboundCallScript = false;

function form_onLoad() {
	showContactInfo();
	populateDate();
	var callBackNumberAttr = Xrm.Page.getAttribute("ftp_callbacknumber");
	if (!!callBackNumberAttr) {
		//fires formatTelephoneNumber()
		callBackNumberAttr.fireOnChange();
	}
	
	// only set default direction on create form
	if (Xrm.Page.ui.getFormType() == 1) {
		setInteractionDirectionByTeamMembership();
		Xrm.Page.getAttribute("tri_firstcallinteraction").fireOnChange();
	}

	getAnyMissingDataFromVeteran();
	refreshLinker();
	lockdownFormForSubsequentUSDSessions();
}

function lockdownFormForSubsequentUSDSessions() {
	/*
	the ftp_initialusdsessioncomplete field is updated by a USD action call when users closes a session after saving the interaction for the first time
	 */
	var attr = Xrm.Page.getAttribute("ftp_initialusdsessioncomplete");
	if (!!attr) {
		var firstSessionComplete = attr.getValue();
		if (firstSessionComplete == true) {
			var contrs = Xrm.Page.ui.controls.get();
			for (var i in contrs) {
				try {
					contrs[i].setDisabled(true);
					contrs[i].clearNotification()
				} catch (e) {}
			}
		}
	}
}

function showContactInfo() {
	//only show ftp_contactname if ftp_interactwith != Veteran (100000000)
	var interactedWithValue = Xrm.Page.getAttribute("ftp_interactedwith").getValue();
	var notAVeteran = interactedWithValue != 100000000;
	var con = Xrm.Page.getControl("ftp_contactname");
	if (!!con) {
		con.setVisible(notAVeteran);
		con.getAttribute().setRequiredLevel(notAVeteran ? "required" : "none");
	}
}

function populateDate() {
	//fill ftp_interactedon if it's empty
	var currentValue = Xrm.Page.getAttribute("ftp_interactedon").getValue();
	if (!currentValue)
		Xrm.Page.getAttribute("ftp_interactedon").setValue(new Date());
}

function setInteractionDirectionByTeamMembership() {
	//true = outbound
	//false = inbound
	retrieveTeamsForUser(
		function (retrievedRecords) {
		for (var i = 0, l = retrievedRecords.length; i < l; i++) {
			if (retrievedRecords[i].hasOwnProperty("Name")) {
				var thisTeamName = retrievedRecords[i].Name;
				//INBOUND
				if (thisTeamName == "CCA Team" || thisTeamName == "TAN" || thisTeamName == "Pharmacy") {
					writeToConsole("User is member of " + thisTeamName + ".  Direction: Inbound");
					Xrm.Page.getAttribute("ftp_direction").setValue(false);
					break;
				}
				//OUTBOUND
				if (thisTeamName == "PACT User") {
					writeToConsole("User is member of " + thisTeamName + ".  Direction: Outbound");
					Xrm.Page.getAttribute("ftp_direction").setValue(true);
					break;
				}
			}
		}
	});
}

function refreshLinker() {
	if (Xrm.Page.ui.getFormType() > 1) {
		var rlinker = Xrm.Page.getControl("WebResource_RequestLinker");
		rlinker.setSrc(rlinker.getSrc());

		//blocked by kknab 9/19/16
		/*
		if (location.href.indexOf("Outlook15White") != -1) {
		//USD Session
		window.open(("http://event/?eventname=Save Interaction Id&id=" + Xrm.Page.data.entity.getId() + ""));
		}
		 */
	}
}

function getAnyMissingDataFromVeteran() {
    if (Xrm.Page.ui.getFormType() == 1) {
        /*
		Phone numbers are filled initially by url parameters from the Veteran Alerts control's "Saved" USD event.
		But if we are missing any phone numbers when this form loads for the first time, check the veteran record to see if we can fill in any missing data that ESR did not provide.
		*/
        var mappedFieldList = [
			{ contactField: "ftp_HomePhone", interactionField: "ftp_homephone" },
			{ contactField: "ftp_WorkPhone", interactionField: "ftp_businessphone" },
			{ contactField: "ftp_MobilePhone", interactionField: "ftp_mobilephone" }
        ];

        for (var i = mappedFieldList.length - 1; i > -1; i--) {
            var attr = Xrm.Page.getAttribute(mappedFieldList[i].interactionField);
            if (!attr || (!!attr && !!attr.getValue())) {
                mappedFieldList.splice(i, 1);
            }
        }

        if (mappedFieldList.length > 0) {
            var veteranValue = Xrm.Page.getAttribute("ftp_veteran").getValue();
            if (!!veteranValue) {
                var queryString = "";
                for (var i = 0; i < mappedFieldList.length; i++) { queryString += (i == 0 ? mappedFieldList[i].contactField : ("," + mappedFieldList[i].contactField)); }
                SDK.REST.retrieveRecord(
					veteranValue[0].id,
					"Contact",
					queryString,
					null,
					function (retrievedContact) {
					    if (!!retrievedContact) {
					        for (var i = 0; i < mappedFieldList.length; i++) {
					            var thisPhoneNumber = mappedFieldList[i];
					            if (retrievedContact.hasOwnProperty(thisPhoneNumber.contactField) && !!retrievedContact[thisPhoneNumber.contactField]) {
					                Xrm.Page.getAttribute(thisPhoneNumber.interactionField).setValue(retrievedContact[thisPhoneNumber.contactField]);
					                Xrm.Page.getAttribute(thisPhoneNumber.interactionField).setSubmitMode("always");
					            }
					        }
					    }
					},
					errorHandler
				);
            }
        }
    }
}

function usdSave() {
	var ftype = Xrm.Page.ui.getFormType();
	if (ftype === 1) {
		/*
		interactiontype
		reasonforrequestid
		callbacknumber
		lastNameChecked
		SSN
		DOB;
		 */
		debugger;
		Xrm.Page.getAttribute("tri_interactiontype").setRequiredLevel("none");
		Xrm.Page.getAttribute("tri_reasonforrequest").setRequiredLevel("none");
		Xrm.Page.getAttribute("ftp_callbacknumber").setRequiredLevel("none");
		Xrm.Page.getAttribute("ftp_lastname").setRequiredLevel("none");
		Xrm.Page.getAttribute("ftp_ssn").setRequiredLevel("none");
		Xrm.Page.getAttribute("ftp_dob").setRequiredLevel("none");
		Xrm.Page.data.entity.save();
	} else {
		alert("ID eq" + Xrm.Page.data.entity.getId());
		Xrm.Page.getAttribute("tri_interactiontype").setRequiredLevel("required");
		Xrm.Page.getAttribute("tri_reasonforrequest").setRequiredLevel("required");
		Xrm.Page.getAttribute("ftp_callbacknumber").setRequiredLevel("required");
		Xrm.Page.getAttribute("ftp_lastname").setRequiredLevel("required");
		Xrm.Page.getAttribute("ftp_ssn").setRequiredLevel("required");
		Xrm.Page.getAttribute("ftp_dob").setRequiredLevel("required");
	}

}

function Mask(field, format) {
	//deprecated in favor of formatTelephoneNumber
	return;
	var thisControl = Xrm.Page.getControl(field);
	if (!!thisControl && thisControl.getControlType() == "standard" && !!(thisControl.getAttribute().getValue())) {
		thisControl.setFocus();
		$("#" + field + "_i").mask(format);
		thisControl.blur();
	}
}

function maskPhone() {
	//deprecated in favor of formatTelephoneNumber
	return;
	Mask("ftp_callbacknumber", "(000) 000-0000");
}

function formatTelephoneNumber(pContext) {
    if (!!pContext) {
        var changedAttribute = pContext.getEventSource();
        if (!!changedAttribute) {
            var value = changedAttribute.getValue();
            if (!!value) {
                var tempValue = value.toString().replace(/[^0-9A-Za-z]/g, "").toUpperCase();
                if (tempValue.substr(0, 1) == "1") tempValue = tempValue.substr(1, 99);
                var formattedValue = (tempValue.length >= 10) ? "(" + tempValue.substr(0, 3) + ") " + tempValue.substr(3, 3) + "-" + tempValue.substr(6, 4) : tempValue.substr(0, 3) + "-" + tempValue.substr(3, 4);
                changedAttribute.setValue(formattedValue);
            }
        }
    }
}

function setControlsOptional(controls) {
	for (var i = 0; i < controls.length; i++) {
		controls[i].getAttribute().setRequiredLevel("none");
	}
}

function setControlsBusinessRequired(controls) {
	for (var i = 0; i < controls.length; i++) {
		controls[i].getAttribute().setRequiredLevel("required");
	}
}

/*
function navToRequestOnChange() {
debugger;
var interactionId = Xrm.Page.data.entity.getId();
var interaction = REST.SYNC.retrieveRecordSync(interactionId, "ftp_interaction", "ftp_Request", null);
if (interaction.ftp_Request.Id != null) {
Xrm.Utility.openEntityForm("incident", interaction.ftp_Request.Id);
}
else {
setTimeout(navToRequestOnChange, 1000);
}
}
 */
function navToVeteranOnChange() {
    var vetLookupValue = Xrm.Page.getAttribute("ftp_veteran").getValue();
    if (!!vetLookupValue) Xrm.Utility.openEntityForm("ftp_veteran", vetLookupValue[0].id);
}

function updateIdProofBusinessRulesOnChange() {
    Xrm.Page.getControl("ftp_lastname").clearNotification("VALIDATION");
    var lastNameControl = Xrm.Page.getControl("ftp_lastname");
    var lastNameChecked = lastNameControl.getAttribute().getValue();
    var dobControl = Xrm.Page.getControl("ftp_ssn");
    var dobChecked = dobControl.getAttribute().getValue();
    var ssnControl = Xrm.Page.getControl("ftp_dob");
    var ssnChecked = ssnControl.getAttribute().getValue();

    if ((!lastNameChecked && !dobChecked && !ssnChecked) //all unchecked
		 || (lastNameChecked && !dobChecked && !ssnChecked) //two unchecked
		 || (dobChecked && !ssnChecked && !lastNameChecked) //two unchecked
		 || (ssnChecked && !lastNameChecked && !dobChecked) //two unchecked
		 || (lastNameChecked && dobChecked & ssnChecked)) { //all checked
        setControlsBusinessRequired([lastNameControl, dobControl, ssnControl]); //all check boxes required
    }
    else if (lastNameChecked && dobChecked && !ssnChecked) { //two checked
        setControlsBusinessRequired([lastNameControl, dobControl]);
        setControlsOptional([ssnControl]); //leave third check box optional
        ssnControl.clearNotification();
    }
    else if (dobChecked && ssnChecked && !lastNameChecked) { //two checked
        setControlsBusinessRequired([dobControl, ssnControl]);
        setControlsOptional([lastNameControl]); //leave third check box optional
        lastNameControl.clearNotification();
    }
    else if (ssnChecked && lastNameChecked && !dobChecked) { //two checked
        setControlsBusinessRequired([ssnControl, lastNameControl]);
        setControlsOptional([dobControl]); //leave third check box optional
        dobControl.clearNotification();
    }
}

function passesIdProtocol() {
    var lastNameChecked = Xrm.Page.getAttribute("ftp_lastname").getValue();
    var dobChecked = Xrm.Page.getAttribute("ftp_ssn").getValue();
    var ssnChecked = Xrm.Page.getAttribute("ftp_dob").getValue();

    if ((!lastNameChecked && !dobChecked)
		 || (!dobChecked && !ssnChecked)
		 || (!ssnChecked && !lastNameChecked)) {

        Xrm.Page.getControl("ftp_lastname").setNotification("Please validate at least two fields before saving.", "VALIDATION");
        return false;
    }
    return true;
}

function populateSubject() {
	var subject = "";
	var vetValue = Xrm.Page.getAttribute("ftp_veteran").getValue();
	if (!!vetValue) subject += vetValue[0].name;
	subject += " - " + Xrm.Page.getAttribute("tri_interactiontype").getText();
	Xrm.Page.getAttribute("ftp_subject").setValue(subject);
}

function ftp_details_onChange() {
	var val = Xrm.Page.getAttribute("ftp_details").getValue();
	val = removeCarriageReturns(val);
	Xrm.Page.getAttribute("ftp_details").setValue(val);
}

function removeCarriageReturns(pText) {
	return !!pText ? pText.replace(/(\r\n|\r|\n)/gm, ". ").replace("..", ".") : "";
}

function form_onSave(econtext) {
	var eventArgs = econtext.getEventArgs();

	//prevent auto save
	if (eventArgs.getSaveMode() == 70) {
		eventArgs.preventDefault();
		return;
	}

	var isOutbound = Xrm.Page.getAttribute("ftp_direction").getValue() == true;

	//check ID protocol checkboxes before continuing
	if (passesIdProtocol()) {
		populateSubject();
		var number = Xrm.Page.getAttribute("ftp_callbacknumber").getValue();
		var isFirstCallResolution = Xrm.Page.getAttribute("tri_firstcallinteraction").getValue();
		debugger;

		//this is required because USD Saved event on Interaction hosted control is not working
		var savedViaComboRequestButton = Xrm.Page.getAttribute("ftp_comborequest").getValue();
		if (savedViaComboRequestButton) {
			//this section is deprecated, replaced by script behind the Create Combo Request web resource that is embedded on this form
			/* if (location.href.indexOf("Outlook15White") != -1) {
			var vetValue = Xrm.Page.getAttribute("ftp_veteran").getValue();
			if (!!vetValue) {
			var vetId = vetValue[0].id;
			var vetName = vetValue[0].name;
			setTimeout(
			function () {
			window.open("http://event/?eventname=Shared Create MRR from Interaction&callback=" + number + "&vetId=" + vetId + "&vetName=" + vetName);
			}
		, 500
			);
			}
			} */
		}
		if (!savedViaComboRequestButton && !isFirstCallResolution && (!isOutbound || (isOutbound && !_usedOutboundCallScript))) {
			if (location.href.indexOf("Outlook15White") != -1) {
				//USD Session
				setTimeout(
					function () {
						var rfrValue = Xrm.Page.getAttribute("tri_reasonforrequest").getValue();
						var rfrName = !!rfrValue ? rfrValue[0].name : "";
						var rfrId = !!rfrValue ? rfrValue[0].id : "";
						var detailsValue = removeCarriageReturns(Xrm.Page.getAttribute("ftp_details").getValue());
						window.open("http://event/?eventname=Set interaction fields&outbound=false&callbacknumber=" + number + "&reasonforrequestname=" + rfrName + "&reasonforrequestid=" + rfrId + "&details=" + detailsValue);
					},
					500
				);
				//this event only exists on the "New Interaction" hosted control
				//this event in turn calls action calls to:
				//1. create a new non-fcr request in the Shared New Request hosted control
				//2. copy the url string parameters (above) into the $Context parameters
			}
		}

		//pharmacy outbound interactions
		if (isOutbound && _usedOutboundCallScript) {
			writeToConsole("firing onSave logic for pharmacy outbound interaction");
			/*
			7/6/17: the _usedOutboundCallScript flag can only be set to true via the setupOutboundInteraction() function,
			which is fired via an action call in the Pharmacy Outbound Call script, which is only accessible to Pharmacy USD users
			 */
			/*
			check required fields before saving this outbound interaction and opening the new request
			these fields are filled in on the form via Agent Script Answers in USD
			 */
			if (Xrm.Page.getAttribute("ftp_outboundinteractionaddress").getValue() != null && Xrm.Page.getAttribute("ftp_outboundinteractionsubreasonid").getValue() != null) {
				if (location.href.indexOf("Outlook15White") != -1) {
					//USD Session
					setTimeout(
						function () {
							/*reason for request, tri_reasonforrequest*/
							var rfrValue = Xrm.Page.getAttribute("tri_reasonforrequest").getValue();
							var rfrName = !!rfrValue ? rfrValue[0].name : "";
							var rfrId = !!rfrValue ? rfrValue[0].id : "";

							/*reason for call, ftp_outboundinteractionsubreasonid*/
							var subreasonValue = Xrm.Page.getAttribute("ftp_outboundinteractionsubreasonid").getValue();
							var subreasonName = !!subreasonValue ? subreasonValue[0].name : "";
							var subreasonId = !!subreasonValue ? subreasonValue[0].id : "";

							var eventUrl = "http://event/?eventname=Set interaction fields";
							eventUrl += "&outbound=true";
							eventUrl += "&callbacknumber=" + number;
							eventUrl += "&reasonforrequestname=" + rfrName + "&reasonforrequestid=" + rfrId;
							eventUrl += "&subreasonname=" + subreasonName + "&subreasonid=" + subreasonId;

							if (subreasonName == "Other") {
								var otherReasonText = Xrm.Page.getAttribute("ftp_outboundinteractioncallreasonother").getValue();
								eventUrl += !!otherReasonText ? ("&subreasonothertext=" + otherReasonText) : "";
							}

							var detailsValue = removeCarriageReturns(Xrm.Page.getAttribute("ftp_details").getValue());
							eventUrl += "&details=" + detailsValue;
							window.open(eventUrl);
						},
						500
					);
				}
			}
			else {
				alert("Missing some required fields for a pharmacy outbound interaction. Make sure you followed the Pharmacy Outbound Call script.");
				eventArgs.preventDefault();
				return;
			}
		}
	}
}

function retrieveTeamsForUser(CallBackParameter) {
	if (typeof CallBackParameter == "function") {
		var currentUserId = Xrm.Page.context.getUserId();
		var oDataPath = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/";
		var query = "SystemUserSet(guid'" + currentUserId + "')/teammembership_association?$select=Name,TeamId";

		$.ajax({
			type: "GET",
			contentType: "application/json; charset=utf-8",
			datatype: "json",
			url: oDataPath + query,
			beforeSend: function (XMLHttpRequest) {
				//Specifying this header ensures that the results will be returned as JSON.
				XMLHttpRequest.setRequestHeader("Accept", "application/json");
			},
			success: function (data, textStatus, XmlHttpRequest) {
				if (!!data)
					CallBackParameter(data.d.results);
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert("Error retrieving teams for current user: " + errorThrown);
			},
			async: true,
			cache: false
		});
	}
	else {
		alert("Did not retrieve teams for current user.  CallBackParameter must be a function.");
		//CallBackParameter must be a function
	}
}

function setUpControlledSubstanceRenewalForPharmacyTeamMembers() {
    try {
        var reasonForSubstanceRenewalControl = Xrm.Page.getControl("ftp_reasonforcontrolledsubstancerenewal");
        if (!reasonForSubstanceRenewalControl) return;
        reasonForSubstanceRenewalControl.getAttribute().setRequiredLevel("none")
        reasonForSubstanceRenewalControl.setVisible(false);
        reasonForSubstanceRenewalControl.clearNotification();

        var rfrAtt = Xrm.Page.getAttribute("tri_reasonforrequest");
        if (!!rfrAtt) {
            var rfrValue = rfrAtt.getValue();
            if (!!rfrValue && rfrValue[0].name == "Medication - Renewal Narcotic") {
                retrieveTeamsForUser(
					function (retrievedRecords) {
					    if (retrievedRecords.length < 1) return;
					    for (var i = 0, l = retrievedRecords.length; i < l; i++) {
					        var thisTeam = retrievedRecords[i];
					        if (thisTeam.hasOwnProperty("Name") && thisTeam.Name == "Pharmacy") {
					            reasonForSubstanceRenewalControl.getAttribute().setRequiredLevel("required");
					            reasonForSubstanceRenewalControl.clearNotification();
					            reasonForSubstanceRenewalControl.setVisible(true);
					            break;
					        }
					    }
					}
				);
            }
            else {
                reasonForSubstanceRenewalControl.getAttribute().setValue();
            }
        }
    }
    catch (e) {
        alert('Error occurred in set Reason for Renewal: ' + e.message);
    }
}

function filterRFR() {
    //deprecated for regular lookup filtering via form field configuration
    return;
    try {
        if (Xrm.Page.getControl("tri_reasonforrequest")) {
            retrieveTeamsForUser(
				function (retrievedRecords) {
				    if (retrievedRecords.length < 1) return;
				    for (var i = 0, l = retrievedRecords.length; i < l; i++) {
				        var thisTeam = retrievedRecords[i];
				        if (thisTeam.hasOwnProperty("Name") && thisTeam.hasOwnProperty("TeamId") &&
							(thisTeam.Name == "Pharmacy" || thisTeam.Name == "PACT User" || thisTeam.Name == "CCA Team" || thisTeam.Name == "TAN")) {
				            var fetchXml = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">' +
								'     <entity name="ftp_reasonforrequest">' +
								'         <attribute name="ftp_reasonforrequestid" />' +
								'         <attribute name="ftp_reason" />' +
								'         <attribute name="createdon" />' +
								'         <filter type="and">' +
								'             <condition attribute="statecode" operator="eq" value="0" />' +
								'             <condition attribute="ftp_display" operator="eq" value="1" />' +
								'         </filter>' +
								'        <link-entity name="ftp_ftp_reasonforrequest_team" from="ftp_reasonforrequestid" to="ftp_reasonforrequestid">' +
								'            <link-entity name="team" from="teamid" to="teamid" alias="ac">' +
								'                <filter type="and">' +
								'                    <condition attribute="teamid" value="' + thisTeam.TeamId + '" uitype="team" uiname="' + thisTeam.Name + '" operator="eq" />' +
								'                </filter>' +
								'            </link-entity>' +
								'        </link-entity>' +
								'     </entity>' +
								' </fetch>';
				            var layoutXml = '<grid name="resultset" object="1" jump="ftp_reasonforrequestid" select="1" icon="1" preview="1">' +
								'<row name="result" id="ftp_reasonforrequestid">' +
								'<cell name="ftp_reason" width="150" />' +
								'<cell name="createdon" width="150" />' +
								'</row>' +
								'</grid>';
				            Xrm.Page.getControl("tri_reasonforrequest").addCustomView(
								"{00000000-0000-0000-0000-00000000000" + i + "}",
								"ftp_reasonforrequest",
								"Reasons for Request",
								fetchXml,
								layoutXml,
								true
							);
				            break;
				        }
				    }//end for loop
				}
			);
        }
    }
    catch (e) {
        alert('Error occurred in filter RFR: ' + e.message);
    }
}

function writeToConsole(message) {
    if (typeof console != 'undefined') console.log(message);
}

/*used by USD agent script answers*/
function setupOutboundInteraction(pOutboundCallRFRName) {
    /*requires SDK.REST library for retrieval of appropriate Reason For Request record*/
    var retrievedReasons = [];
    var rfrName = !!pOutboundCallRFRName ? pOutboundCallRFRName : "Pharmacy Outbound Call";
    var query = "$select=ftp_reasonforrequestId,ftp_reason&$filter=ftp_reason eq '" + rfrName + "' and statecode/Value eq 0&$orderby=CreatedOn desc";
    SDK.REST.retrieveMultipleRecords(
		"ftp_reasonforrequest",
		query,
		function (retrievedRecords) {
		    if (!!retrievedRecords && retrievedRecords.length > 0) retrievedReasons = retrievedReasons.concat(retrievedRecords);
		},
		errorHandler,
		function () {
		    if (retrievedReasons.length > 0) {
		        var outboundRFR = retrievedReasons[0];
		        Xrm.Page.getAttribute("tri_interactiontype").setValue(167410009); /*set type to 'Other'*/
		        Xrm.Page.getAttribute("tri_reasonforrequest").setValue([{ id: outboundRFR.ftp_reasonforrequestId, name: outboundRFR.ftp_reason, entityType: "ftp_reasonforrequest" }]);/*reason for request*/
		        Xrm.Page.getAttribute("ftp_direction").setValue(true);/*outbound*/
		        Xrm.Page.getAttribute("tri_firstcallinteraction").setValue(false);/*first call resolution = no, as of 5/30/17*/
		        Xrm.Page.getAttribute("ftp_interactedwith").setRequiredLevel("none");

		        /*Hide Combo Request button?*/

		        /*Show Outbound Interaction Details tab*/
		        Xrm.Page.ui.tabs.get("Tab_OutboundPharmacyInteraction").setVisible(true);
		        Xrm.Page.ui.tabs.get("Tab_OutboundPharmacyInteraction").setDisplayState("expanded");
		        Xrm.Page.getControl("ftp_outboundinteractionwhoansweredtext").setVisible(false);

		        _usedOutboundCallScript = true;
		    }
		    else {
		        /*
				could not find the "Pharmacy Outbound Call" reason for request record
				future: if we setup the proper N:N relationship between ftp_reasonforrequest and ftp_facility entity, we can filter against current user's facility.
				*/
		        alert("Script error in setupOutboundInteraction(): Could not find '" + rfrName + "' reason for request.");
		    }
		}
	);
}

/*used by USD agent script answers*/
function setOutboundInteractionAddressSelection(pValue) {
    if (!!pValue) {
        Xrm.Page.getAttribute("ftp_outboundinteractionaddress").setValue(pValue);
    }
}

/*used by USD agent script answers*/
function setOutboundInteractionSubReasonSelection(pName) {
    /*
	find sub-reason record called pName and fill the ftp_outboundinteractionsubreasonid lookup field on this interaction
	*/
    if (!!pName) {
        var retrievedReasons = [];
        var query = "$select=ftp_subreasonId,ftp_name&$filter=ftp_name eq '" + pName + "' and statecode/Value eq 0&$orderby=CreatedOn desc";
        SDK.REST.retrieveMultipleRecords(
			"ftp_subreason",
			query,
			function (retrievedRecords) {
			    if (!!retrievedRecords && retrievedRecords.length > 0) retrievedReasons = retrievedReasons.concat(retrievedRecords);
			},
			errorHandler,
			function () {
			    if (retrievedReasons.length > 0) {
			        var subreason = retrievedReasons[0];
			        Xrm.Page.getAttribute("ftp_outboundinteractionsubreasonid").setValue([{ id: subreason.ftp_subreasonId, name: subreason.ftp_name, entityType: "ftp_subreason" }]);
			        Xrm.Page.getAttribute("ftp_outboundinteractionsubreasonid").fireOnChange();
			    }
			    else {
			        /*
					could not find the reason for request record called pName
					*/
			        alert("Script error in setOutboundInteractionSubReasonSelection(): Could not find '" + pName + "' sub-reason record.");
			    }
			}
		);
    }
}

function ftp_outboundinteractionsubreasonid_onChange() {
    var reasonValue = Xrm.Page.getAttribute("ftp_outboundinteractionsubreasonid").getValue();
    var reasonName = !!reasonValue && reasonValue.length == 1 ? reasonValue[0].name : null;

    /*setup ftp_outboundinteractioncallreasonother field */
    var otherTextControl = Xrm.Page.getControl("ftp_outboundinteractioncallreasonother");
    if (!!otherTextControl) {
        otherTextControl.setVisible(reasonName == "Other");
        otherTextControl.getAttribute().setRequiredLevel(reasonName == "Other" ? "required" : "none");
        //otherTextControl.setFocus();
    }
}

function ftp_outboundinteractionwhoansweredos_onChange() {
    /*
		keep the value of ftp_outboundinteractionwhoansweredtext field in-sync with user's selection in the ftp_outboundinteractionwhoansweredos field;
		if ftp_outboundinteractionwhoansweredos value is 100000005 ("other"), show and require the ftp_outboundinteractionwhoansweredtext field
	*/
    var whoAnsweredOSValue = Xrm.Page.getAttribute("ftp_outboundinteractionwhoansweredos").getValue();

    /*setup ftp_outboundinteractionwhoansweredtext field*/
    var whoAnsweredTextControl = Xrm.Page.getControl("ftp_outboundinteractionwhoansweredtext");
    if (!!whoAnsweredTextControl) {
        whoAnsweredTextControl.setVisible(whoAnsweredOSValue == 100000005);
        whoAnsweredTextControl.getAttribute().setValue((!!whoAnsweredOSValue && whoAnsweredOSValue != 100000005) ? Xrm.Page.getAttribute("ftp_outboundinteractionwhoansweredos").getText() : null);
        whoAnsweredTextControl.getAttribute().setRequiredLevel(whoAnsweredOSValue == 100000005 ? "required" : "none");
        if (whoAnsweredOSValue == 100000005) { whoAnsweredTextControl.setFocus(); }
    }
}

function tri_firstcallinteraction_onChange() {
	var fcrAttr = Xrm.Page.getAttribute("tri_firstcallinteraction");
	var callBackNumberAttr = Xrm.Page.getAttribute("ftp_callbacknumber");
	if (!!fcrAttr && !!callBackNumberAttr) {
		callBackNumberAttr.setRequiredLevel(fcrAttr.getValue() == true ? "none" : "required");
	}
}

function errorHandler(error) {
    writeToConsole(error.message);
    alert(error.message);
}