﻿function CreateGoogleStoreMap() {

    // Only show GoogleMap if StoreName object can be found.
    //Otherwise we assume that we are not on store page.

    // Get params from Browser Page
    if (document.getElementById("routeBox")) {

        jQuery("#AddressBox").hide();
        var street = readCookie("street");
        var zip = readCookie("zip");
        var city = readCookie("city");
        eraseCookie("street");
        eraseCookie("zip");
        eraseCookie("city");
        if (street != null) {
            jQuery("#start_street").val(street);
        }
        if (zip != null) {
            jQuery("#start_zip").val(zip);

        }
        if (city != null) {
            jQuery("#start_city").val(city);
        }


    }
    if (document.getElementById("storename")) {

        var name = jQuery("#storename").attr("value");
        var street = jQuery("#storeaddress").attr("value");
        var zip = jQuery("#storezip").attr("value");
        var city = jQuery("#storecity").attr("value");
        var phone = jQuery("#storephone").attr("value");
        var id = jQuery("#storeid").attr("value");

        var lat = jQuery("#storelat").attr("value");
        var lng = jQuery("#storelng").attr("value");


        var mapdiv = "GoogleMaps";

        showAddress(name, street, zip, city, phone, mapdiv, id, lat, lng);

    }
    if (document.getElementById("storenames")) {


        var name = jQuery("#storenames").attr("value");
        var street = jQuery("#storeaddresss").attr("value");
        var zip = jQuery("#storezips").attr("value");
        var city = jQuery("#storecitys").attr("value");
        var phone = jQuery("#storephones").attr("value");
        
        var ids = jQuery("#storeids").attr("value").toString().split(";");
        var lat = jQuery("#storelats").attr("value");
        var lng = jQuery("#storelngs").attr("value");
        var center = jQuery("#mapCenters").attr("value");
        var mapCenterId = jQuery("#mapCentersId").attr("value");
        var mapCenterLat = jQuery("#mapCentersLat").attr("value");
        var mapCenterLng = jQuery("#mapCentersLng").attr("value");
        var zoom = jQuery("#mapZoom").attr("value");
        var mapdiv = "GoogleMaps";


        n = name.toString().split(";");
        s = street.toString().split(";");
        z = zip.toString().split(";");
        ci = city.toString().split(";");
        p = phone.toString().split(";");
        lats = lat.toString().split(";");
        lngs = lng.toString().split(";");
       
       
        var map = new GMap2(document.getElementById(mapdiv));


        if (n.length > 1) {
            for (var i = 0; i < n.length - 1; i++) {
                showMultiAddress(n[i], s[i], z[i], ci[i], p[i], ids[i], map, lats[i], lngs[i]);

            }
        }
        if (center == "Danmark") {
            map.setUIToDefault();
            map.setCenter(new GLatLng(parseFloat(56.072035471800866), parseFloat(11.40380859375)), parseInt(zoom, 10));
        }
        else
            centerMap(center, zoom, map, mapCenterId, mapCenterLat, mapCenterLng);

    }
}
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function findNearestStore() {
    var street = jQuery("#MyPlaceStreet").val();
    var city = jQuery("#MyPlaceCity").val();
    var zip = jQuery("#MyPlaceZip").val();

    var lat = jQuery("#storelats").attr("value");
    var lng = jQuery("#storelngs").attr("value");
    var ids = jQuery("#storeids").attr("value");
    

    LookUpNearestStore(street, zip, city, lat, lng, ids);


}

function isInteger(val) {
    if (val == null) {
        return false;
    }

    if (val.length == 0) {
        return false;
    }

    for (var i = 0; i < val.length; i++) {
        var ch = val.charAt(i);
        if (i == 0 && ch == "-") {
            continue;
        }
        if (ch < "0" || ch > "9") {
            return false;
        }
    }
    return true
}
function CreateGooleMapWithRouteDesc() {

    var mapdiv = "GoogleMaps";
    var directionsdiv = "GoogleRoute";


    var end_street = jQuery("#storeaddress").attr("value");
    var end_zip = jQuery("#storezip").attr("value");
    var end_city = jQuery("#storecity").attr("value");

    var start_street = jQuery("#start_street").val();
    var start_zip = jQuery("#start_zip").val();
    var start_city = jQuery("#start_city").val();


    var btn = document.getElementsByName("unitDistance");
    var valid
    var unittype;
    for (var x = 0; x < btn.length; x++) {
        valid = btn[x].checked
        if (valid) {
            unittype = btn[x].value; break
        }
    }

    if (start_street != "") {
        if (start_city != "") {
            if (!isInteger(start_city)) {
                calcRoute(mapdiv, directionsdiv, unittype, start_street, start_zip, start_city, end_street, end_zip, end_city);
                jQuery("#printcountainer").show();
            }
            else
                alert("Bynavns feltet kan ikke indeholde postnummer");
        }
        else alert("Indtast venligst både Gade og By");
    }

}
function makePopups() {
    jQuery("[name='popupLink']").each(function () {

        jQuery(this).attr("target", "_blank");

    });

}

function roundCorners() {

    jQuery("img.img_left, img.img_right, img.img_small_left, img.img_small_right").each(function () {
        var classname = jQuery(this).attr('class');
        classname = "round" + classname;

        if (jQuery(this).parent().get(0).tagName == "A") {
            jQuery(this).parent().wrap("<div class='" + classname + "' name='jqrc'></div>");
        }
        else {
            jQuery(this).wrap("<div class='" + classname + "' name='jqrc'></div>");
        }
    });
    jQuery("div[name=jqrc]").prepend("<div class='cornerLT'>&#160;</div><div class='cornerLB'>&#160;</div><div class='cornerRT'>&#160;</div><div class='cornerRB'>&#160;</div>");

}
function activateButton() {

    jQuery("#<%= btnSearch.ClientID %>").removeAttr("disabled");

}
function deactivateButton(obj) {

    if (obj.value == '') {

        jQuery("#<%= btnSearch.ClientID %>").attr("disabled", "true");

    }
}

function lookupStore() {
    var postnr = jQuery("#txtPostnr").val();
    jQuery.ajax({
        type: "POST",
        url: "/layouts/TL/Ajax.aspx/LookupStore",
        data: "{postnr:'" + postnr + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            // Replace the div's content with the page method's return.
            jQuery("#storeView").html(msg.d);
        }
    });
}
function showStore() {
    var id = jQuery("#hdnId").val();
    if (id != '') {
        jQuery.ajax({
            type: "POST",
            url: "/layouts/TL/Ajax.aspx/presentChosenStore",
            data: "{id:'" + id + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                // Replace the div's content with the page method's return.
                jQuery("#storeView").html(msg.d);
            }
        });
    }
}
function showPopup() {
    if (jQuery("#popupPanel").length == 0)
        jQuery("#main").append("<div class='productInBasketPopup' id='popupPanel'><h2>Varen er lagt i kurven</h2></div>");
    jQuery("#popupPanel").css("top", jQuery(document).scrollTop() + (jQuery(window).height() / 2) - 20);
    jQuery("#popupPanel").show();
    setTimeout("hidePopup()", 2000);

}
function hidePopup() {
    jQuery("#popupPanel").hide();
}





