/*=================================================================
*	log_ajax.js
*   -----------
*   Description : JavaScript Ajax functions
*   Copyright   : (c) 2009 Arthur Aminov, EAI Developer, Smile 012
*   Last build  : 01.07.2009 @ 13:00
=================================================================*/

	function showWorkingImg(isWork)
	{
		objImg = returnOBJ("country_flag");

if (typeof tmpImgSrc == "undefined") tmpImgSrc = objImg.src;
if (isWork == true) {
objImg.src = "images/working.gif";
} else {
objImg.src = tmpImgSrc;
}
/*		if (typeof tmpImgSrc == "undefined") {
			tmpImgSrc = objImg.src;
			if (p != 1) objImg.src = "images/working.gif";
			else {
				objImg.src = tmpImgSrc;
				delete tmpImgSrc;
			}
		} else {
			objImg.src = tmpImgSrc;
			delete tmpImgSrc;
		}*/
	}
//=================================================================
// GLOBAL Variable
//=================================================================
	var xmlHttp = false;
    var ajaxInProgress = false;
	var timerH = 0;	// timer handle
//=================================================================
// get xml http object
//=================================================================
	function GetXmlHttpObject()
	{
		var xmlHttp = null;
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) {
			// Internet Explorer
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			// Firefox, Opera 8.0+, Safari
			catch (e) {
				xmlHttp = new XMLHttpRequest();
			}
		}
		return xmlHttp;
	}

	xmlHttp = GetXmlHttpObject();
//=================================================================
// Main ajax function
//=================================================================
	function callAjaxHTTP(filephp, args, id, func, method)
	{
    	//if (ajaxInProgress) return 0;

		if (xmlHttp == null) {
			alert ("Browser does not support HTTP Request");
			return 0;
		}

		/*objImg = returnOBJ("country_flag");
		tmpImgSrc = objImg.src;
		objImg.src = "images/working.gif";*/
		showWorkingImg(true);

	    //divDisplay("divWorking", "block");
		//bodyCursor("wait");
	    tooltip(false);

		var url = "";
		method = isDefined(method) ? method : "GET";

		if (method == "GET") {
			url = filephp + "?" + args + "&sid=" + Math.random();

			// ajax action
			xmlHttp.open(method, url, true);

		} else {	// POST
			url = args + "&ses=" + Math.random();

			// ajax action
			xmlHttp.abort();
			xmlHttp.open(method, filephp, true);

			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", url.length);
			xmlHttp.setRequestHeader("Connection", "close");
		}

		// callback function
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				ajaxInProgress = false;
				if (method == "GET") {
					func(id, xmlHttp.responseText);
				} else {
					func(id, xmlHttp.responseText);
//					document.write(xmlHttp.responseText);
				}
				//divDisplay("divWorking", "none");
				//objImg.src = tmpImgSrc;
				showWorkingImg();
				//bodyCursor();
			}
		}
		xmlHttp.send(method == "GET" ? null : url);

       	ajaxInProgress = true;

		return 1;
	}
//=================================================================
// Display Statistics info on Div and change image from + to -
//=================================================================
	function displayStatistics(divID, imgID, phpfile, args)
	{
	    var objDiv = returnOBJ(divID);
	    var objImg = returnOBJ(imgID);

	    if(objDiv.style.display == "block") {
		    objDiv.style.display = "none";
			objDiv.innerHTML = "";
			objDiv.style.height = "0px";
		    objImg.src = "images/plus.gif";
	    } else {
			if (ajaxInProgress) return 0;
			callAjaxHTTP(phpfile, args, divID, divDynamicHeight);
		    objImg.src = "images/minus.gif";
		}
	}

	// timer change div height dynamically
	function timerDivDynamicHeight(obj, pxCnt, height, iHTML)
	{
		if (typeof cnt == "undefined") cnt = 0;

		cnt += pxCnt;
		if (cnt <= height)
			obj.style.height = cnt;
		else {
			clearTimeout(timerH);
			obj.innerHTML = iHTML;
			cnt = 0;
		}
	}
	// change div height dynamically
	function divDynamicHeight(id, responseTxt)
	{
		if (!trim(responseTxt)) return;

		var pxCnt = 20;
		var interval = 1;
		var obj = returnOBJ(id);

	    obj.style.display = "block";
		obj.innerHTML = responseTxt;
		height = obj.offsetHeight;
		iHTML = obj.innerHTML;
		obj.innerHTML = "";

		timerH = setInterval(function () {timerDivDynamicHeight(obj, pxCnt, height, iHTML);}, interval);
	}
//=================================================================
// [EOF]
//=================================================================