var totalOfferEuro = 0;
var totalOfferDollar = 0;


function evalLine(curIndex,totIndex){
    // Calcule et affecte le total dans le formulaire
    // curIndex stocke la valeur de l'index courant pour l'évaluation
    // totIndex stocke le nombre total de lignes de produits
    // Le contenu de la balise <div> : id = total_curIndex
    // Valeur en Euro : frmPUEuro, valeur en dollar : frmPUDollar

    var htmlCode = "";

    // Construction du nom des variables
    var quantite = "frmqte_"+curIndex;
    var prixEuro = "frmPUEuro_"+curIndex;
    var divName = "total_"+curIndex;

    // Récupération des prix en Euros et en Dollars
    var floatPUEuro = document.formPresentation[prixEuro].value;

    // Récupération de la quantité saisie
    var intQuantite = document.formPresentation[quantite].value*1;

    var prixLigneEuro = intQuantite*floatPUEuro;

    // Affichage de la ligne correspondante
    htmlCode = "<font class=\"tab-content\">\n";
    htmlCode = htmlCode + prixLigneEuro + " &euro; \n";
    htmlCode = htmlCode + "</font>\n";

    affLine(htmlCode,divName);

    evalTotal(totIndex); // Evalue le prix total de la commande

    return;
}

function affLine(htmlCode,divName){
    if (document.all||document.getElementById) {
        if (document.getElementById) {
            document.getElementById(divName).innerHTML = htmlCode
        }
        else {
                divName.innerHTML = htmlCode;
        }
    }

    return;
}

function evalTotal(totIndex){
    // Calule et affiche le total des lignes de commandes
    // Calcul du total général : bouclage sur toutes les occurrences

    var htmlCode = "";
    var totalEuro = 0;
    var ligneEuro = 0;
    var portEuro  = 0; // Frais de port inclus
    
    // Décommenter pour version n+1
    //portEuro = document.formPresentation["frmPort"].value*1;

    for(i=1;i<=totIndex;i++){
        quantite = "frmqte_"+i;
        prixEuro = "frmPUEuro_"+i;
        ligneEuro = (document.formPresentation[quantite].value)*document.formPresentation[prixEuro].value;
        totalEuro = totalEuro + ligneEuro;

        htmlCode = "<font class=\"tab-content\">\n";
        htmlCode = htmlCode + ligneEuro + " &euro;\n";
        htmlCode = htmlCode + "</font>\n";
        affLine(htmlCode,"total_"+i);
    }

    // Décommenter pour version n+1
    portEuro = document.formPresentation['port'].value*1;
    //alert('Port'+portEuro);
    totalEuro = totalEuro + portEuro;

    htmlCode = "<font class=\"tab-content\">\n";
    htmlCode = htmlCode + totalEuro + " &euro;\n";
    htmlCode = htmlCode + "</font>\n";

    // Affichage du total correspondant
    if (document.all||document.getElementById) {
        if (document.getElementById) {
            document.getElementById("total").innerHTML = htmlCode
        }
        else {
                total.innerHTML = htmlCode;
        }
    }
    return;
}


function evaluateCharge(zone){
    var tvaField = "tva_"+zone;
    var totalLineEuro = 0;
    var portEuroField = "portEuro_"+zone;
    var htmlCode = "";
    var totalHTEuro = 0;

    if(document.formLivraison[tvaField].value*1 == 0){
       // Les totaux sont simplement évalués en additionnant le total + les charges
       totalLineEuro = document.formLivraison.totalEuro.value*1 + document.formLivraison[portEuroField].value*1;
    } else {
      // Sinon, on calcule le total TTC de la ligne + les frais de port
       totalHTEuro = document.formLivraison.totalEuro.value*1 + document.formLivraison[portEuroField].value*1;
       var totalEuro =  totalHTEuro + (totalHTEuro *(document.formLivraison[tvaField].value*1 / 100));
       totalLineEuro = Math.round(totalEuro * 100) / 100;
    }

    htmlCode = "<font class=\"tab-content\">\n";
    htmlCode = htmlCode + totalLineEuro+ " &euro;\n";
    htmlCode = htmlCode + "</font>\n";

    affLine(htmlCode,"total");


    return;

}

function validateTerms(theForm){
    if(theForm.frmAccept.checked == true)
       return true;

    alert("You cannot continue without accept Terms, Conditions and licence agreement");
    return false;
}

function swapContent(srcField,targetField){
    // Permet de basculer le contenu d'un champ dans un autre champ du formulaire
    if(srcField.value.length > 0){
        document.formCustomer[targetField].value = srcField.value;
    }
    return;
}

function verifMail(mail) {
      if ((document.formCustomer[mail].value.indexOf("@")>=0)&&(document.formCustomer[mail].value.indexOf(".")>=0)) {
         return true
      } else {
         alert("L'adresse saisie est invalide !");
         document.formCustomer[mail].focus();
         return false
      }
   }
function newWindow(url){
    var theWindow = window.open(url,'','height=600,width=700,menubar=no,status=yes,toolbar=no');
    return;
}

// Vérification de la saisie du code Intra Communautaire
function verif_code(){
  if(document.formCustomer.frmClientCodeIntra.value == ""){
    if(confirm('V.A.T. code is a mandatory field if you come from a E.E.C. country.\nAre you sure you want to leave this field blank ?')){
      return true;
    } else {
      return false;
    }
  }
}

