////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	DLib Form Field functions - copyright davidviner.com 2010-2011
//
//	01.06.2011	5.9.2	DJV		Fixed incs bug in button.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//F/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Anchor tag link.
// @href		string		The well-formed HTML for the field.
// @text		string		The 'text' that should appear as the clickable link (this can also be an image if required).
// @href		string		The URL to jump to when the link is clicked.
// @[target]	string		The target window if required.
// @[other]		string		Any other parameters required.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function href (text, href, target, other)
{
	if (target == undefined) target = "";
	if (other == undefined) other = "";

	if (target != "")
		target = ' target="' + target + '"';

	if (other != "")
		other = " " + other;

	if (href.toLowerCase (href.substr (0, 7)) != "mailto:")
		href = encodeURI (href);

	return '<a href="' + href + '"' + target + other + '>' + text + '</a>';
}

//F/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Anchor tag for a popup window.
// @pophref		string		The well-formed HTML for the field.
// @text		string		The 'text' that should appear as the clickable link (this can also be an image if required).
// @href		string		The URL for the popup window to open when the link is clicked.
// @[width]		int			Popup window width (default 600).
// @[height]	int			Popup window height (default 750).
// @[name]		string		The target window name (default is "popup").
// @[att]		string		Specify which attributes to enable (m = menubar, s = status bar, t = toobar, b =
//							scrollbars). All default to off except scrollbars.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function pophref (text, href, width, height, name, att)
{
	if (width == undefined) width = 600;
	if (height == undefined) height = 750;
	if (name == undefined) name = "popup";
	if (att == undefined) att = "b";

	href = encodeURI (href);

	var attrs = "menubar=" + (att.indexOf ("m") > -1 ? "yes" : "no") +
		",status=" + (att.indexOf ("s") > -1 ? "yes" : "no") +
		",toolbar=" + (att.indexOf ("t") > -1 ? "yes" : "no") +
		",scrollbars=" + (att.indexOf ("b") > -1 ? "yes" : "no");

	return '<a href="javascript:void(0)" onclick="window.open(\'' + href + "', '" + name +
		"', 'width=" + width + ',height=' + height + ',' + attrs + '\'); return false;">' +
		text + '</a>';
}

//F/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Radio button field.
// @radio		string	The well-formed HTML for the field.
// @incs		string	The INCS value (See HtmlGlobal).
// @val			string	The value to be used if this radio button is selected.
// @[text]		string	The text that should be printed next to the check box.
// @[chk]		boolean	Whether the button should be selected when initially displayed (default is false).
// @[other]		string	Any other parameters required.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function radio (incs, val, text, chk, disabled, other)
{
	if (val == undefined) val = "";
	if (text == undefined) text = "";
	if (chk == undefined) chk = false;
	if (disabled == undefined) disabled = false;
	if (other == undefined) other = "";

	return '<input type="radio"' + _handleIDC (incs) + ' value="' + val + '"' +
		(chk ? ' checked="checked"' : '') + (disabled ? ' disabled="disabled"' : '') +
		((other != "") ? ' ' + other : '') + ' /> ' + text + "\n";
}

//F/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Input button field. If only two parameters are detected then they are assumed to be value and onClick (buttons don't
// require a name setting if all that is required is for them to perform some function using onClick).
// @button		string	The field HTML.
// @incs		string	The INCS value (see HtmlGlobal) or, if 2 params sent, then this becomes the 'value' instead.
// @val			string	The text to be displayed on the button.
// @[onclick]	int		Either the JavaScript that will get sent to the onClick parameter or, if the first character is
//						'@' or '£', the rest of the onClick value is treated as a link (@ opens up in the same window,
//						£ in a new window). Additionally, this field also recognises the format "£winname£url" where
//						winname is the name of window to be used (the default window name is 'Win').
// @[other]		string	Any other parameters required.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function button (incs, val, onclick, other)
{
	if (onclick == undefined && other == undefined)
	{
		onclick = val;
		val = incs;
		other = incs = "";
	}
	else
	{
		if (onclick == undefined) onclick = "";
		if (other == undefined) other = "";
	}

	if (incs != "")
		incs = _handleIDC (incs);

	if (onclick != "")
	{
		if (onclick.charAt (0) == "@")
			onclick = "window.location = &#039;" + encodeURI (onclick.substr (1)) + "&#039;";
		else
		if (onclick.charAt (0) == "£")
		{
			var winName= "Win";
			onclick = onclick.substr (1);

			if (onclick.indexOf ("£") > -1)
			{
				var oc = onclick.split ("£");
				winName = oc[0];
				onclick = oc[1];
			}

			onclick = "window.open (&#039;" + encodeURI (onclick) + "&#039;,&#039;" + winName + "&#039;,&#039;&#039;)";
		}
	}

	return '<input ' + (incs == '' ? '' : ' ' + incs) + 'type="button" value="' + val + '" onclick="' + onclick + '"' +
		(other == "" ? "" : ' ' + other) + ' />';
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


