

var isIphone = "no";
var isIpad = "no";
var isMobileVersion = "no";

var agt2 = new String(navigator.userAgent.toLowerCase());
if (agt2.indexOf("iphone") != -1) {
	isIphone = "yes";
}
if (agt2.indexOf("ipad") != -1) {
	isIpad = "yes";
}



var t;


function getInsideWindowWidth() {
	isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
	if (window.innerWidth) {
		return window.innerWidth;
	}
	else if (isIE6CSS) {
		// measure the html element's clientWidtht
		return document.documentElement.clientWidth;
	}
	else if (document.body && document.body.clientWidth) {
		return document.body.clientWidth;
	}
	return 1; // 0 could lead to dividing 0 by 2.
}



var mainImageUrl = "";


function resetStyleSheet(mobileVersion) {
	var xBrowserCssRule;
	//alert("start resetStyleSheet");
	
	// If it exists asign parameter to the global var
	if (mobileVersion == "yes") {			// don't change this evaluation - FF4 bug
		isMobileVersion = mobileVersion;
	}
	

	if (document.styleSheets[0].rules) {				// IE
		xBrowserCssRule = document.styleSheets[0].rules
	}
	else if (document.styleSheets[0].cssRules) {		// Mozilla and the others
		xBrowserCssRule = document.styleSheets[0].cssRules
	}



	// in case of resize

	xBrowserCssRule[2].style.visibility = 'hidden';
	
	var winWidth = 0;
	winWidth = getInsideWindowWidth();
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	// live
	if (winWidth < 240 || isMobileVersion == "yes") {	// too small to show normal site
	//
	// testing (bigger value so you can see the mobileVersion div during a FF resize)
	//if (winWidth < 340 || isMobileVersion == "yes") {	// too small to show normal site
	/////////////////////////////////////////////////////////////////////////////////////

		//alert("mobile section");
		
		// show mobileVersion div
		xBrowserCssRule[18].style.visibility = 'visible';
		xBrowserCssRule[18].style.width = '240px';			// avoids long page in normal view

		// hide the normalVersion div
		xBrowserCssRule[19].style.visibility = 'hidden';

		// clear the background, etc.
		xBrowserCssRule[0].style.backgroundImage = '';
		xBrowserCssRule[0].style.backgroundColor = '#ffffff';

		// set the h1 and h2 font sizes/colors to sensible values for a tiny screen
		xBrowserCssRule[20].style.fontSize = '1.2em';
		xBrowserCssRule[21].style.fontSize = '1.1em';
		xBrowserCssRule[22].style.fontSize = '1.0em';
		xBrowserCssRule[20].style.color = '#000000';
		xBrowserCssRule[21].style.color = '#000000';
		xBrowserCssRule[22].style.color = '#000000';


	}
	else {		// NORMAL VERSION

		//alert("normal section")

		// set everything to non-mobile value in case of resize

		// avoids long page in normal view
		xBrowserCssRule[18].style.width = '100%';
		// hide mobileVersion div
		xBrowserCssRule[18].style.visibility = 'hidden';
		//alert(xBrowserCssRule[18].style.visibility)

		// show the normalVersion div
		xBrowserCssRule[19].style.visibility = 'visible';
		xBrowserCssRule[19].style.color = '#ffffff';
		
		// restore the background, etc.
		xBrowserCssRule[0].style.background = '#232651 url(images/1_1024.jpg) no-repeat left top'

		// set the h1 and h2 font sizes/colors back to previous values
		xBrowserCssRule[20].style.fontSize = '';
		xBrowserCssRule[21].style.fontSize = '';
		xBrowserCssRule[22].style.fontSize = '';
		xBrowserCssRule[20].style.color = '#ffffff';
		xBrowserCssRule[21].style.color = '#ffffff';
		xBrowserCssRule[22].style.color = '#ffffff';


		var winWidthMinus10 = winWidth - 20;
		var leftPosTopShiftedRight = 0;

		var leftPosMiddle = parseInt((winWidthMinus10 - 862) / 2);
		if (leftPosMiddle < 0) {
			leftPosMiddle = 0;
		}

		var leftPosFooter = parseInt((winWidthMinus10 - 963) / 2);
		if (leftPosFooter < 0) {
			leftPosFooter = 0;
		}


		var leftPosIcon = 0;

		if (winWidthMinus10 - 1108 > 2) {
			// show icons
			xBrowserCssRule[2].style.visibility = 'visible';
			
			leftPosIcon = parseInt((winWidthMinus10 - 1108) / 2);
			leftPosTopShiftedRight = leftPosIcon + 226;

		}
		else if (winWidthMinus10 - 882 > 2) { // no icons
			leftPosTopShiftedRight = parseInt((winWidthMinus10 - 882) / 2);
		}



		xBrowserCssRule[2].style.left = leftPosIcon + 'px';
		xBrowserCssRule[3].style.left = leftPosTopShiftedRight + 'px';
		xBrowserCssRule[4].style.left = leftPosTopShiftedRight + 'px';
		xBrowserCssRule[5].style.left = leftPosMiddle + 'px';
		xBrowserCssRule[6].style.left = leftPosMiddle + 10 + 'px';
		xBrowserCssRule[7].style.left = leftPosMiddle + 555 + 'px';
		xBrowserCssRule[8].style.marginLeft = leftPosFooter + 'px';
		xBrowserCssRule[9].style.marginLeft = leftPosFooter + 'px';
		xBrowserCssRule[16].style.left = leftPosFooter + 190 + 'px';
		xBrowserCssRule[17].style.left = leftPosFooter + 531 + 'px';
		xBrowserCssRule[10].style.left = leftPosFooter + 'px';
		xBrowserCssRule[11].style.left = 605 + leftPosFooter + 'px';
		xBrowserCssRule[12].style.left = leftPosFooter + 'px';
		xBrowserCssRule[13].style.left = leftPosFooter + 605 + 'px';
		xBrowserCssRule[14].style.marginLeft = 5 + leftPosFooter + 'px';
		xBrowserCssRule[15].style.left = leftPosFooter + 605 + 'px';



		if (winWidth > 1600) {
			var imageURL = 'url(images/' + thisPage + '_1850.jpg)';
		}
		else if (winWidth > 1366) {
			var imageURL = 'url(images/' + thisPage + '_1600.jpg)';
		}
		else if (winWidth > 1280) {
			var imageURL = 'url(images/' + thisPage + '_1366.jpg)';
		}
		else if (winWidth > 1024) {
			var imageURL = 'url(images/' + thisPage + '_1280.jpg)';
		}
		else {
			var imageURL = 'url(images/' + thisPage + '_1024.jpg)';
		}




		if (isIphone == "yes" || isIpad == "yes") { // long pages
			if (thisPage == 9 || thisPage == 10 || thisPage == 11 || thisPage == 12 || thisPage == 13 || thisPage == 14 || thisPage == 18 || thisPage == 21 || thisPage == 22 || thisPage == 23 || thisPage == 24 || thisPage == 25 || thisPage == 27 || thisPage == 28) {
				imageURL = 'url(images/iphone_1024.jpg)';
				xBrowserCssRule[0].style.backgroundAttachment = "fixed";
			}
		}


		mainImageUrl = imageURL; // used in slideshow
		xBrowserCssRule[0].style.backgroundImage = imageURL;

		clearTimeout(t);

		if (thisPage != 1 && thisPage != 20 && thisPage != 9 && thisPage != 10 && thisPage != 11 && thisPage != 12 && thisPage != 13 && thisPage != 14 && thisPage != 18 && thisPage != 23 && thisPage != 24 && thisPage != 25 && thisPage != 27 && thisPage != 22) { // home, video or accommodation

			if (thisPage == 29) {
				t = setTimeout("changePicture29()", 10000);
			}
			else {
				t = setTimeout("changePicture()", 10000);
			}
		}

	}
	//alert("end resetStyleSheet");
}


window.onresize=resetStyleSheet;





function offerOldSite() {
	if (isRunningIE6OrBelow == true) {
		document.getElementById("ie6only").style.visibility = "visible";
		var html5 = '<p>We now have a different version of our website which works with your browser - you can use it by going to <a href="home-explorer.html">www.whalesborough.co.uk/home-explorer.html (click here)</a></p>';
		document.getElementById("ie6only").innerHTML = html5;
	}
}


function correctIphoneIpad() { // on home pages only
	if (isIphone == "yes") {
		document.getElementById("homemaintext").style.fontSize = "0.75em";
	}
}



var lastSlideNo = 1;


function changePicture() {
	if (isIphone == "no" && isIpad == "no" && isMobileVersion == "no") {

		lastSlideNo += 1;

		t = setTimeout("changePicture()", 10000); 
		fade();
	
		var imageURL = "";

		if (lastSlideNo == 5) {
			imageURL = mainImageUrl;
			lastSlideNo = 2;
		}
		else {
			imageURL = 'url(images/slide' + lastSlideNo + '.jpg)';
		}
		document.body.style.backgroundImage=imageURL;

	}
}



function changePicture29() {
	if (isIphone == "no" && isIpad == "no" && isMobileVersion == "no") {

		t = setTimeout("changePicture29()", 10000); 
		fade();
	
		var imageURL = "";

		if (lastSlideNo == 1) { // 2nd pic showing
			imageURL = 'url(images/restaurant_cornwall_2.jpg)';
			lastSlideNo += 1;
		}
		else if (lastSlideNo == 2) { // 3rd pic showing
			imageURL = 'url(images/restaurant_cornwall_3.jpg)';
			lastSlideNo += 1;
		}
		else if (lastSlideNo == 3) { // 4th pic showing
			imageURL = 'url(images/restaurant_cornwall_4.jpg)';
			lastSlideNo += 1;
		}
		else if (lastSlideNo == 4) { // 4th pic showing
			imageURL = 'url(images/restaurant_cornwall_5.jpg)';
			lastSlideNo += 1;
		}
		else if (lastSlideNo == 5) { // 4th pic showing
			imageURL = 'url(images/restaurant_cornwall_6.jpg)';
			lastSlideNo += 1;
		}
		else if (lastSlideNo == 6) { // 4th pic showing
			imageURL = 'url(images/restaurant_cornwall_7.jpg)';
			lastSlideNo += 1;
		}
		if (lastSlideNo == 7) {
			imageURL = mainImageUrl;
			lastSlideNo = 1; // nb =
		}
		document.body.style.backgroundImage=imageURL;

	}
}











///////////////////////// image fade in and outs
//http://www.switchonthecode.com/tutorials/javascript-tutorial-simple-fade-animation




var TimeToFade = 2000.0;


function fade() {

	var element = document.getElementById("fade");

	element.style.opacity = 1;
	element.style.filter = 'alpha(opacity = 100)';


	element.FadeTimeLeft = TimeToFade;




	setTimeout("animateFade(" + new Date().getTime() + ",'" + "fade" + "')", 33); 
}

function animateFade(lastTick, eid) {  
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;
 
	var element = document.getElementById(eid);

	// happens last cycle only
	if (element.FadeTimeLeft <= elapsedTicks) {
		return;
	}

	element.FadeTimeLeft -= elapsedTicks;
	var newOpVal = element.FadeTimeLeft/TimeToFade;

	element.style.opacity = newOpVal;
	element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
 
	setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}






function navigate(choice) {

	if (choice.options[choice.selectedIndex].value == "Family Holidays") {
		var url = "family-breaks.html";
	}
	else if (choice.options[choice.selectedIndex].value == "Romantic Hideaways") {
		var url = "romantic-holidays.html";
	}
	else if (choice.options[choice.selectedIndex].value == "Walking Holidays") {
		var url = "walking-holidays.html";
	}
	else if (choice.options[choice.selectedIndex].value == "Surfing Holidays") {
		var url = "surfing-holidays.html";
	}
	else if (choice.options[choice.selectedIndex].value == "Dog Friendly Accommodation") {
		var url = "dog-friendly-holiday-cottages.html";
	}
	else if (choice.options[choice.selectedIndex].value == "Cycling Holidays") {
		var url = "cycling-holidays.html";
	}
	else if (choice.options[choice.selectedIndex].value == "Wildlife Lovers") {
		var url = "bude-cornwall-wildlife.html";
	}

	if (url) {
		top.location.href = url;
	}
}





function swopIcon(topLinkNo) {


	var theImg = '<img src="images/icon-leaf.png" alt="" />';
	
	if (thisPage == 2) {
		theImg = '<img src="images/icon-family-holidays.png" alt="" />';
	}
	else if (thisPage == 3) {
		theImg = '<img src="images/icon-romantic-breaks.png" alt="" />';
	}
	else if (thisPage == 4) {
		theImg = '<img src="images/icon-walking-holidays.png" alt="" />';
	}
	else if (thisPage == 5) {
		theImg = '<img src="images/icon-board.png" alt="" />';
	}
	else if (thisPage == 6) {
		theImg = '<img src="images/icon-dog-friendly.png" alt="" />';
	}
	else if (thisPage == 7) {
		theImg = '<img src="images/icon-cycling-holidays.png" alt="" />';
	}
	else if (thisPage == 8) {
		theImg = '<img src="images/icon-bude-cornwall-wildlife.png" alt="" />';
	}




	if (topLinkNo == 1) {
		theImg = '<img src="images/icon-family-holidays.png" alt="" />';
	}
	else if (topLinkNo == 2) {
		theImg = '<img src="images/icon-romantic-breaks.png" alt="" />';
	}
	else if (topLinkNo == 3) {
		theImg = '<img src="images/icon-walking-holidays.png" alt="" />';
	}
	else if (topLinkNo == 4) {
		theImg = '<img src="images/icon-board.png" alt="" />';
	}
	else if (topLinkNo == 5) {
		theImg = '<img src="images/icon-dog-friendly.png" alt="" />';
	}
	else if (topLinkNo == 6) {
		theImg = '<img src="images/icon-cycling-holidays.png" alt="" />';
	}
	else if (topLinkNo == 7) {
		theImg = '<img src="images/icon-bude-cornwall-wildlife.png" alt="" />';
	}
	document.getElementById("icons").innerHTML = theImg;
}





function positionMenu() {
	if( typeof( window.pageYOffset ) == 'number' ) { // Mac
		if (window.pageYOffset > 600) {
			document.getElementById("fixedcottagebuttons").style.visibility = "visible";
		}
		else {
			document.getElementById("fixedcottagebuttons").style.visibility = "hidden";
		}
	}
	else {
		if (document.documentElement.scrollTop > 600) {
			document.getElementById("fixedcottagebuttons").style.visibility = "visible";
		}
		else {
			document.getElementById("fixedcottagebuttons").style.visibility = "hidden";
		}

	}



	if (isIphone == "no" && isIpad == "no") {
		setTimeout("positionMenu()", 10); 
	}

}







function checkAndSubmit1(tF) { //tF = ref to form

	var continueOn = "yes";
	if (tF.name.value == "") { alert("You need to type in your name please."); continueOn = "no"; }
	else if (tF.terms[0].checked) {
			alert("You have not accepted our terms and conditions"); continueOn = "no";
	}

	if ( continueOn  == "yes") {

		str = new String(tF.name.value);
		tF.name.value = replaceSpChars(str);

		str = new String(tF.address.value);
		tF.address.value = replaceSpChars(str);

		str = new String(tF.telephone.value);
		tF.telephone.value = replaceSpChars(str);

		str = new String(tF.email.value);
		tF.email.value = replaceSpChars(str);

		str = new String(tF.guest1.value);
		tF.guest1.value = replaceSpChars(str);

		str = new String(tF.guest2.value);
		tF.guest2.value = replaceSpChars(str);

		str = new String(tF.guest3.value);
		tF.guest3.value = replaceSpChars(str);

		str = new String(tF.guest4.value);
		tF.guest4.value = replaceSpChars(str);

		str = new String(tF.guest5.value);
		tF.guest5.value = replaceSpChars(str);

		str = new String(tF.guest6.value);
		tF.guest6.value = replaceSpChars(str);

		str = new String(tF.guest7.value);
		tF.guest7.value = replaceSpChars(str);
	
		str = new String(tF.dates1.value);
		tF.dates1.value = replaceSpChars(str);

		str = new String(tF.dates2.value);
		tF.dates2.value = replaceSpChars(str);


		tF.action = "http://www.whalesborough.co.uk/cgi-bin/whalesboroughbooking.pl";
		tF.submit();	
	}		
}





function replaceSpChars(str) {
		var re = /=/gi
		str = str.replace(re,"equalsSign")
		re = /&/gi
		str = str.replace(re,"amperSand")
		re = /\+/gi
		str = str.replace(re,"plusSign")
		re = /\n/gi
		str = str.replace(re,"newLineSi")
		return str;
}
		
		
function UnreplaceSpChars(str) {
		var re = /equalsSign/gi
		str = str.replace(re,"=")
		re = /amperSand/gi
		str = str.replace(re,"\&")
		re = /plusSign/gi
		str = str.replace(re,"\+")
		re = /newLineSi/gi
		str = str.replace(re,"\n")
		return str;
}




function checkAndSubmit2(tF) { //tF = ref to form


	var continueOn = "yes";
	var emailOK = checkEmail();
	if ( emailOK != "good" ) {
		continueOn = "no"; 
	}
	else if (tF.name.value == "") { alert("Please type your name."); continueOn = "no"; }
	else if (tF.message.value == "" || tF.message.value == "Enter Your Message Here" ) { alert("Please type your message."); continueOn = "no"; }

	if ( continueOn  == "yes") {

		str = new String(tF.name.value);
		tF.name.value = replaceSpChars(str);

		str = new String(tF.email.value);
		tF.email.value = replaceSpChars(str);

		str = new String(tF.subject.value);
		tF.subject.value = replaceSpChars(str);

		str = new String(tF.message.value);
		tF.message.value = replaceSpChars(str);

		tF.action = "http://www.whalesborough.co.uk/cgi-bin/whalesboroughcontact.pl";
		tF.submit();	
	}		
}




function checkEmail() {
		var myRegExp = / /gi
		str = new String(document.forms[0].email.value)
		str = str.replace(myRegExp,"")
		var myRegExp2 = /\./;
		var myRegExp3 = /\@/;
		var myRegExp4 = /\,/;
		var myRegExp5 = /www\./;

		if ( str.search(myRegExp4) != -1 ) {
		alert("Your email address appears to be wrong - you seem to have typed a comma instead of a full stop. Please correct it and click 'Submit Form' again.");
		return "bad";
		}
		else if ( str.search(myRegExp2) == -1 || str.search(myRegExp3) == -1) {
		alert("Your email address appears to be wrong. Please correct it and click 'Submit Form' again.");
		return "bad";
		}
		else if ( str.search(myRegExp5) != -1 ) {
		alert("Your email address appears to be wrong - it should not contain www. Please correct it and click 'Submit Form' again.");
		return "bad";
		}
		else {
		return "good";
		}
}






