// identifiants des articles logiciels
var logicielId = new Array(1, 2, 29);

function calculPrixLogiciel(indice, prix) {
    var form = document.forms["purchaseform"];
    var qty = eval("form.select" + indice + ".options[form.select" + indice + ".selectedIndex].value");
    eval("form.total" + indice).value = getNumberFormatted(prix * qty);
    eval("form.taux" + indice).value = getNumberFormatted(prix * qty);
}

function calculTauxMois(ind, dur) {
    var form = document.forms["purchaseform"];
    if (dur == "3") {
        var tx = eval("form.taux" + ind + "_3").value;
        eval("form.taux" + ind).value = tx;
    } else {
        eval("form.taux" + ind).value = eval("form.taux" + ind + "_12").value;
    }

    if (eval("form.radio" + ind)[0].checked) {
        eval("form.abon" + ind).value = "3";
    } else {
        eval("form.abon" + ind).value = "12";
    }
}

function calculPrixAbon(ind) {
    var form = document.forms["purchaseform"];
    var duree = eval("form.abon" + ind).value;
    var qty = eval("form.select" + ind + ".options[form.select" + ind + ".selectedIndex].value");
    var tx = eval("form.taux" + ind).value;
    var txt = parseFloat(tx.replace(",", "."));
    var subTotal = Math.round(100 * txt * qty * duree) / 100;
    eval("form.total" + ind).value = getNumberFormatted(subTotal);
}

function getNumberFormatted(n) {
    if (n > 0) {
        var cents = Math.round((100 * n)).toString();
        var l = cents.length - 2;
        return cents.substring(0, l) + "," + cents.substring(l, l + 2);
    } else {
        return "0,00";
    }
}

function calculTotal() {
    var form = document.forms["purchaseform"];
    var somme = 0;
    for (var i = 0; i < form.elements.length; i++) {
        var element = form.elements[i];
        if (element.type != "button" && element.name.match("total([0-9]+)")) {
            somme += parseFloat(form.elements[i].value.replace(",", "."));
        }
    }

    /*
    calculFraisPort();
    if (typeof(form.radioPort) != "undefined") {
        somme += parseFloat(form.totalPort.value.replace(",", "."));
    }
    */
    form.totalGeneral.value = getNumberFormatted(somme);
    form.total.value = somme;
}

function calculeNombreLogiciels() {
    var quantite = 0;
    for (var i = 0; i < logicielId.length; i++) {
        var elem = document.getElementById("select" + logicielId[i]);
        if (elem != null) {
            quantite += parseFloat(elem.value);
        }
    }

    return quantite;
}

/*
function calculFraisPort() {
    var form = document.forms["purchaseform"];

    if (typeof(form.radioPort) != "undefined") {
        var quantite = calculeNombreLogiciels();

        var coutUnitaire = form.radioPort[0].checked ? 7 : 14;

        var totPort = coutUnitaire * quantite;

        form.totalPort.value = getNumberFormatted(totPort);
    }
}

function verifFraisPort() {
    var form = document.forms["purchaseform"];
    var pays = form.pays.value;

    if (typeof(form.radioPort) != "undefined" && calculeNombreLogiciels() > 0) {
        if ((pays == "FR" && form.radioPort[0].checked) || (pays != "FR" && form.radioPort[1].checked)) {
            return true;
        } else {
            alert("Les frais de port ne sont pas correctement saisis !");
            return false;
        }
    } else {
        return true;
    }
}
*/

function verifChamps() {

    var form = document.forms["purchaseform"];
    if (parseFloat(form.totalGeneral.value.replace(",", ".")) == 0) {
        alert("Vous n'avez pas défini votre commande !");
        return false;
    } else if (!form.qualite.checked && particulier) {
        alert("Vous n'avez pas déclaré être un Particulier !");
        return false;
    } else if (form.elements["societe"].value == "" && !particulier) {
        alert("Vous n'avez pas saisi la Société !");
        return false;
    } else if (form.elements["nom"].value == "") {
        alert("Vous n'avez pas saisi votre Nom !");
        return false;
    } else if (form.elements["prenom"].value == "") {
        alert("Vous n'avez pas saisi votre Prénom !");
        return false;
    } else if (form.elements["adresse"].value == "") {
        alert("Vous n'avez pas saisi votre adresse !");
        return false;
    } else if (form.elements["pays"].value == "???") {
        alert("Vous n'avez pas saisi votre pays !");
        return false;
    } else {
        return true;
    }
}

function isEmailValid() {

    var form = document.forms["purchaseform"];
    var elm = form.email.value;
    if (elm.indexOf(" ") + "" == "-1" && elm.indexOf("@") + "" != "-1" && (elm.lastIndexOf(".") > elm.indexOf("@")) && elm != "") {
        return true;
    } else {
        alert("Vous n'avez pas saisi correctement votre email !");
        form.email.focus();
        return false;
    }
}

function valider(mode) {
    //if (verifFraisPort()) {
        var form = document.forms["purchaseform"];

        form.payMethods.value = mode;
        if (verifChamps() && isEmailValid()) {
            form.submit();
        }
    //}
}

function openFrame(winName, features) {
    window.location = "conditionsvente.html";
}

function updateContact() {
    var form = document.forms["purchaseform"];
    form.contact.value = form.ping.options[form.ping.selectedIndex].text;
}

function gotoURL2() {
    window.location = "conditionsvente.html";
}
