// $Id: common.js,v 1.3 2007/06/06 22:08:10 cmanley Exp $

function isScriptLoaded(src) {
  var scripts = document.getElementsByTagName('script');
  for (var i = 0; i < scripts.length; i++) {
    if (scripts[i].getAttribute('src') == src) {
      return true;
    }
  }
  return false;
}


function loadScript(src) {
  if (isScriptLoaded(src)) {
    return true;
  }
  if (document.createElement && document.childNodes) {
    var e = document.createElement('script');
    e.setAttribute('src', src);
    e.setAttribute('type', 'text/javascript');
    //e.setAttribute('defer', 'defer');
    var head = document.getElementsByTagName('head')[0];
    var scripts = document.getElementsByTagName('script');
    if (scripts.length) {
      head.insertBefore(e, scripts[0]);
    }
    else {
      document.getElementsByTagName('head')[0].appendChild(e);
    }
    return true;
  }
  return false;
}


// Extend the primitive type classes
if (typeof(String.prototype.trim) === 'undefined') {
 String.prototype.trim = function() {
  return this.replace(/(^\s+|\s$)/g, '');
 }
}

if (typeof(String.prototype.toHtml) === 'undefined') {
 String.prototype.toHtml = function() {
  return this.replace(/[&<>]/g, function(c) { if (c == '&') return '&amp;'; if (c == '<') return '&lt;'; if (c == '>') return '&gt;'; });
 }
}

if (typeof(String.prototype.nl2br) === 'undefined') {
 String.prototype.nl2br = function() {
  return this.replace(/\r?\n/g, '<br/>');
 }
}

Number.prototype.toCurrencyNL = function() {
 var f = Math.round(this * 100);
 if (f == 0) {
  return '0,00';
 }
 var s = String(f);
 var e = s.length > 2 ? s.substr(0,s.length - 2) : '0';
 var c = s.substr(s.length - 2);
 var re = /(\d+)(\d{3})/;
 while (re.test(e)) {
  e = e.replace(re, '$1' + '.' + '$2');
 }
 return e + ',' + c;
}

String.prototype.toCurrencyNL = function() {
 return Number(this).toCurrencyNL();
}

if (typeof(Date.prototype.getUnixTime) === 'undefined') {
 Date.prototype.getUnixTime = function() {
  return Math.floor(this.valueOf() / 1000);
 }
}


function cookieCheck() {
 document.cookie = "cookiecheck=1";
 var enabled = (document.cookie.indexOf("cookiecheck") != -1);
 // Check if session cookie is present, if not then page was loaded from browser cache and is probably invalid.
 if (enabled) {
  if (document.cookie.indexOf("PHPSESSID") == -1) {
   document.location.reload(true);
   return;
  }
 }
 else {
  var e = document.getElementById('cookies_disabled');
  if (e) {
   e.style.display = 'block';
  }
 }
}



/**
 * Returns the text value of an element.
 */
function domNodeTextValue(n) {
  if (n.nodeType == 3) { // text node
    return n.nodeValue;
  }
  var result = '';
  if (n.hasChildNodes()) {
    var nodes = n.childNodes;
    for (var i = 0; i < nodes.length; i++) {
      result += domNodeTextValue(nodes[i]);
    }
  }
  return result;
}


// Array of code to call when document has loaded.
var onloadCallStack = new Array('cookieCheck()');


function setDocumentTitle() {
  var e = document.getElementById('pageTitle');
  if (e) {
    document.title += ' - ' + domNodeTextValue(e);
  }
}
onloadCallStack.push('setDocumentTitle()');


// Document onload event handler.
function documentLoaded() {
  for (var i = 0; i < onloadCallStack.length; i++) {
    eval(onloadCallStack[i]);
  }
}

// Set event handlers
onload = documentLoaded;
