/* ###############################################
Contact search Allianz Se Reinsurance Website
#################################################*/
	// Global variables
	var resultElement;
	var countrySelector;
	var statusSelector;
	var businessSelector;
	var selectedIdx = 0;
	var isSelected = false;

	function setGlobals() {
		countrySelector = document.searchContact.pickCountry;
		statusSelector = document.searchContact.pickStatus;
		businessSelector = document.searchContact.pickBusiness;
		resultElement = document.getElementById('dspResult');
		if(resultElement != null) resultElement.style.display = 'none';
	};
	
	// Error Handling
	function exeption(filen, func,e) {
		window.status = 'Error in quickfinder application : ' + e + '. Please check ' + func + ' in file ' + filen + '!';
		return;
	};

	// Setting global variables to find result from field values
	// Calculation/presentation of result in html container of hosting page
	function setValue() {
	 try {
		selectedIdx = countrySelector.selectedIndex;
		// Reset information display
		resultElement.innerHTML = '';

		// Store available field values
		var selectedCountry = countrySelector.options[selectedIdx].text;
		var contactValue = countrySelector.options[selectedIdx].value;
		var statusFlag = 'external'; //statusSelector.options[statusSelector.selectedIndex].value;
		var businessFlag = businessSelector.options[businessSelector.selectedIndex].value;

		// Checking values of all fields
		isSelected = true;
		
		if(statusFlag == 'external' || statusFlag == '') {
			initialValues(externalArr);
			for(var i = 0 ; i < countrySelector.options.length ; i++) {
				if(countrySelector.options[i].text == selectedCountry) {
					countrySelector.options[i].selected = true;
					contactValue = decodeURIComponent(countrySelector.options[i].value);
					break;
				}
				else {
						contactValue =	'<b>No contact available for external Allianz companies in ' +
						selectedCountry +
						'.</b><br><br>' +
						'Please re-select the country.';
				};
			};
		}
		else if(statusFlag == 'internal') {
			initialValues(internalArr);
			for(var i = 0 ; i < countrySelector.options.length ; i++) {
				if(countrySelector.options[i].text == selectedCountry) {
					countrySelector.options[i].selected = true;
					contactValue = decodeURIComponent(countrySelector.options[i].value);
					break;
				}
				else {
						contactValue =	'<b>No contact available for internal Allianz companies in ' +
						selectedCountry +
						'.</b><br><br>' +
						'Please re-select the country.';
				};				
			};
		}
		else {
			isSelected = false;
		};

		if(selectedIdx > 0) {
			switch(businessFlag) {
				case 'Life & Health' : 
					contactValue = contactValue.split('|')[0];
					break;
				case 'Non-Life' : 
					contactValue =  contactValue.split('|')[1];
					break;
				case 'Agricultural Business' : 
					contactValue =  decodeURIComponent(zurich);
					break;
				default : 
					isSelected = false;
					break;
			};
/* old - two bl options only ----
			if(businessFlag == 'Life & Health') {
				contactValue = contactValue.split('|')[0];
			}
			else if(businessFlag == 'Non-Life') {
				contactValue =  contactValue.split('|')[1];
			}
			else {
				isSelected = false;
			};
*/
		}	
		else {
			isSelected = false;
		};

		// No contact note
		if((typeof contactValue == 'undefined' || contactValue == 'null') && isSelected) {
			contactValue =	'<b>Searching for ' +
						businessFlag +
						' products in ' + selectedCountry +
						'......</b><br><br>' +
						'We presently are not able to present contact data ' + 
						'for ' + statusFlag + ' companies.';
		};
		
		// Show contact information
		if(isSelected && resultElement != null && typeof contactValue != 'undefined') {
				resultElement.innerHTML = contactValue;
		};
	 }
	 catch(e) {
		exeption('application.js', 'setValue()',e);
	 };
	};	
	
	// Remove elements / reset list field exept of "-- Please select --"
	function clearElements(fldObj) {
	 try {
		for(var i = fldObj.options.length ; i > 0 ; i--) {
			fldObj.options[i] = null;
		};
	 }
	 catch(e) {
		exeption('application.js', 'clearElements()',e);
	 };
	};
	
	// Add new option element to selection field
	function addOption(fldObj,txt,val) {
	 try {
		var newElement = new Option(txt,val,false,false);
		fldObj.options[fldObj.length] = newElement;
	 }
	 catch(e) {
		exeption('application.js', 'addOption()',e);
	 };
	};
	
	// Fill country list field initially
	function initialValues(arr) {
	 try {
		clearElements(countrySelector);
		if(arr != null && arr.length > 0) {
			for(var i = 0 ; i < arr.length ; i++) {
				addOption(countrySelector,arr[i].name,arr[i].lifehealth + '|' + arr[i].nonlife);
			};
		}
		else {
			exeption('application.js', 'initialValues()','No valid data array or no values in data array');
		}; 
	 }
	 catch(e) {
		exeption('application.js', 'initialValues()',e);
	 };
	};
	
	// Onload handling
	function doload() {
		setGlobals();
		initialValues(externalArr);
	};
