﻿
//Realestate search box
function OnRealEstateSearchReady() {    
    $("select#cityId").change(OncityIdChange);
    $("select#townId").change(OntownIdChange);
    $('#priceMin').watermark('min');
    $('#priceMax').watermark('max');
    SetCityTownValues();
    $('#priceMin').priceFormat({
        prefix: '',
        clearPrefix: true,
        allowNegative: true,
        centsSeparator: '',
        thousandsSeparator: '.',
        centsLimit: 0
    });
    $('#priceMax').priceFormat({
        prefix: '',
        clearPrefix: true,
        allowNegative: true,
        centsSeparator: '',
        thousandsSeparator: '.',
        centsLimit: 0
    });
}

function SetCityTownValues()
{
    if (selectedCityId > 0)
    {
        PopulateTown(selectedCityId, selectedTownId);
    }

    if (selectedTownId > 0)
    {
        PopulateDistrict(selectedTownId, selectedDistrictId);
    }

}

function OncityIdChange() {
    var selectedcityId = $("#cityId").val();
    PopulateTown(selectedcityId, -1);
    ClearForCityChange();
}

function OntownIdChange() {
    var selectedTownId = $("#townId").val();
    PopulateDistrict(selectedTownId, -1);
}

function PopulateTown(varCityId, varDefaultTownId) {
    var url = '/CityTownAjax/GetTowns/?cityId=' + varCityId;
    PopulateList("townId", url, varDefaultTownId, "İlçe Seçiniz");
}

function PopulateDistrict(varTownId, varDefaultDistrictId) {
    var url = '/CityTownAjax/GetDistricts/?townId=' + varTownId;
    PopulateList("districtId", url, varDefaultDistrictId, "Semt Seçiniz");
}

function ClearForCityChange() {
    $("#districtId").html('<option value="">Semt Seçiniz</option>');
}

function PopulateList(varListId, varUrl, varSelectedId, varPleasSelectText)
{
    $("#" + varListId).html('<option value="">' + varPleasSelectText + '</option>');    
    $.getJSON(varUrl, function (JSON) {
        var options = '<option value="">' + varPleasSelectText + '</option>';
        $.each(JSON, function (i, data) {
            options += '<option value="' + data.Value + '">' + data.Name + '</option>';
        });
        $("#" + varListId).html(options);
        if (varSelectedId > 0) {
            $("#" + varListId).val(varSelectedId);
        }
    });
}

function OnRealEstateSearchClick(){
    //int? satisTipiLabelId, int? realEstateTypeId, int? cityId, int? townId, int? districtId, int? priceMin, int? priceMax, int? advertId

    var satisTipi = $('input[name=satisTipiLabelId]:checked').val();
    if (satisTipi == undefined) {
        satisTipi = '';
    }
    var url = '/Advert/SearchResult?satisTipiLabelId=' + satisTipi + '&realEstateTypeId=' + $("#realEstateTypeId").val() + '&cityId=' + $("#cityId").val() + '&townId=' + $("#townId").val() + '&districtId=' + $("#districtId").val() + '&priceMin=' + $("#priceMin").val().replace(/\./g, '') + '&priceMax=' + $("#priceMax").val().replace(/\./g, '') + '&advertId=' + $("#advertId").val();

    document.location = url;
}



//General functions
//onkeypress="return CheckAllowedChars(event,numbers)"
var numbers = '1234567890';

function CheckAllowedChars(e, allow) {
    var e = e || window.event;
    var kCode = e.which || e.keyCode;
    if (kCode == 8 || kCode == 0 || kCode == 9) //backspace veya extended tuslar
    {
        return true;
    }

    var ctrl = (document.all) ? event.ctrlKey : e.modifiers & Event.CONTROL_MASK;
    if (e.ctrlKey && (kCode == 86 || kCode == 118)) //CTRL+V / 118-v
        return true;
    
    var strval = String.fromCharCode(kCode);
    if (allow.indexOf(strval) < 0) { DoStopEvent(e); return false; }
    return true;
}

function DoStopEvent(pE) {
    if (!pE)
        if (window.event)
            pE = window.event;
        else
            return;
    if (pE.cancelBubble != null)
        pE.cancelBubble = true;
    if (pE.stopPropagation)
        pE.stopPropagation();
    if (pE.preventDefault)
        pE.preventDefault();
    if (window.event)
        pE.returnValue = false;
    if (pE.cancel != null)
        pE.cancel = true;
}
