/**
 * Filename    : misc.js
 * Author      : Robert Cerny
 * Created     : 21.08.2004
 * Last Change : 21.06.2005
 *
 * Contains misc. useful javascript functions.
 *
 * Depends on  : trans.js
 *
 * Copyright 2004 Robert Cerny
 */

/**
 * Write a string into a document.
 * Escapes the "script" problem.
 * WARNING: not tested in this form.
 *
 * string - the string to write
 * _document - the document to write to, if null the current document
 */
function _write(string, _document) {
    if (string == null || string.length == 0)
        return;
    if (_document == null)
        _document = document;

    var rest = string;
    var cut = rest.indexOf("script");
    while (cut > 0) {
        _document.write(rest.substring(0, cut + 4));
        rest = rest.substring(cut + 4);
        cut = rest.indexOf("script");
    }
    _document.write(rest);
}

/**
 * Returns true if string is blank
 *
 * s - String to check
 */
function isBlankString(s) {
    for (var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t') && (c != '\r')) return false;
    }
    return true;
}

/**
 * Returns the value of a paramter passed in the query part of an url.
 *
 * parameter - the name of the parameter
 * url - the url to look in, if null the url of the current document
 */
function getURLParameterValue(parameter, url) {
    if (url == null)
        url = document.URL;

    var regex = "/.*" + parameter + "=([^&]*).*/";
    var match = new RegExp(eval(regex)).exec(url);
    if (match != null && match[1] != null)
        return unescape(match[1]);
    else
        return null;
}

/**
 * Focuses on the field with id fieldId
 *
 * fieldId - the id of the field to focus on
 */
function _focus(fieldId) {
    var field = document.getElementById(fieldId);
    if (field != null && field.focus) {
        field.focus();
    }
}

/**
 * Retrieve cookie.
 *
 * name - name of the cookie to retrieve
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else
        begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
        end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
}


/**
 * Sets a cookie.
 *
 * name - name of the cookie
 * value - value of the cookie
 * expires - expiration date of the cookie
 */
function setCookie(name, value, expires, path, domain, secure) {
    var _cookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
    document.cookie = _cookie;
}

/**
 * http://www.crockford.com/javascript/remedial.html
 * Thanks to Douglas Crockford.
 */
function trim(_string) {
    if (_string == null)
        return null;
    return _string.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
}

/**
 * Inspects an object o.
 * Creates a new window and writes all properties of o to it.
 *
 * o - object to inspect
 */
function inspect(o) {
    var all = "<html><body>";
    for (var e in o)
        all += "<p>" + e + ": " + o[e] + "</p>";
    all += "</html></body>";
    var newWindow = window.open();
    newWindow.document.writeln(all);
}
