/*
* etnoFramework - PHP framework  Copyright (C) 2009 Radek Krawiec
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
* Redistributions of files must retain the above copyright notice.
*/

function sendAjaxRequest (url,queryString,onSuccessFunction,onFailureFunction,onWaitFunction,divId) 
{
    var xmlHttpReq = false;
    var self = this;
	
    // Mozilla/Safari
    if (window.XMLHttpRequest) 
	{
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) 
	{
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    /*
     * TODO - good info here: https://developer.mozilla.org/pl/XMLHttpRequest
     */
    
    self.xmlHttpReq.open('POST', url, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() 
	{
        if (self.xmlHttpReq.readyState == 4)
        {
        	if (divId != null && divId != '')
        	{
        		var params = new Array(2);
        		params[1] = "'"+divId+"'";
        	}
        	else
        	{
        		var params = new Array(1);
        	}
        	params[0] = 'self.xmlHttpReq.responseText';
            callFunctionByName(onSuccessFunction, params);
        }
        else
        {
        	if (self.xmlHttpReq.readyState == 3)
        	{
        		if (onWaitFunction != '')
        		{
        			callFunctionByName(onWaitFunction, null);
        		}
        	}
        }
    }
    self.xmlHttpReq.send(queryString);
}

function callFunctionByName (functionName, params)
{
	 var call = functionName + '(';
	 if (params != null)
	 {
		for (i=0; i<params.length; i++)
		{
			call += params[i] + ',';
		}
		// remove the last comma
		call = call.substring(0,call.length-1);
	 }
	 call += ');';
     return eval(call);
}

function reloadDiv (response, divId)
{
	var div = document.getElementById(divId);
	if (div)
	{
		div.innerHTML = response;
	}
}
