
// Open link in a new tab/window
function a_onclick_ext ()
{
	return !window.open (this.href);
}

// Open the print dialog - pointless, really, but some people ask for it :-(
function a_onclick_prn ()
{
	window.print ();
	return false;
}

// Check to see if any forms on the page have had their values modified, and alert the user before proceeding
function a_onclick_ucl ()
{
	var msg	= "Changes you have made will not be saved."				+ "\r\n\r\n"
		+ "Click 'OK' to discard your changes and move to the next screen."	+ "\r\n\r\n"
		+ "Click 'Cancel' to return to the previous page so that you can save your changes.";

	for (var x = document.forms.length; x--; ) for (var y = document.forms[x].elements.length; y--; )
	{
		var e	= document.forms[x].elements[y];
		switch (e.type)
		{
			case "hidden":
			case "submit":		/*	Do nothing	*/						break;
			case "radio":
			case "checkbox":	if (e.checked	!= e.defaultChecked)	return confirm (msg);		break;
			default:		if (e.value	!= e.defaultValue)	return confirm (msg);		break;
		}
	}

	return true;
}

// Open a general popup window.
function a_onclick_pop ()
{
	alert (this.title);
	return false;
}

// Set the font size of the elements listed in ids (can be an array or a string)
// Uses cookies for persistance
function a_onclick_fsize (ids, size, unit)
{
	if (typeof (ids) == "string")
		ids = [ids];

	for (c = 0, x = ids.length; x--; )
	{
		CookieJar.set ("fontEl_" + c++, ids[x], 90);

		if (el = document.getElementById (ids[x]))
			el.style.fontSize = size + unit;
	}

	CookieJar.set ("fontSize", size, 90);
	CookieJar.set ("fontUnit", unit, 90);
	CookieJar.set ("fontNum",  c,    90);

	return false;
}

// Image mouseover swaps
function a_onmouseover_img ()
{
	if (!this.targetElement) this.targetElement		= document.getElementById (this.rev);
	if ( this.targetElement) this.targetElement.src	= this.href;//.replace (/\.([^.]+)$/, '.L.$1');
	return false;
}

// Check the REL attribute of <a> elements and assign an onclick (etc) event
function init_as ()
{
	var els = document.getElementsByTagName ("a");

	if (els) for (var x = els.length; x--; )
	{
		var r = els[x].getAttribute ("rel");

		if (r) switch (r.toLowerCase ())
		{
			case "img": els[x].onmouseover	= a_onmouseover_img;	// We also want them to open links in a new window, so don't break here

			case "ext": els[x].onclick	= a_onclick_ext;	break;
			case "ucl": els[x].onclick	= a_onclick_ucl;	break;
			case "prn": els[x].onclick	= a_onclick_prn;	break;
			case "pop": els[x].onclick	= a_onclick_pop;	break;
		}
	}
}


// Load persistant font sizes set with a_onclick_fsize
function onload_fsizes ()
{
	fontElem	= [];
	fontNum		= CookieJar.get ("fontNum");
	fontSize	= CookieJar.get ("fontSize");
	fontUnit	= CookieJar.get ("fontUnit");

	if (fontNum != null && fontSize != null && fontUnit != null)
	{
		for (x = parseInt (fontNum); x--; )
			if ((e = CookieJar.get ("fontEl_" + x)) != null)
				fontElem.push (e);

		a_onclick_fsize (fontElem, fontSize, fontUnit);
	}
}


// Double-tap prevention
function form_onsubmit_nodbltap ()
{
	if (!this.submitted)
		return this.submitted = true
	return false;
}


// Set up clearable default value in an edit box
function form_init_defaulttext (input)
{
	if (input && input.type == "text")
	{
		input.onfocus	= function () { if (this.value == this.defaultValue)	this.value = ""; };
		input.onblur	= function () { if (this.value == "")			this.value = this.defaultValue; };
	}
}


// Set up pretty niceness in search box
function form_onload_search (form)
{
	form_init_defaulttext (form.find);
}


// Set up pretty niceness in signup box
function form_onload_signup (form)
{
	form_init_defaulttext (form.signup_email);
}

// Set up pretty niceness in product notification box
function form_onload_notify (form)
{
	form_init_defaulttext (form.notify_name);
	form_init_defaulttext (form.notify_email);
}


// Set up pretty niceness in login form
function form_onload_login (form)
{
	var lu		= form.login_username;
	var lp		= form.login_password;
	var lt		= form.login_passtext;

	form_init_defaulttext (lu);

	if (lt)
	{
		lp.value		= "";

		lp.style.display	= "none";
		lt.style.display	= "inline";

		lt.onfocus		= function ()
		{
			lp.style.display	= "inline";
			lt.style.display	= "none";
			lp.focus ();
		}

		lp.onblur		= function ()
		{
			if (!this.value)
			{
				lp.style.display	= "none";
				lt.style.display	= "inline";
			}
		}
	}
	else
	{
		lt		=  lp.cloneNode (false)
		lt.id		= "login_passtext";
		lt.name		= "login_passtext";

		lt.onfocus	= function ()
		{
			this.parentNode.replaceChild (lp, this)
			lp.focus ();
		};

		lp.onblur	= function ()
		{
			if (!this.value)
				this.parentNode.replaceChild (lt, this);
		};

		lp.value	= "";


		lt.setAttribute ("type", "text");
		lp.parentNode.replaceChild (lt, lp);
	}
}


// Run any initialisaton functions we need to on forms
function init_forms ()
{
	for (var x = document.forms.length; x--; ) switch (document.forms[x].name)
	{
		case "login":	form_onload_login	(document.forms[x]);			break;
		case "search":	form_onload_search	(document.forms[x]);			break;
		case "signup":	form_onload_signup	(document.forms[x]);			break;
		case "notify":	form_onload_notify	(document.forms[x]);			break;
		//default:	document.forms[x].onsubmit	= form_onsubmit_nodbltap;	break
	}
}

//DomLoaded.load (onload_fsizes);
DomLoaded.load (init_as);
DomLoaded.load (init_forms);

