function removeFrame(city){
	parent.location="https://www.bookingbedandbreakfast.co.uk/bookingform.php?city=" + city;
}

function changeArea(selection){
	if (selection != ""){
		var mystring;
		mystring = "?area=" + selection;
		window.top.location.href=mystring;
	}
}

function areYouSure(){
	var answer = confirm("Are you sure you want to delete this booking? \nAll payment details and contact details will be deleted forever!");
	if (answer){
		return true;
	}else{
		return false;
	}
}

function areYouSureUser(){
	var answer = confirm("Are you sure you want to delete this user? \nThey will no longer be able to sign into the website and you will need to either delete their Bed and Breakfasts or assign them to another user.");
	if (answer){
		return true;
	}else{
		return false;
	}
}

function makeAlertWrongCity(){
	alert("The postcode you have entered does not match any of the cities where the Bouverie Bed and Breakfast Association operate.  \nPlease select your preferred city from the drop-down list on the right.");
}

function show(id){
	document.getElementById(id).style.display = "inline";
}
function hide(id){
	document.getElementById(id).style.display = "none";
}
function stripslashes(str) {
	return str.replace('/\0/g', '0').replace('/\(.)/g', '$1');
}
function updateLegalLink(id){
	if (id){
		document.getElementById("legalLink").innerHTML = "<a href='legal.php?tc="+id+"' target='_blank' title='Terms and Conditions'>Read terms and conditions here.</a>";
		//document.getElementById("pricesLink").innerHTML = "<a href='prices.php?bb="+id+"' target='_blank' style='font-weight:bold; color:#77B8C2;'>(list prices)</a>";
		var myVar = "rooms"+id;
		document.getElementById("roomsList").innerHTML = eval(myVar);
		
		var myVar = "quoted"+id;
		myVar = stripslashes(myVar);
		try{
			myVar = eval(myVar);
		}catch(err){
			//Some of the clients haven't changed the quoted text. This is the default text.
			myVar = 'If you have arranged a specific total price for your stay with the selected B&B, enter the value in the "Quoted Price" box above, otherwise leave it blank.';
		}
		document.getElementById("quotedCopy").innerHTML = myVar;
		
		myPriceListLink = '<a href="javascript:priceList(locationRoomsArray['+id+']);" style="font-weight:bold; color:#77B8C2;">(See Prices)</a>';
		document.getElementById("priceList1").innerHTML = myPriceListLink;
		document.getElementById("priceList2").innerHTML = myPriceListLink;
	}
}
function focusOnBandBName(){
	var item = document.getElementById("bandbname");
	if (item){item.focus();}
}
function clearField(fieldValue){
	if (fieldValue == " Enter Postcode"){
		var myVar = document.getElementById("postcodeSearch");
		myVar.value = "";
	}	
}

function resetField(fieldValue){
	if (fieldValue == ""){
		var myVar = document.getElementById("postcodeSearch");
		myVar.value = " Enter Postcode";
	}
}
/*
function mapSize(){
	myVar = document.getElementById("map");
	size = document.documentElement.clientHeight-170;
	if (size < 544) size = 544;
	myVar.style.height=size+"px"
	
	size = document.body.offsetWidth-255;
	myVar.style.width=size+"px"
}

function layoutSizes(){
	myVar = document.getElementById("mainCopy");
	size = document.body.offsetWidth-280;
	myVar.style.width=size+"px"
}
*/
function mapSize(){
    if (map !== undefined) {
        var zoom = map.getZoom();
        var centre = map.getCenter();
    }
    var myVar = document.getElementById("map");
    //var loader = document.getElementById("loader");
    if (document.documentElement.clientHeight < window.innerHeight){
        var height = window.innerHeight;
    }else{
        var height = document.documentElement.clientHeight;
    }
    if (document.documentElement.clientWidth < window.innerWidth){
        var width = window.innerWidth;
    }else{
        var width = document.documentElement.clientWidth;
    }

    var size = height * 70 /100;
    //if (size < 480) size = 480;
    //myVar.style.height=size+"px"
    size = width * 70/100;

    if (size <840){
        size = 840;
    }

    myVar.style.width=size+"px";
    size = size * 1/2;
    myVar.style.height=size+"px";
    if (loader != null) {
        loader.style.width = myVar.style.width;
        loader.style.height = myVar.style.height;
    }
    if (map !== undefined) {
        map.returnToSavedPosition();
        //map.setCenter(centre);
        //map.setZoom(zoom);
    }
}

function changeClass(theClass,element,value){
	var cssRules;
	if (document.all){cssRules = 'rules';
	}else if (document.getElementById){ cssRules = 'cssRules';}
	for (var i = 0; i < document.styleSheets.length; i++){
		for (var j = 0; j < document.styleSheets[i][cssRules].length; j++) {
			if (document.styleSheets[i][cssRules][j].selectorText == theClass) {
				document.styleSheets[i][cssRules][j].style[element] = value;
			}
		}
	}
}

function layoutSizes(){
	if (document.documentElement.clientWidth < window.innerWidth){
		var width = window.innerWidth;
	}else{
		var width = document.documentElement.clientWidth;
	}
	var myVar = document.getElementById("mainCopy");
	var size = width-350;
	if (size < 450)size = 450;
	//alert("New width of #mainCopy: "+size);
	myVar.style.width=size+"px"
	size = size-10;

	try{
		var myVar2 = document.getElementById("readableTextWidthInAdmin");
		myVar2.style.width=size+"px";
	}catch(err){
	}
}

function setClass(objectID,newClass) {
	var object = document.getElementById(objectID);
	object.className = newClass;
}
function nextMonth(){
	$currentHighestMonth++; $currentLowestMonth++;
	setClass("calendar"+months[$currentHighestMonth - 3], "hide");
	setClass("calendar"+months[$currentHighestMonth], "show");
	if ($currentHighestMonth > 10){
		setClass("next", "hide");
	}else{
		setClass("next", "show");
	}	
	if ($currentLowestMonth > 0){
		setClass("prev", "show");
	}else{
		setClass("prev", "hide");
	}						
}
function prevMonth(){
	$currentHighestMonth--; $currentLowestMonth--;
	setClass("calendar"+months[$currentLowestMonth], "show");
	setClass("calendar"+months[$currentHighestMonth+1], "hide");						
	if ($currentHighestMonth > 10){
		setClass("next", "hide");
	}else{
		setClass("next", "show");
	}	
	if ($currentLowestMonth > 0){
		setClass("prev", "show");
	}else{
		setClass("prev", "hide");
	}							
}	

function defaultCityChanger(){
	if (document.forms[0]){
		document.forms[0].changeCityList.options[0].selected = true;
	}
}

function getDaysInMonth(month, year){
	var days;
	switch (month){
		case 9:
			days = 30;
			break;
		case 4:
			days = 30;
			break;
		case 6:
			days = 30;
			break;
		case 11:
			days = 30;
			break;
		case 2:
			if (year % 4 == 0){
				days = 29;
				break;
			}else{		
				days = 28;
				break;
			}	
			break;
		default:
			days = 31;
			break;
	}
	return days;
}
function alertFunction(myVar){
	switch (myVar){
		case 1:
			myVar = "Telephone Available";
			break;
		case 2:
			myVar = "Credit Cards Accepted";
			break;
		case 3:
			myVar = "Chilren's rates";
			break;
		case 4:
			myVar = "Dogs welcome by arrangement";
			break;
		case 5:
			myVar = "Parking on site";
			break;
		case 6:
			myVar = "Disabled access";
			break;
		case 7:
			myVar = "Residents' Lounge";
			break;
		case 8:
			myVar = "Smoking garden for residents' use";
			break;
		case 9:
			myVar = "Bar";
			break;
		case 10:
			myVar = "Continental breakfast only";
			break;
		case 11:
			myVar = "Housekeeper/24 hour reception";
			break;
		case 12:
			myVar = "Luggage storage";
			break;
		case 13:
			myVar = "Guests get own house keys";
			break;
		case 14:
			myVar = "TV in bedroom";
			break;
		case 15:
			myVar = "Tea/Coffee making facilities";
			break;
		case 16:
			myVar = "Rooms with bath";
			break;
		case 17:
			myVar = "Rooms with shower";
			break;
		case 18:
			myVar = "Safe";
			break;
		case 19:
			myVar = "Internet Access";
			break;
		case 20:
			myVar = "Bathroom - En-suite and good size";
			break;
		case 21:
			myVar = "Bathroom - Small en-suite";
			break;
		case 22:
			myVar = "Bathroom - Private not en-suite good size";
			break;
		case 23:
			myVar = "Bathroom - Small private not en-suite";
			break;
		case 24:
			myVar = "Bathroom - Shared";
			break;			
	}
	alert(myVar);
}
function notShownYet(){
	alert("You have not yet selected any B&B from the drop down list.\nOnce you have done, you can use this link to display the price list for that B&B.");
}

function warnAboutDecember(month){
	if ((month-0) == 12){
		alert("You have selected the \"From\" date to be December.\n\n***** DO NOT SET THE \"TO\" DATE TO JANUARY *****\n\nSet it to 31 December.\n\nThen make a new pricing season beginning 1st January to the end of your Hogmanay season.");
	}	
}
function noPricesAvailable(){
	alert("The owner of this B&B has not added pricing information for this room.\n\nYou cannot book this room through this website.");
}

function hidePricingTable(){
	if (document.getElementById('title').value=='Details'){
		document.getElementById('pricingTable').style.display='none';
	}else{
		document.getElementById('pricingTable').style.display='block';
	}
}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}

function addBed(type) {
	if (ul === undefined) { var ul = document.getElementById("bedlist") };
	var typeLabel = "Adult Bed ";
	if (type == 0)
	{
		typeLabel = "Child Bed ";
	}
	var li = document.createElement("li");
	var litext = document.createTextNode(typeLabel);
	var liinput = document.createElement("input");
	liinput.setAttribute("type", "hidden");
	liinput.setAttribute("name", "beds[]");
	liinput.setAttribute("value", type);
	var lia = document.createElement("a");
	lia.setAttribute("title", "Remove");
	lia.setAttribute("onclick", "javascript:removeChild(this)");
	var liatext = document.createTextNode("X");
	lia.appendChild(liatext);
	li.appendChild(litext);
	li.appendChild(liinput);
	li.appendChild(lia);
	ul.appendChild(li);
}

function removeBed(obj) {
	obj.parentNode.parentNode.removeChild(obj.parentNode);
}

function setCheckout() {
	var checkin = document.getElementById("checkin");
	var checkout = document.getElementById("checkout");
	var ci = checkin.value.split('/');
	var co = checkout.value.split('/');
	var inDate = new Date();
	var outDate = new Date();
	inDate.setFullYear(parseInt(ci[2],10) + 2000,ci[1]-1,ci[0]);
	var setCheckout = false;
	var days = 1;
	if (checkout.value.length == 0) {
		setCheckout = true;
	}

	if (co.length == 3) {
		outDate.setFullYear(parseInt(co[2],10) + 2000,co[1]-1,co[0]);
		if (outDate.getTime() <= inDate.getTime()) {
			setCheckout = true;
		}
	}
	var outstr = "";
	if (setCheckout) {
		outDate = inDate;
		outDate.setDate(outDate.getDate() + 1);
		if (outDate.getDate() < 10) {
			outstr += "0";
		}
		outstr += outDate.getDate();
		outstr += "/";
		if (outDate.getMonth() < 10) {
			outstr += "0";
		}
		outstr += outDate.getMonth() + 1;
		outstr += "/";
		if (outDate.getFullYear() < 10) {
			outstr += "0";
		}
		outstr += outDate.getFullYear().toString().substring(2);
		checkout.value = outstr;
	}
}

function toggleStats(statsLink, statsDiv) {
    var obj = document.getElementById(statsLink)
    var tgt = document.getElementById(statsDiv);
    obj.innerHTML = obj.innerHTML == "+" ? "-" : "+";
    tgt.style.display = tgt.style.display == "none" ? "" : "none";
}

function switchImage(img) {
    var tgt = document.getElementById('mainImage');
    var src = document.getElementById(img);
    tgt.src = src.src;
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}   