// JavaScript Document
function Browser() {
  var ua, s, i;
  this.isIE    = false;
  this.isNS    = false;
  this.version = null;
  ua = navigator.userAgent;
  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function ChangeCard(numChange,numArray){
	for( var i = 1; i <= numArray; i++){
		document.getElementById('change'+i).style.display = 'none';
	}
	document.getElementById('scheda_label').style.backgroundImage = 'url(immagini/bg_label_'+numChange+'.gif)';
	document.getElementById('change'+numChange).style.display = 'block';
}

function CreateXmlHttpReq(handler) {
  var xmlhttp = null;
  try {
    xmlhttp = new XMLHttpRequest();
  } catch(e) {
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  xmlhttp.onreadystatechange = handler;
  return xmlhttp;
}

function myHandlerTrovaCaseDX() {
    if (myRequest.readyState == 4 && myRequest.status == 200) {
		var e = document.getElementById("trova_casa");
    	e.innerHTML = myRequest.responseText;
    }
}

function selectTrovaCaseDX(vars) {
	//alert(vars);
	myRequest = CreateXmlHttpReq(myHandlerTrovaCaseDX);
    myRequest.open("GET", "ajaxPHP/trovaCase.php"+vars);
    myRequest.send(null);
}

function myHandlerComuniDX() {
    if (myRequest.readyState == 4 && myRequest.status == 200) {
		var e = document.getElementById("comuni");
    	e.innerHTML = myRequest.responseText;
    }
}

function selectComuniDX(vars) {
	//alert(vars);
	myRequest = CreateXmlHttpReq(myHandlerComuniDX);
    myRequest.open("GET", "ajaxPHP/trovaComuni.php"+vars);
    myRequest.send(null);
}

/**/
function openScheda(divId){
	if (document.getElementById(divId).style.display == "none"){
		document.getElementById(divId).style.display = "block";
	} else {
		document.getElementById(divId).style.display = "none";
	}
}

function ConvalidaContattiImmobileForm(lingua) {
	 var index = parseInt(lingua);
	 var nome = document.form.nome.value;
	 nomeArray = new Array("Il campo nome &eacute; obbligatorio", "Name is required");
	 var cognome = document.form.cognome.value;
	 cognomeArray = new Array("Il campo cognome &eacute; obbligatorio", "Surname is required");
     var email = document.form.email.value;
	 emailArray = new Array("Il campo email non &eacute; corretto", "Email is incorrect");
	 var telefono = document.form.telefono.value;
	 telefonoArray = new Array("Il campo telefono &eacute; obbligatorio", "Telephone is required");
	 var messaggio = document.form.messaggio.value;
	 messaggioArray = new Array("Il campo messaggio &eacute; obbligatorio", "Message is required");
     var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
        if ((nome == "") || (nome == undefined)) {
		   var e = document.getElementById("alertBox");
    	   e.innerHTML = nomeArray[index];
		   setTimeout("doRefreshConvalida();", 3000);
        } else if ((cognome == "") || (cognome == undefined)) {
           var e = document.getElementById("alertBox");
    	   e.innerHTML = cognomeArray[index];
		   setTimeout("doRefreshConvalida();", 3000);
        } else if ((telefono == "") || (telefono == undefined)) {
           var e = document.getElementById("alertBox");
    	   e.innerHTML = telefonoArray[index];
		   setTimeout("doRefreshConvalida();", 3000);
        } else if ((!email_reg_exp.test(email) || (email == "") || (email == undefined))) {
           var e = document.getElementById("alertBox");
    	   e.innerHTML = emailArray[index];
		   setTimeout("doRefreshConvalida();", 3000);
        } else if ((messaggio == "") || (messaggio == undefined)) {
          var e = document.getElementById("alertBox");
    	  e.innerHTML = messaggioArray[index];
		  setTimeout("doRefreshConvalida();", 3000);
        } else {
           document.form.submit();
        }
}

function ConvalidaRichiediImmobileForm(lingua) {
	 var index = parseInt(lingua);
	 var provincia = document.form.provincia.value;
	 provinciaArray = new Array("Il campo provincia &eacute; obbligatorio", "State is required");
	 var comune = document.form.comune.value;
	 comuneArray = new Array("Il campo comune &eacute; obbligatorio", "City is required");
	 var tipologia = document.form.tipologia.value;
	 tipologiaArray = new Array("Il campo tipologia &eacute; obbligatorio", "Type is required"); 
	 var nome = document.form.nome.value;
	 nomeArray = new Array("Il campo nome &eacute; obbligatorio", "Name is required");
	 var cognome = document.form.cognome.value;
	 cognomeArray = new Array("Il campo cognome &eacute; obbligatorio", "Surname is required");
     var email = document.form.email.value;
	 emailArray = new Array("Il campo email non &eacute; corretto", "Email is incorrect");
	 var telefono = document.form.telefono.value;
	 telefonoArray = new Array("Il campo telefono &eacute; obbligatorio", "Telephone is required");
     var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
        if ((provincia == "") || (provincia == undefined)) {
		   var e = document.getElementById("alertBox");
    	   e.innerHTML = provinciaArray[index];
		   setTimeout("doRefreshConvalida();", 3000);
        } else if ((comune == "") || (comune == undefined)) {
		   var e = document.getElementById("alertBox");
    	   e.innerHTML = comuneArray[index];
		   setTimeout("doRefreshConvalida();", 3000);
        } else if ((tipologia == "") || (tipologia == undefined)) {
		   var e = document.getElementById("alertBox");
    	   e.innerHTML = tipologiaArray[index];
		   setTimeout("doRefreshConvalida();", 3000);
        } else if ((nome == "") || (nome == undefined)) {
		   var e = document.getElementById("alertBox");
    	   e.innerHTML = nomeArray[index];
		   setTimeout("doRefreshConvalida();", 3000);
        } else if ((cognome == "") || (cognome == undefined)) {
           var e = document.getElementById("alertBox");
    	   e.innerHTML = cognomeArray[index];
		   setTimeout("doRefreshConvalida();", 3000);
        } else if ((telefono == "") || (telefono == undefined)) {
           var e = document.getElementById("alertBox");
    	   e.innerHTML = telefonoArray[index];
		   setTimeout("doRefreshConvalida();", 3000);
        } else if ((!email_reg_exp.test(email) || (email == "") || (email == undefined))) {
           var e = document.getElementById("alertBox");
    	   e.innerHTML = emailArray[index];
		   setTimeout("doRefreshConvalida();", 3000);
        } else {
           document.form.submit();
        }
}

function doRefreshConvalida() { 
  //alert("OK");
  var e = document.getElementById("alertBox");
  e.innerHTML = "";
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


function rata_CP(bf_cr,bf_ir,bf_ar,bf_nr) {
	return (bf_cr * A_N_I(bf_ir / bf_nr / 100,bf_nr * bf_ar) + .5)
}
function A_N_I(bf_ip,bf_np) {
	return (bf_ip * Math.pow((1 + bf_ip), bf_np)) / (Math.pow((1 + bf_ip), bf_np) - 1)
}

function calcolaMutuo() {
	var tipo = getCheckedValue(document.form.tipo_tasso)	// tipo di tasso
	if (tipo == 0){
		var titolotasso = "Tasso fisso";
	} else if (tipo == 1){
		var titolotasso = "Tasso variabile";
	}
	var c = parseFloat("0"+document.form.M.value);		// capitale
	var i = parseFloat("0"+document.form.tasso.value);	// interesse
	var a = parseFloat("0"+document.form.durata.value);			// numero anni
	var n = parseFloat("0"+document.form.cadenza.value);	// N.rate all'anno
	var r = Math.floor(rata_CP(c,i,a,n));
	var rM = r
	
	if(n==12) { freq="mensile" };
	if(n==6) { freq="bimestrale" };
	if(n==4) { freq="trimestrale" };
	if(n==2) { freq="semestrale" };
	//document.form.rata.value=r;
	// creazione della pagina di stampa
	var riga = "";
	var iniziotabella = "";
	var finetabella = "";
	var ti = 0; 
	var tr = 0; 
	var tk = 0;
	var m = n*a;								// numero rate
	for (z=1;z<=m;z++) {
		q = Math.floor((c*i/n/100)+.5)      // quota interessi
		k = r - q                           // quota capitale
		c = c - k                           // capitale residuo
		if (z==m) {							// ultima rata
			k = k + c
			r = r + c
			c = 0
		}
		ti += q                         // totale interessi
		tr += r							// totale rate
		tk += k							// totale capitale
		/**/if (tipo == 0){
			iniziotabella = "<div id=\"tabella_mutuo\"><table>";
			riga += "<tr><td><strong>"+z+"&ordf; Rata  "+r+" &euro;</strong></td><td>quota interessi "+q+" &euro;</td><td>quota capitale "+k+" &euro;</td><td>debito residuo "+c+" &euro;</td></tr>";
			finetabella = "</table></div>";
		}
	}
	var rigaTitolo = "<div style='color:#CC0000; text-transform:uppercase; font-weight:bold; padding:30px 0px 0px 0px;'>Calcolo su capitale finanziato di "+tk+" &euro; a "+titolotasso+"</div><div style='margin:0px; padding:0px; height:1px; width:100%; background-color:#CC0000;'><img src='immagini/shim.gif' height='1' alt=' ' title=''/></div><div style='margin:0px; padding:0px; height:3px; width:20%; background-color:#CC0000;'><img src='immagini/shim.gif' height='3' alt=' ' title=''/></div><div style=\"padding:20px 0px;\"><strong>Numero di anni: "+a+"<br />Frequenza della rata: "+freq+"<br />Tasso d&acute;interesse: "+i+"%<br /><br />Importo rata: "+rM+" &euro;<br />";
	if (tipo == 0){
		rigaTitolo += "Ultima rata: "+r+" &euro;<br /><br />Totale interessi "+ti+" &euro;<br />Totale delle rate "+tr+" &euro;";
		rigaTitolo += "<br /><br />N.B. Nel tasso fisso, l&acute;interesse pagato dal cliente viene pattuito all&acute;inizio del rapporto e viene mantenuto inalterato per tutta la durata del mutuo.";
		var rigaAmmortamento = "<div style='color:#CC0000; text-transform:uppercase; font-weight:bold; margin-top:10px;'>Piano d&acute;ammortamento</div><div style='margin:0px; padding:0px; height:1px; width:100%; background-color:#CC0000;'><img src='immagini/shim.gif' height='1' alt=' ' title=''/></div><div style='margin:0px; padding:0px; height:3px; width:20%; background-color:#CC0000;'><img src='immagini/shim.gif' height='3' alt=' ' title=''/></div>";
	} else if (tipo == 1) {
		rigaTitolo += "<br /><br />N.B. Il tasso variabile muta con il trascorrere del tempo, essendo collegato ad un indice variabile, come l&acute;Euribor o LIBOR o altri, al quale viene aggiunta una ulteriore percentuale, chiamata &quot;spread&quot;.<br /><br />Di solito il Tasso viene aggiornato semestralmente o annualmente, indipendentemente dalla rateizzazione scelta sulla base delle variazioni dell&acute;indice di riferimento avvenute nel corso del rapporto.";
		var rigaAmmortamento = "";
	}
	rigaTitolo += "</strong></div>"
	riga = rigaTitolo + rigaAmmortamento + iniziotabella + riga + finetabella;

	var e = document.getElementById("mutuoBox");
    e.innerHTML = riga;
}

function cambiaTasso(fisso,variabile) { 
  //alert("OK");
  if (getCheckedValue(document.form.tipo_tasso) == 0){
	document.form.tasso.value = fisso;
  } else if (getCheckedValue(document.form.tipo_tasso) == 1){
	document.form.tasso.value = variabile;  
  }
}

