﻿$tabsShop = null;

// 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;
}

// 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 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") {
                img.src = _appTheme + "Images/Postcode/bifa.jpg";

                var js = "var json=" + result + ";";
                try {
                    var q = "(" + result + ")";
                    var json = eval(q);

                    $("#SmallShop_ShopName").html(json.ShopName);
                    // $("#SmallShop_ShopName").attr("href", _siteRoot + "DisplayShop.aspx?shopId=" + json.ShopId);
                    $("#SmallShop_OwnerName").html(json.OwnerName);
                    $("#SmallShop_Street").html(json.Street);
                    $("#SmallShop_City").html(json.City + json.Postcode);
                    $("#SmallShop_Phone").html(json.Phone);
                    $("#SmallShop_Email").html("Mail");
                    $("#SmallShop_Email").attr("href", 'mailto:' + json.Email);

                    $("#SmallShop_Image").css("background-image", "url('" + json.Image + "')");

                    $("#SmallShop_Loader").css("display", "none");
                    FillShopDetails(json.ShopId);
                } catch (e) {
                    //alert(e);
                }

            }
            else {
                img.src = _appTheme + "Images/Postcode/x.png";
            }
        });
    }
}


function FillShopData(text1, image) {
    var whiteSpace = false;

    var textBox = document.getElementById(text1);
    var img = document.getElementById(image);
    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 {
        img.src = _appTheme + "Images/Postcode/loading.gif";
        var post = textBox.value;
        $(".PostCodeSelector").attr("value", post);
        textBox.blur();
        //        PageMethods.GetCityFromPostcode(textBox.value, function(result) {
        //            try {
        //                var city = $get("ShopSearch_tbCities");
        //                if (city != null) {
        //                    var cityName = result.split('|')[0];
        //                    city.value = cityName;
        //                    city.innerText = cityName;
        //                }
        //            }
        //            catch (e) {
        //            }
        //        });
        PageMethods.GetShopByPostcode(textBox.value, function(result) {
            if (result != "null") {
                img.src = _appTheme + "Images/Postcode/bifa.jpg";

                var js = "var json=" + result + ";";
                try {
                    var q = "(" + result + ")";
                    var json = eval(q);

                    $("#SmallShop_ShopName").html(json.ShopName);
                    // $("#SmallShop_ShopName").attr("href", _siteRoot + "DisplayShop.aspx?shopId=" + json.ShopId);
                    $("#SmallShop_OwnerName").html(json.OwnerName);
                    $("#SmallShop_Street").html(json.Street);
                    $("#SmallShop_City").html(json.City + json.Postcode);
                    $("#SmallShop_Phone").html(json.Phone);
                    $("#SmallShop_Email").html("Mail");
                    $("#SmallShop_Email").attr("href", 'mailto:' + json.Email);

                    $("#SmallShop_Image").css("background-image", "url('" + json.Image + "')");
                    $("#SmallShop_Loader").css("display", "none");

                    OpenSmallShopDetails();
                    FillShopDetails(json.ShopId);
                } catch (e) {
                    //alert(e);
                }

            }
            else {
                img.src = _appTheme + "Images/Postcode/x.png";
            }
        });
    }
}


function FillShopDataForShopID(_shopId, _acc) {
    PageMethods.GetShopById(_shopId, function(result) {
        if (result != "null") {
            var js = "var json=" + result + ";";
            try {
                var q = "(" + result + ")";
                var json = eval(q);

                $("#SmallShop_ShopName").html(json.ShopName);
                //$("#SmallShop_ShopName").attr("href", _siteRoot + "DisplayShop.aspx?shopId=" + json.ShopId);
                $("#SmallShop_OwnerName").html(json.OwnerName);
                $("#SmallShop_Street").html(json.Street);
                $("#SmallShop_City").html(json.City + json.Postcode);
                $("#SmallShop_Phone").html(json.Phone);
                $("#SmallShop_Email").html("Mail");
                $("#SmallShop_Email").attr("href", 'mailto:' + json.Email);

                $("#SmallShop_Image").css("background-image", "url('" + json.Image + "')");

                if (_acc != null) {
                    _acc.set_SelectedIndex(1);
                }
                $("#SmallShop_Loader").css("display", "none");
                OpenSmallShopDetails();
                FillShopDetailsFromJSON(json);

                if ($tabsShop != null)
                    $tabsShop.tabs('select', 3);
            } catch (e) {
                //alert(e);
            }

        }
    });
}

function FillShopDetailsFromJSON(json) {
    var divServices = $('#ShopDetails_Services');

    divServices.empty();
    var servicesTable = "<table width='100%'>";
    for (var i = 0; i < json.Services.length; i += 3) {
        if (i % 2 == 0) {
            servicesTable += "<tr class='ShopSearchServicesAlternateRow'>";
        }
        else {
            servicesTable += "<tr class='ShopSearchServicesRow'>";
        }
        servicesTable += "<td width='33%' style='padding-left:10px'><a style='text-decoration:none' href='" + json.Services[i].URL + "'><span class='ShopSearchServicesText'>" + json.Services[i].ServiceName + "</span><a></td>";

        if (i + 1 < json.Services.length) {
            servicesTable += "<td width='33%' style='padding-left:10px'><a style='text-decoration:none' href='" + json.Services[i + 1].URL + "'><span class='ShopSearchServicesText'>" + json.Services[i + 1].ServiceName + "</span></a></td>";
        }
        if (i + 2 < json.Services.length) {
            servicesTable += "<td width='34%' style='padding-left:10px'><a style='text-decoration:none' href='" + json.Services[i + 2].URL + "'><span class='ShopSearchServicesText'>" + json.Services[i + 2].ServiceName + "</span></a></td>";
        }
        servicesTable += "</tr>";
    }
    servicesTable += "</table>";
    divServices.append(servicesTable);

    var divSchedule = $('#ShopDetails_Schedule');
    divSchedule.empty();
    var scheduleTable = "<table cellpadding='0' cellspacing='0' width='100%' style=''>";
    for (var i = 0; i < json.Schedule.length; i++) {

        if (i % 2 == 0) {
            scheduleTable += "<tr class='ShopSearchScheduleAlternateRow'><td style='padding-left:10px'>" + json.Schedule[i].Day + "</td><td>" + json.Schedule[i].Opened + "</td></tr>";
        }
        else {
            scheduleTable += "<tr class='ShopSearchScheduleRow'><td style='padding-left:10px'>" + json.Schedule[i].Day + "</td><td>" + json.Schedule[i].Opened + "</td></tr>";
        }
    }
    scheduleTable += "</table>";
    divSchedule.append(scheduleTable);


    var shopImage = $('#ShopDetails_ShopImage');
    shopImage.attr("src", json.ShopImage);

    var network = $('#ShopDetails_Network');
    network.empty();

    var networkTable = "<table width='100%' cellspacing='5px'>";
    for (var i = 0; i < json.Network.length; i += 3) {
        networkTable += "<tr>";
        networkTable += "<td width='33%' valign='top' onclick='FillShopDataForShopID(" + json.Network[i].ShopId + ",null)' style='background-color:#f0f0f0;cursor:pointer;'><table><tr><td valign='top'><img src='" + json.Network[i].ShopImage + "' height='60'/></td><td valign='top'><span class='ShopSearchNetworkName'>" + json.Network[i].ShopName + "</span><br><span class='ShopSearchNetworkAddress'>" + json.Network[i].Street + "<br>" + json.Network[i].City + " " + json.Network[i].Postcode + "</span></td></tr></table></td>";
        if (i + 1 < json.Network.length) {
            networkTable += "<td width='33%' valign='top' onclick='FillShopDataForShopID(" + json.Network[i + 1].ShopId + ",null)' style='background-color:#f0f0f0;cursor:pointer;'><table><tr><td valign='top'><img src='" + json.Network[i + 1].ShopImage + "' height='60'/></td><td valign='top'><span class='ShopSearchNetworkName'>" + json.Network[i + 1].ShopName + "</span><br><span class='ShopSearchNetworkAddress'>" + json.Network[i + 1].Street + "<br>" + json.Network[i + 1].City + " " + json.Network[i + 1].Postcode + "</span></td></tr></table></td>";
        }
        if (i + 2 < json.Network.length) {
            networkTable += "<td width='34%' valign='top' onclick='FillShopDataForShopID(" + json.Network[i + 2].ShopId + ",null)' style='background-color:#f0f0f0;cursor:pointer;'><table><tr><td valign='top'><img src='" + json.Network[i + 2].ShopImage + "' height='60'/></td><td valign='top'><span class='ShopSearchNetworkName'>" + json.Network[i + 2].ShopName + "</span><br><span class='ShopSearchNetworkAddress'>" + json.Network[i + 2].Street + " " + json.Network[i + 2].City + "<br>" + json.Network[i + 2].Postcode + "</span></td></tr></table></td>";
        }

        networkTable += "</tr>";

    }
    network.append(networkTable);

    var showMode = "table-cell";
    if ($.browser.msie) {
        showMode = "block";
    }

    try {
        if (json.Bovag == 'True') {
            $("#imgBovag").css("display", "block");
        }
        else {
            $("#imgBovag").css("display", "none");
        }
    }
    catch (e) {
    }

    try {
        // comment this to enable
        $("#tabshopOverons").css("display", "none");
        $("#tabshopOveronsSeparator").css("display", "none");

        // uncomment this to enable
        /*if (json.Details == "") {
        $("#tabshopOverons").css("display", "none");
        $("#tabshopOveronsSeparator").css("display", "none");
        }
        else {
        $("#tabshopOverons").css("display", showMode);
        $("#tabshopOveronsSeparator").css("display", showMode);
        var overons = $('#ShopDetails_Overons');
        overons.empty();
        overons.append(json.Details);
        }*/
    }
    catch (e) {
        // alert(e);
    }

    try {
        if (json.Network.length <= 1) {
            $("#tabshopKenteken").css("display", "none");
            $("#tabshopNetworkSeparator").css("display", "none");
        }
        else {
            $("#tabshopKenteken").css("display", showMode);
            $("#tabshopNetworkSeparator").css("display", showMode);
        }
        //if (json.Foto == "") 
        {
            $("#tabshopFoto").css("display", "none");
            $("#tabshopFotoSeparator").css("display", "none");
        }
        /*else {
            $("#tabshopFoto").css("display", showMode);
            $("#tabshopFotoSeparator").css("display", showMode);
            var foto = $('#ShopDetails_Foto');
            foto.empty();
            foto.append(json.Foto);
        }*/
    }
    catch (e) {
        //alert(e);
    }
    load(json.Location.Lng, json.Location.Lat);
    addShop(json.Location.Lng, json.Location.Lat, json.ShopName, json.Street, json.Postcode, '', 'true', 23);
}

function FillShopDetails(_shopId) {
    PageMethods.GetShopById(_shopId, function(result) {
        if (result != "null") {
            var js = "var json=" + result + ";";
            try {
                var q = "(" + result + ")";
                var json = eval(q);
                FillShopDetailsFromJSON(json);
            } catch (e) {
                //alert(e);
            }
        }

    });
}

function ShopSearch_Collapse() {
    $("#MainShopSearchPanel").slideUp(600);
    try {
        $("#MainTyreSearchInnerPanel").slideDown(600);
    }
    catch (e) {
        //alert(e);
    }
}

function ShopSearch_Expand() {
    $("#MainShopSearchPanel").slideDown(600);
    try {
        OpenAccordion();
        $("#MainTyreSearchInnerPanel").slideUp(600, function() {
        });
    }
    catch (e) {
        //alert(e);
    }
}

function ShopSearch_Init() {
    // only expand 
    var shopDetails = gup("shopdetails");

    if (shopDetails != "") {
        ShopSearch_Expand();
    }
    else {
        ShopSearch_Collapse();
    }

    if (gup("shopid") != "") {
        OpenAccordion();
    }

    $tabsShop = $("#tabsShop").tabs({ selected: 3, fx: { opacity: 'toggle', duration: 'fast'} });
    $('#tabshopOverons').click(function() { // bind click event to link
        $tabsShop.tabs('select', 4); // switch to third tab
        $get('tabshopOverons').className = 'ShopTabSelected';
        $get('tabshopBanden').className = 'ShopTab';
        $get('tabshopAuto').className = 'ShopTab';
        $get('tabshopKenteken').className = 'ShopTab';
        $get('tabshopFoto').className = 'ShopTab';
        return false;
    });
    $('#tabshopBanden').click(function() { // bind click event to link
        $tabsShop.tabs('select', 3); // switch to third tab
        $get('tabshopOverons').className = 'ShopTab';
        $get('tabshopBanden').className = 'ShopTabSelected';
        $get('tabshopAuto').className = 'ShopTab';
        $get('tabshopKenteken').className = 'ShopTab';
        $get('tabshopFoto').className = 'ShopTab';
        return false;
    });
    $('#tabshopAuto').click(function() { // bind click event to link
        $tabsShop.tabs('select', 2); // switch to third tab
        $get('tabshopOverons').className = 'ShopTab';
        $get('tabshopBanden').className = 'ShopTab';
        $get('tabshopAuto').className = 'ShopTabSelected';
        $get('tabshopKenteken').className = 'ShopTab';
        $get('tabshopFoto').className = 'ShopTab';
        return false;
    });
    $('#tabshopKenteken').click(function() { // bind click event to link
        $tabsShop.tabs('select', 1); // switch to third tab
        $get('tabshopOverons').className = 'ShopTab';
        $get('tabshopBanden').className = 'ShopTab';
        $get('tabshopAuto').className = 'ShopTab';
        $get('tabshopKenteken').className = 'ShopTabSelected';
        $get('tabshopFoto').className = 'ShopTab';
        return false;
    });

    $('#tabshopFoto').click(function() { // bind click event to link
        $tabsShop.tabs('select', 0); // switch to third tab
        $get('tabshopOverons').className = 'ShopTab';
        $get('tabshopBanden').className = 'ShopTab';
        $get('tabshopAuto').className = 'ShopTab';
        $get('tabshopKenteken').className = 'ShopTab';
        $get('tabshopFoto').className = 'ShopTabSelected';
        return false;
    });


    $('#tabshopTwitter').click(function() { // bind click event to link
        $tabsShop.tabs('select', 5); // switch to third tab
        $get('tabshopOverons').className = 'ShopTab';
        $get('tabshopBanden').className = 'ShopTab';
        $get('tabshopAuto').className = 'ShopTab';
        $get('tabshopKenteken').className = 'ShopTab';
        $get('tabshopFoto').className = 'ShopTab';
        $get('tabshopTwitter').className = 'ShopTabSelected';
        return false;
    });
}

function InitSite() {
    $(document).ready(function() {
        // setup watermark
        try {
            $('input[type=text], textarea').simpleWaterMark('watermark');
        }
        catch (e) {
        }
    });
}
