﻿// moves the focus on wnter key pressed from a textbox to a specified button
function MoveFocusToControl(e, btn) {
    var characterCode;
    if (e && e.which) // NN4 specific code
    {
        e = e;
        characterCode = e.which;
    } else {
        e = event
        characterCode = e.keyCode; // IE specific code
    } if (characterCode == 13) //// Enter key is 13
    {
        e.returnValue = false;
        e.cancelBubble = true;

        var obj2 = document.getElementById(btn);

        eval(unescape(obj2.href.replace("javascript:", "")));
        return false;
    } else return true;
}

function activateOnEnter(btn, e) {

    var key;
    if (window.event)
        key = window.event.keyCode;
    else
        key = e.which;
    if (key == '13') {
        var but = document.getElementById(btn);
        if (but != null) {
            but.focus();
            but.click();
            event.keyCode = 0;
        }
    }
}

$tabsShop = null;

$shopLat = 0;
$shopLng = 0;

// sets the background of a panel
function loadBgd(panel) {

    var td = document.getElementById(panel);
    var height = td.clientHeight;
    var width = td.clientWidth;
    td.style.backgroundImage = "url(" + _siteRoot + "/Helpers/ImageBuilder.aspx?type=RoundedCorners&amp;width=" + width + "&amp;height=" + height + "&amp;background-mode=solid&amp;corner=10&amp;inner-background=ffffff&amp;outer-background=f6f6f6&amp;border-size=0&amp;border-color=ffffff&amp;imageCodec=png)";
}

// return 'name' parameter from url
function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

function GetPageUrl() {
    var url = window.location.href;
    if (url.indexOf("?") > -1) {
        var s = url.split("?")[0];
        return s;
    }
    return url;
}

function MakeRoundRectangle(control, innerColor, outerColor, radius, borderSize, borderColor) {
}

function hasNumbers(t) {
    var regex = /\d/g;
    return regex.test(t);
}

// validates the zipcode and sets the favorite shop setting the image accordingly
function CustomPostcodeSearch_DoSearch(text1, image, cityTB) {
    var whiteSpace = false;

    var textBox = document.getElementById(text1);
    var img = document.getElementById(image);
    var city = document.getElementById(city);
    var letters = textBox.value.length + 1;
    if (textBox.value.substring(4, 5) == " ") {
        whiteSpace = true;
    }

    if (((letters <= 6) && (whiteSpace == false)) || ((letters <= 7) && (whiteSpace == true))) {
        /*if (letters <= 4) {
        if (hasNumbers(textBox.value)) {
        textBox.style.backgroundColor = "red";
        }
        else {
        textBox.style.backgroundColor = "white";
        }
        }*/
        textBox.focus();
    }
    else {
        if (img != null) {
            img.src = _appTheme + "Images/Postcode/loading.gif";
        }
        textBox.blur();
        var post = textBox.value;
        $(".PostCodeSelector").attr("value", post);
        PageMethods.GetCityFromPostcode(textBox.value, function(result) {
            if (city != null) {
                city.value = result;
            }
        });
        PageMethods.GetShopByPostcode(textBox.value, function(result) {
            if (result != "null") {


                var js = "var json=" + result + ";";
                try {
                    var q = "(" + result + ")";
                    var json = eval(q);

                    $("#tableShopDetails_ShopName").html(json.ShopName);
                    $("#tableShopDetails_ShopName").attr("href", _siteRoot + "DisplayShop.aspx?shopId=" + json.ShopId);
                    $("#tableShopDetails_ShopOwner").html(json.OwnerName);
                    $("#tableShopDetails_ShopDetails").html(json.Street + " " + json.Postcode + " " + json.City + " " + json.Phone);
                    $("#tableShopDetails_ShopEmail").html("Mail");
                    $("#tableShopDetails_ShopEmail").attr("href", 'mailto:' + json.Email);

                    $("#tableShopDetails_imgShopOwner").css("background-image", "url('" + json.Image + "')");
                    //$("#SmallShop_Image").attr("onmouseover", "TipSimpleImage('" + json.OwnerPictureLarge + "&width=200');");
                    $("#tableShopDetails_lnkImage").attr("onclick", "document.location='" + _siteRoot + "DisplayShop.aspx?shopId=" + json.ShopId + "';")
                    //$("#SmallShop_Image").attr("onmouseout", "UnTip();");
                    //$("#SmallShop_Loader").css("display", "none");

                    OpenSmallShopDetails();
                    FillShopDetails(json.ShopId);
                } catch (e) {
                    //alert(e);
                }

            }
            else {
                img.src = _appTheme + "Images/Postcode/x.jpg";
            }
        });
    }
}

function FillShopInformation(shop) {
    $("[id$='_trSearch']").css("display", "none");
    $("[id$='_trShop']").css("display", "block");
    $("[id$='_tdFavorite']").css("display", "block");
    
    var path = _siteRoot + 'Ajax/Ajax_Shop.aspx?shopID=' + shop.ShopID + '';
    $.getJSON(path, function(json) {
        try {
            eval(callBackFunction + "(json)");
        } catch (e) {
        }
    });
    try {
            $("#ShopName").html(shop.ShopName);
            $("#ShopName").attr("href", _siteRoot + "DisplayShop.aspx?shopId=" + shop.ShoID);
            $("#ShopOwner").html(shop.ShopOwner);
            $("#ShopDetails").html(shop.Adress + "<br /> Telephone: " + shop.Telephone);
            $("#ShopEmail").html("Mail");
            $("#ShopEmail").attr("href", 'mailto:' + shop.Email);

            $("#imgShopOwner").attr("src", shop.ShopOwnerImage);
            $("#imgShopOwner").attr("onclick", "document.location='" + _siteRoot + "DisplayShop.aspx?shopId=" + shop.ShopID + "';");
            
        } catch (e) {

        }
}

function FillFavouriteShop(shop) {
    $("[id$='_trSearch']").css("display", "none");
    $("[id$='_trShop']").css("display", "block");

    try {
        $("#ShopName").html(shop.ShopName);
        $("#ShopName").attr("href", _siteRoot + "DisplayShop.aspx?shopId=" + shop.ShoID);
        $("#ShopOwner").html(shop.ShopOwner);
        $("#ShopDetails").html(shop.Adress + "<br /> Telephone: " + shop.Telephone);
        $("#ShopEmail").html("Mail");
        $("#ShopEmail").attr("href", 'mailto:' + shop.Email);

        $("#imgShopOwner").attr("src", shop.ShopOwnerImage);
        $("#imgShopOwner").attr("onclick", "document.location='" + _siteRoot + "DisplayShop.aspx?shopId=" + shop.ShopID + "';");

    } catch (e) {

    }
}

function DeleteFavouriteShop(ShopID) {
    $("[id$='_trSearch']").css("display", "block");
    $("[id$='_trShop']").css("display", "none");

    var path = _siteRoot + 'Ajax/Ajax_Shop.aspx?shopID=' + ShopID + '';
    $.getJSON(path, function(json) {
        try {
            eval(callBackFunction + "(json)");
        } catch (e) {
        }
    });
}

function InitSite() {
    $(document).ready(function() {
        // setup watermark
        try {
            $('input[type=text], textarea').simpleWaterMark('watermark');
        }
        catch (e) {
        }
    });
}

