var configData = null;

function doLoad(){
	//put onLoad logic here
	configData = parseDataParametersFromUrl(location.search);
	//example config object: {sourceEntityLogicalName: "Incident", sourceEntityId: "8315a3cd-bced-e411-9402-00155d14e625", sourceEntityLookupFieldName: "CustomerId", targetEntityName: "Contact" }
	routeToTargetEntity(configData);
}

function parseDataParametersFromUrl(pQueryString) {
	var fullParameterArray = pQueryString.substr(1).split("&");

	//clean up the URL query string and split each member into a key/value pair
	for (var i in fullParameterArray) {
		fullParameterArray[i] = fullParameterArray[i].replace(/\+/g, " ").split("=");
	}
	
	var customDataString = "";
	for(var i in fullParameterArray){
		if(fullParameterArray[i][0].toLowerCase() == "data"){
			customDataString = fullParameterArray[i][1];
			break;
		}
	}
	
	var customDataArray = decodeURIComponent(customDataString).split("&");
	for (var i in customDataArray) {
		customDataArray[i] = customDataArray[i].replace(/\+/g, " ").split("=");
	}
	
	
	var configObject = {};
	for (var i in customDataArray) {
		if (customDataArray[i][0] == "sourceEntityLogicalName") configObject.sourceEntityLogicalName = customDataArray[i][1];
		if (customDataArray[i][0] == "sourceEntityId") configObject.sourceEntityId = customDataArray[i][1];
		if (customDataArray[i][0] == "sourceEntityLookupFieldName") configObject.sourceEntityLookupFieldName = customDataArray[i][1];
		if (customDataArray[i][0] == "targetEntityName") configObject.targetEntityName = customDataArray[i][1];
	}
	return configObject;
}

function routeToTargetEntity(pConfigData) {
	if (!!pConfigData &&
		pConfigData.hasOwnProperty("sourceEntityLogicalName") && !!pConfigData.sourceEntityLogicalName &&
		pConfigData.hasOwnProperty("sourceEntityId") && !!pConfigData.sourceEntityId &&
		pConfigData.hasOwnProperty("sourceEntityLookupFieldName") && !!pConfigData.sourceEntityLookupFieldName &&
		pConfigData.hasOwnProperty("targetEntityName") && !!pConfigData.targetEntityName) {
		SDK.REST.retrieveRecord(
			pConfigData.sourceEntityId,
			pConfigData.sourceEntityLogicalName,
			pConfigData.sourceEntityLookupFieldName,
			null,
			function (retrievedRecord) {
				if (!!retrievedRecord && !!retrievedRecord[pConfigData.sourceEntityLookupFieldName]) {
					var entityReference = retrievedRecord[pConfigData.sourceEntityLookupFieldName];
					var clientUrl = SDK.REST.hasOwnProperty("_getClientUrl") ? SDK.REST._getClientUrl() : SDK.REST._getServerUrl(); //account for older versions of SDK.REST.js library
					if(pConfigData.targetEntityName.toLowerCase() == "contact"){
						var parameters = "contactid=" + entityReference.Id;
						//Xrm.Utility.openWebResource("ftp_/VeteranAlerts/VeteranAlerts.html", encodeURIComponent(parameters));
						var wrURL = clientUrl + "/WebResources/ftp_/VeteranAlerts/VeteranAlerts.html?Data=" + encodeURIComponent(parameters);
						window.open(wrURL);
					}
					else{
						var recordUrl = clientUrl + "/main.aspx?etn=" + pConfigData.targetEntityName.toLowerCase() + "&pagetype=entityrecord&id=%7b" + entityReference.Id + "%7d";
						window.open(recordUrl);
					}
				}
			},
			function (error) {
				//alert(error);
			}
		);
	}
	else {
		//don't have enough information; could throw alert or raise a USD event
	}
}