/**
	* Collection of usefull javascripts
  *
  * @example
  * <script type="text/javascript" src="epitools/_epitools.js"></script>
  *
  * Encoding: ISO-8859-1
  * @package EpiTools
  * @author Epistema {@link http://www.epistema.com}
  * @copyright Copyright 2001 - 2006, Epistema
  * @filesource
  * @version $Rev: 1236 $
  */


/**
 * Open a centered window. Open the window as a modal dialog if possible
 *
 * Warning : if a window is open modal, the links are not interpreted the same way,
 *           in particular href="javascript:..." opens a new window and should not be used
 *
 */
function openCenteredWindow(url, name, width, height, params, bNoModal, bNoRefresh)
{
	if (window.showModalDialog && !bNoModal)
	{
		var winParms = "dialogWidth:" + width + "px;dialogHeight:" + height + "px;help:no";

		if (params)
		{
			params = params.replace(/=/ig, ":");
			params = params.replace(/,/ig, ";");

			winParms += ";" + params;
		}

		var newWin = window.showModalDialog(url, window, winParms);

		if (!bNoRefresh)
			RefreshWindow(window);

		return newWin;
	}
	else
	{
		var left = Math.floor( (screen.width - width) / 2);
		var top = Math.floor( (screen.height - height) / 2);
		var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;

		if (params)
			winParms += "," + params;

		var win = window.open(url, name, winParms);

		if (parseInt(navigator.appVersion) >= 4)
			win.window.focus();

		return win;
	}
}

/**
 * if a window has been open with openCenteredWindow, you should use this
 * in order to get the window opener.
 * @global mixed OpenerWindow
 */
var OpenerWindow = false;

try
{
	if (window.top.dialogArguments)
		OpenerWindow = window.top.dialogArguments;
	else
		OpenerWindow = window.top.opener;
}
catch(e)
{
}

function RefreshWindow(aWin)
{
	var theDate = new Date();

	var openerLocation = aWin.location;
	var newURL = openerLocation.protocol + "//" + openerLocation.host + openerLocation.pathname;
	var search = openerLocation.search;

	if (search.length != 0)
	{
		var index = search.indexOf("RefreshTime");
		if(index != -1)
		{
			// remove old RefreshTime in search
			search = search.substring(0, index) + search.substring(index + "RefreshTime=000".length);
			var lastChar = search.substring(search.length - 1);
			if(lastChar == "&" || lastChar == "?")
				search = search.substring(0, search.length - 1);
			search = search.replace("?&", "?");
			search = search.replace("&&", "&");
		}
	}

	if (search.length == 0)
		newURL += "?";
	else
		newURL += search + "&";

	var ms = theDate.getMilliseconds();
	if(ms < 10)
		ms = "00" + ms;
	else if(ms < 100)
		ms = "0" + ms;
	newURL += "RefreshTime=" + ms;

	newURL += openerLocation.hash;

	aWin.setTimeout("window.location.href = '"+newURL+ "';", 50);
}

/**
 * Password generation :
 * @param string ElementId the element's ID for which to set the password (optional)
 * @param integer PasswordLength length of the generated password
 * @param bool bAddNonAlphanum Add non alpha num chars
 * @return string the password if ElementId is empty
 */
function GeneratePassword(ElementId, PasswordLength, bAddNonAlphanum)
{
	var sPassword = "";

	for (i=0; i < PasswordLength; i++)
	{
		numI = getRandomNum();

		if (!bAddNonAlphanum)
		{
			while (checkPunc(numI))
				numI = getRandomNum();
		}

		sPassword = sPassword + String.fromCharCode(numI);
	}

	if (document.getElementById(ElementId))
		document.getElementById(ElementId).value = sPassword
	else
		return sPassword;
}

function getRandomNum()
{
	// between 0 - 1
	var rndNum = Math.random()

	// rndNum from 0 - 1000
	rndNum = parseInt(rndNum * 1000);

	// rndNum from 33 - 127
	rndNum = (rndNum % 94) + 33;

	return rndNum;
}

function checkPunc(num)
{
	if ((num >=33) && (num <=47)) { return true; }
	if ((num >=58) && (num <=64)) { return true; }
	if ((num >=91) && (num <=96)) { return true; }
	if ((num >=123) && (num <=126)) { return true; }

	if ((num >=65) && (num <=90)) { return true; } // capitals
	if ((num == 108) ||
			(num == 49) ||
			(num == 73)) { return true; } // l and 1 and I

	return false;
}

function Upper(TextField)
{
	var CurrentTextValue = TextField.value;
	var NewTextValue = CurrentTextValue.toUpperCase();

	if (CurrentTextValue != NewTextValue)
		TextField.value = NewTextValue;
}

function stripNonAlphaNum(TextField)
{
	TextField.value = TextField.value.replace(/[^a-zA-Z0-9]+/g,'');
}
