/**
 * Cookies
 */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/**
 * Opacity
 */
function setOpacity(oggetto, valore) {
	oggetto.style.opacity = value/10;
	oggetto.style.filter = 'alpha(opacity=' + value*10 + ')';
}

/**
 * DHTML date validation script.
 */
function isInteger(s){
	var i;
	for (i = 0; i < s.length; i++){
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag){
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

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 DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
	}
	return this
}

function isDate(dtStr, silent){
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		if (! silent) alert("Il formato della data deve essere dd/mm/yyyy");
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		if (! silent) alert("Inserire un mese valido nella data");
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		if (! silent) alert("Inserire un giorno valido nella data");
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		if (! silent) alert("Inserire l'anno di 4 cifre"); // year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		if (! silent) alert("Inserire una data valida");
		return false
	}
return true
}

/**
 * Conversione data da US a IT
 */
function dateUS(dataIT){
  var primoSep;
  var secondoSep;
  var strDataIT = new String(dataIT);
  var mese;
  var giorno;
  var anno;
  var txtDataUS;
  var txtData;
  primoSep = strDataIT.indexOf("/");
  secondoSep = strDataIT.lastIndexOf("/");
  if (secondoSep > primoSep) {
    if (primoSep > 0) {
      giorno = strDataIT.substr(0,primoSep);
      if (secondoSep > 0) {
        anno = strDataIT.substr(secondoSep + 1, strDataIT.length - secondoSep - 1);
        mese = strDataIT.substr(primoSep + 1, secondoSep - primoSep - 1);
      }
    }
  }
  txtDataUS = mese + "/" + giorno + "/" + anno;
  return txtDataUS;
}

/**
 * Rimuove gli spazi all'inizio e alla fine
 */
function trim(stringa)
{
    while (stringa.substring(0,1) == ' ')
    {
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' ')
    {
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}

/**
 * Controlla se una stringa contiene un valore numerico
 */
function isNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}


/**
 * Controlla il carattere digitato e lo filtra se non è contenuto nell'elenco
 */
// Come usare la funzione...
// <!-- <input type="text" name="textfield" onKeyPress="return keyRestrict(event,'1234567890')"> -->
// <!-- <input type="text" name="textfield" onKeyPress="return keyRestrict(event,'abcdefghijklmnopqrstuvwxyz ')"> -->
// <!-- <input type="text" name="textfield" onKeyPress="return keyRestrict(event,'abcdefghijklmnopqrstuvwxyz '+String.fromCharCode(241))"> -->

function getKeyCode(e)
{
    if (window.event)
        return window.event.keyCode;
    else if (e)
            return e.which;
         else
            return null;
}

function keyRestrict(e, validchars) {
    var key='', keychar='';
    key = getKeyCode(e);
    if (key == null)
        return true;
    keychar = String.fromCharCode(key);
    keychar = keychar.toLowerCase();
    validchars = validchars.toLowerCase();
    if (validchars.indexOf(keychar) != -1)
        return true;
    if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
        return true;
    return false;
}

/**
 * Aggiunge il metodo format agli oggetti number
 */
// *******************************************
// NUMBER OBJECT ADDITIONAL PROTOTYPES
// VB-like string replicator
// *******************************************
String.prototype.times = function(n)
{
 var s = '';
 for (var i = 0; i < n; i++)
  s += this;

 return s;
}

// Zero-Previous
Number.prototype.zp = function(n) { return this.toString().zp(n); }

// Zero-Trailing
String.prototype.zt =
  function(n) { return this + '0'.times(n - this.length); }

// Zero-Leading
String.prototype.zl =
  function(n) { return '0'.times(n - this.length) + this; }

// fractional part of a number
Number.prototype.fractional =
  function() { return parseFloat(this) - parseInt(this); }

// decimal digits arithmetic rounding
Number.prototype.round = function(n)
{
return Math.round(this * Math.pow(10, n)) / Math.pow(10, n);
}

// format a number with n decimal digits
Number.prototype.format = function(ni, nd, ms)
{
 var decSep = ',';
 var milSep = '.';
 // round the fractional part to n digits, skip the '0.' and zero trail
 var f = this.fractional().round(nd).toString().substr(2).zt(nd);
 var i = new Number(parseInt(this).toString().zl(ni));
 //arrotondando separatamente la prate frazionaria bisogna aggiungere l'eventuale parte intera risultate dall'arrotondamento
 i = i + parseInt(this.fractional().round(nd));
 var iStr = i.toString();
 //inserimento separatori delle migliaia
 if ((ms) && (iStr.length > 0)) {
  var contaMilSep = parseInt((iStr.length - 1) / 3);
  var j;
  for (j=1;j<=contaMilSep;j++) {
   iStr = iStr.substr(0, iStr.length - ((3 * j) + (j - 1))) + milSep + iStr.substr(iStr.length - ((3 * j) + (j - 1)));
  }
 }

 // integer part + dot + fractional part, skipping the '0.'
 if (f.length > 0)
  return iStr + decSep + f;
 else
  return iStr;
}

String.prototype.comma2dot = function() { return this.replace(/,/g,"."); }
String.prototype.commaRemove = function() { return this.replace(/,/g,""); }
String.prototype.dot2comma = function() { return this.replace(/\./g,"."); }
String.prototype.dotRemove = function() { return this.replace(/\./g,""); }

/**
 * OnLoad
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function Apri(url) {
  location.href = url;
}

function ApriNew(url) {
  var newWindow = window.open(url, '_blank');
  newWindow.focus();
  return false;
}


