//
// common.js
//

// Author: Colin Jaggs
// Date: 6th October 2004
// Description: Common JS functions use throughout the site

// common variables
var popUpWin = false;

// close any existing popup windows
function closePopups()
{
	if (popUpWin) popUpWin.close();
}

// only allow numbers to be entered in text boxes (usage: onKeyPress="return numberOnly()")
function numbersOnly(e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if ((keycode == 13) || ((keycode >= 48) && (keycode <= 57))) { return true; }
	else return false;
}

function popUpPage(url)
{
	if (popUpWin.open) popUpWin.close();
	
	popUpWin = window.open(url, "popupwin", "width=650,height=500,scrollbars=1,toolbar=1")
}

// generic popup image function - opens a set width and height and displays the image as passed in through imagePath at the full width and height specified
function popUpImage(imagePath, width, height, windowTitle, scroll)
{
	windowTitle = windowTitle || "Walton Garden Buildings";
	closePopups();
	popUpWin = window.open("", "popupwin", "width=" + width + ",height=" + height + ",left=" +( (screen.width / 2) - (width / 2)) + ",top=" + ((screen.height / 2) - (height / 2)) + ",scrollbars=" + ((scroll) ? 1 : 0));
	popUpWin.document.open();
	popUpWin.document.write("<html><head><title>" + windowTitle + "</title></head><body bottommargin=0 topmargin=0 leftmargin=0 rightmargin=0 marginwidth=0 marginheight=0><table border=0 cellspacing=0 cellpadding=0 width=100% height=100%><tr><td align=center valign=middle><img src='" + imagePath + "' alt='" + windowTitle + "'></td></tr></table></body></html>");
	popUpWin.document.close();
}

function printerFriendly(url)
{
	if (popUpWin.open) popUpWin.close();
	
	popUpWin = window.open(url, "popupwin", "width=650,height=500,scrollbars=1,toolbar=1")
}

function asCurrency(value)		// format a number as currency
{
	// check the number is a valid number or return empty string
	value = parseFloat(value);
	if (isNaN(value)) return 0;

	// convert to string and check for a decimal place - if there isn't one then append .00 to the value
	if (value.toString().indexOf(".") < 1) value = value.toString() + ".00";

	// split in to xxx and xx, and format the xx part
	var poundsPence = value.toString().split(".");
	while (poundsPence[1].length < 2) poundsPence[1] += "0";

	return poundsPence[0] + "." + poundsPence[1];
}

function quickSearchHdrKeyPress(e)
{
	if (getKeyCode(e) == 13) { quickSearch(document.forms[0].txtSearch); return false; }
	return true;
}

function quickSearchFtrKeyPress(e)
{
	if (getKeyCode(e) == 13) { quickSearch(document.forms[0].txtSearchFtr); return false; }
	return true;
}

function newsletterKeyPress(e)
{
	if (getKeyCode(e) == 13) { joinNewsletter(document.forms[0].txtNewsletter); return false; }
	return true;
}

function getKeyCode(e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	return keycode;
}

function quickSearch(source)
{
	var url = HTTPHost + "/product-list";
	if ((source.value != "") && (source.value != "Enter Keyword")) url += "&Text=" + source.value;
	location.href = url;
}

function joinNewsletter(source)
{
	if ((source.value == "") || (source.value == 'Enter Email'))
	{
		alert("Please enter your email address to subscribe to our newsletter");
		return false;
	}

	location.href = HTTPHost + '/mailing-list&Email=' + source.value;
}

// cross browser function to find an element by id
function objectById(id)
{ 
	if (document.getElementById) return document.getElementById(id); 
	else if (document.all) return document.all[id]; 
	else if (document.layers) return document.layers[id]; 
} 
