﻿function addBookmark(title, url) {
	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;
	}
}

function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		alert('Status: Cound not create XmlHttpRequest Object. Consider upgrading your browser.');
	}
}

function gotoURL(URL) {
	window.location = URL;
}

var __defaultFired = false;
var __nonMSDOMBrowser = false;

function WebForm_FireDefaultButton(event, target) {
	if (!__defaultFired && event.keyCode == 13 && !(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea"))) {
		var defaultButton;
		if (__nonMSDOMBrowser)
			defaultButton = document.getElementById(target);
		else
			defaultButton = document.all[target];

		if (typeof (defaultButton.click) != "undefined") {
			__defaultFired = true;
			defaultButton.click();
			event.cancelBubble = true;

			if (event.stopPropagation) event.stopPropagation();
			return false;
		}

		if (typeof (defaultButton.href) != "undefined") {
			__defaultFired = true;
			eval(defaultButton.href.substr(11));
			event.cancelBubble = true;

			if (event.stopPropagation) event.stopPropagation();
			return false;
		}

	}
	return true;
}

function WebForm_TextBoxKeyHandler() {
	return true;
}

var checkCastleAvailable = getXmlHttpRequestObject();
function checkCastle(imageId, date) {
	if (checkCastleAvailable.readyState == 4 || removeImageReq.readyState == 0) {
		checkCastleAvailable.open("POST", '/Ajax/checkAvailable.aspx', true);
		checkCastleAvailable.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
		checkCastleAvailable.onreadystatechange = function () { checkCastleResponse(castleId, date) };

		var param = 'id=' + imageId;

		removeImageReq.send(param);
	}
}
function checkCastleResponse(castleId, date) {
	if (removeImageReq.readyState == 4) {
		var response = removeImageReq.responseText;
		if (response == "success") {
			alert("yes");
		} else {
			alert("no");
		}
	}
}

window.addEvent('load', function () {
	$$('.newsletter').getElements('input').each(function (item) {
		item.addEvent("focus", function () { if (this.value == this.defaultValue) this.set('value', ''); });
		item.addEvent("blur", function () { if (this.value == '') this.set('value', this.defaultValue); });
		return false;
	});
	if (document.getElementById("checkAvailableDate")) {
		hideBookingElements();
		$('postcodeDropDown').addEvent("change", function () { checkPostCode() });
	};
	if (document.getElementById("selectAnotherCastle")) {
		$('selectAnotherCastle').addEvent("click", function () { selectAnotherCastleBox(); return false });
	}
	new SmoothScroll({ duration: 300 }, window);
	if (document.getElementById("contactName")) {
		new FormCheck('form1');
	}
	if (document.getElementById("contactform")) {
		new FormCheck('form1');
	}
	if (document.getElementById("productTable")) {
		setFeaturedHeights();
	};
	if (document.getElementById("castlePickUp")) {
		$('castlePickUp').disabled = true;
	};
	if (document.getElementById("extraProductsPopup")) {
		$('extraProductsPopup').addEvent("click", function() { loadExtraProductsPopup(); return false });
	};
});

function setFeaturedHeights() {
	var maximumFeaturedHeight = 0;
	var featuredProdsArray = $('productTable').getElements('.featuredPrice');
	featuredProdsArray.each(function (item) {
		if (item.offsetHeight > maximumFeaturedHeight) {
			maximumFeaturedHeight = item.offsetHeight;
		};
	});
	featuredProdsArray.setStyle('height', maximumFeaturedHeight);
	maximumFeaturedHeight = 0;
	var featuredTitleArray = $('productTable').getElements('.featuredTitle');
	featuredTitleArray.each(function (item) {
		if (item.offsetHeight > maximumFeaturedHeight) {
			maximumFeaturedHeight = item.offsetHeight;
		};
	});
	featuredTitleArray.setStyle('height', maximumFeaturedHeight);
}

function hideBookingElements() {
	$('availabilityIndicatorFuture').hide();
	$('availabilityIndicatorYes').hide();
	$('availabilityIndicatorNo').hide();
	$('availabilityIndicatorHoliday').hide();
	$('availabilityIndicatorBooked').hide();
	$('inprogress').hide();
	$('inprogress2').hide();
	$('postcodeDropDown').hide();
	new Fx.Reveal($('totalprice')).dissolve();
}

function checkPostCode() {
	$('inprogress2').show();
	$('totalprice').hide();
	castleID = $('castleid').value;
	checkDate = $('checkAvailableDate').value;
	checkedPostCode = $('postcodeDropDown').value;

	var req = new Request({
		method: 'get',
		url: "/ajax/checkPostcode.aspx",
		data: { 'castleid': castleID, 'date': checkDate, 'postcode': checkedPostCode },
		onComplete: function (response) {
			$('inprogress2').hide();
			if (response == "fail") {
				alert("error - invalid data");
			} else if (response == "invalid id") {
				alert("error - invalid castle");
			} else if (response == "holiday") {
				alert("error - no bookings");
			} else if (response == "unavailable") {
				alert("error - unavailable");
			} else {
				var castlePrice = parseFloat($('castleprice').value);
				var shippingPrice = parseFloat(response);
				$('totalprice').innerHTML = "<div id='shippingPrice'>Delivery, Set Up and Collection Cost: &pound;" + shippingPrice + "</div><div id='castlePrice'> Item Price: &pound;" + castlePrice + "</div><div id='totalPrice'>Total Price: &pound;" + (shippingPrice + castlePrice) + "</div><a href='/booking2.aspx#bookingform' id='clickToBook'>Click Here to Continue</a>";
				new Fx.Reveal($('totalprice')).reveal();
			}
		}
	}).send();
}

function checkAvail() {

	hideBookingElements();

	$('inprogress').show();
	
	var castleID = "";
	var checkDate = "";

	castleID = $('castleid').value;
	checkDate = $('checkAvailableDate').value;

	var req = new Request({
		method: 'get',
		url: "/ajax/checkAvailable.aspx",
		data: { 'castleid': castleID, 'date': checkDate },
		onComplete: function (response) {
			new Fx.Reveal($('inprogress')).dissolve({ duration: 300 });
			if (response == "success") {
				new Fx.Reveal($('availabilityIndicatorYes')).reveal();
				var bookingFormSlide = new Fx.Scroll(window).toElement('bookingSlideToHere');
				new Fx.Reveal($('postcodeDropDown')).reveal({ duration: 300 });
			} else if (response == "fail") {
				new Fx.Reveal($('availabilityIndicatorNo')).reveal({ duration: 300 });
			} else if (response == "invalid id") {
				new Fx.Reveal($('availabilityIndicatorNo')).reveal({ duration: 300 });
			} else if (response == "holiday") {
				new Fx.Reveal($('availabilityIndicatorHoliday')).reveal({ duration: 300 });
			} else if (response == "unavailable") {
				new Fx.Reveal($('availabilityIndicatorBooked')).reveal({ duration: 300 });
			}
		}
	}).send();
}

function selectAnotherCastleBox() {
	TINY.box.show('/alternativeCastles.aspx?castleid=' + $('castleid').value + "&date=" + $('checkAvailableDate').value, 1, 800, 0, 1);
	var tinyBoxSlide = new Fx.Scroll(window).toElement('tinycontent');
	$('tinymask').addEvent("click", function () {
		var tinyBoxSlide = new Fx.Scroll(window).toElement('content');
	});
}

function loadExtraProductsPopup() {
	TINY.box.show('/extraProducts.aspx', 1, 800, 0, 1);
}

function bookCastle(castleid, bookdate) {
	window.location = "/Booking.aspx?id=" + castleid + "&date=" + bookdate;
}

function goToCastle(categoryid, castleid) {
	window.location = "/Product-List.aspx?cat=" + categoryid + "#castle" + castleid;
}

function setAppropriateEndDate() {
	$('castlePickUp').disabled = false;

	var searchtime = $('castleDropOff').value;
	searchtime = parseFloat(searchtime.replace(":", ""));
	var howlong = parseFloat($('maxLength').value) * 100;
	var newsearchtime = parseFloat(searchtime) + howlong;
	var finalsearchtime = "0" + newsearchtime;
	finalsearchtime = finalsearchtime.substring(finalsearchtime.length, finalsearchtime.length - 4);
	finalsearchtime = finalsearchtime.substr(0, 2) + ":" + finalsearchtime.substr(2, 2);

	for (var i = 0; i <= $('castlePickUp').length - 1; i = i + 1) {
		$('castlePickUp').options[i].disabled = false;
		var ddlText = document.getElementById('castlePickUp').options[i].value;
		var ddlTextint = parseFloat(ddlText.replace(":", ""));
		if (ddlText != "Overnight") {
			if (ddlTextint == searchtime || ddlTextint < searchtime) {
				$('castlePickUp').options[i].disabled = true;
			};
			if (ddlTextint > newsearchtime) {
				$('castlePickUp').options[i].disabled = true;
			};
			if (ddlText == finalsearchtime) {
				$('castlePickUp').selectedIndex = i;
			}
		}
	}
}

function fillAltDeliveryBoxes() {
	if ($('altDelivery').value == "1") {
		$('altAdd1').value = $('contactAdd1').value;
		$('altAdd2').value = $('contactAdd2').value;
		$('altTown').value = $('contactTown').value;
		var tmpPostCode = $('contactPostcode').value;
		tmpPostCode = tmpPostCode.replace($('pCode1').value, "");
		tmpPostCode = tmpPostCode.replace(" ", "");
		if (document.getElementById("pCode2"))
		{
			$('pCode2').value = tmpPostCode;
		}
	}
}

function checkDetailsAndBook() {
	var errorMessages = "";
	if ($('contactName').value == "") {
		errorMessages = "Contact name must be entered<br />"
	}
	if ($('contactName').value == "") {
		errorMessages = errorMessages + "Contact name must be entered"
	}
}
