
// Continuing is likely to delete something, so request confirmation
function a_onclick_del ()
{
	var msg	= "This will delete the " + this.ocLabel + " from the database."	+ "\r\n\r\n"
		+ "Click 'OK' to delete it."						+ "\r\n\r\n"
		+ "Click 'Cancel' to keep it and return to the previous screen.";

	return confirm (msg);
}

// 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;
		}
	}

	if (document.FCKlist) for (var x = document.FCKlist.length; x--; )
	{
		var e	= FCKeditorAPI.GetInstance (document.FCKlist[x]);
		if (e && e.IsDirty ())	return confirm (msg);
	}

	return true;
}
function a_onclick_stickerSent ()
{
	
	var msg	= "Click 'OK' to mark this invoice as Sticker Sent."	+ "\r\n\r\n"
		+ "Click 'Cancel' to return to the previous screen.";

	return confirm (msg);
}

// 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;
}

// Check the REL attribute of <a> elements and assign an onclick 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 "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 "del":
					c = (els[x].getAttribute ("title") || "Delete item").split (" ");
					c.splice (0, 1);

					els[x].ocLabel		= c.join (" ");
					els[x].onclick		= a_onclick_del;
													break;
		}
	}
}


// Initialise select elements
function init_selects ()
{
	var els		= document.getElementsByTagName ("select");

	if (els) for (var x = els.length; x--; )
	{
		els[x].defaultValue = els[x].value;				// Set a default value (for a_onclick_ucl)

		if (/\bsort\b/i.test (els[x].className)) els[x].onchange = function ()
		{
			var id = this.id.split ("_");

			window.location.href	= "?task=sort"
						+ "&id="	+ parseInt (id[1])
						+ "&pd="	+ parseInt (id[2])
						+ "&s0="	+ parseInt (id[3])
						+ "&s1="	+ this.value;
		}
	}
}

// Initialise form elements
// ToDo: Find a way to make this not bother at all if there are no 'cal' elements
function init_cals ()
{
	var elTypes		= ["a", "input", "select", "button"];
	var cals_close		= function ()
	{
		if (document.calendars) for (var x = document.calendars.length; x--; )
			document.calendars[x].closeCalendar ();
	};

	for (var y = 0; y < elTypes.length; y++)
	{
		var els		= document.getElementsByTagName (elTypes[y]);
		if (els) for (var x = 0; x < els.length; x++)
		{
			if (/\bcal\b/.test (els[x].className))
			{
				if (!document.calendars)
					document.calendars = [];
				document.calendars.push (new JsDatePick ({ useMode: 2, target: els[x].id, cellColorScheme: "torqoise" }));

				els[x].onfocus	= els[x].onclick;
			}
			else if (!els[x].onfocus)
				els[x].onfocus	= cals_close;
		}
	}
}

// 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);
	}
}

function openURL(url)
{
	window.open(url,"credits","width=300,height=400,top=0,left=0,toolbar=no,scrollbars=yes,menubar=no,status=no,location=no,resizable=yes");
}
//DomLoaded.load (onload_fsizes);
DomLoaded.load (init_as);
DomLoaded.load (init_selects);

