function CreateXMLHttp()
{
	var	i;
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			xmlhttp = false;
		}
	}
	if ( !xmlhttp && typeof XMLHttpRequest != 'undefined' )
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch(e)
		{
			xmlhttp = false;
		}
	}
	return( xmlhttp );
}

// --------------------------------------------------------------------------------------------------------------

function $(elmt_id)
{
	return( document.getElementById(elmt_id) );
}

function setjsdbg(txt)
{
	$("jsdbg").innerHTML = txt;
}

function addjsdbg(txt)
{
	$("jsdbg").innerHTML += txt+"<br />";
}

// --------------------------------------------------------------------------------------------------------------

function getPageScroll()
{
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}

function getPageSize()
{
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY)
	{
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else
		if (document.body.scrollHeight > document.body.offsetHeight)
		{ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
			}
			else
			{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

// --------------------------------------------------------------------------------------------------------------

function EndOfAjaxMsg()
{
	if ( $("ajaxmsg") )
	{
		$("ajaxmsg").style.display = "none";
	}
}

function AjaxCall2(url,postdata)
{
	postdata = unescape(postdata);
	var requestTimer = 0;
	var xmlhttp = CreateXMLHttp();
	if ( postdata )
	{
		xmlhttp.open("POST",url,false);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	}
	else
	{
		xmlhttp.open("GET",url,false);
	}
	requestTimer = setTimeout(function() { alert("Error : server timeout on "+url+"\n"+postdata);setTimeout("EndOfAjaxMsg()",5); }, 1000*10);
	xmlhttp.onreadystatechange =
	function (aEvt)
	{
		if (xmlhttp.readyState == 4)
		{
			if ( requestTimer )
			{
				clearTimeout(requestTimer);
			}
			if(xmlhttp.status == 200)
			{
//				alert( xmlhttp.responseText);
				if ( $("ajaxmsg") )
				{
					$("ajaxmsg").innerHTML = "Ok !";
				}
				setTimeout("EndOfAjaxMsg()",1500);
			}
			else
			{
				alert("Error : invalid response from server ("+xmlhttp.status+")");
				setTimeout("EndOfAjaxMsg()",5);
			}
		}
	}
	if ( postdata )
	{
		xmlhttp.send(postdata);
	}
	else
	{
		xmlhttp.send(null);
	}
	if ( requestTimer )
	{
		clearTimeout(requestTimer);
		if ( $("ajaxmsg") )
		{
			$("ajaxmsg").innerHTML = "Ok !";
		}
		setTimeout("EndOfAjaxMsg()",1500);
	}
}

function DoWaitOn()
{
	if ( $("ajaxmsg") )
	{
		$("ajaxmsg").innerHTML = "...";
		$("ajaxmsg").style.display = "block";
	}
}

function AjaxCall(url,postdata)
{
	if ( $("ajaxmsg") )
	{
		var pageSize = getPageSize();
		var pageScroll = getPageScroll();
		var x = (pageSize[0] - 400) / 2;
		var y = pageScroll[1]+200;
		$("ajaxmsg").style.left = x+"px";
		$("ajaxmsg").style.top = y+"px";
		$("ajaxmsg").innerHTML = "...";
		$("ajaxmsg").style.display = "block";
	}
	setTimeout("AjaxCall2('"+url+"','"+escape(postdata)+"')",200);
}

// --------------------------------------------------------------------------------------------------------------

function PopupMsg(popup_title,content)
{
	var pageSize = getPageSize();
	var pageScroll = getPageScroll();
	var x = (pageSize[0] - 600) / 2;
	var y = pageScroll[1]+200;
	$("popupmsg").style.left = x+"px";
	$("popupmsg").style.top = y+"px";
	$("popup_title").innerHTML = popup_title;
	$("popup_content").innerHTML = content;
	$("popupmsg").style.display = "block";

}

// --------------------------------------------------------------------------------------------------------------

function JumpToURL(dst)
{
	document.location = dst;
}

// --------------------------------------------------------------------------------------------------------------

function CreateCRM()
{
	document.body.appendChild($("crm"));
	$("crm_iframe").src = "/crm.php?action=crm";
	var pageSize = getPageSize();
	var pageScroll = getPageScroll();
	var x = (pageSize[0] - 600) / 2;
	var y = pageScroll[1]+200;
	$("crm").style.left = x+"px";
	$("crm").style.top = y+"px";
	$("crm").style.display = "block";
}

function DestroyCRM()
{
	$("crm_iframe").src = "";
	$("crm").style.display = "none";
}



