/*
* etnoFramework - PHP framework  Copyright (C) 2009 Radek Krawiec
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
* Redistributions of files must retain the above copyright notice.
*/

/**
 * This function hides fields whose ID's are in the comma separated
 * list "sectionFieldsListId".
 * @param sectionFieldsListId - comma-separated list of ID's of fields that are to be shown or hidden
 * @param linkId - id the the <a href> element whose label is to be changed
 * @param linkLabel - new link label
 * @return
 */
function showHideSection(sectionFieldsListId,linkId,visibleLinkLabel,hiddenLinkLabel)
{
	var sectionFieldsList = document.getElementById(sectionFieldsListId);
	if (sectionFieldsList != null)
	{
		var list = sectionFieldsList.value;
		var ids = list.split(",");
		for (i=0; i<ids.length; i++)
		{
			var field = document.getElementById(ids[i]);
			if (field != null)
			{
				var tr = field.parentNode.parentNode;
				showHideElement(tr);
			}
		}
	}
	/*
	 * Change link label
	 */
	var link = document.getElementById(linkId);
	if (link != null)
	{
		if (link.innerHTML == visibleLinkLabel)
		{
			link.innerHTML = hiddenLinkLabel;
		}
		else
		{
			link.innerHTML = visibleLinkLabel;
		}
	}
}

/**
 * This function hides an element that is visible, or show one that
 * is hidden.
 * @param elem - HTML element object
 * @return void
 */
function showHideElement(elem)
{
	if (elem != null)
	{
		if (elem.style.display != 'none')
		{
			elem.style.display = 'none';
		}
		else
		{
			elem.style.display = '';
		}
	}
}

function isElementVisible(id)
{
	var e = document.getElementById(id);
	if (e)
	{
		if (e.style.display=='none' || e.style.visibility=='hidden')
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	return false;
}

/**
 * This method takes the form whose name is given as the parameter,
 * finds its all fields, reads their values and creates a query string from them.
 * @param formName - string name of the form (name attribute of the form tag)
 * @return a query string, e.g. "name=Kasia&age=22&language=sv"
 */
function getQueryString (formName)
{
	var form = document.forms[formName];
	if (form == null)
	{
		throw "Form with name '"+formName+"' does not exist";
		return null;
	}
	
	var elems = form.elements;
	var queryString = '';
	for (i=0; i<elems.length; i++)
	{
		queryString += elems[i].name + '=' + elems[i].value + '&';
	}
	
	// remove the last ampersand
	queryString = queryString.substr(0, queryString.length-1);
	return queryString;
}