var a_aja   = 0;
// var xmlHttp = null;
var xmlHttp = false;

// 
function daGetXmlHttpObject () 
{
    xmlHttp = false;
    try 
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } 
    catch (e) 
    {
        // Internet Explorer
        var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                        "MSXML2.XMLHTTP.5.0",
                                        "MSXML2.XMLHTTP.4.0",
                                        "MSXML2.XMLHTTP.3.0",
                                        "MSXML2.XMLHTTP",
                                        "Microsoft.XMLHTTP");
        for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
        {
            try 
            {
                xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
            } 
            catch (e) 
            {}
        }
    }
    if (!xmlHttp) 
    {
        alert('dajax problem!');
    }
    else 
    {
        return xmlHttp;
    }
}

// 
function dajaxRequest (type,ref,url,params,post,pare)
{
    xmlHttp = false;

    // alert('type:'+type+',ref:'+ref+',url:'+url+',params:'+params+',post:'+post+',pare:'+pare);
    var icallback = dashowResponse;
    
    if (!xmlHttp) 
    {
        xmlHttp = daGetXmlHttpObject();
    }

    if ( xmlHttp==null ) 
    {
        icallback(ref,'dajax problem!',pare);
        return;
    }

    if ( xmlHttp.readyState==4 || xmlHttp.readyState==0 ) 
    {
        xmlHttp.onreadystatechange = stateChanged;
        if (type=='GET')
        {
            xmlHttp.open('GET',url+'?'+params,true);
            //xmlHttp.setRequestHeader('If-Modified-Since','0');
            xmlHttp.send(null);
        }
        else
        {
            xmlHttp.open('POST',url+'?'+params,true);
            //xmlHttp.open('POST',url,true);
            xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            //--xmlHttp.setRequestHeader('Content-type', 'multipart/form-data');
            xmlHttp.setRequestHeader('Content-length', post.length);
            //xmlHttp.setRequestHeader('Content-length', binaryLength_UTF8(post));
            xmlHttp.setRequestHeader('Connection', 'close');
            xmlHttp.send(post);
            //alert(post);
        }
    } 
    else 
    {
        //setTimeout("dajaxRequest('"+url+"','"+ref+"',"+callback+")",1000);
    }

    function stateChanged() 
    {
        //alert(xmlHttp.readyState);
        if ( xmlHttp.readyState==4 ) 
        {
            if ( xmlHttp.status==200 ) 
            {
                icallback(ref,xmlHttp.responseText,pare);
            } 
            else 
            {
                // alert(xmlHttp.status);
                icallback(ref,'&lsaquo; '+xmlHttp.status+' &rsaquo;',pare);
            }
        }
    }
}

// 
function dashowResponse (ref,response,pare) 
{
    if (pare=='parent') { parent.document.getElementById(ref).innerHTML = response; }
    else                { document.getElementById(ref).innerHTML = response; }

    // document.getElementById(ref).appendChild(document.createTextNode(xmlHttp.response))
    // daevalScript(response);
   
    if (response!='')
    {
        var re = /<script.*?>([\s\S]*?)<\//igm;
        var match;
        while (match=re.exec(response))
        {
            if (window.execScript) { window.execScript(match[1]); }
            else                   { eval(match[1]); }
        }
    }
}

function daevalScript (scripts)
{   
    try
    {   
        if(scripts != '')   
        {   
            var script = "";
            scripts = scripts.replace(
                                        /<script[^>]*>([\s\S]*?)<\/script>/gi, 
                                        function(){ if (scripts !== null) script += arguments[1] + "\n"; return ''; }
                                     );
            if(script) (window.execScript) ? window.execScript(script) : window.setTimeout(script, 0);
        }
        return false;
    }
    catch(e)
    {   
        alert(e)
    }
}

//function ajaxRefresh (ref,url,obj,secs) 
function dajaxRefresh (ref,url,params,obj,refreshSeconds) 
{
    //alert(window[varInterval]+' '+varInterval);
    if (obj.value!='undefined' ) 
    {
        clearInterval(obj.value);
    }

    dajax('GET',ref,url,params,'');
    
    if ( trim(url)!='' && trim(ref)!='' && refreshSeconds>0 ) // && secs>0
    {
        obj.value = setInterval( "dajax('GET','"+ref+"','"+url+"','"+params+"','')",refreshSeconds*1000 );
    }
    //alert('+');
}

//
function dajax (type,ref,url,params,post,pare)  {
    // alert('type:'+type+"\n"+'url:'+url+"\n"+'params:'+params+"\n"+'post:'+post+"\n"+'ref:'+ref+"\n");
    dajaxRequest(type,ref,url,params,post,pare);
}

//
function dajaxShow (url,ref) {
    dajaxRequest(url,ref);
}

//
function dajaxSave (url,ref) {
    dajaxRequest(url,ref);
}

