function directTextFieldEnterPressed(event) {
	var inputs = document.getElementsByTagName('input');
	var directInput = null;
	for (i = 0; i < inputs.length; i++) {
		if (inputs[i].id.match('keyWordSearchInput')) {
			directInput = inputs[i];
		}
	}
	if (directInput != null && event.keyCode == 13 && false) {
		directInput.nextSibling.onclick(event);
	}
}
function hideEmptyDivs() {
	var divs = document.getElementsByTagName('div');
	for (i = 0; i < divs.length; i++) {
		if (divs[i].id.match('compareItemDiv') && divs[i].childNodes.length == 0) {
			divs[i].style.display = 'none';
		}
	}
}
function openCharacteristics() {
	new Effect.toggle('characteristics'+'_container','blind', { duration: 0.3 });
	$('characteristics_header').className = 'selected';
}
function giveEveryEvenComapreDivCssClass() {
	var allDivs = document.getElementsByTagName('div');
	var compareDivs = new Array();
	var j = 0;
	for (i = 0; i < allDivs.length; i ++) {
		if (allDivs[i].id.match('compareItemDiv') && allDivs[i].firstChild != null) {
			compareDivs[j++] = allDivs[i];
		}
	}
	for (i = 0; i < compareDivs.length; i++) {
		if ((i % 2) == 1) {
			compareDivs[i].className = compareDivs[i].className + ' comaprisonChartBackground';
		}
	}
}
function enableOrDisableCompareCheckboxesOnPopup(id, chckbx) {
	enableOrDisableCompareCheckboxes(chckbx.checked, id);
}
function enableOrDisableCompareCheckboxes(value, id) {
	var inputs = document.getElementsByTagName('input');
	for (i = 0; i < inputs.length; i++ ) {
		if (inputs[i].id.match(id)) {
			if(!inputs[i].disabled){
				inputs[i].checked = value;
			}	
		}
	}
}
function openConfigurationPopupForEmail() {
	openConfigurationPopupForPDF();
	var tds = document.getElementsByTagName('td');
	for (i = 0; i < tds.length; i++) {
		if (tds[i].id.match('emailElement')) {
			tds[i].style.display = '';
			child = tds[i].firstChild;
			if (child != null && (child.nodeName == 'input' || child.nodeName == 'textarea' || child.nodeName == 'INPUT' || child.nodeName == 'TEXTAREA')) {
				child.value = '';
				
			}
		}
		if (tds[i].id.match('printElement')) {
			tds[i].style.display = 'none';
		}
	}
	if ($('emailButton') != null) {
		$('emailButton').style.display = '';
	} 
	if ($('printButton') != null) {
		$('printButton').style.display = 'none';
	}
	showBlackout();
}
function closeConfigurationPopupForPDF() {
	var popupDiv = document.getElementById('pdfPopup');
	popupDiv.style.display = 'none';
	var tds = document.getElementsByTagName('td');
	for (i = 0; i < tds.length; i++) {
		if (tds[i].id.match('emailElement')) {
			tds[i].style.display = 'none';
		}
	}
	if ($('emailButton') != null) {
		$('emailButton').style.display = 'none';
	} 
	if ($('printButton') != null) {
		$('printButton').style.display = '';
	}
	hideBlackout();
}
function openConfigurationPopupForPDF(){
	var popupDiv = document.getElementById('pdfPopup');
	popupDiv.style.display = 'block';
	var scroll = findContainerDiv().scrollTop;
	popupDiv.style.top = 200 + (scroll) + 'px';
	showBlackout();
}


function closeConfigurationPopupForPDFOpenEmailSentMsg() {
	var popupDiv = document.getElementById('pdfPopup');
	popupDiv.style.display = 'none';
	var popupDivEmailSent = document.getElementById('pdfPopupEmailSent');
	popupDivEmailSent.style.display = 'block';
}
function closeConfigurationEmailSent() {
	var popupDiv = document.getElementById('pdfPopupEmailSent');
	popupDiv.style.display = 'none';
	var tds = document.getElementsByTagName('td');
	for (i = 0; i < tds.length; i++) {
		if (tds[i].id.match('emailElement')) {
			tds[i].style.display = 'none';
		}
	}
	if ($('emailButton') != null) {
		$('emailButton').style.display = 'none';
	} 
	if ($('printButton') != null) {
		$('printButton').style.display = '';
	}
	hideBlackout();
}
function openPDFInNewWiwndowComparison(module){
	var allInputs = document.getElementsByTagName('input');
	var indicator = null;
	for(var index=0; index<allInputs.length; index++) {
		if (allInputs[index].id.match('checkboxPressedIndicator')) {
			indicator = allInputs[index];
		}
	}
	if (indicator != null && indicator.value == 'true') {	
		var hostAddr =  getApplicationContext();
		var endAddress = hostAddr + '/PDF/' + module;
		window.open(endAddress, 'printWindow', 'resizable=true, scrollbars=true, width=800, height=420');
		closeConfigurationPopupForPDF();
	}
}
function openPdf(context,fileName){
	var endAddress = context + '/PDF?pdfFileName='+ fileName;
	window.open(endAddress, 'printWindow', 'resizable, scrollbars, width=800, height=420');
}

function openHotelDetailPdf(context,fileName){
	var endAddress = context + '/PDF?pdfFileName='+ fileName + '&isHotelDetail=true';
	window.open(endAddress, 'printWindow', 'resizable, scrollbars, width=800, height=420');
}

function openPdfBrochure(context,fileName){
	var endAddress = context + '/PDF?pdfFileName='+ fileName+'&isBrochure=true';
	window.open(endAddress, 'printWindow', 'resizable, scrollbars, width=800, height=420');
}
function getCompareCheckboxes() {
	var allInputs = document.getElementsByTagName('input'); //all inout tags
	var checkBoxes = new Array(); //here there will be only compare checkboxes
	var j = 0; //this loop fills the 'checkBoxes' array
	for (i = 0; i < allInputs.length; i ++) {
		if (allInputs[i] != null && allInputs[i].id != null) {
			if (allInputs[i].id.match('compareCheckbox') != null) {
				checkBoxes[j] = allInputs[i];
				j++;
			}
		}
	}
	return checkBoxes;
}
function getAccoOsiCodesInComaSeparatedList() {
	var checkBoxes = getCompareCheckboxes();
	var returnStr = '';
	for (i = 0; i < checkBoxes.length; i ++) {
		if (checkBoxes[i] != null && checkBoxes[i].checked == true) {
			returnStr = returnStr + ',' + checkBoxes[i].title;
		}
	}
	return returnStr.substring(1, returnStr.length);
}
function getExternalImagePath() {
	var imagePath = "";
	var object = $('jsLabels:extImages');
	
	if(object != null){
		imagePath = object.value;
	}
	
	return imagePath;
}
function addCompareButton(target) {
	var anchor = document.createElement('a');
	var hostAddr = getApplicationContext();
	anchor.setAttribute('href', hostAddr + '/pages/comparison/comparisonPage.faces?compareOsi=' + getAccoOsiCodesInComaSeparatedList());
	anchor.setAttribute('onclick', "submitRedirection('results');");
	var image = document.createElement('img');
	image.setAttribute('src', getExternalImagePath() + '/orangButtonVergelijk.gif');
	target.appendChild(anchor);
	anchor.appendChild(image);
}
//function gets a value from a hidden field on the page, which should be the app context taken from the webapp
function getApplicationContext() {
	var allInputs = document.getElementsByTagName('input');
	for(var index=0; index<allInputs.length; index++) {
		if (allInputs[index].id.match('appContext')) {
			return allInputs[index].value;
		}
	}
	alert('No app context input hidden on the page! App context not available!');
}
//function checks wether any of the pdf checkboxes is checked
//is it is not so the pdf button is grayed out
function isAnyCheckboxEnabled() {
	var allInputs = document.getElementsByTagName('input');
	var pdfCheckboxes = new Array();
	var j = 0;
	for(var index=0; index<allInputs.length; index++) {
		if (allInputs[index].id.match('compareCheckbox') || allInputs[index].id.match('mapCheckbox')) {
			pdfCheckboxes[j] = allInputs[index];
			j++;
		}
	}
	var found = false;
	for(var index=0; index<pdfCheckboxes.length; index++) {
		if (pdfCheckboxes[index].checked && !pdfCheckboxes[index].disabled) {
			found = true;
		}
	}
	if (pdfCheckboxes.length == 0) {
		found = true;
	}	
	if (isEmailPopupShown() && found) {
		found = isValidEmail(getPDFButton('emailAdres').value);
	}
	if (found) {
		putNormalImageOnPDFButton();
	} else {
		putGrayedOutImageOnPDFButton();
	}
}

function checkIfHotelDetailMailCanBeSend(){
	
	var mailAddressInput = document.getElementById('mailForm:mailAddress');
	
	if(mailAddressInput != null){
		var validMailAddress = isValidEmail(mailAddressInput.value);
		
		if (validMailAddress){
			putNormalImageOnPDFButton();
		}else{
			putGrayedOutImageOnPDFButton();
		}	
	}else{
		putGrayedOutImageOnPDFButton();
	}
}

//function clears selection on pdf/email popup
function clearPopupSelection() {
	var allInputs = document.getElementsByTagName('input');
	var pdfCheckboxes = new Array();
	var j = 0;
	for(var index=0; index<allInputs.length; index++) {
		if (allInputs[index].id.match('compareCheckbox') || allInputs[index].id.match('mapCheckbox') || allInputs[index].id.match('allCheckbox')) {
			pdfCheckboxes[j] = allInputs[index];
			j++;
		}
	}
	var found = false;
	for(var index=0; index<pdfCheckboxes.length; index++) {
		pdfCheckboxes[index].checked = false;
	}
}
//Function puts a normal looking image on the pdf button
function putNormalImageOnPDFButton() {
	var button = getPDFButton('pdfPrintButton');
	if (button != null) {	
		button.src = getExternalImagePath() + '/btn-print.gif';
	}
	var button = getPDFButton('emailSendButton');
	if (button != null) {
		button.src = getExternalImagePath() + '/btn-email.gif';
		button.removeAttribute('disabled');
	}
}
//Function puts a inactive looking image on the pdf button
function putGrayedOutImageOnPDFButton() {
	var button = getPDFButton('pdfPrintButton');
	if (button != null) {	
		button.src = getExternalImagePath() + '/btn-print-pale.gif';
	}
	var button = getPDFButton('emailSendButton');
	if (button != null) {
		button.src = getExternalImagePath() + '/btn-email-pale.gif';
		button.disabled = 'true';
	}
}
//Function returns the reference to the PDF printing button
function getPDFButton(name){
	var allInputs = document.getElementsByTagName('input');
	for(var index=0; index<allInputs.length; index++) {
		if (allInputs[index].id.match(name)) {
			return allInputs[index];
		}
	}
}
//Function checks whether the is the emailSendButton displayed and therefore
//the email popup (not the pdf one) shown
function isEmailPopupShown() {
	var allInputs = document.getElementsByTagName('div');
	var emailButton = null;
	for(var index=0; index<allInputs.length; index++) {
		if (allInputs[index].id.match('emailButton')) {
			emailButton = allInputs[index];
		}
	}
	if (emailButton != null && emailButton.style.display != 'none') {
		return true;
	} else {
		return false;
	}
}	
//Function validates an email adress
function isValidEmail(emailAddress) {
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return re.test(emailAddress);
}





