/*
 * Jquery actions
 */
$(document).ready(function() {

	$(document).ajaxStart($.blockUI).ajaxStop($.unblockUI);

	showPackageType();

	if (!$('#departure_date').val()) {
		$('#departure_date').val("dd/mm/yyyy");
	}
	if (!$('#return_date').val()) {
		$('#return_date').val("dd/mm/yyyy");
	}

	$("#country").change(function() {
		var country = $(this).val() ? $(this).val() : '';
		var resort = $("#resort").val();
		var resorts = null;
		$.getJSON('/en/search/getbycountry?countries='+country, function(j) {
			if(j) {
				var options = '<option label="Select" value="0">Select</option>';
				for(var i = 0; i < j.length; i++) {
					var added = null;
					if(j[i].id == resort) {
						added = "selected = 'selected'";
					}
					options += '<option value="' + j[i].id + '"' + added +'>' + j[i].name + '</option>';
					resorts = resorts + "," + j[i].id;
				}
				$('#resort').html(options);					
			}
		});
	}).change();	

	$("#mainmenu li").mouseover(function() {
		$(this).find("ul").attr('style', 'width: 160px; visibility: visible;');
	}).mouseout(function() {
		$(this).find("ul").attr('style', 'width: 160px; visibility: hidden;');
	});

	$(".searchlink").click(function (event) {
			$.blockUI({ message: '<div id="progress"><center><img src="/images/web/progress-logo.jpg"/></center><br/>Your holiday is on its way in just a moment<img align="center" src="/images/web/loadbar.gif"/></div>' });
	});

});

/*
 * Trim a value
 */
function trim(str) {
	return str.replace(/^\s+|\s+$/g, "");
}

/*
 * Check length of an object value
 */
function checkLen(value) {
	var val = trim(value);
	return val.length;
}

/*
 * Show hide the top navigation
 */
function showhideMenu(obj, event) {
    var Y = 550;
    var e = window.event || event;
    if(!e.clientY) {
            Y = 100;
    } else {
            Y = e.clientY;
    }
    var X = 100;
    if(!e.clientX) {
            X = 100;
    } else {
            X = e.clientX;
    } 
 
    var childNode = obj.childNodes;
    for (i = 0; i < childNode.length; i++) {
            if (childNode.item(i).tagName == "UL") {
                    var ulNode = childNode.item(i);
                    var visibility = ulNode.style.visibility;
                    if (Y <= 400 &&  X <= 400 && (!visibility || visibility == "hidden")) {
                            ulNode.style.visibility = "visible";
                    } else if(X > 400 && X <= 500 && Y <= 320 && (!visibility || visibility == "hidden")) {
			ulNode.style.visibility = "visible";
		    } else if(X > 500  && Y <= 250 && (!visibility || visibility == "hidden")) { 
			ulNode.style.visibility = "visible";
		    } else {
                            ulNode.style.visibility = "hidden";
                    }
            }
    }
}

/*
 * Blurs the quick search text box
 */
function blurQuickSearchText() {
	if (document.getElementById('searchtext').value == "") {
		document.getElementById('searchtext').value = "Search properties";
		// document.getElementById('Go').setAttribute('disabled','disabled');
	}
	// else if ( document.getElementById('searchtext').value != "" &&
	// document.getElementById('matches').value != 1) {
	// document.getElementById('searchtext').value = "Invalid entry";
	// document.getElementById('searchtext').style.backgroundColor = '#FF0505';
	// document.getElementById('searchtext').style.color = '#FFFFFF';
	// //document.getElementById('Go').setAttribute('disabled','disabled');
	// } else {
	// //document.getElementById('Go').removeAttribute('disabled');
	// }
}

/*
 * Checks the quick search text box
 */
function focusQuickSearchText() {
	if (document.getElementById('searchtext').value != "" && $("#matches").val() == 0) {
		document.getElementById('searchtext').value = "";
		document.getElementById('matches').value = '0';
		document.getElementById('searchtext').style.backgroundColor = '#FFFFFF';
		document.getElementById('searchtext').style.color = '#000000';
		// document.getElementById('Go').setAttribute('disabled','disabled');
	}
}

/*
 * Blurs the newsletter text box
 */
function blurNewsletterText() {
	if (document.getElementById('newsletterbox').value == "") {
		document.getElementById('newsletterbox').value = "Enter e-mail address";
	}
	document.getElementById('newsletterbox').style.backgroundColor = '#FFFFFF';
	document.getElementById('newsletterbox').style.color = '#000000';
}

/*
 * Focus on newsletter textbox
 */
function focusNewsletterText() {
	if (document.getElementById('newsletterbox').value != "") {
		document.getElementById('newsletterbox').value = "";
	}
	document.getElementById('newsletterbox').style.backgroundColor = '#FFFFFF';
	document.getElementById('newsletterbox').style.color = '#000000';
}

/*
 * Ajax add for newsletter
 */
function addNewsletter() {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var email = document.getElementById('newsletterbox').value;
	var url = window.location.protocol + "//" + window.location.host + "/"
			+ 'en/newsletter/add/email/' + email;
	if (checkLen(email) == 0) {
		document.getElementById('newsletterbox').value = 'Please specify your e-mail address.';
		document.getElementById('newsletterbox').style.backgroundColor = '#FF0505';
		document.getElementById('newsletterbox').style.color = '#FFFFFF';
	} else if (reg.test(email) != true) {
		document.getElementById('newsletterbox').value = 'Please check the format of your e-mail addres';
		document.getElementById('newsletterbox').style.backgroundColor = '#FF0505';
		document.getElementById('newsletterbox').style.color = '#FFFFFF';
	} else {
		openModal(url, 'Travelzest Newsletter', 150, 400);
	}
}

/*
 * Open modal window
 */
function openModal(url, title, height, width) {
	var $dialog = $('<div id="modal"></div>');
	$dialog.load(url).dialog( {
		autoOpen : false,
		title : title,
		width : width,
		height : height,
		modal : true
	});
	$dialog.dialog('open');
	return false;
}

/*
 * Open modal map window
 */
function openModalMap(url, title, height, width) {
	var $dialog = $('<div id="mapmodal"></div>');
	$dialog.load(url).dialog( {
		autoOpen : false,
		title : title,
		width : width,
		height : height,
		modal : true
	});
	$dialog.dialog('open');
	return false;
}

/*
 * Open modal form
 */
function openModalForm(url, title, height, width, formId) {
	$("#favlinkfeedbackform").load(url);
	$("#favlinkfeedbackform").dialog( {
		autoOpen : false,
		title : title,
		width : width,
		height : height,
		modal : true,
		buttons : {
			"Submit" : function() {
				$("#" + formId).submit();
			}
		}
	});
	$("#favlinkfeedbackform").dialog('open');
	return false;
}

/*
 * Open modal form
 */
function openModalAvailForm(url, title, height, width, formId) {
	$("#availform").dialog( {
		autoOpen : false,
		title : title,
		width : width,
		height : height,
		modal : true,
		open : function() {
			$(this).load(url);			
		},
		buttons : {
			"Submit" : function() {
				var isValid = $("#travellers_form").valid();
				var bValid = checkRooms();
				if(isValid && bValid) {
					loadProgress();
					var formData = $("#travellers_form").serialize();
					$("#availform").parent().hide();
					window.location.href="/en/search/results/?"+formData;
				}
			}
		}	
	});
	$("#availform").dialog("open");
}

/*
 * Open modal form
 */
function openModalCallMeForm(url, title, height, width, formId) {
	$("#favlinkcallmeform").dialog( {
		autoOpen : false,
		title : title,
		width : width,
		height : height,
		modal : true,
		open : function() {
			$(this).load(url);			
		},
		buttons : {
			"Submit" : function() {
				var isValid = $("#callme_form").valid();
				if(isValid) {
					var formData = $("#callme_form").serialize();
					$(".ui-button").hide();
					$("#favlinkcallmeform").html("Thank you for your interest. One of our travel specialists will call you back.");
				}
			}
		},
		close: function(event, ui) {
			$("#favlinkcallmeform").dialog("close");
		}	
	});
	$("#favlinkcallmeform").dialog("open");
}

/*
 * Open modal form
 */
function openModalMoreInfoForm(url, title, height, width, formId) {
	$("#favlinkmoreinfoform").dialog( {
		autoOpen : false,
		title : title,
		width : width,
		height : height,
		modal : true,
		open : function() {
			$(this).load(url);			
		},
		buttons : {
			"Submit" : function() {
				var isValid = $("#moreinfo_form").valid();
				if(isValid) {
					var formData = $("#moreinfo_form").serialize();
					$(".ui-button").hide();
					$("#favlinkmoreinfoform").html("Thank you for your interest. We will get back to you very soon with the information required");
				}
			}
		},
		close: function(event, ui) {
			$("#favlinkmoreinfoform").dialog("close");
		}	
	});
	$("#favlinkmoreinfoform").dialog("open");
}

/*
 * Open modal form
 */
function openModalEmailForm(url, title, height, width, formId) {
	$("#favlinkemailform").dialog( {
		autoOpen : false,
		title : title,
		width : width,
		height : height,
		modal : true,
		open : function() {
			$(this).load(url);			
		},
		buttons : {
			"Submit" : function() {
				var isValid = $("#email_form").valid();
				if(isValid) {
					var formData = $("#email_form").serialize();
					$(".ui-button").hide();
					$("#favlinkemailform").html("Thank you. The page will be emailed to you shortly.");	
				}
			}
		},
		close: function(event, ui) {
			$("#favlinkemailform").dialog("close");
		}	
	});
	$("#favlinkemailform").dialog("open");
}

/*
 * Open pnieapple rating window
 */
function openModalPineapple() {
	var $dialog = $('<div id="pineapple">Our properties are rated on a 5 point pineapple scale (Pineapples are the universal symbol of hospitality) as a guideline. Many of our properties are selected for their authentic character. Properties may lack the modern amenities that are used by the industry to determine a star rating.<br/><br/>Some properties types like villas and cottages do not have conventional star ratings. We assess the properties and assign a pineapple rating as an indication of the overall quality for that type of property. If, for example, you are looking at a hundred year old French cottage, it may not have the amenities of a 4 star hotel, but we may accord it a 4 pineapple rating due the quality of the furnishings, grounds and its charm relative to other French cottages.</div>');
	$dialog.dialog( {
		autoOpen : false,
		title : "Travelzest Rating",
		width : 400,
		height : 250,
		modal : true
	});
	$dialog.dialog('open');
	return false;
}

/*
 * Open travellers modal
 */
function openModalTravellers(url, title, height, width) {
	var $dialog = $('<div></div>').load(url, checkRooms).dialog( {
		autoOpen : false,
		title : title,
		width : width,
		height : height,
		modal : true,
		buttons : {
			"Submit" : function() {
				var bValid = checkRooms();
				if (bValid) {
					$(this).dialog("close");
					$("#travellers_form").submit();
				}
			}
		}
	});
	$dialog.dialog('open');
	return false;
}

/*
 * Generic show/hide with image replacements
 */
function showHide(id, imageId) {
	var element = document.getElementById(id);
	var imageElement = document.getElementById(imageId);
	if (element.style.display == 'none') {
		element.style.display = 'block';
		imageElement.src = '/images/web/split-icon.jpg';
	} else {
		element.style.display = 'none';
		imageElement.src = '/images/web/add-icon.gif';
	}
}

/*
 * Set opacity for the banner image
 */
function setOpacity(imageobject, opacity) {
	var object = imageobject.style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = 'alpha(opacity=' + opacity + ')';
}

/*
 * Change opacity for the banner image
 */
function ChangeOpacity(id, msDuration, msStart, fromO, toO) {
	var element = document.getElementById(id);
	var opacity = element.style.opacity * 100;
	var msNow = (new Date()).getTime();
	opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;
	if (opacity < 0) {
		setOpacity(element, 0);
	} else if (opacity > 100) {
		setOpacity(element, 100);
	} else {
		setOpacity(element, opacity);
		element.timer = window.setTimeout("ChangeOpacity('" + id + "',"
				+ msDuration + "," + msStart + "," + fromO + "," + toO + ")",
				10);
	}
}

/*
 * Image fade in fade out
 */
function imageFadeIn(currentImgId, newImg, altText, altContainerId,
		containerId, imageAlt, imageTitle) {
	currentImg = document.getElementById(currentImgId);
	if (containerId) {
		var container = document.getElementById(containerId);
		var altContainer = document.getElementById(altContainerId);
		if (container) {
			container.style.backgroundImage = 'url(' + currentImg.src + ')';
			container.style.backgroundRepeat = 'no-repeat';
			altContainer.innerHTML = altText;
		}
	}

	setOpacity(currentImg, 0);

	currentImg.src = newImg;
	currentImg.alt = imageAlt;
	currentImg.title = imageTitle;
	if (currentImg.timer) {
		window.clearTimeout(currentImg.timer);
	}

	var starttime = (new Date()).getTime();
	currentImg.timer = window.setTimeout("ChangeOpacity('" + currentImgId
			+ "',1000," + starttime + ",0,100)", 1);
}

/*
 * Go to a specific anchor
 */
function gotoId(obj) {
	var id = obj.selectedIndex;
	var value = obj.options[id].value;
	window.location = '#' + value;
}

/*
 * Validate Search box
 */
function validateSearch() {
	var countryObj = document.getElementById('country');
	//var airportObj = document.getElementById('airport');
	var resortObj = document.getElementById('resort');
	var packageTypeObj = document.getElementsByName('package_type');
	var departDateObj = document.getElementById('departure_date');
	var returnDateObj = document.getElementById('return_date');
	var errorObj = document.getElementById('formerrors');
	var validated = true;
	var packageTypeObjValue = null;
	for ( var i = 0; i < packageTypeObj.length; i++) {
		if (packageTypeObj[i].checked) {
			packageTypeObjValue = packageTypeObj[i].value;
		}
	}
	
	if(countryObj.value == 0) {
		errorObj.innerHTML = "Please select a destination";
		errorObj.style.display = "block";
		validated = false;
		countryObj.style.backgroundColor = '#FF0505';
		countryObj.style.color = '#FFFFFF';
		return false;
	}	

	if(resortObj.value == 0) {
		errorObj.innerHTML = "Please select a destination";
		errorObj.style.display = "block";
		validated = false;
		resortObj.style.backgroundColor = '#FF0505';
		resortObj.style.color = '#FFFFFF';
		return false;
	}

//	if(airportObj) {
//		if (airportObj.value == 0 && packageTypeObjValue != 'accommodation') {
//			errorObj.innerHTML = "Please select a departure airport";
//			errorObj.style.display = "block";
//			validated = false;
//			airportObj.style.backgroundColor = '#FF0505';
//			airportObj.style.color = '#FFFFFF';
//			return false;
//		} else {
//			airportObj.style.backgroundColor = '#FFFFFF';
//			airportObj.style.color = '#000000';
//		}
//	}
	
	if (departDateObj.value == "" || returnDateObj.value == ""
			|| departDateObj.value == "dd/mm/yyyy"
			|| returnDateObj.value == "dd/mm/yyyy") {
		errorObj.innerHTML = "Please enter a depart and return date";
		errorObj.style.display = "block";
		departDateObj.style.backgroundColor = '#FF0505';
		departDateObj.style.color = '#FFFFFF';
		returnDateObj.style.backgroundColor = '#FF0505';
		returnDateObj.style.color = '#FFFFFF';
		return false;
	} else {
		var departDatePart = departDateObj.value.match(/\d+/g); 
		dyear = departDatePart[2];
		dmonth = departDatePart[1];
		dday = departDatePart[0];
		var returnDatePart = returnDateObj.value.match(/\d+/g);
		ryear = returnDatePart[2];
		rmonth = returnDatePart[1];
		rday = returnDatePart[0];

		var departDateObj1 = new Date();
		departDateObj1.setFullYear(dyear, dmonth - 1, dday);
		var returnDateObj1 = new Date();
		returnDateObj1.setFullYear(ryear, rmonth - 1, rday);
		departDateObj1.setHours(0,0,0,0);
		returnDateObj1.setHours(0,0,0,0);		
		if (returnDateObj1 < departDateObj1) {
			errorObj.innerHTML = "Please select a return date which is after the depart date.";
			errorObj.style.display = "block";
			departDateObj.style.backgroundColor = '#FF0505';
			departDateObj.style.color = '#FFFFFF';
			returnDateObj.style.backgroundColor = '#FF0505';
			returnDateObj.style.color = '#FFFFFF';
			return false;
		} else if (returnDateObj1.valueOf() == departDateObj1.valueOf()) {
			errorObj.innerHTML = "Your return date cannot be the same as your departure date.";
			errorObj.style.display = "block";
			departDateObj.style.backgroundColor = '#FF0505';
			departDateObj.style.color = '#FFFFFF';
			returnDateObj.style.backgroundColor = '#FF0505';
			returnDateObj.style.color = '#FFFFFF';
			return false;
		}
	}

	if (validated) {
		errorObj.innerHTML = "";
		errorObj.style.display = "none";
//		if(airportObj) {
//			airportObj.style.backgroundColor = '#FFFFFF';
//			airportObj.style.color = '#000000';
//		}
		departDateObj.style.backgroundColor = '#FFFFFF';
		departDateObj.style.color = '#000000';
		returnDateObj.style.backgroundColor = '#FFFFFF';
		returnDateObj.style.color = '#000000';
		loadProgress();
		return true;
	} else {
		return false;
	}
}

/*
 * Validate Shortlist Search box
 */
function validateShortlistSearch() {
	var resortObj = document.getElementById('resort');
	//var airportObj = document.getElementById('airport');
	var departDateObj = document.getElementById('departure_date');
	var returnDateObj = document.getElementById('return_date');
	var errorObj = document.getElementById('formerrors');
	var validated = true;
	
	if(resortObj.value == 0) {
		errorObj.innerHTML = "Please select a destination";
		errorObj.style.display = "block";
		validated = false;
		resortObj.style.backgroundColor = '#FF0505';
		resortObj.style.color = '#FFFFFF';
		return false;
	}	

//	if(airportObj) {
//		if (airportObj.value == 0 && packageTypeObjValue != 'accommodation') {
//			errorObj.innerHTML = "Please select a departure airport";
//			errorObj.style.display = "block";
//			validated = false;
//			airportObj.style.backgroundColor = '#FF0505';
//			airportObj.style.color = '#FFFFFF';
//			return false;
//		} else {
//			airportObj.style.backgroundColor = '#FFFFFF';
//			airportObj.style.color = '#000000';
//		}
//	}
	
	if (departDateObj.value == "" || returnDateObj.value == ""
			|| departDateObj.value == "dd/mm/yyyy"
			|| returnDateObj.value == "dd/mm/yyyy") {
		errorObj.innerHTML = "Please enter a depart and return date";
		errorObj.style.display = "block";
		departDateObj.style.backgroundColor = '#FF0505';
		departDateObj.style.color = '#FFFFFF';
		returnDateObj.style.backgroundColor = '#FF0505';
		returnDateObj.style.color = '#FFFFFF';
		return false;
	} else {
		var departDatePart = departDateObj.value.match(/\d+/g); 
		dyear = departDatePart[2];
		dmonth = departDatePart[1];
		dday = departDatePart[0];
		var returnDatePart = returnDateObj.value.match(/\d+/g);
		ryear = returnDatePart[2];
		rmonth = returnDatePart[1];
		rday = returnDatePart[0];

		var departDateObj1 = new Date();
		departDateObj1.setFullYear(dyear, dmonth - 1, dday);
		var returnDateObj1 = new Date();
		returnDateObj1.setFullYear(ryear, rmonth - 1, rday);
		departDateObj1.setHours(0,0,0,0);
		returnDateObj1.setHours(0,0,0,0);		
		if (returnDateObj1 < departDateObj1) {
			errorObj.innerHTML = "Your return date cannot be earlier than your departure date.";
			errorObj.style.display = "block";
			departDateObj.style.backgroundColor = '#FF0505';
			departDateObj.style.color = '#FFFFFF';
			returnDateObj.style.backgroundColor = '#FF0505';
			returnDateObj.style.color = '#FFFFFF';
			return false;
		} else if (returnDateObj1.valueOf() == departDateObj1.valueOf()) {
			errorObj.innerHTML = "Your return date cannot be the same as your departure date.";
			errorObj.style.display = "block";
			departDateObj.style.backgroundColor = '#FF0505';
			departDateObj.style.color = '#FFFFFF';
			returnDateObj.style.backgroundColor = '#FF0505';
			returnDateObj.style.color = '#FFFFFF';
			return false;
		}
	}

	if (validated) {
		errorObj.innerHTML = "";
		errorObj.style.display = "none";
//		if(airportObj) {
//			airportObj.style.backgroundColor = '#FFFFFF';
//			airportObj.style.color = '#000000';
//		}
		departDateObj.style.backgroundColor = '#FFFFFF';
		departDateObj.style.color = '#000000';
		returnDateObj.style.backgroundColor = '#FFFFFF';
		returnDateObj.style.color = '#000000';
		loadProgress();
		return true;
	} else {
		return false;
	}
}

function loadProgress() {
		$.blockUI({ message: '<div id="progress"><center><img src="/images/web/progress-logo.jpg"/></center><br/>Your holiday is on its way in just a moment<img align="center" src="/images/web/loadbar.gif"/></div>' });
}

/*
 * Update ajax divs
 */
function updateAjaxResults(id) {
	var element = $('#' + id);
	if (id == 'topdealsbestsellers') {
		var selectedVal = element.children(":first").text();
		selectedVal = trim(selectedVal);
		var selects = selectedVal.split(',');
		$('#topdealsbestsellersresults').children().each(function() {
			var child = $(this);
			if (child.attr('id')) {
				for ( var i in selects) {
					var v = selects[i];
					if (v == child.attr('id')) {
						child.show();
					} else if (v == 'All Destinations') {
						child.show();
					} else {
						child.hide();
					}
				}
			}
		});
	}
}

/*
 * Add to shortlist
 */
function addShortlist(id, type) {
	var callUrl = window.location.protocol + "//" + window.location.host + "/"
			+ 'en/shortlist/add/type/' + type + '/id/' + id;
	var linkUrl = window.location.protocol + "//" + window.location.host + "/"
			+ 'en/shortlist/index/type/' + type;
	$.get(callUrl, function(data) {
		document.getElementById('shortlistlink').setAttribute('href', linkUrl);
		document.getElementById('shortlistcount').innerHTML = data;
		document.getElementById('addshortlist').style.display = "none";
		document.getElementById('removeshortlist').style.display = "block";
		document.getElementById('addshortlistflash').style.display = "block";
		document.getElementById('removeshortlistflash').style.display = "none";
		$('html, body').animate( {
			scrollTop : 0
		}, 'slow');
		return false;
	});
}

/*
 * Remove from shortlist
 */
function removeShortlist(id, type) {
	var callUrl = window.location.protocol + "//" + window.location.host + "/"
			+ 'en/shortlist/remove/type/' + type + '/id/' + id;
	$
			.get(
					callUrl,
					function(data) {
						document.getElementById('shortlistcount').innerHTML = data;
						document.getElementById('removeshortlist').style.display = "none";
						document.getElementById('addshortlist').style.display = "block";
						document.getElementById('removeshortlistflash').style.display = "block";
						document.getElementById('addshortlistflash').style.display = "none";
						$('html, body').animate( {
							scrollTop : 0
						}, 'slow');
						return false;
					});
}

/*
 * Expand or collapse reviews
 */
function expandReview(id, type) {
	var shortElement = document.getElementById("short_" + id);
	var fullElement = document.getElementById("full_" + id);
	if (type == 'short') {
		shortElement.style.display = 'block';
		fullElement.style.display = 'none';
	} else {
		shortElement.style.display = 'none';
		fullElement.style.display = 'block';
	}
}

/*
 * Expand or collapse service
 */
function expandService(type) {
	var shortElement = document.getElementById("shortreview");
	var fullElement = document.getElementById("fullreview");
	if (type == 'short') {
		shortElement.style.display = 'block';
		fullElement.style.display = 'none';
	} else {
		shortElement.style.display = 'none';
		fullElement.style.display = 'block';
	}
}

/*
 * Expand or collapse service
 */
function expandPrice(type) {
	var shortElement = document.getElementById("shortprice");
	var fullElement = document.getElementById("fullprice");
	if (type == 'short') {
		shortElement.style.display = 'block';
		fullElement.style.display = 'none';
	} else {
		shortElement.style.display = 'none';
		fullElement.style.display = 'block';
	}
}

/*
 * Expand or collapse service
 */
function expandDistance(type) {
	var shortElement = document.getElementById("shortdistance");
	var fullElement = document.getElementById("fulldistance");
	if (type == 'short') {
		shortElement.style.display = 'block';
		fullElement.style.display = 'none';
	} else {
		shortElement.style.display = 'none';
		fullElement.style.display = 'block';
	}
}

function showPackageType() {
	var obj = document.getElementById('country');
	var packageType = document.getElementById("package-type");
	if (obj && packageType) {
		var val = obj.value;
		if (val == 21 || val == 23 || val == 20) {
			document.getElementById("package-type").style.display = "block";
		} else {
			document.getElementById("package-type").style.display = "none";
		}
	}
}

function showChildren() {
	var obj = document.getElementById("rooms");
	var v = obj.value;

	var children = false;
	for (i = 1; i <= 1; i++) {
		var childObj = document.getElementById("children_" + i);
		var childV = childObj.value;
		for (j = 1; j <= childV; j++) {
			children = true;
			var subSubChildObj = document.getElementById("childagediv_" + i + "_"
					+ j);
			subSubChildObj.style.display = "block";
		}
		for (k = 8; k > childV; k--) {
			if(document.getElementById("childagediv_" + i + "_" + k) != null) {
				var subSubChildObj = document.getElementById("childagediv_" + i + "_"
						+ k);
				subSubChildObj.style.display = "none";
			}
		}
	}
	if (children) {
		document.getElementById("children").style.display = "block";
	} else {
		document.getElementById("children").style.display = "none";
	}

}

function checkPax() {
	var totalRoomsObj = document.getElementById("rooms");
	var totalRooms = totalRoomsObj.value;
	var totalCount = 0;
	var errorDiv = document.getElementById("errors");
	var errorLineDiv = document.getElementById("errorsline");
	var errors = false;
	for (i = 1; i <= 1; i++) {
		var adultObj = document.getElementById("adults_" + i);
		var childObj = document.getElementById("children_" + i);
		var adtCount = parseInt(adultObj.value);
		var chdCount = parseInt(childObj.value);
		var infCount = 0;
		var totalInRoom = adtCount + chdCount;
		totalCount += totalInRoom;
		if ((adtCount + chdCount) > 2) {
			for (j = 1; j <= chdCount; j++) {
				var subChildObj = document.getElementById("childage_" + i + "_"
						+ j);
				var childAge = parseInt(subChildObj.value);
				if(childAge >= 2 && !errors && totalRooms == 1) {
					errorDiv.innerHTML += "<br/>There can be a total of 2 guests in a room. Please add another room or amend your party size. For family rooms please call to book";
					errorDiv.style.display = "block";
					errorLineDiv.style.display = "block";
					validated = false;
					adultObj.style.backgroundColor = '#FF0505';
					childObj.style.backgroundColor = '#FF0505';
					adultObj.style.color = '#FFFFFF';
					childObj.style.color = '#FFFFFF';
					errors = true;					
				} else {
					if(childAge > 0 && childAge < 2) {
						infCount++;
					}
				}		
			}
			var allowedpax = totalRooms * 2;
			if((allowedpax < (adtCount + chdCount - infCount)) && !errors) {
				errorDiv.innerHTML += "<br/>There can be a total of 2 guests in a room. Please add another room or amend your party size. For family rooms please call to book";
				errorDiv.style.display = "block";
				errorLineDiv.style.display = "block";
				validated = false;
				adultObj.style.backgroundColor = '#FF0505';
				childObj.style.backgroundColor = '#FF0505';
				adultObj.style.color = '#FFFFFF';
				childObj.style.color = '#FFFFFF';
				errors = true;					
			}
		}
		
		if(chdCount) {
			for (j = 1; j <= chdCount; j++) {
				var childAgeObj = document.getElementById("childage_" + i + "_" + j);
				var childAge = parseInt(childAgeObj.value);
				if(!(childAge > 0)) {
					errorDiv.innerHTML = "<br/>Please specify the age of the children travelling";
					errorDiv.style.display = "block";
					errorLineDiv.style.display = "block";
					childAgeObj.style.backgroundColor = '#FF0505';
					childAgeObj.style.color = '#FFFFFF';
					errors = true;					
				} else {
					childAgeObj.style.backgroundColor = '#FFFFFF';
					childAgeObj.style.color = '#000000';					
				}
			}
		}  
		if(!errors) {
			errorDiv.innerHTML = "";
			errorDiv.style.display = "none";
			errorLineDiv.style.display = "none";
			adultObj.style.backgroundColor = '#FFFFFF';
			childObj.style.backgroundColor = '#FFFFFF';
			adultObj.style.color = '#000000';
			childObj.style.color = '#000000';			
		} else {
			adultObj.style.backgroundColor = '#FFFFFF';
			childObj.style.backgroundColor = '#FFFFFF';
			adultObj.style.color = '#000000';
			childObj.style.color = '#000000';
		}
	}
	if (totalCount > 9) {
		errorDiv.innerHTML += "<br/>Party sizes of more than 9 travellers are considered group bookings and cannot be booked online. Please call our reservations team on 0800 171 2150 and they will be able to help you.";
		errorDiv.style.display = "block";
		errors = true;
		for (i = 1; i <= 1; i++) {
			var adultObj = document.getElementById("adults_" + i);
			var childObj = document.getElementById("children_" + i);
			adultObj.style.backgroundColor = '#FF0505';
			childObj.style.backgroundColor = '#FF0505';
			adultObj.style.color = '#FFFFFF';
			childObj.style.color = '#FFFFFF';
		}
	}
	if (totalCount == 0) {
		errorDiv.innerHTML += "You have not assigned any travellers to this room.";
		errorDiv.style.display = "block";
		errors = true;
		for (i = 1; i <= 1; i++) {
			var adultObj = document.getElementById("adults_" + i);
			var childObj = document.getElementById("children_" + i);
			adultObj.style.backgroundColor = '#FF0505';
			childObj.style.backgroundColor = '#FF0505';
			adultObj.style.color = '#FFFFFF';
			childObj.style.color = '#FFFFFF';
		}
	}

	if (!errors) {
		errorDiv.innerHTML = "";
		errorDiv.style.display = "none";
		errorLineDiv.style.display = "none";
		adultObj.style.backgroundColor = '#FFFFFF';
		childObj.style.backgroundColor = '#FFFFFF';
		adultObj.style.color = '#000000';
		childObj.style.color = '#000000';
	}
	
	return !errors;

}

function checkRooms() {
	
	var departDateObj = document.getElementById('trav_departure_date');
	var returnDateObj = document.getElementById('trav_return_date');
	var errorDiv = document.getElementById("errors");
	var errorLineDiv = document.getElementById("errorsline");
	errorDiv.innerHTML = "";
	errorDiv.style.display = "none";
	errorLineDiv.style.display = "none";
	if (departDateObj.value == "" || returnDateObj.value == ""
			|| departDateObj.value == "dd/mm/yyyy"
			|| returnDateObj.value == "dd/mm/yyyy") {
		errorDiv.innerHTML = "Please enter a depart and return date";
		errorDiv.style.display = "block";
		errorLineDiv.style.display = "block";
		departDateObj.style.backgroundColor = '#FF0505';
		departDateObj.style.color = '#FFFFFF';
		returnDateObj.style.backgroundColor = '#FF0505';
		returnDateObj.style.color = '#FFFFFF';
		return false;
	} else {
		var departDatePart = departDateObj.value.match(/\d+/g);
		dyear = departDatePart[2];
		dmonth = departDatePart[1];
		dday = departDatePart[0];
		var returnDatePart = returnDateObj.value.match(/\d+/g);
		ryear = returnDatePart[2];
		rmonth = returnDatePart[1];
		rday = returnDatePart[0];

		var departDateObj1 = new Date();
		departDateObj1.setFullYear(dyear, dmonth - 1, dday);
		var returnDateObj1 = new Date();
		returnDateObj1.setFullYear(ryear, rmonth - 1, rday);
		departDateObj1.setHours(0,0,0,0);
		returnDateObj1.setHours(0,0,0,0);
		if (returnDateObj1 < departDateObj1) {
			errorDiv.innerHTML = "Your return date cannot be earlier than your departure date.";
			errorDiv.style.display = "block";
			departDateObj.style.backgroundColor = '#FF0505';
			departDateObj.style.color = '#FFFFFF';
			returnDateObj.style.backgroundColor = '#FF0505';
			returnDateObj.style.color = '#FFFFFF';
			return false;
		} else if (returnDateObj1.valueOf() == departDateObj1.valueOf()) {
			errorDiv.innerHTML = "Your return date cannot be the same as your departure date.";
			errorDiv.style.display = "block";
			departDateObj.style.backgroundColor = '#FF0505';
			departDateObj.style.color = '#FFFFFF';
			returnDateObj.style.backgroundColor = '#FF0505';
			returnDateObj.style.color = '#FFFFFF';
			return false;
		}
	}	

	showChildren();
	var validated = checkPax();
	if (validated) {
		errorDiv.innerHTML = "";
		errorDiv.style.display = "none";
		errorLineDiv.style.display = "none";
		departDateObj.style.backgroundColor = '#FFFFFF';
		departDateObj.style.color = '#000000';
		returnDateObj.style.backgroundColor = '#FFFFFF';
		returnDateObj.style.color = '#000000';
		return true;
	} else {
		return false;
	}
	return validated;
}

function createBookmark() {
	var title = document.title;
	var url = window.location.href;

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url, "");
	} else if (window.external) { // IE Favorite
		window.external.AddFavorite(url, title);
	} else if (window.opera && window.print) { // Opera Hotlist
		return true;
	}
}

