
//**************
function checkValue(objName,objValue) {
	if (objValue.length == 0) {
		validatePrompt (objName,"Il campo è obbligatorio");
		return false
	}
	return true
}
//**************
function validatePrompt (objName, PromptStr) {
	alert (PromptStr);
	objName.focus();
	return;
}
//**************
function isDigit(c) {
	return ( ( c >= "0" ) && ( c <= "9" ) )
}

//**************
function isInteger(objName,objValue, opt) {
	objValue = trim(objValue)
	if (opt) {if (!checkValue(objName,objValue)) return false}
	if (objValue.length == 0) return true;

	var i;
	for ( i = 0; i < objValue.length; i++ )
	    {   
	        if ( ! isDigit(objValue.charAt(i)) ){
						validatePrompt (objName,"Il valore deve essere di tipo numerico!");
						return false;
			}
	    }
	return true;
}
//**************
function trim(v) {
	return lTrim(rTrim(v));
}

//**************
function lTrim(v) {
	var i = 0;
    while ( (i < v.length) && charInString(v.charAt(i), " \t\n\r") )
       i++;
    return v.substring(i, v.length);
}

//**************
function rTrim(v) {
	var i = v.length;
    while ( (i > 0) && charInString(v.charAt(i-1), " \t\n\r") )
       i--;
    return v.substring(0, i);
}
//**************
function charInString(c, v) {
	for (i = 0; i < v.length; i++) {
		if (v.charAt(i) == c)
			return true;
    }
    return false
}

//**************
function validaCF(element)
{
   var campo;
   campo = element;
   caratteri= new Array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
   pari= new Array (0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25)
   dispari=new Array (1,0,5,7,9,13,15,17,19,21,1,0,5,7,9,13,15,17,19,21,2,4,18,20,11,3,6,8,12,14,16,10,22,25,24,23)
   cod=campo.value.toLowerCase();
   check=true;
   if (cod.length!=16)
   {
      check=false
   }
   else
   {
      lettere=cod.substr(0,6)+cod.substr(8,1)+cod.substr(11,1)+cod.substr(15);
      numeri=cod.substr(6,2)+cod.substr(9,2);//+cod.substr(12,3);
      for (i=0;i<10;i++)
      {
         if (lettere.charCodeAt(i)<97 || lettere.charCodeAt(i)>122)
         {
            check=false;
         }
      }
      for (i=0;i<5;i++)
      {
         if (numeri.charCodeAt(i)<48 || numeri.charCodeAt(i)>57)
         {
            check=false;
         }
      }   
   }
   //checksum del codice fiscale
   test=cod.substr(15,1);
   var somma=0
   for (i=0;i<16;i=i+2)
   { //dispari
       carattere=cod.substr(i,1)
       for (k=0;k<36;k++)
       {
          if (carattere==caratteri[k])
          {
             somma=somma+dispari[k]
             break
          }
       }
    }
    for (i=1;i<15;i=i+2)
    { //pari
       carattere=cod.substr(i,1)
       for (k=0;k<36;k++)
       {
          if (carattere==caratteri[k])
          {
             somma=somma+pari[k]
             break
          }
       }
    }
   resto=somma % 26;
   var lettera=String.fromCharCode(97+resto);            
   if (test != lettera)
   {
      check=false;
   }   
   if (check==false)
   {
		event.returnValue = false;
		var answer;
		answer = confirm("Attenzione: Campo Codice Fiscale non corretto\nPremere OK per cancellare il campo e inserirne uno corretto\nPremere Annulla per controllare il valore inserito");
		if (answer)
			element.value = "";
   }	
      else
   {
      event.returnValue = true;
   }
}
    
/****************************************************************/
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
