var taux, tag_taux = "";

// setAge
function setAge(v)
{
	$('age').value = v,
	$('t_age').innerHTML = v + " ans"
	doSimulation(document.forms[0]);
}
	
// setMontant
function setMontant(v)
{
	$('montant').value = v,
	$('t_montant').innerHTML = v + " &euro;"
	doSimulation(document.forms[0]);
}

// setRevenu
function setRevenu(v)
{
	v = Math.round(v) * 100;
	$('revenu').value = v, maxValue = "";
	if(v == 4000)
		maxValue = "+ de ";
	
	$('t_revenu').innerHTML = maxValue + v + " &euro;/mois";
	
	doSimulation(document.forms[0]);
}

// checkInteger
function checkInteger(fld)
{
	if (!fld.value.length || isNaN(parseInt(fld.value)))
	{
		fld.value = "";
		return false;
	}
	else
	{
		fld.value = parseInt(fld.value);
		return true;
	}
}

// zeroControls
function zeroControls(frm)
{
	document.getElementById("recommandation").innerHTML = "";
	for (var i=1; i<=3; i++)
	{
		document.getElementById("mensualite_" + i).innerHTML = "";
		frm.mensualite[i-1].checked = false;
	}
}

// informer SiteCatalyst pour la première simulation
var simulationCount = 0;
function doSiteCatalyst() {
	if (++simulationCount > 1) return;
	var s = s_gi(s_account);
	s.linkTrackVars = "eVar4,events"; 
	s.linkTrackEvents = "event3";
	s.eVar4 = "Crédit renouvelable";
	s.events = "event3";
	mboxLoadSCPlugin(s);
	s.tl(this,'o','Simulation');
}

// doSimulation
function doSimulation(frm)
{
	if (!checkInteger(frm.montant)
	 || !checkInteger(frm.revenu)
	 || !checkInteger(frm.age)
	 || (!frm.proprietaire[0].checked && !frm.proprietaire[1].checked))
	{
		zeroControls(frm);
		return;
	}
	doSiteCatalyst();
		
	var score = 0, montant = parseInt(frm.montant.value);
	
	if (montant <= 1200)
		score += 139;
	else if (montant <= 3100)
		score += 123;
	else if (montant <= 5000)
		score += 99;
	else
		score += 79;
	
	score += (frm.proprietaire[0].checked) ? 101 : 79;
	score += (parseInt(frm.revenu.value) <= 2000) ? 79 : 94;
	score += (parseInt(frm.age.value) > 35) ? 90 : 79;

	// taux
	a = new Array(
		 new Array(364, 19, "WD")
		,new Array(384, 17, "WQ") 
		,new Array(397, 15, "WT") 
		,new Array( -1,  9, "WN")
		);

	var v = mapInterval(score, a);
	taux = v[1];
	document.getElementById("recommandation").innerHTML = "<strong>TEG revisable : " + taux + "%</strong>"; // + "("+score+")";
	tag_taux = v[2];
	
	// montant des remboursements
	a = new Array(	
		 new Array( 501, 15, 15, 20)
		,new Array(1001, 30, 30, 35)
		,new Array(1501, 40, 45, 55)
		,new Array(2001, 55, 60, 70)
		,new Array(3001, 80, 90,105)
		,new Array(4001,110,120,140)
		,new Array(6001,160,180,210)
		,new Array(  -1,265,300,350)
		);
	var remb = mapInterval(frm.montant.value, a);
	for (var i=1; i<=3; i++)
		document.getElementById("mensualite_" + i).innerHTML = remb[i] + " €";
}

// mapInterval
function mapInterval(x, a)
{
	for (var i=0; i<a.length; i++)
		if (x < a[i][0])
			return a[i];
	return a[a.length - 1];
}

function goSimulation() {
	go(URL_SIMULATION);
}

function goCredit() {
	go(URL_CREDIT);
}


// go
function go(url)
{
	if (!tag_taux)
	{
		alert("Avant d’effectuer cette demande, renseignez les 4 infos du simulateur et découvrez le taux proposé par 123credit.com\n\nL'équipe 123credit.com");
		return;
	}
	
	var frm = document.forms["simulation"];
	for (var remb=0, i=1; i<=frm.mensualite.length; i++)
		if (frm.mensualite[i-1].checked)
			remb = i;
	
	if (!remb)
	{
		alert("Sélectionnez la mensualité que vous souhaitez et effectuez votre demande en ligne\n\nL'équipe 123credit.com");
		return;
	}
	
//	tag = tag_taux + (3-remb+1).toString(), ref = "<%=Request.QueryString("co")%>";
// 	modif WPD le 8/10 pour inverser l'indicateur de mensualité
	tag = tag_taux + remb.toString();
	ref = co;
	
	var param = "from=ppr";
	param += "&age=" + frm.age.value;
	param += "&montant=" + frm.montant.value.replace(',','.').replace(' ', '');
	param += "&taux=" + taux.toString().replace(',','.').replace('%', '');
	param += "&revenu=" + frm.revenu.value.replace(',','.').replace(' ', '');
	param += "&proprietaire=" + (frm.proprietaire[0].checked ? "oui" : "non");
	param += "&mensualite=" + parseFloat(document.getElementById("mensualite_" + remb).innerHTML).toString().replace(',','.').replace(' ', '');

	if (ref && ref.length)
	{
		param += "&co=" + tag + ref.substring(3) + tracking_sans_co;
		if (url.match(/^http/))
			window.location.href = url + '?' + param;
		else
			window.location.href = base+ "pages/" + url + ".asp?" + param;
	}
	else
	{
		wOpen(base + "popup.php?page=" + escape(url) + "&" + param + "&co=" + tag, 530, 310);
	}
}

function tipClose()
{
	document.getElementById('myToolTip').style.display = "none";
}

function tipOpen(e, name)
{	
	var mouseX = e.pageX || (e.clientX ? e.clientX + document.body.scrollLeft : 0);
	var mouseY = e.pageY || (e.clientY ? e.clientY + document.documentElement.scrollTop : 0);
	
	if(!document.getElementById('myToolTip')){
		var toolTip = document.createElement("div");	
		toolTip.id = "myToolTip";
		document.body.appendChild(toolTip);
		toolTip.className = "tool";
	}else{
		var toolTip = document.getElementById('myToolTip');
	}
	toolTip.innerHTML = ""+name+"";
	toolTip.style.display = "block";
	toolTip.style.left = mouseX +"px";
	toolTip.style.top = mouseY + 10 + "px";
}

function initSimulateur(){
	showOnglet(0);
	preload('img/onglet_corner_last_on.gif', 'img/onglet_corner_last_off.gif', 'img/onglet_corner_before.gif', 'img/onglet_corner_off.gif', 'img/onglet_corner_on.gif', 'img/onglet_bg_off.gif', 'img/onglet_bg_on.gif')

	// age
	var slider_age = new Control.Slider('handle0','track0',
	{
		 range:$R(18,80)
		,onSlide:function(v){ setTimeout("setAge(" + Math.round(v) + ")", 0); }
		,onChange:function(v){ setTimeout("setAge(" + Math.round(v) + ")", 0); }
	});
	slider_age.setValue(35);
	setAge(35);

	// montant
	var slider_montant = new Control.Slider('handle1','track1',
	{
		 range:$R(5,100)
		,onSlide:function(v){ setTimeout("setMontant(" + (Math.round(v) * 100) + ")", 0); }
		,onChange:function(v){ setTimeout("setMontant(" + (Math.round(v) * 100) + ")", 0); }
	});
	slider_montant.setValue(30);
	setMontant(3000);

	// revenu
	var slider_revenu = new Control.Slider('handle2','track2',
	{
		 range:$R(0,40)
		,onSlide:function(v){ setTimeout("setRevenu(" + v + ")", 0); }
		,onChange:function(v){ setTimeout("setRevenu(" + v + ")", 0); }
	});
	slider_revenu.setValue(20);
	setRevenu(20);
}

function preload()
{
	imgload = new Array();
	for(var j=0;j<preload.arguments.length;j++)
	{
		imgload[j] = new Image();
		imgload[j].src = preload.arguments[j];
	}
}
