// prevent crooks from framing the site
// if (top != self) top.location = self.location;  

function test(num) {
// use for debugging
  alert("Testing " + num);
}

function printLine(str) {
// writes str to the position in the HTML file where 'printLine()' is called
// use this to write Active X content (object, embed, applet tags) as a workaround for the clicking required to activate an Active X control
  document.write(str);
}

function pageId(page_url) {
// return the id (folder) of the page in the url (without arguments or hashes or "index.html" etc.)
  var p_url = page_url;

  // test for trailing slash on url, remove it if it is present
  if (page_url.charAt(page_url.length-1)=="/") {
    p_url = page_url.substr(0, page_url.length-1);}

  var temp = p_url.split("/");
  var temp2;
  if (temp.length > 1) temp2 = temp[temp.length - 2];
  else temp2 = temp;
  temp = temp[temp.length - 1].split("#");
  temp = temp[temp.length - 1].split("?");
  temp = temp[temp.length - 1];

  // do not return "index.html" etc, return previous page id instead
  var test_for = new Array("index.html", "index_html", "index.php", "index.asp");
  if (isIn(temp, test_for)) return temp2;
  else return temp;
}

function getQuery(key_str) {
// return value of key_str variables query string of url
// Example: url = "index.html?alert=5&page=index"; if key_str = "alert" then it returns "5"
  if(window.location.search) {
    var query = window.location.search.substr(1);
    var pairs = query.split("&");
    for(var i = 0; i < pairs.length; i++) {
      var pair = pairs[i].split("=");
      if(unescape(pair[0]) == key_str) return unescape(pair[1]);
    }
  return null;
  }
}

function setState(id, dsply) {
// change an html element's visibility state
  obj = document.getElementById(id);
  if (obj) {
    if (dsply=='hide') obj.style.visibility = 'hidden';
    if (dsply=='show') obj.style.visibility = 'visible';
  }
  else return null;
}

function setDisplay(id, dsply) {
// change an html block element's display attribute
  obj = document.getElementById(id);
  if (obj) {
    if (dsply=='hide') obj.style.display = 'none';
    if (dsply=='show') obj.style.display = 'block';
  }
  else return null;
}

function writeHTML(id, str) {
// write HTML in content of element
  obj = document.getElementById(id);
  if (obj) obj.innerHTML = str;
  else return null;
}

function isIn(x, seq) {
// check to see if x is in seq
  l = seq.length; found = 0; i = 0;
  while ((found==0) && (i < l)) {
    if (seq[i] == x) found = 1;
    i++;
  }
  return found;
}


function random(X) {
// return a random integer in the range 0 to X-1 
  return Math.floor(X * (Math.random() % 1));
}

function randomSequence(N) {
// generate a sequence of length N, with unique integer elements with values < N
var seq = new Array();
i = 0;
while (i < N) {
  x = random(N);
  if (!isIn(x, seq)) {
    seq[i] = x;
    i++;   
  }
}
return seq;
}

function getCurrentTime() {
// return current time in milliseconds
  var datenow = new Date();
  return datenow.getTime();
}

function ms2Date(timediff) {
// convert milliseconds time difference into hours, minutes and secs difference
  var diff = timediff;
  var date_array = new Array();
  var days_diff = Math.floor(diff/1000/60/60/24);
  date_array[0] = days_diff;
  diff = diff - days_diff*1000*60*60*24;
  var hours_diff = Math.floor(diff/1000/60/60);
  date_array[1] = hours_diff;
  diff = diff - hours_diff*1000*60*60
  var minutes_diff = Math.floor(diff/1000/60);
  date_array[2] = minutes_diff;
  diff = diff - minutes_diff*1000*60;
  var seconds_diff = Math.floor(diff/1000);
  date_array[3] = seconds_diff;
  return date_array;
}

function date2Ms(datediff) {
// convert a date difference (from a normal array) into a milliseconds time difference
  var msdiff = datediff[3]*1000;
  msdiff *= datediff[2]*60; msdiff *= datediff[1]*60; msdiff *= datediff[0]*24;
  return msdiff;
}

function arrayIndexOf(element, arr) {
// returns index of an array element
  var indx = -1;
  for (var i=0;i<arr.length;i++) {
    if (arr[i] == element) indx = i;
  }
  return indx;
}

// COOKIE FUNCTIONS

function cookiesAllowed() {
// returns true if cookies are enabled for this web site, false if not
   setCookie('checkCookie', 'test', 1);
   if (getCookie('checkCookie')) {
      deleteCookie('checkCookie');
      return true;
   }
   return false;
}

function setCookie(name, value, expires, options) {
// From http://www.hunlock.com/blogs/Cookie_Monsters_Inc
// Accepts up to 6 arguments. Only Name and Value are required
// name is a string which indicates the name of your cookie. Avoid using symbols in the name of the cookie. 
// Value is the value of the cookie.
// If you'd like the cookie to expire you can specify the number of days to keep the cookie.
// For instance for the cookie "name" to exist for only 4 days you'd specify 
// "setCookie('name', 'value', 4);"
// The rest of the options are passed in Javascript object notation (JSON) to allow some 
// flexibility for what you want to set and what you don't want to set. 
// The names that are looked for are path, domain, and secure. You can specify any or all of them.
// Ex. setCookie('name','value',1, { "path" : "/", "secure" : "true" });
// Ex2. To set domain and path with a different syntax (but passing the same basic object)...
//   var cookieOptions = {};
//   cookieOptions.domain='yahoo.com';
//   cookieOptions.path='/';
//   setCookie('name', 'value', 1, cookieOptions);
//  For overview of Options, see http://www.hunlock.com/blogs/Cookie_Monsters_Inc.

   if (options==undefined) { options = {}; }
   if ( expires ) {
      var expires_date = new Date();
      expires_date.setDate(expires_date.getDate() + expires)
   }
   document.cookie = name+'='+escape( value ) +
      ( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + 
      ( ( options.path ) ? ';path=' + options.path : '' ) +
      ( ( options.domain ) ? ';domain=' + options.domain : '' ) +
      ( ( options.secure ) ? ';secure' : '' );
}

function getCookie( name ) {
// Accept name of a cookie and if it exists it will pass back the value,
// if it doesn't exist it will pass back null.
  var start = document.cookie.indexOf( name + "=" );
  var len = start + name.length + 1;
  if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
    return null;
  }
  if ( start == -1 ) return null;
  var end = document.cookie.indexOf( ';', len );
  if ( end == -1 ) end = document.cookie.length;
  return unescape( document.cookie.substring( len, end ) );
}

function deleteCookie( name, path, domain ) {
// This function accepts a name (mandatory) and an optional path and domain. 
// If the name of the cookie is found ( it calls getCookie ), the expires value of the 
// cookie is set to a date in the far past which automatically deletes the cookie.
   if ( getCookie( name ) ) document.cookie = name + '=' +
      ( ( path ) ? ';path=' + path : '') +
      ( ( domain ) ? ';domain=' + domain : '' ) +
      ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

// Get cookie routine by Shelley Powers 
function getCookie2(data_name) {
// extract data from cookie
  var search = data_name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) { 
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
    }
  }
  return returnvalue;
}

function full_window(href) {
// Opens a new popup window & maximises it.

height = screen.availHeight;
width = screen.availWidth;
win_params = "height=" + height + ",width=" + width + ",scrollbars=yes,location=no,menubar=no,directories=no,status=no,toolbar=no,statusbar=no,resizable";

new_window = window.open(href,'',win_params);
if (new_window.opener == null) new_window.opener = self;
new_window.focus();
return false;
}

function isValidEmail(email) {
// check email address
  if (email.indexOf("@")== -1) return false;
  else
    var adr = email.split("@");
    if (adr[0].length < 1) {
      writeHTML("alertbox", "ERROR: User address absent"); setDisplay("alertbox", "show");
      return false; }
    else if (adr[1].indexOf(".") == -1) {
           writeHTML("alertbox", "ERROR: No Dot in Email Address");
           setDisplay("alertbox", "show");
           return false;}
         else if (adr[1].length < 3) {
                writeHTML("alertbox", "ERROR: Domain Incorrect"); setDisplay("alertbox", "show");
                return false; }
              else return true; 
}