// Copyright Informatique Direct Impact 2005
// Author: yanick rochon

var language;

//**********************************************************
// Page init is called by every page by onLoad() in BODY tag
//**********************************************************
function init()
{
	if (navigator.appName == 'Microsoft Internet Explorer')
	{
		try
		{
			document.getElementById("bottom").style.marginLeft = "2px";
		}
		catch (e)
		{
		
		}
		try
		{
			document.getElementById("loginHeader").style.width = "390px";
		}
		catch (e)
		{
		
		}
	}
}
// Page init /

function stringReplace(srcString, findWhat, replaceWith)
{
	var pos = 0;
	var len = findWhat.length;
	pos = srcString.indexOf(findWhat);
	
	//alert(srcString);
	
	while (pos != -1)
	{
			preString = srcString.substring(0, pos);
			postString = srcString.substring(pos + len, srcString.length);
			srcString = preString + replaceWith + postString;
			pos = srcString.indexOf(findWhat);
	}
	
	return srcString;
}

function getImgPath(imgID)
{
	var fullPath = document.images[imgID].src;
	var fullPathLength = fullPath.length;
	var imgPath = '';
	
	for (var i = fullPathLength; i >= 0; i--)
	{
		character = fullPath.substr(i, 1);

		if (character == '/')
		{
			break;
		}
	}
	
	imgPath = fullPath.substr(0, i) + '/';
	return imgPath;
}

// Show or hide a specific object on the page
function showHide(arg, id)
{
	// Images that are used for the Expand/Collapse
	var ExpImg = 'Expand.gif';
	var ColImg = 'Collapse.gif';
	
	// Alt text that is used for the Expand/Collapse image
	var altExpText = 'Ouvrir';
	var altColText = 'Fermer';
	
	// Path where are stored images that are used for the expand and collapse
	//var imgPath = getImgPath('showHide' + id);
	var imgPath = '../../img/icons/';
	
	maxMinImg = 'showHide' + id;
	what = document.getElementById(arg);
	
	// Show leaves
	if (what.style.display == 'none')
	{
		// Get path from the MaxMin image - 10 (Expand.gif) to be able to change 
		// the Expand image to Collapse
		MinSrc = imgPath + ColImg;
	
    	document.images[maxMinImg].src = MinSrc;
    	document.images[maxMinImg].alt = altColText;
    	
    	what.style.display = '';
		//lastBranchStatus = 'block';
    }
    // Hide leaves
	else
	{
		// Get path from the MaxMin image - 12 (Collapse.gif) to be able to change 
		// the Expand image to Collapse
		MaxSrc = imgPath + ExpImg;
	
		document.images[maxMinImg].src = MaxSrc;
		document.images[maxMinImg].alt = altExpText;
    	
    	what.style.display = 'none';
		//lastBranchStatus = 'none';
	}
}

function getPageName()
{
	strURL = location.href;
	start = strURL.lastIndexOf("/") + 1;
	
	if (strURL.lastIndexOf("?") != -1)
	{
		intLength = strURL.lastIndexOf("?") - start;
	}
	else
	{
		intLength = strURL.length - start;
	}
		
	pageName = strURL.substr(start, intLength);
	return pageName;
}

function redirect(targetPageName)
{
	var currentPageName;
	var url;
	var target;
	
	currentPageName = getPageName();
	url = location.href;
	target = stringReplace(url, currentPageName, '');
	
	window.location = target + targetPageName;
}

function searchRecord()
{
	url = location.href.substring(0, location.href.indexOf('?'));
	recordID = document.getElementById('cboSearch').value;
	
	location.href = url + '?searchID=' + recordID;
}

function filterRecords()
{
	url = location.href.substring(0, location.href.indexOf('?'));
	filter = document.getElementById('cboFilter').value;
	
	location.href = url + '?filter=' + filter;
}

// First function to keep current value before user change it
var currentValue
function keepValue(arg)
{
	currentValue = arg.value;
}

function validation(arg, msg)
{
	// Check if user is just adding a new record
	action = Request.QueryString('action');
	if (action != 0)
	{
		if (!confirm(msg))
		{
			arg.value = currentValue;
		}
	}
}