// trim a string
function trim(str) {
  return str.replace(/^\s*|\s*$/g,"");
}

// escape quotes in strings
function escape_quotes(str) {
  return str.replace(/\'/g,"\\'").replace(/\"/g,"\\\"");
}

// escapes only double-quotes in strings
function escape_double_quotes(str) {
  return str.replace(/\"/g,"\\\"");
}

// similar to explode in PHP
function explode(sep, str) {
  return str.split(sep);
}

// similar to implode in php
function implode(sep, arr) {
  var my_sep = '';
  var ret = '';
  for (var i = 0; i < arr.length; i++) {
    ret += my_sep + arr[i];
    my_sep = sep;
  }
  return ret;
}

// similar to in_array in PHP
function in_array(which, arr) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] == which) {
      return true;
    }
  }
  return false;
}

// open a popup
function open_popup(url, width, height, name) {
  if (width) {
    width_clause = 'width=' + width + ',';
  } else {
    width_clause = '';
  }
  if (height) {
    height_clause = 'height=' + height + ',';
  } else {
    height_clause = '';
  }
  if (!name || !name.length) {
    name = '_blank';
  }

  wnd = window.open(url, name, width_clause + height_clause + 'scrollbars=yes, status=yes, resizable=yes');
  wnd.focus();
}

// add the entitify method to the String object
Function.prototype.method = function (name, func) {
  this.prototype[name] = func;
  return this;
};

String.
method('entitify', function () {
  return this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}).
method('quote', function (noquotes) {
  var c, i, l = this.length, o = '';
  for (i = 0; i < l; i += 1) {
    c = this.charAt(i);
    if (c >= ' ') {
      if (c == '\\' || c == '"') {
        o += '\\';
      }
      o += c;
    } else {
      switch (c) {
        case '\b':
        o += '\\b';
        break;
        case '\f':
        o += '\\f';
        break;
        case '\n':
        o += '\\n';
        break;
        case '\r':
        o += '\\r';
        break;
        case '\t':
        o += '\\t';
        break;
        default:
        c = c.charCodeAt();
        o += '\\u00' + Math.floor(c / 16).toString(16) +
        (c % 16).toString(16);
      }
    }
  }
  if (noquotes) {
    return o;
  } else {
    return '"' + o + '"';
  }
});

// include another js script
function include(script_filename) {
  document.write('<' + 'script');
  document.write(' language="javascript"');
  document.write(' type="text/javascript"');
  document.write(' src="' + script_filename + '">');
  document.write('</' + 'script' + '>');
};

// include another js script, DOM way
function include_dom(script_filename) {
  var html_doc = document.getElementsByTagName('head').item(0);
  var js = document.createElement('script');
  js.setAttribute('language', 'javascript');
  js.setAttribute('type', 'text/javascript');
  js.setAttribute('src', script_filename);
  html_doc.appendChild(js);
  return false;
}

// check enter in a text field / text area
// call with onKeyPress="checkEnter(event, 'action')", where
// action is the javascript code to execute
function checkEnter(e, action){ //e is event object passed from function invocation
  var characterCode; // literal character code will be stored in this variable

  if (e && e.which) { //if which property of event object is supported (NN4)
    e = e;
    characterCode = e.which; //character code is contained in NN4's which property
  } else {

    e = event;
    characterCode = e.keyCode; //character code is contained in IE's keyCode property
  }

  if (characterCode == 13){ // if generated character code is equal to ascii 13 (if enter key)
    // document.msg.m.value = document.msg.m.value.substr(0, document.msg.m.value.length - 1);
    noClose = true;
    eval(action);
    e.returnValue = false;   // for IE, no enter gets entered in the textarea
    return false;            // for NN, no enter gets entered in the textarea
  } else {
    return true;
  }
}

// URL functions
function cleanURL(url) {
  // eliminate spaces and replace them with "-"
  url = url.replace(/\s/g, '-');
  // eliminate slashes and replace them with "-"
  url = url.replace(/\//g, '-');
  // french characters
  url = eliminateAccentedChars(url);
  // eliminate all chars that are not allowed
  url = url.replace(/[^\x21-\x7E]/g, '');
  url = url.replace(/[\x3F\x26]/g, '');
  // lower case
  url = url.toLowerCase();
  return url;
}

function eliminateAccentedChars(text) {
  var chars = "ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ";
  var replaces = "SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy";
  result = "";
  for (var j = 0; j < text.length; j++) {
    replacement = text.charAt(j)
    for (var i = 0; i < chars.length; i++) {
      if (chars.charAt(i) == replacement) {
        replacement = replaces.charAt(i);
        break;
      }
    }
    result = result + replacement;
  }
  return result;
}

// ajax functions
//include(__INSTALL_URL + 'js/cf_ajax.js');
//include(__INSTALL_URL + 'js/cms.js');
include(__INSTALL_URL + 'js/prototype/prototype.js');
include(__INSTALL_URL + 'js/browser_detect.js');

function __o(objectName) { return document.getElementById(objectName); } //get element by ID

// function that retrieves the value of a radio control
// the radio-controls must have the ids in the format:
// <name>_0, <name>_1, <name>_2 ...
function radioValue(name) {
  var i = 0;
  var obj;
  while(obj = __o(name + '_' + i)) {
    if (obj.checked) {
      return obj.value;
    }
  };
}

function cloneObject(what) {
  for (i in what) {
    this[i] = what[i];
  }
}

var MAX_DUMP_DEPTH = 10;

function var_dump(obj, name, indent, depth) {
  if (depth > MAX_DUMP_DEPTH) {
    return indent + name + ": <Maximum Depth Reached>\n";
  }
  if (typeof obj == "object") {
    var child = null;
    var output = indent + name + "\n";
    indent += "\t";
    for (var item in obj) {
      try {
        child = obj[item];
      } catch (e) {
        child = "<Unable to Evaluate>";
      }
      if (typeof child == "object") {
        output += var_dump(child, item, indent, depth + 1);
      } else {
        output += indent + item + ": " + child + "\n";
      }
    }
    return output;
  } else {
    return obj;
  }
}
