var vtva1 = 0;
var vtva2 = 0;
top.devise_sel = "Euro";

function af_port() {
	vtoupays = new Array();
	vtoupays = top.tpays.sort();
	vpoids = calcul_poids();
	var vtarif ="";
	for (i in vtoupays) {
		separ = vtoupays[i].split(",");
		if (separ[0] == top.pays_select) vtarif= separ[1]; 
	}
	var vport = "<input type=text readonly size=10 maxlength=15 value='" + francs(frais_port()) + " " + top.devise[top.devise_sel].abrev + "' style='background-color:#f0f0f0;color:#000088'>";
	aff = document.getElementById('sel_port');
	aff.innerHTML = vport;
	var vlivraison = "" ;
	selcodep = top.client_codep.substr(0,2);
	if (selcodep == "97") vtarif = "p";
	if (selcodep == "98") vtarif = "u";
	if (top.client_codep == "98000") vtarif = "a";
	
	switch(vtarif){
		case "a": vlivraison = "Colissimo Suivi Standard";
			  break;
		case "b": vlivraison = "Colissimo Suivi Europe";
			  break;
		case "c": vlivraison = "Colissimo Suivi Europe";
			  break;
		case "d": if (top.livraison == "Economique") vlivraison = "Colis Postal Économique";
			  else vlivraison = "Colis Postal Prioritaire";
			  break;
		case "e": if (top.livraison == "Economique") vlivraison = "Colis Postal Économique";
			  else vlivraison = "Colis Postal Prioritaire";
			  break;
		case "f": if (top.livraison == "Economique") vlivraison = "Colis Postal Économique";
			  else vlivraison = "Colis Postal Prioritaire";
			  break;
		case "g": if (top.livraison == "Economique") vlivraison = "Colis Postal Économique";
			  else vlivraison = "Colis Postal Prioritaire";
			  break;
		case "o": if (top.livraison == "Economique") vlivraison = "Colis Outre-Mer DOM Économique";
			  else vlivraison = "Colis Outre-Mer DOM Prioritaire";
			  break;
		case "p": vlivraison = "Colissimo DOM Suivi";
			  break;
		case "t": if (top.livraison == "Economique") vlivraison = "Colis Outre-Mer TOM Économique";
			  else vlivraison = "Colis Outre-Mer TOM Prioritaire";
			  break;
		case "u": vlivraison = "Colissimo TOM Suivi";
			  break;
		
	}
	top.colis = vlivraison;
	var vselport = "";
	if (vtarif == "a" || vtarif == "b" || vtarif == "c"  || vtarif == "p" || vtarif == "u") {
		vselport = "<input type=text readonly size='30' style='font-size:13px;' value='" + vlivraison + "' style='background-color:#f0f0f0;color:#000088'>";
	}
	if (vtarif == "d" || vtarif == "e" || vtarif == "f" || vtarif == "g") {
		vselport = "<select name=selport onchange='top.livraison=this.value;af_port();paniera();'>";
		if (top.livraison == "Economique") vselport = vselport + "<option selected value='Economique'>Colis Postal Économique</option>";
		else vselport = vselport + "<option value='Economique'> Colis Postal Économique</option>";
		if (top.livraison == "Prioritaire") vselport = vselport + "<option selected value='Prioritaire'>Colis Postal Prioritaire</option>";
		else vselport = vselport + "<option value='Prioritaire'>Colis Postal Prioritaire</option>";
		vselport = vselport + "</select>";
	}
	if (vtarif == "o" || vtarif == "t") {
		vselport = "<select name=selport onchange='top.livraison=this.value;af_port();paniera();'>";
		if (top.livraison == "Economique") vselport = vselport + "<option selected value='Economique'>Colis Outre-Mer Économique</option>";
		else vselport = vselport + "<option value='Economique'>Colis Outre-Mer Économique</option>";
		if (top.livraison == "Prioritaire") vselport = vselport + "<option selected value='Prioritaire'>Colis Outre-Mer Prioritaire</option>";
		else vselport = vselport + "<option value='Prioritaire'>Colis Outre-Mer Prioritaire</option>";
		vselport = vselport + "</select>";
		vseport = vselport + "</select>";
	}
	aff = document.getElementById('sel_port1');
	aff.innerHTML = vselport;
}

function spays () {
	vtoupays = new Array();
	vtoupays = top.tpays.sort();
	vpaysa = "<select onchange='top.pays_select=this.value;paniera();af_port();af_paiement ();spays()'>";
	vpays = "";
	vsel = "";
	for (i in vtoupays) {
		separ = vtoupays[i].split(",");
		if (separ[0] == top.pays_select) {
			vsel = "selected";
			top.pays_code = separ[2];
		}
		else vsel="";
		vpays = vpays + "<option " + vsel + " value='" + separ[0] + "'>" + separ[0] + "</option>";
	}
	vpays = vpaysa + vpays + "</select>";
	aff = document.getElementById('sel_pays');
	aff.innerHTML = vpays;
	//sel_pays.innerHTML = vpays;
}

function sapays (selpnom) {
	vtoupays = new Array();
	vtoupays = top.tpays.sort();
	vpaysa = "<select name=" + selpnom + "'>";
	if (selpnom == "sapays") vpaysa = "<select  name='sapays' onchange='top.pays_select=this.value;paniera();af_port();af_paiement ();spays()'>";
	if (selpnom == "safpays") vpaysa = "<select  name='safpays' onchange='top.paysf_select=this.value;'>";
	vpays = "";
	vsel = "";
	for (i in vtoupays) {
		separ = vtoupays[i].split(",");
		vsel="";
		if (separ[0] == top.pays_select && selpnom == "sapays") {
			vsel = "selected";
			top.pays_code = separ[2];
		}
		else vsel="";
		if (separ[0] == top.paysf_select && selpnom == "safpays") {
			vsel = "selected";
			top.pays_code = separ[2];
		}
		vpays = vpays + "<option " + vsel + " value='" + separ[0] + "'>" + separ[0] + "</option>";
	}
	vpays = vpaysa + vpays + "</select>";
	aff = document.getElementById(selpnom);
	aff.innerHTML = vpays;
	//sel_pays.innerHTML = vpays;
}

function af_paiement () {
	var vcarte = 0;
	if ((top.pays_select != "France Métropolitaine") && (top.pays_select != "Guadeloupe") && (top.pays_select != "Guyane") && (top.pays_select != "Martinique") && (top.pays_select != "Mayotte") && (top.pays_select != "Réunion") && (top.pays_select != "Polynésie Française") && (top.pays_select != "Nouvelle Calédonie")) {
		top.paiement_sel = "Carte Bancaire";
		vcarte = 1;
	}
	//if ((top.pays_select == "Guadeloupe") || (top.pays_select == "Guyane") || (top.pays_select == "Martinique") || (top.pays_select == "Réunion") || (top.pays_select == "Polynésie Française")) {
		//vcarte = 2;
		//if (top.paiement_sel == "Contre Remboursement") top.paiement_sel = "Chèque à la Commande";
	//}	
	
	var vpaie = "<select name=spaie onchange='top.paiement_sel=this.value;'>";
	if (top.paiement_sel == "Carte Bancaire") vpaie = vpaie + "<option  selected value='Carte Bancaire'>Carte Bancaire</option>"
	else vpaie = vpaie + "<option  value='Carte Bancaire'>Carte Bancaire</option>"
	//if (top.paiement_sel == "Paypal") vpaie = vpaie + "<option  selected value='Paypal'>Paypal</option>"
	//else vpaie = vpaie + "<option  value='Paypal'>Paypal</option>"
	if (vcarte != 1) {
		if (top.paiement_sel == "Chèque à la Commande") vpaie = vpaie + "<option  selected value='Chèque à la Commande'>Chèque à la Commande</option>"
		else vpaie = vpaie + "<option  value='Chèque à la Commande'>Chèque à la Commande</option>"
	}
	if (vcarte == 0) {
		if (top.paiement_sel == "Contre Remboursement") vpaie = vpaie + "<option  selected value='Contre Remboursement'>Contre Remboursement</option>"
		else vpaie = vpaie + "<option  value='Contre Remboursement'>Contre Remboursement</option>"
	}
	vpaie = vpaie + "</select>";
	aff = document.getElementById('sel_paiement');
	aff.innerHTML = vpaie;
}

function af_commande(larg_nom) {
	if (top.pays_select != "France Métropolitaine") {larg_nom = 320;}
	var vptot = 0;
	var vtot = 0;
	var aftva = "";
	var vcom = "<table cellspacing=0 cellpadding=1 border=1 bordercolor=#eeeee0 align=center>";
	for (i in top.panier) {
		if (!top.panier[i].qte) top.panier[i].qte = 0;
		if (top.panier[i].qte != 0) {
			if (top.pays_select == "France Métropolitaine") {
				aftva = (top.p[i].tva == 1) ? "5,5%" : "19,6%";
				aftva = "<td width=40 bgcolor=#ddddcc align=center><font face=Times New Roman color=#000000 size=2>&nbsp;" + aftva + "</font></td>";
			}
			else {aftva = "";}
			vcom = vcom + "<tr><td width=" + larg_nom + " bgcolor=#ddddcc><font face=Times New Roman color=#000000 style='font-size:13px;'><b>" + top.p[i].nom + "</b> - " + top.p[i].marque + " - " + top.p[i].condit + "</font></td><td width=75 bgcolor=#ddddcc align=right><font face=Times New Roman color=#000000 style='font-size:13px;'>" + francs(top.p[i].prix) +  " " + top.devise[top.devise_sel].abrev + "&nbsp;</font></td><td width=30 bgcolor=#ddddcc align=center><font face=Times New Roman color=#000000 style='font-size:13px;'>&nbsp;" + String(top.panier[i].qte) + "</font></td>" + aftva + "<td width=75 bgcolor=#ddddcc align=right><font face=Times New Roman color=#000000 style='font-size:13px;'>" + francs(top.p[i].prix * top.panier[i].qte) + " " + top.devise[top.devise_sel].abrev + "&nbsp;</font></td></tr>";
		}
		vtot = vtot + top.p[i].prix * top.panier[i].qte;
		if (top.p[i].tva == 1) vtva1 = vtva1 + top.p[i].prix * top.panier[i].qte - (top.p[i].prix * top.panier[i].qte) / (1 + top.tva1);
		if (top.p[i].tva == 2) vtva2 = vtva2 + top.p[i].prix * top.panier[i].qte - (top.p[i].prix * top.panier[i].qte) / (1 + top.tva2);
	}
	vtva1 = Math.round(vtva1 * 100) / 100;
	vtva2 = Math.round(vtva2 * 100) / 100;
	vcom = vcom + "</table>";
	aff = document.getElementById('af_com');
	aff.innerHTML = vcom;
 	larg_nom = String(larg_nom - 60);
	vcom = "<table cellspacing=0 cellpadding=3 border=1 bordercolor=#eeeee0 align=center>";
	vcom = vcom + "<tr><td width=" + larg_nom + "></td><td width=200 align=right><font face=Times New Roman color=#000000 style='font-size:13px;'><b>Total de la commande : </b></font></td><td width=80 bgcolor=#e5e5c5 align=right><font face=Times New Roman color=#000000 style='font-size:13px;'><b>" + francs(vtot) + " " + top.devise[top.devise_sel].abrev + "</b></font></td></tr>";
	if (top.pays_select == "France Métropolitaine") {
		vcom = vcom + "<tr><td width=" + larg_nom + "></td><td width=200 align=right><font face=Times New Roman color=#000000 style='font-size:13px;'>Total de la commande HT : </font></td><td width=80 bgcolor=#f0f0dd align=right><font face=Times New Roman color=#000000 style='font-size:13px;'><i>" + francs(vtot - vtva1 - vtva2) + " " + top.devise[top.devise_sel].abrev + "</i></font></td></tr>";
		if ( vtva1 != 0) vcom = vcom + "<tr><td width=" + larg_nom + "></td><td width=200 align=right><font face=Times New Roman color=#000000 style='font-size:13px;'>T.V.A. à " + String(top.tva1 * 100) + "%  : </font></td><td width=80 bgcolor=#f0f0dd align=right><font face=Times New Roman color=#000000 style='font-size:13px;'><i>" + francs(vtva1) + " " + top.devise[top.devise_sel].abrev + "</i></font></td></tr>";
		if ( vtva2 != 0) vcom = vcom + "<tr><td width=" + larg_nom + "></td><td width=200 align=right><font face=Times New Roman color=#000000 style='font-size:13px;'>T.V.A. à " + String(top.tva2 * 100) + "%  : </font></td><td width=80 bgcolor=#f0f0dd align=right><font face=Times New Roman color=#000000 style='font-size:13px;'><i>" + francs(vtva2) + " " + top.devise[top.devise_sel].abrev + "</i></font></td></tr>";
		if ((vtva1 != 0) && (vtva2 != 0)) vcom = vcom + "<tr><td width=" + larg_nom + "></td><td width=200 align=right><font face=Times New Roman color=#000000 style='font-size:13px;'>Total T.V.A. : </font></td><td width=80 bgcolor=#f0f0dd align=right><font face=Times New Roman color=#000000 style='font-size:13px;'><i>" + francs(vtva1 + vtva2) + " " + top.devise[top.devise_sel].abrev + "</i></font></td></tr>";
	}
	vcom = vcom + "<tr><td width=" + larg_nom + "></td><td width=200 align=right><font face=Times New Roman color=#000000 style='font-size:13px;'><b>Frais de port : </b></font></td><td width=80 bgcolor=#e5e5c5 align=right><font face=Times New Roman color=#000000 style='font-size:13px;'><b>" + francs(frais_port()) + " " + top.devise[top.devise_sel].abrev + "</b></font></td></tr>";
	if (top.paiement_sel == "Contre Remboursement") {
		vcom = vcom + "<tr><td width=" + larg_nom + "></td><td width=200 align=right><font face=Times New Roman color=#000000 style='font-size:13px;'><b>Frais Contre Remboursement : </b></font></td><td width=80 bgcolor=#e5e5c5 align=right><font face=Times New Roman color=#000000 style='font-size:13px;'><b>" + francs(top.contre_remb) + " " + top.devise[top.devise_sel].abrev + "</b></font></td></tr>";
		vtot = vtot + top.contre_remb;
	}
	vcom = vcom + "<tr><td width=" + larg_nom + "></td><td width=200 align=right><font face=Times New Roman color=#000000 style='font-size:13px;'><b>Total à Payer : </b></font></td><td width=80 bgcolor=#e5e5c5 align=right><font face=Times New Roman color=#000000 style='font-size:13px;'><b>" + francs(vtot + frais_port()) + " " + top.devise[top.devise_sel].abrev + "</b></font></td></tr>";
	vcom = vcom + "</table>";
	top.total_com = vtot + frais_port();
	aff = document.getElementById('af_com1');
	aff.innerHTML = vcom;
}

function gen_num() {
	if (top.com_num == "") {
		var vnum = "";
		vdate = new Date();
		van = String(vdate.getFullYear());
		van = van.substr(2,3);
		emois = vdate.getMonth();
		if (emois < 10) vmois = "0" + String(1 + emois);
		else vmois = String(1 + emois);
		ejour = vdate.getDate();
		if (ejour < 10) vjour = "0" + String(ejour);
		else vjour = String(ejour);
		eheure = vdate.getHours();
		if (eheure < 10) vheure = "0" + String(eheure);
		else vheure = String(eheure);
		eminute = vdate.getMinutes();
		if (eminute < 10) vminute = "0" + String(eminute);
		else vminute = String(eminute);
		eseconde = vdate.getSeconds();
		if (eseconde < 10) vseconde = "0" + String(eseconde);
		else vseconde = String(eseconde);
		vnom = top.client_nom + "ZZZ";
		vnom = vnom.toUpperCase();
		vnom = String(vnom.charCodeAt(0)) + String(vnom.charCodeAt(1)) + String(vnom.charCodeAt(2));
		vprenom = top.client_prenom + "ZZZ";
		vprenom = vprenom.toUpperCase();
		vprenom = String(vprenom.charCodeAt(0));
		vnum = van + vmois + vjour + vheure + vminute + vseconde + vnom + vprenom;
		top.com_num = vnum;
	}
	return(top.com_num);
	
}

function fin() {
	for (i in top.panier) {
		top.panier[i].qte = 0;
	}
	top.frames.page.location.href="home.htm";
}

function ctrl_input() {
  //if (document.fact.facture.checked) {
  //top.client_fact = "on";
  //}
  //else {
    //top.client_fact = "off";
  //}
	var valid = 1;
	if (top.client_nom.length < 2 ) {valid = 0 ; valert = "Nom trop court";}
	else top.client_nom = top.client_nom.substr(0,1).toUpperCase() + top.client_nom.substr(1,top.client_nom.length).toLowerCase();
	for (i=0 ; i < top.client_nom.length ; i++) {
		   code = top.client_nom.charCodeAt(i);
		   if (code == 39) top.client_nom = top.client_nom.replace("'"," ");
		   if (code == 59) top.client_nom = top.client_nom.replace(";"," ");
		
	}
	if (top.client_prenom.length < 2 ) {valid = 0 ; valert = "Prénom trop court";}
	else top.client_prenom = top.client_prenom.substr(0,1).toUpperCase() + top.client_prenom.substr(1,top.client_prenom.length).toLowerCase();
	for (i=0 ; i < top.client_prenom.length ; i++) {
	    code = top.client_prenom.charCodeAt(i);
	    if (code == 39) top.client_prenom = top.client_prenom.replace("'"," ");
	    if (code == 59) top.client_prenom = top.client_prenom.replace(";"," ");
	}
	
	if ((top.client_adres1.length == 0) && (top.client_adres2.length == 0) ) {valid = 0 ; valert = "Entrez votre adresse dans le champ correspondant";}
	for (i=0 ; i < top.client_adres1.length ; i++) {
	    code = top.client_adres1.charCodeAt(i);
	    if (code == 39) top.client_adres1 = top.client_adres1.replace("'"," ");
	    if (code == 59) top.client_adres1 = top.client_adres1.replace(";"," ");
	}
	for (i=0 ; i < top.client_adres2.length ; i++) {
	    code = top.client_adres2.charCodeAt(i);
	    if (code == 39) top.client_adres2 = top.client_adres2.replace("'"," ");
	    if (code == 59) top.client_adres2 = top.client_adres2.replace(";"," ");
	}
	
	if ((top.client_codep.length < 2) && (top.pays_select == "France Métropolitaine") ) {valid = 0 ; valert = "Code Postal non Valide";}
		for (i=0 ; i < top.client_codep.length ; i++) {
			code = top.client_codep.charCodeAt(i);
			if (code == 59) top.client_codep = top.client_codep.replace(";"," ");
		}
	
	if (top.client_ville.length < 2 ) {valid = 0 ; valert = "Nom de ville trop court";}
	else top.client_ville = top.client_ville.toUpperCase();
		for (i=0 ; i < top.client_ville.length ; i++) {
		    code = top.client_ville.charCodeAt(i);
		    if (code == 39) top.client_ville = top.client_ville.replace("'"," ");
		    if (code == 59) top.client_ville = top.client_ville.replace(";"," ");
		    
		}
		
	for (i=0 ; i < top.client_tel.length ; i++) {
	    code = top.client_tel.charCodeAt(i);
	    if (!(((code >= 48) && (code <= 57)) || (code == 32))) {
		valid = 0;
		valert = "Numéro de téléphone non Valide !";
	    }
	}
	
	for (i=0 ; i < top.client_com.length ; i++) {
		    code = top.client_com.charCodeAt(i);
		    if (code == 39) top.client_com = top.client_com.replace("'"," ");
		    if (code == 59) top.client_com = top.client_com.replace(";"," ");
	}
	
	emal = 1;
	aqte = 0;
	for (i=0 ; i < top.client_mail.length ; i++) {
		if (top.client_mail.charCodeAt(i) == 64) aqte++;
	}
	if (aqte == 1) {
		emaila = top.client_mail.substring(0,top.client_mail.indexOf("@",0));
		emailb = top.client_mail.substring(top.client_mail.indexOf("@",0)+1,top.client_mail.length);
		for (i=0 ; i < emaila.length ; i++) {
			code = emaila.charCodeAt(i);
			if (!((code == 46) || (code == 45) || (code == 95) || ((code >= 65) && (code <= 90)) || ((code >= 97) && (code <= 122)) || ((code >= 48) && (code <= 57)))) {
				emal = 0;
			}
			if (i > 0) {
				if ((code == 46) && emaila.charCodeAt(i-1) == 46) emal = 0;
			}
		}
		vpt = 0;
		for (i=0 ; i < emailb.length ; i++) {
			code = emailb.charCodeAt(i);
			if (!((code == 46) || (code == 45) || ((code >= 65) && (code <= 90)) || ((code >= 97) && (code <= 122)) || ((code >= 48) && (code <= 57)))) {
				emal = 0;
			}
			if (code == 46) vpt = 1;
			if (i > 0) {
				if ((code == 46) && emailb.charCodeAt(i-1) == 46) emal = 0;
			}
		}
		if (vpt == 0) emal = 0;
	}
	else emal = 0;
	if (emal == 0) {valid =0; valert = "Adresse é-mail non valide";}
	if (valid == 1) {
		top.frames.page.location.href = "saisie2.htm";
	}
	else alert(valert);
}
