var new_window

function openWindow(url,name,width,height){
    var options = "width=" + width + ",height=" + height + "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no";

    new_window = window.open(url, name, options);
    window.self.name = "main";
    new_window.focus();
}



function check_date(field,vuota){
	var checkstr = "0123456789";
	var DateField = field;
	var Datevalue = "";
	var DateTemp = "";
	var separatore = "/";
	var ToDay= new Date();
	var day=ToDay.getDate()+'';
	if (day.length==1) { day= '0' + day;}
	var month=(ToDay.getMonth()+1)+'';
	if (month.length==1) { month= '0' + month;}
	var year=ToDay.getFullYear();
	var leap = 0;
	var err = 0;
	var i;

   err = 0;
   DateValue = DateField.value;
   DateValue = DateValue.replace(".", "/");
   DateValue = DateValue.replace(".", "/");
   if (DateValue.length == 0)
      {
       if (vuota == 'N') {DateValue=day + '/' + month +'/' + year;}
      }
     else
      {
       a=DateValue.split('/');
       switch(a.length) {
             case 1:
                  if (a[0].length == 1) { a[0] = '0' + a[0];}
                  DateValue=a[0] + '/' + month +'/' + year;
                  break;
             case 2:
                  if (a[0].length == 1) { a[0] = '0' + a[0];}
                  if (a[1].length == 1) { a[1] = '0' + a[1];}
                  DateValue=a[0] + '/' + a[1] +'/' + year;
                  break;
             case 3:
                  if (a[0].length == 1) { a[0] = '0' + a[0];}
                  if (a[1].length == 1) { a[1] = '0' + a[1];}
                  if (a[2].length == 1) { a[2] = '0' + a[2];}
                  DateValue=a[0] + '/' + a[1] +'/' + a[2];
                  break;
                        }
       }
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
      if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
         DateTemp = DateTemp + DateValue.substr(i,1);
      }
   }
   DateValue = DateTemp;

   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */

   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
/*   if (DateValue.length != 10) { alert("Lungh data:"+DateValue.length);
      err = 19;}
*/
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }

   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }

   /* Validation of day*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }

   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }

   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }

   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; separatore = "";
   }

   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      DateField.value = day + separatore + month + separatore + year;
   }

   /* Error-message if err != 0 */
   else {
      alert("La data è errata! il formato corretto è gg/mm/aa.");
	  alert("Errore N."+err);
      DateField.select();
      DateField.focus();
      return false;
   }
}

function ConfermaCancPermesso(ID) {
    var msg="Attenzione : confermate la cancellazione del permesso ?";
    if (confirm(msg))
        location.href="MyTime.asp?Tipo=permesso&Op=canc&ID=" + ID
    }

function ConfermaCancOmessa(ID) {
    var msg="Attenzione : confermate la cancellazione ?";
    if (confirm(msg))
        location.href="MyTime.asp?Tipo=omessa&Op=canc&ID=" + ID
    }

function VisTimbrature(Tipo){
    if (Tipo=='permesso') {
        if (typeof(document.FormPermesso)!='undefined') {
           CID=document.FormPermesso.Rich_CID.value;
           }
        else
          { CID='';}
        }
    else
 //       if (typeof(document.MiaForm2)!='undefined') {
 //          CID=document.MiaForm2.Rich_CID.value;
 //          }
 //       else
 //         { CID='';}
    new_window = window.open('TimbraVis.asp?CIDr=' + CID, 'TimbraVis');
    new_window.focus();
    }

function VisRimanenze(Tipo){
    if (Tipo=='permesso') {
        if (typeof(document.FormPermesso)!='undefined') {
           CID=document.FormPermesso.Rich_CID.value;
           }
        else
          { CID='';}
        }
    else
  //      if (typeof(document.MiaForm2)!='undefined') {
  //         CID=document.MiaForm2.Rich_CID.value;
  //         }
  //      else
  //        { CID='';}
    new_window = window.open('RimanenzeVis.asp?CIDr=' + CID, 'TimbraVis');
    new_window.focus();
    }

// Controlla che la data inserita si trovi nel range corretto
function dataInRange(dataLimiteMin, dataLimiteMax, dataInserita)
{
  var dLimiteMin = new Date(Date.parse(dataLimiteMin.substr(3,2)+'/'+dataLimiteMin.substr(0,2)+'/'+dataLimiteMin.substr(6)));
  var dLimiteMax = new Date(Date.parse(dataLimiteMax.substr(3,2)+'/'+dataLimiteMax.substr(0,2)+'/'+dataLimiteMax.substr(6)));
  var dInserita = new Date(Date.parse(dataInserita.value.substr(3,2)+'/'+dataInserita.value.substr(0,2)+'/'+dataInserita.value.substr(6)));
  //debugger;
  if (isNaN(dInserita))
    return true;
  if (dLimiteMin >= dInserita) {
      dataInserita.select();
      dataInserita.focus();
      alert('Non è possibile inserire un permesso con data antecedente o uguale alla data di chiusura del cartellino(' + dataLimiteMin + '), per l\'inserimento contattare l\'amministrazione del personale.');
    return false; } 
  if (dInserita > dLimiteMax ) {
      var msg="Attenzione : la data che avete inserito(" + dataInserita.value + ") è collocata in un tempo futuro oltre 90 gg, è corretta ?";
      if (confirm(msg)) {
            return true;
                       }
        else
          { 
            dataInserita.select();
            dataInserita.focus();
            return false; } 
          } 
    return true;
} 


// Controlla che la data inserita si trovi in un range corretto
function dataMancataTimb(dataLimiteMin, dataLimiteMax, dataInserita)
{
/*  var dLimiteMin = new Date(Date.parse(dataLimiteMin.substr(3,2)+'/'+dataLimiteMin.substr(0,2)+'/'+dataLimiteMin.substr(6)));
  var dLimiteMax = new Date(Date.parse(dataLimiteMax.substr(3,2)+'/'+dataLimiteMax.substr(0,2)+'/'+dataLimiteMax.substr(6)));
  var dInserita = new Date(Date.parse(dataInserita.value.substr(3,2)+'/'+dataInserita.value.substr(0,2)+'/'+dataInserita.value.substr(6)));
  //debugger;
  if (isNaN(dInserita))
    return true;
  if (dLimiteMin >= dInserita) {
      dataInserita.select();
      dataInserita.focus();
      alert('Non è possibile inserire una mancata timbratura con data antecedente o uguale alla data di chiusura del cartellino(' + dataLimiteMin + '), per l\'inserimento contattare l\'amministrazione del personale.');
    return false; } 
  if (dInserita > dLimiteMax ) {
      dataInserita.select();
      dataInserita.focus();
      alert("Attenzione : non è possibile inserire una mancata timbratura con data futura");
      return false;
     }
*/
  return true;
} 

