// here we define global variable
var g_ajaxTargetElement = "";

function xmlhttpPost(strURL, strTargetElement) 
{
    var xmlHttpReq = false;
    var self = this;
    g_ajaxTargetElement = strTargetElement;
    
    // Mozilla/Safari
    if(window.XMLHttpRequest) 
    {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if(window.ActiveXObject) 
    {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    document.getElementById(strTargetElement).innerHTML = "<div class=\"ui_ajax_loader\"><img src=\"images/ajax_loader_big_circle.gif\" /></div>";

    self.xmlHttpReq.open('GET', strURL);
    //self.xmlHttpReq.open('POST', strURL, true);
    //self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() 
    {
        if(self.xmlHttpReq.readyState == 4) 
        {
            updatePageElement(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(strURL);
}

function getquerystring() 
{
    var form = document.forms['f1'];
    var word = form.word.value;
    qstr = 'w=' + escape(word);  // NOTE: no '?' before querystring
    return qstr;
}

function updatePageElement(str)
{
    document.getElementById(g_ajaxTargetElement).innerHTML = str;
}




function AjaxConnection(strURL, strTargetElement, showLoader) 
{
	this.setOptions = setOptions;
	this.getOptions = getOptions;
	this.connect = connect;
	this.targetURL = strURL;
	this.targetElement = strTargetElement;
	this.showLoader = showLoader;
	this.options = strURL;
	this.xmlobj = null;
} 

function setOptions(opt)
{
	for(i=0;i<opt.length;i++)  
	{
		this.options += "&"+opt[i];
	}
}

function getOptions()
{
	return this.options;
}

function connect()
{
	with(this)
	{
		xmlobj = init_object();
		
		if(!xmlobj || xmlobj == null)
		{
		    return;
		}

		if(showLoader)
		{
			document.getElementById(targetElement).innerHTML = "<div class=\"ui_ajax_loader\"><img src=\"images/ajax_loader_big_circle.gif\" /></div>";
		}
		  
	    //xmlobj.open("POST", targetURL, true);
	    //xmlobj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	    xmlobj.open("GET", targetURL);

	    xmlobj.onreadystatechange = function() 
	    {
		    if(!xmlobj || xmlobj == null)
		        return;
		        
		    if(xmlobj.readyState != 4)
			    return;
		    //eval(return_func + '(xmlobj.responseText)');
		    document.getElementById(targetElement).innerHTML = xmlobj.responseText;
		    delete xmlobj;
	    }
    
	    xmlobj.send(options);
	}
}

function init_object() 
{
	var x = null;
	try 
	{
		x = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) 
	{
		try
		{
			x = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			x = null;
		}
	}

	if(!x && typeof XMLHttpRequest != "undefined")
		x = new XMLHttpRequest();

	return x;
}

/*
Example usage:

connection = new AjaxConnection();
opts = new Array("firstname=Joe","lastname=User");
connection.setOptions(opts);
connection.connect("callBack");

function callBack(content) 
{
	alert(content);
}

*/