//Code modified by Chris A. on 06-25-08 to fix a bug that made the entire thing crash whenever a mailto was included in the form.
//Please make sure you put the class = "mail" on all mailtos for this checking to work.

window.onload = addHelp;

function addHelp()
{
  var strID, objHelp;

  // Check we're working with a DOM compliant browser
  if (document.getElementById && document.appendChild && document.removeChild)
  {
    var objHelpform = document.getElementById('form1');
	
	
    if(objHelpform != null)
	{
		var objAnchors = objHelpform.getElementsByTagName('a');
		
		/*alert("Length is " + objAnchors.length + " .");
		alert("objAnchors[0] is " + objAnchors[0] + " .");
		alert("objAnchors[0].className is " + objAnchors[0].className + " .");*/
		
		// Iterate through all anchors in the form
		for (var iCounter=0; iCounter<objAnchors.length; iCounter++)
		{
		  /*alert(iCounter + " : " + objAnchors[iCounter].className + " seen");
	
		  alert("objAnchors[iCounter] is " + objAnchors[iCounter] + " .");
		  alert("objAnchors[iCounter].className is " + objAnchors[iCounter].className + " .");
		  
		  alert("In front of the if");*/
		  if (objAnchors[iCounter].className != 'leave' && objAnchors[iCounter].className != 'mail')
		  {
			  //alert("Inside the if.");
			// Locate the associated help text's container,
			// and hide it
			strID = getIDFromHref(objAnchors[iCounter].href);
			//alert("strID is " + strID + ".");
			objHelp = document.getElementById(strID);
			objHelp.style.display = 'none';
	
			// Add events to reveal/hide
			objAnchors[iCounter].onclick = function(event){return expandHelp(this, event);}
			objAnchors[iCounter].onkeypress = function(event){return expandHelp(this, event);}
	
			// Move beneath current form field
			objAnchors[iCounter].parentNode.appendChild(objHelp);
		  }
		  //alert("Outside the if.");
		}
		
		//alert("Outside the loop.");
		
		// Remove the remainder of the old help section
		var objOldnode = document.getElementById('helpcontainer');
		
		objOldnode.parentNode.removeChild(objOldnode);
	
		// Release memory to prevent IE memory leak
		// Thanks to Mark Wubben <http://novemberborn.net/>
		// for highlightint the issue
		objHelpform = null;
		objHelp = null;
		objAnchors = null;
	  }
  	}
}

// Return the ID of the element from the "href" attribute
function getIDFromHref(strHref)
{
  var iOffset = strHref.indexOf('#') + 1;
  var iEnd = strHref.length;

  return strHref.substring(iOffset, iEnd);
}

function expandHelp(objAnchor, objEvent)
{
  var iKeyCode;

  // If from the keyboard, check the user is
  // activating it rather than tabbing through
  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);

  // Toggle on and off
  if (objHelp.style.display == 'none')
    objHelp.style.display = 'block';
  else
    objHelp.style.display = 'none';

  return false;
}
