var LocationTransfer = false;

function subMenu(id) {
    $('#' + id).fadeToggle(500);
}

function setBasvuruMenu(step, selection) {
    if (step == '1') {
        if (selection == '1') {
            $.ajax({
                url: '/basvuru/step2a.html',
                cache: false,
                dataType: "html",
                success: function (data) {
                    $('#basvuru_selection').empty();
                    $('#basvuru_selection').html(data);
                    $.colorbox.resize({ height: "210px" });
                    $('#selection1').val("1");
                }
            });
        } else if (selection == '2') {
            $.ajax({
                url: '/basvuru/step2b.html',
                cache: false,
                dataType: "html",
                success: function (data) {
                    $('#basvuru_selection').empty();
                    $('#basvuru_selection').html(data);
                    $.colorbox.resize({ height: "260px" });
                    $('#selection1').val("2");
                }
            });
        } else if (selection == '3') {
            $.ajax({
                url: '/basvuru/step2b.html',
                cache: false,
                dataType: "html",
                success: function (data) {
                    $('#basvuru_selection').empty();
                    $('#basvuru_selection').html(data);
                    $.colorbox.resize({ height: "260px" });
                    $('#selection1').val("3");
                }
            });
        } else if (selection == '4') {
            $.ajax({
                url: '/basvuru/step2a.html',
                cache: false,
                dataType: "html",
                success: function (data) {
                    $('#basvuru_selection').empty();
                    $('#basvuru_selection').html(data);
                    $('#basvuru_sub').fadeIn(500);
                    $.colorbox.resize({ height: "210px" });
                    $('#selection1').val("4");
                }
            });
        } else {
            $('#basvuru_selection').empty();
            $('#basvuru_selection').html('<div class="error_case">Geçici bir hata oluştu. Lütfen daha sonra tekrar deneyiniz.</div>');
        }
    } else if (step == '2') {

        if (LocationTransfer) {
            if (self.location.href.indexOf('/basvuru/') != -1) {
                if (self.location.href.indexOf('?') != -1)
                    top.location.href = self.location.href.substring(0, self.location.href.indexOf('?')) + '?LS=' + (selection == '1' ? 'TURKIYE' : 'YDISI');
                else
                    top.location.href = self.location.href + '?LS=' + (selection == '1' ? 'TURKIYE' : 'YDISI');
            } else
                top.location.href = '/basvuru/?LS=' + (selection == '1' ? 'TURKIYE' : 'YDISI');
        }

        $.ajax({
            url: 'SessionState.aspx?LS=' + (selection == '1' ? 'TURKIYE' : 'YDISI'),
            cache: false,
            dataType: "html",
            success: function (data) {
            }
        });

        if (selection == '1') {
            $('#country').empty();
            $('#country').html('<div class="selected_country">Şu anda işlemlerinizi Türkiye\'den yapıyorsunuz. <a onclick="changeCountry();" class="sub_link fake_link">Değiştir</a></div>');
            $('#call_center_number').html('444 30 20');
            $('#head_nav').css('background', 'url("/images/back_fromturkey.jpg") top right no-repeat');
            $('#selection2').val("1");
            getSelections();
            $.colorbox.close();
            $('#RLnk01').attr('href', '/basvuru/?LS=TURKIYE');
            $('#RLnk02').attr('href', '/basvuru/yerler.aspx?LS=TURKIYE');
            $('#RLnk03').attr('href', '/basvuru/belgeler.aspx?LS=TURKIYE');
        } else if (selection == '2') {
            $('#country').empty();
            $('#country').html('<div class="selected_country">Şu anda işlemlerinizi Yurtdışı Temsilciliklerimizden yapıyorsunuz. <a onclick="changeCountry();" class="sub_link fake_link">Değiştir</a></div>');
            $('#call_center_number').html('+90 444 30 20');
            $('#head_nav').css('background', 'url("/images/back_fromforeign.jpg") top right no-repeat');
            $('#selection2').val("2");
            getSelections();
            $.colorbox.close();
            $('#RLnk01').attr('href', '/basvuru/?LS=YDISI');
            $('#RLnk02').attr('href', '/basvuru/yerler.aspx?LS=YDISI');
            $('#RLnk03').attr('href', '/basvuru/belgeler.aspx?LS=YDISI');
        } else {
            $('#basvuru_selection').empty();
            $('#basvuru_selection').html('<div class="error_case">Geçici bir hata oluştu. Lütfen daha sonra tekrar deneyiniz.</div>');
        }
    } else {
        $('#basvuru_selection').empty();
        $('#basvuru_selection').html('<div class="error_case">Geçici bir hata oluştu. Lütfen daha sonra tekrar deneyiniz.</div>');
    }
}

function getSelections() {
    var s1 = $('#selection1').val();
    var s2 = $('#selection2').val();
    if (s1 == '1') {
        if (s2 == '1') {
            window.location = '/basvuru/?LS=TURKIYE';
        } else if (s2 == '2') {
            window.location = '/basvuru/?LS=YDISI';
        }
    } else if (s1 == '2') {
        window.location = '/basvuru/RandevuOnay.aspx?TcKn=' + $('#tckimlikno').val() + '&RefNo=' + $('#refno').val();
    } else if (s1 == '3') {
        window.location = '/basvuru/RandevuOnay.aspx?TcKn=' + $('#tckimlikno').val() + '&RefNo=' + $('#refno').val();
    } else if (s1 == '4') {
    }
}

function changeCountry() {
    $.colorbox({ href: '/basvuru/changecountry.html' });
}


function setUser() {
    if (!checkTCRefForm()) return;
    getSelections();
    $.colorbox.close();
}

function checkUser() {
    if (!checkTCRefForm()) return;
    $.colorbox.close();
    window.location = '/basvuru/RandevuOnay.aspx?TcKn=' + $('#tckimlikno').val() + '&RefNo=' + $('#refno').val();
}

function showContent(id) {

    $('#' + id).toggleClass('slide_off');
    $('#' + id).toggleClass('slide_on');
    $('#' + id + '_desc').toggle(500);

}

function showTitles(id) {

    $('#' + id).toggleClass('faq_header_active');
    $('#' + id).toggleClass('faq_header');
    $('#' + id + '_content').toggle(500);

}

function setQuestion() {
    var question = window.location.hash;
    if (question != null && question != '') {
        if (question.indexOf('x') == -1) return;
        var settedarea = question.substring(1, question.indexOf('x'));
        var settedquestion = question.substring(1);
        showTitles(settedarea);
        showContent(settedquestion);
    } else { }
}



function setAreaHeader(val) {
    var header = val;
    if (header == 'bilgi') {
        $('#talep_header').html('Talep Ettiğiniz Bilgi:');
    } else if (header == 'yorum') {
        $('#talep_header').html('Yorumunuz:');
    } else if (header == 'sikayet') {
        $('#talep_header').html('Şikayetiniz:');
    } else if (header == 'diger') {
        $('#talep_header').html('Talebinizi Detaylandırın:');
    }
}


function setWriteableArea() {
    $("#talep_text").animate({ "height": "500px" }, "slow");
}


function sendForm() {

    var Ad = $('#ad').val();
    var Soyad = $('#soyad').val();
    var TC = $('#tc').val();
    var Mail = $('#email').val();
    var Kaynak = $('#kaynak').val();
    var Talep = $('#talep').val();
    var TalepText = $('#talep_text').val();

    $.ajax({
        type: 'POST',
        url: '/ajax/sendMail.aspx',
        data: 'ad=' + Ad + '&soyad=' + Soyad + '&tc=' + TC + '&mail=' + Mail + '&kaynak=' + Kaynak + '&talep=' + Talep + '&taleptext=' + TalepText,
        cache: false,
        dataType: "html",
        error: function (code) {
            $.colorbox({ html: '<div id="error">Beklenmeyen bir hata oluştu.</div><div id="errordesc">Hata Kodu: ' + code + '</div>' });
        },
        success: function (data) {
            if (data == "1") {
                $.colorbox({ html: '<div id="success">' + Kaynak + ' kaynakli ' + Talep + ' talepli isteğiniz başarılı bir şekilde gönderilmiştir.</div>', width: '450px', height: '300px' });
            } else {
                $.colorbox({ html: '<div id="error">Talebiniz gönderilirken bir hata oluştu.</div><div id="errordesc">Hata: ' + data + '</div>', width: '450px', height: '300px' });
            }
        }
    });

}

function setRandevu(action, step, insts) {

    if (action == "TC") {
        if (step == "1") {
            if (insts == "1") {
                //TC Kimlik Var
                $('#kimlik_soru').animate({ "opacity": "show" }, 500, "", function () { $('#tc1').attr('disabled', 'disabled'); $('#tc2').attr('disabled', 'disabled'); });
            } else if (insts == '0') {
                //TC Kimlik Yok
                $('#no_uyari').animate({ "opacity": "show" }, 500, "", function () { $('#tc1').attr('disabled', 'disabled'); $('#tc2').attr('disabled', 'disabled'); });
            }
        } else if (step == '2') {
            if (insts == "1") {
                //TC Kimlik Yazili
                window.location.href = '/basvuru/RandevuTalebiSave1.aspx';
            } else if (insts == '0') {
                //TC Kimlik Yazili Degil
                $('#kimlik_uyari').animate({ "opacity": "show" }, 500, "", function () { $('#yazili1').attr('disabled', 'disabled'); $('#yazili2').attr('disabled', 'disabled'); });
            } else if (insts == '2') {
                //TC Kimlik Yazili Degil Kabul Etti
                $.colorbox({ html: '<div class="popup_content centered">T.C. Nüfus Cüzdanınızı pasaport başvurunuzdan önce yenilemeniz gerekmektedir. Kabul Ediyorum linkini işaretleyerek bu işlemi randevunuzdan önce tamamlamayı kabul etmiş bulunuyorsunuz. Devam etmek istediğinize emin misiniz?<div class="centered button_area"><img src="/images/btn_mini_evet.gif" onclick="setRandevu(\'TC\', \'2\', \'3\');" border="0" class="fake_link"/>&nbsp;&nbsp;&nbsp;&nbsp;<img src="/images/btn_mini_hayir.gif" onclick="setRandevu(\'TC\', \'2\', \'4\');" border="0" class="fake_link"/></div>', width: '450px', height: '250px' });
            } else if (insts == '3') {
                //TC Kimlik Yazili Degil Uyarisina Evet cevabini verdi
                //Kabul ettigine dair ajax request gonderilebilir
                $.colorbox.close();
                window.location.href = '/basvuru/RandevuTalebiSave1.aspx';
            } else if (insts == '4') {
                //TC Kimlik Yazili Degil Uyarisina Hayir cevabini verdi
                $.colorbox.close();
                window.location.href = '/basvuru/RandevuTalebi.aspx';
            }
        }
    } else if (action == 'Pasaport') {
        if (step == '1') {
            if (insts != '0') {
                $('#sehir_secimi').animate({ "opacity": "show" }, 500, "", function () { $('#pturu').attr('disabled', 'disabled'); });
            }
        } else if (step == '2') {
            if (insts != '0') {
                $('#kisi_sayisi').animate({ "opacity": "show" }, 500, "", function () { });
            }
        } else if (step == '3') {
            if (insts != '0') {
                $('#sehir_status').animate({ "opacity": "show" }, 500, "", function () { });
            }
        } else if (step == '4') {
            alert(insts);
            window.location.href = '/randevu/randevu3.html';
        }
    } else if (action == 'Kisisel') {
        window.location.href = '/randevu/randevu4.html';
    } else if (action == 'RTarih') {
        if (step == "1") {
            $('#randevu_merkez_status').animate({ "opacity": "show" }, 500, "", function () { $('#merkez_status_title').html('<font class="black">' + insts + '</font> tarihi için Ankara - Çankaya Randevuları'); $('#tarih').val(insts); });
        } else if (step == "2") {
            var tarih = $('#hfTarih').val();
            $.colorbox({ html : '<div class="popup_content centered">Randevu için ' + tarih + ' tarihi ' + insts + ' saatini seçmiş bulunuyorsunuz. <br/><br/> Bu tarih ve saati onaylıyor musunuz? <div class="centered button_area"><img src="/images/btn_mini_evet.gif" onclick="setRandevu(\'RTarih\', \'3\', \'' + insts + '\');" border="0" class="fake_link"/>&nbsp;&nbsp;&nbsp;&nbsp;<img src="/images/btn_mini_hayir.gif" onclick="setRandevu(\'RTarih\', \'4\', \'' + insts + '\');" border="0" class="fake_link"/></div>', width: '450px', height: '205px' });
        } else if (step == "3") {
            //Tarihi Kabul etti
            $.colorbox.close();
            var tarih = $('#hfTarih').val();
            //alert(tarih + ' - ' + insts);
            //window.location.href = '/randevu/randevu5.html';
            $('#hfAppState').val("AppSave");
            document.getElementById("bodyForm").submit();
        } else if (step == "4") {
            // Tarihi Kabul etmedi            
            $.colorbox.close();

        }
    } else if (action == 'Onay') {
        if (insts == '1') {
            $('#randevu').animate({ "opacity": "hide" }, 300, "", function () { });
            $('#randevu_success').animate({ "opacity": "show" }, 500, "", function () { });
        } else if (insts == '0') {
            $('#randevu').animate({ "opacity": "hide" }, 300, "", function () { });
            $('#randevu_cancelled').animate({ "opacity": "show" }, 500, "", function () { });
        }
    }
}

function setPrint() {
    window.print();
}

function cancelEvent(status) {
    if (status == '1') {
        $.colorbox.close();
        $('#randevu_bilgi').animate({ "opacity": "hide" }, 300, "", function () { });
        $('#randevu_cancelled').animate({ "opacity": "show" }, 500, "", function () { });
    } else if (status == '0') {
        $.colorbox.close();
    } else {
        $.colorbox({ html: '<div class="popup_content centered">Geçerli randevunuzu iptal etmek istediğinize emin misiniz?<div class="centered button_area"><img src="/images/btn_mini_evet.gif" onclick="cancelEvent(\'1\');" border="0" class="fake_link"/>&nbsp;&nbsp;&nbsp;&nbsp;<img src="/images/btn_mini_hayir.gif" onclick="cancelEvent(\'0\');" border="0" class="fake_link"/></div>', width: '450px', height: '160px' });
    }
}
function checkUserStatus() {
    if (!checkTCRefForm()) return;
    $.colorbox.close();
    window.location = '/basvuru/RandevuOnay.aspx?TcKn=' + $('#tckimlikno').val() + '&RefNo=' + $('#refno').val();
}

function showFailReasons() {
    $('#fail_reasons').animate({ "opacity": "show" }, 500, "", function () { });
}

function checkTCRefForm() {
    var t = $('#tckimlikno').val();
    var r = $('#refno').val();
    return /\d{11}/.test(t) && (/\d{4}/.test(r) || r.substring(0, 3) == 'REF');
}
