﻿var map, ilceControl, mahalleControl, eczArray, markersArray, infowindow, mc, nobetciSayisi;

function ilkGorunum() {
    clearOverlays();
    $('#uyari').fadeOut(500);	
    firstLocation = new google.maps.LatLng(39.900282777, 32.7626085346);
    map.setCenter(firstLocation);
    map.setZoom(11);
}
function clearOverlays() {
    if (markersArray) {
        var l = markersArray.length;
        for (i = 0; i < markersArray.length; i++) {
            markersArray[i].setMap(null);
        }
        //for (i = 0; i < l; i++) { markersArray.splice(0, 1); }
        markersArray.length = 0;
    }
    try { mc.clearMarkers(); }
    catch (err) { }
}
function nobetciSayisi(s, e) {
    var bulunanNobetciSayisi = eczArray.length -1;
    var mesaj = nobetciSayisi + " nöbetçi eczaneden " + bulunanNobetciSayisi + " tanesinin harita kaydı bulundu!";
    var uyariDiv = document.getElementById("uyari");
    uyariDiv.innerHTML = "<span>"+mesaj+"</span>";
    $('#uyari').fadeIn(300);
}
function haritayaEkle(s, e) {
    try { popup.HideWindow(popup.GetWindow(3)); }
    catch (err) { }
    var eczAdi="";
    var adres = "";
    var eposta = "";
    eczAdi = eczaneAdText.GetText();
    if (eczAdi == "") { alert("Eczane adını giriniz!"); return; }
    adres = document.getElementById("adresTextArea").value;
    if (adres == "") { alert("İletişim bilgilerini giriniz!"); return; }
    eposta = txtEposta.GetText();
    if (!validate(eposta)) { return; }
    cursor_wait();
    cbMailGonder.PerformCallback(eczAdi + "%" + adres + "%" + eposta + "%EczaneEkleme");   
}
function mailGonder(s, e) {
    try { popup.HideWindow(popup.GetWindow(2)); }
    catch (err) { }
    var sender = "";
    sender = adSoyadText.GetText();
    if (sender == "") { alert("Adınızı giriniz!"); return; }
    var senderAdress = "";
    senderAdress = emailText.GetText();
    if (!validate(senderAdress)) { return; }
    var mail = "";
    mail = document.getElementById("mesajTextArea").value;
    if (mail == "") { alert("Mesajınızı giriniz!"); return; }
    cursor_wait();
    cbMailGonder.PerformCallback(sender + "%" + senderAdress + "%" + mail);
}
function mailSonuc(s, e) {
    cursor_clear();
    var mesaj;
    var sonuc = e.result;
    if (sonuc == "1") {mesaj = "Mailiniz gönderildi, en kısa sürede değerlendirilip sizinle iletişime geçilecektir!"; }
    else {mesaj = "Mail gönderilemedi !"; }
    var uyariDiv = document.getElementById("uyari");
    uyariDiv.innerHTML = "<span>" + mesaj + "</span>";
    $('#uyari').fadeIn(10);
    $('#uyari').animate({ opacity: 1.0 }, 3000);
    $('#uyari').fadeOut('slow', function () {
      $(this).hide();
  });
  try { popup.HideWindow(popup.GetWindow(3)); }
  catch (err) { }
  try { popup.HideWindow(popup.GetWindow(2)); }
  catch (err) { }
}
function resultParcala(s,e) {
    var eczID,eczAdi,eczLon,eczLat,eczMah,eczIlce,nobetciMi;
    var resultString = e.result;
    var eczaneler = resultString.split("+++");
    eczArray = new Array(eczaneler.length);
    for (var i = 0; i < eczaneler.length; i++) {
        var bos = new Array(4);
        bos[0] = "";
        bos[1] = "";
        bos[2] = "";
        bos[3] = "";
        bos[4] = "";
        bos[5] = "";
        bos[6] = "";
        eczArray[i] = bos;
    }
    for (var i = 0; i < eczaneler.length; i++) {
        if(eczaneler[i] != "")
        {
            var eczaneBilgisi = eczaneler[i].split("$$");
            eczID = eczaneBilgisi[0];
            eczAdi = eczaneBilgisi[1];
            eczMah = eczaneBilgisi[2];
            eczIlce = eczaneBilgisi[3];
            eczLon = eczaneBilgisi[4];
            eczLon = eczLon.replace(",", ".");
            eczLat = eczaneBilgisi[5];
            eczLat = eczLat.replace(",", ".");
            nobetciMi = eczaneBilgisi[6].split("---")[1];
            nobetciSayisi = eczaneBilgisi[6].split("---")[0];
            eczArray[i][0] = eczID;
            eczArray[i][1] = eczAdi
            eczArray[i][2] = eczMah;
            eczArray[i][3] = eczIlce;
            eczArray[i][4] = eczLon
            eczArray[i][5] = eczLat;
            eczArray[i][6] = nobetciMi;
        }  
    }
    addMarkers();
}

function addMarkers() {
    for (var i = 0; i < eczArray.length; i++) {
        var myLatlng = new google.maps.LatLng(eczArray[i][5], eczArray[i][4]);
        var contentString = "<h2 style=\"color:#d0070f\">" + eczArray[i][1] + "</h2></br><span style=\"font-weight:bold\">" + eczArray[i][2] + "</br>" + eczArray[i][3] + "</span>";
        infowindow = new google.maps.InfoWindow({
            content: contentString
        });
        var img;
        if (eczArray[i][6] == "1") { img = 'images/nobetciikon.png'; }
        else { img = 'images/eczane.png'; }
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            icon: img,
            animation: google.maps.Animation.DROP,
            html: contentString
        });
        google.maps.event.addListener(marker, 'mouseover', function () {
            infowindow.setContent(this.html);
            infowindow.open(map, this);
        });
        markersArray.push(marker);
    }
    cluster();
}

function cluster() {
    var mcOptions = { gridSize: 50, maxZoom: 15 };
    mc = new MarkerClusterer(map, markersArray, mcOptions);
}

function init() {
    markersArray = [];
    infowindow = null;
    var myLatlng = new google.maps.LatLng(39.900282777, 32.7626085346);
    var myOptions = {
        zoom: 11,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        panControl: true,
        panControlOptions: {
            position: google.maps.ControlPosition.TOP_RIGHT
        },
        zoomControl: true,
        zoomControlOptions: {
            position: google.maps.ControlPosition.TOP_RIGHT
        }
    }
    map = new google.maps.Map(document.getElementById("mapDiv"), myOptions);
    RequestAjax('ilce.xml', '', loadIlceler);
    RequestAjax('mah.xml', '', loadMahalle);
}

function loadIlceler(XmlDoc) {
    ilceControl = ilceCombo2;
    mahalleControl = mahalleCombo2;
    ilceControl.AddItem("İlçe Seçiniz");
    mahalleControl.ClearItems();
    var names = XmlDoc.documentElement.getElementsByTagName("n");
    for (var i = 0; i < names.length; i++) {
        if (names[i].getAttribute("p") != "16") continue; //Bu ilçe seçili ilin ilçesi mi?
        var val = names[i].getAttribute("la") + ";" + names[i].getAttribute("lo") + ";" + names[i].getAttribute("id");
        var txt = names[i].getAttribute("n");
        ilceControl.AddItem(txt,val);
    }
}

/*İlçeler değiştiğimde loadMahalle fonksiyonunu tetikler.*/
function ilceComboSelChanged() {
    var selVal = ilceControl.GetValue().split(';');
    map.panTo(new google.maps.LatLng(parseFloat(selVal[0]), parseFloat(selVal[1])));
    map.setZoom(13);
    RequestAjax('mah.xml', '', loadMahalle);
}

/*Mahalle dropdownlistinin doldurulmasını sağlar.*/
function loadMahalle(XmlDoc) {
    mahalleControl = mahalleCombo2;
    ilceControl = ilceCombo2;
    mahalleControl.ClearItems();
    mahalleControl.AddItem("Mahalle Seçiniz","-1");
    var names = XmlDoc.documentElement.getElementsByTagName("n");
    for (var i = 0; i < names.length; i++) {
        if (ilceControl.selectedIndex <= 0) continue;
        var val = ilceControl.GetValue();
        if (val != null){
            if (names[i].getAttribute("p") != val.split(';')[2]) continue;
        }    
        var vl =  names[i].getAttribute("la") + ";" + names[i].getAttribute("lo") + ";" + names[i].getAttribute("id");
        var txt = names[i].getAttribute("n");
        mahalleControl.AddItem(txt,vl);
    }
}

/*Mahalle sınırlarına pan eder.*/
function mahComboSelChanged(s,e) {
    var selVal = mahalleControl.GetValue().split(';');
    map.panTo(new google.maps.LatLng(parseFloat(selVal[0]), parseFloat(selVal[1])));
    map.setZoom(15);
}
function RequestAjax(url, params, callBack) {
    var httpReq = new XMLHttpRequest();
    httpReq.open("GET", url, true);
    httpReq.onreadystatechange = function () {
        if (httpReq.readyState == 4 && httpReq.status == 200) {
            callBack(httpReq.responseXML);
        }
    }
    httpReq.send(params);
}

function setPrintUrl() {
    var urlVal = document.getElementById("url");
    var printUrl = urlVal.innerHTML;

    var index = printUrl.search("Default");
    if (index != -1) {

        printUrl = printUrl.replace("Default", "print");
    }
    else {
        var ss = printUrl.split("?");
        if (ss.length > 1) {
            printUrl = ss[0] + "print.aspx?" + ss[1];
        }
        else {
            printUrl += "print.aspx";
        }

    }
    var a = document.getElementById("printHref");
    a.href = printUrl;
}

function validate(email) {
    if (email == "") { alert("Mail adresinizi giriniz!"); return false; }
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (reg.test(email) == false) {
        alert('Geçersiz eposta !');
        return false;
    }
    return true;
}

function cursor_wait() {
    document.body.style.cursor = 'wait';
}

// Returns the cursor to the default pointer
function cursor_clear() {
    document.body.style.cursor = 'default';
}
