var a_aja   = 0;
// var xmlHttp = null;
var xmlHttp = false;

// 
function GetXmlHttpObject() 
{
    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('ajax problem!');
	}
    else 
    {
		return xmlHttp;
	}
}

// 
//function ajaxRequest(type,url,params,ref,callback)
function ajaxRequest(type,url,params,post,ref)
{
	// alert('type:'+type+', url:'+url+', params:'+params+', post:'+post+', ref:'+ref);
    // var icallback = callback;
	var icallback = showResponse;
	
    if (!xmlHttp) 
    {
        xmlHttp = GetXmlHttpObject();
    }

    if ( xmlHttp==null ) 
    {
		icallback(ref,'ajax problem!');
		return;
	}

    if ( xmlHttp.readyState==4 || xmlHttp.readyState==0 ) 
    {
		xmlHttp.onreadystatechange = stateChanged;
		if (type=='GET')
        {
            xmlHttp.open("GET",url+'?'+params,true);
            xmlHttp.send(null);
        }
        else
        {
            xmlHttp.open("POST",url+'?'+params,true);
            xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlHttp.setRequestHeader("Content-length", post.length);
            xmlHttp.setRequestHeader("Connection", "close");
            xmlHttp.send(post);
        }
	} 
    else 
    {
        setTimeout("ajaxRequest('"+url+"','"+ref+"',"+callback+")",1000);
	}

    function stateChanged() 
    {
        if ( xmlHttp.readyState==4 ) 
        {
            if ( xmlHttp.status==200 ) 
            {
                icallback(ref,xmlHttp.responseText);
            } 
            else 
            {
                // alert(xmlHttp.status);
                icallback(ref,'~~~ '+xmlHttp.status);
            }
        }
    }
}

// 
function showResponse(ref,response) 
{
    document.getElementById(ref).innerHTML = response;
    // document.getElementById(ref).appendChild(document.createTextNode(xmlHttp.response))
    
    // evalScript(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]);
        }
        // eval(match[1]);
    }
}

function evalScript(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 dajax( type,url,params,post,ref ) {
    // alert('type:'+type+"\n"+'url:'+url+"\n"+'params:'+params+"\n"+'post:'+post+"\n"+'ref:'+ref+"\n");
    ajaxRequest(type,url,params,post,ref);
}

//
function dajaxShow( url,ref ) {
	ajaxRequest(url,ref);
}

//
function dajaxSave( url,ref ) {
	ajaxRequest(url,ref);
}
