String.prototype.reverse = function()
{
    var s = "";
    var i = this.length;
    while (i>0) {
        s += this.substring(i-1,i);
        i--;
    }
    return s;
}

function emailDecode(emailEncoded)
{
	return(emailEncoded.reverse());
}

function getAncestorByTagName(node, tagName)
{
	pNode = node;
	while(pNode.tagName != 'BODY')
	{
		pNode = pNode.parentNode;
		if(pNode.tagName == tagName)
			return(pNode);
	}
	return(null);
}

function submitForm(thisNode, action)
{
	pNode = getAncestorByTagName(thisNode, 'FORM');

	if(!pNode)
		return(true);		// Execute link.

	oldAction = pNode.getAttribute('action');		// Get original form action.
	pNode.setAttribute('action', action);
	pNode.submit();
	pNode.setAttribute('action', oldAction);		// Re-set to original form action.
	return(false);			// Don't execute link.
}

function showImageLoading(thisNode, id, enterFileNameMsg)
{
	if(!document.getElementById) return(false);

	iNode = document.getElementById("image"+id);
	if(iNode && !iNode.value)
	{
		window.alert(enterFileNameMsg);
		return(false);
	}

	dNode = document.getElementById("imgDiv"+id);
	if(dNode)
		dNode.style.display = 'block';

	return(true);
}

function checkAccountData(formNode, enterFullNameMsg, enterUserNameMsg)
{
	var msg = '';

	if(!formNode.fullname.value)
		msg += enterFullNameMsg + '\n';

	if(!formNode.username.value)
		msg += enterUserNameMsg + '\n';

	if(msg.length > 0)
	{
		window.alert(msg);
		return(false);
	}

	return(true);
}

function inputFocus(id)
{
	if(!document.getElementById) return;

	inputElem = document.getElementById(id);
	inputElem.focus();
}

function checkLogin(formNode, enterUsernameMsg, enterPasswordMsg)
{
	//alert(document.loginForm);
	//alert(obj.name);

	if(!formNode.username.value)
	{
		window.alert(enterUsernameMsg);
		return(false);
	}

	if(!formNode.password.value)
	{
		window.alert(enterPasswordMsg);
		return(false);
	}

	return(true);
}

function checkOccurrence(formNode)
{
/*
	if(formNode.dateSpec[0].checked==true)
	{
		if(!formNode.dateOnce.value)
		{
			window.alert('Please enter the event date.');
			return(false);
		}
	}

	else if(formNode.dateSpec[1].checked==true)
	{
		if(!formNode.elements['dateMulti[0]'].value
			&& !formNode.elements['dateMulti[1]'].value)
		{
			window.alert('Please enter the event start and stop dates.');
			return(false);
		}
		else if(!formNode.elements['dateMulti[0]'].value)
		{
			window.alert('Please enter the event start date.');
			return(false);
		}
		else if(!formNode.elements['dateMulti[1]'].value)
		{
			window.alert('Please enter the event stop date.');
			return(false);
		}
	}
*/

	return(true);
}

function replaceSpanWithAnchor(spanNode, onclickFunc)
{
	text = spanNode.childNodes[0].nodeValue;
	textNode = document.createTextNode(text)
	anchorNode = document.createElement('a');
	anchorNode.appendChild(textNode);
	anchorNode.setAttribute('href','#');
	anchorNode.onclick = onclickFunc;
	anchorNode.className = spanNode.className;

	spanNode.parentNode.replaceChild(anchorNode, spanNode);
}

function replaceAnchorWithSpan(anchorNode)
{
	text = anchorNode.childNodes[0].nodeValue;
	textNode = document.createTextNode(text)
	spanNode = document.createElement('span');
	spanNode.appendChild(textNode);
	spanNode.className = anchorNode.className;

	anchorNode.parentNode.replaceChild(spanNode, anchorNode);
}

/**************************************************************************************************\
DESCRIPTION:
	This function is called when the user clicks on "next" or "previous" links above the current
	image displayed by the PHP method addImageStack().  It changes the displayed image, changes
	next/previous nodes to anchors or spans as neccessary, and changes the destination link
	for the image.

	This function uses the "imgs" array, which is created by the PHP code jsLoadImages().
\**************************************************************************************************/

function showImage(thisNode)
{
	var i, imgNodes, thisParent;
	var spanNode, anchorNode;
	var filename;

	pnNodes = new Array();
	thisParent = thisNode.parentNode;
	for(i=0; i<thisParent.childNodes.length; i++)
		if(thisParent.childNodes[i].tagName == 'SPAN' || thisParent.childNodes[i].tagName == 'A')
			pnNodes[pnNodes.length] = thisParent.childNodes[i];

	if(thisNode.childNodes[0].nodeValue == pnNodes[0].childNodes[0].nodeValue)
	{
		//alert(thisNode.childNodes[0].nodeValue);
		if(--imgNum == 0)
			replaceAnchorWithSpan(pnNodes[0]);

		if(pnNodes[1].tagName == 'SPAN')
			replaceSpanWithAnchor(pnNodes[1], function(){return showImage(this)});
	}
	else
	{
		if(++imgNum == imgs.length-1)
			replaceAnchorWithSpan(pnNodes[1]);

		if(pnNodes[0].tagName == 'SPAN')
			replaceSpanWithAnchor(pnNodes[0], function(){return showImage(this)});
	}

	// Change the "src" attribute for the image node.
	// Note that the alternate text is unchanged.
	filename = imgs[imgNum].filename;		// The filename or URI of the "thumbnail" of the image.
	original = imgs[imgNum].original;		// The filename or URI of the original image.
	imgNodes = document.getElementsByTagName('img');
	for(i=0; i<imgNodes.length; i++)
	{
		if(imgNodes[i].className == thisParent.className)
		{
			imgNodes[i].setAttribute('src', filename);

			// Change the "href" value of the parent anchor of the image to point to the original image.
			imgNodes[i].parentNode.setAttribute('href', original);

			break;
		}
	}

	return(false);
}
