
function password(elm){
	var mdp = document.getElementById("code_professionnel");
	if (window.ActiveXObject) {
		elm.style.display="none";
		elm.value="";
		mdp.style.display="inline";
		mdp.focus();
	}else{
		elm.type='password'; 
		elm.value='';
		elm.focus();
	}
}
function identifier(){
	var mdp = document.getElementById("Password");	
	var pwd = document.getElementById("code_professionnel");
	if (window.ActiveXObject) {
		pwd.style.display="none";
		mdp.style.display="inline";
		mdp.value="Code Pro";
	}else{
		mdp.type='text'; 
		mdp.value="Code Pro";
	}
}


function getXhr(){
	var xhr = null; 
	if(window.XMLHttpRequest) // Firefox et autres
		xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
		try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
	}
	else { // XMLHttpRequest non support&eacute; par le navigateur 
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		xhr = false; 
	} 
	return xhr;
}

//----------------------------------- AJAX pour le title prixpro -----------------------------------
// function aff_prix_pro(id){
	// var httpRequest = getXhr();
	
	// httpRequest.onreadystatechange = function() {
		// if (httpRequest.readyState == 4 && httpRequest.status == 200) {
			// document.getElementById("prix_pro_"+id).innerHTML = httpRequest.responseText;
		// }
	// }

	// httpRequest.open('POST', "aff_prix_pro.php", true);	
	// httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	// httpRequest.send("id="+id);
// }
//------------------------------------ fin pour le title prixpro -----------------------------------

function se_logger(){

	var httpRequest = getXhr();
	var pwd;

	httpRequest.onreadystatechange = function() {
		if (httpRequest.readyState == 4 && httpRequest.status == 200) {
			document.getElementById("identification").innerHTML = httpRequest.responseText;		
			if (location.pathname.indexOf("stock.php") != -1 || location.pathname.indexOf("details.php") != -1)
				window.location.replace(window.location.href);
		}
	}
	
	httpRequest.open('POST', "identification.php", true);	
	httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	pwd = document.getElementById("Password").value;
	if (pwd == "") pwd = document.getElementById("code_professionnel").value;
	
	httpRequest.send("Password="+pwd);
}

function se_delogger(){
	var httpRequest = getXhr();

	httpRequest.onreadystatechange = function() {
		if (httpRequest.readyState == 4 && httpRequest.status == 200) {
			document.getElementById("deconnecte").innerHTML = httpRequest.responseText;		
			if (location.pathname.indexOf("stock.php") != -1 || location.pathname.indexOf("details.php") != -1)
				window.location.replace(window.location.href);			
		}
	}
	
	httpRequest.open('POST', "identification.php", true);	
	httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');	
	httpRequest.send("se_deconnecter=1");
}

function go(champs1,champs2){

	var champ = champs1;
	var xhr = getXhr();

	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			// On se sert de innerHTML pour rajouter les options a la liste
			document.getElementById("div_"+champs2).innerHTML = xhr.responseText;
		}
	}

	xhr.open("POST","requete_"+champs2+".php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	sel = document.getElementById(champs1);
	champs1 = sel.options[sel.selectedIndex].value;
	xhr.send("ref_"+champ+"="+champs1);
}	

//document.onkeypress = key_down_;
  
function key_down_(touche) {
	var flag= "";

	if (navigator.appName.substring(0,3) == "Net" || navigator.appName.substring(0,3) == "Ope") { //mozilla & opera
		// k=touche.which;
		if (document.getElementById("Password") && document.getElementById("Password").value != "" && document.getElementById("Password").value != "S'identifier"){
			flag = "yes";		
		}else{
			flag= "no";
		}
	}
	else { //IE 
		// k=window.event.keyCode;
		if (document.getElementById("code_professionnel") && document.getElementById("code_professionnel").value != "" && document.getElementById("Password").value != "S'identifier"){	
			flag = "yes";
		}else{
			flag= "no";					
		}
	}

	if (touche==13 && flag=="yes") se_logger();
}

document.onkeydown = key_down;

var select01=false;
  
function key_down(touche) {
	if (navigator.appName.substring(0,3) == "Net") {
		k=touche.which;
	}
	else {
		k=window.event.keyCode;
	}
  
	if (select01==true) {
		if (k==13) document.monformulaire.submit();
	}
}

function element_selection01(){
	select01=true;
}

function element_deselection01(){
	select01=false;
}

function calcul_pret(){
	var dureeEcheance=document.getElementById('dureeEcheance').value;
	var montantTotal=document.getElementById('montantTotal').value;
	var tauxAnnuel=document.getElementById('tauxAnnuel').value;
	var montantEcheance=document.getElementById('montantEcheance').value;

	var tauxPer = Math.pow(1+ (tauxAnnuel/100), (1/12))-1;

	//if (montantTotal == "" && montantEcheance == ""){
	//alert("Renseignez le montant total du crédit ou l'échéance par mois que vous souhaitez");
	//}

	if (montantTotal != "")
	{
	montantEcheance = (montantTotal * tauxPer) / (1 - Math.pow(1+tauxPer, -dureeEcheance));
	document.getElementById('montantEcheance').value = Math.round(montantEcheance*100) / 100;
	}
	else{
	montantTotal = montantEcheance * (1 - Math.pow(1+tauxPer, -dureeEcheance)) / tauxPer  ;
	document.getElementById('montantTotal').value = Math.round(montantTotal*100) / 100;
	}
	
	document.getElementById('commentaires').value = Math.round((montantEcheance*dureeEcheance-montantTotal)*100)/100;
	
}
