////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	DLib Form Field functions - copyright davidviner.com 2010-2011
//
//	08.12.2011	5.10.1	DJV		Changed parameter href to url in href to avoid confusion.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//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).
// @url			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, url, target, other)
{
	if (target == undefined) target = "";
	if (other == undefined) other = "";

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

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

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

	return '<a href="' + url + '"' + 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) + ' />';
}

//F/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Text input field. If only two parameters are detected then they are assumed to be the name and size.
// @textInput	string		The field HTML.
// @incs		string		The INCS value.
// @[val]		string		The value to be displayed in the field.
// @[size]		int/[int]	The size of the field (default 20). If the size is sent as a negative value then both the
//							size and maxlength parameters are set to the absolute value. To set separate size and
//							maxlength values use an array (size, maxlength).
// @[other]		string		Any other parameters required.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function textInput (incs, val, size, other)
{
	if (size == undefined)
	{
		if (val == undefined)
		{
			val = "";
			size = 20;
		}
		else
		{
			size = parseInt (val);
			val = "";
		}
	}
	else
	if (val == undefined)
		val = "";

	if (other == undefined)
		other = "";

	ml = 0;

	if (isArray (size))
	{
		ml = size [1];
		size = size [0];
	}
	else
	if (size < 0)
		size = ml = Math.abs (size);

	return '<input type="text"' + _handleIDC (incs, 3) + ' value="' + val.replace (/"/g, "&quot;") +
		'" size="' + size + '"' + (ml > 0 ? ' maxlength="' + ml + '"' : "") +
		(other == "" ? "" : " " + other) + ' />';
}

//F/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Select field initialiser. Creating select fields is performed using three functions: select (which initialises the
// field), option (one for each item required in the list) and endSelect which finishes it off. Well-formed HTML is
// only generated if all three are used.
// @select	string	The HTML for the initial part of the whole component.
// @incs	string	The INCS value.
// @[size]	int		The number of items to be displayed. If set to 1 (the default) then it generates a drop-down menu,
//					any value greater than one produces a scrollable list. A negative number forces it into multiple
//					select mode where multiple items can be selected using Ctrl-click and the name is forced to be an
//					array by appending '[]' to it unless it is already set.
// @[other]	string	Any other parameters required.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function select (incs, size, other)
{
	if (size == undefined)
		size = 1;

	if (other == undefined)
		other = "";

	var mult = "";

	if (size < 0)
		mult = ' multiple="multiple"';

	incs = _handleIDC (incs);

	if (mult != "" && incs.indexOf ("[]") == -1)
	{
		var nm1 = 'name"' + _attrs.name + '"',
			nm2 = 'name"' + _attrs.name + '[]"';

		incs = incs.replace (nm1, nm2);
	}

	return '<select' + incs + ' size="' + Math.abs (size) + '"' + (other == "" ? "" : " " + other) + mult + ">";
}

//F/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Option - a selection field item definition. Creating select fields is performed using three functions: select (which
// initialises the field), option (one for each item required in the list) and endSelect which finishes it off.
// Well-formed HTML is only generated if all three are used.
// @option		string	The HTML for the option part of a select component.
// @val			mixed	The value of this option.
// @[txt]		string	The text that represents the above value that should appear in the menu or list. If omitted
//						then the value is substituted in its place.
// @[selected]	boolean	If this option is to be selected when the list/menu is first displayed.
// @[other]		string	Any other parameters required.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function option (val, txt, selected, other)
{
	if (txt == undefined)
		txt = val;

	if (selected == undefined)
		selected = false;

	if (other == undefined)
		other = "";

	return '<option value="' + val + '"' + (other == "" ? "" : " " + other) +
		(selected ? ' selected="selected"' : "") + ">" + txt + "</option>";
}

//F/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Select field finaliser. Creating select fields is performed using three functions: select (which initialises the
// field), option (one for each item required in the list) and endSelect which finishes it off. Well-formed HTML is
// only generated if all three are used.
// @endSelect	string	The HTML for the finaliser part of a select component.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function endSelect ()
{
	return "</select>";
}

//F/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Checkbox field.
// @checkBox	string	The field HTML.
// @incs		string	The INCS (see HtmlGlobal) or an array of parameters.
// @[val]		string	The value to be used if this checkbox is ticked.
// @[txt]		string	Any text to be printed after (or before if the first character is a @) to the checkbox.
// @[chk]		boolean	Whether the box should be ticked when initially displayed (default is false).
// @[other]		string	Any other parameters required.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function checkBox (incs, val, txt, chk, other)
{
	if (val == undefined) val = "";
	if (txt == undefined) txt = "";
	if (chk == undefined) chk = false;
	if (other == undefined) other = "";

	var fld = '<input type="checkbox"' + _handleIDC (incs) + (val != "" ? ' value="' + val + '"' : "") +
		(chk ? ' checked="checked"' : "") + (other == "" ? "" : " " + other) +  ' />';

	return ((txt != "" && txt.charAt (0) == "@") ? txt.substr (1) + " " + fld : fld + txt) + "\n";
}

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


