// cookie.js
// Copyright 2004 Ben Measures
//
// function existCookie(name)
// Checks if a cookie exists.
// Returns true if cookie is found, false otherwise.
//
// function setCookie(name, content, expiry, path, domain, secure)
// Sets a cookie. 
// Returns true if successful, false otherwise.
//
// function getCookie(name)
// Retrives a cookie.
// Returns the value of the cookie if successful, false otherwise.
//
// function deleteCookie(name, path, domain, secure)
// Expires a cookie.
// Returns false if the cookie is still accessible, true otherwise.

function existCookie(name)
{
  if (typeof(document.cookie) == 'undefined') {
    return false;
  } else {
    return (document.cookie.indexOf(escape(name)) != -1);
  }
}

function setCookie(name, content, lifespan_in_days, path, domain, secure)
{
  if ((typeof(document.cookie) == 'undefined') || (typeof(name) == 'undefined')) {
    return false;
  } else {
    document.cookie = (escape(name) + '=' + escape(content))
                      + ((lifespan_in_days) ? "; max-age=" + 60*60*24*lifespan_in_days : "")
		      + ((path) ? "; path=" + path : "")
		      + ((domain) ? "; domain=" + domain : "")
		      + ((secure) ? "; secure" : "");

    var cookie_success = (getCookie(name) == content);
    return cookie_success;
  }
} 

function getCookie(name)
{
  if ((typeof(document.cookie) == 'undefined') || !existCookie(name)) {
    return false;
  } else {
    var cookiestring = document.cookie;

    var startpos = cookiestring.indexOf(escape(name));
    var next_semicolon = cookiestring.indexOf(";", startpos);
    var endpos = ((next_semicolon == -1) ? cookiestring.length : next_semicolon);

    return unescape(cookiestring.substring(startpos + escape(name).length + 1, endpos));
  }
}

function deleteCookie(name, path, domain, secure)
{
  if ((typeof(document.cookie) == 'undefined') || !existCookie(name)) {
    return true;
  } else {
    var current_time = new Date();
    document.cookie = (escape(name) + '=' + escape(getCookie(name)))
                      + ('; expires=' + current_time.toGMTString())
		      + ((path) ? "; path=" + path : "")
		      + ((domain) ? "; domain=" + domain : "")
		      + ((secure) ? "; secure" : "");

    return !existCookie(name);
  }
}

function customMenu()
{
  if (existCookie('menu_tweek')) {
    var nature = document.getElementById('Nat');
    var txt = getCookie('menu_tweek');
    nature.innerHTML = '<a class="listing" onclick="this.blur()" href="/naturistAccommodation" title="Perfect for a naturist holiday">' + txt + '</a>';
  }
}

/* This function adds message above the textarea for certain visitors when they complete booking form  */

function customForm()
{
  if (existCookie('menu_tweek')) {
    var nature = document.loginForm.referral;
    var msgbox = document.getElementById('ownerMsg');
    var msg = "Simon and Ruth ask, \"Will we be correct if we assume you\'re naturists?\"";
    var txt = getCookie('menu_tweek');
    nature.value = txt;
    if (msgbox.firstChild) {
      msgbox.removeChild(msgbox.firstChild);
    }
    msgbox.appendChild(document.createTextNode(msg));
  }
}

/* This function enables information getting sent with the enquiry form about guest type  */

function guestTypeWithForm()
{
  if (existCookie('menu_tweek')) {
    var nature = document.loginForm.referral;
    var txt = getCookie('menu_tweek');
    nature.value = txt;
  }
}

/* Test cookies aren't blocked */

function checkCookieAccepted() {
    if (existCookie('menu_tweek')){
    }
    else {
	alert('Your browser has blocked a cookie we\'d like to set.\n\nPlease change your browser settings to allow cookies from this site.\n\nThis cookie will help our web site deliver content and services to you that are relevant to your requirements. Thanks.');
    }
}
