function showInputPwd()
{
	document.getElementById('inputPwd').style.display = '';

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

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

}
function doPaypal(info)
{
	if (info == "")
	{
		alert("Paypal payments not enabled");
		
		return false;
	}
	
	var paypalForm = document.getElementById("paypalForm");
	paypalForm.encrypted.value = info;
	paypalForm.submit();
	return false;
}

productAddIframe = null;

mx = 0;
my = 0;

function addProductToCart(event, productId)
{
	var body = document.getElementsByTagName("body");
	body = body.length >= 1 ? body[0] : null;
	
	mx = getScreenX(event);
	my = getScreenY(event);
	if (body)
	{
		if (!productAddIframe)
		{
			productAddIframe = document.createElement("iframe");
			productAddIframe.style.display = "none";
			body.appendChild(productAddIframe);
		}
		productAddIframe.contentWindow.location.replace("my_cart.php?addProduct=" + productId);
	}
	return false;
}

function onProductAdded(total)
{
	doc = window.document;
	var order_total = doc.getElementById("order_total");
	order_total.innerHTML = total;
	
	divPopup("popupProductAdded", mx, my);
	//alert("The product is added to your cart. Total cart amount is $ " + total);
}

function onPic(object, id)
{
	var fotoGroot = document.getElementById('fotoGroot');
	
	fotoGroot.width = imageSizes[id][0] < g_maxWidth ? imageSizes[id][0] : g_maxWidth;
	fotoGroot.src = object.src;
	
	return false;
}

function delayedWrite(string, objectId)
{
	if (objectId)
	{
		var obj = document.getElementById(objectId);
		obj.innerHTML = string;
	}
	else
	{
		document.write(string);
	}
}

// Div Popups

function getPixels(str)
{
	return str != "" ? parseInt(str.substr(0, str.length-2)) : 0;
}

function getObjectHeight(obj)
// return the height of given object according to the style sheet
{
	var height;
	height = getPixels(obj.style.height);
	
	height += getPixels(obj.style.paddingTop) + getPixels(obj.style.paddingBottom);
	height += getPixels(obj.style.marginTop) + getPixels(obj.style.marginBottom);
	height += getPixels(obj.style.borderTopWidth) + getPixels(obj.style.borderBottomWidth);

	return height;
}

function getObjectWidth(obj)
// return the height of given object according to the style sheet
{
	var width;
	width = getPixels(obj.style.width);
	
	width += getPixels(obj.style.paddingLeft) + getPixels(obj.style.paddingRight);
	width += getPixels(obj.style.marginLeft) + getPixels(obj.style.marginRight);
	width += getPixels(obj.style.borderLeftWidth) + getPixels(obj.style.borderRightWidth);
	
	return width;
}

function hideDivPopup(id)
{
	var obj = document.getElementById(id);
	
	obj.style.display = "none";
}

function divPopup(id, x, y)
{
	var obj = document.getElementById(id);
	
	// make sure div stays inside screen
	var height = getObjectHeight(obj);
	var width = getObjectWidth(obj);
	
	x -= width / 2;
	y -= height / 2;
	
	var availHeight = document.body.offsetHeight-10;
	if (y-document.body.scrollTop + height > availHeight)
		y = availHeight + document.body.scrollTop - height; 
		
	var availWidth = document.body.offsetWidth-10;
	if (x-document.body.scrollLeft + width > availWidth)
		x = availWidth + document.body.scrollLeft - width;

	obj.style.position = "absolute";
	obj.style.top = y + "px";
	obj.style.left = x + "px";
	
	obj.style.display = "";
}

function getScreenX(event)
{
	var x = event.layerX ? event.layerX : event.screenX + document.body.scrollLeft;
	
	if (window.innerHeight) {
		//x -=window.screenX+(window.outerWidth-window.innerWidth);
	}
	else {
		x -= window.screenLeft;
	}
	return x;
}

function getScreenY(event)
{
	var y = event.layerY ? event.layerY : event.clientY + document.body.scrollTop;
	
	if (window.innerHeight) {
		//y -= window.screenY+(window.outerHeight-window.innerHeight);
	}
	else {
		//y -= window.screenTop;
	}

	return y;
}

var previousPopup = null;

function photoPopup(src, width, height)
{
	var screenWidth = window.screen.width;
	var screenHeight = window.screen.height - 100;
	var ratio = width / height;
	
	if (width > screenWidth)
	{
		width = screenWidth;
		height = Math.floor(width / ratio);
	}
	
	if (height > screenHeight)
	{
		height = screenHeight;
		width = Math.floor(height * ratio);	
	}
	var left = (screenWidth - width)/2;
	var top = (screenHeight - height)/2;
	if (left < 0)
		left = 0;
	if (top < 0)
		top = 0;
		
	if (previousPopup)
		previousPopup.close();
	var w = window.open('index.php?blank', 'photo', 'left=' + left + ', top=' + top + ', width=' + width + ',height=' + height);

	previousPopup = w;	
	w.document.write("<a href='javascript:window.close();' title='Click to close'><img border='0' width='" + width + "' height='" + height + "' src='" + src + "'></a>");

	list = w.document.getElementsByTagName('body');
	var body = list.length > 0 ? list[0] : null;
	
	if (body)
	{
		body.style.marginTop="0px";
		body.style.marginLeft="0px";
	}
}

function submitButtonsOnly(formId)
{
	var f = document.getElementById(formId);
	
	for(i in f.elements)
	{
		var e = f.elements[i];
		var type = typeof e.type == "undefined" ? false : e.type.toLowerCase();
		if (type == "text" || type == "textarea")
		{
			e.onkeydown = function(_event)
			{
				var event = _event ? _event : window.event;
				var code = event.which ? event.which : event.keyCode;
				
				return code != 13;
			}
		}
	}
}