/**
	* 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 (!width)
		width = 800;

	if (!height)
		height = 550;

	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 alpha = 'abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ';
	var num   = '23456789';
	var punct = '!"#$%&\'()*+,-./:;=?@[]_';

	var aPassword = [];

	// Init password with spaces
	for(var i = 0; i < PasswordLength; i++)
		aPassword[i] = ' ';

	// One number
	i = parseInt(Math.random() * PasswordLength);
	aPassword[i] = num.charAt(parseInt(Math.random() * num.length));

	// One punctuation character
	if (bAddNonAlphanum)
	{
		i = parseInt(Math.random() * PasswordLength);
		while(aPassword[i] == ' ')
		{
			i++;
			if (i >= PasswordLength)
				i = 0;
		}
		aPassword[i] = punct.charAt(parseInt(Math.random() * punct.length));
	}

	// Complete blanks with random characters
	var characters = alpha + num;
	//if (bAddNonAlphanum)
	//	characters += punct;

	for (i=0; i < PasswordLength; i++)
	{
		if (aPassword[i] == " ")
			aPassword[i] = characters.charAt(parseInt(Math.random() * characters.length));
	}

	// Get string
	var sPassword = '';
	for (i=0; i < PasswordLength; i++)
		sPassword += aPassword[i];

	// Populate form field with generated password
	if (document.getElementById(ElementId))
		document.getElementById(ElementId).value = sPassword
	else
		return aPassword;
}

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,'');
}

function strip_tags (input, allowed) 
{   
    allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
    var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
        commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
    return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {        return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
    });
}

function IsHTMLSnippetEmpty(input)
{
	input = strip_tags(input, '<img><object>');
	
	input = input.replace('&nbsp;', ' ');
	input = input.replace('&#160;', ' ');
	input = input.replace('\xc2\xa0', ' ');

	input = input.strip();
	
	return input == "";
}

