/*********************************************************
*	Titolo		 :nhxnwl001																
*																												
* Autore/i	 :L.Montebove, ADL
*																												
* Descrizione:funzion javascript di utilizzo generale	
*																												
*																												
*********************************************************
*/

/**
 *Descrizione:
 *imposta utente e livello sulla status bar
 *
 *
 *
 *Ritorna:
 *
 *Parametri:
 *		str_Ute nome utente
 *		str_Liv livello Utente
 */
function statusString(str_Ute, str_Liv) // Domenico
{
	window.status="Utente = " + str_Ute + " - Livello = " + str_Liv;
}

/**
 *Descrizione:
 *Elimina un carattere anomalo da una stringa numerica
 *
 *
 *Ritorna:
 *		Il valore intero corrispondente
 *
 *Parametri:
 *		Stringa la stringa da convertire
 *		Carattere il carattere da cambiare
 */
function ConvertString(Stringa,Carattere)
{
	var ritorno="";
	for (k=0; k<= Stringa.length; k++)
	{
		if (Stringa.charAt(k) != Carattere)
			ritorno=ritorno+Stringa.charAt(k);
	}
	return parseInt(ritorno);
}

/**
 *Descrizione:
 *sostituisce un carattere in una stringa
 *
 *
 *Ritorna:
 *		La stringa modificata
 *
 *Parametri:
 *		Stringa la stringa da convertire
 *		Car1 il carattere da sostituire
 *		Car2 il carattere con cui sostituirlo
 */
function replaceChar(Stringa,Car1,Car2)
{
	var ritorno="";
	for (k=0; k<= Stringa.length; k++)
	{
		if (Stringa.charAt(k) != Car1)
			ritorno=ritorno+Stringa.charAt(k);
		else
			ritorno=ritorno+Car2;
	}
		return ritorno;
}

/**
 *Descrizione:
 *imposta il campo B5 di una form al valore
 *dato
 *
 *Ritorna:
 *		
 *Parametri:
 *		val il valore da assegnaresostituirlo
 */
function impostaB5(val) 
{
  document.forms[0].B5.value = val;
}


/**
 *Descrizione:
 *inverte una data da gg/mm/aaaa a
 *aaaammgg
 *
 *Ritorna:
 *		La data convertita
 *Parametri:
 *		Stringa la data da convertire
 */
function InvertiData(Stringa)
{
	anno = Stringa.substring(6,10)
	mese = Stringa.substring(3,5)
	gior = Stringa.substring(0,2)
	return (anno+mese+gior)
}

/**
 *Descrizione:
 *controllo che la stringa passata sia una data 
 *valida
 *
 *Ritorna:
 *		vero se si tratta di una data, 
 *		falsoin caso contrario
 *Parametri:
 *		Stringa la data da controllare
 */
function ControllaData(Stringa)
{
	var anno
	var mese
	var mesi
	var giorno
	
	if (Stringa.length != 10)
		{
		return (false)
		}
	else
		{
		anno = parseInt(Stringa.substring(6,10))
		}
	
   	if (Stringa.substring(0,1) == 0)
   	{
		gior = parseInt(Stringa.substring(1,2))
   	}
	else
	{
		gior = parseInt(Stringa.substring(0,2))
   	}

  	if (Stringa.substring(3,4) == 0)
   	{
		mese = parseInt(Stringa.substring(4,5));
   	}
	else
	{
		mese = parseInt(Stringa.substring(3,5));
   	}
	
	if (isNaN(anno)==true)
		{
		return false
		}
	if (isNaN(gior)==true)
		{
		return false
		}
	if (isNaN(mese)==true)
		{
		return false
		}
	if (anno < 1900)
		{
		return false
		}

	mesi = new Array(31,28,31,30,31,30,31,31,30,31,30,31)
	if ((anno % 4) ==0)
		{
		mesi[1]=29
		}
	else
		{
		if ((anno % 25) ==0)
			{
			mesi[1]=29
			}
		}
	if (mese > 12)
		{
		return false
		}
	if (gior > mesi[mese-1])
		{
		return false
		}
	return true
}


/**
 *Descrizione:
 *controllo che la data1 sia minore della data2
 *
 *
 *Chiama:
 *		ControllaData
 *Ritorna:
 *		vero se data1 < data2 
 *		falso in caso contrario
 *Parametri:
 *		Data1 la data che dovrebbe essere minore
 *		Data1 la data che dovrebbe essere maggiore
 */
function CronologiaDate(Data1,Data2) 
{
  var anno1;
	var mese1;
	var gior1;
  var anno2;
	var mese2;
	var gior2;

  // Controllo delle due date
  
  if (!ControllaData(Data1)) {
  	return (false);	
  }
  

	if (!ControllaData(Data2)) {
  	return (false);	
  }
  
  anno1 = parseInt(Data1.substring(6,10));

  if (Data1.substring(0,1) == 0) {
    gior1 = parseInt(Data1.substring(1,2))
  }
	else {
    gior1 = parseInt(Data1.substring(0,2))
  }

  if (Data1.substring(3,4) == 0) {
		mese1 = parseInt(Data1.substring(4,5));
  }else {
    mese1 = parseInt(Data1.substring(3,5));
  }

  anno2 = parseInt(Data2.substring(6,10));

  if (Data2.substring(0,1) == 0) {
    gior2 = parseInt(Data2.substring(1,2))
  }
	else {
    gior2 = parseInt(Data2.substring(0,2))
  }

  if (Data2.substring(3,4) == 0) {
		mese2 = parseInt(Data2.substring(4,5));
  }else {
    mese2 = parseInt(Data2.substring(3,5));
  }

  if (anno1>anno2) {

    return(false);
  }

  else {

    if ((mese1>mese2) && (anno1==anno2)) {
      return(false);
    }
    else {
      if ((gior1>gior2) && (mese1==mese2) && (anno1==anno2)) {
        return(false);
      }
    }
  }

  return (true);

}


/**
 *Descrizione:
 *controllo della partita iva 
 *
 *Chiama:
 *		digitPersGiuridica
 *Ritorna:
 *		vero se OK
 *		falso in caso contrario
 *Parametri:
 *		Stringa la p. iva da controllare
 */
function PIVAOK(Stringa)
{
  if (!isNaN(Stringa) || Stringa == "") {
    if (Stringa.length == 11)
      return digitPersGiuridica(Stringa);
    else if (Stringa.length == 0)
      return true;
    else
      return false;
  }
  else
    return false;
}

/**
 *Descrizione:
 *controllo del codice fiscale
 *
 *Chiama:
 *		digitPersGiuridica
 *		DigitPersFisica
 *Ritorna:
 *		vero se OK
 *		falso in caso contrario
 *Parametri:
 *		codContr il cod. fiscale  da controllare
 */
function CFOK(codContr)
{
  var codRit;
  var i;
  var appSte;

  codRit = true;

  if (codContr.length == 16) {

           for (i=0;i<16;i++) {
             appSte = codContr.charAt(i);
             if ((i==6 || i==7 || i==9 || i==10 || (i < 15 && i > 11)) && isNaN(appSte)) {
                codRit = false;
             }
             else if ((i < 6 || i == 8 || i == 11 || i == 15) && !isNaN(appSte)) {
                codRit = false;
             }
           }
           if (codRit) {
              codRit = DigitPersFisica(codContr);
           }
  }
  else if (codContr.length == 11) {

           for (i=0;i<9;i++) {
             appSte = codContr.charAt(i);
             if (isNaN(appSte)) {
                codRit = false;
             }
           }
           if (codRit) {
              codRit = digitPersGiuridica(codContr);
           }
  }
  else if (codContr.length == 0) {
    codRit = true;
  }
  else {
    codRit = false;
  }
  return codRit;
}

/**
 *Descrizione:
 *Calcolo del check-digit per persona fisica
 *
 *Ritorna:
 *		vero se OK
 *		falso in caso contrario
 *Parametri:
 *		codContr il cod. fiscale  da controllare
 */
function DigitPersFisica(codContr)
{

    var i;
    var SumTot;
    var Resto;
    var Digit;
    var appSte;

    SumTot = 0;
    Resto = 0;

    for (i=0;i<15;i++) {

        appSte = codContr.charAt(i);

        if (((i + 1) % 2) == 0) { //elem. pari

            if (appSte == "0" || appSte == "A")
                  SumTot = SumTot + 0;
            else if (appSte == "1" || appSte == "B")
                  SumTot = SumTot + 1;
            else if (appSte == "2" || appSte == "C")
                  SumTot = SumTot + 2;
            else if (appSte == "3" || appSte == "D")
                  SumTot = SumTot + 3;
            else if (appSte == "4" || appSte == "E")
                  SumTot = SumTot + 4;
            else if (appSte == "5" || appSte == "F")
                  SumTot = SumTot + 5;
            else if (appSte == "6" || appSte == "G")
                  SumTot = SumTot + 6;
            else if (appSte == "7" || appSte == "H")
                  SumTot = SumTot + 7;
            else if (appSte == "8" || appSte == "I")
                  SumTot = SumTot + 8;
            else if (appSte == "9" || appSte == "J")
                  SumTot = SumTot + 9;
            else if (appSte == "K")
                  SumTot = SumTot + 10;
            else if (appSte == "L")
                  SumTot = SumTot + 11;
            else if (appSte == "M")
                  SumTot = SumTot + 12;
            else if (appSte == "N")
                  SumTot = SumTot + 13;
            else if (appSte == "O")
                  SumTot = SumTot + 14;
            else if (appSte == "P")
                  SumTot = SumTot + 15;
            else if (appSte == "Q")
                  SumTot = SumTot + 16;
            else if (appSte == "R")
                  SumTot = SumTot + 17;
            else if (appSte == "S")
                  SumTot = SumTot + 18;
            else if (appSte == "T")
                  SumTot = SumTot + 19;
            else if (appSte == "U")
                  SumTot = SumTot + 20;
            else if (appSte == "V")
                  SumTot = SumTot + 21;
            else if (appSte == "W")
                  SumTot = SumTot + 22;
            else if (appSte == "X")
                  SumTot = SumTot + 23;
            else if (appSte == "Y")
                  SumTot = SumTot + 24;
            else if (appSte == "Z")
                  SumTot = SumTot + 25;
        }
        else {

            if (appSte == "0" || appSte == "A")
                  SumTot = SumTot + 1;
            else if (appSte == "1" || appSte == "B")
                  SumTot = SumTot + 0;
            else if (appSte == "2" || appSte == "C")
                  SumTot = SumTot + 5;
            else if (appSte == "3" || appSte == "D")
                  SumTot = SumTot + 7;
            else if (appSte == "4" || appSte == "E")
                  SumTot = SumTot + 9;
            else if (appSte == "5" || appSte == "F")
                  SumTot = SumTot + 13;
            else if (appSte == "6" || appSte == "G")
                  SumTot = SumTot + 15;
            else if (appSte == "7" || appSte == "H")
                  SumTot = SumTot + 17;
            else if (appSte == "8" || appSte == "I")
                  SumTot = SumTot + 19;
            else if (appSte == "9" || appSte == "J")
                  SumTot = SumTot + 21;
            else if (appSte == "K")
                  SumTot = SumTot + 2;
            else if (appSte == "L")
                  SumTot = SumTot + 4;
            else if (appSte == "M")
                  SumTot = SumTot + 18;
            else if (appSte == "N")
                  SumTot = SumTot + 20;
            else if (appSte == "O")
                  SumTot = SumTot + 11;
            else if (appSte == "P")
                  SumTot = SumTot + 3;
            else if (appSte == "Q")
                  SumTot = SumTot + 6;
            else if (appSte == "R")
                  SumTot = SumTot + 8;
            else if (appSte == "S")
                  SumTot = SumTot + 12;
            else if (appSte == "T")
                  SumTot = SumTot + 14;
            else if (appSte == "U")
                  SumTot = SumTot + 16;
            else if (appSte == "V")
                  SumTot = SumTot + 10;
            else if (appSte == "W")
                  SumTot = SumTot + 22;
            else if (appSte == "X")
                  SumTot = SumTot + 25;
            else if (appSte == "Y")
                  SumTot = SumTot + 24;
            else if (appSte == "Z")
                  SumTot = SumTot + 23;

        }
    }

    appSte = codContr.charAt(15);
    Resto = SumTot % 26;

    if (Resto == 0)
            Digit = "A";
    else if (Resto == 1)
            Digit = "B";
    else if (Resto == 2)
            Digit = "C";
    else if (Resto == 3)
            Digit = "D";
    else if (Resto == 4)
            Digit = "E";
    else if (Resto == 5)
            Digit = "F";
    else if (Resto == 6)
            Digit = "G";
    else if (Resto == 7)
            Digit = "H";
    else if (Resto == 8)
            Digit = "I";
    else if (Resto == 9)
            Digit = "J";
    else if (Resto == 10)
            Digit = "K";
    else if (Resto == 11)
            Digit = "L";
    else if (Resto == 12)
            Digit = "M";
    else if (Resto == 13)
            Digit = "N";
    else if (Resto == 14)
            Digit = "O";
    else if (Resto == 15)
            Digit = "P";
    else if (Resto == 16)
            Digit = "Q";
    else if (Resto == 17)
            Digit = "R";
    else if (Resto == 18)
            Digit = "S";
    else if (Resto == 19)
            Digit = "T";
    else if (Resto == 20)
            Digit = "U";
    else if (Resto == 21)
            Digit = "V";
    else if (Resto == 22)
            Digit = "W";
    else if (Resto == 23)
            Digit = "X";
    else if (Resto == 24)
            Digit = "Y";
    else if (Resto == 25)
            Digit = "Z";

    if (Digit == appSte)
        return true;
    else
        return false;
}

/**
 *Descrizione:
 *Calcolo del check-digit per persona giuridica
 *
 *Ritorna:
 *		vero se OK
 *		falso in caso contrario
 *Parametri:
 *		codContr il cod. fiscale/p. iva  da controllare
 */
function digitPersGiuridica(codContr)
{
    var i;
    var appSte;
    var SumTot;
    var UltimaCifra;
    var appint;
    SumTot = 0;

    for (i=0;i<10;i++) {

        appSte = codContr.charAt(i);
        appint = parseInt(appSte);
        if (((i + 1) % 2) == 0) {  // elementi pari
            if ((appint * 2) > 9)
                SumTot = SumTot + ((appint * 2) - 9);
            else
                SumTot = SumTot + (appint * 2);
        }
        else
            SumTot = SumTot + appint;
    }

    if (SumTot > 99)
        UltimaCifra = SumTot % 100;
    else if (SumTot > 9)
        UltimaCifra = SumTot % 10;
    else
        UltimaCifra = SumTot;

    if (UltimaCifra == 0) UltimaCifra = 10;

    appSte = codContr.charAt(10); // istruzione per il controllo dell'11imo byte

    if ((10 - UltimaCifra) == parseInt(appSte))
        return true;
    else
        return false;
}

/**
 *Descrizione:
 *selezione elemento da una lista con checkbox
 *
 *Ritorna:
 *
 *Parametri:
 *
 */
function selez()
{
  var i;
  for (i=0;i<document.forms[0].elements.length;i++) {
    if (document.forms[0].elements[i].value != document.forms[0].CTRL.value
      && document.forms[0].elements[i].checked == true) {
      document.forms[0].CTRL.value = document.forms[0].elements[i].value;
      document.forms[0].submit();
    }
  }
}

/**
 *Descrizione:
 *arrotonda un numero alle cifre richieste
 *
 *Ritorna:
 *		il numero arrotondato
 *Parametri:
 *		number il numero da arrotondare
 *		decimals il numero di cifre decimali da usare
 */
function roundTo(number, decimals)
{
	multiplier = Math.pow(10, decimals);
    return Math.round(number*multiplier)/(multiplier);
}

/**
 *Descrizione:
 *richiama una pagina di help
 *
 *Ritorna:
 *
 *Parametri:
 *		helpPage il nome della pagina da visualizzare
 */
function aiuto(helpPage)
{
  var features = "";
//  features = "toolbar=yes,scrollbars=yes,location=yes,directories=yes,status=yes,menubar=yes,resizable=yes,width=700,height=500";
  //features = "scrollbars=yes,resizable=yes,width=700,height=500";
  features = "scrollbars=yes";
  dlg = window.open ("", "popDialog", features);
  dlg.document.write ("<HTML><HEAD><title>Guida: Interconnessione con la Unione Europea</title></HEAD>");
  dlg.document.write ("<frameset cols='300,*' onBlur='self.focus()'>");
  dlg.document.write ("<frame name='sommario' target='principale' src='/pesca2/V2NLWL004.html'>");
  dlg.document.write ("<frame name='principale' src='/pesca2/");
  dlg.document.write (helpPage + ".html'>");
  dlg.document.write ("</frameset>");
  dlg.document.write ("</HTML>");
}

/**
 *Descrizione:
 *richiama una pagina con 
 *un calendario per selezionare
 *una data (impostazione iniziale
 *pari al campo data passato)
 *
 *Ritorna:
 *		la data selezionata
 *Parametri:
 *		il campo data passato
*/
function ShowCalendar(datefield) {
	window.dateField = document.forms[0].elements[datefield]
	calendar = window.open("../NHXNWL001.html", "", "width=300,height=300")
}


/**
 *Descrizione:
 *mostra nella status bar istruzioni
 *sull' uso del calendario
 *Ritorna:
 *
 *Parametri:
 *		
 */
function SetCalStatusBar() {
	window.status = "Clicca sull'icona del calendario per riempire il campo con una data"
}

/** ADL
 *Descrizione:
 *	controlla che il campo di valore str_Value sia impostato
 *
 *Ritorna:
 *	True se str_Value è valorizzato
 *	False altrimenti
 *
 *Parametri:
 *	str_value valore da controllare
 */
function ctrl_EsValue(str_Value)
{
	var ret;
	
	ret = false;
	
	if (str_Value != "")
		ret = true;

	return ret;
}

/** ADL
 *Descrizione:
 *	controlla che il primo parametro sia maggiore del secondo
 *
 *Ritorna:
 *	true se primo parametro maggiore del secondo
 *	false altrimenti
 *
 *Parametri:
 *	int_Primo primo valore
 *	int_Secondo secondo valore
 */
function ctrl_Max(int_Primo, int_Secondo)
{
	var ret;
	
	ret = false;
	
	if (int_Primo > int_Secondo)
		ret = true;
	
	return ret;
}

/** ADL
 *Descrizione:
 *	restituisce una stringa con il timestamp corrente
 *
 *Ritorna:
 *	la striga del timestamp formattato
 *
 *Parametri:
 *	nessuno
 */
function get_TimeStamp()
{

	var time = new Date()
	var anno = time.getFullYear()
	var mese = time.getMonth() + 1
	var giorno = time.getDate() 
	var ora = time.getHours()
	var minuti = time.getMinutes()
	var secondi = time.getSeconds()
	var millisecondi= time.getMilliseconds()

	var temps = String(anno)
	temps += ((mese < 10) ? "0" : "") + mese
	temps += ((giorno <10) ? "0" : "") + giorno
	temps += ((ora < 10)  ? "0" : "") + ora
	temps += ((minuti < 10) ? "0" : "") + minuti
	temps += ((secondi < 10) ? "0" : "") + secondi
	
//	alert (millisecondi)
	
	if (millisecondi < 10)
	{
		temps += "00" + millisecondi
	}
	else if (millisecondi < 100)
	{
		temps += "0" + millisecondi
	}
	else
	{
		temps += String(millisecondi)
	}
	
	return temps
}

/** ADL
 *Descrizione:
 *	sostituisce la parte di una stringa data
 *
 *Ritorna:
 *	la striga modificata
 *
 *Parametri:
 *	str_OldStr stringa da modificare
 *	str_NewIns stringa da inserire nella porzione di stringa str_OldStr
 *	int_Init   posizione iniziale
 *	int_End    posizione finale
 */
function Sostituzione(str_OldStr, str_NewIns, int_Init, int_End)
{
	if (int_Init > int_End)
	{
		alert("Sostituzione: indice iniziale maggiore del finale")
		return null
	}

	if (int_End > str_OldStr.length)
	{
		alert("Sostituzione: lunghezza della stringa in cui operare la sostituzione minore dell'indice finale")
		return null
	}

	if (str_NewIns.length != (int_End-int_Init+1))
	{
		alert("Sostituzione: lunghezza della stringa da inserire non compatibile")
		return null
	}
	
	var str_first = str_OldStr.substring(0,int_Init)
	var str_second = str_OldStr.substring(int_End+1)

//	alert(str_first+ " Lunghezza: "+str_first.length)
//	alert(str_second+ " Lunghezza: "+str_second.length)

	return (str_first+str_NewIns+str_second)
}

/** ADL
 *Descrizione:
 *	sostituisce la parte di una stringa header data
 *
 *Ritorna:
 *	la striga modificata
 *
 *Parametri:
 *	head_OldStr stringa header da modificare
 *	head_NewIns stringa che sostituisce la parte di interesse in head_OldStr
 *	int_Type id della parte dell'header da sostituire
 */
function SostituzioneH(OldHead, NewInsHead, int_Type)
{
	var str_first = "";
	var str_second = "";
	var int_Init;
	var int_End;

	if(int_Type < 0 || int_Type > 11)
	{
		alert("Sostituzione: id della parte di header da sostituire inesistente")
		return null
	}
	
	switch(int_Type)
	{
	
		case 0: // Intestazione header (0-6): non fa nulla
			return OldHead;
		break;
		case 1: // Identificativo richiedente (7-15)
			int_Init = 7;
			int_End = 15;
		break;
		case 2: // TimeStap (16-32)
			int_Init = 16;
			int_End = 32;
		break;
		case 3: // Identificativo workstation (33-39)
			int_Init = 33;
			int_End = 39;
		break;
		case 4: // Tipologia di servizio richiesto (40-44)
			int_Init = 40;
			int_End = 44;
		break;
		case 5: // Servizio logico (45-49)
			int_Init = 45;
			int_End = 49;
		break;
		case 6: // Funzione (50-53)
			int_Init = 50;
			int_End = 53;
		break;
		case 7: // Competenza territoriale (54-60)
			int_Init = 54;
			int_End = 60;
		break;
		case 8: // Modalità di risposta (61-61)
			int_Init = 61;
			int_End = 61;
		break;
		case 9: // Modalità di trasmissione dei dati (62-62)
			int_Init = 62;
			int_End = 62;
		break;
		case 10: // Certificazione (63-63)
			int_Init = 63;
			int_End = 63;
		break;
		case 11: // Filler (64-83)
			int_Init = 64;
			int_End = 83;
		break;
	}

	if (NewInsHead.length != (int_End-int_Init+1))
	{
		alert("Sostituzione: lunghezza della stringa da inserire non compatibile")
		return null
	}
	
	str_first = OldHead.substring(0,int_Init)
	str_second = OldHead.substring(int_End+1)

//	alert(str_first+ " Lunghezza: "+str_first.length)
//	alert(str_second+ " Lunghezza: "+str_second.length)

	return (str_first+NewInsHead+str_second)
}
