/* Menu di navigazione */
var fullvalues;
function showMenu(idmenu) {
	if (selectedMenu) {
		hideMenu();
	}
	selectedMenu=idmenu;
	document.getElementById("elencointerno"+idmenu).style.display='block';
	if (idmenu!=onMenu) document.getElementById("immagine"+idmenu).src="../img/cerchio_rosso.gif";
}
function hideMenu() {
	clearTimeout(closingTimeout);
	document.getElementById("elencointerno"+selectedMenu).style.display='none';
//	document.getElementById("immagine"+selectedMenu).src="img/cerchio_bianco.gif";
	selectedMenu=false;
}
function isOut() {
	clearTimeout(closingTimeout);
	if (selectedMenu!=onMenu) document.getElementById("immagine"+selectedMenu).src="../img/cerchio_bianco.gif";
	closingTimeout=setTimeout("hideMenu();", 1000);
}
function isOver() {
	clearTimeout(closingTimeout);
}

/* Box prestito */
function showBox() {
	document.getElementById("calcoff").style.display='none';
	document.getElementById("calcon").style.display='block';
}
function hideBox() {
	document.getElementById("calcon").style.display='none';
	document.getElementById("calcoff").style.display='block';
}

/* Calcola prestito */

function populateSelect(isCliente) {
  	var maxImporto;
  	var maxRate;
  	if (isCliente) {
  	  	maxImporto=26000;
  	  	maxRate=84;
  	} else {
  	  	maxImporto=20000;
  	  	maxRate=72;
  	}
  	var select=document.getElementById("importo");
  	select.options.length=0;
	
	var opt = document.createElement('OPTION');
	opt.value = 0;
	opt.text = "Seleziona importo";
	select.options.add(opt); 
		
  	for (var importo=maxImporto; importo>=2000; importo-=1000) {
  	  
		var opt = document.createElement('OPTION');
		opt.value = importo;
		opt.text = importo;
		select.options.add(opt); 
  	  
	}
  	var select=document.getElementById("rate");
  	select.options.length=0;
	
	var opt = document.createElement('OPTION');
	opt.value = 0;
	opt.text = "Seleziona rate";
	select.options.add(opt); 
	
  	for (var rate=maxRate;rate>=12;rate-=6) {
 		var opt = document.createElement('OPTION');
		opt.value = rate;
		opt.text = rate;
		select.options.add(opt); 
	}
	
	
	document.getElementById("valorerata").value="Rata mensile";
}
// Determina per approssimazioni successive il tasso applicato ad un
// finanziamento con pagamenti a scadenze regolari per una durata stabilita.
function Price(NroRate, ImpRata, ImpFin, MesiDilazione) { 
   	var Tasso = 0;
  	var errore = 0.5;   
   	while (errore > 0.0000001) {   
		if ( Rata(ImpFin, Tasso, NroRate, MesiDilazione) > ImpRata ) {
			Tasso = Tasso - errore;
		} else {
			Tasso = Tasso + errore;
		}  	   
		errore = errore / 2;   
   }   
   return Tasso;
}

function Rata(ImpErogato,Tasso,NroRate,MesiDilazione) {
	var TassoMth = parseFloat(Tasso / 12);
	var ImpRata;	
   	if (TassoMth == 0) {
    	ImpRata = ImpErogato / NroRate;
	} else {	  
		ImpRata = (ImpErogato * (1 + (TassoMth * MesiDilazione))) / (1 + ((1 - Math.pow((1 + TassoMth),(-NroRate + 1)) ) / TassoMth));
	}
	return ImpRata;
}

//Calcolo semplice TAN da TAEG (zero spese)
function Taeg2Tan(Taeg) {    
    return ( Math.pow((Taeg + 1),(1 / 12)) - 1 ) * 12;  
}

//Calcola TAN a partire da TAEG, ImpErogato e NroRate conosciuti
function Taeg2Tan2(Taeg, ImpErogato, NroRate, SpeseIstruttoria, SpeseIncassoRata) {    
    var Tasso = Taeg2Tan(Taeg);	
	var ImpRata = Rata(ImpErogato, Tasso, NroRate, 1) - SpeseIncassoRata;
	var ImpTot =  parseInt(ImpErogato) + parseInt(SpeseIstruttoria);
    var Taeg2Tan2 = Price(NroRate, ImpRata, ImpTot);
	return Taeg2Tan2;
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
		
//arrotonda ai 5 cent superiori
function Arrotonda05(Importo) {
	var intero;
	var decimale;
	var Arrotonda05;    
    intero = parseInt(Importo);
    decimale = (Importo - intero) * 100;    
    if ( Math.abs(decimale / 5 - parseInt(decimale / 5)) < 0.0001 ) {
       Arrotonda05 = intero + decimale / 100;
	} else {
       decimale = (parseInt((decimale) / 5) + 1) * 5
       Arrotonda05 = intero + decimale / 100;
	}	
	return Arrotonda05;    
}

// Calcolo assicurazione x PL
// CosaRitorna: 1 ritorna premio, 2 rata con assic
function CalcAssicPL(ImpFin, Tasso, NroRate, TipoPacchetto, CosaRitorna) {
   
	//pacchetto Base
	var eEURO = 1936.27;
	var QuotaRata = 150 / eEURO;
	var QuotaContr = 1000 / eEURO;
	var Tasse = 0.025;
	var TassoAss = 0.039;
	var TassoVita = (0.271 / 1000) * NroRate; 
	var CalcAssicPL;
  	
  	if (TipoPacchetto == 1) { //pacchetto Base 
    	TassoAss = TassoAss;
	} 
	if (TipoPacchetto == 2) { //pacchetto Base + Garante
    	TassoAss = TassoAss + 0.0186;
  	} 
	if (TipoPacchetto == 3) { //pacchetto Base + Perdita Lavoro
    	TassoAss = TassoAss + 0.0117;
	}    
  
  	var TassoFinito = TassoAss + (TassoVita * (1 + Tasse));
  
  	var NewImpRata = ((Tasso / 12) / (1 - Math.pow((Tasso / 12 + 1), NroRate * (-1))));
	NewImpRata /= (1 - NroRate * TassoFinito * (Tasso / 12) * (1 / (1 - Math.pow((Tasso / 12) + 1, NroRate * (-1)) )));
	NewImpRata *= (ImpFin + (QuotaContr * (1 + Tasse)) + (QuotaRata * NroRate * (1 + Tasse)));
  	var NewPremio = (((NewImpRata * NroRate * TassoVita) + ((QuotaRata * NroRate) + QuotaContr)) * (1 + Tasse)) + (NewImpRata * NroRate * TassoAss);

  	if (CosaRitorna == 1) {
     	CalcAssicPL = NewPremio; // premio
  	} else {
	  	CalcAssicPL = NewImpRata; //rata con assicurazione
  	}
  
  	return CalcAssicPL;

}

function calcolaRata() {
  	var ImpErogato=document.getElementById("importo").options[document.getElementById("importo").selectedIndex].value;
  	var NroRate=parseInt(document.getElementById("rate").options[document.getElementById("rate").selectedIndex].value);
  	
	if (parseInt(ImpErogato) != 0 && NroRate != 0) {
	
		//var Taeg = parseFloat(9.99/100);
		var Taeg = parseFloat(11.99/100);
		var SpeseIncassoRata = 1.50;
		var SpeseIstruttoria = 125;
		var MesiDilazione = 1;   
		var ImpTot =  parseInt(ImpErogato) + parseInt(SpeseIstruttoria);
		//var Tasso = Taeg2Tan2(Taeg, ImpErogato, NroRate, SpeseIstruttoria, SpeseIncassoRata);
    	var Tasso = Taeg2Tan(Taeg);
		//var ImpRata = Rata(parseInt(ImpErogato), Tasso, NroRate, 1) - SpeseIncassoRata;
		var correzione;
		switch (NroRate) {
			case 84: correzione = 0.5; break;
			case 78: if(ImpErogato<22000) correzione = 0.5; else correzione = 0.45; break;
			case 72: correzione = 0.5; break;
			case 54: if(ImpErogato<22000) correzione = 0.45; else correzione = 0.4; break;
			case 48: if(ImpErogato<22000) correzione = 0.4; else correzione = 0.45; break;
			case 42: if(ImpErogato<22000) correzione = 0.4; else correzione = 0.4; break;
			case 36: if(ImpErogato<22000) correzione = 0.4; else correzione = 0.45; break;
			case 18: correzione = 0.55; break;
			case 12: if(ImpErogato<23000) correzione = 0.7; else correzione = 0.65; break;
			default: correzione = 0.45; break;
		}
		var ImpRataAssic = CalcAssicPL(parseInt(ImpErogato), Tasso, NroRate, 1, 2);
		document.getElementById("valorerata").value = Arrotonda05(ImpRataAssic - correzione).toFixed(2) + " €";
		
	}
}

/* calcola prestito per altro box */

function populateSelect1(isCliente) {
  	var maxImporto;
  	var maxRate;
  	if (isCliente) {
  	  	maxImporto=26000;
  	  	maxRate=84;
  	} else {
  	  	maxImporto=20000;
  	  	maxRate=72;
  	}
  	var select=document.getElementById("importo1");
  	select.options.length=0;

  	for (var importo=maxImporto; importo>=1000; importo-=1000) {
  	  
		var opt = document.createElement('OPTION');
		opt.value = importo;
		opt.text = importo;
		select.options.add(opt); 
  	  
	}
  	var select=document.getElementById("rate1");
  	select.options.length=0;

  	for (var rate=maxRate;rate>=12;rate-=6) {
 		var opt = document.createElement('OPTION');
		opt.value = rate;
		opt.text = rate;
		select.options.add(opt); 
	}
	
	calcolaRata1();
}

function calcolaRata1() {
  	var capitale=document.getElementById("importo1").options[document.getElementById("importo1").selectedIndex].value;
  	var rate=document.getElementById("rate1").options[document.getElementById("rate1").selectedIndex].value;
  	
  	var rata=Math.round(capitale*Math.pow(1+0.059/12,rate)*(0.059/12/(Math.pow(1+0.059/12,rate)-1))*100)/100;
  	document.getElementById("valorerata1").value=rata;
}

/* onLoad Function*/

function onLoadFunction () {
//	if (onMenu!=0) document.getElementById("immagine"+onMenu).src="../img/cerchio_rosso.gif";'
}

// start scroller code

var ScrollerBox = {
			
		container:null,
		scrollable:null,
		boxes:null,
		
		interval:null,
		position:0,
		
		boxesWidth:null,
		containerWidth:null,
		
		speed:10,
		frameRate:20,
		availableSteps:null,
		
		debugTarget:null,
		
		init: function (containerId, movingBoxId,boxesClass) {
			this.container=document.getElementById(containerId);
			this.element=document.getElementById(movingBoxId);
			this.element.style.marginLeft="0px";
			this.container.style.overflow="hidden";
			this.containerWidth=this.container.offsetWidth;
			this.boxes=getElementsByClassName(boxesClass);
			this.speed=10;
			if (this.boxes.length>0) {
				this.boxesWidth=this.boxes[0].offsetWidth;
				this.availableSteps=Math.round(this.containerWidth/this.boxesWidth);
			}
		},
		
		moveLeft:function(totale) {
			if (this.position>-this.boxes.length+this.availableSteps) {
				this.position--;
				if (!this.interval) {
					this.interval=setInterval("ScrollerBox.doMotion("+totale+");",1000/this.frameRate)
				}			
			}
		},
		moveRight:function(totale) {
			if (this.position<0) {
				this.position++;
				if (!this.interval) {
					this.interval=setInterval("ScrollerBox.doMotion("+totale+");",1000/this.frameRate)
				}
			}
		},
		doMotion:function(totale) {
			var curMargin=parseInt(this.element.style.marginLeft);
			var curPos=curMargin/this.boxesWidth;
			var direction=((curPos-this.position)>0)?1:-1;
			var diff=Math.abs(Math.abs(curMargin)-Math.abs(this.position*this.boxesWidth));
			if (this.debugTarget) {
				this.debugTarget.innerHTML = "Margine attuale: " + curMargin + "<br />Positione attuale: " + curPos + "<br /> Posizione obiettivo: " + this.position + "<br /> Direzione: " + direction + "<br /> Differenziale movimento: " + diff;
			}
			if (this.speed>diff) {
				this.stopMove(totale);
				this.element.style.marginLeft=(this.position*this.boxesWidth)+"px";
			} else {
				this.element.style.marginLeft=(curMargin-(direction*this.speed))+"px";
			}
		},
		
		stopMove:function(totale) {
			clearInterval(this.interval);
			this.interval=null;
			posizione=this.position;
			totale=-totale
			if (posizione>totale) {
					document.getElementById('freccia_sx').src='../img/freccia_blu_sx.gif';
					document.getElementById('freccia_dx').src='../img/freccia_blu_dx.gif';
			}
			if (posizione==0) {
					document.getElementById('freccia_sx').src='../img/frca_grigia_sx.gif';
					document.getElementById('freccia_dx').src='../img/freccia_blu_dx.gif';
			}			
			if (posizione==totale) {
					document.getElementById('freccia_sx').src='../img/freccia_blu_sx.gif';
					document.getElementById('freccia_dx').src='../img/frca_grigia_dx.gif';
			}
		}

	}
var ScrollerBoxVert = {
			
		container:null,
		scrollable:null,
		boxes:null,
		
		
		interval:null,
		position:0,
		
		boxesHeight:null,
		containerHeight:null,
		
		speed:10,
		frameRate:20,
		availableSteps:null,
		
		debugTarget:null,
		
		init: function (containerId, movingBoxId,boxesClass) {
			this.container=document.getElementById(containerId);
			this.element=document.getElementById(movingBoxId);
			this.element.style.marginTop="0px";
			this.container.style.overflow="hidden";
			this.containerHeight=this.container.offsetHeight;
			this.boxes=getElementsByClassName(boxesClass);
			this.speed=10;
			if (this.boxes.length>0) {
				this.boxesHeight=this.boxes[0].offsetHeight;
				this.availableSteps=Math.round(this.containerHeight/this.boxesHeight);
			}
		},
		
		moveTop:function(totale) {
			if (this.position>-this.boxes.length+this.availableSteps) {
				this.position--;
				if (!this.interval) {
					this.interval=setInterval("ScrollerBoxVert.doMotion("+totale+");",1000/this.frameRate)
				}
			}
		},
		moveBottom:function(totale) {
			if (this.position<0) {
				this.position++;
				if (!this.interval) {
					this.interval=setInterval("ScrollerBoxVert.doMotion("+totale+");",1000/this.frameRate)
				}
			}
		},
		doMotion:function(totale) {
			var curMargin=parseInt(this.element.style.marginTop);
			var curPos=curMargin/this.boxesHeight;
			var direction=((curPos-this.position)>0)?1:-1;
			var diff=Math.abs(Math.abs(curMargin)-Math.abs(this.position*this.boxesHeight));
			if (this.debugTarget) {
				this.debugTarget.innerHTML = "Margine attuale: " + curMargin + "<br />Positione attuale: " + curPos + "<br /> Posizione obiettivo: " + this.position + "<br /> Direzione: " + direction + "<br /> Differenziale movimento: " + diff;
			}
			if (this.speed>diff) {
				this.stopMove(totale);
				this.element.style.marginTop=(this.position*this.boxesHeight)+"px";
			} else {
				this.element.style.marginTop=(curMargin-(direction*this.speed))+"px";
			}
		},
		
		stopMove:function(totale) {
			clearInterval(this.interval);
			this.interval=null;
			posizione=this.position;
			totale=-totale
			if (posizione>totale) {
					document.getElementById('freccia_su').src='../img/frca_up_rossa.gif';
					document.getElementById('freccia_giu').src='../img/frca_dwn_rsa.gif';
			}
			if (posizione==0) {
					document.getElementById('freccia_su').src='../img/frca_up_gria.gif';
					document.getElementById('freccia_giu').src='../img/frca_dwn_rsa.gif';
			}			
			if (posizione==totale) {
					document.getElementById('freccia_su').src='../img/frca_up_rossa.gif';
					document.getElementById('freccia_giu').src='../img/frca_dwn_gria.gif';
			}
		}
		

	}	
	
	
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};


// end scroller


// inizion calcolatore Budget

function budget1() {
	
	var entrate = Math.abs(budget.entrate1.value) + Math.abs(budget.entrate2.value);
	
	var uscite = Math.abs(budget.uscite1.value) + Math.abs(budget.uscite2.value) + Math.abs(budget.uscite3.value) + Math.abs(budget.uscite4.value) + Math.abs(budget.uscite5.value) +  + Math.abs(budget.uscite6.value)  + Math.abs(budget.uscite7.value)  + Math.abs(budget.uscite8.value)  + Math.abs(budget.uscite9.value)  + Math.abs(budget.uscite10.value);
	
	var differenza = entrate - uscite;
	
	document.budget.usciteTot.value= uscite;
	document.budget.entrateTot.value= entrate;
	document.budget.differenza.value= differenza;
}

//Tracking code URL parameter check
function getURLParameter(name) {
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}

// note

function selezione() {
	box = eval("document.prestito.note");
	box.checked = !box.checked;
}

var alreadysubmitted = false;
	function controlloForm()
	{
		if(alreadysubmitted == false)
		{
			var privacy = document.prestito.note.checked; // controllo a parte
			
			
			// controlla se è stato ceccato la privacy
			if(privacy == false)
			{
				alert('E necessario leggere le note informative.');
				window.open('../servizio/note.htm','main','width=550,height=300,top=100,left=100,status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=yes');
				selezione();
				return false;				
			}
			alreadysubmitted = true;
			//document.prestito.submit();
			if ((getCookie('cid')!='') && (getCookie('cid')!=null))
				{
				 if(getCookie('csrc')=="prestiti")
					 {
					fullval_prest="?id="+getCookie('cid')+"&"+getCookie('cfield2')+"="+getCookie('cfieldans')
					 }
				}

			document.location.href="/ids/gohttpsbind.cgi?/italy/citifinancial/forms/richiedi_1.htm"+fullval_prest
			return true;
		}
		else
		{
			
			return false;
		}
	}

	function getCookie(showIcard)
	{
		if (document.cookie.length>0)
		  {
           var fst;
              c_start=document.cookie.indexOf(showIcard + "=")
			  fst = document.cookie.indexOf(showIcard);

					var sequ = document.cookie.substring(c_start+showIcard.length,c_start+showIcard.length+1);
					if(sequ!="=")
						 {
							 return null;
						 }

					 else
						{
							c_start=c_start + showIcard.length+1 
							c_end=document.cookie.indexOf(";",c_start)

							if (c_end==-1) c_end=document.cookie.length
							return unescape(document.cookie.substring(c_start,c_end))
		 				 }
		  }

		  else
		  return null;
	} 

	