// Affiche dans une popup les infos sur un type de livraison
function JS_panier_affiche_info_livraison(mode) {
	haut=500;
	larg=500;
	hauteur=Math.round((screen.availHeight-haut)/2);
	largeur=Math.round((screen.availWidth-larg)/2);
	window.open("pgs/livr" + mode + ".php","Info","scrollbars=yes,status=no,top="+hauteur+",left="+largeur+",width="+larg+",height="+haut);
}

// Affiche dans une popup les infos 3dsecure
function JS_panier_affiche_3dsecure() {
	haut=500;
	larg=500;
	hauteur=Math.round((screen.availHeight-haut)/2);
	largeur=Math.round((screen.availWidth-larg)/2);
	window.open("pgs/3dsecure.php","Info","scrollbars=yes,status=no,top="+hauteur+",left="+largeur+",width="+larg+",height="+haut);
}

// Change le mode de port
function JS_panier_change_mode_port(port) {
	document.f.actions.value="L" + port;
	document.f.submit();
}

// Saisie d'un code promo
function JS_promo(code) {
	document.f.actions.value="%" + code;
	document.f.submit();
}

// Change le pays de livraison
function JS_panier_change_pays() {
	if (document.f.Pays.value.substr(0,1)=="-")
	{
		alert("Vous devez sélectionner une destination valide !");
		return;
	}
	document.f.actions.value="PY"+document.f.Pays.value;
	document.f.submit();
}

// Change le quantité d'un article en commande
function JS_panier_change_quantite(id, nom, qte_dispo)  {

	var qte=parseInt(document.getElementById('id_qte'+id).value);
	var msg = "";

	if (parseInt(qte_dispo) < qte) {
		document.getElementById('id_qte'+id).value=qte_dispo;
		if (parseInt(qte_dispo)>1) 
			msg="Il n'y a que " + qte_dispo + " " + nom + " disponibles en stock !";
		else
			msg="Il n'y a que " + qte_dispo + " " + nom + " disponible en stock !";
		alert(msg);
	}
	document.f.actions.value="M";
	document.f.submit();
}

// Valide le panier pour poursuivre la commande
function JS_panier_valider_panier() {

	if (document.f.Pays.value.substr(0,1)=="-")
	{
		alert("Vous devez sélectionner une destination valide !");
		return;
	}

	document.f.action="?page=commande";
	document.f.submit();
}

// Efface la couleur rouge (si champs vide) et le remet à blanc
function JS_commande_couleur(obj) {
     obj.style.backgroundColor = "#FFFFFF";
}

// Vérifie la validité des infos de coordonnées et passe au paiement si Ok
function JS_commande_CheckForm() {

	var msg = "";
	var ml = document.forms['worldpay'].email.value;
	ml = ml.replace(/@/,"§"); 


	//ici nous vérifions si le champs nom et vide, changeons la couleur du champs et définissons un message d'alerte
	if (document.forms['worldpay'].raisonsociale.value  == "")	{
		msg += "Veuillez saisir votre raison sociale\n";
		document.forms['worldpay'].raisonsociale.style.backgroundColor = "#FFADB1";
	}

	if (document.forms['worldpay'].tel.value == "")	{
		msg += "Veuillez saisir votre numéro de téléphone\n";
		document.forms['worldpay'].tel.style.backgroundColor = "#FFADB1";
	}

	//Vérification du mail s'il n'est pas vide on vérifie le . et @
	if (document.forms['worldpay'].email.value != "")	{
	indexAroba = document.forms['worldpay'].email.value.indexOf('@');
	indexPoint = document.forms['worldpay'].email.value.indexOf('.');
	if ((indexAroba < 0) || (indexPoint < 0))		{
		//dans le cas ou il manque soit le . soit l'@ on modifie la couleur d'arrière plan du champ mail et définissons un message d'alerte
 		document.forms['worldpay'].email.style.backgroundColor = "#FFADB1";
		msg += "Le mail est incorrect\n";
		}
	}
	else
	{
		document.forms['worldpay'].email.style.backgroundColor = "#FFADB1";
		msg += "Veuillez saisir votre adresse e-mail\n";
	}

	if (document.forms['worldpay'].add1.value == "")	{
		msg += "Veuillez saisir votre adresse\n";
		document.forms['worldpay'].add1.style.backgroundColor = "#FFADB1";
	}
	
	if (document.forms['worldpay'].codepostal.value == "")	{
		msg += "Veuillez saisir votre code postal\n";
		document.forms['worldpay'].codepostal.style.backgroundColor = "#FFADB1";
	}
 
	if (document.forms['worldpay'].ville.value == "")	{
		msg += "Veuillez saisir votre ville\n";
		document.forms['worldpay'].ville.style.backgroundColor = "#FFADB1";
	}

	//Si aucun message d'alerte a été initialisé on retourne TRUE
	if (msg == "") 
	{
		document.worldpay.submit();
		return;
	}
 	else
	{
		alert(msg);
		return;
	}
}

// Fonction de swap des zones cachées
function JS_commande_wrap()	{
	JS_DivStatus('indic','cachediv')
	JS_DivStatus('fact','normal')
}

// Affiche les CVG dans une popup
function JS_paiement_affiche_CGV() {
	haut=600;
	larg=400;
	hauteur=Math.round((screen.availHeight-haut)/2);
	largeur=Math.round((screen.availWidth-larg)/2);
	window.open("pgs/cgv2.php", "site", "toolbar=0,location=0,directories=0,status=0,scrollbars=yes,resizable=0,menubar=0,top="+hauteur+",left="+largeur+",width="+larg+",height="+haut);
}

// Swap des zones cachées
function JS_paiement_montre(qui)	{
	JS_DivStatus('cb','cachediv');
	JS_DivStatus('ch','cachediv');
	JS_DivStatus('vi','cachediv');
	JS_DivStatus(qui,'normal');
}

// Validation du formaulaire de paiement
function JS_paiement_valide_formulaire() {

	var msg="";
	var a=0;
	
	for (var i = 0; i < document.forms['worldpay'].modepay.length; i++)
	{
		if (document.forms['worldpay'].modepay[i].checked==true) {
			a=1;
			document.forms['worldpay'].action="?page=payok";
			if (document.forms['worldpay'].modepay[i].value=="CB") document.forms['worldpay'].action=document.forms['worldpay'].adressecic.value;
		}
	}
	
	if (a==0)	{
		msg += "Veuillez choisir un mode de paiement\n";
	}

	if (document.forms['worldpay'].accept.checked == false)	{
		msg += "Veuillez accepter les Conditions Générales de Vente\n";
		document.forms['worldpay'].accept.style.borderColor = "#FFADB1";
	}

	if (msg == "") 
	{
		document.worldpay.submit();
		return;
	}
	else
	{
		alert(msg);
		return;
	}
}

// Affiche ou cache une zone (DIV)
function JS_DivStatus( nom, etat )
{
	var divID = nom;
	if ( document.getElementById && document.getElementById( divID ) ) // Pour les navigateurs récents
		{
			Pdiv = document.getElementById( divID );
			PcH = true;
		}
	else if ( document.all && document.all[ divID ] ) // Pour les veilles versions
		{
			Pdiv = document.all[ divID ];
			PcH = true;
		}
	else if ( document.layers && document.layers[ divID ] ) // Pour les très veilles versions
		{
			Pdiv = document.layers[ divID ];
			PcH = true;
		}
	else
		{
			PcH = false;
		}
	if ( PcH )
		{
			Pdiv.className = etat;
		}
}

// Fonction d'envoi de mail codée (antispam)
function envoi_mail(id, serveur, domaine) {
	location.href = 'mailto:'+id+'@'+serveur+'.'+domaine;
}