/**
 * Browser related methods
 */

var Browser = {};

Browser.IE = /*@cc_on!@*/false;
Browser.IE6 = false;
if (Browser.IE) {
    var uaStr = navigator.userAgent.toLowerCase();
    var uaVer = parseInt(uaStr.match(/msie (\d+)/)[1]);
    Browser.IE6 = uaVer < 7;
}

/**
 * Document related methods
 */

function $(id)
{
    return document.getElementById(id);
}

function addOnloadEvent(fn) {
    var oldFn = window.onload;
    if (typeof window.onload == "function") {
        window.onload = function() {
            if (oldFn) {
                oldFn();
            }
            fn();
        }
    }
    else {
        window.onload = fn;
    }
}

function addOnunloadEvent(fn) {
    var oldFn = window.onunload;
    if (typeof window.onunload == "function") {
        window.onunload = function() {
            if (oldFn) {
                oldFn();
            }
            fn();
        }
    }
    else {
        window.onunload = fn;
    }
}

function getId(e)
{
    var id = e.getAttribute("id");
    return id != null ? id : "";
}

function hasTagName(e, tagName)
{
    return e.tagName && e.tagName.toLowerCase() == tagName.toLowerCase();
}

function hasClass(e, className)
{
    return e.className && e.className.match(new RegExp("(\\s|^)" + className + "(\\s|$)"));
}

function addClass(e, className)
{
    if (this.hasClass(e,className)) {
        return;
    }
    e.className += e.className == "" ? className : " " + className;
}

function removeClass(e, className)
{
    if (hasClass(e,className)) {
        var r = new RegExp('(\\s|^)' + className + '(\\s|$)');
        e.className=e.className.replace(r, " ").trim();
    }
}

function getStyle(e, style)
{
    var s = e.currentStyle ? e.currentStyle : document.defaultView.getComputedStyle(e, null);
    return s[style];
}

function stopEventPropagation(event)
{
    if (event && event.stopPropagation) {
        event.stopPropagation();
    }
    else {
        window.event.cancelBubble = true;
    }
}

function toInt(value)
{
    var i = parseInt(value, 10);
    if (i != NaN && i != value - 0) {
        return NaN;
    }
    return i;
}

function isArray(object)
{
    return object.constructor == Array;
}

if (!Array.indexOf) {
    Array.prototype.indexOf = function(elem)
    {
        for (var i = 0; i < this.length; i++) {
            if (this[i] === elem){
                return i;
            }
        }
        return -1;
    }
}

/**
 *  String related methods
 */

String.prototype.trim = function()
{
  	var i = 0;
    var count = this.length;
	  var len = count;

	  while ((i < len) && (this.charAt(i) <= ' ')) {
	      i++;
	  }
	  while ((i < len) && (this.charAt(len - 1) <= ' ')) {
	      len--;
	  }
	  return ((i > 0) || (len < count)) ? this.substring(i, len) : this;
}

String.prototype.equalsIgnoreCase = function(s)
{
    return typeof s == "string" && this.toLowerCase() == s.toLowerCase();
}

String.prototype.utf8encode = function()
{
    var result = "";
    for (var i = 0; i < this.length; i++) {
        var c = this.charCodeAt(i);
        if (c < 128) {
            result += String.fromCharCode(c);
        }
        else if((c >= 128) && (c < 2048)) {
            result += String.fromCharCode((c >> 6) | 192, (c & 63) | 128);
        }
        else {
            result += String.fromCharCode((c >> 12) | 224, ((c >> 6) & 63) | 128, (c & 63) | 128);
        }
    }
    return result;
}

String.prototype.utf8decode = function()
{
    var result = "";
    var i = 0;
    var c = c1 = c2 = 0;
    while (i < this.length) {
        c = this.charCodeAt(i++);
        if (c < 128) {
            result += String.fromCharCode(c);
        }
        else if((c >= 192) && (c < 224)) {
            c2 = s.charCodeAt(i++);
            result += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        }
        else {
            c2 = s.charCodeAt(i++);
            c3 = s.charCodeAt(i++);
            result += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        }

    }
    return result;
}

String.prototype.startsWith = function(s)
{
    return this.indexOf(s) == 0;
}

