﻿function LoginMouseUp() {
    //$('button_auth').removeClass('blue_active');

}
function LoginMouseDown() {
    //$('button_auth').addClass('blue_active');

}
/* maria for clearin the interest session */
var sessionVartoClear = 'ACreate';
function clearSessionVar(sessionVartoClear) {

    var clearLabelWadjaX = new WADJAX('labels', { statusBar: false });
    clearLabelWadjaX.send('clearSessionVar', sessionVartoClear, function (res) {

    });
    
    if ($('dummy')) {
        if (!$('dummy').hasClass('dndD')) {
           $('dummy').addClass('dndD');
        }
    }   
}

function Login() {
    try {
        if ($('button_auth'))
            $('button_auth').removeClass('blue_active');
        try {
            HideDropdown();
        } catch (fError) { }
        var oWadjaX = new WADJAX('User', { statusBar: false });

        var un = $('text_username').get('value');
        var pw = $('text_pass').get('value');
        var chk = $('chk_remember').checked;
        if (chk) { chk = "1"; } else { chk = "0"; }

        if (un.length > 0 && pw.length > 0) {
            //#Disable multiple clicks
            if ($('button_auth')) {
                $('button_auth').disabled = 1;
                //$('button_auth').set('value', 'working...');
                $('button_auth').set('value', login_working);
            }
            //#Home page sponsored labels project
            var Labels = '';
            try { Labels = randomlabels_array; }
            catch (Error) { }
            var domain = '';
            if ($('networkname'))
                domain = $('networkname').get('html').replace('.', '');
            oWadjaX.send('signIn', un, pw, chk, Labels, domain, function (res) {
                if (!res.error) {
                    if (res.value.indexOf('Error: ') != 0) { //#if the result string does not start with 'Error: '
                        try {
                            if (res.value.indexOf('#FF#') > -1) {
                                top.location.href = res.value.replace("#FF#", "");
                            }
                            else {
                                if (mp_metrix) mpmetrics.track("Sign In processed");
                                var userObject = JSON.decode(res.value);
                                //#if Login was done through "Third-party Connect" page
                                //#make the Third-party connection
                                if (top.location.pathname == "/sessions/connect.aspx") {
                                    if (!ThirdPartyConnect(userObject.DetailsID))
                                        ; // connection not made; already used for another wadja user
                                }
                                //#overlay preloading
                                //if ($('overlaymask'))
                                //    $('overlaymask').removeClass('dnd');

                                //alert(location.pathname);
                                if (top.location.pathname == '/sessions/landingpage.aspx') {
                                    top.location.href = currentUrl.replace("user/default.aspx?u=", "");
                                    top.location.hash = '#!My/' + labelID + '/1';
                                }
                                else if (top.location.pathname == "/sessions/connect.aspx") {
                                    top.location.href = '/'; // currentUrl.replace("user/default.aspx?u=", "");
                                }
                                else {
                                    if (currentUrl.indexOf('?u=no') > -1)
                                        top.location.href = '/' + userObject.WadjaID;
                                    else
                                        top.location.href = currentUrl.replace("user/default.aspx?u=", "");
                                }
                                //$('button_auth').set('value', 'redirecting...');
                                if ($('button_auth'))
                                    $('button_auth').set('value', login_redirecting);
                            }

                        }
                        catch (Error) {
                            if (mp_metrix) mpmetrics.track("Sign In processed");
                            var userObject = JSON.decode(res.value);
                            top.location.href = '/' + userObject.WadjaID;
                            if ($('button_auth'))
                                $('button_auth').set('value', login_redirecting);
                        }
                    }
                    else {
                        $('auth_res_1').set('html', res.value.replace('Error: ', ''));
                        MyElement.reveal('auth_res_div_1');
                        //#Restore button functionality
                        if ($('button_auth')) {
                            $('button_auth').disabled = 0;
                            //$('button_auth').set('value', 'sign in');
                            $('button_auth').set('value', login_signBtn);
                        }
                    }
                }
                else {
                    //#Restore button functionality
                    if ($('button_auth')) {
                        $('button_auth').disabled = 0;
                        //$('button_auth').set('value', 'sign in');
                        $('button_auth').set('value', login_signBtn);
                    }
                }
            });
        }
        else {
            //$('auth_res_1').set('html', 'Username and Password must be filled');
            $('auth_res_1').set('html', login_enterCredentials);
            //#Restore button functionality
            if ($('button_auth')) {
                $('button_auth').disabled = 0;
                //$('button_auth').set('value', 'sign in');
                $('button_auth').set('value', login_signBtn);
            }
            MyElement.reveal('auth_res_div_1');
        }
    }
    catch (Error) { alert(Error); }
}
function handleEnter_login(field, event) {
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13) {
        Login();
        return false;
    }
    else
        return true;
}
function ThirdPartyConnect(detailsID) {
    try {
        //make third party connection
        var thirdPartyWadjaX = new WADJAX('User', { statusBar: false });
        thirdPartyWadjaX.send('ThirdPartyConnect', detailsID, function (res) {
            if (!res.error) {
                if (res.value == '-2') { //connection already exists for a different user
                    return false;
                }
                else { //connection made!
                    return true;
                }
            }
            else {
                //error occurred...
                return false;
            }
        });

    }
    catch (e) {
        alert(e.Message);
        return false;
    }
}

function HideDropdown_popup() {
    if (document.getElementById('frame_file').contentWindow.$('networkname')) {
        //change to inactive 
        document.getElementById('frame_file').contentWindow.$('networkname').removeClass('activenetworkname');
        document.getElementById('frame_file').contentWindow.$('networkname').addClass('inactivenetworkname');
    }
    if (document.getElementById('frame_file').contentWindow.$('networknameDropdown')) {
        //hide values
        document.getElementById('frame_file').contentWindow.$('networknameDropdown').addClass('dndD');
    }
}

function HideDropdown() {
    if ($('networkname')) {
        //change to inactive 
        $('networkname').removeClass('activenetworkname');
        $('networkname').addClass('inactivenetworkname');
    }
    if ($('networknameDropdown')) {
        //hide values
        $('networknameDropdown').addClass('dndD');
    }
}
