

// webservice postcode check - begin
var busySearching = false;
var lastPostcodeVal = '';
var lastHuisnummerVal = '';

function checkPostcode(postcode, huisnummer, landen, RegexPostcode, RegexHuisnummer) {

    var validP = false;
    if (postcode.search(RegexPostcode) == -1)
        validP = false;
    else
        validP = true;

    var validHn = false;
    if (huisnummer.search(RegexHuisnummer) == -1)
        validHn = false;
    else
        validHn = true;

    if (validP && validHn) {

        var oCountry = $('.countrydd').get(0);
        oCountry = oCountry.value;
        oCountry = oCountry.toLowerCase();

        if (landen.indexOf("|" + oCountry + "|") > -1) {
            if (lastPostcodeVal != postcode || lastHuisnummerVal != huisnummer) {
                busySearching = false;
                lastPostcodeVal = postcode;
                lastHuisnummerVal = huisnummer;

               // var straatTxt = $('.address').get(0);
               // var plaatsTxt = $('.city').get(0);

               // straatTxt.value = '';
               // plaatsTxt.value = '';

                if (postcode.length > 0 && huisnummer.length > 0) {

                    displayElement('loadingAnimationDiv1', 'true');
                    setTimeout("UpdateImg('spinner1','/img/spinner.gif');", 50);
                    displayElement('loadingAnimationDiv2', 'true');
                    setTimeout("UpdateImg('spinner2','/img/spinner.gif');", 50);

                    $.ajax({
                        url: "/InputChecks.asmx/DoPostCodeCheck",
                        data: "{'postcode': '" + postcode + "', 'huisnummer': '" + huisnummer + "'}",
                        success: function(msg) {
                            var results = msg.d;
                            //alert(results);
                            oncheckPostcodeComplete(results);
                        },
                        error: function(xhr, desc, exceptionobj) {
                            //alert('[error] webservice checkPostcode');
                            hideLoadingAnimations();
                        }
                   });

                }
            }
        }
    }
}

function oncheckPostcodeComplete(results) {

    if (results.replace('|', '').length > 0)
        fillAddress(results.split('|')[0], results.split('|')[1]);
    else
        fillAddress('', '');

    hideLoadingAnimations();

}

function hideLoadingAnimations() {
    var oloadingAnimationDiv1 = $('#loadingAnimationDiv1').get(0);
    oloadingAnimationDiv1.style.display = 'none';
    var oloadingAnimationDiv2 = $('#loadingAnimationDiv2').get(0);
    oloadingAnimationDiv2.style.display = 'none';
}

function fillAddress(address, city) {

    var oAddress = $('.addresstxtbox').get(0);
    var oCity = $('.citytxtbox').get(0);

    if (address.length != 0) {
        oAddress.value = address;
        oCity.value = city;

        setValidBorderColor(oAddress, true);
        setValidBorderColor(oCity, true);
    }
}

function showError(element) {
    element.value = '';
}

function checkElementZipCode(value, object, Regex, countries) {

    var countryDD = $('.countrydd').get(0);
    var country = countryDD.value;

    country = country.toLowerCase();

    if (countries.indexOf("|" + country + "|") > -1) {
        if (value.search(Regex) == -1)
            setValidBorderColor(object, false);
        else
            setValidBorderColor(object, true);
    }
    else {
        // buitenland
        if (value.search('^(.{1,})$') == -1)
            setValidBorderColor(object, false);
        else
            setValidBorderColor(object, true);

    }
}

// webservice postcode check - end
