var ajaxText = new Array();
var ajaxJson = new Array();
var ajaxOnWork = 0;

function getHttpRequest()
{
    var hr;

    if (window.XMLHttpRequest)    { // Mozilla, Safari, IE7+ ...
        hr = new XMLHttpRequest();
        if (hr.overrideMimeType) hr.overrideMimeType('text/xml'); //for mazilla wa
    } else if (window.ActiveXObject) { // IE 5/6
        try    {
            hr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e)    {
            try    {
                hr = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e)    {}
        }
    }

    return hr;
}

function postAjaxPageText(u,p,f,id)
{
    var hr = getHttpRequest();

    if (!hr) return false;
    if( ++ajaxOnWork == 1 ) waitIcon(true);
    hr.onreadystatechange = function() { ajaxPostRetriveDataText(id,hr,f); };
    hr.open('POST', u, true);
    hr.setRequestHeader("Method", "POST "+u+" HTTP/1.1");
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.setRequestHeader("Content-length", p.length);
    hr.setRequestHeader("Connection", "close");
    hr.send(p);
    return true;
}

function postAjaxPageJson(u,p,f,id)
{
    var hr = getHttpRequest();

    if (!hr) return false;
    if( ++ajaxOnWork == 1 ) waitIcon(true);
    hr.onreadystatechange = function() { retriveAjaxItemsJson(id,hr,f); };
    hr.open('POST', u, true);
    hr.setRequestHeader("Method", "POST "+u+" HTTP/1.1");
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.setRequestHeader("Content-length", p.length);
    hr.setRequestHeader("Connection", "close");
    hr.send(p);
    return true;
}


function ajaxPostRetriveDataText(id,hr,callback) {
    if (!hr.readyState) return;

    if (hr.readyState == 4)    {
        if( !id || id == null ) id = 'null';
        if (hr.status == 200) { // perfect!
            ajaxText[id] = hr.responseText.split('\n');
        } else {
            ajaxText[id] = new Array(); // there was a problem with the request ... 404 (Not Found) 500 (Internal Server Error)
        }
        if( callback != null && callback != '') callback(id);
        if( --ajaxOnWork==0 ) waitIcon(false);
    }
    else {
        // still not ready
    }
}
function postAjaxFormJson(u,f)
{
    var p='',a=postAjaxFormJson.arguments;
    if( a.length<4 ) return;

    if(u=='') u='save2session.php';

    for(var i=2, n=1; i<(a.length-1); i+=2, n++) {
        p += '&'+encodeURIComponent( a[i] )+'='+encodeURIComponent( a[i+1] );
    }
    postAjaxPageJson('ajax/'+u, p.substring(1), f, null);
}
function postAjaxForm(u,f)
{
    var p='',a=postAjaxForm.arguments;
    if( a.length<4 ) return;

    if(u=='') u='save2session.php';

    for(var i=2, n=1; i<(a.length-1); i+=2, n++) {
        p += '&'+encodeURIComponent( a[i] )+'='+encodeURIComponent( a[i+1] );
    }
    postAjaxPageText('ajax/'+u, p.substring(1), f, null);
}
function queryAjaxValuePair2Session(u,f)
{
    var p='',a=queryAjaxValuePair2Session.arguments;
    if( a.length<3 ) return;

    if(u=='') u='read2session.php';

    for(var i=2, n=1; i<a.length; i++, n++) {
        p += '&n'+n+'='+encodeURIComponent( a[i] );
    }
    postAjaxPageText('ajax/'+u, p.substring(1), f, null);
}
function queryAjaxSynclocalizedMsg() {
    var p='',a=queryAjaxSynclocalizedMsg.arguments;
    if( a.length<1 ) return;

    for(var i=0; i<a.length; i++) {p += '&n'+(i+1)+'='+encodeURIComponent( a[i] );}

    var hr = getHttpRequest();
    if (!hr) return false;
    if( ++ajaxOnWork == 1 ) waitIcon(true);
    hr.open('POST', 'ajax/readlocalizedmsg.php', false);
    hr.setRequestHeader("Method", "POST readlocalizedmsg.php HTTP/1.1");
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.setRequestHeader("Content-length", p.length-1);
    hr.setRequestHeader("Connection", "close");
    hr.send(p.substring(1));
    if (hr.status == 200) ajaxText['null'] = hr.responseText.split('\n');
    else ajaxText['null'] = new Array();
    if( --ajaxOnWork==0 ) waitIcon(false);
}
function queryAjaxSyncValuePair2Session(u) {
    var p='',a=queryAjaxSyncValuePair2Session.arguments;
    if( a.length<2 ) return;
    if(u=='') u='read2session.php';

    for(var i=1; i<a.length; i++) {p += '&n'+i+'='+encodeURIComponent( a[i] );}

    var hr = getHttpRequest();
    if (!hr) return false;
    if( ++ajaxOnWork == 1 ) waitIcon(true);
    hr.open('POST', 'ajax/'+u, false);
    hr.setRequestHeader("Method", "POST "+u+" HTTP/1.1");
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.setRequestHeader("Content-length", p.length-1);
    hr.setRequestHeader("Connection", "close");
    hr.send(p.substring(1));
    if (hr.status == 200) ajaxText['null'] = hr.responseText.split('\n');
    else ajaxText['null'] = new Array();
    if( --ajaxOnWork==0 ) waitIcon(false);
}
function postAjaxValuePair2Session(u,f)
{
    var p='',a=postAjaxValuePair2Session.arguments;
    if( a.length<4 ) return;

    if(u=='') u='save2session.php';

    for(var i=2, n=1; i<(a.length-1); i+=2, n++) {
        p += '&n'+n+'='+encodeURIComponent( a[i] )+'&v'+n+'='+encodeURIComponent( a[i+1] );
    }
    postAjaxPageText('ajax/'+u, p.substring(1), f, null);
}
function postAjaxResetPair2Session(u,f)
{
    var p='',a=postAjaxResetPair2Session.arguments;
    if( a.length<3 ) return;

    if(u=='') u='reset2session.php';

    for(var i=2, n=1; i<a.length; i++, n++) {
        p += '&n'+n+'='+encodeURIComponent( a[i] );
    }
    postAjaxPageText('ajax/'+u, p.substring(1), f, null);
}
function getAjaxPage(page,callback)
{
    var hr = getHttpRequest();

    if (!hr) return false;

    if( ++ajaxOnWork == 1 ) waitIcon(true);
    hr.onreadystatechange = function() { retriveAjaxText(hr,callback); };
    hr.open('GET', 'ajax/'+page, true);
    hr.send(null);
    return true;
}
function getAjaxText(page,callback)
{
    var hr = getHttpRequest();

    if (!hr) return false;

    if( ++ajaxOnWork == 1 ) waitIcon(true);
    hr.onreadystatechange = function() { retriveAjaxText(hr,callback); };
    hr.open('GET', 'ajax/txt/'+page, true);
    hr.send(null);
    return true;
}
function getAjaxItemsText(id,page,callback)
{
    var hr = getHttpRequest();

    if (!hr) return false;

    if( ++ajaxOnWork == 1 ) waitIcon(true);
    hr.onreadystatechange = function() { retriveAjaxItemsText(id,hr,callback); };
    hr.open('GET', 'ajax/txt/'+page, true);
    hr.send(null);
    return true;
}


function retriveAjaxText(hr,callback)
{
    if (!hr.readyState) return;

    if (hr.readyState == 4)    {
        var s;
        if (hr.status == 200) { // perfect!
            s = hr.responseText;
        } else {
            s = "err" + hr.status; // there was a problem with the request ... 404 (Not Found) 500 (Internal Server Error)
        }
        if( callback != null && callback != '') callback(s);
        if( --ajaxOnWork==0 ) waitIcon(false);
    }
    else {
        // still not ready
    }
}
function retriveAjaxItemsText(id,hr,callback)
{
    if (!hr.readyState) return;

    if (hr.readyState == 4)    {
        if (hr.status == 200) { // perfect!
            ajaxText[id] = hr.responseText.split('\n');
        } else {
            ajaxText[id] = new Array(); // there was a problem with the request ... 404 (Not Found) 500 (Internal Server Error)
        }
        if( callback != null && callback != '') callback();
        if( --ajaxOnWork==0 ) waitIcon(false);
    }
    else {
        // still not ready
    }
}

function getAjaxItemsJson(id,page,callback)
{
    var hr = getHttpRequest();

    if (!hr) return false;

    if( ++ajaxOnWork == 1 ) waitIcon(true);
    hr.onreadystatechange = function() { retriveAjaxItemsJson(id,hr,callback); };
    hr.open('GET', 'ajax/json/'+page, true);
    hr.send(null);
    return true;
}

function retriveAjaxItemsJson(id,hr,callback)
{
    if (!hr.readyState) return;

    if (hr.readyState == 4 ) {
        if (hr.status == 200) { // perfect!
            try { ajaxJson[id] = eval("(" + hr.responseText + ")"); }
            catch(e) {ajaxJson[id] = new Array(); } // no json data
        }
        else {
            ajaxJson[id] = new Array(); // there was a problem with the request ... 404 (Not Found) 500 (Internal Server Error)
        }
        if( callback != null && callback != '') callback();
        if( --ajaxOnWork==0 ) waitIcon(false);
    }
    else {
        // still not ready
    }
}
/* */
function selectFromJson(id)
{
    if( !ajaxJson[id] || ajaxJson[id].length == 0 ) return;

    var obj = document.getElementById(id);
    if( !obj ) return false;

    for(var i=0; i < ajaxJson[id].length; i++ )
    {
        if( !ajaxJson[id][i].cod ) break;
        if( !ajaxJson[id][i].desc ) break;
        if( isIE ) obj.add(new Option(ajaxJson[id][i].desc,ajaxJson[id][i].cod));
        else obj.add(new Option(ajaxJson[id][i].desc,ajaxJson[id][i].cod),null);
    }
    return;
}
function selectFromJsonWithDef(id,cur)
{
    if( !ajaxJson[id] || ajaxJson[id].length == 0 ) return;

    var obj = document.getElementById(id),sel=false;
    if( !obj ) return false;

    for(var i=0; i < ajaxJson[id].length; i++ )
    {
        if( !ajaxJson[id][i].cod ) break;
        if( !ajaxJson[id][i].desc ) break;
        if( ajaxJson[id][i].cod == cur ) sel=true; else sel=false;
        if( isIE ) {
            obj.add(new Option(ajaxJson[id][i].desc,ajaxJson[id][i].cod,sel));
            if( sel ) obj.selectedIndex=obj.options.length-1;
        }
        else obj.add(new Option(ajaxJson[id][i].desc,ajaxJson[id][i].cod,sel),null);
    }
    return;
}
function selectFromJsonObj(json,id)
{
    if( !json || json.length == 0 ) return false;

    var obj = document.getElementById(id);
    if( !obj ) return false;

    for(var i=0; i < json.length; i++ )
    {
        if( !json[i].cod ) break;
        if( !json[i].desc ) break;

        obj.add(new Option(json[i].desc,json[i].cod,json[i].selected));
    }
    return;
}
/**/
function removeChildNodes(node)
{
    if( node.removeChild )
    {
        for(var i = 0; i < node.childNodes.length; i++) { node.removeChild(node.childNodes[i]); }
    }
    else
    {
        for(var i=0; i < node.childNodes.length; i++) { node.childNodes[i].removeNode(true); }
    }
}
function waitIcon(bVisible)
{
    var obj = document.getElementById('waiticon');
    var s;

    if( bVisible ) s='inline'; else s='none';
    if( obj ) obj.style.display=s;
}

