var ValidVatID;

function CountPriceOnForm() {
    var strCurrency = 'EUR'
    var mode = document.getElementById('idMode');
    var ctrlLicCount = document.getElementById('lic_count');
    if (ctrlLicCount.value == "" || ctrlLicCount.value == "0" || !isNumeric(ctrlLicCount.value)) {
        ctrlLicCount.value = "";
        ctrlLicCount.select();
        alert("Please enter the correct number of licenses. ");
        return;
    }

    var ctrlSbsLic = document.getElementById('sbs_lic');
    if (ctrlSbsLic) is_sbs = ctrlSbsLic.checked;

    document.form1.action = "/exchange-folder-sync/buy/?cur=" + strCurrency + "&lic=" + ctrlLicCount.value + "&sbs=" + is_sbs; ;
    document.form1.submit();
}
function ClientCountrySelected(ctrlCountry) {
    var countryCode = ctrlCountry.options[ctrlCountry.selectedIndex].value;

    // Ustaw nazwe kraju w ukrytym polu formularza
    document.getElementById('client_country_name').value = ctrlCountry.options[ctrlCountry.selectedIndex].innerHTML;

    if (IsEuCountry(countryCode) && countryCode != 'DE_iAmFalse' && countryCode != 'AT_iAmFalse') {
        document.getElementById('idTaxRow0').style.display = "block";
    }
    else {
        document.getElementById('idTaxRow0').style.display = "none";
    }
}

function LicenseCountrySelected(ctrlCountry) {
    var countryCode = ctrlCountry.options[ctrlCountry.selectedIndex].value;

    // Ustaw nazwe kraju w ukrytym polu formularza
    document.getElementById('license_country_name').value = ctrlCountry.options[ctrlCountry.selectedIndex].innerHTML;
}
function ShowKnowFromEdit() {
    var ctrlCombo = document.getElementById('know_from');
    if (ctrlCombo.options[ctrlCombo.selectedIndex].value == 'Inne') {
        document.getElementById('know_from_other').style.display = 'block';
        document.getElementById('know_from_other').value = "Please provide brief information...";
        document.getElementById('know_from_other').style.select();
    }
    else if (ctrlCombo.options[ctrlCombo.selectedIndex].value == 'Inna stona internetowa') {

        document.getElementById('know_from_other').style.display = 'block';
        document.getElementById('know_from_other').value = "Please provide the website name...";
        document.getElementById('know_from_other').select();
    }
    else {
        document.getElementById('know_from_other').style.display = 'none';
    }

}
function ShowLicenseData() {
    document.getElementById('license_data').style.display = document.getElementById('dif_license').checked ? 'block' : 'none';
}
function ValidateFormData() {
    // Liczba licencji
    var ctrlLicCount = document.getElementById('lic_count');
    if (ctrlLicCount.value == "" || ctrlLicCount.value == "0" || !isNumeric(ctrlLicCount.value)) {
        alert("Geben Sie richtige Lizenzenanzahl ein.");
        ctrlLicCount.focus();
        ctrlLicCount.select();
        return false;
    }

    // Osoba odpowiedzialna
    var ctrlRespPersonFirstName = document.getElementById('client_first_name');
    if (trim(ctrlRespPersonFirstName.value) == '') {
        ctrlRespPersonFirstName.select();
        ctrlRespPersonFirstName.focus();
        alert("Geben Sie den Vornamen des Auftragsberechtigten an.");
        return false;
    }

    // Osoba odpowiedzialna
    var ctrlRespPersonLastName = document.getElementById('client_last_name');
    if (trim(ctrlRespPersonLastName.value) == '') {
        ctrlRespPersonLastName.select();
        ctrlRespPersonLastName.focus();
        alert("Geben Sie den Nachnamen des Auftragsberechtigten an.");
        return false;
    }

    // Email
    var ctrlUserMail = document.getElementById('client_mail');
    if (!IsSmtpAddress(ctrlUserMail.value)) {
        ctrlUserMail.select();
        ctrlUserMail.focus();
        alert("Geben Sie Ihre korrekte E-Mail an.");
        return false;
    }

    // Telefon kontaktowy
    var ctrlUserPhone = document.getElementById('client_phone');
    if (ctrlUserPhone.value == '') {
        ctrlUserPhone.select();
        ctrlUserPhone.focus();
        alert("Geben Sie Ihre Telefonnummer für Kontakte an.");
        return false;
    }

    // Nazwa firmy
    var ctrlClientName = document.getElementById('client_name');
    if (trim(ctrlClientName.value) == '') {
        ctrlClientName.select();
        ctrlClientName.focus();
        alert("Geben Sie den Firmennamen oder den Vor- und Nachnamen des Rechnungsempfängers an.");
        return false;
    }

    // Adres firmy
    var ctrlClientAddr = document.getElementById('client_addr');
    if (trim(ctrlClientAddr.value) == '') {
        ctrlClientAddr.select();
        ctrlClientAddr.focus();
        alert("Geben Sie die Anschrift der Firma oder der Person für den Rechnungsempfang an.");
        return false;
    }

    // Miasto
    var ctrlClientCity = document.getElementById('client_city');
    if (trim(ctrlClientCity.value) == '') {
        ctrlClientCity.select();
        ctrlClientCity.focus();
        alert("Geben Sie den Firmensitz an.");
        return false;
    }

    // Kraj
    var ctrlCountry = document.getElementById('client_country_code');
    if ('' == ctrlCountry.options[ctrlCountry.selectedIndex].value) {
        ctrlCountry.focus();
        alert("Bitte wählen Sie das Land aus!");
        return false;
    }

    // Firma na ktora wystawic licencje
    var ctrlDifLicense = document.getElementById('dif_license');
    if (ctrlDifLicense.checked) {
        var ctrlLicenseName = document.getElementById('license_name');
        if (trim(ctrlLicenseName.value) == '') {
            ctrlLicenseName.select();
            ctrlLicenseName.focus();
            alert("Geben Sie den Firmennamen oder den Vor- und Nachnamen des Lizenznehmers an.");
            return false;
        }

        var ctrlLicenseAddr = document.getElementById('license_addr');
        if (trim(ctrlLicenseAddr.value) == '') {
            ctrlLicenseAddr.select();
            ctrlLicenseAddr.focus();
            alert("Bitte wählen Sie das Land der Firma oder der Person für die Lizenzausstellung aus.");
            return false;
        }

        var ctrlLicenseCountry = document.getElementById('license_country_code');
        if ('' == ctrlLicenseCountry.options[ctrlLicenseCountry.selectedIndex].value) 
        {
            ctrlLicenseCountry.focus();
            alert("Geben Sie die Land der Firma oder der Person für die Lizenzausstellung an. ");
            return false;
        }
    }


    // VAT ID
    var ctrlVAT = document.getElementById('client_nip');
    /*if (IsEuCountry(ctrlCountry.options[ctrlCountry.selectedIndex].value) && ctrlCountry.options[ctrlCountry.selectedIndex].value != "DE" && ctrlCountry.options[ctrlCountry.selectedIndex].value != 'AT') */
    if (IsEuCountry(ctrlCountry.options[ctrlCountry.selectedIndex].value))
    {
        if (trim(ctrlVAT.value) == '') 
        {
            ctrlVAT.focus();
            if (!confirm("Ihre Firma ist in der EU ansässig, es wurde aber keine USt-IdNr angegeben. Dem Rechnungsbetrag werden 22% MwSt. zugerechnet. Fortsetzen?")) 
            {
                return false;
            }
        }
        else if (ValidVatID == false) 
        {
            ctrlVAT.focus();
            alert("Geben Sie korrekte USt-IdNr. ein oder lassen Sie das Feld leer.");
            return false;
        }

    }

    // Regulamin
    var ctrlAgreement = document.getElementById('idAgree');
    if (!ctrlAgreement.checked) {
        ctrlAgreement.focus();
        alert("Für Auftragserteilung lesen und akzeptieren Sie unsere Geschäftsbedingungen. Falls Sie mit unseren Geschäftsbedingungen nicht einverstanden sind, können wir Ihren Auftrag nicht abwickeln.");
        return false;
    }

    document.getElementById('VatID_checked').value = 'true';
    return true;
}

function CheckVatID() {
    document.getElementById('message-line-error-1').style.display = 'none';
    document.getElementById('message-line-error-2').style.display = 'none';
    document.getElementById('message-line-yes').style.display = 'none';
    document.getElementById('message-line-no').style.display = 'none';
    document.getElementById('message-line-timeout').style.display = 'none';

    if (document.getElementById('client_nip').value != '') {
        ValidVatID = false;
        document.getElementById('loading').style.display = '';

        var strVatID = document.getElementById('client_nip').value;
        var strCountryCode = document.getElementById('client_country_code').value;

        if (typeof XMLHttpRequest == "undefined") XMLHttpRequest = function() {
            try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch (e) { }
            try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch (e) { }
            try { return new ActiveXObject("Msxml2.XMLHTTP") } catch (e) { }
            try { return new ActiveXObject("Microsoft.XMLHTTP") } catch (e) { }
            throw new Error("This browser does not support XMLHttpRequest.")
        };
        
        http = new XMLHttpRequest();
        var url = "/libs/vatchecker_js.php";
        var params = "cc=" + strCountryCode + "&vid=" + strVatID;

        http.open("POST", url, true);

        //Send the proper header information along with the request
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.setRequestHeader("Content-length", params.length);
        http.setRequestHeader("Connection", "close");

        http.onreadystatechange = function() {
            if (http.readyState == 4 && http.status == 200) {
                document.getElementById('loading').style.display = 'none';
                var strResponse = http.responseText;
                if (strResponse == 'Error') {
                    document.getElementById('message-line-error-1').style.display = 'inline';
                    document.getElementById('message-line-error-2').style.display = 'inline';
                }
                else if (strResponse == 'NO') {
                    document.getElementById('message-line-no').style.display = '';
                    document.getElementById('message-line-error-2').style.display = '';
                }
                else if (strResponse == 'YES') {
                    document.getElementById('message-line-yes').style.display = '';
                    ValidVatID = true;
                }
                else {
                    document.getElementById('message-line-timeout').style.display = '';
                    ValidVatID = true;
                }
            }
        }
        http.send(params);
    }
}

function ShowExampleSection(sectionName) {
    var counter = 0;
    if (sectionName == "")
        return;
    tempArray = document.getElementsByTagName('div');

    for (counter = 0; counter < tempArray.length; counter++) {
        if (tempArray[counter].style.display == 'block') {
            tempArray[counter].style.display = 'none';
        }
    }
    document.getElementById(sectionName).style.display = 'block';

}

function HideAllExamplesSection() {

    tempArray = document.getElementsByTagName('div');

    for (counter = 0; counter < tempArray.length; counter++) {
        if (tempArray[counter].style.display == 'block') {
            tempArray[counter].style.display = 'none';
        }
    }
}
function ExamplesOnLoad() {
    var url = document.URL;
    var match = /ex\=([\w\s\-]+)/;
    var isThere = url.match(match);
    if (isThere && isThere.length==2)
    {
        ShowExampleSection(isThere[1]);
    }
    /*
    var url = document.URL;
    var sSubStrings;

    if ((sSubStrings = url.split('?ex=')).length != 1) {
        ShowExampleSection(sSubStrings[1]);
    }
    else {
        //HideAllExamplesSection();
    }
    */
}
