
/**
 * Default behavior for ajax actions if response code is 401 (authorization error)
 */
$.ajaxSetup({
    error: function(request, status, errorThrown){
        if(request.status == 401){
            window.location.replace(request.responseText);
        }
    }
});

/**
 * Shows warning message
 * @param divId - Div identifier..
 * @param message - message to display.
 */
function showWarning(divId, message){

    $('#' + divId).removeClass('page-error-label');
    $('#' + divId).addClass('page-warning-label');
    $('#' + divId).html(message);
    $('#' + divId).show();
}


function showError(divId, message){

    $('#' + divId).removeClass('page-warning-label');
    $('#' + divId).addClass('page-error-label');
    $('#' + divId).html(message);
    $('#' + divId).show();
}

function clearWarning(divId){
    $('#' + divId).html('');
    $('#' + divId).hide();
}


function showPageWarning(message){
    $('#warning_label').removeClass('page-error-label');
    $('#warning_label').addClass('page-warning-label');
    $('#warning_label').html(message);
    $('#warning_label').show();
}

function showPageError(message){

    $('#warning_label').removeClass('page-warning-label');
    $('#warning_label').addClass('page-error-label');
    $('#warning_label').html(message);
    $('#warning_label').show();
}

function clearPageWarning(){
    $('#warning_label').html('');
    $('#warning_label').hide();
}

$.fn.showWarning = function(message) {
    $(this.selector + '_warning').removeClass('dialog-error-label');
    $(this.selector + '_warning').addClass('dialog-warning-label');
    $(this.selector + '_warning').show();
    $(this.selector + '_warning').html(message);
}

$.fn.showError = function(message) {

    $(this.selector + '_warning').removeClass('dialog-warning-label');
    $(this.selector + '_warning').addClass('dialog-error-label');
    $(this.selector + '_warning').show();
    $(this.selector + '_warning').html(message);
}

function trim(str) {
    return str.replace(/^\s+|\s+$/g,"");
}


function validateEmail(value)
{
    if (value == '' ) {
        return false;
    }
    reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test(value) == false) {
        return false;
    }

    return true;
}


function validateSubdomain(value) {
    if (value == '' || value.lenght > 32) {
        return false;
    }

    reg = /^([A-Za-z0-9]{1,30}[-]{0,1}[A-Za-z0-9]{1,30})$/;
    if(reg.test(value) == false) {
        return false;
    }
    return true;
}
    
function feedAndOpenDialog(fieldId, soruceUrl, initializingErrorMessage)
{
    try {
        $.ajax({
            url: soruceUrl,
            success: function(data){
                if (data =='') {
                    showPageError('Permission failed');
                    return;
                }

                $('#' + fieldId + '_content').html('');
                $('#' + fieldId + '_content').append(data);
                $('#' + fieldId).show();
                $('#' + fieldId).dialog("open");
            }
        });

    }catch(err) {
        showPageError(initializingErrorMessage);
    }
}

function goTo(pUrl) {
    window.location = pUrl;
}

/**
 * Shows messages area if there are any message to display.
 * 
 * @param messageAmount - amount of messages to display.
 */
function showMessages(messageAmount) {
    if (messageAmount > 0) {
        var messageDiv = document.getElementById('messages_container');
        messageDiv.style.display = 'block';
        setTimeout(function() {
            messageDiv.style.display = 'none';
        }, 3000);
    }
}

var requestInProgress = false;
function isRequestInProgress()
{
    if (requestInProgress)
    {
        return true;
    }
       
    requestInProgress = true;
    setTimeout( function() {
        requestInProgress = false;
    }, 1000 );
    return false;
}

function urlEncodeCharacter(c) {
    return '%' + c.charCodeAt(0).toString(16);
};

function urlDecodeCharacter(str, c)
{
    return String.fromCharCode(parseInt(c, 16));
};

function urlEncode(s)
{
    return encodeURIComponent(s).replace( /\%20/g, '+' ).replace( /[!'()*~]/g, urlEncodeCharacter );
};

function urlDecode(s)
{
    return decodeURIComponent(s.replace( /\+/g, '%20' )).replace( /\%([0-9a-f]{2})/g, urlDecodeCharacter);
};
