// $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 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);
			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,5}$/))			{msg += 'Il numero atto deve essere compreso tra una e cinque cifre\n';}
		if (!document.forms[0].elements["attodata"].value.match (/^[123]?[0-9]\/1?[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 Tarsu, Icp, Tosap, Ipt\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 fofrmattato come euro
//-----------------------------------------------------------------------------------------
function recalc(){
		var f = document.forms[0];
		var parziale = 0;
		var coeff = 1;

		if (document.forms[0].elements["tipotributo"].selectedIndex == 0 ){coeff = 1.00;}else{coeff = 1.15;}	
		
		document.forms[0].elements["sanz1"].value = arrotonda(0.0375 * document.forms[0].elements["trib1"].value / coeff, 2);
		document.forms[0].elements["inte1"].value = arrotonda(0.0250 * document.forms[0].elements["trib1"].value / coeff * document.forms[0].elements["gior1"].value / 365, 2);
		
		document.forms[0].elements["sanz2"].value = arrotonda(0.0600 * document.forms[0].elements["trib2"].value / coeff, 2);
		document.forms[0].elements["inte2"].value = arrotonda(0.0250 *  document.forms[0].elements["trib2"].value / coeff  * document.forms[0].elements["gior2"].value / 365, 2);
		
		document.forms[0].elements["sanz3"].value = arrotonda(0.0625 * document.forms[0].elements["trib3"].value / coeff, 2);
		document.forms[0].elements["inte3"].value = arrotonda(0.0250 * document.forms[0].elements["trib3"].value / coeff  * document.forms[0].elements["gior3"].value / 365, 2);
		
		document.forms[0].elements["sanz4"].value = arrotonda(0.1000 * document.forms[0].elements["trib4"].value / coeff, 2);
		document.forms[0].elements["inte4"].value = arrotonda(0.0250 * document.forms[0].elements["trib4"].value / coeff  * document.forms[0].elements["gior4"].value / 365, 2);
		
		document.forms[0].elements["sanz5"].value = arrotonda(0.1250 * document.forms[0].elements["trib5"].value / coeff, 2);
		document.forms[0].elements["inte5"].value = arrotonda(0.0250 *  document.forms[0].elements["trib5"].value / coeff  * document.forms[0].elements["gior5"].value / 365, 2);
		
		document.forms[0].elements["sanz6"].value = arrotonda(0.2000 * document.forms[0].elements["trib6"].value / coeff, 2);
		document.forms[0].elements["inte6"].value = arrotonda(0.0250 * document.forms[0].elements["trib6"].value / coeff  * document.forms[0].elements["gior6"].value / 365, 2);
		
		parziale = 0
		parziale += parseFloat(document.forms[0].elements["trib1"].value);
		parziale += parseFloat(document.forms[0].elements["trib2"].value);
		parziale += parseFloat(document.forms[0].elements["trib3"].value);
		parziale += parseFloat(document.forms[0].elements["trib4"].value);
		parziale += parseFloat(document.forms[0].elements["trib5"].value);
		parziale += parseFloat(document.forms[0].elements["trib6"].value);
		if (document.forms[0].elements["tipotributo"].selectedIndex == 0 )
		{
			document.forms[0].elements["trib"].value = arrotonda(parziale, 2);
			document.forms[0].elements["eca"].value = "0.00" ;
		}	
		else
		{
			document.forms[0].elements["trib"].value = arrotonda(parziale / 1.15, 2);
			document.forms[0].elements["eca"].value = arrotonda(parziale * 0.13043478, 2) ;
		}
		
		parziale = 0
		parziale += parseFloat(document.forms[0].elements["sanz1"].value);
		parziale += parseFloat(document.forms[0].elements["sanz2"].value);
		parziale += parseFloat(document.forms[0].elements["sanz3"].value);
		parziale += parseFloat(document.forms[0].elements["sanz4"].value);
		parziale += parseFloat(document.forms[0].elements["sanz5"].value);
		parziale += parseFloat(document.forms[0].elements["sanz6"].value);
		document.forms[0].elements["sanz"].value = arrotonda(parziale, 2);
		
		parziale = 0
		parziale += parseFloat(document.forms[0].elements["inte1"].value);
		parziale += parseFloat(document.forms[0].elements["inte2"].value);
		parziale += parseFloat(document.forms[0].elements["inte3"].value);
		parziale += parseFloat(document.forms[0].elements["inte4"].value);
		parziale += parseFloat(document.forms[0].elements["inte5"].value);
		parziale += parseFloat(document.forms[0].elements["inte6"].value);
		document.forms[0].elements["inte"].value = arrotonda(parziale, 2);
		
		parziale = 0
		parziale += parseFloat(document.forms[0].elements["trib"].value);
		parziale -= parseFloat(document.forms[0].elements["versato"].value);
		parziale += parseFloat(document.forms[0].elements["eca"].value);				
		parziale += parseFloat(document.forms[0].elements["sanz"].value);	
		parziale += parseFloat(document.forms[0].elements["inte"].value);		
				
		document.forms[0].elements["importotmp"].value = arrotonda(parziale, 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;
}
