/**
 * Validavimo klasė
 *
 * @param string outputId - Vieta kurioje turi atsirasti klaidos pranešimas
 */
function validator(outputId)
{
    disableForms();
    this.outputId = outputId;
    this.messages = new Array();
    this.status   = true;

    /** CSS */
    this.css         = false;
    this.errorClass  = 'errorClass';
    this.normalClass = 'normalClass';
}

/**
 * Validavimo klasė
 *
 * @param string fieldId - Laukelio Id
 * @param string message - Klaidos pranešimas
 * @param string regexp  - Papildoma tikrinimo sąlyga
 */
validator.prototype.set = function(fieldId, message, regexp)
{
    var obj = $('#'+ fieldId);
    var value = obj.val();
    value = trim(value);
    if((regexp !== undefined && !regexp.test(value)) || value == '')
    {
        if(message !== undefined)
            this.messages.push(message);

        if(this.css)
        {
            obj.attr('class', this.errorClass);
        }
        enableForms();
        this.status = false;
    }
    else
    {
        if(this.css)
        {
            obj.attr('class', this.normalClass);
        }
    }
}

/**
 * Validavimo klasė
 *
 * @param string selector  - Selectorius
 * @param string message   - Klaidos pranešimas
 * @param string regexp    - Papildoma tikrinimo sąlyga
 */
validator.prototype.check = function(selector, message, regexp)
{
    if(this.validate(selector, regexp))
    {
        if(this.css)
            $(selector).attr('class', this.normalClass);
    }
    else
    {
        if(message !== undefined)
            this.messages.push(message);

        if(this.css)
            $(selector).attr('class', this.errorClass);

        enableForms();
        this.status = false;
    }
}

/**
 * Pridedam klaidą
 *
 * @param string selector - Selectorius
 * @param string rule     - Taisyklė
 *
 * @return bool
 */
validator.prototype.validate = function(selector, rule)
{
    if(rule !== undefined && !rule)
        return true;

    var value = $(selector).val();
    value = trim(value);

    switch(rule)
    {
        case false:
            return true;

        case undefined:
            if(value == '')
                return false;
            else
                return true;

        case 'email':
            var regexp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            return regexp.test(value);

        default:
            return rule.test(value);
    }
    return true;
}

/**
 * Pridedam klaidą
 *
 * @param string message - Klaidos pranešimas
 */
validator.prototype.setError = function(message)
{
    enableForms();
    if(message !== undefined)
        this.messages.push(message);

    this.status = false;
}

/**
 * Tikrinti formą
 *
 * @param bool output - Ar reikalingas klaidos išvedimas
 *
 * @return bool
 */
validator.prototype.output = function(output)
{
    if(this.outputId !== undefined && (output || output === undefined))
    {
        if(this.messages.length > 0)
        {
            var html = '<div id="inError" class="klaida tekstas centruotiTeksta">';
            html += '<div class="icon delete FloatRigt"><a href="#" onclick="msg.close(\'inError\');return false;"></a></div>';
            for(var i in this.messages)
            {
                html += '<p class="TitleWarning">'+ this.messages[i] +'</p>';
            }
            html += '</div>';
            $('#'+ this.outputId).html(html);
        }
    }
    return this.status;
}

function trim(str)
{
    if (typeof(str) != 'undefined')
    {
        str = str.replace(/^\s+/, '');
        for(var i = str.length - 1; i >= 0; i--)
        {
            if(/\S/.test(str.charAt(i)))
            {
                str = str.substring(0, i + 1);
                break;
            }
        }
        return str;
    }
}