/* 
*  Author:  David Gailey
*  File name: ErrorMessages.js
*
*  functions that add error messages to the top of the page and remove all of them			
*  instance parameter is zero based
*  element affected must be a <div> with className 'error_messages' and should already contain a <ul>
*              
*/

function insertErrorMessage(msg,instance)
{
	var i;
	var emInstances = new Array;
	var x = document.getElementsByTagName('div');
	for(i=0;i<x.length;i++)
	{
		if(x[i].className.match('error_messages') != null)
		{
			emInstances.push(x[i]);
		}
	}
	if(typeof(instance)== 'undefined')instance = 0;  //default to 0 if none is declared
	else if(instance == 'last') instance = emInstances.length - 1; //can declare the 'last' to choose the last instance
	else instance = parseInt(instance);

	var em = emInstances[instance];
	var em_ul = em.getElementsByTagName('ul');
	if(typeof(msg)== 'undefined') msg = defaultMsg; //declare from properties file before including this js file
	msg = msg.toString();
	msg = msg.replace(/^(\s)*/, ''); //trim front of string
    msg = msg.replace(/(\s)*$/, ''); //trim end of string
	
	
	if(escape(em_ul[0].innerHTML).search(escape(msg)) == -1) //compared using escape to eliminate special characters that could cause problems
	{
		em_ul[0].innerHTML += '<li>' + msg + '</li>';
	}
	
}

function removeErrorMessages(instance)
{
	var emInstances = new Array;
	var x = document.getElementsByTagName('div');
	var i;
	
	
	for(i=0;i<x.length;i++)
	{
		if(x[i].className.match('error_messages') != null)
		{
			emInstances.push(x[i]);
		}
	}
	
	if(instance == 'last') instance = emInstances.length - 1; 
	
	if(typeof(instance) != 'undefined') //check if a specific instance is defined in the function call
	{
		var em_ul = emInstances[parseInt(instance)].getElementsByTagName('ul');
		em_ul[0].innerHTML = ''; //only erases first ul, may need to change later if multiple 'ul's in error_messages div need erased
	}
	else
	{
		for(i=0;i<emInstances.length;i++)
		{
			var em_ul = emInstances[i].getElementsByTagName('ul');
			em_ul[0].innerHTML = ''; //only erases first ul, may need to change later if multiple 'ul's in error_messages div need erased
		}
	}
}












