// Helper functions
function FilterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function GetScrollTop() {
	scrollTop = FilterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);

	return scrollTop;
}

function GetViewportHeight() {
	var height;
	if (typeof window.innerWidth != 'undefined')
	{
		height = window.innerHeight;
	} else if(typeof document.documentElement != 'undefined' && 
		typeof document.documentElement.clientWidth != 'undefined' &&
		document.documentElement.clientWidth != 0)
	{ 
		height = document.documentElement.clientHeight;
	} else {
		height = document.getElementsByTagName('body')[0].clientHeight;
	}

	return height;
}

function FindHeight()
{
	height = document.documentElement.clientHeight;
	if (height == 0)
		height = 50 + document.getElementById('CenterAll').offsetHeight;

	return height;
} 

// Make the dimmer cover the whole scrollable area
function MaximizeDimmer(id)
{
	var new_height;
	var dimmer = document.getElementById(id);

	height = FindHeight();
	dimmer.style.height = height + "px";
}

function ShowDimmer(id)
{
	document.getElementById(id).style.display = 'block';
}

function HideDimmer(id)
{
	document.getElementById(id).style.display = 'none';
}

function ImagePopup(url, w, h)
{
	dimmer = document.getElementById('ImageDimmer');
	img_pop = document.getElementById('ImagePopup');

	img_pop.innerHTML = '<img src=\"'+url+'\" width=\"'+w+'\" height=\"'+h+'\" onclick=\"HideDimmer(\'ImageDimmer\'); img_pop.style.display = \'none\';\"/>';
	img_pop.style.display = 'block';
	ShowDimmer('ImageDimmer');
	MaximizeDimmer('ImageDimmer');

	scroll_top = GetScrollTop();
	body_height = document.body.scrollHeight;
	body_viewport_height = GetViewportHeight();

	// Reset padding from previous view
	img_pop.style.padding = 0;

	/*
	popup_height = img_pop.offsetHeight;

	if (popup_height > body_viewport_height)
		padding = (body_height / 2) - (popup_height / 2);
	else
		padding = scroll_top + (body_viewport_height / 2) - (popup_height / 2);
	*/

	img_pop.style.paddingTop = scroll_top + "px";
}

function AjaxToInnerHtml(id, url)
{
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState==4) {
			document.getElementById(id).innerHTML = xmlHttp.responseText;
		}
	}

	xmlHttp.open("GET", url+'?a=1', true);
	xmlHttp.send(null);
}

function ShowCart()
{
	document.getElementById('CartDimmer').style.display = 'block';
	document.getElementById('CartPopup').style.display = 'block';
	MaximizeDimmer('CartDimmer');
}

function HideCart()
{
	document.getElementById('CartPopup').style.display = "none";
	document.getElementById('CartDimmer').style.display = "none";
}

function HideMsg()
{
	document.getElementById('MsgPopup').style.display = "none";
	document.getElementById('MsgDimmer').style.display = "none";
}

function HideError()
{
	document.getElementById('ErrorPopup').style.display = "none";
	document.getElementById('ErrorDimmer').style.display = "none";
}
