﻿function addressValid(field){
  var character;
  var counter1;
  var counter2;
  var fieldvalid;
  var invalidcharacters;
  var regexp1;
  var regexp2;
  fieldvalid = true;
  invalidcharacters = '!\"#$%&()*\'+,/:;<=>?[\\]^{|}~£¬';
  regexp1 = new RegExp('(@.*@)|(\\.\\.)|(@\\.)|(^\\.)');
  regexp2 = new RegExp('^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$');
  for (counter1 = 0; counter1 < field.length; ++ counter1){
    character = field.charCodeAt(counter1);
    for (counter2 = 0; counter2 < invalidcharacters.length; ++ counter2){
      if (invalidcharacters.charCodeAt(counter2) == character){
        fieldvalid = false;
        break;
      }
    }
    if (fieldvalid == false){
      break;
    }
  }
  if (fieldvalid == false){
    return fieldvalid;
  }
  fieldvalid = regexp1.test(field) == false && regexp2.test(field) == true;
  return fieldvalid;
}
function changeImage(){
  ++ imageid;
  if (imageid > preloadname.length){
    imageid = 1;
  }
  imageleftmargin = 100;
  imageopacity = 0;
  imagewidth = 400;
  document.getElementById('ppageimage').style.marginLeft = imageleftmargin.toString() + 'px';
  setOpacity('imgpageimage', imageopacity);
  document.getElementById('imgpageimage').src = 'imagebank/' + preloadname[imageid - 1];
  document.getElementById('imgpageimage').style.clip = 'rect(0, 300px, 300px, 0)';
  document.getElementById('divpageimage').style.visibility = 'visible';
  moveInImage();
  changeImageOpacity();
}
function changeImageOpacity(){
  if (imageopacity < 100){
    ++ imageopacity;
    setOpacity('imgpageimage', imageopacity);
    setTimeout(function(){changeImageOpacity();}, 25);
  }
}
function charactersValid(mode, field){
  var character;
  var counter1;
  var counter2;
  var fieldvalid;
  var validcharacters;
  fieldvalid = true;
  if (mode == 'dl'){
    validcharacters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  }
  else if (mode == 'spphd'){
    validcharacters = ' ()+-0123456789';
  }
  for (counter1 = 0; counter1 < field.length; ++ counter1){
    character = field.charCodeAt(counter1);
    for (counter2 = 0; counter2 < validcharacters.length; ++ counter2){
      if (validcharacters.charCodeAt(counter2) == character){
        break;
      }
    }
    if (counter2 == validcharacters.length){
      fieldvalid = false;
      break;
    }
  }
  return fieldvalid;
}
function displayWeather(weatherresponse){
  var displaydate;
  var displaytime;
  var displaytitle;
  var monthname;
  var observationdate;
  var observationdatetime;
  var observationday;
  var observationhours;
  var observationminutes;
  var observationmonth;
  var observationseconds;
  var observationyear;
  var sky;
  var temperature;
  var winddegrees;
  var winddirection;
  var windspeed;
  monthname = new Array('gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre');
  if (weatherresponse != null){
    try{
      observationdatetime = weatherresponse.weatherObservation.datetime;
      observationdate = new Date();
      observationdate.setFullYear(parseInt(observationdatetime.substr(0, 4), 10), parseInt(observationdatetime.substr(5, 2), 10) - 1, parseInt(observationdatetime.substr(8, 2), 10));
      observationdate.setHours(parseInt(observationdatetime.substr(11, 2), 10) + 1, parseInt(observationdatetime.substr(14, 2), 10), parseInt(observationdatetime.substr(17, 2), 10));
      observationday = observationdate.getDate().toString();
      observationmonth = monthname[observationdate.getMonth()];
      observationyear = observationdate.getFullYear().toString();
      observationhours = observationdate.getHours().toString();
      if (observationhours.length == 1){
        observationhours = '0' + observationhours;
      }
      observationminutes = observationdate.getMinutes().toString();
      if (observationminutes.length == 1){
        observationminutes = '0' + observationminutes;
      }
      observationseconds = observationdate.getSeconds().toString();
      if (observationseconds.length == 1){
        observationseconds = '0' + observationseconds;
      }
      if (observationhours >= '00' && observationhours <= '05'){
        displaytitle = 'Questa notte';
      }
      else if (observationhours >= '06' && observationhours <= '12'){
        displaytitle = 'Questa mattina';
      }
      else if (observationhours >= '13' && observationhours <= '17'){
        displaytitle = 'Questo pomeriggio';
      }
      else if (observationhours >= '18' && observationhours <= '21'){
        displaytitle = 'Questa sera';
      }
      else if (observationhours >= '22' && observationhours <= '24'){
        displaytitle = 'Questa notte';
      }
      displaydate = observationday + ' ' + observationmonth + ' ' + observationyear;
      displaytime = observationhours + ':' + observationminutes + ':' + observationseconds;
      switch(weatherresponse.weatherObservation.clouds){
        case 'clear sky':
          sky = 'sereno';
          break;
        case 'few clouds':
          sky = 'poco nuvoloso';
          break;
        case 'scattered clouds':
          sky = 'parzialmente nuvoloso';
          break;
        case 'broken clouds':
          sky = 'nuvoloso';
          break;
        case 'overcast':
          sky = 'coperto';
          break;
        default:
          sky = 'non disponibile';
      }
      temperature = weatherresponse.weatherObservation.temperature;
      if (temperature.length > 1 && temperature.charAt(0) == '0'){
        temperature = temperature.substring(1, temperature.length);
      }
      windspeed = weatherresponse.weatherObservation.windSpeed;
      if (windspeed){
        if (windspeed.length > 1 && windspeed.charAt(0) == '0'){
          windspeed = windspeed.substring(1, windspeed.length);
        }
      }
      winddegrees = weatherresponse.weatherObservation.windDirection;
      if (winddegrees){
        if (winddegrees >= 0 && winddegrees <= 22){
          winddirection = 'nord';
        }
        else if (winddegrees >= 23 && winddegrees <= 67){
          winddirection = 'nord-est';
        }
        else if (winddegrees >= 68 && winddegrees <= 112){
          winddirection = 'est';
        }
        else if (winddegrees >= 113 && winddegrees <= 157){
          winddirection = 'sud-est';
        }
        else if (winddegrees >= 158 && winddegrees <= 202){
          winddirection = 'sud';
        }
        else if (winddegrees >= 203 && winddegrees <= 247){
          winddirection = 'sud-ovest';
        }
        else if (winddegrees >= 248 && winddegrees <= 292){
          winddirection = 'ovest';
        }
        else if (winddegrees >= 293 && winddegrees <= 337){
          winddirection = 'nord-ovest';
        }
        else if (winddegrees >= 338 && winddegrees <= 360){
          winddirection = 'nord';
        }
      }
      document.getElementById('spantitle').innerHTML = displaytitle;
      document.getElementById('spansky').innerHTML = sky;
      document.getElementById('spantemperature').innerHTML = temperature;
      document.getElementById('spanhumidity').innerHTML = weatherresponse.weatherObservation.humidity.toString();
      try{
        document.getElementById('spanpressure').innerHTML = weatherresponse.weatherObservation.hectoPascAltimeter.toString();
      }
      catch(e){
      }
      try{
        document.getElementById('spanpressure').innerHTML = weatherresponse.weatherObservation.seaLevelPressure.toString();
      }
      catch(e){
      }
      if (windspeed && winddirection){
        if (windspeed == '1'){
          document.getElementById('spanwind').innerHTML = windspeed + ' nodo da ' + winddirection;
        }
        else{
          document.getElementById('spanwind').innerHTML = windspeed + ' nodi da ' + winddirection;
        }
      }
      else{
        document.getElementById('spanwind').innerHTML = 'non disponibile';
      }
      document.getElementById('spandate').innerHTML = displaydate;
      document.getElementById('spantime').innerHTML = displaytime;
      document.getElementById('divweather').style.display = 'block';
    }
    catch(e){
    }
  }
}
function encodeQuotes(field){
  field = field.replace(/\"/g, '{').replace(/\'/g, '}');
  return field;
}
function getDate(date){
  var day;
  var displaydate;
  var month;
  var monthname;
  var weekday;
  var weekdayname;
  var year;
  monthname = new Array('gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre');
  weekdayname = new Array('Domenica', 'Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 'Venerdì', 'Sabato');
  day = date.getDate().toString();
  month = monthname[date.getMonth()];
  weekday = weekdayname[date.getDay()];
  year = date.getFullYear().toString();
  displaydate = weekday + ' ' + day + ' ' + month + ' ' + year;
  return displaydate;
}
function getTimeStamp(){
  var currentdate;
  var currenthour;
  var currentmillisecond;
  var currentminute;
  var currentsecond;
  var timestamp;
  currentdate = new Date();
  currenthour = currentdate.getHours().toString();
  if (currenthour.length == 1){
    currenthour = '0' + currenthour;
  }
  currentminute = currentdate.getMinutes().toString();
  if (currentminute.length == 1){
    currentminute = '0' + currentminute;
  }
  currentsecond = currentdate.getSeconds().toString();
  if (currentsecond.length == 1){
    currentsecond = '0' + currentsecond;
  }
  currentmillisecond = currentdate.getMilliseconds().toString();
  if (currentmillisecond.length == 1){
    currentmillisecond = '00' + currentmillisecond;
  }
  else if (currentmillisecond.length == 2){
    currentmillisecond = '0' + currentmillisecond;
  }
  timestamp = currenthour + currentminute + currentsecond + currentmillisecond;
  return timestamp;
}
function getWeather(){
  var weatherscript;
  weatherscript = new jsonScriptRequest(geonamesurl + '&callback=displayWeather');
  weatherscript.buildScriptTag();
  weatherscript.addScriptTag();
}
function highlightImage(mode, element){
  if (mode == 1){
    element.style.filter = 'alpha(opacity = 70)';
    element.style.opacity = '0.70';
    element.style.MozOpacity = '0.70';
  }
  else{
    element.style.filter = 'alpha(opacity = 100)';
    element.style.opacity = '0.99999';
    element.style.MozOpacity = '0.99999';
  }
}
function highlightLink(mode, element){
  if (mode == 1){
    element.style.color = linkhighlightcolor;
  }
  else{
    element.style.color = linkcolor;
  }
}
function highlightPageImage(mode){
  if (mode == 1){
    if (imageopacity > 70){
      -- imageopacity;
      setOpacity('imgpageimage', imageopacity);
      imagetimeoutid = setTimeout(function(){highlightPageImage(1);}, 25);
    }
  }
  else{
    if (imageopacity < 100){
      ++ imageopacity;
      setOpacity('imgpageimage', imageopacity);
      imagetimeoutid = setTimeout(function(){highlightPageImage(0);}, 25);
    }
  }
}
function initMenu(currentmenu){
  if (currentmenu >= 7 && currentmenu <= 10){
    setMenu(1);
  }
  else if (currentmenu >= 11 && currentmenu <= 15){
    setMenu(2);
  }
  else if (currentmenu >= 16 && currentmenu <= 19){
    setMenu(3);
  }
  else if (currentmenu >= 20 && currentmenu <= 22){
    setMenu(4);
  }
  else if (currentmenu >= 23 && currentmenu <= 24){
    setMenu(5);
  }
  else if (currentmenu >= 25 && currentmenu <= 26){
    setMenu(6);
  }
}
function moveInImage(){
  if (imageleftmargin > 0){
    imageleftmargin -= 2;
    setLeftMargin('ppageimage', imageleftmargin);
    setClippingRectangle('ppageimage', 400 - imageleftmargin);
    setTimeout(function(){moveInImage();}, 25);
  }
  else{
    if (preloadname.length > 1){
      setTimeout(function(){moveOutImage();}, 5000);
    }
  }
}
function moveOutImage(){
  if (imagewidth > 0){
    imagewidth -= 10;
    setClippingRectangle('ppageimage', imagewidth);
    setTimeout(function(){moveOutImage();}, 25);
  }
  else{
    setTimeout(function(){changeImage();}, 100);
  }
}
function openEnlargementWindow(caption, enlargementname, enlargementwidth, enlargementheight){
  var captionheight;
  var enlargementwindow;
  var firefoxindex;
  var firefoxname;
  var htmltext;
  var notice;
  var noticeheight;
  var windowheight;
  var windowsettings;
  var windowwidth;
  firefoxname = 'Firefox/';
  firefoxindex = navigator.userAgent.indexOf(firefoxname);
  notice = 'Questa finestra si chiude automaticamente 30 secondi dopo l\'apertura.';
  windowwidth = enlargementwidth + 22;
  document.getElementById('divmeasure').style.maxWidth = windowwidth.toString() + 'px';
  document.getElementById('pmeasure').className = 'size12 margin1111110';
  document.getElementById('spanmeasure').innerHTML = caption;
  captionheight = document.getElementById('tdmeasure').offsetHeight;
  document.getElementById('pmeasure').className = 'size9 margin111166';
  document.getElementById('spanmeasure').innerHTML = notice;
  noticeheight = document.getElementById('tdmeasure').offsetHeight;
  windowheight = enlargementheight + captionheight + noticeheight + 11;
  if (firefoxindex != -1){
    windowheight += 2;
  }
  windowsettings = 'left=10, top=10, width=' + windowwidth.toString() + ', height=' + windowheight.toString() + ', alwaysRaised=1, location=0, menubar=0, resizable=0, scrollbars=0, status=0, toolbar=0';
  htmltext = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n';
  htmltext += '<head>\n';
  htmltext += '<title>' + shortname + ' | Ingrandimento immagine</title>\n';
  htmltext += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n';
  htmltext += '<meta http-equiv="Content-Language" content="it">\n';
  htmltext += '<meta http-equiv="ImageToolBar" content="no">\n';
  htmltext += '<style type="text/css">\n';
  htmltext += 'body.enlargement {background-color: ' + pagebgcolor + '; color: ' + textcolor + ';}\n';
  htmltext += '.center {text-align: center;}\n';
  htmltext += '.fontdefault {font-family: Tahoma, Geneva, Helvetica, sans-serif;}\n';
  htmltext += '.margin0000 {margin: 0;}\n';
  htmltext += '.margin1111110 {margin-left: 11px; margin-right: 11px; margin-top: 11px; margin-bottom: 0;}\n';
  htmltext += '.margin111166 {margin-left: 11px; margin-right: 11px; margin-top: 6px; margin-bottom: 6px;}\n';
  htmltext += '.padding0000 {padding: 0;}\n';
  htmltext += '.size12 {font-size: 12px;}\n';
  htmltext += '.size9 {font-size: 9px;}\n';
  htmltext += '</style>\n';
  htmltext += '</head>\n';
  htmltext += '<body class="enlargement fontdefault margin0000 padding0000" onload="setTimeout(function(){window.close();}, 30000);">\n';
  htmltext += '<table cellpadding="0" cellspacing="0" width="' + windowwidth + '">\n';
  htmltext += '<tr>\n';
  htmltext += '<td class="center">\n';
  htmltext += '<p class="size12 margin1111110"><img alt="' + caption + '" src="' + enlargementname + '" title="" width="' + enlargementwidth.toString() + '" height="' + enlargementheight.toString() + '"></p>\n';
  htmltext += '<p class="size12 margin1111110">' + caption + '</p>\n';
  htmltext += '<p class="size9 margin111166">' + notice + '</p>\n';
  htmltext += '</td>\n';
  htmltext += '</tr>\n';
  htmltext += '</table>\n';
  htmltext += '</body>\n';
  htmltext += '</html>';
  enlargementwindow = window.open('', '', windowsettings);
  enlargementwindow.document.open();
  enlargementwindow.document.write(htmltext);
  enlargementwindow.document.close();
  enlargementwindow.focus();
}
function openWebSiteWindow(url){
  var websitewindow;
  var windowsettings;
  if (screen.width >= 1024 && screen.height >= 768){
    windowsettings = 'left=10, top=10, width=800, height=600, alwaysRaised=1, location=0, menubar=0, resizable=1, scrollbars=1, status=0, toolbar=1';
  }
  else{
    windowsettings = 'left=10, top=10, width=700, height=500, alwaysRaised=1, location=0, menubar=0, resizable=1, scrollbars=1, status=0, toolbar=1';
  }
  websitewindow = window.open(url, 'WebSite', windowsettings);
  websitewindow.focus();
}
function preloadImages(){
  var counter;
  for (counter = 0; counter < preloadname.length; ++ counter){
    preloadimage[counter] = new Image();
    if (preloadname[counter] != ''){
      preloadimage[counter].src = 'imagebank/' + preloadname[counter];
    }
    else{
      preloadimage[counter].src = '';
    }
  }
}
function printPage(title, enlargementname, enlargementwidth, enlargementheight){
  var counter;
  var htmltext;
  var printwindow;
  var windowsettings;
  windowsettings = 'left=10, top=10, width=80, height=60, alwaysRaised=1, location=0, menubar=0, resizable=0, scrollbars=0, status=0, toolbar=0';
  htmltext = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n';
  htmltext += '<html>\n';
  htmltext += '<head>\n';
  htmltext += '<title>Stampa pagina</title>\n';
  htmltext += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n';
  htmltext += '<meta http-equiv="Content-Language" content="it">\n';
  htmltext += '<meta http-equiv="ImageToolBar" content="no">\n';
  htmltext += '<style type="text/css">\n';
  htmltext += 'body.print {background-color: ' + printbgcolor + '; color: ' + printcolor + ';}\n';
  htmltext += '.bold {font-weight: bold;}\n';
  htmltext += '.center {text-align: center;}\n';
  htmltext += '.fontdefault {font-family: Tahoma, Geneva, Helvetica, sans-serif;}\n';
  htmltext += '.margin0000 {margin: 0;}\n';
  htmltext += '.margin00200 {margin-left: 0; margin-right: 0; margin-top: 20px; margin-bottom: 0;}\n';
  htmltext += '.margin0040 {margin-left: 0; margin-right: 0; margin-top: 4px; margin-bottom: 0;}\n';
  htmltext += '.margin0060 {margin-left: 0; margin-right: 0; margin-top: 6px; margin-bottom: 0;}\n';
  htmltext += '.margin25252525 {margin: 25px;}\n';
  htmltext += '.padding0000 {padding: 0;}\n';
  htmltext += '.size11 {font-size: 11pt;}\n';
  htmltext += '.size7 {font-size: 7pt;}\n';
  htmltext += '.size9 {font-size: 9pt;}\n';
  htmltext += '</style>\n';
  htmltext += '</head>\n';
  htmltext += '<body class="print fontdefault size9 margin25252525 padding0000">\n';
  htmltext += '<p class="margin0000 center"><img alt="" src="images/printlogoit.jpg" width="153" height="102"></p>\n';
  htmltext += '<p class="size11 bold margin00200">' + title + '</p>\n';
  for (counter = 0; counter < pagetextparagraph.length; ++ counter){
    if (counter == 0){
      htmltext += '<p class="margin0060">' + pagetextparagraph[counter] + '</p>\n';
    }
    else{
      htmltext += '<p class="margin0040">' + pagetextparagraph[counter] + '</p>\n';
    }
  }
  htmltext += '<p class="margin00200 center"><img alt="" src="images/' + enlargementname + '" width="' + enlargementwidth.toString() + '" height="' + enlargementheight.toString() + '"></p>\n';
  htmltext += '<p class="size7 margin00200">Pagina generata e stampata con codice proprietario ' + authorname.italics() + '<br>Copyright &copy; ' + copyrightyears + ' ' + companycorporatename + ' - Copyright &copy; ' + copyrightyears + ' ' + authorname + ' - Tutti i diritti riservati</p>\n';
  htmltext += '</body>\n';
  htmltext += '</html>';
  printwindow = window.open('', '', windowsettings);
  printwindow.document.open();
  printwindow.document.write(htmltext);
  printwindow.document.close();
  printwindow.focus();
  printwindow.print();
  printwindow.close();
}
function redirect(url){
  window.location.href = url;
}
function setClippingRectangle(element, rightclipping){
  document.getElementById('imgpageimage').style.clip = 'rect(0, ' + rightclipping.toString() + 'px, 300px, 0)';
}
function setFocus(elementid){
  document.forms[0].elements[elementid - 1].focus();
}
function setLeftMargin(element, leftmargin){
  document.getElementById(element).style.marginLeft = leftmargin.toString() + 'px';
}
function setMenu(elementid){
  if (elementid != menuid){
    if (menuid != 0){
      document.getElementById('divsubmenu' + menuid.toString()).style.display = 'none';
    }
    document.getElementById('divsubmenu' + elementid.toString()).style.display = 'block';
    menuid = elementid;
  }
  else{
    document.getElementById('divsubmenu' + menuid.toString()).style.display = 'none';
    menuid = 0;
  }
}
function setOpacity(element, opacity){
  if (opacity == 100){
    document.getElementById(element).style.filter = 'alpha(opacity = 100)';
    document.getElementById(element).style.opacity = '0.99999';
    document.getElementById(element).style.MozOpacity = '0.99999';
  }
  else{
    document.getElementById(element).style.filter = 'alpha(opacity = ' + opacity.toString() + ')';
    document.getElementById(element).style.opacity = (opacity / 100).toString();
    document.getElementById(element).style.MozOpacity = (opacity / 100).toString();
  }
}
function shortenField(field, maxlength){
  var shortenedfield;
  if (field.length <= maxlength){
    shortenedfield = field;
  }
  else{
    shortenedfield = field.substr(0, maxlength - 3);
    if (shortenedfield.charAt(shortenedfield.length - 1) == ' '){
      shortenedfield = shortenedfield.substr(0, shortenedfield.length - 1);
    }
    shortenedfield += '...';
  }
  return shortenedfield;
}
function showValidation(mode, message){
  if (mode == 1){
    document.getElementById('spanvalidation').innerHTML = message;
    document.getElementById('divvalidation').style.visibility = 'visible';
  }
  else{
    document.getElementById('divvalidation').style.visibility = 'hidden';
  }
}
function trim(field){
  var character;
  character = field.charCodeAt(0);
  while (character == 10 || character == 13 || character == 32){
    field = field.substring(1, field.length);
    character = field.charCodeAt(0);
  }
  character = field.charCodeAt(field.length - 1);
  while (character == 10 || character == 13 || character == 32){
    field = field.substring(0, field.length - 1);
    character = field.charCodeAt(field.length - 1);
  }
  field = field.replace(/&/g, '').replace(/</g, '').replace(/>/g, '').replace(/\\/g, '').replace(/{/g, '').replace(/}/g, '').replace(/~/g, '');
  return field;
}