var ValidVatID;

function CountPrice(combo, anchor) {
    var strCurrency = combo.options[combo.selectedIndex].value;
    window.location = "/exchange-folder-sync/pricing/?cur=" + strCurrency + anchor;
}

function CountPriceOnForm() {
    var ctrlCurrency = document.getElementById('idCurrency');
    var strCurrency = ctrlCurrency.options[ctrlCurrency.selectedIndex].value;
    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";
        //document.getElementById('paypal-verified').style.display = "block";
    }
    else {
        document.getElementById('idTaxRow0').style.display = "none";
        //document.getElementById('paypal-verified').style.display = "none";
    }
    if (countryCode == 'DE_iAmFalse' || countryCode == 'AT_iAmFalse' || countryCode == 'CH_iAmFalse') {
        document.getElementById('saferpay').style.display = "block";
    }
    else {
        document.getElementById('saferpay').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("Please enter the correct number of licenses.   ");
        ctrlLicCount.focus();
        ctrlLicCount.select();
        return false;
    }

    // Title
    var ctrlTitle = document.getElementById('client_title');
    if ('' == ctrlTitle.options[ctrlTitle.selectedIndex].value) {
        ctrlTitle.focus();
        alert("Enter the title of the person responsible for this order.");
        return false;
    }


    // Osoba odpowiedzialna
    var ctrlRespPersonFirstName = document.getElementById('client_first_name');
    if (trim(ctrlRespPersonFirstName.value) == '') {
        ctrlRespPersonFirstName.select();
        ctrlRespPersonFirstName.focus();
        alert("Enter the name of the person responsible for this order.");
        return false;
    }

    // Osoba odpowiedzialna
    var ctrlRespPersonLastName = document.getElementById('client_last_name');
    if (trim(ctrlRespPersonLastName.value) == '') {
        ctrlRespPersonLastName.select();
        ctrlRespPersonLastName.focus();
        alert("Enter the last name of the person responsible for this order.");
        return false;
    }

    // Email
    var ctrlUserMail = document.getElementById('client_mail');
    if (!IsSmtpAddress(ctrlUserMail.value)) {
        ctrlUserMail.select();
        ctrlUserMail.focus();
        alert("Enter the correct e-mail address.   ");
        return false;
    }

    // Telefon kontaktowy
    var ctrlUserPhone = document.getElementById('client_phone');
    if (ctrlUserPhone.value == '') {
        ctrlUserPhone.select();
        ctrlUserPhone.focus();
        alert("Enter a contact telephone number.   ");
        return false;
    }

    // Nazwa firmy
    var ctrlClientName = document.getElementById('client_name');
    if (trim(ctrlClientName.value) == '') {
        ctrlClientName.select();
        ctrlClientName.focus();
        alert("Enter the name of the company to be placed on the invoice.");
        return false;
    }

    // Adres firmy
    var ctrlClientAddr = document.getElementById('client_addr');
    if (trim(ctrlClientAddr.value) == '') {
        ctrlClientAddr.select();
        ctrlClientAddr.focus();
        alert("Enter the address of the company to be placed on the invoice.");
        return false;
    }

    // Kod pocztowy
    var ctrlClientZipCode = document.getElementById('client_zip_code');
    if (trim(ctrlClientZipCode.value) == '') {
        ctrlClientZipCode.select();
        ctrlClientZipCode.focus();
        alert("Enter the ZIP Code of the company to be placed on the invoice.");
        return false;
    }

    // Miasto
    var ctrlClientCity = document.getElementById('client_city');
    if (trim(ctrlClientCity.value) == '') {
        ctrlClientCity.select();
        ctrlClientCity.focus();
        alert("Enter the city of the company to be placed on the invoice.");
        return false;
    }

    // Kraj
    var ctrlCountry = document.getElementById('client_country_code');
    if ('' == ctrlCountry.options[ctrlCountry.selectedIndex].value) {
        ctrlCountry.focus();
        alert("Please choose your country.  ");
        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("Enter the name of the licensee.   ");
            return false;
        }

        var ctrlLicenseAddr = document.getElementById('license_addr');
        if (trim(ctrlLicenseAddr.value) == '') {
            ctrlLicenseAddr.select();
            ctrlLicenseAddr.focus();
            alert("Enter the address (street and city) of the licensee.   ");
            return false;
        }

        var ctrlLicenseCountry = document.getElementById('license_country_code');
        if ('' == ctrlLicenseCountry.options[ctrlLicenseCountry.selectedIndex].value) 
        {
            ctrlLicenseCountry.focus();
            alert("Choose the country of the licensee.  ");
            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("Your company is based in the EU, but you have not entered a valid VAT ID. 22% VAT will be added to the order value.\n\nDo you want to continue?")) 
            {
                return false;
            }
        }
        else if (ValidVatID == false) 
        {
            ctrlVAT.focus();
            alert("Please enter a valid VAT ID or leave this field empty!");
            return false;
        }

    }

    // Regulamin
    var ctrlAgreement = document.getElementById('idAgree');
    if (!ctrlAgreement.checked) {
        ctrlAgreement.focus();
        alert("To place an order, please read and accept the Terms and Conditions for Sales of Services and Order Completion . If you reject its provisions, we will not be able to complete your order.");
        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();
    }
    */
}
