﻿
function load() {
    var r = document.getElementById('tContentRight');
    var m = document.getElementById('tContentMiddle');    
    m.setAttribute('style', 'height:' + r.offsetHeight + 'px;');
}


function OpenWindow(url_, height_, width_) 
{
    var win = new Window({ className: 'alphacube', width: width_, height: height_, resizable: false, showEffect: Effect.BlindDown, hideEffect: Effect.SwitchOff, draggable: false, minimizable: false, maximizable: false });    
    win.setAjaxContent(url_, null, true, true)    
    win.setCloseCallback(function() { win.hide(); win.destroy(); });
}

function OpenAuthentication(RedirectURL) 
{
    var url = 'Authentication.ashx';
    new Ajax.Request(url, {
        method: 'post',
        parameters: 'a=status',
        asynchronous: false,
        onSuccess: function(answer) {
            var r = answer.responseText.evalJSON();
            if (r.status == true) {
                if (confirm("Tem a certeza que deseja terminar a sua Sessão!?")) {
                    new Ajax.Request(url, {
                        method: 'post',
                        parameters: 'a=logout',
                        asynchronous: false,
                        onSuccess: function(answer) {
                            window.location = 'Default.aspx'
                        }
                    });
                }
            }
            if (r.status == false) {
                OpenWindow('aLogin.aspx?r=' + RedirectURL, 155, 305);
            }
        }
    });

}

function ValidateField(idField, idAnswer, NameField) 
{
    var title = $(idField).value.trim();

    if (title.length < 4) 
    {
        $(idAnswer).update('O ' + NameField + ' é obrigatório!');
        $(idField).focus();
        return false;
    }
    
    return true;
}

function ValidateNewInsert() {
    return ValidateProductTitle() && ValidateImage();
}
function ValidateNewUpdate() {
    var image = $('fplImage').value.trim();

    if (image.length < 1)
        return ValidateProductTitle();
    else
        return ValidateProductTitle() && ValidateImage();
}

function ValidateProductTitle() {
    var title = $('txbTitle').value.trim();

    if (title.length < 4) {
        $('pStatus').update('O Título é obrigatório!');
        $('txbTitle').focus();
        return false;
    }
    return true;
}
function ValidateImage() {
    var image = $('fplImage').value.trim();
    var ext = image.substring(image.lastIndexOf('.') + 1).toLowerCase();

    if (ext != 'gif' && ext != 'jpeg' && ext != 'jpg' && ext != 'png') {
        $('pStatus').update('A imagem é obrigatória!');
        $('fplImage').focus();
        return false;
    }
    return true;
}

function ValidateLogin() 
{
    var username = $('txbUsername').value.trim();
    var password = $('txbPassword').value.trim();
    if (username.length < 1) {
        $('lblStatusLogin').update('O Username é obrigatório!');
        $('txbUsername').focus();
        return false;
    }
    if (password.length < 1) {
        $('lblStatusLogin').update('A Password é obrigatório!');
        $('txbPassword').focus();
        return false;
    }

    var r = new Ajax.Request('Authentication.ashx', {
        method: 'post',
        parameters: 'a=login&u=' + username + '&p=' + password,
        asynchronous: false,
        onSuccess: function(answer) {
            var r = answer.responseText.evalJSON();
            if (r.status)
                window.location = window.location.href;
            else
                $('lblStatusLogin').update('Login Inválido! :(');
        },
        onFailure: function(answer) {
            $('lblStatusLogin').update('Erro! tente mais tarde...');
        }
    });
    return false;
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}
