﻿// JavaScript Document
function creerRequete(){
	try {
		requete = new XMLHttpRequest();
    }catch (error){
		try {
			requete = new ActiveXObject("msxml2.XMLHTTP");
		}
		catch (error){
			try {
				requete = new ActiveXObject("Microsoft.XMLHTTP");
			}
				catch (error){
					requete = null;
			}
		}

	}
if (requete == null) alert ('impossible de créer une requete au serveur');
}

var compteur
var page;

function changeColor(){//4573B3
	if(parseInt(compteur)<4){
		if (document.getElementById("nbResults").style.visibility=="visible"){
			document.getElementById("nbResults").style.visibility="hidden";
			
		}else{
			document.getElementById("nbResults").style.visibility="visible";
			
		}
		compteur++;	
		window.setTimeout( "changeColor()", 250 );
	}
}

function affiche_nbResults(){
	creerRequete();
	var getRequest ="";
	requete.onreadystatechange = function (){
		if(requete.readyState == 4) {
			var contenu = requete.responseText;
			var divId = "nbResults";
			if (document.getElementById("msgResults")){
				if (contenu>=140){
					document.getElementById("msgResults").innerHTML="conseil : s&eacute;lectionnez au moins un crit&egrave;re pour affiner vos r&eacute;sultats";
				}else{
					document.getElementById("msgResults").innerHTML="&nbsp;";
				}
			}
//			alert (contenu);
			document.getElementById(divId).innerHTML=contenu;
//			document.getElementById(divId).style.color="#FFFFFF"
//			changeColor();
			compteur=0
			document.getElementById("nbResults").style.visibility="visible";
			changeColor();
		}
	}
	// construction requete GET
	for (var i=0;i<document.frmSearch.elements.length;i++){
		if(document.frmSearch.elements[i].type=="checkbox"){
			if (document.frmSearch.elements[i].name.substr(0,10)=="chkmarques" && document.frmSearch.elements[i].checked){
				getRequest +=document.frmSearch.elements[i].name+"="+document.frmSearch.elements[i].value+"&";
			}else if (document.frmSearch.elements[i].name.substr(0,10)!="chkmarques"){
				getRequest +=document.frmSearch.elements[i].name+"="+document.frmSearch.elements[i].checked+"&";
			}
		}else if(document.frmSearch.elements[i].type=="radio"){
			if (document.frmSearch.elements[i].checked){
				getRequest +=document.frmSearch.elements[i].name+"="+document.frmSearch.elements[i].value+"&";
			}
		}else{
			getRequest +=document.frmSearch.elements[i].name+"="+document.frmSearch.elements[i].value+"&";
		}
	}
	getRequest = getRequest.substr(0,getRequest.length-1);
	requete.open("get","http://www.choisir-sa-voiture.com/ecologique/recherche/affichage.php?from=index_pc&"+getRequest,true);
	requete.send(null);
}


function affiche_combo(idmarque,idmodele,from){
	creerRequete();
	//
	requete.onreadystatechange = function combo_series(){
		if(requete.readyState == 4) {
			contenu = requete.responseText;
			document.getElementById('selectModele').innerHTML=contenu;
		}
	}
	requete.open("get","http://www.choisir-sa-voiture.com/ecologique/recherche/affichage.php?idmarque="+idmarque+"&idmodele="+idmodele+"&from="+from,true);
	requete.send(null);
}

function ajoute_comparateur(id){
	creerRequete();
	var getRequest ="";
	requete.onreadystatechange = function (){
		if(requete.readyState == 4) {
			var contenu = requete.responseText;
			var divId = "nbComp";
			var divIdNav = "nbCompNav";
			if(contenu.substr(0,5)=="plein"){
				var width;
				document.getElementById('divBack').style.visibility="visible";
				document.getElementById('divBack').style.height=GetWinSize('h');
				(_dom==4)?width=getWindowWidth()-19:width=getWindowWidth();
				resizeDivTo(document.getElementById('msgComp'),width,getWindowHeight());
				moveDivTo(document.getElementById('msgComp'),0,getWinYOffset());
				//alert(document.body.parentNode.scrollTop);
				var moveY;
				(_dom==4)?moveY=0:moveY=document.body.parentNode.scrollTop;
				moveDivTo(document.getElementById('tabComp'),(getWindowWidth()-getDivWidth(document.getElementById('tabComp')))/2,moveY+200);
				document.getElementById('msgComp').style.visibility="visible";
				if(document.getElementById('groupby'))
					document.getElementById('groupby').style.visibility="hidden";
				if(document.getElementById('orderby'))
				document.getElementById('orderby').style.visibility="hidden";
			}else{
				if (document.getElementById(divId))
					document.getElementById(divId).innerHTML=contenu;
				if (document.getElementById(divIdNav))
					document.getElementById(divIdNav).innerHTML=contenu;
				document.getElementById('divboutoncomparer'+id).innerHTML='<img src="http://www.choisir-sa-voiture.com/ecologique/images/ajoute.png" alt="cliquez pour comparer" name="boutoncomparer'+id+'" width="78" height="33" border="0" id="boutoncomparer'+id+'" class="main" onMouseOver="this.src=\'http://www.choisir-sa-voiture.com/ecologique/images/ajouteon.png\';" onMouseOut="this.src=\'http://www.choisir-sa-voiture.com/ecologique/images/ajoute.png\';" onclick="javascript:supprime_comparateur(\''+id+'\');" />';
				//document.getElementById('boutoncomparer'+id).src="http://www.choisir-sa-voiture.com/ecologique/images/ajoute.png";
			}
		}
	}
	requete.open("get","http://www.choisir-sa-voiture.com/ecologique/resultat/affichage.php?idversion="+id,true);
	requete.send(null);
}

function switchComp(id,rang){
	creerRequete();
	var getRequest ="";
	requete.onreadystatechange = function (){
		if(requete.readyState == 4) {
			var contenu = requete.responseText;
			var divId = "nbComp";
			var divIdNav = "nbCompNav";
			if (document.getElementById(divId))
				document.getElementById(divId).innerHTML=contenu;
			if (document.getElementById(divIdNav))
				document.getElementById(divIdNav).innerHTML=contenu;
				//document.getElementById('boutoncomparer'+id).src="http://www.choisir-sa-voiture.com/ecologique/images/ajoute.png";
			document.location.href='comparatif.php';
		}
	}
	requete.open("get","http://www.choisir-sa-voiture.com/ecologique/resultat/affichage.php?idversion="+id+"&rang="+rang,true);
	requete.send(null);
}

function supprime_comparateur(id){
	creerRequete();
	var getRequest ="";
	requete.onreadystatechange = function (){
		if(requete.readyState == 4) {
			var contenu = requete.responseText;
			var divId = "nbComp";
			var divIdNav = "nbCompNav";
			if (document.getElementById(divId))
				document.getElementById(divId).innerHTML=contenu;
			if (document.getElementById(divIdNav))
				document.getElementById(divIdNav).innerHTML=contenu;
			if(document.getElementById('divboutoncomparer'+id))
				document.getElementById('divboutoncomparer'+id).innerHTML='<img src="http://www.choisir-sa-voiture.com/ecologique/images/boutoncomparer-eco.png" alt="cliquez pour comparer" name="boutoncomparer'+id+'" width="78" height="33" border="0" id="boutoncomparer'+id+'" class="main" onMouseOver="this.src=\'http://www.choisir-sa-voiture.com/ecologique/images/boutoncomparer-ecoon.png\';" onMouseOut="this.src=\'http://www.choisir-sa-voiture.com/ecologique/images/boutoncomparer-eco.png\';" onclick="javascript:ajoute_comparateur(\''+id+'\');" />';
//			setBtn();
			//document.getElementById('boutoncomparer'+id).src="http://www.choisir-sa-voiture.com/ecologique/images/ajoute.png";
		}
	}
	requete.open("get","http://www.choisir-sa-voiture.com/ecologique/resultat/affichage.php?suppr=1&idversion="+id,true);
	requete.send(null);
}
function envoiBrochure(id){
	document.getElementById('divBack').style.visibility="visible";
	document.getElementById('divBack').style.height=GetWinSize('h');
	moveDivTo(document.getElementById('divBack'),0,getWinYOffset());
	creerRequete();
	requete.onreadystatechange = function (){
		if(requete.readyState == 4) {
			var contenu = requete.responseText;
			var divId = "msg";
			var width;
			(_dom==4)?width=getWindowWidth()-19:width=getWindowWidth();
			resizeDivTo(document.getElementById('msgBrochure'),width,getWindowHeight());
			moveDivTo(document.getElementById('msgBrochure'),0,getWinYOffset());
			//alert(document.body.parentNode.scrollTop);
			var moveY;
			(_dom==4)?moveY=0:moveY=document.body.parentNode.scrollTop;
			moveDivTo(document.getElementById('tabBrochure'),(getWindowWidth()-getDivWidth(document.getElementById('tabBrochure')))/2,moveY+200);
			document.getElementById('msgBrochure').style.visibility="visible";
//			document.getElementById(divId).innerHTML=contenu;
		}
	}
	requete.open("get","http://www.choisir-sa-voiture.com/mail/envoimail.php?type=brochure&id="+id,true);
	requete.send(null);
}
function envoiFiche(id){
	document.getElementById('divBack').style.visibility="visible";
	document.getElementById('divBack').style.height=GetWinSize('h');
	(_dom==4)?width=getWindowWidth()-19:width=getWindowWidth();
	resizeDivTo(document.getElementById('envoi'),width,getWindowHeight());
	moveDivTo(document.getElementById('envoi'),0,getWinYOffset());
	var moveYenvoi;
	(_dom==4)?moveYenvoi=0:moveYenvoi=document.body.parentNode.scrollTop;
	moveDivTo(document.getElementById('tabEnvoi'),(getWindowWidth()-getDivWidth(document.getElementById('tabEnvoi')))/2,moveYenvoi+200);
	document.getElementById('envoi').style.visibility="visible";
	creerRequete();
	requete.onreadystatechange = function (){
		if(requete.readyState == 4) {
			var contenu = requete.responseText;
			var divId = "msg";
			var width;
			(_dom==4)?width=getWindowWidth()-19:width=getWindowWidth();
			resizeDivTo(document.getElementById('msgFiche'),width,getWindowHeight());
			moveDivTo(document.getElementById('msgFiche'),0,getWinYOffset());
			//alert(document.body.parentNode.scrollTop);
			var moveY;
			(_dom==4)?moveY=0:moveY=document.body.parentNode.scrollTop;
			moveDivTo(document.getElementById('tabFiche'),(getWindowWidth()-getDivWidth(document.getElementById('tabFiche')))/2,moveY+200);
			document.getElementById('envoi').style.visibility="hidden";
			document.getElementById('msgFiche').style.visibility="visible";
//			document.getElementById(divId).innerHTML=contenu;
		}
	}
	requete.open("get","http://www.choisir-sa-voiture.com/mail/envoimail.php?type=fiche&id="+id,true);
	requete.send(null);
}

function envoiPromos(id){
	document.getElementById('divBack').style.visibility="visible";
	document.getElementById('divBack').style.height=GetWinSize('h');
	(_dom==4)?width=getWindowWidth()-19:width=getWindowWidth();
	resizeDivTo(document.getElementById('envoiPromos'),width,getWindowHeight());
	moveDivTo(document.getElementById('envoiPromos'),0,getWinYOffset());
	var moveYenvoi;
	(_dom==4)?moveYenvoi=0:moveYenvoi=document.body.parentNode.scrollTop;
	moveDivTo(document.getElementById('tabEnvoiPromos'),(getWindowWidth()-getDivWidth(document.getElementById('tabEnvoiPromos')))/2,moveYenvoi+200);
	document.getElementById('envoiPromos').style.visibility="visible";
	creerRequete();
	requete.onreadystatechange = function (){
		if(requete.readyState == 4) {
			var contenu = requete.responseText;
//			alert(contenu);
			var divId = "msg";
			var width;
			(_dom==4)?width=getWindowWidth()-19:width=getWindowWidth();
			resizeDivTo(document.getElementById('msgPromos'),width,getWindowHeight());
			moveDivTo(document.getElementById('msgPromos'),0,getWinYOffset());
			resizeDivTo(document.getElementById('msgPromosNOK'),width,getWindowHeight());
			moveDivTo(document.getElementById('msgPromosNOK'),0,getWinYOffset());
			//alert(document.body.parentNode.scrollTop);
			var moveY;
			(_dom==4)?moveY=0:moveY=document.body.parentNode.scrollTop;
			moveDivTo(document.getElementById('tabPromos'),(getWindowWidth()-getDivWidth(document.getElementById('tabPromos')))/2,moveY+200);
			moveDivTo(document.getElementById('tabPromosNOK'),(getWindowWidth()-getDivWidth(document.getElementById('tabPromosNOK')))/2,moveY+200);
			if (contenu=="OK"){
				document.getElementById('envoiPromos').style.visibility="hidden";
				document.getElementById('msgPromos').style.visibility="visible";
			}else if (contenu=="pas de promo"){
				window.setTimeout( "afficheDiv()", 1000 );
			}
//			document.getElementById(divId).innerHTML=contenu;
		}
	}
	requete.open("get","http://www.choisir-sa-voiture.com/mail/envoimail.php?type=promos&id="+id,true);
	requete.send(null);
}

function afficheDiv(){
	document.getElementById('envoiPromos').style.visibility="hidden";
	document.getElementById('msgPromosNOK').style.visibility="visible";
}


function reponse(mail){
	regexp_mail=/^[a-z0-9!#$%'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
	if(!regexp_mail.test(mail)){
		document.getElementById('divmsg').innerHTML="Adresse email incorrecte, veuillez recommencer.";
		document.getElementById('textfield').value='';
	}else{
		creerRequete();
		requete.onreadystatechange = function (){
			if(requete.readyState == 4) {
				document.getElementById('divmsg').innerHTML="";
				contenu = requete.responseText;
				document.getElementById('divinscription').innerHTML=contenu;
			}
		}
		requete.open("get","inscription/_inscription.php?email="+escape(mail),true);
		requete.send(null);
	}
}

// Devis en concession dans la page fiche.php
function submitFormDevis(varDevis){
	creerRequete();
	var getRequest ="";
	var params = varDevis;
	requete.onreadystatechange = function (){
		if(requete.readyState == 4) {
			var contenu = requete.responseText;
			if (document.getElementById('frmCoord')){
				document.getElementById('frmCoord').innerHTML=contenu;
				document.getElementById('divBack').style.visibility="visible";
				document.getElementById('divBack').style.height=GetWinSize('h');
				document.getElementById('msgDevis').style.visibility="visible";
			}
		}
	}
	requete.open("get","../../fiche/resultat/devisconcession.php?"+params,true);
	requete.send(null);
}
