//////////////////////////////////////////////////////////////////////////////
// Break out of any frames we happen to be in; they're no longer used anywhere on the site
//////////////////////////////////////////////////////////////////////////////

if (top.location.href != document.location.href) {
	if (typeof(bNoFrameBuster) != "undefined" && !bNoFrameBuster) {
		top.location.href = document.location.href;
	}
}

//////////////////////////////////////////////////////////////////////////////
// Hide items in the page relating to booking but not if there is a chance it came from an external page (ie search engines result pages)
//////////////////////////////////////////////////////////////////////////////
if ( (String(document.location.search).indexOf('booking=off') > -1) && (!getDomainName(document.referrer) || isVirginURL(document.referrer))) {
	document.write('<style type="text/css">\n');
	document.write('.bookingrelated { display: none; }\n');
	document.write('</style>\n');
}

/////////////////////////////////////////////////////////////////////////
// 	Extending the array functionality if needed
/////////////////////////////////////////////////////////////////////////
if ( String( typeof Array.prototype.push ) == "undefined" )
{
	Array.prototype.push = function( item )
	{
		this[ this.length ] = item;
	}
}

//////////////////////////////////////////////////////////////////////////////
// Is the given URL a vhols one?
//////////////////////////////////////////////////////////////////////////////
function isVirginURL( url ) {
	testurl = String(url).toLowerCase();
	//TODO: this needs improving
	if ( testurl.indexOf( "virgin" ) > -1 ) return true;
	if ( testurl.indexOf( "atopvweb" ) > -1 ) return true;
	if ( testurl.indexOf( "ecom" ) > -1 ) return true;

	return false;
}

//////////////////////////////////////////////////////////////////////////////
// Gets just the domain name of the passed in URL
//////////////////////////////////////////////////////////////////////////////
function getDomainName(url) {

	// Returns just the domain name minus http:// and page names etc.
	var r = /\w+:\/\/([^\/]+)/,
		m = r.exec(url);

	if (m) {
		return m[1];
	}
	else {
		return null;
	}
}

//////////////////////////////////////////////////////////////////////////////
// Jump to a URL selected from a drop-down box
//////////////////////////////////////////////////////////////////////////////
function jump(formname, fieldname) {
	var f = eval('document.forms[formname].' + fieldname);
	if (f.selectedIndex && f.options[f.selectedIndex].value) document.location.href = f.options[f.selectedIndex].value;
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}

//////////////////////////////////////////////////////////////////////////////
// Open callcentre details in a new window
//////////////////////////////////////////////////////////////////////////////
function callcentre() {
	var remote = window.open("../pages/callcentre.html", "callcentre", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=300,height=300");
	remote.focus();
}


//////////////////////////////////////////////////////////////////////////////
// Open excursion details in a new window
//////////////////////////////////////////////////////////////////////////////
function excursion(site, code) {
	var re = /([^a-z0-9])/i;

	code = String(code).replace(/\s$/,"");
	// Replace all non alphanumerics with xNN, where NN is Hexadecimal ASCII code
	while (re.test(code)) {
		code = code.replace(re, 'x' + baseConverter(String(re.exec(code)[1]).charCodeAt(0), 10, 16));
	}

	excursionWindow = window.open('/' + site + '/excursions/ex_' + code + '.shtml', 'price','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=300');

	excursionWindow.focus();
}


//////////////////////////////////////////////////////////////////////////////
// Generic image cycler
//////////////////////////////////////////////////////////////////////////////
function PicCycler(imgname, pathstart, pathend, currentindex, maxindex, delay) {

	// See if the image load finished yet (avoid case where image will never swap if it takes too long to download, because the download is cancelled by the next swap)
	if (document.images[imgname].complete) {
		// Image loaded, so OK to swap now
		if (++currentindex > maxindex) currentindex = 1;
		document.images[imgname].src = pathstart + currentindex + pathend;
	}
	setTimeout("PicCycler('" + imgname + "', '" + pathstart + "', '" + pathend + "', " + currentindex + ", " + maxindex + ", " + delay +")", delay);
}

//////////////////////////////////////////////////////////////////////////////
// Generic cookie functions
//////////////////////////////////////////////////////////////////////////////
function setCookie(name, value, expires, path) {

	var thisCookie = new String(escape(name) + '=' + escape(value)),
		days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'),
		months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

	if (expires) {
		expires = new Date(expires);
		thisCookie += ';EXPIRES=' +
						days[expires.getUTCDay()] + ', ' +
						(expires.getUTCDate() < 10 ? '0' : '') + expires.getUTCDate() + '-' +
						months[expires.getUTCMonth()] + '-' +
						String(expires.getUTCFullYear()).substring(1) + ' ' +
						(expires.getUTCHours() < 10 ? '0' : '') + expires.getUTCHours() + ':' +
						(expires.getUTCMinutes() < 10 ? '0' : '') + expires.getUTCMinutes() + ':' +
						(expires.getUTCSeconds() < 10 ? '0' : '') + expires.getUTCSeconds() +
						' GMT';
	}

	if (path) {
		thisCookie += ';PATH=' + path;
	}

	document.cookie = thisCookie;
}


function getCookie(name) {

	var c = new String(document.cookie).split(/; /),
		p;

	for (var i = 0; i < c.length; i++) {
		p = c[i].split('=');
		if (p[0] == escape(name)) return unescape(String(p[1]).replace(/\+/g, ' '));
	}
	return '';
}


function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}


//////////////////////////////////////////////////////////////////////////////
// Useful utility functions
//////////////////////////////////////////////////////////////////////////////
function baseConverter(number,ob,nb) {
	// Created 1997 by Brian Risk.  http://members.aol.com/brianrisk
	number = String(number).toUpperCase();  // Line patched by Russ
	var list = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var dec = 0;
	for (var i = 0; i <=  number.length; i++) {
		dec += (list.indexOf(number.charAt(i))) * (Math.pow(ob , (number.length - i - 1)));
	}
	number = "";
	var magnitude = Math.floor((Math.log(dec))/(Math.log(nb)));
	for (var i = magnitude; i >= 0; i--) {
		var amount = Math.floor(dec/Math.pow(nb,i));
		number = number + list.charAt(amount);
		dec -= amount*(Math.pow(nb,i));
	}
	return number;
}

//////////////////////////////////////////////////////////////////////////////
// Check to see if a date is valid.
// Dates accepted in ATOP format (20 Feb 2005)
//////////////////////////////////////////////////////////////////////////////
function isValidDate(dateStr) {
	var millis = Date.parse(dateStr);
	if(isNaN(millis)) return false; // If not a number return false

	// Check the parsed day value against the raw.
	var date = new Date(millis);
	var parsedDay = date.getDate();
	var rawDay = parseInt(dateStr.substr(0, dateStr.indexOf(" ")));
	if(rawDay != parsedDay) return false;
	else return true;
}


//////////////////////////////////////////////////////////////////////////////
// Various Dreamweaver utility functions
//////////////////////////////////////////////////////////////////////////////
function MM_preloadImages() {
	var d = document;
	if(d.images)  {
		if(! d.MM_p) {
			d.MM_p = new Array();
		}
		var j = d.MM_p.length,
			a = MM_preloadImages.arguments;
		for(var i = 0; i < a.length; i++) {
			if (a[i].indexOf("#") != 0) {
				d.MM_p[j] = new Image;
				d.MM_p[j++].src = a[i];
			}
		}
	}
}
function MM_openBrWindow( theURL, winName, features ) { //v2.0
	window.open(theURL,winName,features);
}
function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.0
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && document.getElementById) x=document.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function changeImage(whichimage, mypath) {
	document.images[whichimage].src = mypath;
}

///////////////////////////////////////////////////////////////////////////
// Good^H^H^H^H Assert functions -- Tobin
///////////////////////////////////////////////////////////////////////////
function assert( fact, message ){
	if ( ! fact ){
		var msg = "Assert Failure!" + message;
		if( arguments.callee.caller != null ){
			msg += "\nFailure in function: " + arguments.callee.caller.toString().match(/function\s+(\w+)/)[1] + "(...)";
		}
		alert(msg);
	}

}

function assert( fact ){
	assert( fact, "No Message Given" );
}


/////////////////////////////////////////////////////////////////////////
//	Gets the value of the selected option for the supplied dropdown.
/////////////////////////////////////////////////////////////////////////
function SelectedValue(dropdown) {
	return dropdown.options[dropdown.selectedIndex].value;
}

/////////////////////////////////////////////////////////////////////////
//	Gets the text of the selected option for the supplied dropdown.
/////////////////////////////////////////////////////////////////////////
function SelectedText(dropdown) {
	return dropdown.options[dropdown.selectedIndex].text;
}

/////////////////////////////////////////////////////////////////////////
// Clears dropdown
/////////////////////////////////////////////////////////////////////////
function ClearDropDown(dropdown, dummyText) {
	dropdown.options.length = 0;
	if(dummyText) dropdown.options[dropdown.options.length] = new Option(dummyText, "");
}

/////////////////////////////////////////////////////////////////////////
//	Nav Bar Scripts
/////////////////////////////////////////////////////////////////////////
function navbarShow(vMenuItem,vMenugfk) {
	document.getElementById(vMenuItem).oldsrc = document.getElementById(vMenuItem).src ;
	document.getElementById(vMenuItem).src = vMenugfk;
}

function navbarRestore(vMenuItem) {
	document.getElementById(vMenuItem).src = document.getElementById(vMenuItem).oldsrc;
}


//////////////////////////////////////////////////////////////////////////////
// Check a given checkbox (if present) is ticked; alert if not
//////////////////////////////////////////////////////////////////////////////
function checkterms(el) {
	if (el) {
		if (el.checked) {
			return true;
		} else {
			alert("You must agree to the additional terms before proceeding with this booking");
			return false;
		}
	}
}

