/*
NOME SORGENTE   :  j_general.js

CREATO          :  18/02/2001
AGGIORNATO      :  21/02/2002

PROGRAMMATO DA  :  Alberto Roli
COPYRIGHT (c)   :  TTY Creo S.r.l. 2002

DESCRIZIONE     :  Funzioni di utilizzo standard in javascript 

RIEPILOGO       :  In questo file sono definite le seguenti funzioni:

                   isLetter
                   isDigit
                   isEmpty
                   isNumeric
                   isAlphanumeric
                   isAlphabetic
                   ValStr
                   CodNome
                   CodCogn
                   CodAnno
                   CodGior
                   CodMese
                   CodFis
                   CodFisNC
                   CheckTarga
                   Subm
                   Control
                   SetVal
                   OpenPage
                   CheckNumeric
                   StopInput
                   Stampa
*/


// restituisce true se v_char Š una lettera altrimenti restituisce false
function isLetter (v_char)
{
    return (((v_char >= "a") && (v_char <= "z")) || ((v_char >= "A") && (v_char <= "Z")));
}


// restituisce true se v_char Š una numero altrimenti restituisce false
function isDigit (v_char)
{
    return ((v_char >= "0") && (v_char <= "9"));
}


// restituisce true se v_string Š vuoto altrimenti restituisce false
function isEmpty(v_string)
{   
    return ((v_string == null) || (v_string.length == 0))
}


// restituisce true se v_string Š numerico altrimenti restituisce false
function isNumeric(v_string)
{   var f_ci;

    if (isEmpty(v_string))
    {
        if (isNumeric.arguments.length == 1)
            return false;
        else
            return (isNumeric.arguments[1] == true);
    }

    /* search through string's characters one by one
       until we find a non-numeric character.
       when we do, return false; if we don't, return true. */

    for (f_ci = 0; f_ci < v_string.length; f_ci++)
    {
        // check that current character is number.
        var f_char = v_string.charAt(f_ci);

        if (! isDigit(f_char))
        return false;
    }

    // all characters are numbers.
    return true;
}


// restituisce true se v_string Š numerico altrimenti restituisce false
function isAlphanumeric(v_string)
{   var f_ci;

    if (isEmpty(v_string))
    {
        if (isAlphanumeric.arguments.length == 1)
            return false;
        else
            return (isAlphanumeric.arguments[1] == true);
    }

    /* search through string's characters one by one
       until we find a non-alphanumeric character.
       when we do, return false; if we don't, return true. */

    for (f_ci = 0; f_ci < v_string.length; f_ci++)
    {
        // check that current character is number or letter.
        var f_char = v_string.charAt(f_ci);

        if (! (isLetter(f_char) || isDigit(f_char)))
        return false;
    }

    // all characters are numbers or letters.
    return true;
}


// restituisce true se v_string Š alfabetica altrimenti restituisce false
function isAlphabetic (v_string)
{   var f_ci;

    if (isEmpty(v_string))
    {
        if (isAlphabetic.arguments.length == 1)
            return false;
        else
            return (isAlphabetic.arguments[1] == true);
    }

    /* search through string's characters one by one
       until we find a non-alphabetic character.
       when we do, return false; if we don't, return true. */

    for (f_ci = 0; f_ci < v_string.length; f_ci++)
    {
        // check that current character is letter.
        var f_char = v_string.charAt(f_ci);

        if (! isLetter(f_char))
            return false;
    }

    // all characters are letters.
    return true;
}



function ValStr(v_string)
{
    var f_lettere = "QWERTYUIOPASDFGHJKLZXCVBNM";
    var f_temp;
    var f_buff = "";

    for (f_ci = 0; f_ci <= v_string.length; f_ci++)
    {
        f_temp = "" + v_string.substring(f_ci, f_ci + 1);
        if (f_lettere.indexOf(f_temp) != "-1")
        f_buff = f_buff + f_temp
    }

    return f_buff;
}


// funzione per controllare la porzione del nome del codice fiscale
function CodNome(v_nome, v_codFis)
{
	var f_nomTmp = "";
	var f_fisTmp = "";

    v_codFis = v_codFis.toUpperCase();
    f_fisTmp = v_codFis.substring(3, 6);
	v_nome = v_nome.toUpperCase();
    v_nome = ValStr(v_nome);

	for(f_ci = 0; f_ci < v_nome.length; f_ci++)
	{
		switch (v_nome.charAt(f_ci))
		{
			case 'A': break;
			case 'E': break;
			case 'I': break;
			case 'O': break;
			case 'U': break;

			default : f_nomTmp = f_nomTmp + v_nome.charAt(f_ci);
				      break;
		}
	}

	if (f_nomTmp.length > 3)
	{
		f_nomTmp = f_nomTmp.substring(0, 1) + f_nomTmp.substring(2, 4);
	}
	else if (f_nomTmp.length < 3)
	{
		for(f_ci = 0; f_ci < v_nome.length; f_ci++)
		{
			switch (v_nome.charAt(f_ci))
			{
				case 'A':
				case 'E':
				case 'I':
				case 'O':
				case 'U': f_nomTmp = f_nomTmp + v_nome.charAt(f_ci);
					      break;

				default : break;
			}
		}

		while (f_nomTmp.length < 3)
		{
			f_nomTmp = f_nomTmp + 'X';
		}
	}

	f_nomTmp = f_nomTmp.substring(0, 3);

	return (f_nomTmp == f_fisTmp);
}


// funzione per controllare la porzione del cognome del codice fiscale
function CodCogn(v_cogn, v_codFis)
{
	var f_cogTmp = "";
	var f_fisTmp = "";

    v_codFis = v_codFis.toUpperCase();
	f_fisTmp = v_codFis.substring(0, 3);
	v_cogn = v_cogn.toUpperCase();
    v_cogn = ValStr(v_cogn);

	for(f_ci = 0; f_ci < v_cogn.length; f_ci++)
	{
		switch (v_cogn.charAt(f_ci))
		{
			case 'A': break;
			case 'E': break;
			case 'I': break;
			case 'O': break;
			case 'U': break;

			default : f_cogTmp = f_cogTmp + v_cogn.charAt(f_ci);
				      break;
		}
	}

	if (f_cogTmp.length < 3)
	{
		for(f_ci = 0; f_ci < v_cogn.length; f_ci++)
		{
			switch (v_cogn.charAt(f_ci))
			{
				case 'A':
				case 'E':
				case 'I':
				case 'O':
				case 'U': f_cogTmp = f_cogTmp + v_cogn.charAt(f_ci);
					      break;

				default : break;
			}
		}

		while (f_cogTmp.length < 3)
		{
			f_cogTmp = f_cogTmp + 'X';
		}
	}

	f_cogTmp = f_cogTmp.substring(0, 3);
	return (f_cogTmp == f_fisTmp);
}


// funzione per controllare la porzione dell'anno di nascita del codice fiscale
function CodAnno(v_aa, v_codFis)
{
	var f_aaTmp = "";
    var f_fisTmp = "";

    v_codFis = v_codFis.toUpperCase();
    f_fisTmp = v_codFis.substr(6, 2);
    f_aaTmp = v_aa.substr(2, 2);

    return (f_fisTmp == f_aaTmp);
}


/* funzione per controllare la porzione del giorno di nascita e sesso del 
   codice fiscale */
function CodGior(v_gg, v_codFis, v_sex)
{
	var f_ggTmp = "";
    var f_fisTmp = "";
    var f_sexTmp = "";
    var f_giorno = 0;

    v_codFis = v_codFis.toUpperCase();
    f_fisTmp = v_codFis.substr(9, 2);
    f_ggTmp = v_gg;
    f_sexTmp = v_sex;
    if (f_sexTmp == "M")
    {
        return (f_fisTmp == f_ggTmp);
    }

    if (f_sexTmp == "F")
    {
        if (f_ggTmp.substr(0, 1) == "0")
        {
            f_ggTmp = f_tempGG.substr(1, 1);
        }
        f_giorno = parseInt(f_ggTmp);
        return (f_fisTmp == (f_giorno + 40));
    }
}


// funzione per controllare la porzione del mese di nascita del codice fiscale
function CodMese(v_mm, v_codFis)
{
	var f_mmTmp = "";
    var f_fisTmp = "";

    f_codFis = f_codFis.toUpperCase();
    f_fisTmp = f_codFis.substr(8, 1);
    f_mmTmp = v_mm;
    
    if (f_fisTmp == "A")
        return (f_mmTmp == "01");

    if (f_fisTmp == "B")
        return (f_mmTmp == "02");

    if (f_fisTmp == "C")
        return (f_mmTmp == "03");

    if (f_fisTmp == "D")
        return (f_mmTmp == "04");

    if (f_fisTmp == "E")
        return (f_mmTmp == "05");
        
    if (f_fisTmp == "H")
        return (f_mmTmp == "06");
        
    if (f_fisTmp == "L")
        return (f_mmTmp == "07");

    if (f_fisTmp == "M")
        return (f_mmTmp == "08");

    if (f_fisTmp == "P")
        return (f_mmTmp == "09");

    if (f_fisTmp == "R")
        return (f_mmTmp == "10");

    if (f_fisTmp == "S")
        return (f_mmTmp == "11");

    if (f_fisTmp == "T")
        return (f_mmTmp == "12");

    return false;
}


// funzione che controlla la validit… del codice fiscale in tutte le sue parti
function CodFis(v_nome, v_cogn, v_gg, v_mm, v_aa, v_sex, v_codFis, v_form)
{
    f_codFis = document.forms[v_form].elements[v_codFis].value;
    f_fisLen = f_codFis.length;

	if ((f_fisLen != 16) ||
	    (! CodNome(v_nome, f_codFis)) ||
	    (! CodCogn(v_cogn, f_codFis)) ||
	    (! CodAnno(v_aa, f_codFis))||
	    (! CodMese(v_mm, f_codFis))||
	    (! CodGior(v_gg, f_codFis, f_sex)))
    {
	    alert("Codice fiscale non valido");

	    document.forms[v_form].elements[v_codFis].value = "";
	    document.forms[v_form].elements[v_codFis].focus();

	    return;
	}
}


// funzione che controlla la validit… del codice fiscale pati nome e cognome
function CodFisNC(v_nome, v_cogn, v_codFis, v_form)
{
    f_codFis = document.forms[v_form].elements[v_codFis].value;
    f_fisLen = f_codFis.length;

	if ((f_fisLen != 16) ||
	    (! CodNome(v_nome, f_codFis)) ||
	    (! CodCogn(v_cogn, f_codFis)) )
    {
	    alert("Codice fiscale non valido");

	    document.forms[v_form].elements[v_codFis].value = "";
	    document.forms[v_form].elements[v_codFis].focus();

	    return;
	}
}


// funzione che controlla la validit… di un numero di targa v_targa
function CheckTarga(v_targa)
{
    f_targa = document.general.elements[v_targa].value;
    
    if ((f_targa.length < 7) || (f_targa.length > 8))
    {
        document.general.elements[v_targa].value = "";
        alert ("Numero di targa non valido");
        return;
    }

    switch (f_targa.length)
    {
	    case 7: if (! isLetter(f_targa.substring(0,1)))
	            {
	                document.general.elements[v_targa].value = "";
				    alert ("Numero di targa non valido");
					return;
				}

				if (! isLetter(f_targa.substring(1,2)))
				{
				    document.general.elements[v_targa].value = "";
                    alert ("Numero di targa non valido");
                    return;
                }

				if (! isDigit(f_targa.substring(2,3)))
				{
				    document.general.elements[v_targa].value = "";
                    alert ("Numero di targa non valido");
                    return;
                }

				if (! isDigit(f_targa.substring(3,4)))
				{
				    document.general.elements[v_targa].value = "";
                    alert ("Numero di targa non valido");
                    return;
                }

				if (! isDigit(f_targa.substring(4,5)))
				{
				    document.general.elements[v_targa].value = "";
                    alert ("Numero di targa non valido");
                    return;
                }

				if (! isLetter(f_targa.substring(5,6)))
				{
				    document.general.elements[v_targa].value = "";
                    alert ("Numero di targa non valido");
                    return;
                }

				if (! isLetter(f_targa.substring(6,7)))
				{
				    document.general.elements[v_targa].value = "";
                    alert ("Numero di targa non valido");
                    return;
                }
				break;

	    case 8: if (!isLetter(f_targa.substring(0,1)))
	            {
	                document.general.elements[v_targa].value = "";
                    alert ("Numero di targa non valido")
                    return;
                }

				if (! isLetter(f_targa.substring(1,2)))
				{
				    document.general.elements[v_targa].value = "";
                    alert ("Numero di targa non valido");
                    return;
                }

 				if (! isAlphanumeric(f_targa.substring(2,8)))
 				{
 				    document.general.elements[v_targa].value = "";
                    alert ("Numero di targa non valido");
                    return;
                }
 			    break;
    }
    return true;
}

// funzione che esegue la submit di un form denominato v_form
function Subm(v_form, v_action)
{
    document.forms[v_form].action = v_action;
    document.forms[v_form].submit();
}


// funzione che controlla la validit… di x numero di campi input 
function Control(v_form, v_action, v_fields)
{
    var v_aFields;
    
    f_aFields = v_fields.split(",");
    
    for (f_ci in f_aFields)
    {
        if (document.forms[v_form].elements[f_aFields[f_ci]])
        {
            f_value = document.forms[v_form].elements[f_aFields[f_ci]].value;
            if (isEmpty(f_value))
            {
                alert ("Hai lasciato vuoto uno o piů campi.");
                document.forms[v_form].elements[f_aFields[f_ci]].focus();
                return;
            }
        }
    }
    
    Subm(v_form, v_action);
}


/* funzione che assegna un valore v_value ad un campo input v_field in un form
   v_form */
function SetVal(v_form, v_field, v_value)
{
    if ((document.forms[v_form].elements[v_field]) && (! isEmpty(v_value)))
    {
        if (document.forms[v_form].elements[v_field].type == "checkbox")
            document.forms[v_form].elements[v_field].checked = true;

        document.forms[v_form].elements[v_field].value = v_value;
    }
}

/* apre una popup con la pagina v_url e le opzioni v_options e ne setta il 
   focus */
function OpenPage(v_url, v_options)
{
  f_window = window.open(v_url, 'popup', v_options);
  f_window.focus();
}


/* funzione che controlla se il C.A.P. Š valido (numerico) - puo essere
utilizzata per controllare qualsiasi campo se si vuole - v_message Š il 
messaggio di display in caso di errore */
function CheckNumeric(v_form, v_field, v_maxlen, v_message)
{
    f_value = document.forms[v_form].elements[v_field].value;
    if ((! isNumeric(f_value)) || f_value.length < v_maxlen)
    {
        alert (v_message);
        document.forms[v_form].elements[v_field].value = "";
        document.forms[v_form].elements[v_field].focus();
    }
}

/* funzione che limita il numero di caratteri digitabili in un campo input o
   textarea */
function StopInput(v_form, v_field, v_maxlen)
{
    if (document.forms[v_form].elements[v_field].value.length > v_maxlen) 
        document.forms[v_form].elements[v_field].value = document.forms[v_form].elements[v_field].value.substring(0, v_maxlen);
}


// funzione che stampa la pagina corrente
function Stampa()
{
    f_result = confirm("Se si ha l'opzione Stampa colori e immagini di sfondo disabilitata la stampa risulterŕ diversa.");
    if (f_result)
        window.print();
}
