if (typeof navigator.language === 'undefined' && typeof navigator.userLanguage !== 'undefined') {
    navigator.language = navigator.userLanguage;
}




// http://www.w3.org/TR/REC-html40/types.html#type-name
String.prototype.isValidId = function() {
    return /^[a-zA-Z][a-zA-Z\d-_:\.]*$/.test(this);
}




String.prototype.leftTrim = function() {
    return this.replace(/^\s+/, '');
}

String.prototype.rightTrim = function() {
    return this.replace(/\s+$/, '');
}

String.prototype.trim = function() {
    return this.leftTrim().rightTrim();
}




// http://www.w3.org/TR/REC-html40/types.html#type-cdata
String.prototype.getArray = function() {
    return this.trim().replace(/\s+/g, ' ').split(' ');
}

Array.prototype.contains = function(element) {
    this[this.length] = element;
    var i = 0;
    while (this[i] !== element) {
        i++;
    }
    return i < --this.length;
}

Array.prototype.values = function() {
    var i = 0
      , values = new Array;
    for (; i < this.length; i++) {
        if (!values.contains(this[i])) {
            values[values.length] = this[i];
        }
    }
    return values;
}

String.prototype.containsClassName = function(className) {
    return this.getArray().contains(className);
}

String.prototype.getNormalized = function() {
    return this.getArray().values().join(' ');
}

String.prototype.withClassName = function(className) {
    if (!this.containsClassName(className)) {
        return this.getNormalized() + ' ' + className;
    } else {
        return this;
    }
}

String.prototype.withoutClassName = function(className) {
    var pattern = new RegExp(' ' + className + ' ', 'g');
    return (' ' + this.getNormalized() + ' ').replace(pattern, ' ').getNormalized();
}




document.getUniqueId = function() {
    var PREFIX = 'RANDOM:'
      , suffix;
    do {
        suffix = Math.floor(Math.random() * 1000000000000000000);
    } while (typeof $(PREFIX + suffix) === 'undefined' && $(PREFIX + suffix) === null);
    return PREFIX + suffix;
}




function closestAncestorByNodeName(element, nodeName) { // useful to assign onsubmit to a form taking only a field
    element = $(element);

    while (element !== null && element.nodeName.toLowerCase() !== nodeName.toLowerCase()) {
        element = element.parentNode;
    }
    return element;
}

function removeNode(node) {
    var parentNode = node.parentNode;
    parentNode.removeChild(node);
}

function createCookie(name, value, miliseconds) {
    if (miliseconds) {
        var date = new Date;
        date.setTime(date.getTime() + miliseconds);
        var expires = '; expires=' + date.toGMTString();
    } else {
        var expires = '';
    }
    document.cookie = name + '=' + value + expires + '; path=/';
}

function readCookie(name) {
    var nameEQ = name + '='
      , ca = document.cookie.split(';')
      , i = 0;
    for (; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) === ' ') {
            c = c.substring(1, c.length);
        }
        if (c.indexOf(nameEQ) === 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, '', -1);
}




// window.innerWidth and window.innerHeight become available in Internet Explorer.
function setInnerWidth() {
    if (!this.areInnerSizesSet && typeof this.innerWidth === 'undefined') {
        this.areInnerSizesSet = true;
    }
    if (typeof this.innerWidth === 'undefined' || this.areInnerSizesSet) {
        this.areInnerSizesSet = true;
        if (typeof this.document.documentElement !== 'undefined' && typeof this.document.documentElement.clientWidth === 'number' && this.document.documentElement.clientWidth !== 0) {
            this.innerWidth = this.document.documentElement.clientWidth;
        } else if (typeof this.document.body !== 'undefined' && typeof this.document.body.clientWidth === 'number') {
            this.innerWidth = this.document.body.clientWidth;
        } else {
            this.innerWidth = 0;
        }
    }
}

function setInnerHeight() {
    if (!this.areInnerSizesSet && typeof window.innerHeight === 'undefined') {
        this.areInnerSizesSet = true;
    }
    if (typeof this.innerHeight === 'undefined' || this.areInnerSizesSet) {
        this.areInnerSizesSet = true;
        if (typeof this.document.documentElement !== 'undefined' && typeof this.document.documentElement.clientHeight === 'number' && this.document.documentElement.clientHeight !== 0) {
            this.innerHeight = this.document.documentElement.clientHeight;
        } else if (typeof this.document.body !== 'undefined' && typeof this.document.body.clientHeight === 'number') {
            this.innerHeight = this.document.body.clientHeight;
        } else {
            this.innerHeight = 0;
        }
    }
}

window.initializeInnerSizes = function() {
    this.areInnerSizesSet = false;
    Event.observe(this, 'load', setInnerWidth);
    Event.observe(this, 'resize', setInnerWidth);
    Event.observe(this, 'load', setInnerHeight);
    Event.observe(this, 'resize', setInnerHeight);
}




// Standard functionality of <label> tags implemented for Safari.
function makeLabelsMoveFocus() {
    function moveFocus(source) {
        if (source.getAttribute) {
            var id = source.getAttribute('for');
            if (typeof $(id) !== 'undefined' && $(id) !== null) {
                if (($(id).nodeName.toLowerCase() === 'input' && new Array('checkbox', 'radio', 'text', 'password').contains($(id).type)) || $(id).nodeName.toLowerCase() === 'textarea' || $(id).nodeName.toLowerCase() === 'select') {
                    if ($(id).getAttribute('type') === 'radio') {
                        $$('[name=' + $(id).getAttribute('name') + ']').each(function(element) {
                                                                                 element.removeAttribute('checked');
                                                                             });
                        $(id).setAttribute('checked', 'checked');
                    } else if ($(id).getAttribute('type') === 'checkbox') {
                        if ($(id).getAttribute('checked') === 'checked') {
                            $(id).removeAttribute('checked');
                        } else {
                            $(id).setAttribute('checked', 'checked');
                        }
                    }
                    $(id).focus();
                }
            }
        }
    }
    var labels = $$('label')
      , i = 0;
    for (; i < labels.length; i++) {
        Event.observe(labels[i], 'click', function() {
            moveFocus(this);
        });
    }
}




window.initializeInnerSizes();
if (navigator.userAgent.indexOf('AppleWebKit') !== -1) {
    Event.observe(window, 'load', makeLabelsMoveFocus);

}