function El(id)
{
	return document.getElementById(id);
}

// Left-trim a string and return the result
function LTrimStr(sString)
{
	if (sString == '') return '';

	i = 0;
	while (i < sString.length && sString.charAt(i) == ' ') i++;

	sString = sString.substring(i, sString.length);
	return sString;
}

function IsNumber(st)
{
	return (st.match(/[ ]*\-?[0-9]+[.]?[0-9]*[ ]*/) == st)
}

// Get an instance of the XMLHttpRequest object
function GetXHRObject()
{
	var xhr = null; // The XMLHTTPRequest object

	if (window.XMLHttpRequest) // Gecko and Opera
		xhr = new XMLHttpRequest();
	else if (window.ActiveXObject) // Internet Explorer
		xhr = new ActiveXObject("Microsoft.XMLHTTP");

	return xhr;
}

// Send a GET request and load the received content into the given div
function LoadAsyncToDiv(url, divId, actn)
{
	var xhr = GetXHRObject();

	if (xhr == null)
	{
		alert("Sorry, your browser doesn't support AJAX");
		return;
	}
	xhr.open("GET", url, true);

	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4) // data received
		{
			// insert the received content into the div
			El(divId).innerHTML = xhr.responseText;
			if (actn != null && LTrimStr(xhr.responseText) != "")
				eval(actn); // execute additional code
		}
	}
	// Send the request
	xhr.send(null);
}

// Send a POST request and load the received content into the given div
// POST paramerters are urlencoded and stored in encodedData
function LoadAsyncToDivPost(url, encodedData, divId, actn)
{
	var xhr = GetXHRObject();

	if (xhr == null)
	{
		alert("Sorry, your browser doesn't support AJAX");
		return;
	}
	xhr.open("POST", url, true);

	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4) // data received
		{
			// insert the received content into the div
			El(divId).innerHTML = xhr.responseText;
			if (actn != null && LTrimStr(xhr.responseText) != "")
				eval(actn); // execute additional code
		}
	}

	// Send the request
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.send(encodedData);
}

function LoadAdPage(adid)
{
	var url = '/ads/getad.php?adid=' + encodeURIComponent(adid);
	LoadAsyncToDiv(url, 'adBox', "FixLocation('adBox');ShowElem('adBox');BlockBackground();");
}

function PostBackShowAgainCheckBox(adid)
{
	var url = 'ads/recurrent.php?adid=' + encodeURIComponent(adid) +
			'&show_again=' + (document.showAdAgainForm.showAdAgainCheckbox.checked ? '1' : '0');
	UnblockBackground();
	LoadAsyncToDiv(url, 'adBox');
}

// block access to elements on the back
function BlockBackground()
{
	var tmpDiv = El("fullPageBlankDiv");
	
	if (tmpDiv)
	{
		tmpDiv.style.opacity = 0.4;
		tmpDiv.style.filter = "alpha(opacity=40)";
		tmpDiv.style.top = "0px";
		tmpDiv.style.left = "0px";

		var h = 0;
		// set the position
		if (window.innerHeight)
			h = window.innerHeight;
		else if (document.documentElement.clientHeight)
			h = document.documentElement.clientHeight;

		if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
		{
			tmpDiv.style.width = document.body.scrollWidth + "px";
			tmpDiv.style.height = (Math.max(h, document.body.scrollHeight) - 1) + "px";
		}
		else
		{
			tmpDiv.style.width = document.body.offsetWidth + "px";
			tmpDiv.style.height = (Math.max(h, document.body.offsetHeight) - 1) + "px";
		}
		tmpDiv.style.display = "block";
		setTimeout('CheckBlockBackground();', 100);
	}
}

function CheckBlockBackground()
{
	var tmpDiv = El("fullPageBlankDiv");
	
	if (tmpDiv && tmpDiv.style.display == "block")
	{
		var h = 0;
		// set the position
		if (window.innerHeight)
			h = window.innerHeight;
		else if (document.documentElement.clientHeight)
			h = document.documentElement.clientHeight;

		if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
		{
			tmpDiv.style.width = document.body.scrollWidth + 16 + "px";
			tmpDiv.style.height = Math.max(h, document.body.scrollHeight) + "px";
		}
		else
		{
			tmpDiv.style.width = document.body.offsetWidth + 16 + "px";
			tmpDiv.style.height = Math.max(h, document.body.offsetHeight) + "px";
		}
		setTimeout('CheckBlockBackground();', 100);	
	}
}

function UnblockBackground()
{
	var tmpDiv = El("fullPageBlankDiv");
	if (tmpDiv)
	{
		tmpDiv.style.display = "none";
	}
}

function FixLocation(divId)
{
	var tmpDiv = El(divId);
	if (tmpDiv == null) return;

	var w = tmpDiv.clientWidth; 
	var h = tmpDiv.clientHeight;
	
	var topDelta;
	
	// Get the scrolly delta
	if (self.pageYOffset)
		topDelta = self.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop)
		topDelta = self.document.documentElement.scrollTop;
	else if (document.body)
		topDelta = document.body.scrollTop;
	else
		topDelta = Math.floor(h / 2);

	topDelta -= Math.floor(h / 2);

	// set the position
	if (window.innerHeight)
		tmpDiv.style.top = (window.innerHeight / 2 + topDelta) + "px";
	else if (document.documentElement.clientHeight)
		tmpDiv.style.top = (document.documentElement.clientHeight / 2  + topDelta) + "px";
	else
		tmpDiv.style.top = (300 + topDelta) + "px";

	if (window.innerWidth)
		tmpDiv.style.left = (window.innerWidth / 2 - Math.floor(w / 2)) + "px";
	else if (document.documentElement.clientWidth)
		tmpDiv.style.left = (document.documentElement.clientWidth / 2 - Math.floor(w / 2)) + "px";
	
	setTimeout("FixLocation('" + divId + "');", 20);
}

function HideElem(id)
{
	El(id).style.display = "none";
}

function ShowElem(id)
{
	El(id).style.display = "block";
}
