//SOME FUNCTIONS____________________________________________________________________________________________

function emailvalid(email)
{
    if (email.match(/^[A-Za-z0-9\.\_\-]{1,32}\@[A-Za-z0-9\.\_]{1,32}\.[a-zA-Z]{2,6}$/))
    {
        return true;
    }
    else
    {
        return false;
    }
}


//STRING PROTOTYPE___________________________________________________________________________________________

String.prototype.ltrim = function(chars)
{
    chars = chars || "\\s";
    return this.replace(new RegExp("^[" + chars + "]+", "g"), "");
}


String.prototype.rtrim = function(chars)
{
    chars = chars || "\\s";
    return this.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


String.prototype.trim = function(chars)
{
    return this.rtrim(chars).ltrim(chars);
}


//ARRAY PROTOTYPE___________________________________________________________________________________________


Array.prototype.valueExists = function(value)
{
    if (this.length == 0) return falsel;

    for (var i = 0; i < this.length; i++)
    {
        if (this[i] == value) return true;
    }

    return false;
}


//URL HELPER_______________________________________________________________________________________________

function Url(url)
{
    if (url == undefined)
    {
        url = location.href;
    }

    url = url.trim("/");

    this.url = url;

    var self = this;

    var editUrl = function(params)
    {
        var url_params = new Array();

        for (var name in params)
        {
            url_params.push(name);
            url_params.push(params[name]);
        }

        self.url = url_params.join("/");
    }


    this.getHost = function()
    {
        return "http://" + location.host;
    }


    this.getShortUrl = function()
    {
        return this.url.replace(this.getHost(), "");
    }


    this.getUrlParams = function()
    {
        var params_obj = {};

        var params = this.url.replace(this.getHost() + "/", "").split("/");
        var count  = params.length;

        for (var i = 0; i < count; i+=2)
        {
            params_obj[params[i]] = params[i+1];
        }

        return params_obj;
    }


    this.setParam = function(name, value)
    {
        var params = this.getUrlParams();

        params[name] = value;

        editUrl(params);

        return this;
    }


    this.getParam = function(name)
    {
        return this.getUrlParams()[name]
    }


    this.removeParam = function()
    {
        delete this.getUrlParams()[name];
        return this;
    }


    this.apply = function()
    {
        return this.getHost() + "/" + this.url;
    }
}


// FORMS _______________________________________________________________________________________________

function setFormElementValue(element_id, value)
{
    var element = $("#" + element_id);

    element.val(value);

    element.focus(function()
    {
        if($(this).val() == value)
        {
            $(this).val("");
        }
    });

    element.blur(function()
    {
        if($(this).val() == "")
        {
            $(this).val(value);
        }
    });
}





