
/* Summary: This is the for regular login. Code is similar to thickbox_login.js
 * It was taken out because the thickbox_login is used for the thickbox template
 * */

var REGLOGIN_G = new function() {
   this.error = {};
}


function RegularLogin() {

 var tmp = this;
 $('#r_login').bind('click', function() {
                      return tmp.loginWrapper();        
                    });

}


RegularLogin.prototype = new General();

// Summary: Wrapper for field checks
RegularLogin.prototype.loginWrapper = function() {
    var error = this.usernamePasswordWrapper();
    // return back to callee if error exist
    if (error) {
        this.processForm();
        return false;
    } else {
        this.parseLoginChecks();
    }
}

// Summary: check the login status, if errors exist show the error message
RegularLogin.prototype.checkLogin = function(msg) {
    for (var x in msg) {
        var y = '#' + x + '_';
       $(y).show();
       REGLOGIN_G.error[x] = 1;
    }
    this.processForm(msg);
}


// Summary: Parse the login username and password fields
RegularLogin.prototype.parseLoginChecks = function() {
    var dmain = this.getDomain();
    var user = this.user;
    var password = this.password;
    var lpath = '/ajax/LUSER/' + user + '/LPASSWORD/' + password;
    var url = dmain + lpath;
    this.ajaxCall(url, ['checkLogin']); 
}


// Summary: Process form, either go ahead with registration of show login errors
// params: msg - json object
RegularLogin.prototype.processForm = function(msg) {
   if (msg) { //make sure msg exist
       if (msg['success'] == 0) { // successful login
           document.form_ltbox.submit();
       }
   }
}

// Summary: Check and set erors for username and password strings in addition
//          toggle  hide() and set the proper error checking 'err_login_inv_[username,password]'
//          This function will always be called to check username,password string before ajax call
RegularLogin.prototype.usernamePasswordWrapper = function() {
    this.user = $('#hiduser').attr('value');
    this.password = $('#hidpass').attr('value');

    REGLOGIN_G.error['hiduser'] = this.checkString(this.user, '#err_login_empty_');
    REGLOGIN_G.error['hidpass'] = this.checkString(this.password,'#err_login_empty_');

    // resetting the username, password string error checks
    if (!REGLOGIN_G.error['hiduser'] && !REGLOGIN_G.error['hidpass']) {
       $('#err_login_empty_').hide();
    }

    // turn off previous check until next ajax process login request
    if (REGLOGIN_G.error['err_login_inv_password'] == 1) { 
       $('#err_login_inv_password_').hide();
       REGLOGIN_G.error['err_login_inv_password'] = 0;
    } 

    // turn off previous check until next ajax process login request
    if (REGLOGIN_G.error['err_login_inv_username'] == 1) {
       $('#err_login_inv_username_').hide();
       REGLOGIN_G.error['err_login_inv_username'] = 0;
    }

    return REGLOGIN_G.error['hiduser'] || REGLOGIN_G.error['hidpass'];
}

// Check to ensure user inserted some information
RegularLogin.prototype.checkString = function(str, id) {
    var error = 0;
    if (!str || (str.length == 0)) {
        error = 1;
        $(id).show();
    } 
    return error;
}

new RegularLogin();

