﻿$(document).ready(function() {
    $("#btnGonder").click(gonder);

    $("#slider").easySlider({
        auto: true,
        continuous: true
    });
    $("span#bekleyin").ajaxStart(function() {
        $(this).html("<img src='images/ajax-loader.gif' alt='' />");
    });
    $("span#bekleyin").ajaxComplete(function() {
        $(this).html("");
    });
    $("ul.hizmet > li").hover(geldi, gitti);
    iletisimBilgi();
});


//Hizmetler - Hover
function geldi() {
    var x = "#" + $(this).attr("id") + " div.HizmetDetay";
    //$(x).css("display", "block");
    $(x).stop(true, false);
    $(x).css("display", "block").animate({ "top": "-100px" }, 'normal', 'swing', function() { });
    hizmet_ortala(x);
}

function gitti() {
    var x = "#" + $(this).attr("id") + " div.HizmetDetay";
    $(x).stop(true, false);
    $(x).css("display", "none").animate({ "top": "-90px" }, 'fast', 'swing', function() { });
}

function hizmet_ortala(x) {
    $(x + " p").css("top", ((85 - $(x + " p").height()) / 2) + "px");
    $(x + " p").css("left", ((125 - $(x + " p").width()) / 2) + "px");
}

//Watermark
function iletisimBilgi() {
    //Adsoyad
    $("#txtAd").watermark("İsminiz");
    $("#txtAd").click(
			    function() {
			        $("#txtAd")[0].focus();
			    }
		    );
    //Eposta
    $("#txtEposta").watermark("E-posta adresiniz");
    $("#txtEposta").click(
			    function() {
			        $("#txtEposta")[0].focus();
			    }
		    );
    //Telefon
    $("#txtTel").watermark("Telefon numaranız");
    $("#txtTel").click(
			    function() {
			        $("#txtTel")[0].focus();
			    }
		    );
    //Mesaj
    $("#txtMesaj").watermark("Size nasıl yardımcı olabiliriz?");
    $("#txtMesaj").click(
			    function() {
			        $("#txtMesaj")[0].focus();
			    }
		    );
}

//Eposta kontrol
function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}

//Html Tag kontrol
function NoHTML(metin) {
    var reg = /<!*[^<>]*>/gi;
    return metin.replace(reg, "");
}

//Temizle
function temizle() {
    $("#txtAd").val("");
    $("#txtEposta").val("");
    $("#txtTel").val("");
    $("#txtMesaj").val("");
}

function SadeceNumara() {
    var key_code = window.event.keyCode;
    var oElement = window.event.srcElement;
    if (!window.event.shiftKey && !window.event.ctrlKey && !window.event.altKey) {
        if ((key_code > 47 && key_code < 58) ||
                          (key_code > 95 && key_code < 106)) {
            if (key_code > 95)
                key_code -= (95 - 47);
            oElement.value = oElement.value;
        } else if (key_code == 8) {
            oElement.value = oElement.value;
        } else if (key_code != 9) {
            event.returnValue = false;
        }
    }
}

function keypress(e) {

    if ([e.keyCode || e.which] == 8) //this is to allow backspace
        return true;
    if ([e.keyCode || e.which] < 48 || [e.keyCode || e.which] > 57)
        e.preventDefault ? e.preventDefault() : e.returnValue = false;
}

function AllowOnlyNumeric() {
    // Get the ASCII value of the key that the user entered
    var key = window.event.keyCode;

    // Verify if the key entered was a numeric character (0-9) or a decimal (.)
    if ((key > 47 && key < 58) || key == 46)
    // If it was, then allow the entry to continue
        return;
    else
    // If it was not, then dispose the key and continue with entry
        window.event.returnValue = null;
}


//Mesajı gönder
function gonder() {
    var isim = NoHTML($("#txtAd").val());
    if (isim == "İsminiz") {
        isim = "";
    }
    var posta = NoHTML($("#txtEposta").val());
    if (posta == "E-posta adresiniz") {
        posta = "";
    }
    var mesaj = NoHTML($("#txtMesaj").val());
    if (mesaj == "Size nasıl yardımcı olabiliriz?") {
        mesaj = "";
    }
    //            else {
    //                mesaj = NoHTML(mesaj);
    //            }
    var telefon = NoHTML($("#txtTel").val());
    if (telefon == "Telefon numaranız") {
        telefon = "";
    }
    var m;
    $("#sonuc").removeClass();
    $("#sonuc").html("");
    if (isim == "" || posta == "" || mesaj == "") {
        $("#sonuc").addClass("kirmizi");
        m = "Lütfen tüm alanları doldurun.";
        $("#sonuc").html(m);
    }
    else {
        if (isValidEmailAddress(posta)) {
            //Mesajı gönder
            $.ajax({
                type: "POST",
                url: "/mono.asmx/eposta",
                data: "{'isim':'" + isim + "','posta':'" + posta + "','telefon':'" + telefon + "','mesaj':'" + mesaj + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    var sonuc = msg.d;
                    if (sonuc == 1) {
                        m = "Lütfen tüm alanları doldurun.";
                        $("#sonuc").addClass("kirmizi");
                    }
                    else if (sonuc == 2) {
                        m = "Lütfen geçerli bir e-posta adresi yazın.";
                        $("#sonuc").addClass("kirmizi");
                        $("#txtEposta").focus();
                    }
                    else if (sonuc == 3) {
                        m = "Teşekkürler :)";
                        $("#sonuc").addClass("yesil");
                        temizle();
                        iletisimBilgi();
                    }
                    else {
                        m = "Opps... Bir hata oldu sanırım. Lütfen tekrar deneyiniz.";
                    }
                    $("#sonuc").html(m);
                }
            });
        }
        else {
            $("#sonuc").addClass("kirmizi");
            m = "Lütfen geçerli bir e-posta adresi yazın.";
            $("#sonuc").html(m);
        }
    }
}
