function trim( str )
{
	return( str.replace(/^\s+/g, '').replace(/\s+$/g, '') );
}

function makeArray( obj )
{
	var tmpArray = obj;
	
	if( obj == void(0) )
		return( void(0) );
	
	if( !obj.length || (obj.type != void(0) && obj.type.indexOf("select") != -1 && obj.selectedIndex != void(0)) )
	{
		tmpArray = new Array();
		tmpArray[0] = obj;
	}
	
	return( tmpArray );
}

function openWindow( strPage, strName, width, height, strFeature )
{
	var newWin;
	
	if( arguments.length < 3 )
	{
		width = 445;
		height = 570;
	}
	
	if( strFeature == void(0) || strFeature == null )
		strFeature = 'scrollbars=yes,resizable=yes,status=no,location=no,toolbar=no,hotkeys=no';
	
	var x = Math.round((screen.availWidth - width) / 2);
	var y = Math.round((screen.availHeight - height) / 2);
	
	if( strFeature.indexOf('width=') == -1 )
		strFeature += ',width=' + width;
	if( strFeature.indexOf('height=') == -1 )
		strFeature += ',height=' + height;
	if( strFeature.indexOf('left=') == -1 )
		strFeature += ',left=' + x;
	if( strFeature.indexOf('top=') == -1 )
		strFeature += ',top=' + y;

	newWin = self.open(
			strPage,
			strName,
			//'' );
			strFeature );
	
	if( !newWin.closed )
		newWin.focus();
	
	newWin.myWidth	= width;
	newWin.myHeight	= height;
	
//	if( newWin.resize )
//		newWin.resize();
	
	return newWin;
}

function checkSelEl( frm, elName, msgNoSel, msgNoEl )
{
	if( frm[elName] ) // se c'e' almeno un elemento
	{
		el = makeArray( frm[elName] );
		
		for( var i = 0 ; i < el.length ; i++ )
			if( el[i].checked )
				return true;
	}
	else if( msgNoEl != null && msgNoEl != void(0) ) // se non c'e' nessun elemento ed e' previsto un messaggio d'errore
	{
		alert( msgNoEl );
	}
	
	if( frm[elName] && msgNoSel != null && msgNoSel != void(0) ) // se c'e' almeno un elemento ed e' previsto un messaggio d'errore
		alert( msgNoSel );
	
	return false;
}

function getRadioValue( radio )
{
	var el = makeArray( radio );
	
	for( var i = 0 ; i < el.length ; i++ )
		if( el[i].checked )
			return el[i].value;
	
	return null;
}

function StringToNumber( val )
{
	val = new String(val).replace(',', '.');
	
	return( val );
}

function StringToCurrency( val, strDecimalSeparator, strDecimalPrecision )
{
	val = StringToNumber( val );
	
	val = new Number(val).toFixed( strDecimalPrecision );
	
	val = new String(val).replace('.', strDecimalSeparator);
	
	return( val );
}

function toWindowsFilename( val, str )
{
	return( val.replace(/\/|\\|;|:|\*|\?|"|<|>|\|/g, str) );
}

function timeCheck( obj )
{
	var val = trim(obj.value);
	var vct = val.split(/-|\s|_|,|:|;|\.|\*|\\|\//g);
	var blnErr = false;
	var vctRis = new Array();
	
	for( var i = 0 ; i < 3 && val != '' ; i++ )
	{
		if( vct[i] == null || vct[i] == void(0) )
		{
			vct[i] = '00';
		}
		else if( isNaN(vct[i]) || vct[i] < 0 || (i == 0 && vct[i] > 23) || (i > 0 && vct[i] > 59) )
		{
			blnErr = true;
			break;
		}
		else if( (vct[i] + '').length < 2 )
		{
			vct[i] = (vct[i] < 10) ? '0' + vct[i] : '' + vct[i];
		}
		
		vctRis[i] = vct[i];
	}
	
	if( blnErr )
	{
		alert('Formato dell\'orario non corretto');
		obj.focus();
		return false;
	}
	
	obj.value = vctRis.join(':');
	
	return true;
}

function checkEMail( val )
{
	//alert('qui');
	return val.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1;
}

function checkEmail2( obj )
{		
	//alert('qui');
	val = obj.value;
	if( val == '' )
		return true;

	if (val.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
	{
		return(true);
	}
	else
	{
		alert('La sintassi dell\'indirizzo e-mail\n non e\' corretta!');
		return(false);
	}
}
