/**
 * BLAU.DE
 * Create global and section namespaces.
 */

var BLAU = {};
	BLAU.SERVICE = {};

var SHARED = {};
	SHARED.PROCESS = {};

/**
 * Blau.Service: initAccordion
 * Accordion effect for FAQ (defintion) lists. 
 *
 * @param accordionClassName The definition lists' (dl) class name
 */
BLAU.SERVICE.initAccordion = function(accordionClassName) {
	jQuery(document).ready(function() {
		// Hide all dd's that are not active
		jQuery('.' + accordionClassName + ' > dt > a[class!="on"]').parent().next('dd').hide();
		
		jQuery('.' + accordionClassName + ' > dt > a').click(function(event) {
			event.preventDefault();
			
			if (jQuery(this).hasClass('on')) {
				// Make this link look inactive and hide dd
				jQuery(this).removeClass('on');
				jQuery(this).parent().next('dd').hide();
			} else {
				// Hide all visible dd and show current
				jQuery('.' + accordionClassName + ' > dd:visible').hide();
				jQuery(this).parent().next().show();
				
				// Make this link look active
				jQuery('.' + accordionClassName + ' > dt > a').removeClass('on');
				jQuery(this).addClass('on');
			}
			
			// If this click opens a theme, close all related questions
			if (jQuery(this).parent().next('dd').children('dl')) {
				jQuery(this).parent().next('dd').children('dl').children('dt').children('a').removeClass('on');
				jQuery(this).parent().next('dd').children('dl').children('dd').hide();
			}
		});
	
		// Open theme and question, if anchor is given
		anchorName=location.href.split('#')[1];		
		if (anchorName!=null&&anchorName!='') {
			themeName=anchorName.substr(0,anchorName.indexOf("_",anchorName.indexOf("_")+1));	
			jQuery('a[name="'+themeName+'"]').addClass('on');
			jQuery('a[name="'+themeName+'"]').parent().next('dd').show();
			jQuery('a[name="'+anchorName+'"]').addClass('on');
			jQuery('a[name="'+anchorName+'"]').parent().next('dd').show();
			
			// Jump to selected theme
			jQuery(this).scrollTo('a[name="'+themeName+'"]');
		}

	});
}

/**
 * Blau.Service: initMoreSearchResults
 * Hide more search results info/link and just show them.
 */
BLAU.SERVICE.initMoreSearchResults = function() {
	jQuery(document).ready(function() {
		if (jQuery('#moreSearchResults') && jQuery('#moreSearchResultsLink'))
		{
			jQuery('#allSearchResultsInfo').hide();
			jQuery('#firstSearchResultsInfo').show();
			jQuery('#moreSearchResults').hide();
			jQuery('#moreSearchResultsLink').show();
			
			// Activate button for more search results
			jQuery('#moreSearchResultsLink > a').click(function(event) {
				event.preventDefault();
				
				jQuery('#firstSearchResultsInfo').hide();
				jQuery('#allSearchResultsInfo').show();
				jQuery('#moreSearchResultsLink').hide();
				jQuery('#moreSearchResults').show();
			});
		}
	});
}

/**
 * Blau.Service: processRating
 * Hide rating form and show "Thank You" message after rating an FAQ question.
 *
 * @param linkId The id of the rating link
 */
BLAU.SERVICE.processRating = function(linkId) {
	jQuery('#' + linkId)
		.parents('div.form').hide()
		.next('.thankyou').show();
}

/**
 * Blau.Service: initAddMorerecommendees
 * Show additional Recommendee recommendation inputs.
 */
BLAU.SERVICE.initAddMoreRecommendees = function(maxRecommendees) {
	jQuery(document).ready(function() {
		
		jQuery('#recommendationRecommendees > fieldset').each(function(i) {
			firstNameInputId='firstNameInput';
			lastNameInputId='lastNameInput';
			emailInputId='emailInput';
			if (i!=0) {				
				firstNameInputId += '_'+(i-1);
				lastNameInputId += '_'+(i-1);
				emailInputId += '_'+(i-1);
			}
			
			if((jQuery('#'+firstNameInputId).val()+jQuery('#'+lastNameInputId).val()+jQuery('#'+emailInputId).val())!='') {
				jQuery(this).removeClass('hide');
			}
			
		});
	
		jQuery('#addMoreRecommendees').css('display', 'block');
		
		jQuery('#addMoreRecommendees').click(function(event) {
			var foundNext = false;
			
			jQuery('#recommendationRecommendees > fieldset').each(function() {
				if (jQuery(this).hasClass('hide') && foundNext == false) {
					jQuery(this).removeClass('hide');
					foundNext = true;					
					if (this.id == 'RecommendeeList_' + (maxRecommendees-2)) {
						jQuery('#addMoreRecommendees').hide();
						jQuery('#addMoreRecommendees').parent().children('p').show();
					}
				}

			});
			event.preventDefault();
			
			return(false);
		});
	});
}









/**
 * Initializes elements.
 *
 * @param idCollapsedVersion The id of the element of the 'collapsed' version.
 * @param idExpandedVersion The id of the element of the 'expanded' version.
 * @param idCheckbox The id of the checkbox element to get the current state.
 */
function initSwitch(idCollapsedVersion, idExpandedVersion, idCheckbox) {
	if (document.getElementById(idCheckbox).checked) {
		hide(idCollapsedVersion);
		show(idExpandedVersion);    
	} else {
		hide(idExpandedVersion);
		show(idCollapsedVersion);
	}
}

/**
 * Shows an element..
 *
 * Additionally this method allows showing a set of elements, simply use multiple
 * ids as arguments, e.g. show('thisElement', 'thatElement');
 *
 * @param id The id of the element to show.
 */
function show(id) {
	for (var i = 0; i < arguments.length; i++) {
		var element = document.getElementById(arguments[i]);
		if (element) {
			if (element.className == "container") {
				element.style.display = "block";
			} else {
				element.style.display = "inline";
			}
		}
	}
}

/**
 * Hides an element, i.e. sets the class to 'hidden'.
 *
 * Additionally this method allows hiding a set of elements, simply use multiple
 * ids as arguments, e.g. hide('thisElement', 'thatElement');
 *
 * @param id The id of the element to hide.
 */
function hide(id) {
	for (var i = 0; i < arguments.length; i++) {
		var element = document.getElementById(arguments[i]);
		if (element) {
			element.style.display = "none";
		}
	}
}

/**
 * Toggles the visibilities sets hidden elements to show and vice versa. Note that
 * this functionality is based on the "show" and "hide" function in this script.
 *
 * Additionally this method allows toggling a set of elements, simply use multiple
 * ids as arguments, e.g. toggleVisibility('thisElement', 'thatElement');
 *
 * @param id The id of the element to toggle visibility.
 */
function toggleVisibility(id) {
	for (var i = 0; i < arguments.length; i++) {
		var element = document.getElementById(arguments[i]);
		if (element) {
			if (element.style.display == "none") {
				show(element.id);
			} else {
				hide(element.id);
			}
		}
	}
}

/**
 * Hides or shows one element using the hide or show function.
 *
 * @param id The id of the element to hide/show.
 * @param shouldShow if true shows the element, if false hides it.
 */
function showHide(Id, shouldShow) {
	if (shouldShow) {
		show(Id);
	} else {
		hide(Id);
	}
}

// Stores div IDs for radio group switching 
var switchGroups = new Array();

/**
 * Initializes a radio group for switching.
 */
function initRadioSwitchGroup(radioGroupForm, radioGroupName) {
	var radioGroup = radioGroupForm[radioGroupName];
	var switchGroup = switchGroups[radioGroupName];
	for (i in switchGroup) {
		radioButtonId = switchGroup[i][0];
		if (document.getElementById(radioButtonId).checked) {
			radioSwitch(radioGroupName, radioButtonId);
		}
	}
}

/**
 * Adds an id to switch to a radio group.
 */
function addToRadioSwitchGroup(radioGroupName, radioButtonId, expandedFirst, expandedSecond, collapsedFirst, collapsedSecond) {
	if (switchGroups[radioGroupName] == null) {
		switchGroups[radioGroupName] = new Array();
	}
	var radio = new Array();
	radio[0] = radioButtonId;
	radio[1] = new Array(expandedFirst, expandedSecond);
	radio[2] = new Array(collapsedFirst, collapsedSecond);
	switchGroups[radioGroupName].push(radio);
}

/**
 * Switches a radio group to the current selected radio button. 
 */
function radioSwitch(radioGroupName, radioExpandedId) {
	var radios = switchGroups[radioGroupName];
	for (i in radios) {
		var radioId = radios[i][0];
		var expanded = radios[i][1];
		var collapsed = radios[i][2];
		if (radioId == radioExpandedId) {
			show(expanded[0]);
			show(expanded[1]);
			hide(collapsed[0]);
			hide(collapsed[1]);
		} else {
			hide(expanded[0]);
			hide(expanded[1]);
			show(collapsed[0]);
			show(collapsed[1]);
		}
	}
}

// nationality dependent passport/idCard input fields
var currentNationality;
function onNationalityChange(isoCode) {
	if (isoCode == "DE") {
		hide("passportDiv", "passportHelpDiv");
		show("idCardDiv", "idCardHelpDiv");
	}
	else {
		hide("idCardDiv", "idCardHelpDiv");
		show("passportDiv", "passportHelpDiv");
	}
}

/**
 * Prevents multiple clicks on a submit element.
 * This should be used in <code>onclick</code> handler:
 * The submit button is disabled and the proper form is submitted.
 */
function preventMultipleClicks(submitElement) {
	if (submitElement.internalDisabled) {
		return false;
	}
	else {
		submitElement.internalDisabled = true;
		return true;
	}
}

/**
 * Shows ajaxLoaderImage on numberSelection page
 */
function showAjaxLoader() {
	if (document.getElementById('sandGlass') && document.getElementById('switchSelected')) {
		document.getElementById('sandGlass').style.display = 'block';
		document.getElementById('switchSelected').style.display = 'none';
	}
}

/**
 * Hides selection/search related error message if user clicks
 * proposed number/mnp fieldset radiobutton 
 */
function errorRelatedToSelection() {
	jQuery(document).ready(function() {
		jQuery('#selectNumber_adviseRadio').click(function(event) {
			hideErrorRelatedToSelection();
		});
		jQuery('#selectNumber_selectionRadio').click(function(event) {
			showErrorRelatedToSelection();
		});
		jQuery('#selectNumber_mnpRadio').click(function(event) {
			hideErrorRelatedToSelection();
		});
	});
}

function showErrorRelatedToSelection() {
	jQuery('.errorRelatedToSelection').css('display', 'block');
}

function hideErrorRelatedToSelection() {
	jQuery('.errorRelatedToSelection').css('display', 'none');
}

/**
 * Cross-Browser eventListener by someone guy called Scott Andrew
 */
function addEvent(obj, eventType, callFunction, useCaption)
{
	if (obj.addEventListener) {
		obj.addEventListener(eventType, callFunction, useCaption);
		return true;
	} else if (obj.attachEvent) {
		var retVal = object.attachEvent("on"+eventType, callFunction);
		return retVal;
	} else {
		return false;
	}
}

/**
 * Create cookie
 */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

/**
 * Read cookie
 */
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}