function showEmail(name)
{
	document.write("<a href=\"");
	document.write("mailto:" + name + " @ greathill.com (non-spammers please remove spaces)");
	document.write("\"");
	document.write(">");
	document.write(name + " @ greathill.com");
	document.write("</a>");
}

function validateElement(theForm, element, name)
{
	if (element.value == "")
	{
		alert("Please enter a value for the '" + name + "' field.");
		element.focus();
		return false;

	} else if (name == "email")
	{
		if (element.value.indexOf('@') < 1)
		{
			alert("Please enter a valid email address.");
			element.focus();
			return false;
		}
	}
	
	return true;
}

function validateStore(theForm)
{
	if (!validateElement(theForm, theForm.firstname,      "firstname"     )) return false;
	if (!validateElement(theForm, theForm.lastname,       "lastname"      )) return false;
	if (!validateElement(theForm, theForm.email,          "email"         )) return false;
	if (!validateElement(theForm, theForm.phone,          "phone"         )) return false;
	if (!validateElement(theForm, theForm.street1,        "street1"       )) return false;
	if (!validateElement(theForm, theForm.city,           "city"          )) return false;
	if (!validateElement(theForm, theForm.state_province, "state_province")) return false;
	if (!validateElement(theForm, theForm.postcode,       "postcode"      )) return false;
	if (!validateElement(theForm, theForm.country,        "country"       )) return false;
	return (true);
}

function validateForm(theForm, isSupport)
{
	if (             !validateElement(theForm, theForm.name,     "name"    )) return false;
	if (             !validateElement(theForm, theForm.email,    "email"   )) return false;
	if (isSupport && !validateElement(theForm, theForm.subject,  "subject" )) return false;
	if (isSupport && !validateElement(theForm, theForm.comments, "comments")) return false;
	return (true);
}

var paramStr = location.href;
if (paramStr.indexOf("?") > 0 && paramStr.indexOf("=") > 0)
	paramStr += "&";

function getParam(name, def)
{
	var ret = def;
	
	var str = name + "=";

	if (paramStr.indexOf(str) > 0)
	{
		ret = paramStr.substr(paramStr.indexOf(str) + (str.length));
		if (ret.indexOf("&" > 0))
			ret = ret.substr(0,ret.indexOf("&"));
		var loc = ret.indexOf("%20");
		while (loc > 0)
		{
			ret = ret.substr(0, loc) + " " + ret.substr(loc+3);
			loc = ret.indexOf("%20");
		}
	} else
	{
		ret = def;
	}
	return ret;
}

var username = getParam("username", "");
var account  = getParam("account",  "");
var email    = getParam("email",    "");
var backup   = getParam("backup",   "0");
var images   = getParam("images",   "0");
var url      = getParam("url",      "");
var user     = getParam("user",     "");
var password = getParam("password", "");
var nLics    = getParam("nLics",    "1");
var now      = getParam("now",      "");
var contact  = getParam("contact",  "disabled");
var todo     = getParam("todo",     "disabled");
var page     = getParam("page",     "");

//--------------------------------
//-- Return the actual value of the cookie
function getCookieValue(offset)
{
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;

	return unescape(document.cookie.substring(offset, endstr));
}

//--------------------------------
//-- Find and return the value of the given cookie or null if not found
function GetCookie(name)
{
	var arg  = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;

	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) 
		{
			return getCookieValue(j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
			break;
	}

	return null;
}

//--------------------------------
//-- Set the value of the 'name' cookie
function SetCookie(name, value)
{
	var argv    = SetCookie.arguments;
	var argc    = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path    = (argc > 3) ? argv[3] : null;
	var domain  = (argc > 4) ? argv[4] : null;
	var secure  = (argc > 5) ? argv[5] : false;

	document.cookie = name + "=" + escape (value) +
										((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
										((path == null) ? "" : ("; path=" + path)) +
										((domain == null) ? "" : ("; domain=" + domain)) +
										((secure == true) ? "; secure" : "");
}

//--------------------------------
//-- Write a cookie including expiration date
function WriteCookie(name, value, nMonths)
{
	// Make it five years from now
	var expires = new Date();
	var mon = expires.getMonth();
	mon += nMonths;
	if (mon > 12)
	{
		expires.setMonth(mon-12);
		expires.setFullYear(expires.getFullYear() + 1);
	} else
	{
		expires.setMonth(mon);
	}
	SetCookie(name, value, expires, "/");
}

//--------------------------------
//-- Remove a cookie
function ExpireCookie(name)
{
	var expdate = new Date();
	expdate.setFullYear(expdate.getFullYear() - 1);
	SetCookie(name, "", expdate, "/");
}

function setFormFocus(focusName)
{
//	var item = document.getElementByName(focusName); 
//	if (item)
//		item.focus();
	for (i=0;i<document.forms.length;i++)
	{
		for (j=0;j<document.forms[i].elements.length;j++)
		{
			if (document.forms[i].elements[j].name == focusName)
			{
				if (document.forms[i].elements[j].type != "hidden")
				{
					document.forms[i].elements[j].focus();
				}
				return;
			}
		}
	}
}

function sortChange(urlIn, sel)
{
	var sort = sel.options[sel.selectedIndex].value;
	url = urlIn;
	url += ('&sort=' + sort);
	document.location.href = url;
}

function reverseChange(urlIn, checked)
{
	url = urlIn;
	url += ('&reverse=' + (checked?"1":"0"));
	document.location.href = url;
}

function accountChange(urlIn, sel)
{
	var account = sel.options[sel.selectedIndex].value;
	url = urlIn;
	url += ('&account=' + account);
	document.location.href = url;
}

function emailChange(urlIn, sel)
{
	var email = sel.options[sel.selectedIndex].value;
	url = urlIn;
	url += ('&email=' + email);
	document.location.href = url;
}

function messageChange(urlIn, sel)
{
	var msg = sel.options[sel.selectedIndex].value;
	url = urlIn;
	url += ('&msg=' + msg);
	document.location.href = url;
}

function MM_findObj(n, d) //v4.01
{
	var p,i,x;
	if (!d)
		d=document;
	if ((p=n.indexOf("?"))>0&&parent.frames.length)
	{
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}

	if (!(x=d[n])&&d.all)
		x=d.all[n];
		
	for (i=0;!x&&i<d.forms.length;i++)
		x=d.forms[i][n];

	for (i=0;!x&&d.layers&&i<d.layers.length;i++)
		x=MM_findObj(n,d.layers[i].document);

	if (!x && d.getElementById)
		x=d.getElementById(n);

	return x;
}

function MM_swapImgRestore() //v3.0
{
	var i,x,a=document.MM_sr;
	for (i=0; a && i<a.length && (x=a[i]) && x.oSrc; i++)
		x.src=x.oSrc;
}

function MM_swapImage() //v3.0
{
	var i,j=0,x,a=MM_swapImage.arguments;
	document.MM_sr=new Array;

	for (i=0;i<(a.length-2);i+=3)
	{
		if ((x=MM_findObj(a[i]))!=null)
		{
			document.MM_sr[j++]=x;
			if(!x.oSrc)
				x.oSrc=x.src;
			x.src=a[i+2];
		}
	}
}

function XMwriteCookie(n,i,t)
{
	var ep="";
	if (t)
	{
		d = new Date();
		d.setTime(d.getTime()+(t*86400000));
		ep = "; expires="+d.toGMTString();
	}
	document.cookie = n+"="+i+ep+"; path=/";
}

function helpCode(msg)
{
	document.write("[<a href=# ");
	document.write("title=\"" + msg + "\"");
	document.write("onClick=\"javascript:alert('" + msg + "');\"");
	document.write(">?</a>]");
}
