﻿function changeOverview() {
    //hide details div and show div secure
    $('#details').css('display', 'block');
    $('#documents').css('display', 'none');

    //set tab styles
    $('#overview').removeClass();
    $('#overview').addClass('selectedTab firstTab');

    $('#secure').removeClass();
    $('#secure').addClass('shadowTab dialog');

};

function changeSecure() {
    //hide details div and show div secure
    $('#details').css('display', 'none');
    $('#documents').css('display', 'block');

    //set tab styles
    $('#overview').removeClass();
    $('#overview').addClass('reverseTab firstTab');

    $('#secure').removeClass();
    $('#secure').addClass('selectedTab dialog');
};


$(document).ready(function () {

    //dialog when portfolio page
    $('.dialog').click(function () {

        changeOverview();

        $('#Login').dialog({
            resizable: false,
            height: 274,
            width: 199,
            modal: true,
            //closeText: 'CLOSE',
            buttons: {
                'Sign in': function () {
                    var username = $('#username').val();
                    var password = $('#password').val();

                    if (username != '' && password != '') {
                        $.get('/base/Access/Login/' + username + '/' + password, function (data) {
                            var response = $(data).text();
                            if (response == 'True') {
                                //set cookie to open the secure area when reloading
                                $.cookie('open', 'secure');

                                if ($('#remember').is(':checked')) {
                                    $.cookie('dialogU', username);
                                    $.cookie('dialogP', password);
                                }

                                location.reload();
                            }
                            else {
                                $('#username').addClass('wrongDetail');
                                $('#password').addClass('wrongDetail');
                                $('#message').html('Please verify your credentials');
                            }
                        }); //end get
                    }
                } //end sign in function
            } //end buttons
        }); //end dialog
    }); //end click


    //general dialog for the site page
    $('.dialogMenu a').click(function (ev) {

        ev.preventDefault();
        var $selft = $(this);

        $('#Login').dialog({
            resizable: false,
            height: 274,
            width: 199,
            modal: true,
            //closeText: 'CLOSE',
            buttons: {
                'Sign in': function () {
                    var username = $('#username').val();
                    var password = $('#password').val();

                    if (username != '' && password != '') {
                        $.get('/base/Access/Login/' + username + '/' + password, function (data) {
                            var response = $(data).text();
                            if (response == 'True') {
                                if ($('#remember').is(':checked')) {
                                    $.cookie('dialogU', username);
                                    $.cookie('dialogP', password);
                                }
                                if ($selft.attr('href').indexOf('.ashx') == -1) {
                                    document.location = $selft.attr('href');
                                }
                                //in sec doc
                                else {
                                    document.location = $selft.attr('href');
                                    $('#Login').dialog('close');
                                    $('.dialogMenu a').unbind(); //remove dialog
                                }

                            }
                            else {
                                $('#username').addClass('wrongDetail');
                                $('#password').addClass('wrongDetail');
                                $('#message').html('Please verify your credentials');
                            }
                        }); //end get
                    }
                    else {
                        $('#username').addClass('wrongDetail');
                        $('#password').addClass('wrongDetail');
                        $('#message').html('Please verify your credentials');
                    }
                } //end sign in function
            } //end buttons
        }); //end dialog
    }); //end click

    //Use enter key inside the dialog 
    $('#Login').find('input').keypress(function (e) {

        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $(this).parent().parent().parent().parent().find('.ui-dialog-buttonpane').find('button:first').click(); /* Assuming the first one is the action button */
            return false;
        }
    });

    $('#username, #password').click(function () {
        $('#message').html('');
        $('#username').removeClass('wrongDetail');
        $('#password').removeClass('wrongDetail');
    });

});

