<!--
function isEmpty(itm)
{
	if(itm==null)
	{
		return false;	
	}
	val = trim(itm);
	if (val.length <=0)
	{
		return true;
	}
	return false;
}

function isEmptyFckEditor(element_ID)
{
	stringEditor = str_replace('&nbsp;', '', FCKeditorAPI.GetInstance(element_ID).GetXHTML());
	if (stringEditor.replace(/(<([^>]+)>)/ig,'')=='') 
	{
		return true;
	}
	return false;	
}

function checkFile(itm, msg)
{
	val = document.getElementById(itm);
	if(val == null)
	{
		return true;	
	}
	if(val.value.length <= 0)
	{
		if(document.getElementById(itm+"_old") == null)	
		{
			alert(msg + "s");
			return false;
		}
		else
		{
			if(document.getElementById(itm+"_old").value <= 0)	
			{
				alert(msg + "sss");
				return false;	
			}
			else
			{
				return true;
			}
		}
	}
	else
	{
		return true;	
	}
}

function isAdvancedTextAreaEmpty(textareaID, msg)
{
	stringEditor = str_replace('&nbsp;', '', FCKeditorAPI.GetInstance(textareaID).GetXHTML());
	if (stringEditor.replace(/(<([^>]+)>)/ig,'')=='') 
	{
		alert(msg);
		return true;
	}
	return false;
}

function isInt(itm, msg)
{
	if(document.getElementById(itm)==null)
	{
		return true;	
	}
	if(document.getElementById(itm).value.length <= 0)
	{
		return true;	
	}
	if(!eregCheck(document.getElementById(itm).value, msg, '/^([0-9]{1,})$/'))
	{
		document.getElementById(itm).select();
		return false;
	}
	return true;
}

function isInteger(val)
{
	if(!eregCheck(val, "", '/^([0-9]{1,})$/'))
	{
		return false;
	}	
	return true;
}

function isEmail(itm) 
{
	if(itm==null)
	{
		return true;	
	}
	return eregCheck(itm, '', '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9\-]{0,})+(\.{0,})+([a-zA-Z0-9]{2,4})+$/');
}

//function isFloat(itm, msg)
//{
//	if(document.getElementById(itm)==null)
//	{
//		return true;	
//	}
//	if(document.getElementById(itm).value.length<=0)
//	{
//		return true;	
//	}
//	val = str_replace(_FLOAT_DECIMAL_SEP_USER, ".",str_replace(_FLOAT_THOUSAND_SEP_USER, '', document.getElementById(itm).value));
//	if(val.split('.').length > 2)
//	{
//		alert(msg);
//		document.getElementById(itm).select();
//		return false;	
//	}
//	if(isNaN(parseFloat(val)))
//	{
//		alert(msg);
//		document.getElementById(itm).select();
//		return false;
//	}
//	return true;
//}

function isFloat(itm, msg, allowNegative)
{
	if(document.getElementById(itm)==null)
	{
		return true;	
	}
	if(document.getElementById(itm).value.length<=0)
	{
		return true;	
	}
	if(allowNegative == null)
	{
		allowNegative= true;
	}
	ret = isFloatUtente(document.getElementById(itm).value, allowNegative);
	if(!ret)
	{
		alert(msg);
		document.getElementById(itm).select();
		return false;	
	}
	return true;
}

//controlla se il valore passato è float
function isFloatUtente(val, allowNegative)
{
	val = trim(val);
	if(val.length == 0)
	{
		return false;	
	}
	decimal = 0;
	allowedChars = "0123456789" + _FLOAT_DECIMAL_SEP_USER;
	if(allowNegative)
	{
		allowedChars +="-";
	}
  for( i=0; i<val.length; i++ ) {
    ch = val.charAt(i);
    if(ch == _FLOAT_DECIMAL_SEP_USER)
    {
    	decimal++;	
    }
    //non può cominciare con una virgola
    if(i==0 && decimal > 0)
    {
    	return false;	
    }
    //non può finire con una virgola
    if(i==(val.length-1) && (ch == _FLOAT_DECIMAL_SEP_USER))
    {
    	return false;	
    }
    if(i>0 && ch == '-')
    {
    	return false;	
    }
    if ( allowedChars.indexOf(ch) == -1 ) 
    {
      return false;
    }
  }
  if(decimal < 2)
  {
  	return true;
  }
  else
  {
  	return false;
  }
}

//controlla se il valore è una percentuale. Es 12% 12,69% -10,87%
//è fondamentale che l'ultimo carattere sia '%' se no torna false a priori!
function isPercent(val)
{
	val = trim(val);
	if(val.length == 0)
	{
		return false;	
	}
	if(val.charAt(val.length-1) == '%')
	{
		val = val.substring(0,(val.length-1));
		if(!isFloatUtente(val, true))
		{
			return false;	
		}
		//è un numero ok, ma è compreso tra -100 e 100?
//		val = userFloatToJsFloat(val);
//		if(val < -100 || val > 100)
//		{
//			return false;	
//		}
	}
	else
	{
		return false;	
	}
	return true;
}

//Formattazione campi decimali con la virgola
//function formatDecimal(e)
//{
//	if (e.target) 
//		targ = e.target;
//	else if (e.srcElement) 
//		targ = e.srcElement;
//	if (targ.nodeType == 3) // defeat Safari bug
//		targ = targ.parentNode;
//	if(getKeyCode(e) == 110 || getKeyCode(e) == 190)
//	{
//		targ.value = targ.value.substring(0,targ.value.length-1) + ",";
//	}
//}

function isMaxOf(val, maxValue)
{
	if(val > maxValue)
	{
		return false;
	}
	return true;
}

function isMinOf(itm, minValue)
{
	if(val < minValue)
	{
		return false;
	}
	return true;
}

function userFloatToDb(userFloatID, destElementID)
{
	if(_FLOAT_DECIMAL_SEP_USER != '.')
	{
		document.getElementById(destElementID).value = str_replace(",", ".", document.getElementById(userFloatID).value);
	}
	else
	{
		document.getElementById(destElementID).value = document.getElementById(userFloatID).value;
	}
	
}

function formatDecimal(e)
{
	if(_FLOAT_DECIMAL_SEP_USER == '.')
	{
		return;	
	}
	if (e.target) 
	{
		targ = e.target;
		key = e.which;
	}
	else if (e.srcElement) 
	{
		key = e.keyCode;
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) // defeat Safari bug
	{
		targ = targ.parentNode;
		key = e.which;
	}
	if(key == 110 || key == 190)
	{
		targ.value = targ.value.substring(0, targ.value.length-1) + _FLOAT_DECIMAL_SEP_USER;
	}
}

function dateDiffIta (start, end, interval, rounding ) 
{
	start = start.split(/[-/]/);
	end = end.split(/[-/]/);
	return dateDiff( start[2]+"-"+start[1]+"-"+start[0], end[2]+"-"+end[1]+"-"+end[0], interval, rounding );
}

//si aspetta le date in formato iso!
function dateDiff( start, end, interval, rounding ) {
	//metto la data da iso a inglese come la vuole il javascript (di merda!)
	oraStart = "";
	oraEnd = "";
	if(start.length > 10)
	{
		oraStart = start.substring(11);
		start = start.substring(0,10);
	}
	if(end.length > 10)
	{
		oraEnd = end.substring(11);
		end = end.substring(0,10);
	}
	
	start = start.split(/[-/]/);
	start = start[1]+"/"+start[2]+"/"+start[0];
	
	end = end.split(/[-/]/);
	end = end[1]+"/"+end[2]+"/"+end[0];
	
	if(oraStart != "")
	{
		start += " " + oraStart;	
	}
	if(oraEnd != "")
	{
		end += " " + oraEnd;	
	}
	iOut = 0;
	bufferA = Date.parse( start ) ;
  bufferB = Date.parse( end ) ;
  // check that the start parameter is a valid Date. 
  if ( isNaN (bufferA) || isNaN (bufferB) )
  {
    alert('Errore DateDiff 1!!!');
    return null;
	}
	    // check that an interval parameter was not numeric. 
    if ( interval.charAt == 'undefined' ) {
        // the user specified an incorrect interval, handle the error. 
        alert('Intervallo di tempo non specificato') ;
        return null ;
    }
    
    number = bufferB-bufferA ;
    
    // what kind of add to do? 
    switch (interval.charAt(0))
    {
				case 'y':case 'Y':
					iOut = parseInt(number / 31536000000) ;
		            if(rounding) iOut += parseInt((number % 31536000000)/15768000365) ;
					break;
        case 'd': case 'D': 
            iOut = parseInt(number / 86400000) ;
            if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
            break ;
        case 'h': case 'H':
            iOut = parseInt(number / 3600000 ) ;
            if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
            break ;
        case 'm': case 'M':
            iOut = parseInt(number / 60000 ) ;
            if(rounding) iOut += parseInt((number % 60000)/30001) ;
            break ;
        case 's': case 'S':
            iOut = parseInt(number / 1000 ) ;
            if(rounding) iOut += parseInt((number % 1000)/501) ;
            break ;
        default:
        // If we get to here then the interval parameter
        // didn't meet the d,h,m,s criteria.  Handle
        // the error. 		
        alert('Errore DateDiff 2!!!') ;
        return null ;
    }
    return iOut ;
}

//converte in iso la data passato con il formato format
//NB: se passi un datetime ritorna solo la data formattata in ISO, quindi il valore time viene perso!
function dateToIso(val, format)
{
	if(val.length==0)
	{
		return '';	
	}
	if(val.indexOf(' ') != -1)
	{
		val = trim(val.substring(0,val.indexOf(' ')));
	}
	if(format.indexOf(' ') != -1)
	{
		format = trim(format.substring(0,format.indexOf(' ')));
	}
	format = format.split(/[-/]/);
	val = val.split(/[-/]/);
	ret = Array();
	for(i=0;i<format.length;i++)
	{
		if(format[i].toLowerCase() == 'd' )
		{
			ret['d'] = val[i];
		}
		if(format[i].toLowerCase() == 'm' )
		{
			ret['m'] = val[i];
		}
		if(format[i].toLowerCase() == 'y' )
		{
			ret['y'] = val[i];
		}
	}
	return ret['y']+"-"+ret['m']+"-"+ret['d'];
}

function isDate(itm, format, msg)
{
	if(document.getElementById(itm)==null)
	{
		return true;	
	}
	if(document.getElementById(itm).value.length <= 0)
	{
		return true;
	}
	//è una data e basta o un DateTime?
	val = trim(document.getElementById(itm).value);
	if(format.indexOf(' ') != -1)
	{
		if (val.indexOf(' ') != -1)
		{
			vdata = trim(val.substring(0, val.indexOf(' ')));	
			vtime = trim(val.substring(val.indexOf(' ')));
		}
		else
		{
			vdata = trim(val);
			vtime = '';
		}
		format = trim(format.substring(0,format.indexOf(' ')))
	}
	else
	{
		vdata = trim(val);
		vtime = '';
	}
	vdata = vdata.split(/[-/]/);
	format = format.split(/[-/]/);
	if(format.length != 3 || vdata.length != 3)
	{
		alert(msg);
		return false;	
	}
	daysInMonth = new Array();
	daysInMonth[1] = 31;
	daysInMonth[2] = 29;
	daysInMonth[3] = 31;
	daysInMonth[4] = 30;
	daysInMonth[5] = 31;
	daysInMonth[6] = 30;
	daysInMonth[7] = 31;
	daysInMonth[8] = 31;
	daysInMonth[9] = 30;
	daysInMonth[10] = 31;
	daysInMonth[11] = 30;
	daysInMonth[12] = 31;
	
	
	v = new Array();
	for(i=0;i<format.length;i++)
	{
		if(!eregCheck(vdata[i], msg, '/^([0-9]{1,})$/'))
		{
			return false;	
		}
		if (vdata[i].substring(0,1)=="0")
		{
			vdata[i] = vdata[i].substring(1);
		}
		switch(format[i].toLowerCase())
		{
			case 'd':
				v['d'] = parseInt(vdata[i]);
				break;
			case 'm':
				v['m'] = parseInt(vdata[i]);
				break;
			case 'y':
				v['Y'] = parseInt(vdata[i]);
				break;
		}
	}
	if (v['m']<1 || v['m']>12)
	{
		alert(msg);
		document.getElementById(itm).select();
		return false;
	}
	if (v['d']<1 || v['d']>31 || (v['m']==2 && v['d']>daysInFebruary(v['Y'])) || v['d'] > daysInMonth[v['m']])
	{
		alert(msg);
		document.getElementById(itm).select();
		return false
	}
	if (v['Y']==0 || v['Y']<1000){
		alert(msg);
		document.getElementById(itm).select();
		return false;
	}
	//se ci fosse anche l'orario!
	if(vtime != '')
	{
		vtime = vtime.split(/[.:]/);
		for(i=0;i<vtime.length;i++)
		{
			if(!eregCheck(vtime[i], msg, '/^([0-9]{1,})$/'))
			{
				return false;	
			}
			switch(i)
			{
				case 0://ora
					if(parseInt(vtime[i]) > 23 || parseInt(vtime[i]) < 0)
					{
						alert(msg);
						document.getElementById(itm).select();
						return false;	
					}
					break;
				case 1:// minuti e secondi
				case 2:
					if(parseInt(vtime[i]) > 59 || parseInt(vtime[i]) < 0)
					{
						alert(msg);
						document.getElementById(itm).select();
						return false;	
					}
					break;
			}
		}
	}
	return true;
}

//formatta la data sull'onblur
//tipo: date datetime
function formatDateField(fieldID, tipo)
{
	val = document.getElementById(fieldID);
	val.value = trim(val.value);
	//se non e' richiesto e non e' stato inserito nulla esco!
	if(val.value.length == 0)
	{
		return;
	}
	if(tipo == "date")
	{
		date = val.value;
		time="";
	}
	else
	{
		if(val.value.lastIndexOf(" ") != -1)
		{
			date = trim(val.value.substring(0,val.value.lastIndexOf(" ")));
			time = trim(val.value.substring(val.value.lastIndexOf(" ")));
		}
		else
		{
			date = val.value;
			time = "";
		}
		
	}
	if(date.length != 6 && date.length != 10)
	{
		val.select();
		return;
	}
	if (date.indexOf("/") == -1)
	{
		year = date.substring(4);
	}
	else
	{
		year = date.substring(date.lastIndexOf("/")+1);
	}
	cur_year=new Date(); 
	cur_year = '' + cur_year.getFullYear() +'';
	if(cur_year.length>2)
	{
		cur_year = parseInt(cur_year.substring(2));
	}
	if(year.length == 2)
	{
		if(year >= (cur_year+30))
		{
			year = "19" + year;
			//document.getElementById(itm).value.document.getElementById(itm).valueue = document.getElementById(itm).value.document.getElementById(itm).valueue.substring(0,4) + "19" + document.getElementById(itm).value.document.getElementById(itm).valueue.substring(4);
		}
		else
		{
			year = "20" + year;
			//document.getElementById(itm).value.document.getElementById(itm).valueue = document.getElementById(itm).value.document.getElementById(itm).valueue.substring(0,4) + "20" + document.getElementById(itm).value.document.getElementById(itm).valueue.substring(4);
		}
	}
	if(time != "")
	{
		if(time.length==4)
		{
			time = time.substring(0,2)+":"+time.substring(2);
		}
	}
	if (date.indexOf("/") == -1)
	{
		val.value = date.substring(0,2) + "/" + date.substring(2,4) + "/" + year;
	}
	else
	{
		val.value = date.substring(0,date.lastIndexOf("/"))+"/"+year;
	}
	if(time != "")
	{
		val.value += " " + time;
	}
}

function getKeycode(e)
{
	if (e.target) 
	{
		key = e.which;
	}
	else if (e.srcElement) 
	{
		key = e.keyCode;
	}
	return key;
}

function eregCheck(val, msg, filter)
{
	if(typeof filter == 'string')
	{
		filter = eval(filter);
	}
	if(filter.test(val)) 
	{
		return true;
	}
	else 
	{
		if(msg != '')
			alert(msg);	
		return false;
	}	
}

function textCounter(field,cntfield,maxlimit)
{
	if (field.value.length > maxlimit)
	{
		field.value = field.value.substring(0, maxlimit);
	}
	else
	{
		cntfield.value = maxlimit - field.value.length;
	}
}

//-------------------------------------- funzioni di uso comune
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function str_replace(what, by, val)
{
	if (val == null || trim(val) == '')
	{
		return val;
	}
	return val.replace(eval('/\\' + what + '/ig'), by);
}

function trim(inputString) 
{
   if (typeof inputString != "string") { return inputString; }
   if(inputString == ""){ return ""; }
   retValue = inputString;
   ch = retValue.substring(0, 1);
   while (ch == " ") 
   {
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") 
   { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) 
   {
   	  retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue;
}

function calToIsoDate(calElementID, destElementID, sm_date_format, isDateTime)//
{
	document.getElementById(destElementID).value = '';
	cc = new Calendar(0);
	ret = cc.parseDate(document.getElementById(calElementID).value, sm_date_format);
	
	if(!ret)
	{
		return false;
	}
		function fmtMinorTen(val)
		{
			if (val < 10)
				return '0' + val;
			else
				return val;
		}
	document.getElementById(destElementID).value = fmtMinorTen(ret.getFullYear()) + '-' + fmtMinorTen(ret.getMonth()+1) + "-" + fmtMinorTen(ret.getDate());
	if(isDateTime)
	{
		document.getElementById(destElementID).value += ' ' + fmtMinorTen(ret.getHours()) + ':' + fmtMinorTen(ret.getMinutes()) + ':' + fmtMinorTen(ret.getSeconds());
	}
	return true;
}

function isNumeric( val ) 
{
  decimalCount = 0;
  negativeCount = 0;
  Chars = "-0123456789.";
  for (i = 0; i < val.length; i++) 
	{ 
    if (Chars.indexOf(val.charAt(i)) == -1) 
    { 
    	return false; 
    }
    if(val.charAt(i) == ".")
    {
    	decimalCount++;
    }
    if(val.charAt(i) == "-")
    {
    	negativeCount++;
    }
	}
  if (decimalCount > 1)
  {
  	return false;	
  }
  if (negativeCount > 1)
  {
  	return false;	
  }
  return true;
}

function formatNumber(value, decimalpositions)
{
	i = value * Math.pow(10,decimalpositions);
  i = Math.round(i);
  return i / Math.pow(10,decimalpositions);
}

function resizeWindowToFitImage()
{
	setTimeout("_resizeWindowToFitImage()", 800);
}

function _resizeWindowToFitImage()
{
	NS = (navigator.appName=="Netscape")?true:false;
	iWidth = (NS)?window.innerWidth:document.body.clientWidth;
	iHeight = (NS)?window.innerHeight:document.body.clientHeight;
	iWidth = document.images[0].width - iWidth;
	iHeight = document.images[0].height - iHeight;
	window.resizeBy(iWidth, iHeight);
	self.focus();	
}

function CheckIsIE()
{
if (navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER') { return true;}
else { return false; }
}

// -------------------------------------------------- FUNZIONI CHIAMATE DA AJAX
function setElementValueFromAjax(elID, val)
{
	//alert(elID + " " + document.getElementById(elID).type);
	itm = document.getElementById(elID);
	if(itm == null)
	 return;
	switch(itm.type)
	{
		case 'hidden':
		case 'text':
			itm.value = val;
			break;
		case 'select-one':
			for(k=0;k<itm.options.length;k++)
			{
				if(itm.options[k].value == val)
				{
					itm.selectedIndex = k;
				}
			}
			break;
		case 'textarea':
			itm.innerHTML = val;
			break;
	}
}

function hideFrmElement(ItmName)
{
	_chgElementVisibility(ItmName, 'none');
}

function showFrmElement(ItmName)
{
	_chgElementVisibility(ItmName, '');
}

function _chgElementVisibility(itmName, stato)
{
	if(document.getElementById(itmName) != null)
	{
		document.getElementById(itmName).style.display = stato;
	}
	else
	{
		alert("NON ESISTE L'ELEMENTO: 'tr_" + itmName + "' (CHIAMATA A _chgElementVisibility)!!");	
	}
}

function hideDiv(divID)
{
	_chgDivVisibility(divID, 'off');
}

function showDiv(divID)
{
	_chgDivVisibility(divID, 'on');
}
function _chgDivVisibility(divID, stato)
{
	if(stato == 'on')
	{
		document.getElementById(divID).style.display = 'block';
	}
	else
	{
		document.getElementById(divID).style.display = 'none';
	}
}
//-->