//
// $Id: Login.js,v 1.1 2010/07/10 16:47:59 steve Exp $
//
var Login = function(cookieName) {

    this.username    = ''
    this.password    = ''
    this.loginCookie = cookieName
    
    this.cookie    = new Cookie()
    this.url       = new Url()
    this.validate  = new Validate()
    
    this.init = function() {
    
        var self = this

        $('#username').focus( function() { self.clear($(this), 'Login') })
        $('#password').focus( function() { self.clear($(this), '***') })

        $('#submitLogin').click( function() { self.login() })
        $('#logout').click( function() { self.logout() })

        $('#loginTable .mandatory').keyup( function() { self.validate.checkValid('#loginTable .mandatory') })
    }

    this.clear = function(elem, val) {

        if ($(elem).val() == val) {
            $(elem).val('')
        }
    }

    this.loggedIn = function() {

        var self = this

        return self.cookie.readCookie(this.loginCookie) ? 1 : 0 
    }

    this.logout = function() {

        var self = this
        
        var session = self.cookie.readCookie(this.loginCookie)

        $.ajax({
                  type:     'POST',
                  url:      AJAXURL,
                  data:     'Action=logout&Session=' + session,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    }
                }
        })

        self.cookie.eraseCookie(this.loginCookie)

        if (location.href.match(/cms/)) {
            location.replace('index.php')
        } else {
            location.reload()
        }
        return false ;
    }

    this.login = function(tb) {

        var self = this
        
        self.username   = $('#username').val()
        self.password   = $('#password').val()

        // Local validation
        //
        if (! self.validate.checkValue('#loginTable .mandatory')) {
            alert('Please complete all mandatory fields')
            return
        }

        $.ajax({
                  type:     'POST',
                  url:      AJAXURL,
                  data:     'Action=login&Username=' + self.username
                                      + '&Password=' + self.password,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false
                },
            success:
                function(data) {
                    if(data.Error) {
                        alert(data.Message)
                    } else {
                        self.cookie.createCookie(self.loginCookie, data.Session, 3)
                        location.replace('index.php')
                    }
                }
        })
    }
}

