
/* 
Bu eklenti orhan ekici tarafindan yazilmistir. 26.06.2009 
jQuery ile birlikte calisir. 
Sayfadaki butun selectboxlari etkiler. 
not: degisecek selectboxlarin block element eklendiginde sorun cikarmayacak bir element icinde olmasi gerekmektedir. 
        
Surum: 1.00 Beta 
*/
var n = 0;
var tuttum = 0;
var uyar = 0;
var is_ilk = true;
var is_blocked = false;
var super_uyari = 0;
var il_id = 0;
var ilce_id = 0;
var ilname = "";
var ilce_name = "";
var xml = "";
$(function() {
    xml = $("input#ctl00___xml").val();
    getstarted();
})
function getstarted() {
    var is_fill_for_town = false;
    il_id = $("input#ctl00__inCityCode").val();
    ilce_id = $("input#ctl00__inTownCode").val();
    $.xmlDOM(xml).find('il').each(function(m) {

        if (il_id == "0@" + $("input#default_index_il").val()) {
            ilname = $("input#default_index_il").val();
            $("select#cityy").val(il_id);
            ilce_name = $("input#default_index").val(); ;
        }
        $('ul#searchtools select[name="selectCity"]').append('<option value=' + $(this).attr('value') + '@' + $(this).attr('name') + '>' + $(this).attr('name') + '</option>')
        if (m == $.xmlDOM(xml).find('il').length - 1) { if (is_fill_for_town) fillData_for_start(); else { tamamla(); } return false; }
        if ($(this).attr('value') + '@' + $(this).attr('name') == il_id) {
            ilname = $(this).attr('name');
            $("select#cityy").val(il_id);
            is_fill_for_town = true;
        }
    });
}
function fillData_for_start() {
    if (il_id != "0@" + $("input#default_index_il").val()) {
        var uzunluk = 0;

        $.xmlDOM(xml).find('il').each(function() {
            if ($(this).attr('value') + '@' + $(this).attr('name') == $("select#cityy").val()) {
                uzunluk = $(this).find('ilce').length - 1;
                $(this).find('ilce').each(function(y) {
                    if (ilce_id == "0@" + $("input#default_index").val()) {
                        ilce_name = $("input#default_index").val();
                        $("select#townn").val(ilce_id);
                    }
                    $('select[name="selectTown"]').append('<option value=' + $(this).attr('value') + '@' + $(this).text() + '>' + $(this).text() + '</option>')
                    if (y == uzunluk) { tamamla(); return false; }
                    if ($(this).attr('value') + '@' + $(this).text() == ilce_id) {
                        ilce_name = $(this).text();
                        $("select#townn").val(ilce_id);
                    }
                })
            }
        })


    }
}
function tamamla() {
    var yazilacak = "";
    var ata_number = 0;
    var deger = "";
    $('ul#searchtools select').each(function() {
        ata_number++;
        if (ata_number == 1) {
            yazilacak = ilname;
            deger = "city_gizleme";
        }
        else if (ata_number == 2) {
            yazilacak = ilce_name;
            deger = "town_gizleme";
        }
        $(this).hide()
        $(this).after(
                        $('<div />')
                                .addClass('jSelectBoxContainer clearfix')
                                .append($('<a />').addClass('jSelectBoxSelectorLink').attr('href', 'javascript:;').append($('<span />').text(yazilacak)))
                                .append($('<div />').addClass('jSelectBoxSelectElements').attr('id', deger).append($('<ul />')))
                  )
        jSelectUpdate($(this).attr('name'))
    })
    $('a.jSelectBoxSelectorLink').click(function() { $(this).next().toggle(150); $(this).parent().toggleClass('on'); })
}
function jSelectUpdate(sbName) {
    $('select[name="' + sbName + '"]').next().children(':last').children('ul').empty()
    $('select[name="' + sbName + '"]').children().each(function(i) {
        $(this).parent().next().children(':last').children('ul').append($('<li />').append($('<a />').attr({ href: 'javascript:;', rel: i }).html('<span>' + $(this).text() + '</span>')))
    })
    $('div.jSelectBoxContainer ul li:last').addClass('last');
    $('div.jSelectBoxContainer ul li a').unbind("focus")
    $('div.jSelectBoxContainer ul li a').bind("focus", function() {
        $(this).parent().parent().parent().prev().html('<span>' + $(this).text() + '</span>').click();
        $(this).parent().parent().parent().prev().html('<span>' + $(this).text() + '</span>').blur();
        $(this).parent().parent().parent().parent().prev()[0].selectedIndex = $(this).attr('rel');
        $(this).parent().parent().parent().parent().prev().change();

    })
}
function fillData(name) {
    var u = 0;
    $('a.jSelectBoxSelectorLink').each(function() {
        u++;
        if (u == 2) {
            $(this).html('<span> ' + $("input#default_index").val() + '</span>');
            $("select#townn").val("0@" + $("input#default_index").val());
        }
    })
    $('select[name="' + name + '"]').empty();
    $('select[name="' + name + '"]').append('<option value="0@' + $("input#default_index").val() + '">' + $("input#default_index").val() + '</option>')
    var uzunluk = 0;
    $.xmlDOM(xml).find('il').each(function() {
        if ($(this).attr('value') + '@' + $(this).attr('name') == $('select[name="selectCity"]').val()) {
            uzunluk = $(this).find('ilce').length - 1;
            $(this).find('ilce').each(function(y) {
                $('select[name="selectTown"]').append('<option value=' + $(this).attr('value') + '@' + $(this).text() + '>' + $(this).text() + '</option>')
                if (y == uzunluk) { jSelectUpdate(name); return false; }
            })
        }
    })

    b = 0;
}
var kntrl = 0;
var x = 1;
var eski = "";
var y = 1;
var dizi = new Array(500);
var dizi1 = new Array(500);
var i = 0;
dizi[500] = "";
dizi1[500] = "";
var eski1 = "";
var sabit = 0;
var f = 0;
var z = 1;
var u = 0;
var b = 0;

function keyup(e) {
    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    if (e.keyCode == 191) code = 214;
    if (e.keyCode == 220) code = 199;
    if (e.keyCode == 221) code = 220;
    if (e.keyCode == 59) code = 350;
    if (e.keyCode == 219) code = 208;
    if (e.keyCode == 222) code = 304;
    var character = String.fromCharCode(code);

    if (document.getElementById("city_gizleme") != null) {
        if (document.getElementById("city_gizleme").style.display == "block") {
            b = 0;
            // alert("b cityyde= " + b); 
            if ((eski == character) || (u == 0)) {
                if (u == 0) {
                    eski = character;
                    if (y == 1) {
                        $('div.jSelectBoxContainer ul li a ').each(function() {
                            dizi[i] = $(this).text();
                            i++;

                        });
                        for (var g = 0; g < dizi.length; g++) {
                            if (dizi[g].charAt(0) == character) {
                                y = g;
                                break;
                            }
                        }
                    }
                } else {
                    if (y == 0) {
                        y = 72;
                    }
                    if ((y != 3) && (y != 1) && (y != 2)) { y++; }
                    if (y == 3) { y = 42; }
                    if (y == 1) { y = y + 2; }
                    if (y == 2) { y = 4; }

                }
                u = 1;
                if (character == dizi[y].charAt(0)) {
                    $('div.jSelectBoxContainer ul li a ').each(function() {
                        if ($(this).text() == dizi[y]) {
                            $(this).focus();
                            var j = setTimeout('document.getElementById("city_gizleme").style.display = "block";', 200);
                            return false;
                        }
                    });
                }
            } else {
                eski = character;
                for (var d = 0; d < dizi.length; d++) {
                    if (dizi[d].charAt(0) == character) {
                        y = d;
                        break;
                    }
                }
                $('div.jSelectBoxContainer ul li a ').each(function() {
                    if ($(this).text() == dizi[y]) {
                        $(this).focus();
                        var j = setTimeout('document.getElementById("city_gizleme").style.display = "block";', 200);
                        return false;
                    }
                });
            }

        } else {
            if (document.getElementById("town_gizleme").style.display == "block") {
                //            alert("eski1= " + eski1); 
                //            alert("character= " + character); 
                //            alert("b= " + b); 
                if ((eski1 == character) || (b == 0)) {
                    if (b == 0) {
                        eski1 = character;
                        // alert("zye girdi"); 
                        $('div#town_gizleme ul li a').each(function() {


                            dizi1[f] = $(this).text();
                            f++;

                        });
                        f = 0;
                        //  alert(dizi[3]); 
                        for (var g = 0; g < dizi1.length; g++) {
                            if (dizi1[g].charAt(0) == character) {
                                z = g;
                                break;
                            }
                        }

                    } else {
                        z++;
                    }
                    b = 1;
                    if (character == dizi1[z].charAt(0)) {
                        $('div.jSelectBoxContainer ul li a ').each(function() {
                            if ($(this).text() == dizi1[z]) {
                                $(this).focus();
                                var j = setTimeout('document.getElementById("town_gizleme").style.display = "block";', 200);
                                return false;
                            }
                        });
                    }
                } else {
                    eski1 = character;
                    for (var d = 0; d < dizi1.length; d++) {
                        if (dizi1[d].charAt(0) == character) {
                            z = d;
                            break;
                        }
                    }
                    $('div.jSelectBoxContainer ul li a ').each(function() {
                        if ($(this).text() == dizi1[z]) {
                            $(this).focus();
                            var j = setTimeout('document.getElementById("town_gizleme").style.display = "block";', 200);
                            return false;
                        }
                    });
                }
            }
        }
    }
}
function doSomething(e) {
    var hedef;

    if (document.getElementById("city_gizleme") != null) {
        if (!e) var e = window.event;
        if (e.target) hedef = e.target;
        else if (e.srcElement) hedef = e.srcElement;
        if (hedef.nodeType == 3) // defeat Safari bug 
            hedef = hedef.parentNode;
        if (hedef.id == "city_gizleme") {
            b = 0;
        }
        // alert(hedef.id); 
        if ((hedef.id == "city_gizleme") || (hedef.id == "town_gizleme")) {
            x = 1; y = 1;
        } else {
            document.getElementById("city_gizleme").style.display = "none";
            document.getElementById("town_gizleme").style.display = "none";
            sabit = 0;
        } if (document.getElementById("town_gizleme").style.display == "none")
            sabit = 0;
    }
}


/*selectbox kodu bitti*/ 
    
