/**
 * (./) utils.js, v0.1, 05/12/07 13:12
 * (by) cousot stephane @ http://www.ubaa.net/
 * (cc) some right reserved
 *
 * Miscellaneous useful functions
 *
 * This document is a part of the AJAX Admin Framework project, based on the Wrapper project.
 * This project is released under a Creative Commons Licence BY
 * ‹ http://creativecommons.org/licenses/by/3.0/ ›
 */



/**
 * Return whether the current key pressed is not ENTER or not one of the given keyCode.
 * Use this function to disable form submit on enter keypress : see the onKeyPressed event
 *
 *	onkeypressed = "return pass(event)";
 *
 * @param Event		the key pressed event
 * @param array		[ a list of all accepted keycode ]
 */
function pass( event, mask ) {

	if ( !event ) event = window.event;	 // IE only
	
	var key = event.keyCode ? event.keyCode : event.which ;
	
	if ( !mask ) return key!=13;		// ENTER only
	for( var i=0; i<mask.length; i++ ) 	// use mask
		if ( mask[i]==key ) return true;
		
	return false;
	
}

/**
 * Open document in a new window with the given dimensions.
 * 
 * @param String	the target document
 * @param int		the window width
 * @param int		the window height
 */
function popup( url, width, height ) {
	
	width  = Math.min( screen.availWidth, width );
	height = Math.min( screen.availHeight, height );
	
	var x = Math.max( (screen.availWidth-width) / 2, 0 );
	var y = Math.max( (screen.availHeight-height-33) / 2, 0 );
	
	window.open(
		url,
		'_btpopup', 
		'toolbar=0,resizable=1,scrollbars=1,screenx='+x+',screeny='+y+',width='+width+',height='+height
	);
}







String.prototype.trim = function( pattern )
{
	if ( !pattern ) pattern = '\\s*';
    return this.replace(new RegExp("^"+pattern),'').replace(new RegExp(pattern+"$"),'');
}

// strip accents
String.prototype.stripaccent = function()
{

	var out = this;
	var patterns = [
		/[\xC0-\xC2]/g, /[\xE0-\xE2]/g, /[\xC8-\xCA]/g, /[\xE8-\xEB]/g, /[\xCC-\xCE]/g,
		/[\xEC-\xEE]/g, /[\xD2-\xD4]/g, /[\xF2-\xF4]/g, /[\xD9-\xDB]/g, /[\xF9-\xFB]/g
	];
	
	var bychars = [ 'A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u' ];
	
	// search and convert characters
	for( var i=0; i<patterns.length; i++ ) out = out.replace( patterns[i], bychars[i] );
	
	return out;
}

