var adaptSize=false;
var v_coef=100;
var okmenu=true;
var pagehistory=0;
var pageold=0;

window.onresize=adaptContentTO;

function _(o) {
	if (!o) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById(o)) return false;
	return document.getElementById(o);
}

function init() {
	adaptContent();
	//Safari
	/*
	browserV= navigator.appVersion;
	if (browserV.match(/Safari.*?/)) {
		_('imagtransmenu').style.width='263px';
	}
	*/
}

function adaptContent() {
	myWidth=getWidth();
	myBckgndLeft=(myWidth-1828)/2;
	_('page').style.backgroundPosition=myBckgndLeft+'px 0px';
	myHeight=getHeight();
	if (myHeight<570) {
		myHeightPage='570px';
	} else {
		myHeight-=11;
		myHeightPage=myHeight+'px';
	}
	_('page').style.height=myHeightPage;
	/* setLeft('content', 974); */
	adaptSize=false;
}

function adaptContentTO() {
	if (!adaptSize) {
		adaptSize=true;
		setTimeout("adaptContent()", 500);
	}
}

function xhrRequest(type) {
	var xhrSend;
	if (!type) {
		type = 'text';
	}
	if (window.ActiveXObject) {
		try {
			xhrSend = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhrSend = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	} else if (window.XMLHttpRequest) {
		xhrSend = new XMLHttpRequest();
		if (xhrSend.overrideMimeType) {
			xhrSend.overrideMimeType('text/' + type);
		}
	}
	return (xhrSend);
}

function getWidth() {
  myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
	myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	myWidth = document.body.clientWidth;
  }
  return myWidth;
}	

function getHeight() {
  myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
	myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function setLeft(myDiv, myDivSize) {
	myWidth=getWidth();
	myWidth=(myWidth-myDivSize)/2;
	if (myWidth<0) myWidth=0;
	myWidth=myWidth+"px";
	_(myDiv).style.left=myWidth;
	return;
}

function cache_all_bloc() {
	for (i=0;i<=3;i++) {
		if (_('bloc_'+i)) _('bloc_'+i).style.display='none';
	}
	return;
}

function valid_bouton(o) {  // fonction d'appel des boutons flash
	if (!o || o=="") return false;
	//if (o==9) o=pageold;
	myDiv=_('blocleft');
	if (!myDiv) return false;
	//gere_bouton_retour(o);
	switch (o) {
		case 0:
			// 0 > Home
			cache_all_bloc();
			myDiv.className='fondl1';
			if (_('bloc_'+o)) _('bloc_'+o).style.display='block';		
		break;
		case 1:
			// 1 > Imprimer
			//send_impression();
		break;
		case 2:
		case 3:
			// 2 > Savoir
			// 3 > Remboursement
			cache_all_bloc();
			myDiv.className='fondl'+o;
			if (_('bloc_'+o)) _('bloc_'+o).style.display='block';
			if (o==3) {
				lance_tag();
				if (_('NOM_REMB')) _('NOM_REMB').focus();
			}
		break;
		case 5:
			_('bloc_0').style.display='none'; 
			_('bloc_3').style.display='none'; 
			_('bloc_2').style.display='block';
			_('blocleft').className='fondl4';
			
		break;
		case 6:
			_('bloc_0').style.display='none'; 
			_('bloc_2').style.display='none'; 
			_('bloc_3').style.display='block';
			_('blocleft').className='fondl3';
		break;
		case 7:
			_('bloc_4').style.display='none'; 
			_('bloc_5').style.display='block';
			_('blocright').className='fondr2';
		break;
		case 8:
			_('bloc_2').style.display='none'; 
			_('bloc_0').style.display='block'; 
			_('blocleft').className='fondl5';
		break;
		case 9:
			_('bloc_3').style.display='none';
			_('bloc_0').style.display='block'; 
			_('blocleft').className='fondl1'; 
		break;
		case 10:
			_('bloc_5').style.display='none'; 
			_('bloc_4').style.display='block'; 
			_('blocright').className='fondr1';
		break;
		default:
			return false;
		break;
	}
}

function gere_bouton_retour(o) {
	switch (o) {
		case 0:	
			if (_('bouton_fleche_retour')) {
				_('bouton_fleche_retour').style.display='none';
			}	
		break;
		case 1:	
			//rien
		break;
		case 2:	
		case 3:	
			if (_('bouton_fleche_retour')) {
				_('bouton_fleche_retour').style.display='block';
			}		
		break;
		default:
			//rien
		break;
	}
	_('bouton_fleche_retour').onclick=function() { valid_bouton(9) };
	pageold=pagehistory;
	pagehistory=o;
}

function open_popin_nl() {
	if (!_('EMAIL_NL')) return false;
	/*
	myemail=_('EMAIL_NL').value;
	if (myemail=='') {
		alert('Veuillez inscrire votre adresse email.');
		_('EMAIL_NL').focus();
		return false;
	}
	if (!email_check(myemail)) {
		alert('Votre adresse email est incorrect. Veuillez corriger.');
		_('EMAIL_NL').focus();
		return false;
	}
	*/
	if (_('EMAIL_NEWS') && _('newsletterpopin')) {
		_('newsletterpopin').style.display='block';
		_('EMAIL_NEWS').focus();
	}
}

function valid_popin_nl() {
	if (!_('EMAIL_NEWS')) return false;
	if (_('EMAIL_NEWS').value=='') {
		alert('Veuillez inscrire votre adresse email.');
		_('EMAIL_NEWS').focus();
		return false;
	}
	if (!email_check(_('EMAIL_NEWS').value)) {
		alert('Votre adresse email est incorrect. Veuillez corriger.');
		_('EMAIL_NEWS').focus();
		return false;
	}	
	nom=_('NOM_NEWS').value;
	prenom=_('PRENOM_NEWS').value;
	optin=-1;
	if (_('OPTIN2_NEWS_0').checked) optin=0;
	if (_('OPTIN2_NEWS_1').checked) optin=1;
	if (nom && prenom && optin>=0) {
		email=_('EMAIL_NEWS').value;
		save_request_nl(nom, prenom, email, optin);
	} else {
		alert('Veuillez renseigner tous les champs.');
		return false;
	}
}

function disparait(myDivID, coef) {
	myDiv=_(myDivID);
	if (!myDiv) return false;
	if (!coef) coef=parseInt(v_coef);
	coef=coef-10;
	setOpacity(myDiv, coef);
	if (coef > 0) {
		setTimeout("disparait('"+myDivID+"',"+coef+");", 100);
	} else {
		setOpacity(myDiv, v_coef);
		myDiv.style.display='none';
		myDiv.className='nlform';
		if (_('nlokid')) _('nlokid').style.display='none';
		if (_('nlformid')) _('nlformid').style.display='block';
	}
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity; 
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";  
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100; 
	// Vieux Mozilla & Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, Firefox and Mozilla recents, CSS3
	obj.style.opacity = opacity/100;
}

function save_request_nl(nom, prenom, email, optin) {
	var xhrRec = xhrRequest('html');
	xhrRec.onreadystatechange = function() {
		if(xhrRec.readyState == 4 && xhrRec.status == 200) {
			ret = xhrRec.responseText;
			if (ret == 'OK') {
				myDiv=_('newsletterpopin');
				//myDiv.innerHTML='<div class="cb1">&nbsp;</div><img src="/imgs/popin_news_bckgnd_bottom.png" alt="" title="" id="bottom_popin_news" width="356" height="29" />';
				myDiv.className='nlok';
				_('nlokid').style.display='block';
				_('nlformid').style.display='none';
				_('EMAIL_NL').value='';
				_('NOM_NEWS').value='';
				_('PRENOM_NEWS').value='';				
				setTimeout("disparait('newsletterpopin')", 1000);
			} else {
				alert(ret);
				_('EMAIL_NL').value='';
				_('NOM_NEWS').value='';
				_('PRENOM_NEWS').value='';
				_('newsletterpopin').style.display='none';
				return false;
			}		
		}
	}
	xhrRec.open("POST",'ajax_andros.php',true);
	xhrRec.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrRec.send("type=newsletter&mode=1&prenom="+prenom+"&nom="+nom+"&email="+email+"&optin="+optin);
}

function email_check(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1) return false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
	if (str.indexOf(at,(lat+1))!=-1) return false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
	if (str.indexOf(dot,(lat+2))==-1) return false;
	if (str.indexOf(" ")!=-1) return false;
	return true					
}

function send_impression() {
	prenom=_('PRENOM_REMB').value;
	nom=_('NOM_REMB').value;
	email=_('EMAIL_REMB').value;
	adresse=_('RUE_REMB').value;
	cp=parseInt(_('CP_REMB').value);
	ville=_('VILLE_REMB').value;
	optin1=-1; optin2=-1;
	if (_('OPTIN1_REMB_1').checked) optin1=1;
	if (_('OPTIN1_REMB_0').checked) optin1=0;
	if (_('OPTIN2_REMB_1').checked) optin2=1;
	if (_('OPTIN2_REMB_0').checked) optin2=0;
	if (prenom=="" || nom=="" || email=="" || adresse=="" || cp<=0 || ville=="" || optin1<0 || optin2<0) {
		alert('Veuillez renseigner tous les champs.');
		return false;
	} else {
		if (!email_check(email)) {
			alert('Votre email est incorrect.');
			return false;
		} else {
			if (isNaN(cp)) {
				alert('Votre code postal est incorrect.');
				return false;
			} else {
				v_w=610; v_h=842; v_t=0; v_l=100;
				myUrl = 'passeport.php?prenom='+prenom+'&nom='+nom+'&adresse='+adresse+'&cp='+cp+'&ville='+ville;
				myTitle = "Andros :: Passeport pour le gout";
				if (document.all) myTitle='_blank';
				myStats = "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=yes,resizable=0,width="+v_w+",height="+v_h+",left="+v_l+",top="+v_t;
				mywin=window.open(myUrl,myTitle,myStats);
				mywin.focus();
				save_data_popup_passeport(prenom, nom, adresse, cp, ville, email, optin1, optin2);
			}
		}
	} 
}

function save_data_popup_passeport(prenom, nom, adresse, cp, ville, email, optin1, optin2) {
	var xhrRec = xhrRequest('html');
	xhrRec.onreadystatechange = function() {
		if(xhrRec.readyState == 4 && xhrRec.status == 200) {
			ret = xhrRec.responseText;
			if (ret == 'OK') {
				//openWinPasseport(prenom, nom, adresse, cp, ville);
				myDiv=_('blocleft');
				cache_all_bloc();
				myDiv.className='fondl1';
				if (_('bloc_0')) _('bloc_0').style.display='block';
			} else {
				alert(ret);
				return false;
			}		
		}
	}
	xhrRec.open("POST",'ajax_andros.php',true);
	xhrRec.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrRec.send("type=passeport&mode=1&prenom="+prenom+"&nom="+nom+"&email="+email+"&optin1="+optin1+"&optin2="+optin2+"&cp="+cp+"&adresse="+adresse+"&ville="+ville);	
}

function openWinPasseport(prenom, nom, adresse, cp, ville) {
	v_w=610; v_h=842; v_t=0; v_l=100;
	myUrl = 'passeport.php?prenom='+prenom+'&nom='+nom+'&adresse='+adresse+'&cp='+cp+'&ville='+ville;
	myTitle = "Andros :: Passeport pour le gout";
	if (document.all) myTitle='_blank';
	myStats = "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=yes,resizable=0,width="+v_w+",height="+v_h+",left="+v_l+",top="+v_t;
	mywin=window.open(myUrl,myTitle,myStats);
	mywin.focus();	
}

function openWinContactA() {
	v_w=567; v_h=380; v_t=50; v_l=100;
	myUrl = 'forms/form.php?ref=forms/autres_infos.html';
	myTitle = "Andros :: Contactez nous";
	if (document.all) myTitle='_blank';
	myStats = "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=0,width="+v_w+",left="+v_l+",top="+v_t;
	mywin=window.open(myUrl,myTitle,myStats);
	mywin.focus();	
}

function openWinContactR() {
	v_w=567; v_h=480; v_t=10; v_l=100;
	myUrl = 'forms/form.php?ref=forms/reclam_conso.html';
	myTitle = "Andros :: Contactez nous";
	if (document.all) myTitle='_blank';
	myStats = "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=0,width="+v_w+",left="+v_l+",top="+v_t;
	mywin=window.open(myUrl,myTitle,myStats);
	mywin.focus();	
}

function openWinMentions() {
	v_w=550; v_h=660; v_t=10; v_l=100;
	myUrl = 'forms/forms/mentions_legales.html';
	myTitle = "Andros :: Mentions légales";
	if (document.all) myTitle='_blank';
	myStats = "toolbar=no,location=no,directories=no,status=no,scrollbars=no,menubar=no,resizable=0,width="+v_w+",height="+v_h+",left="+v_l+",top="+v_t;
	mywin=window.open(myUrl,myTitle,myStats);
	mywin.focus();
}

function display_sub(sens) {
	switch (sens) {
		case 0:
			okmenu=false;
			setTimeout("cacheTimeout_sub(0)", 500);
			break;
		case 1:	
			okmenu=true;
			cacheTimeout_sub(1);
			break;
		default:
			break;
	}
}

function cacheTimeout_sub(sens) {
	switch (sens) {
		case 0:
			if (!okmenu) {
				_('subcontact').style.display="none";	
			}
		case 1:	
			if (okmenu) {
				_('subcontact').style.display="block";
			}
			break;
		default:
			break;
	}			
}