﻿if (document.getElementById) {
    window.alert = function (txt) {
        var rndId = (1 + Math.random()).toString(16).substring(2);
        div = $("<div id='" + rndId + "'>").html("Loading......");
        $("body").prepend(div);
        div.html(txt);
        div.dialog({
            title: 'Info...',
            modal: true,
            buttons: { "Okay": function () { $(this).dialog("close"); } }
        });
        div.dialog('open');
        setTimeout('$("#' + rndId + '").dialog("close");', 5000);
    };
}
$(document).ready(function () {
    $(".ac").click(function (e) {
        e.preventDefault();
        $(this).parent().next().next().animate({ height: 'show' });
        $(this).parent().next().next().next().css("display", "block");
        $(this).css("display", "none");
    });

    $(".kapa").click(function (e) {
        e.preventDefault();
        $(this).parent().prev().animate({ height: 'hide' });
        $(this).parent().prev().prev().prev().find(':first').css('display', 'block');
        $(this).parent().css("display", "none");
    });

    $('#tabs div.topdiv').hide();
    $('#tabs div.tab_content div:first').show();
    $('#tabs ul li:first').addClass('active');
    $('#tabs ul li a').click(function () {
        $('#tabs ul li').removeClass('active');
        $(this).parent().addClass('active');
        var currentTab = $(this).attr('href');
        $('#tabs div.topdiv').hide();
        $(currentTab).fadeIn();
        $(".customcombo").selectbox();
        return false;
    });
    $(".numeric").keydown(stopNonNumeric);
});
function stopNonNumeric(event) {
    // Allow only backspace and delete
    if (event.keyCode == 46 || event.keyCode == 8) {
        // let it happen, don't do anything
    }
    else {
        // Ensure that it is a number and stop the keypress
        if (!isNumber(event)) {
            event.preventDefault();
        }
    }
}

function isNumber(event) {
    if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105) ||
(event.keyCode == 8) || (event.keyCode == 9) || (event.keyCode == 12) || (event.keyCode == 27) ||
(event.keyCode == 37) || (event.keyCode == 39) || (event.keyCode == 46))
    { return true; }
    else
    { return false; }
}
$(document).ready(function() {
    $("#reg_click").click(function() {

        var bilgiler = $('form').find("input,textarea,select,hidden")
                .not("#__VIEWSTATE,#__EVENTVALIDATION").serialize();
        $.ajax({
            type: "GET",
            url: "register.aspx",
            data: bilgiler,
            success: function(ajaxCevap) {
                $.colorbox({
                    width: "600px",
                    height: "500px",
                    html: ajaxCevap,
                    onComplete: function() {
                        $(".numeric").keydown(stopNonNumeric);
                        $('#location').geo_autocomplete(new google.maps.Geocoder, {
                            mapkey: 'ABQIAAAAbnvDoAoYOSW2iqoXiGTpYBTIx7cuHpcaq3fYV4NM0BaZl8OxDxS9pQpgJkMv0RxjVl6cDGhDNERjaQ',
                            selectFirst: false,
                            minChars: 3,
                            cacheLength: 50,
                            width: 300,
                            scroll: true,
                            scrollHeight: 330
                        });

                        var container = $('div.container');

                        $("#kayit_formu").validate({
                            rules: {
                                firstname: "required",
                                lastname: "required",
                                password: {
                                    required: true,
                                    minlength: 6
                                },
                                confirmpassword: {
                                    required: true,
                                    equalTo: "#password"
                                },
                                email: {
                                    required: true,
                                    email: true
                                },
                                reg_year: {
                                    required: true,
                                    number: true
                                }
                            },
                            messages: {
                                firstname: "Please enter your firstname",
                                lastname: "Please enter your lastname",
                                password: {
                                    required: "Please provide a password",
                                    minlength: "Your password must be at least 8 characters long"
                                },
                                confirmpassword: {
                                    required: "",
                                    equalTo: "Please enter the same password as above"
                                },
                                email: {
                                    required: "Please provide an email address",
                                    email: "Please enter a valid email address"
                                }
                            },
                            errorElement: "div",
                            errorClass: "error_msg"
                        });

                        $("#form_button").click(function() {
                            var valid = $("#kayit_formu").valid();
                            if (valid) {
                                var sonuc = $.post('/ajax/kayit.aspx', $('#kayit_formu').serialize(), function(data) {
                                    $.colorbox.close();
                                    alert(data);
                                 });                                
                            }
                            return false;
                        });
                    }
                });
            }
        });
        return false;
    });
});

function replaceWithLocationMapper(divGelir) {
    //$(divGelir).html('<b>Location:</b><input class="reg_text acinput" type="text" name="location" id="location" value="" tabindex="12" /><br />');
    $.ajax({
        type: "GET",
        url: "/user/ajaxLocationBox.aspx",
        success: function (ajaxCevap) {
            $.colorbox({
                width: "600px",
                height: "200px",
                html: ajaxCevap,
                onComplete: function () {
                    $(".numeric").keydown(stopNonNumeric);
                    $('#location').geo_autocomplete(new google.maps.Geocoder, {
                        mapkey: 'ABQIAAAAbnvDoAoYOSW2iqoXiGTpYBTIx7cuHpcaq3fYV4NM0BaZl8OxDxS9pQpgJkMv0RxjVl6cDGhDNERjaQ',
                        selectFirst: false,
                        minChars: 3,
                        cacheLength: 50,
                        width: 300,
                        scroll: true,
                        scrollHeight: 330
                    });

                    var container = $('div.container');

                    $("#form_button").click(function () {
                        var valid = $("#kayit_formu").valid();
                        if (valid) {
                            var sonuc = $.post('/user/ajaxLocationBox.aspx', $('#kayit_formu').serialize(), function (data) {
                                $.colorbox.close();
                                alert(data);
                            });
                        }
                        return false;
                    });
                }
            });
        }
    });
}

function hataEkle(input, mesaj) {
    if (!input.hasClass('hatali')) {
        input.addClass('hatali');
        input.parent().parent().children('.sagtaraf').addClass('hatalimesaj').data('eski', input.parent().parent().children('.sagtaraf').text()).html(mesaj);
    }
    return true;
}

function hataKaldir(input) {
    if (input.hasClass('hatali')) {
        input.removeClass('hatali');
        input.parent().parent().children('.sagtaraf').removeClass('hatalimesaj').html(input.parent().parent().children('.sagtaraf').data('eski'));
    }
    return true;
}
function login() {
    $.ajax({
        type: "GET",
        url: "/ajaxPublicProxy.aspx?cmd=login",
        data: "kadi=" + $('#kadi').val() + "&parola=" + $('#parola').val(),
        success: function (msg) {
            if (msg == "true") {
                window.location = "/login.aspx?kadi=" + $('#kadi').val() + "&parola=" + $('#parola').val();
            }
            else
                alert("Hatalı kullanıcı adı veya e-posta girdiniz.");
        }
    });
}
