function reloadCurrentPage() {
    window.top.location.reload();
    return false;
}

function isset(variable) {
    if (typeof variable != 'undefined')
        return true;
    else
        return false;
}

function gotoURL(url) {
    location.href = url;
    stopEventBubble();
    return false;
}

function setWindowURL(url) {
    window.location.href = url;
    stopEventBubble();
    return false;
}

function reloadImage(img) {
	var src = img.attr('src');
	src = src.replace(/\?.*/gi, "") + "?rnd=" + randomString();
	img.attr('src', src);
}

function displayLoadingCursor(state) {
    if (state == true) {        
        document.body.style.cursor = 'progress';
    } else {       
        document.body.style.cursor = 'default';
    }
}

function passwordStrength(password) {

    var score   = 0;

    //if password bigger than 6 give 1 point
    if (password.length > 6) score++;

    //if password has both lower and uppercase characters give 1 point

    if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) ) score++;

    //if password has at least one number give 1 point
    if (password.match(/\d+/)) score++;

    //if password has at least one special caracther give 1 point
    if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) ) score++;

    //if password bigger than 12 give another 1 point
    if (password.length > 12) score++;

    document.getElementById('passwordDescription').innerHTML = passwordStrengthDescription[score];
    document.getElementById('passwordStrength').className = 'strength' + score;
}

function eventGetKeyPressed(e) {
    var keyCode;
    if (!e) var e = window.event;
    if (e.keyCode) keyCode = e.keyCode;
    else if (e.which) keyCode = e.which;

    var keyChar = String.fromCharCode(keyCode);
    return keyChar;
}


function eventGetKeyCodePressed(e) {
    var keyCode;
    if (!e) var e = window.event;
    if (e.keyCode) keyCode = e.keyCode;
    else if (e.which) keyCode = e.which;

    return keyCode;
}

function stopEventBubble(e) {
    if (!e) var e = window.event;
    e.cancelBubble = true; // Internet Explorer
    if (e.stopPropagation)
        e.stopPropagation(); // Mozilla/W3C
}

function getFileExtension(filename) {
    return typeof filename != 'undefined' ? filename.substring(filename.lastIndexOf('.')+1, filename.length).toLowerCase() : false;
}

function getFilename(filepath) {    
    return typeof filepath != 'undefined' ? filepath.substring(filepath.replace(/\\/g, '/').lastIndexOf('/')+1, filepath.length) : false;    
}

function appendToFilename(filepath, appended_string) {
    var basename = filepath.substring(filepath.replace(/\\/g, '/').lastIndexOf('/')+1, filepath.length);
    
    var filename = basename.substring(0, basename.lastIndexOf('.'));
    return filepath.replace(filename, filename + appended_string);
}

function randomString() {
    return String((new Date()).getTime()).replace(/\D/gi,'');
}

function randomPassword(length) {
    if (typeof length == 'undefined')
        length = 8;
    length +=0;

    var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!#%&*_';
    var pass = '';
    for(x=0;x<length;x++) {
        i = Math.floor(Math.random() * chars.length);
        pass += chars.charAt(i);
    }
    return pass;
}


function confirmBox(options) {

    var confirm_dialog = $('<div>').attr('id', 'confirm-dialog-box').attr('title', options.title);
    var dialog_p = $('<p>').html(options.message).appendTo(confirm_dialog);
    $('<span>')
    .addClass('ui-icon')
    .addClass('ui-icon-alert')
    .css('float', 'left')
    .css('margin', '0px 7px 40px 0px')
    .prependTo(dialog_p);

    confirm_dialog.appendTo($('body'));

    confirm_dialog.dialog({
        autoOpen: false,
        closeOnEscape: false,
        resizable: false,
        height:180,
        width:420,
        modal: true
    });

    confirm_dialog.dialog( 'option', 'buttons', [
    {
        text: options.accept_label,
        click: function() {
            if (typeof options.accept_func == 'function') {
                options.accept_func();
            }
            $(this).dialog('close');
            $(this).remove();
        }
    },
    {
        text: options.cancel_label,
        click: function() {
            if (typeof options.cancel_func == 'function') {
                options.cancel_func();
            }
            $(this).dialog('close');
            $(this).remove();
        }
    }
    ] );

    confirm_dialog.dialog('open');
}


function promptBox(options) {

    var prompt_dialog = $('<div>').attr('id', 'prompt-dialog-box').attr('title', options.title);
    var dialog_p = $('<p>').html(options.message).appendTo(prompt_dialog);
    $('<span>')
    .addClass('ui-icon')
    .addClass('ui-icon-alert')
    .css('float', 'left')
    .css('margin', '0px 7px 40px 0px')
    .prependTo(dialog_p);
    $('<br>').appendTo(prompt_dialog);

    var prompt_input_text = $('<input>').attr({
        'type': 'text',
        'id': 'prompt_input_text'
    })
    .css('width', '100%')
    .val(options.default_value).appendTo(prompt_dialog);

    prompt_dialog.appendTo($('body'));

    prompt_dialog.dialog({
        autoOpen: false,
        closeOnEscape: false,
        resizable: false,
        height:180,
        width:420,
        modal: true
    });

    prompt_dialog.dialog( 'option', 'buttons', [
    {
        text: options.accept_label,
        click: function() {
            if (typeof options.accept_func == 'function') {
                options.accept_func(prompt_input_text.val());
            }
            $(this).dialog('close');
            $(this).remove();
        }
    },
    {
        text: options.cancel_label,
        click: function() {
            if (typeof options.cancel_func == 'function') {
                options.cancel_func();
            }
            $(this).dialog('close');
            $(this).remove();
        }
    }
    ] );

    prompt_dialog.dialog('open');
}


function alertBox(options) {

    var alert_dialog = $('<div>').attr('id', 'alert-dialog-box').attr('title', options.title);
    var dialog_p = $('<p>').html(options.message).appendTo(alert_dialog);
    $('<span>')
    .addClass('ui-icon')
    .addClass('ui-icon-information')
    .css('float', 'left')
    .css('margin', '0px 7px 40px 0px')
    .prependTo(dialog_p);

    alert_dialog.appendTo($('body'));

    alert_dialog.dialog({
        autoOpen: false,
        closeOnEscape: false,
        resizable: false,
        height:180,
        width:420,
        modal: true
    });

    alert_dialog.dialog( 'option', 'buttons', [
    {
        text: options.accept_label,
        click: function() {
            if (typeof options.accept_func == 'function') {
                options.accept_func();
            }
            $(this).dialog('close');
            $(this).remove();
        }
    } ] );

    alert_dialog.dialog('open');
}


function makeFormFieldsToRequestVariables(request, container) {
    $('#' + container + ' input[type="checkbox"]:checked').each(
        function() {
            if ($(this).attr('disabled') == false) {
                request.setVar($(this).attr('name'), $(this).val());
            }
        }
        );
    $('#' + container + ' input[type="radio"]:checked').each(
        function() {
            if ($(this).attr('disabled') == false) {
                request.setVar($(this).attr('name'), $(this).val());
            }
        }
        );
    $('#' + container + ' input[type="text"]').each(
        function() {
            if ($(this).attr('disabled') == false) {
                request.setVar($(this).attr('name'), $(this).val());
            }
        }
        );
    $('#' + container + ' input[type="hidden"]').each(
        function() {
            if ($(this).attr('disabled') == false) {
                request.setVar($(this).attr('name'), $(this).val());
            }
        }
        );

    $('#' + container + ' textarea').each(
        function() {
            if ($(this).attr('disabled') == false) {
                var tinymce = $('#' + $(this).attr('id')).tinymce();
                if (tinymce != null) {
                    request.setVar($(this).attr('name'), tinymce.getContent());
                } else {
                    request.setVar($(this).attr('name'), $(this).val());
                }
            }
        }
        );

    $('#' + container + ' select').each(
        function() {
            if ($(this).attr('disabled') == false) {
                var Name = $(this).attr('name');

                if ($(this).attr('multiple') == true) {

                    var j = 0;
                    $('#' + $(this).attr('id') + ' :selected').each(
                        function() {
                            var NewName = Name.replace(/\[([^\]]*)\]/, '[' + j + ']');
                            request.setVar(NewName, $(this).val());
                            j++;
                        }
                        );

                } else {
                    $('#' + $(this).attr('id') + ' :selected').each(
                        function() {
                            request.setVar(Name, $(this).val());
                        }
                        );
                }
            }
        }
        );
}


function sendFriendMail(mail_options) {
    var recipient_address = '';

    if (typeof mail_options.to_address != 'undefined') {
        recipient_address = mail_options.to_address;
        mail_options.to_address = null;
    }

    var msgForm = $('<form>').attr({
        id: 'mailSendForm',
        action: 'mailto:' + recipient_address,
        enctype: 'application/x-www-form-urlencoded',
        'accept-charset': 'iso-8859-7, windows-1253',
        method: 'get'
    }).appendTo('body');

    $.each( mail_options, function(opt_name, opt_value){
        $('<textarea>').attr({
            'name': opt_name,
            'value': decodeURIComponent(opt_value)
        }).appendTo(msgForm);
    });

    msgForm.submit();
    //msgForm.remove();
    return false;
}
