﻿TotalBook1 = 0;
TotalBook2 = 0;
TotalBook3 = 0;
TotalBook4 = 0;
TotalBook5 = 0;
TotalBook6 = 0;
TotalBook7 = 0;
TotalBook8 = 0;
TotalBooks = 0;
DefaultTransportCosts = 1.95;


function Hasbooks() {
    if (CountTotalBooks() == 0) { alert("U heeft nog geen aantallen bij de boeken ingevuld."); return false; }
    else { return true; }
}

function CountTotalBooks() {

    Book1 = 0;
    Book2 = 0;
    Book3 = 0;
    Book4 = 0;
    Book5 = 0;
    Book6 = 0;
    Book7 = 0;
    Book8 = 0;

    if (!isEmpty(frm.Book1.value)) { Book1 = new Number(frm.Book1.value); }
    if (!isEmpty(frm.Book2.value)) { Book2 = new Number(frm.Book2.value); }
    if (!isEmpty(frm.Book3.value)) { Book3 = new Number(frm.Book3.value); }
    if (!isEmpty(frm.Book4.value)) { Book4 = new Number(frm.Book4.value); }
    if (!isEmpty(frm.Book5.value)) { Book5 = new Number(frm.Book5.value); }
    if (!isEmpty(frm.Book6.value)) { Book6 = new Number(frm.Book6.value); }
    if (!isEmpty(frm.Book7.value)) { Book7 = new Number(frm.Book7.value); }
    if (!isEmpty(frm.Book8.value)) { Book8 = new Number(frm.Book8.value); }

    TotalNumber = Book1 +
    Book2 +
    Book3 +
    Book4 +
    Book5 +
    Book6 +
    Book7 + 
    Book8;

    return TotalNumber;
}

 function OnlyAllowNumbers(obj) {
    StrTempValue = ""
    ContainsDigits = false;
    for (i = 0; i < obj.value.length; i++) {
        StrChar = obj.value.charAt(i);
        if (isDigit(StrChar)) { StrTempValue = StrTempValue + StrChar; ContainsDigits = true }
        //alert(isDigit(StrChar))
    }

    obj.value = StrTempValue;
}

function RoundNumber(NumberToRound) {

    return Math.round(NumberToRound * 100) / 100

}

function CalculatePrice(ObjInput) {

    SelectedValue = ObjInput.value;
    if (isEmpty(SelectedValue)) { ObjInput.value = 0; }

    StrDiv = "Total" + ObjInput.name;

    switch (ObjInput.name) {

        case "Book1":

            AmountSelectedBook = SelectedValue * 31;
            TotalBook1 = AmountSelectedBook
            break;

        case "Book2":

            AmountSelectedBook = SelectedValue * 31;
            TotalBook2 = AmountSelectedBook
            break;

        case "Book3":

            AmountSelectedBook = SelectedValue * 41.5;
            TotalBook3 = AmountSelectedBook
            break;

        case "Book4":

            AmountSelectedBook = SelectedValue * 33;
            TotalBook4 = AmountSelectedBook
            break;

        case "Book5":

            AmountSelectedBook = SelectedValue * 45;
            TotalBook5 = AmountSelectedBook
            break;

        case "Book6":

            AmountSelectedBook = SelectedValue * 27.5;
            TotalBook6 = AmountSelectedBook
            break;

        case "Book7":

            AmountSelectedBook = SelectedValue * 21.5;
            TotalBook7 = AmountSelectedBook
            break;

        case "Book8":

            AmountSelectedBook = SelectedValue * 0;
            TotalBook8 = AmountSelectedBook
            break;

    }

    //werk het totaal van het geselecteerd book bij
    document.getElementById(StrDiv).innerHTML = "&euro; " + RoundNumber(AmountSelectedBook).toFixed(2)

}

function CalculateTotalPrice() {

    TotalBooks = TotalBook1 +
    TotalBook2 +
    TotalBook3  +
    TotalBook4 +
    TotalBook5 +
    TotalBook6 +
    TotalBook7 +
    TotalBook8;

}

function SetPrices() {

    CalculatePrice(frm.Book1);
    CalculatePrice(frm.Book2);
    CalculatePrice(frm.Book3);
    CalculatePrice(frm.Book4);
    CalculatePrice(frm.Book5);
    CalculatePrice(frm.Book6);
    CalculatePrice(frm.Book7);
    CalculatePrice(frm.Book8);

    CalculateTotalPrice();

    document.getElementById("TotalAllBooks").innerHTML = "&euro; " + RoundNumber(TotalBooks + DefaultTransportCosts).toFixed(2)
    document.getElementById("TransportCosts").innerHTML = "&euro; " + RoundNumber(DefaultTransportCosts).toFixed(2)
    frm.BankAccountAmount.value = RoundNumber(TotalBooks + DefaultTransportCosts).toFixed(2)

    frm.HiddenTotalAmount.value = RoundNumber( (TotalBooks + DefaultTransportCosts) * 100).toFixed(0);    

}

function ShowInvoiceAddress(ObjCheck) {

    BoolHideOrShow = false;
    if (ObjCheck.checked) {BoolHideOrShow = true; }

    if (BoolHideOrShow) {

        document.getElementById("InvoiceEmptyTR").style.display = "";
        document.getElementById("InvoiceCompanyTR").style.display = "";
        document.getElementById("InvoiceDepartmentTR").style.display = "";    
        document.getElementById("InvoiceAddressTR").style.display = "";
        document.getElementById("InvoiceZipCodeCityTR").style.display = "";

    }
    else {

        document.getElementById("InvoiceEmptyTR").style.display = "none";
        document.getElementById("InvoiceCompanyTR").style.display = "none";
        document.getElementById("InvoiceDepartmentTR").style.display = "none";
        document.getElementById("InvoiceAddressTR").style.display = "none";
        document.getElementById("InvoiceZipCodeCityTR").style.display = "none";
    
    }

}

function CheckPersonal() {

    if(
    checkBoxChecked(frm.Gender,"Titelatuur")&&
    checkString(frm.Initials,"Voorletters")&&
    checkString(frm.LastName,"Achternaam")&&
    checkEmail(frm.Email, "E-mailadres")
    ){return true;}
    else{return false;}

}


function CheckAdress() {

    if (
    checkBoxChecked(frm.PrivBiz, "Privé \\ Zakelijk adres") &&
    checkString(frm.CompanyName, "Organisatie") &&
    checkString(frm.Address, "Adres") &&
    checkZipCode(frm.ZipCode, "Postcode") &&
    checkString(frm.City, "Plaats") &&
    checkPhone(frm.Phone, "Telefoon")     
    ) { return true; }
    else { return false; }

}

function CheckOrderForm() {

    if (
   
    CheckPersonal() &&
    CheckAdress()&&
    CheckInvoiceAddress() &&    
    Agreement() &&    
    Payment() &&
    PaymentData()

    ) {
        SetPrices();
        frm.action = "OrderControleren.asp";
        return true; 
    }
    else {return false;}

}

function SendOrderForm() {
    frm.action = "SubmitOrderScript.asp";
    frm.submit();
}

function CheckInvoiceAddress() {
    if (frm.InvoiceAddressCheckbox.checked) {
    
        if(
        checkString(frm.InvoiceCompanyName, "Organisatie bij factuuradres") &&
        checkString(frm.InvoiceAddress, "Adres bij factuuradres") &&
        checkZipCode(frm.InvoiceZipCode, "Postcode bij factuuradres") &&
        checkString(frm.InvoiceCity, "Plaats bij factuuradres")         
        ){return true;}
        else{return false;}

    }
    else{return true;}
}

function Payment() {

    if (frm.PaymentType[0].checked) {
        if (frm.PaymentAgreement.checked) { return true; }
        else {
        
            alert("U heeft nog niet aangegeven akkoord te gaan met automatische afschrijving van het totaalbedrag" );
            return false;
            
        }
    } else {return true;}
}

function PaymentData() {

    if (frm.PaymentType[0].checked) 
    {

        if (
    checkString(frm.BankAccount, "Rekeningnummer") &&
    checkString(frm.BankAccountName, "Naam rekeninghouder") &&
    checkString(frm.BankAccountCity, "Vestigingsplaats bank")
    ) { return true; }
        else { return false; }
    }
    else {return true;}
}

function Agreement() {

    if (frm.AgreeConditions.checked) { return true; }
    else {alert("U heeft nog niet aangegeven akkoord te gaan met de leveringsvoorwaarden");return false;}
}
