/* Summary: Javascript to handle username and password error checks
            and to login
*/

// SIngleton Global Variables
var THICKBOX_G = new function() {
  this.error = {};
}

function ThickboxLogin() {
   this.user = '';
   this.password = '';
   var tmp = this;

   $('#thickbox_login').click(function() {
                               tmp.loginWrapper();
                               }); 
}

// INHERITANCE
ThickboxLogin.prototype = new General();


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


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

// Summary: Get the current domain for the user
ThickboxLogin.prototype.getDomain_ = function() {
    var ref = window.location.href;
    var pattern = /(http:\/\/[^\/]+)/;
    var result = ref.match(pattern);
    var url = 'http://www.friendigo.com:7070/';
    if (result.length) {
        url = result[1];
    } 
    return url;
}


// Summary: Parse the login username and password fields
ThickboxLogin.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: check the login status, if errors exist show the error message
ThickboxLogin.prototype.checkLogin = function(msg) {
    for (var x in msg) {
       var y = '#' + x;
       $(y).show();
       THICKBOX_G.error[x] = 1;
    }
    this.processForm(msg);
}

ThickboxLogin.prototype.ajaxCall_ = function(url, args) {
   var tmpThis = this;
   var len = args.length;
   $.ajax( { type: 'GET',
              url: url,
              dataType: 'json',
              success: function(msg) {
                for (var i=0; i < len; i++) {
                 var method = args[i];
                 tmpThis[method](msg);
                }
              },
              cache: false, 
              timeout: 1000,
              error: function() {
                            alert('an error occurred with AJAX');
                          }
           });
}


// 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
ThickboxLogin.prototype.usernamePasswordWrapper = function() {
    this.user = $('#hiduser2').attr('value');
    this.password = $('#hidpass2').attr('value');

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

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

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

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

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

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

new ThickboxLogin();

