var fondDiv = false;
var dataDiv = false;
// Ajout a la newsletter
function addNewsletter(idForm) {
	
	if(!fondDiv){
		fondDiv = document.createElement('div');
		fondDiv.style.position = 'absolute';
		document.body.appendChild(fondDiv);
	}
	fondDiv.innerHTML = '';
	//alert(getHeight());

	$(fondDiv).setStyles({
	   backgroundColor: '#000000',
	   width: '100%',
	   height: getScrollHeight(),
	   position: 'absolute',
	   opacity: '0.5',
	   overflow: 'hidden',
	   top: '0px',
	   left: '0px',
	   zIndex: '1500',
	   visibility: 'hidden'
	});

	if(!dataDiv){
		dataDiv = document.createElement('div');
		dataDiv.style.position = 'absolute';
		document.body.appendChild(dataDiv);
	}

	$(dataDiv).setStyles({
	   backgroundColor: '#000000',
	   width: '300px',
	   height: '300px',
	   position: 'absolute',
	   border: '1px solid #000',
	   top: '50%',
	   left: '50%',	   
	   marginLeft: '-150px',	   
	   marginTop: (getScrollTop()-150) + 'px',	   	   	   
	   zIndex: '1600',
	   visibility: 'hidden'
	});	

	new Ajax(
		'/js/ajax/newsletter.php',
		{
			update: dataDiv,
			data:$(idForm).toQueryString(),
			onSuccess: function(req) {

					$$(fondDiv).setStyles({visibility: 'visible' });
					$$('select').setStyles({visibility: 'hidden' });
					$$(dataDiv).setStyles({visibility: 'visible' });
	
			},
			evalScripts: true
		}
	).request();		
	
	
	
}



function addLogin(idForm) {
	
	if(!fondDiv){
		fondDiv = document.createElement('div');
		fondDiv.style.position = 'absolute';
		document.body.appendChild(fondDiv);
	}
	fondDiv.innerHTML = '';
	//alert(getHeight());

	$(fondDiv).setStyles({
	   backgroundColor: '#000000',
	   width: '100%',
	   height: getScrollHeight(),
	   position: 'absolute',
	   opacity: '0.5',
	   overflow: 'hidden',
	   top: '0px',
	   left: '0px',
	   zIndex: '1500',
	   visibility: 'hidden'
	});

	if(!dataDiv){
		dataDiv = document.createElement('div');
		dataDiv.style.position = 'absolute';
		document.body.appendChild(dataDiv);
	}

	$(dataDiv).setStyles({
	   backgroundColor: '#000000',
	   width: '300px',
	   height: '300px',
	   position: 'absolute',
	   border: '1px solid #000',
	   top: '50%',
	   left: '50%',	   
	   marginLeft: '-150px',	   
	   marginTop: (getScrollTop()-150) + 'px',	   	   	   
	   zIndex: '1600',
	   visibility: 'hidden'
	});	

	new Ajax(
		'/js/ajax/login.php',
		{
			update: dataDiv,
			data:$(idForm).toQueryString(),
			onSuccess: function(req) {

					$$(fondDiv).setStyles({visibility: 'visible' });
					$$('select').setStyles({visibility: 'hidden' });
					$$(dataDiv).setStyles({visibility: 'visible' });
	
			},
			evalScripts: true
		}
	).request();		
	
	
	
}



function removeDiv()
{
	if(fondDiv){
		$(fondDiv).remove();
		fondDiv = false;
	}	
	if(dataDiv){
		$(dataDiv).remove();
		dataDiv = false;
	}
	$$('select').setStyles({visibility: 'visible' });		
}

function changeMenu(menu_id) {
	

	// On réinitialise tous les menus
	var main_menu_items = $$(".grandmenu");

	main_menu_items.each(function(item){
		var old_id = item.id;		
		
		$(old_id).style.backgroundColor="transparent";
		$(old_id).style.backgroundImage="url(/styles/images/header/onglet.png)";
		$(old_id).style.backgroundRepeat="no-repeat";
		$(old_id).style.backgroundPosition="bottom left";
		$(old_id).style.color="#55b0db";
		
		data = item.id.split('_');
		$("supermenu_a_"+data[1]).style.color="#55b0db";
	})
	

	// On active que celui qui nous interesse	
	if($("supermenu_"+menu_id)) {
		$("supermenu_"+menu_id).style.backgroundColor="transparent";
		$("supermenu_"+menu_id).style.backgroundImage="url(/styles/images/header/onglet_hover.png)";
		$("supermenu_"+menu_id).style.backgroundRepeat="no-repeat";
		$("supermenu_"+menu_id).style.backgroundPosition="bottom left";		
		
		$("supermenu_a_"+menu_id).style.color="#dfdfdf";
	}
	
	
	// Cas des sous menus
	// On initialise
	var main_menu_items = $$('.niveau2');
	
	main_menu_items.each(function(item){
		var old_id = item.id;		
		$(old_id).style.display="none";
	})		
	if($("sub_"+menu_id)) {
		$('noss').style.display="none";
		$('sub_'+menu_id).style.display="block";
	}else{
		$('noss').style.display="none";
	}
}

function change_reference(obj, etat) {
	if(etat==1) {
		$(obj).style.backgroundColor="#fff";
		$(obj).style.backgroundImage="url(/styles/images/reference/bordure.png)";
		$(obj).style.backgroundRepeat="no-repeat";
		$(obj).style.backgroundPosition="top left";			
	}else{
		$(obj).style.background="none";
	}
}




 

function infobulle(id, event){ 

		  if( window.event)
		    event = window.event;
			
			var x = event.clientX;
			var y = event.clientY;


document.getElementById('bulle').style.visibility = 'visible'; 
document.getElementById('bulle').style.left = (x+10)+"px"; 
document.getElementById('bulle').style.top = (y+20+getScrollTop())+"px"; 

txt = $("txt_"+id).innerHTML;
document.getElementById('bulle').innerHTML = txt;


//

} 

function infobulle_cache(){
	document.getElementById('bulle').style.visibility = 'hidden';
}


function changebordertext(name,etat) {
	if(etat) {
		name.style.border="green 1px solid";
	}else{
		name.style.border="red 1px solid";
	}
}

function VerifMail(value)
	{
	a = value;
	valide1 = false;
	
	for(var j=1;j<(a.length);j++){
		if(a.charAt(j)=='@'){
			if(j<(a.length-4)){
				for(var k=j;k<(a.length-2);k++){
					if(a.charAt(k)=='.') valide1=true;
				}
			}
		}
	}
	if(valide1==false) return false;
	return true;
	}




function verifForm(name, type, length) {	
	// TEXT
	if(type=="text") {		
		if(length==0 && name.value.length != 0) {
			changebordertext(name,true);
		}else{
			if(length > name.value.length || name.value.length == 0) {
				changebordertext(name,false);
			}else{
				changebordertext(name,true);
			}
		}
	}
	
	// MAIL
	if(type=="email") {		
		if(length==0 && name.value.length != 0) {
			if(VerifMail(name.value)) {
				changebordertext(name,true);
			}else{
				changebordertext(name,false);
			}
		}else{
			if(length > name.value.length || name.value.length == 0) {
				if(VerifMail(name.value)) {
					changebordertext(name,true);
				}else{
					changebordertext(name,false);
				}
			}else{
				changebordertext(name,true);
			}
		}		
	}
	
	
}


var Error_form = 0;

// Formulaire
function check(champ,NbCarac,idChamp)
{

	if(champ.value.length < NbCarac)
	{				
			document.getElementById("valid" + idChamp).innerHTML="<img src='/styles/images/check/croix-rouge.gif' alt='Non valide' />";
			champ.style.border="#B30000 1px solid";
			champ.style.background="#FFD4D7";						
			
			document.getElementById("err" + idChamp).style.display = "block";	
			document.getElementById("err" + idChamp).innerHTML = "Champ obligatoire";				
			
			Error_form++;	
	}
	else{
			document.getElementById("valid" + idChamp).innerHTML="<img src='/styles/images/check/info_ajax_valid.gif' alt='valide' />";
			champ.style.border="#00CC00 1px solid";
			champ.style.background="#DCEFB3";			
			
			document.getElementById("err" + idChamp).style.display = "none";	
			document.getElementById("err" + idChamp).innerHTML = "";					
						
	}
	
}

function checkEmail(champ)
{
	
	a = champ.value;
	valide1 = false;
	
	for(var j=1;j<(a.length);j++){
		if(a.charAt(j)=='@'){
			if(j<(a.length-4)){
				for(var k=j;k<(a.length-2);k++){
					if(a.charAt(k)=='.') valide1=true;
				}
			}
		}
	}
	if(valide1==false) {
		document.getElementById("validEmail").innerHTML="<img src='/styles/images/check/croix-rouge.gif' alt='Non valide' />";
		champ.style.border="#B30000 1px solid";
		champ.style.background="#FFD4D7";	
		$('errEmail').style.display = "block";
		if(a == "") $('errEmail').innerHTML = "Champ obligatoire";
		else $('errEmail').innerHTML = "Le format de l'email n'est pas correct.";
		Error_form++;	
	}
	else {
		document.getElementById("validEmail").innerHTML="<img src='/styles/images/check/info_ajax_valid.gif' alt='valide' />";
		champ.style.border="#00CC00 1px solid";
		champ.style.background="#DCEFB3";
		$('errEmail').style.display = "none";
		$('errEmail').innerHTML ="";
	}

}