﻿/*
Application Init    */

var Application = {}

Application.LanguageId = 8;

Application.Init = function (id) {
    Application.LanguageId = id;
    Navigation.CreateSubMenuDiv();
    setInterval(Application.KeepSessionAlive, 2 * 60 * 1000);
}

Application.KeepSessionAlive = function () {
    $.post(_siteRoot + "KeepAlive.ashx", null, function () { });
}


/* END */


/* 
Menu Related */

var Navigation = {}

Navigation.ReturnHome = function () {
    document.location = 'Default.aspx';
}

Navigation.MakeSubMenuAjax = function (text, ctrl, altText, offsetX, offsetY) {
    var data = $(text).html();
    if (data != null) {
        Tip(data, FIX, [ctrl, offsetX, offsetY], ABOVE, false, DELAY, 0, FADEIN, 0, FADEOUT, 0, STICKY, true, COPYCONTENT, true, CLICKCLOSE, true, BORDERWIDTH, 0, BGCOLOR, '', OPACITY, 100, PADDING, 0);

    }
}
Navigation.ChangeMenuClass = function (ctrl, text, order) {

    var left = $('#menuLeftDop');
    var right = $('#menuRightDop');
    var control = $("[client=cell_" + ctrl + "]")
    if (text == 'out') {

        left.removeClass('TopMenuLeftSelected');
        left.addClass('TopMenuLeft');

        right.removeClass('TopMenuRightSelected');
        right.addClass('TopMenuRight');

        control.removeClass('TopMenuItemSelected');
        control.addClass('TopMenuItem');

        // tt_HideInit();
    } else {

        left.removeClass('TopMenuLeft');
        left.addClass('TopMenuLeftSelected');

        right.removeClass('TopMenuRight');
        right.addClass('TopMenuRightSelected');

        control.removeClass('TopMenuItem');
        control.addClass('TopMenuItemSelected');
    }
}

Navigation.CreateSubMenuDiv = function () {
    var s = _siteRoot + 'Ajax/SubMenu.aspx';
    $.get(s, function (data) {
        var q = $('#menuSubContainer');

        q.empty();
        q.html(data);
    });
}

var Search = {};

Search.DoSearch = function () {
    document.location = 'SearchResults.aspx?query=' + $('#txtSearch').val();
}

/*
END */

/*
Shop Map    */

var Maps = {};

Maps.map = null;
Maps.directionsService = null;
Maps.directionsDisplay = null;

Maps.Init = function () {
    Maps.directionsDisplay = new google.maps.DirectionsRenderer();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
        zoom: 20,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    Maps.map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    Maps.directionsDisplay.setMap(Maps.map);
    if (document.getElementById("directionsPanel") != null) {
        Maps.directionsDisplay.setPanel(document.getElementById("directionsPanel"));
    }
}

Maps.AddShop = function (lng, lat, shopType) {
    if (Maps.map == null) {
        Maps.Init();
    }

    var loc = new google.maps.LatLng(lat, lng);
    Maps.map.center = loc;
    Maps.map.zoom = 15;

    var iconPT = '';

    if (shopType == '24') {
        iconPT = _appTheme + "/Images/Map/profileBaloonSmall.png";
    }
    else if (shopType == '35') {
        iconPT = _appTheme + "/Images/Map/PassionBaloonSmall.png";
    }
    else if (shopType == '36') {
        iconPT = _appTheme + "/Images/Map/redBaloonSmall.png";
    }
    else if (shopType == '37') {
        iconPT = _appTheme + "/Images/Map/redBaloonSmall.png";
    }
    else if (shopType == '38') {
        iconPT = _appTheme + "/Images/Map/redBaloonSmall.png";
    }

    marker = new google.maps.Marker({
        map: Maps.map,
        icon: iconPT,
        draggable: false,
        animation: google.maps.Animation.DROP,
        position: loc
    });
    google.maps.event.addListener(marker, 'click', toggleBounce);
}

Maps.AddShopForMap = function (lng, lat, shopName, shopOwner, shopCity, shopAdress, shopID, shopTel, shopImage, shopType) {
    if (Maps.map == null) {
        Maps.Init();
    }

    var loc = new google.maps.LatLng(lat, lng);
    Maps.map.center = loc;
    Maps.map.zoom = 8;

    var iconPT = '';
    var newShopName = '';

    if (shopType == 'ProfileBE') {
        iconPT = _appTheme + "Images/Map/profileBaloonSmall.png";
        //newShopName = 'Profile Tyrecenter BE ';
    }
    else if (shopType == 'PassionPneu') {
        iconPT = _appTheme + "Images/Map/PassionBaloonSmall.png";
        //newShopName = 'Passion Pneu ';
    }
    else if (shopType == 'BandendeConde') {
        iconPT = _appTheme + "Images/Map/redBaloonSmall.png";
        //newShopName = 'Banden de Condé';
    }
    else if (shopType == 'Independent') {
        iconPT = _appTheme + "Images/Map/redBaloonSmall.png";
        newShopName = '';
    }
    else if (shopType == 'Donkers') {
        iconPT = _appTheme + "Images/Map/redBaloonSmall.png";
        newShopName = '';
    }

    var beginTable = '<table cellpadding="3" cellspacing="0" border="0">';
    var endTable = '</table>';
    var beginTd = '<td valing="top">';
    var endTd = '</td>';
    var beginTr = '<tr>';
    var endTr = '</tr>';
    var finalTable = '';

    finalTable += beginTable + beginTr + beginTd;

    var img = "<img alt=" + shopName + " src='" + shopImage + "' width='90px' border='0' />";

    finalTable += img;
    finalTable += endTd;

    newShopName += ' ' + shopName;

    finalTable += beginTd + "<span>" + newShopName + "</span><br />" + shopOwner + "<br />" + shopAdress + "<br />" + shopCity + "<br />" + "Tel: " + shopTel + "<br />" + "<a href='DisplayShop.aspx?shopID=" + shopID + "'>Meer informatie</a>" + endTd + endTr + endTable;

    var infowindow = new google.maps.InfoWindow({
        content: finalTable
    });

    var marker = new google.maps.Marker({
        map: Maps.map,
        icon: iconPT,
        draggable: false,
        animation: google.maps.Animation.DROP,
        position: loc
    });

    google.maps.event.addListener(marker, 'click', function () {
        infowindow.open(Maps.map, marker);
    });
}

Maps.CalculateDistance = function (startLat, startLng, endLat, endLng) {
    Maps.directionsService = new google.maps.DirectionsService();
    var start = new google.maps.LatLng(startLng, startLat);
    var end = new google.maps.LatLng(endLng, endLat);
    var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    Maps.directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            Maps.directionsDisplay.setDirections(response);
        }
    });
}

function toggleBounce() {

    if (marker.getAnimation() != null) {
        marker.setAnimation(null);
    } else {
        marker.setAnimation(google.maps.Animation.BOUNCE);
    }
}

/* END */

/*
Shops   */

var Shops = {};

Shops.selectedShop = 0;
Shops.linkFromCity = '';
Shops.linkFromPostCode = '';
Shops.linkIsOk = false;

/*
Method that sheachs the nearest shop of the selected postcode
*/
Shops.SearchShop = function () {
    /*
    var counter = $('#txtPostcode').val().length;
    var postcode = $('#txtPostcode').val();
    if (counter == 4) {
    $('#SearchPostcodeDealer').css("display", "none");
    $('#imgSearchShop').css("display", "block");
    $.getJSON(_siteRoot + '/Ajax/Shop/ShopSearch.aspx?postcode=' + postcode, function (data) {
    if (data.ShopID != null) {
    $('#imgSearchShop').css("display", "none");
    $.getJSON(_siteRoot + 'Ajax/Shop/LoadSmallShop.aspx?shopID=' + data.ShopID, function (response) {
    $('#divNoShop').css('display', 'none');
    $('#divShop').css('display', 'block');
    $('#divShopContent').html(response.Response);
    });
    }
    });
    }
    */

    var postcode = $('#txtPostcode').val();
    document.location = 'ShopDirections.aspx?postcode=' + postcode;
}

Shops.ShopSearchByCity = function () {
    var city = $('#txtCity').val();
    document.location = 'ShopDirections.aspx?city=' + city;
    /*
    $('#SearchCity').css("display", "none");
    $('#imgSearchCity').css("display", "block");
    $.getJSON(_siteRoot + 'Ajax/Shop/ShopSearch.aspx?city=' + city, function (data) {
    if (data.ShopID != null) {
    $('#imgSearchCity').css("display", "none");
    $.getJSON(_siteRoot + 'Ajax/Shop/LoadSmallShop.aspx?shopID=' + data.ShopID, function (response) {
    $('#divNoShop').css('display', 'none');
    $('#divShop').css('display', 'block');
    $('#divShopContent').html(response.Response);
    });
    }
    });
    */
}

/*
Method that loads the small shop details control on the right side menu
*/

Shops.LoadShop = function (shopID) {
    $.getJSON(_siteRoot + 'Ajax/Shop/LoadSmallShop.aspx?shopID=' + shopID, function (response) {
        if (response.Response != null) {
            $('#divNoShop').css('display', 'none');
            $('#divShop').css('display', 'block');
            $('#divShopContent').html(response.Response);
        }
    });
}

Shops.ResetSelectedShopID = function () {
    $.getJSON(_siteRoot + 'Ajax/Shop/ShopSearch.aspx?mode=reset', function (data) {
        $('#divNoShop').css('display', 'block');
        $('#divShop').css('display', 'none');
        $('#SearchPostcodeDealer').css("display", "block");
    });
    $('#txtPostcode').val('');
    if (Application.LanguageId == 4) {
        $('#txtPostcode').Watermark('Your Postcode');
        $('#txtCity').Watermark('Your Location');
    } else if (Application.LanguageId == 5) {
        $('#txtPostcode').Watermark('Votre code postal');
        $('#txtCity').Watermark('Votre localité');
    } else {
        $('#txtPostcode').Watermark('Uw Postcode');
        $('#txtCity').Watermark('Uw Plaats');
    }
    $('#SearchCity').css("display", "block");
   
   
}

Shops.DisplayNormal = function () {
    $('#divNoShop').css('display', 'block');
    $('#divShop').css('display', 'none');
    $('#SearchPostcodeDealer').css("display", "block");
}

/*
Method that changes the lease company logo
*/
Shops.SearchLease = function () {
    var ddList = $('#ddlLeaseCompamy option:selected').val();

    if (ddList == 'Maak uw keuze' || ddList == 'Select lease company' || ddList == 'Selectionnez leasing') {
        if (Application.LanguageId == 8) {
            $('#imgleaseCompany').attr("src", _siteRoot + 'Styles/Images/RightSide/default-leasemaatschappij.jpg');
        }
        else if (Application.LanguageId == 5) {
            $('#imgleaseCompany').attr("src", _siteRoot + 'Styles/Images/RightSide/default_leasemaatschappij_fr.jpg');
        }
        else if (Application.LanguageId == 4) {
            $('#imgleaseCompany').attr("src", _siteRoot + 'Styles/Images/RightSide/default-leasemaatschappij_en.jpg');
        }
        $('#imgLoader').css('display', 'none');
        $('#imgleaseCompany').css('display', 'block');
    }
    else {
        $('#imgLoader').css('display', 'block');
        $('#imgleaseCompany').css('display', 'none');
        $.getJSON(_siteRoot + 'Ajax/Lease/Lease.aspx?leaseName=' + ddList, function (data) {
            if (data.Response != null) {
                $('#imgleaseCompany').attr("src", data.Response);
                $('#imgleaseCompany').css('display', 'block');
                $('#imgLoader').css('display', 'none');
            }
            else {
                $('#imgLoader').css('display', 'none');
            }
        });
        $('#imgLoader').css('display', 'none');
    }
}

/*
Method that changes the selected country to search on
*/

Shops.SetCountry = function () {
    var country = $('#ddlCountry option:selected').val();
    $.get(_siteRoot + 'Ajax/Shop/ShopSearch.aspx?country=' + country);
}

/*
Method that redirects to Display Route page
*/
Shops.GoToRoutePage = function () {
    var postcode = $("#txtPostcodeRoute").val();

    document.location = _siteRoot + 'DisplayRoute.aspx?uloc=' + postcode;
}

Shops.ShowRoute = function () {
    $("#divShopRoute").overlay({
        speed: 0,
        closeSpeed: 0,
        closeOnClick: false,
        load: false
    });
    $("#divShopRoute").overlay().load();
}

/*
End Shops   
*/

var Cars = {};

Cars.UwAuto = false;

Cars.SetUwAuto = function (value) {
    Cars.UwAuto = value;
}


function ValidateEmail(obj, args) {
    var txt = args.Value;
    var icon = $('#lblValidEmail');

    if (txt.length < 1) {
        args.IsValid = false;
        return;
    }

    var goodEmail = txt.match(/^(\w+[\-\.])*\w+@(\w+([\-]+[A-Za-z0-9]+)*\.)+[A-Za-z]+$/);
    if (goodEmail) {
        args.IsValid = true;
        icon.css('display', 'none');
        return;
    }
    else {
        args.IsValid = false;
        icon.css('display', 'block');
        return;
    }
}

function validPoscode(obj, args) {

    var text = $('#txtPostCode').val();

    var goodPostcode = text.match(/^[1-9]{1}[0-9]{3}$/);
    if (goodPostcode) {
        args.IsValid = true;
    }
    else {
        args.IsValid = false;
    }
    return;

}
