﻿var cookie = {
    create: function(name, value, mins) {
        var expires = '';
        if (mins) {
            var date = new Date();
            date.setTime(date.getTime() + (mins * 60 * 1000));
            expires = '; expires=' + date.toGMTString();
        }
        else {
            expires = '';
        }
        document.cookie = name + '=' + value + expires + '; path=/';
    },
    read: function(name) {
        var strCookie = '; ' + document.cookie + '; ';
        var intBegin = strCookie.indexOf('; ' + name + '=');
        if (intBegin !== -1) {
            var val_begin = (intBegin * -1 - name.length - 3) * -1;
            var qs_val = strCookie.substring(val_begin, strCookie.indexOf('; ', val_begin));
            if (qs_val !== '') {
                return unescape(qs_val.replace(/\+/g, ' '));
            }
            else {
                return '';
            }
        }
        else {
            return '';
        };
    },
    user: {
        userid: null,
        roleid: null,
        firstname: null,
        rcode: null,
        pwd: null,
        un: null,
        pid: null,
        plst:null,
        remind:false,
        renewal:null,
        renewyear:null,
        isRenewedByPC:null,
        isSuperUser:null,
        locationId:null
    },
    init: function() {
        //{login:{userid:'1418', roleid:'6', firstname:'Murali', lastname:'Koujala', rcode:'CM' , pwd:'koujala' ,un : 'murali',pid:'0',plst:'' }}
        var objJson=null;
        var strCookie = cookie.read("pencilauth");
        try {
            objJson = eval("(" + strCookie + ")");
            cookie.user.userid = objJson.login.userid;
            cookie.user.roleid = objJson.login.roleid;
            cookie.user.firstname = objJson.login.firstname;
            cookie.user.lastname = objJson.login.lastname;
            cookie.user.rcode = objJson.login.rcode;
            cookie.user.pwd = objJson.login.pwd;
            cookie.user.un = objJson.login.un;
            cookie.user.pid = objJson.login.pid;
            cookie.user.plst = objJson.login.plst;
            cookie.user.renewal = objJson.login.renewal;
            cookie.user.renewyear = objJson.login.renewyear;
            cookie.user.isRenewedByPC = objJson.login.isRenewedByPC;
            cookie.user.isSuperUser = objJson.login.isSuperUser;
            cookie.user.locationId = objJson.login.locationId;
        }
        catch (e) {
            //alert(e.message)
            
            var lastindex = document.location.href.match(/[/]/g).lastIndex;
            var urllog = document.location.href.substring(lastindex,document.location.href.length);
            var nlocation = 'login.htm?url='+urllog;
            
            if(nlocation.indexOf('page.htm')==-1){
                document.location =nlocation;
            }
            
        }
        
    }
    
}
cookie.init();