// Initialisation des Events sur les formulaires du header
function initForms(){

	// formulaire d'identification du bandeau
	if($('form_flash_login')){
		$('flash_login').addEvents({
			'click':function(event){
				//event = new Event(event).stop();
				//$('flash_login').value= ($('flash_login').value!='Email ou Code client') ? $('flash_login').value : '' ;
				if($('flash_login').value=='Email ou Code client')$('flash_login').value='';
			},
			//'mouseleave':function(event){
				//event = new Event(event).stop();
				//$('flash_login').value= ($('flash_login').value!='') ? $('flash_login').value : 'Email ou Code client' ;
			'blur':function(event){
				if($('flash_login').value=='')$('flash_login').value='Email ou Code client';
			}
		});
		$('flash_password').addEvents({
			'click':function(event){
				//event = new Event(event).stop();
				//$('flash_password').value= ($('flash_password').value!='Password') ? $('flash_password').value : '' ;
				if($('flash_password').value=='Password')$('flash_password').value='';
			},
			//'mouseleave':function(event){
				//event = new Event(event).stop();
				//$('flash_password').value= ($('flash_password').value!='') ? $('flash_password').value : 'Password' ;
			'blur':function(event){
				if($('flash_password').value=='')$('flash_password').value='Password';
			}
		});
		$('doFlashlogin').addEvent('click',function(event){
			event = new Event(event).stop();
			var Xdata = $('form_flash_login').toQueryString();
			var myAjax = new Ajax('/misc/_login.php', {
				encoding: 'iso-8859-15',
				method: 'post',
				data: Xdata,
				postBody: Xdata,
				onComplete: function(){
					var res = (myAjax.response.text) ? myAjax.response.text : '';
					var parms = res.split('_');
					if(parms[0]=="login"){
						var str = parms[1];
						var alrt = str.split("|");
						alrt.each(function(itm){
							if(itm){
								var myErr = itm.split(";");
								var myStr = myErr[1];
								var myId = 'myFlashElem_'+myErr[0];
								var myItm = 'flash_'+myErr[0];
								/*var myElem  = new Element('div', {
									id: myId,
									text: myStr
								});*/
								$(myItm).addEvent('click',function(){
									$(myItm).setStyle('border','1px solid #5bc3c2');
									$(myItm).setStyle('background-color','#fff');
									$(myItm).setStyle('color','#555555');
									/*myElem.remove();*/
								});
								if($('myFlashElem_'+myErr[0])){
								}else{
									/*myElem.addClass("warning");
									myElem.inject($(myItm), 'after');*/
									$(myItm).setStyle('border','1px solid #e5004f');
									$(myItm).setStyle('background-color','#fff');
									$(myItm).setStyle('color','#e5004f');
								}
	;						}
						});
					}else{
						var url = "/client/"+parms[0]+".html";
						document.location = url;
					}
				}
			}).request();
		});

	}

	// formulaire de recherche catalogue
	$('q').addEvents({
		'click':function(event){
			//event = new Event(event).stop();
			//$('q').value= ($('q').value!='Rechercher') ? $('q').value : '' ;
			if($('q').value=='Rechercher (réf ou nom)')$('q').value='';
		},
		'blur':function(event){
			//event = new Event(event).stop();
			//$('q').value= ($('q').value!='') ? $('q').value : 'Rechercher' ;
			if($('q').value=='')$('q').value='Rechercher (réf ou nom)';
		},
		'keyup':function(event){
			// remplacement de l'apostrophe Word
			var str = escape($('q').value);
			$('q').value=unescape(str.replace("%u2019","'"));
		}

		
	});
	$('dosearch').addEvent('click',function(event){
		event = new Event(event).stop();
		doSearch();
	});

	// formulaire de choix d'affichage des prix du catalogue
	$('tarifs_ht').addEvent('click',function(event){
		event = new Event(event).stop();
		var Rurl = document.location;
		Rurl = Rurl.toString();
		Rurl = Rurl.replace("#","");
		var reg = new RegExp("((priceMode=)[htc]{2,3})+","gi");
		if(reg.exec(Rurl)!=null){
			Rurl = Rurl.replace(reg,'priceMode=ht');
			document.location = Rurl;
		}else{
			var reg = /\?/;
			Rurl += (reg.exec(Rurl)!=null) ? '&':'?'
			document.location = Rurl+'priceMode=ht';
		}
	});
	$('tarifs_ttc').addEvent('click',function(event){
		event = new Event(event).stop();
		var Rurl = document.location;
		Rurl = Rurl.toString();
		Rurl = Rurl.replace("#","");
		var reg = new RegExp("((priceMode=)[htc]{2,3})+","gi");
		if(reg.exec(Rurl)!=null){
			Rurl = Rurl.replace(reg,'priceMode=ttc');
			document.location = Rurl;
		}else{
			var reg = /\?/;
			Rurl += (reg.exec(Rurl)!=null) ? '&':'?'
			document.location = Rurl+'priceMode=ttc';
		}
	});
	if($('departements_vrp'))
		initSearchVRP();

	if($('formulaire_contact'))
		initContactFrm();
	if($('formulaire_catalogue'))
		initCatalogueFrm();
	if($('formulaire_devis_feux'))
		initDevisFeuxFrm();	
	if($('formulaire_devis_vitrines'))
		initDevisVitrinesFrm();	
	if($('formulaire_devis_mobilier'))
		initDevisMobilierFrm();
	if($('formulaire_devis_reliure'))
		initDevisReliureFrm();			
	if($('nl_body'))
		initNewsletter();
		
}
// newsletter
function doNewsL(res){
	//var med = $('errorCode').empty().addClass('ajax-loading');
	var parms = res.split('_');
	
	var str = parms[1];
	var alrt = str.split("|");
	alrt.each(function(itm){
		if(itm){
			var myErr = itm.split(";");
			var myStr = myErr[1];
			var myId = 'myElem_'+myErr[0];
			var myItm = myErr[0];
			if(myItm=="nlEmail"){
				var myElem  = new Element('div', {
					id: myId
				});
				$(myItm).addEvent('click',function(){
					$(myItm).setStyle('border','1px solid #5bc3c2');
					$(myItm).setStyle('background-color','#fff');
					$(myItm).setStyle('color','#555555');
					myElem.remove();
				});
				$('subNlForm').addEvent('click',function(){
					$(myItm).setStyle('border','1px solid #5bc3c2');
					$(myItm).setStyle('background-color','#fff');
					$(myItm).setStyle('color','#555555');
					myElem.remove();
				});
				if($('myElem_'+myErr[0])){
				}else{
					myElem.setText(myStr);

					myElem.inject($(myItm), 'after');
					myElem.addClass("warning");
					$(myItm).setStyle('border','1px solid #e5004f');
					$(myItm).setStyle('background-color','#fff');
					$(myItm).setStyle('color','#e5004f');
				}
			}else{
				$('nlInfo').empty();
				$('nlInfo').setText(myStr);
				$('nlInfo').setStyle('font-weight','bold');
			}
		}
	});

}

function initNewsletter(){
	$('subNlForm').addEvent('click',function(event){
		event = new Event(event).stop();
		var Xdata = $('nl_body').toQueryString();
		var myAjax = new Ajax('/misc/_newsletter.php', {
			encoding: 'iso-8859-15',
			method: 'post',
			data: Xdata,
			postBody: Xdata,
			onComplete: function(){
				var res = (myAjax.response.text) ? myAjax.response.text : '';
				doNewsL(res);
			}
		}).request();
	});
}
//formulaire de demande de catalogue
function initCatalogueFrm(){
	var d = $('formulaire_catalogue').empty().addClass('ajax-loading');
	var myAjax = new Ajax('/misc/_form_catalogue.php', {
		encoding: 'iso-8859-15',
		method: 'get',
		update:d,
		onComplete: function(){
			d.removeClass('ajax-loading');
			if($('subCatalogueForm'))
				initCatalogueBt();
		}
	}).request();
}
function initCatalogueBt(){
	$('subCatalogueForm').addEvent('click',function(event){
		event = new Event(event).stop();
		var Xdata = $('form_catalogue').toQueryString();
		var d = $('formulaire_catalogue').empty().addClass('ajax-loading');
		var myAjax = new Ajax('/misc/_form_catalogue.php', {
			encoding: 'iso-8859-15',
			method: 'post',
			update:d,
			data: Xdata,
			postBody: Xdata,
			onComplete: function(){
				d.removeClass('ajax-loading');
				if($('subCatalogueForm'))
					initCatalogueBt();
			}
		}).request();	
	});
}
//formulaire de contact
function initContactFrm(){
	var d = $('formulaire_contact').empty().addClass('ajax-loading');
	var myAjax = new Ajax('/misc/_contact.php', {
		encoding: 'iso-8859-15',
		method: 'get',
		update:d,
		onComplete: function(){
			d.removeClass('ajax-loading');
			if($('subContactForm'))
				initContactBt();
		}
	}).request();
}
function initContactBt(){
	$('subContactForm').addEvent('click',function(event){
		event = new Event(event).stop();
		

		var Xdata = $('form_contact').toQueryString();
		var d = $('formulaire_contact').empty().addClass('ajax-loading');
		var myAjax = new Ajax('/misc/_contact.php', {
			encoding: 'iso-8859-15',
			method: 'post',
			update:d,
			data: Xdata,
			postBody: Xdata,
			onComplete: function(){
				d.removeClass('ajax-loading');
				if($('subContactForm'))
					initContactBt();
			}
		}).request();	
	});
}
//formulaire de devis feux
function initDevisFeuxFrm(){
	var d = $('formulaire_devis_feux').empty().addClass('ajax-loading');
	var myAjax = new Ajax('/misc/_devis_feux.php', {
		encoding: 'iso-8859-15',
		method: 'get',
		update:d,
		onComplete: function(){
			d.removeClass('ajax-loading');
			if($('subDevisFeuxForm'))
				initDevisFeuxBt();
		}
	}).request();
}
function initDevisFeuxBt(){
	$('subDevisFeuxForm').addEvent('click',function(event){
		event = new Event(event).stop();
		

		var Xdata = $('formulaire_devis_feux').toQueryString();
		var d = $('formulaire_devis_feux').empty().addClass('ajax-loading');
		var myAjax = new Ajax('/misc/_devis_feux.php', {
			encoding: 'iso-8859-15',
			method: 'post',
			update:d,
			data: Xdata,
			postBody: Xdata,
			onComplete: function(){
				d.removeClass('ajax-loading');
				if($('subDevisFeuxForm'))
					initDevisFeuxBt();
			}
		}).request();	
	});
}
//formulaire de devis mobilier
function initDevisMobilierFrm(){
	var d = $('formulaire_devis_mobilier').empty().addClass('ajax-loading');
	var myAjax = new Ajax('/misc/_devis_mobilier.php', {
		encoding: 'iso-8859-15',
		method: 'get',
		update:d,
		onComplete: function(){
			d.removeClass('ajax-loading');
			if($('subDevisMobilierForm'))
				initDevisMobilierBt();
		}
	}).request();
}
function initDevisMobilierBt(){
	$('subDevisMobilierForm').addEvent('click',function(event){
		event = new Event(event).stop();
		

		var Xdata = $('formulaire_devis_mobilier').toQueryString();
		var d = $('formulaire_devis_mobilier').empty().addClass('ajax-loading');
		var myAjax = new Ajax('/misc/_devis_mobilier.php', {
			encoding: 'iso-8859-15',
			method: 'post',
			update:d,
			data: Xdata,
			postBody: Xdata,
			onComplete: function(){
				d.removeClass('ajax-loading');
				if($('subDevisMobilierForm'))
					initDevisMobilierBt();
			}
		}).request();	
	});
}
//formulaire de devis vitrines
function initDevisVitrinesFrm(){
	var d = $('formulaire_devis_vitrines').empty().addClass('ajax-loading');
	var myAjax = new Ajax('/misc/_devis_vitrines.php', {
		encoding: 'iso-8859-15',
		method: 'get',
		update:d,
		onComplete: function(){
			d.removeClass('ajax-loading');
			if($('subDevisVitrinesForm'))
				initDevisVitrinesBt();
		}
	}).request();
}
function initDevisVitrinesBt(){
	$('subDevisVitrinesForm').addEvent('click',function(event){
		event = new Event(event).stop();
		

		var Xdata = $('formulaire_devis_vitrines').toQueryString();
		var d = $('formulaire_devis_vitrines').empty().addClass('ajax-loading');
		var myAjax = new Ajax('/misc/_devis_vitrines.php', {
			encoding: 'iso-8859-15',
			method: 'post',
			update:d,
			data: Xdata,
			postBody: Xdata,
			onComplete: function(){
				d.removeClass('ajax-loading');
				if($('subDevisVitrinesForm'))
					initDevisVitrinesBt();
			}
		}).request();	
	});
}
//formulaire de devis reliure
function initDevisReliureFrm(){
	var d = $('formulaire_devis_reliure').empty().addClass('ajax-loading');
	var myAjax = new Ajax('/misc/_devis_reliure.php', {
		encoding: 'iso-8859-15',
		method: 'get',
		update:d,
		onComplete: function(){
			d.removeClass('ajax-loading');
			if($('subDevisReliureForm'))
				initDevisReliureBt();
		}
	}).request();
}
function initDevisReliureBt(){
	$('subDevisReliureForm').addEvent('click',function(event){
		event = new Event(event).stop();
		

		var Xdata = $('formulaire_devis_reliure').toQueryString();
		var d = $('formulaire_devis_reliure').empty().addClass('ajax-loading');
		var myAjax = new Ajax('/misc/_devis_reliure.php', {
			encoding: 'iso-8859-15',
			method: 'post',
			update:d,
			data: Xdata,
			postBody: Xdata,
			onComplete: function(){
				d.removeClass('ajax-loading');
				if($('subDevisReliureForm'))
					initDevisReliureBt();
			}
		}).request();	
	});
}
// moteur de recherche des vrp
function initSearchVRP(){
	$('departements_vrp').addEvent('change',function(event){
		event = new Event(event).stop();
		var val = $('departements_vrp').value;
		var dep = val.split(";");
		if(dep[0]!=""){
			document.location = "/services/contact-terrain,"+dep[1]+".html?ind="+dep[0];
		}
	});
}
// Fonction de recherche
function doSearch(){
	var str = $('q').value;
	var inf = $('t').value;
	str=str.replace('/', ' ');
	str=str.replace(',', ' ');
	str=str.replace('°', ' ');
	var reg = /^[a-zA-Z0-9._àäâéèëêïîôöùûüçØø®\'\+\- ]{2,}$/;
	if((reg.exec(str)!=null)&&(str!="Rechercher")){
		$('form_search').action = "/"+ $('t').value + "/rechercher.html";
		$('form_search').submit();
	}
}
// Initialisation des Events sur les boutons des pages
function initButtons(){
	$('addBookmark').addEvent('click',function(event){
		event = new Event(event).stop();
		doBookmark();
	});
	$(document.body).getElements('a[class^=LinkEmail]').each(function(el){
		$(el).addEvent('click',function(event){
			event = new Event(event).stop();
			var loc = el.href;
			loc = loc.replace("_@ntisp@m_","@");
			document.location = loc;
		});

	

	});
}

// Bookmark des pages
function doBookmark(){
	if ( navigator.appName != 'Microsoft Internet Explorer' ){
		window.sidebar.addPanel(document.title,document.location,"");
	}else{
		window.external.addfavorite(document.location,document.title);
	}
}
function intval( mixed_var, base ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: stensi
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: intval('Kevin van Zonneveld');
    // *     returns 1: 0
    // *     example 2: intval(4.2);
    // *     returns 2: 4
    // *     example 3: intval(42, 8);
    // *     returns 3: 42
    // *     example 4: intval('09');
    // *     returns 4: 9
 
    var tmp;
 
    if( typeof( mixed_var ) == 'string' ){
        tmp = parseInt(mixed_var*1);
        if(isNaN(tmp) || !isFinite(tmp)){
            return 0;
        } else{
            return tmp.toString(base || 10);
        }
    } else if( typeof( mixed_var ) == 'number' && isFinite(mixed_var) ){
        return Math.floor(mixed_var);
    } else{
        return 0;
    }
}
// popup du diaporama
function showDiapo(id,cp,nb) {
	var largeur = 800;
	var hauteur = 800;
	var top=(screen.height-hauteur)/2;
	var left=(screen.width-largeur)/2;
	window.open("/diaprod.php?idp="+id+"&cp="+cp+"&nb="+nb,"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+",scrollable=true");
}

window.addEvent('domready', function(){
	initForms();
	initButtons();
});
