﻿var emptyError;
var prevSubType = null;
var viaSearch = false;

$(document).ready(function() {
    // set initial values
    displaySubType();
    $('.viaSearchKey').val("0");

    // hook
    $('#dropSubType').change(function() {
        displaySubType();
    });
    // these will be unbound by autocompleter
    $('.idToggle').focus(function() {
        if ($(this).val() == $(this).attr('title')) {
            $(this).val('');
            $(this).removeClass('input-default');
        }
    });
    $('.idToggle').blur(function() {
        if ($(this).val().trim() == '') {
            $(this).val($(this).attr('title'));
            $(this).addClass('input-default');
        }
    });

});

function viaFooterSearchValidate(evt) {
    if (evt.type == 'keypress' && evt.keyCode != 13) return true;

    var subTiep = $('#dropSubType').val();
    var srchKey = getSearchValue(subTiep);
    if (srchKey == null) {
        /*$('.notify-error').text(emptyError);
        $('.notify-error').show();
        evt.preventDefault();*/
        srchKey = "HEELNEDERLAND";
        //return false;
    } else {
        $('.notify-error').hide();
    }
    $('.viaSearchKey').val(subTiep + '|' + srchKey);
    return true;
}

function displaySubType() {
    var cs = $('#dropSubType').val();
    var inpDefUnbound = false;
    switch (cs) {
        case '10':
        case '11':
        case '13':
        case '20':
        case '21':
        case '22':
        case '12':
            $('#plaats').show();
            $('#plaats').val($('#plaats').attr('title'));
            $('#land').hide();
            $('#horegio').hide();
            $('#provincie').hide();
            $('#landelijk').hide();
            break;
        case '14':
            $('#plaats').hide();
            $('#land').show();
            $('#land').val($('#land').children(':first').val());
            $('#horegio').hide();
            $('#provincie').hide();
            $('#landelijk').hide();
            break;
        case '23':
            $('#plaats').hide();
            $('#land').hide();
            $('#horegio').show();
            $('#horegio').val($('#horegio').children(':first').val());
            $('#provincie').hide();
            $('#landelijk').hide();
            break;
        case '24':
        case '25':
            $('#plaats').hide();
            $('#land').hide();
            $('#horegio').hide();
            $('#provincie').show();
            $('#provincie').val($('#provincie').children(':first').val());
            $('#landelijk').hide();
            break;
        case '205':    // landelijk
        case '102':
        case '111':
            $('#plaats').hide();
            $('#land').hide();
            $('#horegio').hide();
            $('#provincie').hide();
            $('#landelijk').show();
            $('#landelijk').val($('#landelijk').children(':first').val());
            break;
    }
    if (prevSubType != cs) {
        // koop|huur|recreatie|kantoor|bedrijfshal|winkel
        if (prevSubType != null) {
            inpDefUnbound = true;
        }
        var aanbod = null;
        if (cs == '10') aanbod = 'koop';
        if (cs == '11') aanbod = 'huur';
        if (cs == '13') aanbod = 'recreatie';
        if (cs == '20') aanbod = 'kantoor';
        if (cs == '21') aanbod = 'bedrijfshal';
        if (cs == '22') aanbod = 'winkel';
        if (aanbod != null) {
            var url = "clientactie/GetAllLocations/";
            var _prefix = /(\/funda\/|\/fundainbusiness\/)/.exec(window.location.href);
            if (_prefix) url = _prefix[0] + url;
            else url = "/" + url;
        }

        prevSubType = cs;
    }
    if (inpDefUnbound) {
        $('.idToggle').focus(function() {
            if ($(this).val() == $(this).attr('title')) {
                $(this).val('');
                $(this).removeClass('input-default');
            }
        });
        $('.idToggle').blur(function() {
            if ($(this).val().trim() == '') {
                $(this).val($(this).attr('title'));
                $(this).addClass('input-default');
            }
        });
    }
}

function getSearchValue(cs) {
    var chk;
    emptyError = '';
    
    switch (cs) {
        case '10':
        case '11':
        case '13':
        case '20':
        case '21':
        case '22':
        case '12':
            chk = $('#plaats').val().trim();
            if (chk.length == 0 || chk == $('#plaats').attr('title')) {
                emptyError = 'U moet een plaats invullen.';
                return null;
            }
            ret = chk;
            break;
        case '14':
            ret = $('#land').val();
            break;
        case '23':
            ret = $('#horegio').val();
            break;
        case '24':
        case '25':
            ret = $('#provincie').val();
            break;
        case '205':     // landelijk
        case '102':
        case '111':
            ret = $('#landelijk').val();
            break;
    }
    return ret;
}

