var z1;
var z2;
var z3;
var z4;

function init(v,d) {
	try{
		document.getElementById('flash_info').innerHTML = '<marquee behavior="scroll" direction="'+d+'" scrollamount="1" scrolldelay="'+v+'" onMouseOver="this.stop()" onMouseOut="this.start()">'+document.getElementById('flash_info').innerHTML+'</marquee>';
	}catch(e){}
	try{	
		document.getElementById('email_newsletter').onclick = function() { document.getElementById('email_newsletter').value = ""; }
	}catch(e){}
	if(document.getElementById("z1")) {
		z1 = new fx.Combo('z1', {height: true, opacity: true, duration: 1000}); z1.hide('height');
	}
	
	if(document.getElementById("z2")) {
		z2 = new fx.Combo('z2', {height: true, opacity: true, duration: 1000}); z2.hide('height');
	}
	
	if(document.getElementById("z3")) {
		z3 = new fx.Combo('z3', {height: true, opacity: true, duration: 1000}); z3.hide('height');
	}
	
	if(document.getElementById("z4")) {
		z4 = new fx.Combo('z4', {height: true, opacity: true, duration: 1000}); z4.hide('height');
	}
}

function zoom(id) {
	if(id == "z1") {
		if(document.getElementById("z2")) { z2.hide('height'); }
		if(document.getElementById("z3")) { z3.hide('height'); }
		if(document.getElementById("z4")) { z4.hide('height'); }
		z1.toggle();
	}
	if(id == "z2") {
		if(document.getElementById("z1")) { z1.hide('height'); }
		if(document.getElementById("z3")) { z3.hide('height'); }
		if(document.getElementById("z4")) { z4.hide('height'); }
		z2.toggle();
	}
	if(id == "z3") {
		if(document.getElementById("z1")) { z1.hide('height'); }
		if(document.getElementById("z2")) { z2.hide('height'); }
		if(document.getElementById("z4")) { z4.hide('height'); }
		z3.toggle();
	}
	if(id == "z4") {
		if(document.getElementById("z1")) { z1.hide('height'); }
		if(document.getElementById("z2")) { z2.hide('height'); }
		if(document.getElementById("z3")) { z3.hide('height'); }
		z4.toggle();
	}
}

function page(catid,op) {
	new Ajax.Updater('page', 'index.php', {parameters:'catid=' + encodeURIComponent(catid) + '&open=' + op, asynchronous:true, evalScripts:true});
}

function addToFavorites() {
	if (document.all) { window.external.AddFavorite(location.href, document.title); }
	else { alert('Vous pouvez faire CTRL + D pour ajouter cette page dans vos signets, ou favoris.') }
}

function retour() {
	history.go(-1);	
}

function OpenWin(URL,width,height,nom) {
	window.open(URL,nom,"toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width=" + width + ",height=" + height + ",resizable=no");
}

function OpenWin2(URL,width,height,nom) {
	window.open(URL,nom,"toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width=" + width + ",height=" + height + ",resizable=yes");
}

function DisplayZoomPopUp(url) {
	window.open(url,'Zoom','height=500,width=500,left=0,top=0,scrollbars=yes');
}

var InfoDev = {
   /*
    *
    * Dépendences : Mootools 1.2
    * Auteur : Steven TITREN   -   stitren@ekinoxe.fr
    *
    * Ce fichier est la propriété de la société Ekinoxe ORIGIN - www.ekinoxe.fr
    * Ce fichier n'est en aucun cas libre de droit, vous n'avez pas l'autorisation de 
    * le diffuser ou de le réutiliser dans un projet exterieur.
    *
    */
    'auteur'      : 'Steven Titren',
    'email_pro'   : 'stitren@ekinoxe.fr',
    'email_perso' : 'contact@steven-titren.com' 
}

/* Simulateur de prêt */

function remplace(expr,a,b) {
	var i=0
	while (i!=-1) {
		i=expr.indexOf(a,i);
		if (i>=0) {
			expr=expr.substring(0,i)+b+expr.substring(i+a.length);
			i+=b.length;
		}
	}
	return expr
}

function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) {
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
} 

function calcul(form) {	
	taux = remplace(form.taux.value,",",".");
	montant = remplace(form.montant.value,",",".");
	duree = remplace(form.duree.value,",",".");
	
	if(montant == "" || !IsNumeric(montant)) {
		document.getElementById('resultat').innerHTML = "Veuillez indiquer le montant total du crédit";
	} else if(duree == "" || !IsNumeric(duree)) {
		document.getElementById('resultat').innerHTML = "Veuillez indiquer la durée du crédit";
	} else if(taux == "" || !IsNumeric(taux)) {
		document.getElementById('resultat').innerHTML = "Veuillez indiquer le taux";
	} else {
		tm = taux/1200;
		ho = montant*tm;
		baun = 1+tm;
		badeux = Math.pow(baun,-duree);
		ba = 1 - badeux;
		mensualite = Math.round(ho*100/ba)/100;
		cout = Math.round((mensualite*duree-montant)*100)/100;
		
		mensualite = remplace(mensualite.toString(),".",",");
		cout = remplace(cout.toString(),".",",");
		
		document.getElementById('resultat').innerHTML = 'Montant de votre &eacute;ch&eacute;ance : <span>'+mensualite+' &euro;</span> par mois <br/><br/> Co&ucirc;t total du pr&ecirc;t : <span>'+cout+' &euro;</span>';
	}
}

function calcul_notaire(form){
	montant = remplace(form.montant.value,",",".");
	var lth = form.temps.length;
	var temp = "";
	for(i=0;i<lth;i++){
		if(form.temps[i].checked==true){
			 temp = form.temps[i].value;
		} 
	}
	if(montant == "" || !IsNumeric(montant)) {
		document.getElementById('resultat').innerHTML = "Veuillez indiquer le montant total de votre achat";
	}else{
		switch(temp){
			case "neuf":
				var taux = 2.5/100;
				var frais_fixe = 1300;
				frais = montant * taux + frais_fixe;
				frais = Math.round(frais);
				frais = remplace(frais.toString(),".",",");
				document.getElementById('resultat').innerHTML = 'Montant <span>estimé</span> de vos frais de notaire : <span>'+frais+' &euro;</span><br/><br/>Le montant est une provision donnée à titre indicatif';
			break;
			case "ancien":
				var taux = 6.16/100;
				var frais_fixe = 1657;
				frais = montant * taux + frais_fixe;
				frais = Math.round(frais);
				frais = remplace(frais.toString(),".",",");
				document.getElementById('resultat').innerHTML = 'Montant <span>estimé</span> de vos frais de notaire : <span>'+frais+' &euro;</span><br/><br/>Le montant est une provision donnée à titre indicatif';
			break;
			default:
				document.getElementById('resultat').innerHTML = "Veuillez indiquer si l'achat concerne un bien neuf ou ancien";
			break;
		}
	}
}

/**************************/

var requete = null;

function creerRequete() {
    try {
        requete = new XMLHttpRequest();
    } catch (microsoft) {
        try {
            requete = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch(autremicrosoft) {
            try {
                requete = new ActiveXObject('Microsoft.XMLHTTP');
            } catch(echec) {
                requete = null;
            }
        }
    }
    if(requete == null) {
        alert('Impossible de créer l\'objet requête,\nVotre navigateur ne semble pas supporter les object XMLHttpRequest.');
    }
}

function page(catid,op) {
	creerRequete();
	var url = 'scripts/page.php?id='+catid+'&open='+op;
	requete.open('GET', url, true);
	
	requete.onreadystatechange = function() {
		if(requete.readyState == 4) {
			if(requete.status == 200) {
				document.getElementById('page').innerHTML = requete.responseText;
			}
		}
	};
	
	requete.send(null);
}