var saja = {
    procOn:0,
    ClearAlert: function(doClear){
        if(!doClear){
            if(saja.timeOut)
                clearTimeout(saja.timeOut)
            saja.timeOut = setTimeout(function(){saja.ClearAlert(1)},6000)
        }
        else{
            ob=document.getElementById('alertOutput');
            ob.innerHTML = '';
            clearTimeout(saja.timeOut)
        }
    },
    ChangeIndicator: function(ob,procStatus){
        var s = document.getElementById('sajaStatus');
        if(!s) return;
        //saja is working
        if(procStatus){
            if(ob && ob.style) ob.style.cursor = 'wait';
            document.body.style.cursor = 'wait';
            s.style.visibility = 'visible';
        }
        //saja is finished
        else{
            if(ob && ob.style) ob.style.cursor = 'default';
            document.body.style.cursor = 'default';
            if(s) s.style.visibility = 'hidden';
        }
    },
    NewReq: function(){
        if(window.XMLHttpRequest)
            {
            try{return new XMLHttpRequest();}catch(e){}
            }
        else if(window.ActiveXObject)
            {
            try{return new ActiveXObject("Microsoft.XMLHTTP");}catch(e){
                try{return new ActiveXObject("Msxml2.XMLHTTP");}catch(e){}}
            }
    return null;
    },
    run: function(php, id, act, property, ob, session_id, proc_file)
        {
        if(!proc_file) proc_file = '';  
        var req = saja.NewReq();
        if(req)
            {
            if(!saja.procOn) saja.ChangeIndicator(ob,1);
            saja.procOn++;
            saja.ExecReq(req, php, id, act, property, SAJA_PATH + 'SjVis/saja.process.php', ob, session_id, proc_file);
            }
    },
    ExecReq: function(req, args, id, act, property, file, ob, session_id, proc_file){
        req.open('POST',file,true);


        //req.send((SAJA_HTTP_KEY ? escape(saja.rc4(SAJA_HTTP_KEY, args)) : args) + '<!SAJA!>' + session_id + '<!SAJA!>' + proc_file);

        req.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');

        // create string to send
        var to_send = (SAJA_HTTP_KEY ? escape(saja.rc4(SAJA_HTTP_KEY, args)) : args) + '<!SAJA!>' + session_id + '<!SAJA!>' + proc_file;

        // send query
            req.send( to_send );

        req.onreadystatechange=function()
            {
            if (req.readyState==4 && req.status==200)
                {
                actions = req.responseText.split('<saja_split>');       
                actions[0] = unescape(actions[0]);

                if(actions[0]) saja.Put(actions[0], id, act, property)
                if(actions[1]) eval(actions[1]);

                saja.procOn--;

                if(!saja.procOn) saja.ChangeIndicator(ob,0);
                }
            }
    },
    Put: function(content, id, act, property){
        if(!id) return;
        if(property){
            try{ob = document.getElementById(id);}catch(e){}
            if(act=='p')
                ob[property] = content + ob[property];
            else if(act=='a')
                ob[property] += ob[property];
            else if(property.split('.')[0]=='style')
                ob.style[property.split('.')[1]] = content;
            else
                ob[property] = content;
        }
        else
            window[id] = content;
    },
    Get: function(id, property){
        if(!property) return saja.phpSerialize(id);
    
        // Object
        var obj = document.getElementById(id);

        // Value of object
        var value = "";

        // check for multy object
        if( (obj[property]!=undefined)&&(property=='options') )
            {
            // run over obj
            for(var q=0;q<obj.options.length;q++)
                {
                if (obj.options[ q ].selected) value = value + obj.options[ q ].value + '=$|$=';
                }
            }
            else/**/
            {
            // Get property
            value = obj[property];
            }

        return saja.phpSerialize(value);
    },
    phpSerialize: function(v){
        var ret = '';
        if(typeof(v)=='object'){
            var len = v.length;
            if(len){
                ret = 'a:' + len + ':{';
                for(var i=0; i<len; i++){
                    ret += 'i:' + i +';'
                    ret += 's:' + (v[i]+'').length + ':"' + escape(v[i]) +'";'
                }
                ret += '}';
            } else {    
                len = 0;
                for(var i in v) len++;
                ret = 'a:' + len + ':{';
                for(var i in v){
                    ret += 's:' + i.length + ':"' + i +'";'
                    ret += 's:' + v[i].length + ':"' + escape(v[i]) +'";'
                }
                ret += '}';
            }
        } else {
            ret += 's:' + (v+'').length + ':"' + escape(v) +'";'
        }

        return ret;
    },
    SetStyle: function(ob, styleString){
        document.getElementById(ob).style.cssText = styleString;    
    },
    rc4: function(pwd, data){
        pwd_length = pwd.length;
        data_length = data.length;
        var key = []; var box = [];
        var cipher = '';
        for (var i=0; i < 256; i++){
            key[i] = pwd.charCodeAt(i % pwd_length);
            box[i] = i;
        }
        for (j = i = 0; i < 256; i++){
            j = (j + box[i] + key[i]) % 256;
            tmp = box[i];
            box[i] = box[j];
            box[j] = tmp;
        }
        for (a = j = i = 0; i < data_length; i++){
            a = (a + 1) % 256;
            j = (j + box[a]) % 256;
            tmp = box[a];
            box[a] = box[j];
            box[j] = tmp;
            k = box[((box[a] + box[j]) % 256)];
            cipher += String.fromCharCode(data.charCodeAt(i) ^ k);
        }
        return cipher;
    },
    getForm: function(f){
        var vals = {};
        for(var i=0; i<f.length; i++)
            if(f[i].id)
                vals[f[i].id] = f[i].value;
        return vals;
    }
}