// $Id$
// $Log$
//-----------------------------------------------------------------------------------------
// Funzione che serve per lanciare un popup con messaggio impostato da asp
// Dipende dal fatto che sia definita la variabile msg
//-----------------------------------------------------------------------------------------
function tellme()
{
	if (msg != "") 
	{
		alert(msg);
	}
}
//-----------------------------------------------------------------------------------------
// Funzione di verifica form invio comunicazioni a ufficio tributi
//-----------------------------------------------------------------------------------------
function checkmailfm(fm)
{
	var j = 0;
	var msg = "";
	if (document.forms[0].elements[0].value.match(/^\s*$/)){msg += 'email\n';}
	if (document.forms[0].elements[1].value.match(/^\s*$/)){msg += 'testo della comunicazione\n';}
	if (msg)
	{
		msg = "__________________________________\n\n  ATTENZIONE\n __________________________________\n\nI seguenti campi sono obbligatori:\n\n" + msg;
		alert (msg);
		return false;
	}
	return true;
}
//-----------------------------------------------------------------------------------------
// Funzione di verifica correttezza compilazione tributi
//-----------------------------------------------------------------------------------------
function checkfillform(tributo)
{
	var msg = "";
	var totale = 0;
	var versamento = 0;
/*
	Controlli comuni per tutti i tributi
*/	
	document.forms[0].elements["js"].value='on';
	if (!document.forms[0].elements["cc"].value.match(/^\d+$/))						{msg += 'Conto corrente\n';}
	if (!document.forms[0].elements["importoeuro"].value.match (/^\d+$/))			{msg += 'Importo, parte intera\n';}
	if (!document.forms[0].elements["importocent"].value.match (/^\d\d$/))			{msg += 'Importo, parte decimale\n';}
	if (!document.forms[0].elements["importolettere"].value.match (/^[a-zA-Z ]+$/))	{msg += 'Importo in lettere deve essere specificato e contenere solo lettere, cifre e/o spazi\n';}

	switch(parseInt(tributo))
	{
		case 1:
		if (!IsCFisc(document.forms[0].elements["userid"].value) && (!document.forms[0].elements["userid"].value.match(/^\d{11}$/))){msg += 'Codice fiscale/Partita IVA errati o assenti\n';}
		if (document.forms[0].elements["intestatoa"].value.match (/^\s*$/))				{msg += 'Intestatario del versamento non può essere omesso\n';}
		if (document.forms[0].elements["effettuatoda"].value.match (/^\s*$/))			{msg += 'Non può essere omesso chi sta effettuando il versamento\n';}
		if (!document.forms[0].elements["terrenieuro"].value.match (/^\d+$/))			{msg += 'Importo terreni, parte intera\n';}
		if (!document.forms[0].elements["terrenicent"].value.match (/^\d\d$/))			{msg += 'Importo terreni, parte decimale\n';}
		if (!document.forms[0].elements["detrazionieuro"].value.match (/^\d+$/))		{msg += 'Importo detrazioni, parte intera\n';}
		if (!document.forms[0].elements["detrazionicent"].value.match (/^\d\d$/))		{msg += 'Importo detrazioni, parte decimale\n';}
		if (!document.forms[0].elements["fabbricabilieuro"].value.match (/^\d+$/))		{msg += 'Importo aree fabbricabili, parte intera\n';}
		if (!document.forms[0].elements["fabbricabilicent"].value.match (/^\d\d$/))		{msg += 'Importo aree fabbricabili, parte decimale\n';}
		if (!document.forms[0].elements["abitazioneeuro"].value.match (/^\d+$/))		{msg += 'Importo aree abitazione principale, parte intera\n';}
		if (!document.forms[0].elements["abitazionecent"].value.match (/^\d\d$/))		{msg += 'Importo aree abitazione principale, parte decimale\n';}
		if (!document.forms[0].elements["nfabbricati"].value.match (/^\d+$/))			{msg += 'numero di fabbricati deve essere un numero intero\n';}
		if (!document.forms[0].elements["altrieuro"].value.match (/^\d+$/))				{msg += 'Importo altri fabbricati, parte intera\n';}
		if (!document.forms[0].elements["altricent"].value.match (/^\d\d$/))			{msg += 'Importo altri fabbricati, parte decimale\n';}
//
// Solo se non ci sono problemi di formato I do the math
//	
		if (!msg)
		{
			totale += 1 * number(document.forms[0].elements["terrenieuro"].value, 		document.forms[0].elements["terrenicent"].value);
			totale += 1 * number(document.forms[0].elements["fabbricabilieuro"].value, 	document.forms[0].elements["fabbricabilicent"].value);
			totale += 1 * number(document.forms[0].elements["abitazioneeuro"].value, 	document.forms[0].elements["abitazionecent"].value);
			totale += 1 * number(document.forms[0].elements["altrieuro"].value, 		document.forms[0].elements["altricent"].value);
/*
	Riga seguente alterata il 10/2/2005, cambiato il meno in più
*/
			totale += 1 * number(document.forms[0].elements["detrazionieuro"].value, 	document.forms[0].elements["detrazionicent"].value);
			versamento = 1 * number(document.forms[0].elements["importoeuro"].value, 	document.forms[0].elements["importocent"].value);
/* 			
			if (Math.abs(totale - versamento) >= 0.01) 	{msg += 'Il totale non corrisponde\n';}
*/
			if (versamento < 0.01) {msg += 'Il totale del versamento non può essere nullo\n';}
		}
		break;
		
		case 2:
		if (!IsCFisc(document.forms[0].elements["userid"].value) && (!document.forms[0].elements["userid"].value.match(/^\d{11}$/))){msg += 'Codice fiscale/Partita IVA errati o assenti\n';}
		if (!document.forms[0].elements["attonumero"].value.match (/^\d{1,4}\/\d{1,4}$/))	{msg += 'Il numero atto deve essere formato da 4 cifre del protocollo, barra, 4 cifre di atto\n';}
		if ( document.forms[0].elements["attonumero"].value.match (/^0000\/0000$/))	{msg += 'Per favore, indicare il numero (4 cifre del protocollo, barra, 4 cifre di atto)\n';}
		if (!document.forms[0].elements["attodata"].value.match (/^[123]?[0-9]\/[01]?[0-9]\/[12]\d\d\d$/))	{msg += 'Il formato della data non è corretto\n';}
		if (! IsChecked(document.forms[0].elements["relativi"]))	{msg += 'Dovete scegliere tra ICI e Tarsu\n';}
	
		versamento = 1 * number(document.forms[0].elements["importoeuro"].value, 	document.forms[0].elements["importocent"].value);
		if (versamento < 0.01) {msg += 'Importo del versamento non può essere nullo\n';}
		break;
		
		case 3:
		msg = "ATTENZIONE! Tributo non implementato\n";
		break;
		
		case 4:
		recalc();
		if (document.forms[0].elements["importotmp"].value < 0.01) {msg += 'Importo nullo o negativo\n';} 
		break;
		
		case 5:
		if (document.forms[0].elements["causale"].value.match (/^\s*$/))	{msg += 'La causale deve essere specificata\n';}
		versamento = 1 * number(document.forms[0].elements["importoeuro"].value, 	document.forms[0].elements["importocent"].value);
		if (versamento < 0.01) {msg += 'Importo del versamento non può essere nullo\n';}
		break;
		
		case 6:
		msg = "ATTENZIONE! Tributo non implementato\n";
		break;
		
		case 7:
		if (document.forms[0].elements["causale"].value.match (/^\s*$/))	{msg += 'La causale deve essere specificata\n';}
		versamento = 1 * number(document.forms[0].elements["importoeuro"].value, 	document.forms[0].elements["importocent"].value);
		if (versamento < 0.01) {msg += 'Importo del versamento non può essere nullo\n';}
		break;
		
		case 8:
		if (document.forms[0].elements["causale"].value.match (/^\s*$/))	{msg += 'La causale deve essere specificata\n';}
		versamento = 1 * number(document.forms[0].elements["importoeuro"].value, 	document.forms[0].elements["importocent"].value);
		if (versamento < 0.01) {msg += 'Importo del versamento non può essere nullo\n';}
		break;
		
		default:
		msg = "ATTENZIONE! Tributo non implementato\n";
		break;
	}
	if (msg)
	{
		msg = "__________________________________\n\n  ATTENZIONE\n __________________________________\n\nI seguenti campi sono omessi o errati:\n\n" + msg;
		alert (msg);
		return false;
	}
	return true;
}
//-----------------------------------------------------------------------------------------
// verifica correttezza codice fiscale
//-----------------------------------------------------------------------------------------

function IsCFisc(cfins) {
  	var cf = cfins.toUpperCase();
	var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
	if (!cfReg.test(cf)) return false;
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	s = 0;
	for( i = 1; i <= 13; i += 2 )
		s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	for( i = 0; i <= 14; i += 2 )
		s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
		return false;
	return true;
}
//-----------------------------------------------------------------------------------------
// assembla in un numero razionale una parte intera e una parte decimale
//-----------------------------------------------------------------------------------------
function number(intero,decimale)
{
	return intero + "." + decimale;
}
//-----------------------------------------------------------------------------------------
// assembla in un numero razionale una parte intera e una parte decimale
//-----------------------------------------------------------------------------------------
function review()
{
	document.forms[0].action = "tos5.asp";
	document.forms[0].submit();
}
//-----------------------------------------------------------------------------------------
// Scrive un parametro hidden con la valorizzazione di un parametro indispensabile
//-----------------------------------------------------------------------------------------
function json(){
	document.write("<INPUT type='hidden' name='js' value='on'");
}
//-----------------------------------------------------------------------------------------
// A scopo di debug: visualizza tutti le coppie: nome/valore degli elementi di un form
//-----------------------------------------------------------------------------------------
function formdump(f){
		var j;
		var msg = "VALORI DEL FORM:\n";
		for(j=0; j<f.length; j++)
		{
			msg += f.elements[j].name;
			msg +=  " = ";
			msg += f.elements[j].value;
			msg += "\n";
		}
		alert(msg);		
}
//-----------------------------------------------------------------------------------------
// Verifica che un radio button sia checked
//-----------------------------------------------------------------------------------------
function IsChecked(f){
		var j, itis = false;
		for(j=0; j<f.length; j++)
		{
			if (f[j].checked) {itis = true;}
		}
		return itis;
}
//-----------------------------------------------------------------------------------------
// Verifica che un campo sia fofrmattato come euro
//-----------------------------------------------------------------------------------------
function EuroFormat(f){
		var j, itis = false;
		if (!f.value.match(/^\d+\.\d\d$/)) 
		{
			alert("Il valore in euro deve esssere espresso con sole cifre seguite da un punto decimale e quindi da due cifre decimali");
			return false;
		}
		recalc();
		return true;
}
//-----------------------------------------------------------------------------------------
// Verifica che un campo sia fofrmattato come euro
//-----------------------------------------------------------------------------------------
function DayFormat(f){
		var j, itis = false;
		if (!f.value.match(/^\d{1,3}$/)) 
		{
			alert("Il valore dei giorni di ritardo deve essere espresso mediante un numero intero e senza spazi iniziali o finali");
			return false;
		}
		recalc();
		return true;
}
//-----------------------------------------------------------------------------------------
// Verifica che un campo sia formattato come euro
// Modifica richiesta da Sauze. In sostanza eliminato ravvedimento per TARSU
//-----------------------------------------------------------------------------------------
function recalc(){
		var f = document.forms[0];
		var tributo		= 0;
		var interessi	= 0;
		var sanzioneV	= 0;
		var sanzioneR	= 0;
//
// Ravvedimento versamento
//
		document.forms[0].elements["sanz1"].value = arrotonda(0.025 * document.forms[0].elements["trib1"].value , 2);
		document.forms[0].elements["inte1"].value = arrotonda(0.010 * document.forms[0].elements["trib1"].value  * document.forms[0].elements["gior1"].value / 365, 2);		
		document.forms[0].elements["sanz2"].value = arrotonda(0.030 * document.forms[0].elements["trib2"].value , 2);
		document.forms[0].elements["inte2"].value = arrotonda(0.010 *  document.forms[0].elements["trib2"].value   * document.forms[0].elements["gior2"].value / 365, 2);		
		
		tributo 	+= parseFloat(document.forms[0].elements["trib1"].value);
		tributo 	+= parseFloat(document.forms[0].elements["trib2"].value);
		sanzioneV 	+= parseFloat(document.forms[0].elements["sanz1"].value);
		sanzioneV 	+= parseFloat(document.forms[0].elements["sanz2"].value);
		interessi	+= parseFloat(document.forms[0].elements["inte1"].value);
		interessi	+= parseFloat(document.forms[0].elements["inte2"].value);
	
		document.forms[0].elements["trib"].value = arrotonda(tributo, 2);
		document.forms[0].elements["inte"].value = arrotonda(interessi, 2);						
//
// 	ravvedimento dichiarazione
//

		document.forms[0].elements["sanz3"].value = arrotonda(0.0625 * document.forms[0].elements["trib3"].value , 2);
		document.forms[0].elements["sanz4"].value = arrotonda(0.1000 * document.forms[0].elements["trib4"].value , 2);
		document.forms[0].elements["sanz5"].value = arrotonda(0.1250 * document.forms[0].elements["trib5"].value , 2);
		document.forms[0].elements["sanz6"].value = arrotonda(0.2000 * document.forms[0].elements["trib6"].value , 2);
		
		sanzioneR	+= parseFloat(document.forms[0].elements["sanz3"].value);
		sanzioneR	+= parseFloat(document.forms[0].elements["sanz4"].value);
		sanzioneR	+= parseFloat(document.forms[0].elements["sanz5"].value);
		sanzioneR	+= parseFloat(document.forms[0].elements["sanz6"].value);

		document.forms[0].elements["sanz"].value 		= arrotonda(sanzioneV + sanzioneR, 2);				
		document.forms[0].elements["importotmp"].value 	= arrotonda(tributo + sanzioneV + sanzioneR + interessi, 2);
		return true;
}
//-----------------------------------------------------------------------------------------
// arrotondamento di un numero
//-----------------------------------------------------------------------------------------

function arrotonda(Number,howmanydigit){
	var commapos;									// posizione punto decimale
	var i;											// indice di un loop
	if ('number' != typeof Number) 	return NaN;		// non processo variabili che non sono numeri
	if (Number > 0)									// imposto l' arrotondamento al decimale corretto
	{
		Number += 5 * Math.pow (10, -1-howmanydigit);	
	}
	else
	{
		Number -= 5 * Math.pow (10, -1-howmanydigit);	// imposto l' arrotondamento all giusta cifra
	}
	var tmp = Number.toString(10);					// converto in stringa
	commapos= tmp.indexOf(".");						// trovo la posizione del punto decimale
	if (commapos > -1)								// C' è il punto decimale?
	{
		if ((1 + tmp.length  - commapos) > howmanydigit) 	// Stringa decimale maggiore del richiesto?
		{
			tmp = tmp.substring(0,commapos + howmanydigit + (howmanydigit>0? 1:0));
		}
		else
		{
			howmanydigit = howmanydigit + 1 - tmp.length + commapos;
			for (i=0; i <howmanydigit; i++ )
			{	
				tmp = tmp + '0';
			}	
		}	
	}
	else
	{
		tmp = tmp + '.';							// Numero intero
		for (i=0; i <howmanydigit; i++ )
		{	
			tmp = tmp + '0';
		}
	}
	return tmp;
}
//------------------------------------------------------------------------------------------------
// Funzione che ridirige sul gateway corretto a seconda delle modalità di pagamento prescelte.
// Valore 0 è carta di credito
//------------------------------------------------------------------------------------------------
function gateway()
{
	if (document.forms[0].elements["payment"].selectedIndex == 1)
	{	
		document.forms[0].action = "tos14.asp";
	}
	return (true);
}
//------------------------------------------------------------------------------------------------
// Apre popup
//------------------------------------------------------------------------------------------------
function docum(str) {
        searchWin = window.open(str,'docum','scrollbars=no,resizable=no,width=400,height=130,status=no,location=no,toolbar=yes');
}
//------------------------------------------------------------------------------------------------
// Passata da Raffy, apre popup
//------------------------------------------------------------------------------------------------
function warnme(arg) {
	if (arg.value.match(/\S+/)){ 
		alert("STAI INDICANDO UN CONTITOLARE: SEI SICURO DI AVER INDICATO A FIANCO DEL NOME ANCHE LA PERCENTUALE DI POSSESSO");
	}	
}
