function hasParameter(uri, name)
{
	re = new RegExp("[\?\&]" + name + "=");
	return (uri.search(re) != -1);
}

function addParameter(uri, name, value)
{
	re = new RegExp("[\?]");
	if (uri.search(re) != -1) {uri2 = uri + "&";}
	else {uri2 = uri + "?";}
	uri2 = uri2 + name + "=" + value;
	return uri2;
}

function replaceParameter(uri, name, value)
{
	if (hasParameter(uri, name)) {
		re = new RegExp("([\?\&])" + name + "=([^\&]*)(.*)$","");
		uri2 = uri.replace(re, "$1" + name + "=" + value + "$3");
	} else {
		uri2 = addParameter(uri, name, value);
	}
	return uri2;
}

function deleteParameter(uri, name)
{
	re = new RegExp("([\&])" + name + "=([^\&]*)");
	uri = uri.replace(re, "");
	re = new RegExp("([\?])" + name + "=([^\&]*)");
	uri = uri.replace(re, "$1");
	re = new RegExp("[\?][\&]");
	uri = uri.replace(re, "?");
	re = new RegExp("[\?]$");
	uri = uri.replace(re, "");
	return uri;
}

function getURL(page, content, params)
{
	uri = URI;

	if (page > "") {
		uri = replaceParameter(uri, "page", page);
		if (content > "") {
			uri = replaceParameter(uri, "portlet", content);
		} else {
			uri = deleteParameter(uri, "portlet");
		}
	} else {
		if (content > "") {
			if (hasParameter(uri, "page")) {
				uri = replaceParameter(uri, "portlet", content);
			} else {
				uri = replaceParameter(uri, "page", content);
			}
		}
	}

	return (uri + params);
}

function goToPage(page, content, params)
{
	window.location = getURL(page, content, params);
}

function goToPopup(name, page, content, params, width, height, left, top, options)
{
	if (options == '')
	{
		options = "toolbar=no,location=no,directories=no,status=no,";
		options += "menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,";
	}
	if (left > '') options += "left=" + left + ",";
	if (top > '') options += "top=" + top + ",";
	options += "width=" + width + ",height=" + height;
	if (name == '') name = "atlantal";

	window.open(getURL(page, content, params), name, options);
}

function popup(link)
{
	var options = "toolbar=no,location=no,directories=no,status=no,";
	options += "menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,";
	options += "width=800,height=500";

	window.open(link, "Dubecq", options);
}

function popupfiche(link)
{
	var options = "toolbar=no,location=no,directories=no,status=no,";
	options += "menubar=no,scrollbars=no,resizable=no,copyhistory=no,";
	options += "width=500,height=400";

	window.open(link, "test", options);
}

function selectfamille(me)
{
		if(me.options[me.selectedIndex].value!="-1")
		{
		  window.location = me.options[me.selectedIndex].value;
		  me.selectedIndex=0;
		}
}

function VueArticle(id_article, lang)
{
	
	defUrl = '/?page=FicheLayout&FicheLayout_fiche='+id_article;
	
	if(lang == "en")
	{
		defUrl = '/en/?page=FicheLayout&FicheLayout_fiche='+id_article;
	}

	divfiche = document.getElementById("flux");

	divfiche.style.left=((screen.width/2)-300)+"px";
	divfiche.style.top=(document.documentElement.scrollTop+80)+"px";

	divcontainer = document.getElementById("container");
 	divcontainer.style.filter="alpha(opacity=60)";
 	divcontainer.style.opacity=".40";

	/* ALIENOR.NET AJAX SYNCRHO */
	var http_request = getHTTPObject();
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			hideSelect();
			divfiche.style.visibility = 'visible';
			divfiche.innerHTML = http_request.responseText;
		}
	};
	http_request.open('GET', defUrl, true);
	http_request.send(null);
}

function VueArticlePrim(id_article_prim,annee, lang)
{
	defUrl = '/?page=FichePrimLayout'+annee+'&FichePrimLayout'+annee+'_fiche='+id_article_prim;
	
	if(lang == "en")
	{
		defUrl = '/en/?page=FichePrimLayout'+annee+'&FichePrimLayout'+annee+'_fiche='+id_article_prim;
	}

	divfiche = document.getElementById("flux");

	divfiche.style.left=((screen.width/2)-300)+"px";
	divfiche.style.top=(document.documentElement.scrollTop+80)+"px";

	divcontainer = document.getElementById("container");
 	divcontainer.style.filter="alpha(opacity=60)";
 	divcontainer.style.opacity=".40";

	/* ALIENOR.NET AJAX SYNCRHO */
	var http_request = getHTTPObject();
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			hideSelect();
			divfiche.style.visibility = 'visible';
			divfiche.innerHTML = http_request.responseText;
		}
	};
	http_request.open('GET', defUrl, true);
	http_request.send(null);
}

function AjoutPanier(id_article,place_qte, lang)
{

	qte = document.getElementById(place_qte+id_article);

	if(qte.value>=0)
		qte_verf = qte.value;
	else
		qte_verf = 1;

	defUrl = '/?page=PanierLayout&PanierLayout_fiche='+id_article+'&PanierLayout_qte='+parseInt(qte_verf);
	
	if(lang == "en")
	{
		defUrl = '/en/?page=PanierLayout&PanierLayout_fiche='+id_article+'&PanierLayout_qte='+parseInt(qte_verf);
	}


	divfiche = document.getElementById("flux");

	divfiche.style.left=((screen.width/2)-400)+"px";
	divfiche.style.top=(document.documentElement.scrollTop+80)+"px";

	divcontainer = document.getElementById("container");
 	divcontainer.style.filter="alpha(opacity=60)";
 	divcontainer.style.opacity=".40";


	/* ALIENOR.NET AJAX SYNCRHO */
	var http_request = getHTTPObject();
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			hideSelect();
			divfiche.style.visibility = 'visible';
			divfiche.innerHTML = http_request.responseText;
			vuepanierbtn(lang);
		}
	};
	http_request.open('GET', defUrl, true);
	http_request.send(null);
}

/*
function vuepanierbtn(lang)
{
	objimg = document.getElementById("vuepanier");
	
	if(lang == "en")
	{
		objimg.innerHTML = "<a href=\"javascript:VuePanier('"+lang+"');\"><img src=\"/img/ext/"+layout_couleur+"/vuepanier_off_EN.gif\" onMouseOver=\"this.src='/img/ext/"+layout_couleur+"/vuepanier_on_EN.gif'\" onMouseOut=\"this.src='/img/ext/"+layout_couleur+"/vuepanier_off.gif'\" border=\"0\"></a>";
	}
	else {
		objimg.innerHTML = "<a href=\"javascript:VuePanier('"+lang+"');\"><img src=\"/img/ext/"+layout_couleur+"/vuepanier_off.gif\" onMouseOver=\"this.src='/img/ext/"+layout_couleur+"/vuepanier_on.gif'\" onMouseOut=\"this.src='/img/ext/"+layout_couleur+"/vuepanier_off.gif'\" border=\"0\"></a>";
	}
	
}*/

function vuepanierbtn(lang)
{
	objimg = document.getElementById("vuepanier");
	
	if(lang == "en")
	{
		objimg.innerHTML = "<a href=\"javascript:VuePanier('"+lang+"');\" class='row-top'>See my basket</a>";
	}
	else {
		objimg.innerHTML = "<a href=\"javascript:VuePanier('"+lang+"');\" class='row-top'>Voir mon panier</a>";
	}
	
}

/*
function vuepanierprimbtn(annee, lang)
{
	objimg = document.getElementById("vuepanier");
	
	if(lang == "en")
	{
		objimg.innerHTML = "<a href=\"javascript:VuePanierPrim('"+annee+"','"+lang+"');\"><img src=\"/img/ext/"+layout_couleur+"/vuepanier_off_EN.gif\" onMouseOver=\"this.src='/img/ext/"+layout_couleur+"/vuepanier_on_EN.gif'\" onMouseOut=\"this.src='/img/ext/"+layout_couleur+"/vuepanier_off.gif'\" border=\"0\"></a>";
	}
	else {
		objimg.innerHTML = "<a href=\"javascript:VuePanierPrim('"+annee+"','"+lang+"');\"><img src=\"/img/ext/"+layout_couleur+"/vuepanier_off.gif\" onMouseOver=\"this.src='/img/ext/"+layout_couleur+"/vuepanier_on.gif'\" onMouseOut=\"this.src='/img/ext/"+layout_couleur+"/vuepanier_off.gif'\" border=\"0\"></a>";
	}
}*/

function vuepanierprimbtn(annee, lang)
{
	objimg = document.getElementById("vuepanier");
	
	if(lang == "en")
	{
		objimg.innerHTML = "<a href=\"javascript:VuePanierPrim('"+annee+"','"+lang+"');\" class='row-top'>See my basket</a>";
	}
	else {
		objimg.innerHTML = "<a href=\"javascript:VuePanierPrim('"+annee+"','"+lang+"');\" class='row-top'>Voir mon panier</a>";
	}
}

function VuePanier(lang)
{

	defUrl = '/?page=PanierLayout';
	
	if(lang == "en")
	{
		defUrl = '/en/?page=PanierLayout';
	}
	
	divfiche = document.getElementById("flux");

	divfiche.style.left=((screen.width/2)-400)+"px";
	divfiche.style.top=(document.documentElement.scrollTop+80)+"px";

	divcontainer = document.getElementById("container");
 	divcontainer.style.filter="alpha(opacity=60)";
 	divcontainer.style.opacity=".40";

	/* ALIENOR.NET AJAX SYNCRHO */
	var http_request = getHTTPObject();
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			hideSelect();
			divfiche.style.visibility = 'visible';
			divfiche.innerHTML = http_request.responseText;
		}
	};

	http_request.open('GET', defUrl, true);
	http_request.send(null);
}

function VuePanierPrim(annee, lang)
{
	defUrl = '/?page=PanierPrimLayout'+annee;
	
	if(lang == "en")
	{
		defUrl = '/en/?page=PanierPrimLayout'+annee;
	}
	
	divfiche = document.getElementById("flux");

	divfiche.style.left=((screen.width/2)-400)+"px";
	divfiche.style.top=(document.documentElement.scrollTop+80)+"px";

	divcontainer = document.getElementById("container");
 	divcontainer.style.filter="alpha(opacity=60)";
 	divcontainer.style.opacity=".40";

	/* ALIENOR.NET AJAX SYNCRHO */
	var http_request = getHTTPObject();
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			hideSelect();
			divfiche.style.visibility = 'visible';
			divfiche.innerHTML = http_request.responseText;
		}
	};
	http_request.open('GET', defUrl, true);
	http_request.send(null);
}

function AjoutPanierPrim(id_article,place_qte,annee, lang)
{

	qte = document.getElementById(place_qte+id_article);

	if(qte.value>=0)
		qte_verf = qte.value;
	else
		qte_verf = 1;

	defUrl = '/?page=PanierPrimLayout'+annee+'&PanierPrimLayout'+annee+'_fiche='+id_article+'&PanierPrimLayout'+annee+'_qte='+qte_verf;
	
	if(lang == "en")
	{
		defUrl = '/en/?page=PanierPrimLayout'+annee+'&PanierPrimLayout'+annee+'_fiche='+id_article+'&PanierPrimLayout'+annee+'_qte='+qte_verf;
	}
	
	divfiche = document.getElementById("flux");

	divfiche.style.left=((screen.width/2)-400)+"px";
	divfiche.style.top=(document.documentElement.scrollTop+80)+"px";

	divcontainer = document.getElementById("container");
 	divcontainer.style.filter="alpha(opacity=60)";
 	divcontainer.style.opacity=".40";

	/* ALIENOR.NET AJAX SYNCRHO */
	var http_request = getHTTPObject();
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			hideSelect();
			divfiche.style.visibility = 'visible';
			divfiche.innerHTML = http_request.responseText;
			vuepanierprimbtn(annee, lang);
		}
	};
	http_request.open('GET', defUrl, true);
	http_request.send(null);
}

function checkLang(){
	pageUrl = window.location.href;
	if(pageUrl.indexOf('/en/',0)=='-1'){
		lang = 'fr';
	} else { lang = 'en'; }
	return lang;
}

function VueActu(lang, id)
{
	defUrl = '/?page=ActuLayout&ActuLayout_id='+id;
	
	if(lang == "en")
	{
		defUrl = '/en/?page=ActuLayout&ActuLayout_id='+id;
	}
	
	divactu = document.getElementById("actualite");

	lang = checkLang();

	var http_request = getHTTPObject();

	/* ALIENOR.NET AJAX SYNCRHO */
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			divactu.innerHTML = http_request.responseText;
		}
		if (http_request.readyState == 1) {
			chargement = ( lang == 'fr' ? "Chargement de l'actualité..." : "Loading news..." );
			divactu.innerHTML = chargement;
		}
		if (http_request.readyState == 2) {
			divactu.innerHTML = "Interaction...";
		}
	};

	http_request.open('GET', defUrl, true);
	http_request.send(null);
}

function CloseFenetre()
{
	divfiche = document.getElementById("flux");
	divfiche.style.visibility = 'hidden';
	divcontainer = document.getElementById("container");
 	divcontainer.style.filter="alpha(opacity=100)";
  	divcontainer.style.opacity="1";
}

function opaque(div)
{
	divtoopaque = document.getElementById(div);
 	divtoopaque.style.filter="alpha(opacity=100)";
  	divtoopaque.style.opacity="1";
}


function getHTTPObject() {
  var xmlhttp;
  if(window.XMLHttpRequest) // Firefox
	   xmlhttp = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	else { // XMLHttpRequest non supporté par le navigateur
		lang = checkLang();
		if(lang == "en"){
			alert("Your browser doesn't support XMLHTTPRequest objects...");
		}else{
	  	alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	  }
	  return;
	}
  return xmlhttp;
}

function factToLivr()
{
	document.forms.form1.l_civilite[0].checked = document.forms.form1.f_civilite[0].checked;
	document.forms.form1.l_civilite[1].checked = document.forms.form1.f_civilite[1].checked;
	document.forms.form1.l_civilite[2].checked = document.forms.form1.f_civilite[2].checked;
	document.getElementById("l_nom").value = document.getElementById("f_nom").value;
	document.getElementById("l_prenom").value = document.getElementById("f_prenom").value;
	document.getElementById("l_adresse").value = document.getElementById("f_adresse").value;
	document.getElementById("l_cp").value = document.getElementById("f_cp").value;
	document.getElementById("l_ville").value = document.getElementById("f_ville").value;
	document.getElementById("l_civilite").value = document.getElementById("f_civilite").value;
}

function LivrChais()
{
	document.getElementById("l_nom").value = "MAISON F. DUBECQ";
	document.getElementById("l_prenom").value = "Retrait Commande au Chai";
	document.getElementById("l_adresse").value = "167 cours de l'Yser";
	document.getElementById("l_cp").value = "33800";
	document.getElementById("l_ville").value = "BORDEAUX";
	document.getElementById("l_civilite").value = 1;
}

function trim(string)
{
		return string.replace(/(^\s*)|(\s*$)/g,'');
}

function checkFormClient(page_form,page_type)
{
		if (check_textbox(page_type) || check_email())
    {
        return false;
    }
    return true;
}

function check_textbox(page_type)
{
	lang = checkLang();
	if (trim(document.getElementById("f_nom").value) == ""){
		if(lang == "en"){
			alert("Please enter your last name in the billing informations form");
		}else{
			alert("Veuillez indiquer un nom dans les informations de facturation");
		}
		return true;
	}

	if (trim(document.getElementById("f_prenom").value) == ""){
		if(lang == "en"){
			alert("Please enter your first name in the billing informations form");
		}else{
			alert("Veuillez indiquer un prenom dans les informations de facturation");
		}
		return true;
	}

	if (trim(document.getElementById("f_adresse").value) == ""){
		if(lang == "en"){
			alert("Please enter your address in the billing informations form");
		}else{
			alert("Veuillez indiquer une adresse dans les informations de facturation");
		}
		return true;
	}

	if (trim(document.getElementById("f_cp").value) == ""){
		if(lang == "en"){
			alert("Please enter your postal code in the billing informations form");
		}else{
			alert("Veuillez indiquer un code postal dans les informations de facturation");
		}
		return true;
	}

	if (trim(document.getElementById("f_ville").value) == ""){
		if(lang == "en"){
			alert("Please enter your city in the billing informations form");
		}else{
			alert("Veuillez indiquer une ville dans les informations de facturation");
		}
		return true;
	}

	if (trim(document.getElementById("f_tel").value) == ""){
		if(lang == "en"){
			alert("Please enter your phone number in the billing informations form");
		}else{
			alert("Veuillez indiquer un telephone dans les informations de facturation");
		}
		return true;
	}

	if(page_type == "magasin"){
		if (trim(document.getElementById("l_nom").value) == "")
		{
			if(lang == "en"){
				alert("Please enter your last name in the delivery informations form");
			} else {
				alert("Veuillez indiquer un nom dans les informations de livraison");
			}
			return true;
		}

		if (trim(document.getElementById("l_prenom").value) == "")
		{
			if(lang == "en"){
				alert("Please enter your first name in the delivery informations form");
			} else {
				alert("Veuillez indiquer un prenom dans les informations de livraison");
			}
			return true;
		}

		if (trim(document.getElementById("l_adresse").value) == "")
		{
			if(lang == "en"){
				alert("Please enter your address in the delivery informations form");
			} else {
				alert("Veuillez indiquer une adresse dans les informations de livraison");
			}
			return true;
		}

		if (trim(document.getElementById("l_cp").value) == "")
		{
			if(lang == "en"){
				alert("Please enter your postal code in the delivery informations form");
			} else {
				alert("Veuillez indiquer un code postal dans les informations de livraison");
			}
			return true;
		}

		if(trim(document.getElementById("l_cp").value).substring(0,2) == "20" ||
		trim(document.getElementById("l_cp").value).substring(0,2) == "99")
		{
			if(lang == "en"){
				alert("Please contact us for a delivery in overseas France or in Corse.");
			}else{
				alert("Merci de nous contacter pour une livraison hors France continentale.");
			}
			return true;
		}

		if (trim(document.getElementById("l_ville").value) == "")
		{
			if(lang == "en"){
				alert("Please enter your city in the delivery informations form");
			} else {
				alert("Veuillez indiquer une ville dans les informations de livraison");
			}
			return true;
		}

		if (trim(document.getElementById("l_pays").value) == "")
		{
			if(lang == "en"){
				alert("Please enter your country in the delivery informations form");
			} else {
				alert("Veuillez indiquer un pays dans les informations de livraison");
			}
			return true;
		}

	}

	return false;
}

function check_email()
{
	lang = checkLang();
 	re = /^[^\s@]+@[^\s@][^\s@]+\.[^\s@][^\s@]+$/;
	if (!re.test(document.form1.f_email.value))
	{
		if(lang == "en"){
			alert("Please enter a correct e-mail address");
		}else{
			alert("Veuillez saisir une adresse E-mail correcte");
		}
		return true;
	}
	return false;
}

function MoveToUrlCommande(url)
{
	/* var msg = 'Confirmez vous les données suivantes :\n\nINFORMATIONS FACTURATION : \n\n'+document.getElementById("f_prenom").value+' '+document.getElementById("f_nom").value+'\n'+document.getElementById("f_adresse").value+'\n'+document.getElementById("f_cp").value+' '+document.getElementById("f_ville").value+'\nTel. : '+document.getElementById("f_tel").value+'\nE-mail. : '+document.getElementById("f_email").value+'\n\nINFORMATIONS LIVRAISON : \n\n'+document.getElementById("l_prenom").value+' '+document.getElementById("l_nom").value+'\n'+document.getElementById("l_adresse").value+'\n'+document.getElementById("l_cp").value+' '+document.getElementById("l_ville").value; */

	lang = checkLang();
	if(lang == "en"){
		var msg = 'Please confirm the following informations :\n\nBILLING INFORMATIONS : \n\n'+document.getElementById("f_prenom").value+' '+document.getElementById("f_nom").value+'\n'+document.getElementById("f_adresse").value+'\n'+document.getElementById("f_cp").value+' '+document.getElementById("f_ville").value+'\nTel. : '+document.getElementById("f_tel").value+'\nE-mail. : '+document.getElementById("f_email").value;
	}else{
		var msg = 'Confirmez-vous les données suivantes :\n\nINFORMATIONS FACTURATION : \n\n'+document.getElementById("f_prenom").value+' '+document.getElementById("f_nom").value+'\n'+document.getElementById("f_adresse").value+'\n'+document.getElementById("f_cp").value+' '+document.getElementById("f_ville").value+'\nTel. : '+document.getElementById("f_tel").value+'\nE-mail. : '+document.getElementById("f_email").value;
	}
	divcontainer = document.getElementById("container");
 	divcontainer.style.filter="alpha(opacity=60)";
 	divcontainer.style.opacity=".40";

	if (window.confirm(msg))
	{
		window.location = url;
	}
	else
	{
		divcontainer = document.getElementById("container");
 		divcontainer.style.filter="alpha(opacity=100)";
  		divcontainer.style.opacity="1";
	}
}

function hideSelect() {
	for (j=0; j<document.forms.length; j++) {
		var theForm = document.forms[j]
		for(i=0; i<theForm.elements.length; i++){
			if(theForm.elements[i].type == "select-one") {
				theForm.elements[i].style.visibility = "hidden";
			}
		}
	}
}

function unhideSelect() {
	for (j=0; j<document.forms.length; j++) {
		var theForm = document.forms[j]
		for(i=0; i<theForm.elements.length; i++){
			if(theForm.elements[i].type == "select-one") {
				theForm.elements[i].style.visibility = "visible";
			}
		}
	}
}