/// <reference path="Constants.js" />
/// <reference path="../lib/DynamicsPortalToolkit.js" />

if (typeof (Avanade) == "undefined") { Avanade = { __namespace: true }; }
if (typeof (Avanade.Leonardo) == "undefined") { Avanade.Leonardo = { __namespace: true }; }

Avanade.Leonardo.Utils = new function () {
	var self = this;

	const url = Avanade.Leonardo.Constants.url;
	const incidentFields = Avanade.Leonardo.Constants.incident;
	const privateConfigurationFields = Avanade.Leonardo.Constants.ava_privateconfiguration;
	const contactFields = Avanade.Leonardo.Constants.contact;


	self.CustomEncode = function (textCheck) {
		var textEncoding = textCheck;
		var characterReg = /[#$%^&*()_+\=\[\]{}'"\\|<>\/?]/g;
		var specialcharacterReg = /[àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛäëïöüÿÄËÏÖÜŸãñõÃÑÕåÅ¿¡ßçÇ°§]/g;
		let all_special_characters = [
			{
				value: 121800000, encode: "00023", decode: "#"
			},
			{
				value: 121800001, encode: "00024", decode: "$"
			},
			{
				value: 121800002, encode: "00025", decode: "%"
			},
			{
				value: 121800003, encode: "0005E", decode: "^"
			},
			{
				value: 121800004, encode: "00026", decode: "&"
			},
			{
				value: 121800005, encode: "0002A", decode: "*"
			},
			{
				value: 121800006, encode: "00028", decode: "("
			},
			{
				value: 121800007, encode: "00029", decode: ")"
			},
			{
				value: 121800008, encode: "0005F", decode: "_"
			},
			{
				value: 121800009, encode: "0002B", decode: "+"
			},
			{
				value: 121800010, encode: "0003D", decode: "="
			},
			{
				value: 121800011, encode: "0005B", decode: "["
			},
			{
				value: 121800012, encode: "0005D", decode: "]"
			},
			{
				value: 121800013, encode: "0007B", decode: "{"
			},
			{
				value: 121800014, encode: "0007D", decode: "}"
			},
			{
				value: 121800015, encode: "00027", decode: "'"
			},
			{
				value: 121800016, encode: "00022", decode: "\""
			},
			{
				value: 121800017, encode: "0003E", decode: ">"
			},
			{
				value: 121800018, encode: "0003C", decode: "<"
			},
			{
				value: 121800019, encode: "0005C", decode: "\\"
			},
			{
				value: 121800020, encode: "0002F", decode: "/"
			},
			{
				value: 121800021, encode: "0003F", decode: "?"
			},
			{
				value: 121800052, encode: "111WA5C", decode: "|"
			},
		];
		let special_Letters = [
			{
				value: 121800000, encode: "a", decode: "à"
			},
			{
				value: 121800001, encode: "e", decode: "è"
			},
			{
				value: 121800002, encode: "i", decode: "ì"
			},
			{
				value: 121800003, encode: "o", decode: "ò"
			},
			{
				value: 121800004, encode: "u", decode: "ù"
			},
			{
				value: 121800005, encode: "A", decode: "À"
			},
			{
				value: 121800006, encode: "E", decode: "È"
			},
			{
				value: 121800007, encode: "I", decode: "Ì"
			},
			{
				value: 121800008, encode: "O", decode: "Ò"
			},
			{
				value: 121800009, encode: "U", decode: "Ù"
			},
			{
				value: 121800010, encode: "a", decode: "á"
			},
			{
				value: 121800011, encode: "y", decode: "ý"
			},
			{
				value: 121800012, encode: "e", decode: "é"
			},
			{
				value: 121800013, encode: "i", decode: "í"
			},
			{
				value: 121800014, encode: "o", decode: "ó"
			},
			{
				value: 121800015, encode: "u", decode: "ú"
			},
			{
				value: 121800016, encode: "A", decode: "Á"
			},
			{
				value: 121800017, encode: "E", decode: "É"
			},
			{
				value: 121800018, encode: "I", decode: "Í"
			},
			{
				value: 121800019, encode: "O", decode: "Ó"
			},
			{
				value: 121800020, encode: "U", decode: "Ú"
			},
			{
				value: 121800021, encode: "Y", decode: "Ý"
			},
			{
				value: 121800022, encode: "A", decode: "ä"
			},
			{
				value: 121800023, encode: "e", decode: "ë"
			},
			{
				value: 121800024, encode: "i", decode: "ï"
			},
			{
				value: 121800025, encode: "o", decode: "ö"
			},
			{
				value: 121800026, encode: "u", decode: "ü"
			},
			{
				value: 121800027, encode: "y", decode: "ÿ"
			},
			{
				value: 121800027, encode: "A", decode: "Ä"
			},
			{
				value: 121800028, encode: "E", decode: "Ë"
			},
			{
				value: 121800029, encode: "I", decode: "Ï"
			},
			{
				value: 121800030, encode: "O", decode: "Ö"
			},
			{
				value: 121800031, encode: "U", decode: "Ü"
			},
			{
				value: 121800032, encode: "Y", decode: "Ÿ"
			},
			{
				value: 121800033, encode: "c", decode: "ç"
			},
			{
				value: 121800034, encode: "-", decode: "°"
			},
			{
				value: 121800035, encode: "S", decode: "§"
			},
			{
				value: 121800035, encode: "a", decode: "ã"
			},
			{
				value: 121800037, encode: "n", decode: "ñ"
			},
			{
				value: 121800038, encode: "o", decode: "õ"
			},
			{
				value: 121800039, encode: "A", decode: "Ã"
			},
			{
				value: 121800040, encode: "N", decode: "Ñ"
			},
			{
				value: 121800041, encode: "O", decode: "Õ"
			},
			{
				value: 121800042, encode: "C", decode: "Ç"
			},
			{
				value: 121800041, encode: "a", decode: "å"
			},
			{
				value: 121800042, encode: "A", decode: "Å"
			},
			{
				value: 121800043, encode: "a", decode: "â"
			},
			{
				value: 121800044, encode: "e", decode: "ê"
			},
			{
				value: 121800045, encode: "i", decode: "î"
			},
			{
				value: 121800046, encode: "o", decode: "ô"
			},
			{
				value: 121800047, encode: "u", decode: "û"
			},
			{
				value: 121800048, encode: "A", decode: "Â"
			},
			{
				value: 121800049, encode: "E", decode: "Ê"
			},
			{
				value: 121800050, encode: "I", decode: "Î"
			},
			{
				value: 121800051, encode: "O", decode: "Ô"
			},
			{
				value: 121800052, encode: "U", decode: "Û"
			}
		];
		if (characterReg.test(textCheck)) {
			all_special_characters.forEach((element) => {
				textEncoding = textEncoding.replaceAll(element.decode, element.encode);
			});

		} else if (specialcharacterReg.test(textCheck)) {
			special_Letters.forEach((element) => {
				textEncoding = textEncoding.replaceAll(element.decode, element.encode);
			});

		}
		return textEncoding;
	};

	self.CustomDecode = function (textCheck) {
		var textDecoding = textCheck;
		var characterReg = /[#$%^&*()_+\=\[\]{}'"\\|<>\/?]/g;
		var specialcharacterReg = /[àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛäëïöüÿÄËÏÖÜŸãñõÃÑÕåÅ¿¡ßçÇ°§]/g;
		let all_special_characters = [
			{
				value: 121800000, encode: "00023", decode: "#"
			},
			{
				value: 121800001, encode: "00024", decode: "$"
			},
			{
				value: 121800002, encode: "00025", decode: "%"
			},
			{
				value: 121800003, encode: "0005E", decode: "^"
			},
			{
				value: 121800004, encode: "00026", decode: "&"
			},
			{
				value: 121800005, encode: "0002A", decode: "*"
			},
			{
				value: 121800006, encode: "00028", decode: "("
			},
			{
				value: 121800007, encode: "00029", decode: ")"
			},
			{
				value: 121800008, encode: "0005F", decode: "_"
			},
			{
				value: 121800009, encode: "0002B", decode: "+"
			},
			{
				value: 121800010, encode: "0003D", decode: "="
			},
			{
				value: 121800011, encode: "0005B", decode: "["
			},
			{
				value: 121800012, encode: "0005D", decode: "]"
			},
			{
				value: 121800013, encode: "0007B", decode: "{"
			},
			{
				value: 121800014, encode: "0007D", decode: "}"
			},
			{
				value: 121800015, encode: "00027", decode: "'"
			},
			{
				value: 121800016, encode: "00022", decode: "\""
			},
			{
				value: 121800017, encode: "0003E", decode: ">"
			},
			{
				value: 121800018, encode: "0003C", decode: "<"
			},
			{
				value: 121800019, encode: "0005C", decode: "\\"
			},
			{
				value: 121800020, encode: "0002F", decode: "/"
			},
			{
				value: 121800021, encode: "0003F", decode: "?"
			},
		];
		let special_Letters = [
			{
				value: 121800000, encode: "a", decode: "à"
			},
			{
				value: 121800001, encode: "e", decode: "è"
			},
			{
				value: 121800002, encode: "i", decode: "ì"
			},
			{
				value: 121800003, encode: "o", decode: "ò"
			},
			{
				value: 121800004, encode: "u", decode: "ù"
			},
			{
				value: 121800005, encode: "A", decode: "À"
			},
			{
				value: 121800006, encode: "E", decode: "È"
			},
			{
				value: 121800007, encode: "I", decode: "Ì"
			},
			{
				value: 121800008, encode: "O", decode: "Ò"
			},
			{
				value: 121800009, encode: "U", decode: "Ù"
			},
			{
				value: 121800010, encode: "a", decode: "á"
			},
			{
				value: 121800011, encode: "y", decode: "ý"
			},
			{
				value: 121800012, encode: "e", decode: "é"
			},
			{
				value: 121800013, encode: "i", decode: "í"
			},
			{
				value: 121800014, encode: "o", decode: "ó"
			},
			{
				value: 121800015, encode: "u", decode: "ú"
			},
			{
				value: 121800016, encode: "A", decode: "Á"
			},
			{
				value: 121800017, encode: "E", decode: "É"
			},
			{
				value: 121800018, encode: "I", decode: "Í"
			},
			{
				value: 121800019, encode: "O", decode: "Ó"
			},
			{
				value: 121800020, encode: "U", decode: "Ú"
			},
			{
				value: 121800021, encode: "Y", decode: "Ý"
			},
			{
				value: 121800022, encode: "A", decode: "ä"
			},
			{
				value: 121800023, encode: "e", decode: "ë"
			},
			{
				value: 121800024, encode: "i", decode: "ï"
			},
			{
				value: 121800025, encode: "o", decode: "ö"
			},
			{
				value: 121800026, encode: "u", decode: "ü"
			},
			{
				value: 121800027, encode: "y", decode: "ÿ"
			},
			{
				value: 121800027, encode: "A", decode: "Ä"
			},
			{
				value: 121800028, encode: "E", decode: "Ë"
			},
			{
				value: 121800029, encode: "I", decode: "Ï"
			},
			{
				value: 121800030, encode: "O", decode: "Ö"
			},
			{
				value: 121800031, encode: "U", decode: "Ü"
			},
			{
				value: 121800032, encode: "Y", decode: "Ÿ"
			},
			{
				value: 121800033, encode: "c", decode: "ç"
			},
			{
				value: 121800034, encode: "-", decode: "°"
			},
			{
				value: 121800035, encode: "S", decode: "§"
			},
			{
				value: 121800035, encode: "a", decode: "ã"
			},
			{
				value: 121800037, encode: "n", decode: "ñ"
			},
			{
				value: 121800038, encode: "o", decode: "õ"
			},
			{
				value: 121800039, encode: "A", decode: "Ã"
			},
			{
				value: 121800040, encode: "N", decode: "Ñ"
			},
			{
				value: 121800041, encode: "O", decode: "Õ"
			},
			{
				value: 121800042, encode: "C", decode: "Ç"
			},
			{
				value: 121800041, encode: "a", decode: "å"
			},
			{
				value: 121800042, encode: "A", decode: "Å"
			},
			{
				value: 121800043, encode: "a", decode: "â"
			},
			{
				value: 121800044, encode: "e", decode: "ê"
			},
			{
				value: 121800045, encode: "i", decode: "î"
			},
			{
				value: 121800046, encode: "o", decode: "ô"
			},
			{
				value: 121800047, encode: "u", decode: "û"
			},
			{
				value: 121800048, encode: "A", decode: "Â"
			},
			{
				value: 121800049, encode: "E", decode: "Ê"
			},
			{
				value: 121800050, encode: "I", decode: "Î"
			},
			{
				value: 121800051, encode: "O", decode: "Ô"
			},
			{
				value: 121800052, encode: "U", decode: "Û"
			}
		];

		all_special_characters.forEach((element) => {
			textDecoding = textDecoding.replaceAll(element.encode, element.decode)
		});

		return textDecoding;
	};

	self.IncidentAutoNumber = new function () {
		const self = this;
		var sourceField = incidentFields.casetypecode;

		var departmentKeyValues = [];
		departmentKeyValues.push(
			{
				key: 'PSE',
				value: incidentFields.DepartmentTypeCode.PSE,
				availableType: [
					incidentFields.CaseTypeCode.TechQueryRepairInstrQuery,
					incidentFields.CaseTypeCode.TechnicalPublicationQuery
				]
			},
			{
				key: 'FOC',
				value: incidentFields.DepartmentTypeCode.FOC,
				availableType: [incidentFields.CaseTypeCode.CSRFOC]
			},
			{
				key: 'CSM',
				value: incidentFields.DepartmentTypeCode.CSM,
				availableType: [
					incidentFields.CaseTypeCode.ComplaintsFeedback, incidentFields.CaseTypeCode.CustomerQueries
				]
			}
		);

		var documentyKeyValues = [];
		documentyKeyValues.push(
			{
				key: 'TQ',
				value: incidentFields.CaseDocumentType.TQ,
				availableType: [incidentFields.CaseTypeCode.TechQueryRepairInstrQuery]

			},
			{
				key: 'CF',
				value: incidentFields.CaseDocumentType.CF,
				availableType: [incidentFields.CaseTypeCode.ComplaintsFeedback]
			},
			{
				key: 'CQ',
				value: incidentFields.CaseDocumentType.CQ,
				availableType: [incidentFields.CaseTypeCode.CustomerQueries]
			},
			{
				key: 'TPQ',
				value: incidentFields.CaseDocumentType.TPQ,
				availableType: [incidentFields.CaseTypeCode.TechnicalPublicationQuery]
			},
			{
				key: 'CSR',
				value: incidentFields.CaseDocumentType.CSR,
				availableType: [incidentFields.CaseTypeCode.CSRFOC]
			}
		);

		function include(arr, obj) {
			if (arr === undefined || arr === null)
				return false;

			return arr.indexOf(obj) !== -1;
		}

		self.set = function () {
			const caseTypeCode = Dynamics.Portal.OptionSet.Attribute.get(sourceField);
			if (caseTypeCode === {})
				return;

			const value = caseTypeCode.value;
			let departmentValue = null;
			let documentValue = null;

			for (let key in departmentKeyValues) {

				const item = departmentKeyValues[key];

				const present = include(item.availableType, value);
				if (present)
					departmentValue = item.value;
			}

			for (let key in documentyKeyValues) {

				const item = documentyKeyValues[key];

				const present = include(item.availableType, value);
				if (present)
					documentValue = item.value;
			}

			if (departmentValue !== null)
				Dynamics.Portal.OptionSet.Attribute.set(incidentFields.lnd_departmenttypecode, departmentValue);

			if (documentValue !== null)
				Dynamics.Portal.OptionSet.Attribute.set(incidentFields.lnd_casedocumenttype, documentValue);


		};


	};

	self.CkEditor = new function () {
		const self = this;

		self.get = function (field) {
			return getRuntimeValue(field);
		};

		function getRuntimeValue(field) {
			return CKEDITOR.instances[field].getData();
		}
		function generateInstance(field) {
			if (CKEDITOR == null)
				return;
			return CKEDITOR.replace(field, {
				extraPlugins: ['pastebase64', 'base64image']
			});
		}

		function decodeToHtml(txt) {
			txt = txt.replace(/&amp;/g, '&');
			txt = txt.replace(/&quot;/g, '"');
			txt = txt.replace(/&lt;/g, '<');
			txt = txt.replace(/&gt;/g, '>');
			return txt;
		}
		function encodeToHtml(txt) {
			txt = txt.replace(/&/g, '&amp;');
			txt = txt.replace(/"/g, "&quot;");
			txt = txt.replace(/</g, '&lt;');
			txt = txt.replace(/>/g, '&gt;');
			return txt;
		}

		function storeEncodedHtml(field, text) {

			Dynamics.Portal.GenericFields.Attribute.clean(field);
			var newValue = encodeToHtml(text);

			Dynamics.Portal.GenericFields.Attribute.set(field, newValue);
			Dynamics.Portal.GenericFields.Attribute.fireOnChange(field);
		}

		function createCssClass() {
			var style = document.createElement('style');
			style.type = 'text/css';
			style.innerHTML = '.richtext-readonly{pointer-events:none;cursor: not-allowed}';
			document.getElementsByTagName('head')[0].appendChild(style);
		}
		function setEnabled(richTextField) {

			$('#cke_' + richTextField).find('iframe').contents().find('body').attr('contenteditable', true)

			$("#" + richTextField).parent().removeClass("richtext-readonly");
		}
		function setDisabled(richTextField) {

			$('#cke_' + richTextField).find('iframe').contents().find('body').attr('contenteditable', false)

			$("#" + richTextField).parent().addClass("richtext-readonly");

		}

		self.setEditability = function (richTextField, value) {
			//createCssClass(); ##Santo il pointer null impediva lo scroll (ora ho utilizzato l'attributo di ckeditor "contenteditable")

			value ? setEnabled(richTextField) : setDisabled(richTextField);

		};
		self.destroyInstance = function (field, richTextField) {
			if (CKEDITOR.instances[richTextField] != null) {

				var text = getRuntimeValue(richTextField);
				storeEncodedHtml(field, text);

				CKEDITOR.instances[richTextField].destroy();
				Dynamics.Portal.GenericFields.Attribute.clean(richTextField);
			}
		};
		self.create = function (field, richTextField) {
			if (($("#" + field).length === 0) || ($("#" + richTextField).length === 0))
				return;

			var fieldValue = $("#" + field).val();
			var richTextValue = decodeToHtml(fieldValue);
			$("#" + field).val(fieldValue);
			var instance = generateInstance(richTextField);
			CKEDITOR.instances[richTextField].setData(richTextValue);

			Dynamics.Portal.GenericFields.Visibility.set(field, false);
		};
		self.addValidator = function (fieldName, fieldLabel) {
			if (typeof (Page_Validators) == 'undefined') return;
			// Create new validator
			$("#" + fieldName + "_label").parent().addClass("required");

			var newValidator = document.createElement('span');
			newValidator.style.display = "none";
			newValidator.id = "RequiredFieldValidator" + fieldName;
			newValidator.controltovalidate = "casetypecode";
			newValidator.errormessage = "<a href='#" + fieldName + "_label'>" + fieldLabel + Leonardo.Constant.Body.Text.TXT00000496 + "</a>";
			newValidator.validationGroup = "";
			newValidator.initialvalue = "";
			newValidator.evaluationfunction = function () {
				var value = Avanade.Leonardo.Utils.CkEditor.get(fieldName);
				if (value == null || value === "") {
					return false;
				} else {
					return true;
				}
			};

			// Add the new validator to the page validators array:
			Page_Validators.push(newValidator);

			// Wire-up the click event handler of the validation summary link
			$("a[href='#" + fieldName + "_label']").on("click", function () { scrollToAndFocus(fieldName + '_label', fieldName); });

		};
		self.removeValidator = function (fieldName) {
			if (typeof (Page_Validators) == 'undefined') return;

			$.each(Page_Validators, function (index, validator) {
				if (validator.id == "RequiredFieldValidator" + fieldName) {
					ValidatorEnable(validator, false);
				}
			});

			$("#" + fieldName + "_label").parent().removeClass("required");
			$("#" + fieldName).removeAttr('required');

		};
	};

	self.Configuration = new function () {
		const self = this;

		self.TryGetValue = function (key) {
			const urlToRequest = url + Avanade.Leonardo.Constants.fetchUrl.GetValueFromPrivateConfiguration + "/?ava_key=" + key;
			const result = Dynamics.Portal.Fetch.executeWithReturn(urlToRequest, "");

			return result;
		};


	};

	self.DateTimePicker = new function () {
		const self = this;

		self.FormDateHandler = new function (param) {
			var allFormFields = param;
			this.fields = [
				"resolveby",
				"lnd_helicopterflighthoursdata",
				"lnd_componentflighthoursdata",
			];
			function hide(allFields) {
				allFields.forEach(function (item, index) {
					$("input#" + item).parent().find($(".input-append.input-group.datetimepicker")).hide();
					$("#" + item).parent().append(""
						+ "<div class='form-group'>"
						+ "	<div class='input-group date' id='datetimepicker" + index + "'>"
						+ "		<input type='text' class='form-control' />"
						+ "		<span class='input-group-addon'><span class='glyphicon glyphicon-calendar'></span></span>"
						+ "	</div>"
						+ "</div>"
					);
					$('#datetimepicker' + index).datetimepicker({
						format: "DD/MM/YYYY HH:mm"
					})
					$('#datetimepicker' + index).on('dp.change', function () {

						var euDate = $('#datetimepicker' + index).data('date');
						var date = euDate.substring(0, 10);
						var time = euDate.substring(11, 19);

						var ConvertDate = new function (param, param2) {
							const self = this;
							var date = param;
							var time = param2;
							self.ConvertToUS = function (date, time) {
								var timeSeparator = ":";
								var timeParts = time.split(timeSeparator);
								var separator = "/";
								var dateParts = date.split(separator);
								var dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0], timeParts[0], timeParts[1]);
								return dateObject;
							}
							self.ConvertToEU = function (date, time) {
								var timeSeparator = ":";
								var timeParts = time.split(timeSeparator);
								var separator = "/";
								var dateParts = date.split(separator);
								var dateObject = new Date(+dateParts[2], dateParts[0] - 1, +dateParts[1], timeParts[0], timeParts[1]);
								return dateObject;
							}
						}
						var loadDate = $("input#" + item).parent().find($(".input-append.input-group.datetimepicker")).data("date");
						if (loadDate.length == 0)
							return;
						if (loadDate.indexOf("/") == 1) {
							loadDate = "0" + loadDate;
						}
						if (loadDate.indexOf(":") != -1) {
							loadDateDate = loadDate.substring(0, 10);
						}
						var loadDateTime = loadDate.substring(11, 19);

						var finalDate = ConvertDate.ConvertToUS(date, time);
						$("input#" + item).parent().find($(".input-append.input-group.datetimepicker")).data("DateTimePicker").date(finalDate);
					});
				});

			}

			this.OnLoad = function () {
				//hide(this.fields);
			}
		};

		self.OnLoad = function () {

			var config = $('#userdivision').attr("value");

			if (window.location.href.contains("it-IT")) {
				$('.datetimepicker').datetimepicker({
					format: 'DD/MM/YYYY'
				});
			}
			else {
				if (config == 121800000) {
					$('.datetimepicker').datetimepicker({
						format: 'DD/MM/YYYY'
					});
				}
				else {
					$('.datetimepicker').datetimepicker({
						format: 'MM/DD/YYYY'
					});
				}
			}
		};
	};

	self.SharepointHandler = new function () {
		const self = this;

		self.OnLoad = function () {
			$('.add-folder').remove();
		};
	};

	self.ConvertDateToEU = new function (param) {
		const self = this;
		var date = param;

		self.Convert = function (date) {
			var separator = "";
			if (date.indexOf("/") != -1)
				separator = "/";
			else
				separator = ".";

			if (date.startsWith("1/")) {
				date = "0" + date;
			}

			if (date.indexOf(":") != -1) {
				date = date.substring(0, 10);
			}


			var dateParts = date.split(separator);
			var config = $('#userdateformat').attr("value");
			if (separator == "/" && config != 121800000) {
				var dateObject = new Date(+dateParts[2], dateParts[0] - 1, +dateParts[1]);
			}
			else {
				var dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]);
			}

			return dateObject.toString();
		}
	}

	self.Translate = new function () {
		const self = this;

		function TranslateLookupModals() {

			$("button[class|='btn btn-default launchentitylookup']").attr("title", "Open Lookup Modal");
			$("button[class|='btn btn-default clearlookupfield']").attr("title", "Clear");

			if ($("span[class|='fa fa-spinner fa-spin']")) {
				$('div[class^="view-loading"]').text("Loading...")
			}

			if ($("button[aria-label|='Hinzufügen']")) {
				$("button[aria-label|='Hinzufügen']").text("Assign");
			}

			if ($("h1[title|='Suchdatensätze']")) {
				$("h1[title|='Suchdatensätze']").text("Search record")
			}

			if ($("h1[title*='Details anzeigen']")) {
				$("h1[title*='Details anzeigen']").html("<span class='fa fa-info-circle' aria-hidden='true'>Task Detail</span>");
			}

			if ($("button[aria-label|='Auswählen']")) {
				$("button[aria-label|='Auswählen']").text("Select");
			}

			if ($("button[title|='Wert entfernen']")) {
				$("button[title|='Wert entfernen']").text("Remove");
			}

			if ($("button[title|='Abbrechen']")) {
				$("button[title|='Abbrechen']").text("Cancel");
			}

			if ($("div[class|='alert alert-block alert-warning']")) {
				$("div[class|='alert alert-block alert-warning']").text("There are no records to display.");
			}

			if ($("div[class|='modal-header']").length != 0) {
				$("div[class|='modal-header']").find("button").attr("title", "Close");
			}


			setTimeout(function () {
				if ($("input[placeholder|='Suchen']")) {
					$("input[placeholder|='Suchen']").attr("placeholder", "Search");
				}

				if ($("div[class|='content-panel panel panel-default']")) {
					var div = $("div[class|='content-panel panel panel-default']");
					var h2 = div.find("h2");
					h2.text("Selected Records");
				}
			}, 3000);
		}

		self.OnLoad = function () {

			if (location.href.indexOf("eu-EU") === -1) {
				return;
			}
			TranslateLookupModals();

		}
	}

	$(document).ready(function () {

		self.DateTimePicker.OnLoad();
		self.SharepointHandler.OnLoad();
		self.Translate.OnLoad();

		$('.previewCMS').hide();
		var c,
			currentScrollTop = 0,
			navbar = $('#header');

		if (window.matchMedia('min-width: 1440px').matches) {
			$(window).scroll(function () {
				var a = $(window).scrollTop();
				var b = navbar.height();

				currentScrollTop = a;

				if (c < currentScrollTop && a > b + b) {
					navbar.addClass("scrollUp");
				} else if (c > currentScrollTop && !(a <= b)) {
					navbar.removeClass("scrollUp");
				}
				c = currentScrollTop;
			});
		}

		$('[data-toggle="tooltip"]').tooltip();

		$('input[type="file"]').each(function () {
			var $input = $(this),
				$label = $input.parent().prev().children('label'),
				labelVal = $label.html();

			$input.attr('data-multiple-creation', '{count} files selected');
			$input.on('change', function (e) {
				var fileName = '';

				if (this.files && this.files.length > 1)
					fileName = (this.getAttribute('data-multiple-caption') || '').replace('{count}', this.files.length);
				else if (e.target.value)
					fileName = e.target.value.split('\\').pop();

				if (fileName)
					$label.after('<span class="label__file">' + fileName + '</span>');
				else
					$label.html(labelVal);
			});

			// Firefox bug fix
			$input
				.on('focus', function () { $input.addClass('has-focus'); })
				.on('blur', function () { $input.removeClass('has-focus'); });
		});
	});
};
