String.prototype.numberFormat = function(m, b, c){
	var tmp = "";
	var machaine = this;
	for(var t = machaine.length-1; t >= 0; t--){
		tmp+=machaine.charAt(t);
	}
    var i, j, s, r = tmp.split("\n");
    if(m > 0) for(i in r){
        for(s = r[i], r[i] = ""; s.length > m;
            j = c ? m : (j = s.substr(0, m).match(/\S*$/)).input.length - j[0].length
            || m,
            r[i] += s.substr(0, j) + ((s = s.substr(j)).length ? b : "")
        );
        r[i] += s;
    }
    tmp = "";
	machaine = r.join("\n");
	for(var t = machaine.length-1; t >= 0; t--){
		tmp+=machaine.charAt(t);
	}
	return tmp;
};
String.prototype.prixFormat = function(m, b, c){

    var i, j, s, r = this.split("\n");
    if(m > 0) for(i in r){
        for(s = r[i], r[i] = ""; s.length > m;
            j = c ? m : (j = s.substr(0, m).match(/\S*$/)).input.length - j[0].length
            || m,
            r[i] += s.substr(0, j) + ((s = s.substr(j)).length ? b : "")
        );
        r[i] += s;
    }
	return r.join("\n");
};

function showLegal(){
	newPopup( {
					'url':'legal.php',
					'className':'alphacube',
					'title': 'www.auto-mobil.fr',
					'statusbar':'&nbsp;',
					'showCenter':true,
					'width':600,
					'height':500,
					'id':'showLegal'
				}
			);
}
function showPartenaires(){
	newPopup( {
					'url':'ajax.partenaires.php',
					'className':'alphacube',
					'title': 'www.auto-mobil.fr',
					'statusbar':'&nbsp;',
					'showCenter':true,
					'width':700,
					'height':300,
					'id':'showPartenaires'
				}
			);
}
function showNameo(){
	newPopup( {
					'url':'http://www.nameo.fr/',
					'iframe':true,
					'className':'alphacube',
					'title': 'www.nameo.fr',
					'statusbar':'&nbsp;',
					'showCenter':true,
					'maximize':false,
					'width':1050,
					'height':680,
					'id':'showNameo'
				}
			);
}
function updateModele2(options){
	document.body.style.cursor = 'wait';
	value = options[(options.selectedIndex)].value;
	new Ajax.Updater(	'upd_modele',
						"ajax.updateselect.modele2.php?value="+value,
						{	method:'post',
							asynchronous:true,
							onSuccess: function(t) { },
							onComplete: function(){
								Effect.DropOut('modele',{afterFinish:function(){
									document.body.style.cursor = 'default';
									Effect.Appear('modele');
								}});
							},
							evalScripts:true
						}
					);
	return false;
}
function showContact(id, url){

	/*
	var win = newPopup( {
							'url':'ajax.contact.php?id='+id,
							'className':'alphacube',
							'title': "Contact",
							'statusbar':'&nbsp;',
							'showCenter':true,
							'maximizable':true,
							'width':880,
							'height':700,
							'id':'showContact'+id
						}
					);
	*/
	window.open(url, 'showContact'+id, 'menubar=0, resizable=no, width=700, height=700');
}
function showContactBasket(){

	var win = newPopup( {
							'url':'ajax.contact.basket.php',
							'className':'alphacube',
							'title': "Contact",
							'statusbar':'&nbsp;',
							'showCenter':true,
							'maximizable':true,
							'width':880,
							'height':700,
							'id':'showContactBasket'
						}
					);
}
function showVoiture(id, _args){
	var _title = _args != undefined && _args['title'] != undefined	? _args['title'] 	: "Est-Occaz";

	var win = newPopup( {
							'url':'ajax.detail.voiture.php?id='+id,
							'className':'alphacube',
							'title': _title,
							'statusbar':'&nbsp;',
							'showCenter':true,
							'maximizable':false,
							'width':880,
							'height':700,
							'id':'showVoiture'+id
						}
					);
}
function ajaxContact(F, id, Windows, event){

	document.body.style.cursor = 'wait';
	new Ajax.Request(	"ajax.contact.submit.php",
						{	method:'post',
							asynchronous:false,
							parameters:Form.serialize(F),
							onSuccess: function(t) {
								if(t.responseText != ""){alert(t.responseText);}
								else{
									alert('Message transmis');
									if(id > 0)
										Windows.close('showContact'+id, event);
									else
										Windows.close('showContactBasket', event);
								}
							},
							onComplete: function(){
								document.body.style.cursor = 'default';
							},
							evalScripts:true
						}
					);
	return false;
}
function showBasket(){

	var win = newPopup( {
							'url':'ajax.basket.php',
							'className':'alphacube',
							'title': "Mon panier",
							'statusbar':'&nbsp;',
							'showCenter':true,
							'maximizable':true,
							'width':420,
							'height':500,
							'id':'showBasket'
						}
					);
}
function addBasket(id){
	document.body.style.cursor = 'wait';
	new Ajax.Request(	"ajax.basket.add.php?id="+id,
						{	method:'post',
							asynchronous:true,
							onSuccess: function(t) { },
							onComplete: function(){
								document.body.style.cursor = 'default';
								if(!($('showBasket'))) showBasket();
								else updateBasket();
							},
							evalScripts:true
						}
					);
	return false;
}
function updateBasket(){
	new Ajax.Updater(	'mybasket',
						"ajax.basket.lst.php",
						{	method:'post',
							asynchronous:true,
							onSuccess: function(t) { },
							onComplete: function(){
								document.body.style.cursor = 'default';
							},
							evalScripts:true
						}
					);
}
function newBasket(){
	if(confirm('Etes-vous sur de vouloir vider votre panier ?')){
		document.body.style.cursor = 'wait';
		new Ajax.Request(	"ajax.basket.new.php",
							{	method:'post',
								asynchronous:true,
								onSuccess: function(t) { },
								onComplete: function(){
									document.body.style.cursor = 'default';
									updateBasket();
								},
								evalScripts:true
							}
						);
	}
	return false;
}
function rmBasket(id){
	if(confirm('Etes-vous sur de vouloir supprimer cette voiture du panier ?')){
		document.body.style.cursor = 'wait';
		new Ajax.Request(	"ajax.basket.rm.php?id="+id,
							{	method:'post',
								asynchronous:true,
								onSuccess: function(t) { },
								onComplete: function(){
									document.body.style.cursor = 'default';
									updateBasket();
								},
								evalScripts:true
							}
						);
	}
	return false;
}
	function showOptions(){
		if($('optionsRight').style.display == "none"){
			//new Effect.BlindDown('optionsRight', {});
			new Effect.Parallel([
				new Effect.BlindDown('optionsRight', { sync: true, transition:Effect.Transitions.Bounce }),
				new Effect.Opacity('optionsRight', { sync: true, from: 0, to: 1 })
			], {
				duration: 2.0,
				delay:0
			});
			//new Effect.Morph('optionsRight', {style:{height:'550px'}, duration:.5});
		} else {
			//new Effect.Morph('optionsRight', {style:{height:'0px'}, duration:.5});
			new Effect.DropOut('optionsRight');
		}
		return false;
	}
	function banniereAddStats(idBann){
		new Ajax.Request("ajax.banniere.add.clique.php?id="+idBann, 	{
				method:'get',
				asynchronous:true,
				onSuccess: function(t) {},
				onComplete: function(){	},
				evalScripts:false
			}
		);
		return false;
	}

	function changePrix(v){
		$('innerPrix').innerHTML='Entre '+(v[0]).toString().numberFormat(3, " ", true)+' et '+(v[1]).toString().numberFormat(3, " ", true)+' &euro;'
		$('prix_min').value = v[0];
		$('prix_max').value = v[1];
	}
	function changePuis(v){
		$('innerPuis').innerHTML='Entre '+(v[0]).toString().numberFormat(3, " ", true)+' et '+(v[1]).toString().numberFormat(3, " ", true)+' chevaux'
		$('puis_min').value = v[0];
		$('puis_max').value = v[1];
	}
	function changeKm(v){
		$('innerKm').innerHTML='Entre '+(v[0]).toString().numberFormat(3, " ", true)+' et '+(v[1]).toString().numberFormat(3, " ", true)+' km'
		$('km_min').value = v[0];
		$('km_max').value = v[1];
	}
	function voituresTrier(ct, st){
		$('ct').value = ct;
		$('st').value = st;
		updateDatas();
	}
	function voituresGoTo(pg_cour){
		$('pg_cour').value = pg_cour;
		updateDatas();
	}
	function updateDatas(){
		var ct 			= $('ct').value;
		var st 			= $('st').value;
		var pg_cour 	= $('pg_cour').value;
		var prix_min 	= $('prix_min').value;
		var prix_max 	= $('prix_max').value;
		var puis_min 	= $('puis_min').value;
		var puis_max 	= $('puis_max').value;
		var km_min 		= $('km_min').value;
		var km_max 		= $('km_max').value;
		var marque 		= $('marque').value;
		var modele 		= $('modele').value;
		var categorie 	= $('categorie').value;
		var carburant 	= $('carburant').value;
		var fourn	 	= $('fourn') ? $('fourn').value : '';
	//	var type	 	= $('type') ? $('type').value : '';
		var pict	 	= $('pict') ? $('pict').checked : false;
		var porte 		= $('porte').value;
		var options		= getChkValues('option').join(',');
		var type = ($('type') ? $('type').value : 'O');
		document.body.style.cursor = 'wait';
		$('ajaxLoading2').style.display="block";
		/*
		new Effect.Morph('tabRes',{
		  style:'background:#ddd;',
		  duration:0.8
		});
		*/
		new Ajax.Updater(	'nbv',
							"ajax.array.vehicules.php",
							{	method:'get',
								parameters:'ct='+ct
											+'&st='+st
											+'&typereq=num'
											+'&pg_cour='+pg_cour
											+'&prix_min='+prix_min
											+'&prix_max='+prix_max
											+'&puis_min='+puis_min
											+'&puis_max='+puis_max
											+'&km_min='+km_min
											+'&km_max='+km_max
											+'&marque='+marque
											+'&fourn='+fourn
											+'&type='+type
											+'&pict='+(pict ? 1 : 0)
											+'&modele='+modele
											+'&categorie='+categorie
											+'&carburant='+carburant
											+'&porte='+porte
											+'&options='+options,
								asynchronous:true,
								onSuccess: function(t) { },
								onComplete: function(){
									document.body.style.cursor = 'default';
									$('ajaxLoading2').style.display="none";
									/*
									new Effect.Morph('tabRes',{
									  style:'background:#fff;',
									  duration:0.8
									});
									*/
								},
								evalScripts:true
							}
						);
		new Ajax.Updater(	'tabRes',
							"ajax.array.vehicules.php",
							{	method:'get',
								parameters:'ct='+ct
											+'&st='+st
                                                                                        +'&typereq=lst'
											+'&pg_cour='+pg_cour
											+'&prix_min='+prix_min
											+'&prix_max='+prix_max
											+'&puis_min='+puis_min
											+'&puis_max='+puis_max
											+'&km_min='+km_min
											+'&km_max='+km_max
											+'&marque='+marque
											+'&fourn='+fourn
											+'&type='+type
											+'&pict='+(pict ? 1 : 0)
											+'&modele='+modele
											+'&categorie='+categorie
											+'&carburant='+carburant
											+'&porte='+porte
											+'&options='+options,
								asynchronous:true,
								onSuccess: function(t) { },
								onComplete: function(){
									document.body.style.cursor = 'default';
									$('ajaxLoading2').style.display="none";
									/*
									new Effect.Morph('tabRes',{
									  style:'background:#fff;',
									  duration:0.8
									});
									*/
								},
								evalScripts:true
							}
						);
		return false;
	}
	function getChkValues(id) {
		var res = new Array();
		var j = 0;
		try {
			var options = $$('input[id^='+id+']');
			options.each(function(option) {	if(option.checked) res[j++] = option.value;	});
		} catch (e) { /* alert(e); */	}
		return res;
	}
	function updateModele(options){
		document.body.style.cursor = 'wait';
		value = options[(options.selectedIndex)].value;
		new Ajax.Updater(	'upd_modele',
							"ajax.updateselect.modele.php?value="+value,
							{	method:'post',
								asynchronous:true,
								onSuccess: function(t) { },
								onComplete: function(){
									Effect.DropOut('modele',{afterFinish:function(){
										document.body.style.cursor = 'default';
										Effect.Appear('modele');
										updateDatas();
									}});
								},
								evalScripts:true
							}
						);
		return false;
	}
	function updateModele3(options){
		document.body.style.cursor = 'wait';
		value = options[(options.selectedIndex)].value;
		new Ajax.Updater(	'voitures_modele',
							"ajax.updateselect.modele3.php?value="+value,
							{	method:'post',
								asynchronous:true,
								onSuccess: function(t) { },
								onComplete: function(){
									Effect.DropOut('Vmodele',{afterFinish:function(){
										document.body.style.cursor = 'default';
										Effect.Appear('Vmodele');
										prepareInputsForHints();
									}});
								},
								evalScripts:true
							}
						);
		return false;
	}
	function switchPict(lib, href, id){
		//$(id).setAttribute("onclick","return false;");
//		$(lib).style.display="none";
		new Effect.BlindUp(lib,{duration:0.2, afterFinish:function(){
			$(lib).src=href;
			new Effect.BlindDown(lib,{duration:0.2, afterFinish:function(){/*$(id).setAttribute("onclick","switchPict('"+lib+"', '"+href+"', '"+id+"');return false;");*/}});
		}});
	}
/* Gestion des Hints */
	function prepareInputsForHints() {
		var inputs = document.getElementsByTagName("input");
		for (var i=0; i<inputs.length; i++){
			// test to see if the hint span exists first
			if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
				// the span exists!  on focus, show the hint
				inputs[i].onfocus = function () {
					this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
				}
				// when the cursor moves away from the field, hide the hint
				inputs[i].onblur = function () {
					this.parentNode.getElementsByTagName("span")[0].style.display = "none";
				}
			}
		}
		var inputs = document.getElementsByTagName("textarea");
		for (var i=0; i<inputs.length; i++){
			// test to see if the hint span exists first
			if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
				// the span exists!  on focus, show the hint
				inputs[i].onfocus = function () {
					this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
				}
				// when the cursor moves away from the field, hide the hint
				inputs[i].onblur = function () {
					this.parentNode.getElementsByTagName("span")[0].style.display = "none";
				}
			}
		}

		// repeat the same tests as above for selects
		var selects = document.getElementsByTagName("select");
		for (var k=0; k<selects.length; k++){
			if (selects[k].parentNode.getElementsByTagName("span")[0]) {
				selects[k].onfocus = function () {
					this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
				}
				selects[k].onblur = function () {
					this.parentNode.getElementsByTagName("span")[0].style.display = "none";
				}
			}
		}
	}

