
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 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 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 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 || checkout.value == "dd/mm/yy") {
		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";
 }
}   