/*
Contextual Help in Form v.0.1
Copyright (C) 2006  Andrea Paiola
http://www.andreapaiola.it

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.
*/

/*
Adapted by William Ghelfi <trumbitta@gmail.com> 20060928
See the "[Wiz]" comments in the code.
*/

function addHelp(nome,testo_apertura,testo_chiusura)
{
	var iOffset, iEnd;
	var strHref, strID;
	var objHelp;
	
	if (document.getElementById && document.appendChild && document.removeChild)
	{
		var objHelpform = document.getElementById(nome);
		
		if(objHelpform != null){
		
		var objAnchors = objHelpform.getElementsByTagName('a');
		for (var iCounter=0; iCounter<objAnchors.length; iCounter++)
		{	
			strID = getIDFromHref(objAnchors[iCounter].href);
			//[Wiz] 'If - Else' condition Added by Wiz for non-help exclusions	
			auxID = 'gnappo';
			var auxAttributes = objAnchors[iCounter].attributes;
			if (auxAttributes.getNamedItem("id") != null) {
				auxID = auxAttributes.getNamedItem("id").value;
			}

			if (strID != null) {
					objHelp = document.getElementById(strID);
					objHelp.style.display = 'none';	
					objAnchors[iCounter].firstChild.nodeValue = testo_apertura;
					objAnchors[iCounter].onclick = function(event){return expandHelp(this, event,testo_apertura,testo_chiusura);}
					objAnchors[iCounter].onkeypress = function(event){return expandHelp(this, event,testo_apertura,testo_chiusura);}	
					objAnchors[iCounter].parentNode.appendChild(objHelp);
			}
			else {
				objAnchors[iCounter].style.display = 'none';
			}
			if (auxID.indexOf('showme') != -1) {
				objAnchors[iCounter].style.display = 'inline';
			}
		}
		
				
		// Release memory to prevent IE memory leak
		// Thanks to Mark Wubben <http://novemberborn.net/>
		// for highlightint the issue
		
		objHelpform = null;
		objAnchors = null;
		}
	}
}

function getIDFromHref(strHref)
{
	iOffset = strHref.indexOf('#') + 1;
	iEnd = strHref.length;
	//[Wiz] Added by Wiz for non-help exclusions
	helpOffset = strHref.indexOf('help');
	if (helpOffset == -1)
	{
		return null;
	}
	//[wiz] End
	return strHref.substring(iOffset, iEnd);
}

function expandHelp(objAnchor, objEvent, testo_apertura, testo_chiusura)
{
	var iKeyCode;

	if (objEvent && objEvent.type == 'keypress')
	{
		if (objEvent.keyCode)
			iKeyCode = objEvent.keyCode;
		else if (objEvent.which)
			iKeyCode = objEvent.which;
		
		if (iKeyCode != 13 && iKeyCode != 32)
			return true;
	}
	
	strID = getIDFromHref(objAnchor.href);
	objHelp = document.getElementById(strID);
	
	if (objHelp.style.display == 'none'){
		objAnchor.firstChild.nodeValue = testo_chiusura;		
		objHelp.style.display = 'block';
	}else{
		objAnchor.firstChild.nodeValue = testo_apertura;
		objHelp.style.display = 'none';
	}

	return false;
}

