﻿Ext.ns("OXX.Login");


OXX.Security = Ext.extend(Object, {
    
    response: '',
    message: '',

    constructor: function(config) {

    //    OXX.Security.superclass.constructor.call(this, config);

        this.cookieName = "cookie_login";
        this.loginFlag = false;             // True, when logged

//        this.contentEl = Ext.get(this.schemeId).down('div.mycontent');

  //      this.init();

    },


    cookieValue: function() {

        return OXX.Cookie.read(this.cookieName);
    },

    getHashword: function() {

        return OXX.Cookie.read(this.cookieName);
    },

    init: function() {

    },

    isAnonymous: function() {
        return Ext.isEmpty(OXX.Cookie.read(this.cookieName));
    },


    getMessage: function() {
        return this.message;
    },

    showPersonalia: function() {
        OXX.Util.redirect(1);
    },

    checkResponse: function(t, beSilent) {

        if (t.status == 200) {
            var obj = Ext.decode(t.responseText);
            this.response = obj[0].result;
            this.message = obj[1].message;
            if (this.response != 'ok') {
                if (beSilent)
                    return false;
                else {
                    alert(obj[1].code + ":" + obj[1].message);
                    return false;
                }
            } else {
                return true;
            }
        } else {
            alert('Det har skjedd en teknisk feil:' + t.responseText);
            return false;
        }
    },




    login: function(values) {

    },

    logout: function() {
        OXX.Cookie.create(OXX.Application.security.cookieName, '');
    },



    // declare the actions
    onLogin: function(values) {
        OXX.Cookie.create(OXX.Application.security.cookieName, '');

        OXX.UserActions.login(values['LOGIN_USERNAME'], values['LOGIN_PASSWORD'], function(o, t) {
            var ok = OXX.Application.security.checkResponse(t);
            if (ok) {
                OXX.Cookie.create(OXX.Application.security.cookieName, obj[1].message);
            } else {
                OXX.Cookie.create(OXX.Application.security.cookieName, '');
            }
        }, OXX.Application.security);
    },






    update: function(values) {
        if (!this.isAnonymous()) {
            var cookie = OXX.Cookie.read(this.cookieName);
            OXX.UserActions.update(cookie, values, function(o, t) {
                var ok = this.checkResponse(t);
                if (ok) {
                    return true;
                } else {

                    return false;
                }
            }, this);
        } else {
            alert('Kanke !');
        }

    },

    buy: function(paymethod) {
        if (!this.isAnonymous()) {
            var cookie = OXX.Cookie.read(this.cookieName);
            var items = OXX.Application.getBasketItems();
            OXX.UserActions.buy(cookie, paymethod, items, function(o, t) {


            });

        } else {
            alert(OXX.Cookie.read(this.cookieName));
        }
    }
});



Ext.onReady(function() {

  
    OXX.Application.security = new OXX.Security({
        schemeId: 'mylogin'
    });

});    




