// Copyrights DataCHEST 2002
// Author: yanick rochon

var currentLine;
var currentClass;
var currentTab;
var currentButton;

function preloadImages()
{
	var doc = document;
	
	if (doc.images)
	{
		if(!doc.imagesArray)
		{
			doc.imagesArray = new Array();
		}
					
		var i;
		var j 	 = d.MM_p.length;
		var args = preloadImages.arguments; 

		for (i=0; i<args.length; i++)
		{
			if (args[i].indexOf("#") != 0)
			{ 
				doc.imagesArray[j] = new Image; 
				doc.imagesArray[j++].src = args[i];
			}
		}
	}
}

function swapImage(arg)
{	
	// Do this loop because if the image is placed under the <A HREF>
	// Mozilla is taking the cariage return as a childNode
	for (var i=0; i<arg.childNodes.length; i++)
	{
		if (arg.childNodes[i].tagName == 'IMG' || arg.childNodes[i].tagName == 'INPUT')
		{
			var img 		 = arg.childNodes[i].id;
			var imgSrc 		 = arg.childNodes[i].src;
			// Check if image is already selected
			if (imgSrc.indexOf('On') != -1)
			{
				return;
			}
			var imgExtension = imgSrc.substr(imgSrc.length - 4, 4);
			var imgOver 	 = imgSrc.substr(0, imgSrc.length - 4) + 'Over'+ imgExtension;
			
			break;
		}
	}
	
	document.getElementById(img).src = imgOver;
}

function restoreImage(arg)
{
	// Do this loop because if the image is placed under the <A HREF>
	// Mozilla is taking the cariage return as a childNode
	for (var i=0; i<arg.childNodes.length; i++)
	{
		if (arg.childNodes[i].tagName == 'IMG' || arg.childNodes[i].tagName == 'INPUT')
		{
			var img 		 = arg.childNodes[i].id;
			var imgSrc 		 = arg.childNodes[i].src;
			// Check if image is already selected
			if (imgSrc.indexOf('On') != -1)
			{
				return;
			}
			var imgExtension = imgSrc.substr(imgSrc.length - 4, 4);
			var imgNormal 	 = imgSrc.substr(0,imgSrc.length - 8) + imgExtension;
			
			break;
		}
	}
	
	document.getElementById(img).src = imgNormal;
}

function tabOver(tab)
{
	currentTab = tab;
	tab.className = 'tabOn';
	tab.style.cursor = "hand";
}

function restoreTab()
{
	currentTab.className = 'tabOff';
}

function checkAll()
{
	var form = document.forms[0];
	var chkAll = document.getElementById('chkAll').checked;
	
	var lineIndex = '';
	
	for (var i = 0; i < form.length; i++)
	{
		if (form.elements[i].type == 'checkbox' && form.elements[i].name != 'chkAll')
		{
			form.elements[i].checked = chkAll;
			
			lineIndex = form.elements[i].name.substr(5);
			
			if (!isNaN(lineIndex))
			{
				if (lineIndex % 2 == 0)
				{
					var className = 'tdOdd';
				}
				else
				{
					var className = 'tdEven';
				}
						
				selectLine(lineIndex,className);
			}
		}
	}
}

// This function is necessary to be called by the onLoad event because when the user press No
// for a delete confirmation, the history leads him to the previous page, checkbox is checked
// but line is not selected
function onLoadCheck()
{
	var form = document.forms[0];
	var lineIndex = '';
	
	// Check if all checkboxes are selected
	for (var i = 0; i < form.length; i++)
	{
		if (form.elements[i].type == 'checkbox' && form.elements[i].checked && form.elements[i].name != 'chkAll')
		{
			lineIndex = form.elements[i].name.substr(5);
			
			if (!isNaN(lineIndex))
			{
				if (lineIndex % 2 == 0)
				{
					var className = 'tdOdd';
				}
				else
				{
					var className = 'tdEven';
				}

				selectLine(lineIndex,className);
			}
		}
	}
}

function selectLine(lineIndex,className)
{
	var form = document.forms[0];
	var check = 'check' + lineIndex;
	var line = 'line' + lineIndex;
	var allSelected = true;

	if (document.forms[0].elements(check).checked)
	{
		highlight(document.getElementById(line));
		
		// Check if all checkboxes are selected
		for (var i = 0; i < form.length; i++)
		{
			if (form.elements[i].type == 'checkbox' && form.elements[i].name != 'chkAll')
			{
				if (form.elements[i].checked == false)
				{
					allSelected = false;
					break;
				}
			}
		}
		
		if (allSelected)
		{
			document.all.chkAll.checked = true;
		}
	}
	else
	{
		currentLine = document.getElementById('line' + lineIndex);
		currentClass = className;
		
		clearHighlight();
		
		document.all.chkAll.checked = false;
	}
}

function highlight(arg)
{
	var line = document.getElementById(arg.parentNode.id);
	currentClass = line.className;
	
	if (arg.tagName != 'IMG')
  	{
		currentClass = line.className;
		line.className = 'tdOver';
  
		highlightCheck(line);
		//highlightDelete(line);
		highlightCopy(line);
  
		//Set the text in the status bar
		setStatus('')
  	}
}

function highlightCheck(line)
{	
	try
	{
		var image = 'Check' + line.id.substr(4);
		document.images[image].src = '../images/icons/Check-3.gif';		
	}

	catch (e)
	{
		switch (e.name)
		{
			case "TypeError" :
				break;

			default:

		}
	}
}

function highlightDelete(line)
{
	try
	{
		// The function MM_swapImage is defined directly into the page and is provided by Dreamweaver
		//MM_swapImage('Del' + line.id.substr(4),'','../images/icons/Delete.gif',1);
	}
	
	catch (e)
	{
		switch (e.name)
		{
			case "TypeError" :
				break;
				
			default:
			
		}
	}
}

function highlightCopy(line)
{
	try
	{
		// The function MM_swapImage is defined directly into the page and is provided by Dreamweaver
		MM_swapImage('CopyMove' + line.id.substr(4),'','../images/icons/CopyFile-3.gif',1);
	}

	catch (e)
	{
		switch (e.name)
		{
			case "TypeError" :
				break;

			default:

		}
	}
}

function clearHighlight(arg)
{
	var line = document.getElementById(arg.parentNode.id);
	
	if (line != null)
	{
		line.className = currentClass;
	}

	try
	{
		//MM_swapImage('Del' + currentLine.id.substr(4),'','../images/icons/Delete.gif',1);
		//MM_swapImage('Check' + currentLine.id.substr(4),'','../images/icons/Check.gif',1);
	}

	catch (e)
	{
		switch (e.name)
		{
         case "TypeError" :
            break;

         default:
         	
		}
	}

	setStatus('');
}

function buttonOver(arg)
{	
	arg.className = 'buttonOver';
}

function buttonNormal(arg)
{
	arg.className = 'button';
}