﻿////////////////////////////////////////////////////////////////
/// ChainBizz A/S Javascript Functions for Expert.dk Website ///
/// CopyRight 2010 - ChainBizz A/S                           ///
/// Written By: Lasse Rasch and Casper Simonsen              ///
/// Ver. 1.0                                                 ///
////////////////////////////////////////////////////////////////

// ** Frontpage Section ** //

var currentIndex = 0;
var curr;
var all;
var slider;
var sliding = 0;

function itemShown_handler(sender, args) {
    if (sliding != 1) {


        flowplayer("*").each(function() {
            if (this.isLoaded())
                try {
                this.stop();
            }
            catch (Error)
            { }
        });
             
        
        getCurrentIndex();
        $(".sliderTeaser_selected").removeClass('sliderTeaser_selected').addClass('sliderTeaser');
  
        var sliderbottom = document.getElementById("sliderTeaser" + currentIndex);
        sliderbottom.className = "sliderTeaser_selected";
    }

}
function getCurrentIndex() {
    if (slider == null)
        slider = $find('ctl05_FrontpageSlider');
    if (all == null)
        all = $find('ctl05_FrontpageSlider').get_items();

    curr = slider.get_currentItem();


    for (var i = 0; i < all.length; i++)
        if (curr == all[i])
        currentIndex = i;


}
function newItemIndex(gotoIndex) {

    var right = Telerik.Web.UI.RotatorScrollDirection.Right;
    var left = Telerik.Web.UI.RotatorScrollDirection.Left;
    sliding = 1;

    var index = currentIndex;
    var done = "false";
    var diff = index - gotoIndex;


    if (diff == -3 || diff == 1) {

        showNextItem(right);
    }
    else {

        if (diff == -1 || diff == 3) {
            showNextItem(left);
        }
        else {
            showNextItem(left);
            showNextItem(left);
        }

    }
    sliding = 0;
}
function showNextItem(direction) {
    if (slider == null)
        slider = $find('ctl05_FrontpageSlider');

    slider.showNext(direction);

}


// ** Image Section ** //

// Change Image Styling to Support Zoom functionality
function test() {

    var mainimage = document.getElementById('mainimage');

    if (mainimage != null) {
        var tests = document.getElementById('mainimage').rel;
        var test = document.getElementById('content_0_ProductContainer_container_ctl00_container_thumbnails');
        if (test != null && tests == '') {
            var value = document.getElementById('srcvalue');

            document.getElementById('mainimage').href = value.attributes.getNamedItem("Value").nodeValue.toString();
            document.getElementById('mainimage').rel = "background-color:#000; background-opacity:50";
            document.getElementById('mainimage').setAttribute("class", "MagicThumb");
            MagicThumb.refresh('mainimage');

            var stringID = value.attributes.getNamedItem("Value").nodeValue.toString();
            var stringID = stringID.replace('_large', '_small');
            var selected = document.getElementById(stringID);
            selected.className = "image_selected";

            var s = getElementsByName_iefix('a', 'thumblinks');
            for (var i = 0; i < s.length; i++) {
                s[i].href = s[i].href.replace('_small', '_large');
                s[i].rev = s[i].rev.replace('_small', '_large');
            }

        }
    }
}

function changeimage(url) {

    var s = getElementsByName_iefix('img', 'thumbsen');
    var curr = document.getElementById(url);
    for (var i = 0; i < s.length; i++) {
        s[i].setAttribute("class", "image");
    }

    curr.setAttribute("class", "image_selected");
}






// ** Menu Section ** //

function UpdateTop(panel, rotator) {


    var s = getElementsByName_iefix('div', 'subMenu');
    var t = getElementsByName_iefix('div', 'topMenu');
    var subPanel = panel.toString() + "SUB";
    var topPanel = panel.toString() + "TOP";



    for (var i = 0; i < s.length; i++) {

        if (document.getElementById(subPanel) == s[i]) {
            if (s[i].style.display == "")
                s[i].style.display = "none";
            else
                s[i].style.display = "";
        }
        else
            s[i].style.display = "none";

    }
    for (var i = 0; i < t.length - 1; i++) {

        if (document.getElementById(topPanel) == t[i]) {
            if (t[i].className == 'level1_selected')
                t[i].className = "level1";
            else
                t[i].className = "level1_selected";

        }
        else
            t[i].className = "level1";

    }

    if (document.getElementById(topPanel) == t[t.length - 1]) {
        if (t[t.length - 1].className == "offer_selected")
            t[t.length - 1].className = "offer";
        else
            t[t.length - 1].className = "offer_selected";
    }
    else {
        t[t.length - 1].className = "offer";
    }


    var oRotator = $find(rotator);

    oRotator.repaint();


}


// ** Product Display Section ** //
function changeView(CssStyle) {

    var div = document.getElementById('content_0_container');
    var date = new Date();
    var expirationDate = new Date();
    expirationDate.setTime(date.getTime() + (24 * 60 * 60 * 1000));
    var expirationString = "; expires=" + expirationDate.toGMTString() + "; path=/";


    document.cookie = 'liste=' + CssStyle + expirationString;
    document.cookie = 'url=' + location.href + expirationString;
    var viewmode1 = document.getElementById('viewmode1');
    var viewmode2 = document.getElementById('viewmode2');
    var viewmode3 = document.getElementById('viewmode3');

    var hidden = document.getElementById('content_0_hdnStyle');
    hidden.setAttribute("Value", CssStyle);
    div.className = CssStyle;
    var all = getElementsByName_iefix('div', 'View1')

    for (var i = 0; i < all.length; i++) {

        all[i].setAttribute("class", "type");
        all[i].setAttribute("className", "type");
    }
    if (CssStyle == 'list-01') {
        viewmode1.setAttribute("class", "type1_selected");
        viewmode2.setAttribute("class", "type2");
        viewmode3.setAttribute("class", "type3");
    }
    if (CssStyle == 'list-02') {
        viewmode1.setAttribute("class", "type1");
        viewmode2.setAttribute("class", "type2_selected");
        viewmode3.setAttribute("class", "type3");
    }
    if (CssStyle == 'list-03') {
        viewmode1.setAttribute("class", "type1");
        viewmode2.setAttribute("class", "type2");
        viewmode3.setAttribute("class", "type3_selected");
    }

    var viewmode1b = document.getElementById('viewmode1b');
    var viewmode2b = document.getElementById('viewmode2b');
    var viewmode3b = document.getElementById('viewmode3b');



    var all = getElementsByName_iefix('div', 'View1b')

    for (var i = 0; i < all.length; i++) {

        all[i].setAttribute("class", "type");
        all[i].setAttribute("className", "type");
    }
    if (CssStyle == 'list-01') {
        viewmode1b.setAttribute("class", "type1_selected");
        viewmode2b.setAttribute("class", "type2");
        viewmode3b.setAttribute("class", "type3");
    }
    if (CssStyle == 'list-02') {
        viewmode1b.setAttribute("class", "type1");
        viewmode2b.setAttribute("class", "type2_selected");
        viewmode3b.setAttribute("class", "type3");
    }
    if (CssStyle == 'list-03') {
        viewmode1b.setAttribute("class", "type1");
        viewmode2b.setAttribute("class", "type2");
        viewmode3b.setAttribute("class", "type3_selected");
    }



}


// ** Google Section ** //
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("storename")) {
        var name = document.getElementById("storename").attributes.getNamedItem("Value").nodeValue;
        var street = document.getElementById("storeaddress").attributes.getNamedItem("Value").nodeValue;
        var zip = document.getElementById("storezip").attributes.getNamedItem("Value").nodeValue;
        var city = document.getElementById("storecity").attributes.getNamedItem("Value").nodeValue;

        var mapdiv = "GoogleMaps";
        var map = new GMap2(document.getElementById(mapdiv));
        showAddress(name, street, zip, city, map);

        if (document.getElementById("storenames")) {

            var names = document.getElementById("storenames").getAttribute("value");
            var streets = document.getElementById("storeaddresss").getAttribute("value");
            var zips = document.getElementById("storezips").getAttribute("value");
            var citys = document.getElementById("storecitys").getAttribute("value");
            var lats = document.getElementById("storelats").getAttribute("value");
            var lngs = document.getElementById("storelongs").getAttribute("value");
            var links = document.getElementById("storelinks").getAttribute("value");

            n = names.toString().split(";");
            s = streets.toString().split(";");
            z = zips.toString().split(";");
            ci = citys.toString().split(";");
            la = lats.toString().split(";");
            ln = lngs.toString().split(";");
            li = links.toString().split(";");


            if (n.length > 1) {
                for (var i = 0; i < n.length - 1; i++) {
                    if (name != n[i]) {

                        showSmallMarker(n[i], s[i], z[i], ci[i], la[i], ln[i], li[i], map);
                    }
                }
            }

        }
    }


}


function CreateGooleMapWithRouteDesc() {

    var mapdiv = "GoogleMaps";
    var directionsdiv = "GoogleRoute";

    var end_street = document.getElementById("storeaddress").value;
    var end_zip = document.getElementById("storezip").value;
    var end_city = document.getElementById("storecity").value;

    var start_street = document.getElementById("start_street").value;
    var start_zip = document.getElementById("start_zip").value;
    var start_city = document.getElementById("start_city").value;


    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);
            }
            else
                alert("Bynavns feltet kan ikke indeholde postnummer");
        }
        else alert("Indtast venligst både Gade og By");
    }
}


// ** CheckOut Flow Section ** //


function otheraddress(sender) {


    var div = document.getElementById("content_0_otherdeliveryaddresssection")
    if (sender == true) {

        div.style.display = "";

    }
    else
        div.style.display = "none";

}

// Check if Terms Checkbox is selected.
function ensureChecked(source, args) {
    var cb = document.getElementById('cb_acceptterms');

    if (cb.checked == true) {
        args.IsValid = true;

    } else {
        args.IsValid = false;
    }

}





function validatetextbox(val, minchars, type, validatorid, defaultval) {

    var valid = CheckLength(val.value, minchars);


    /// Int Validation
    if (type == 1) {
        if (!isInteger(val.value)) {
            valid = false;
        }
    }
    /// E-Mail Validation
    else if (type == 2) {
        with (val.value) {
            apos = val.value.indexOf("@");
            dotpos = val.value.lastIndexOf(".");
            if (apos < 1 || dotpos - apos < 2) {
                valid = false;
            }
        }
    }

    if (val.value == defaultval) {
        valid = false;
    }

    if (valid == true) {
        validatorid.setAttribute("class", "ok");

    }
    else {
        validatorid.setAttribute("class", "");
    }

}






// ** Sandbox Section ** //

function mousein() { this.style.cursor = "hand"; }
function mouseout() { this.cursor = "default"; }


function getElementsByName_iefix(tag, name) {

    var elem = document.getElementsByTagName(tag);
    var arr = new Array();
    for (i = 0, iarr = 0; i < elem.length; i++) {
        att = elem[i].getAttribute("name");
        if (att == name) {
            arr[iarr] = elem[i];
            iarr++;
        }
    }
    return arr;
}


function CheckLength(val, minval) {


    if (val.length < minval)
        return false;
    else
        return true;
}
// Check if Value in Integer
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 validate(email, rating, savebtnid) {


    var savebtn = document.getElementById(document.getElementById(savebtnid).value);
    var tbEmail = document.getElementById(document.getElementById(email).value);
    var validRat = validate_rating('', document.getElementById(rating).value);

    var valid = true;
    if (validRat) {

        apos = tbEmail.value.indexOf("@");
        dotpos = tbEmail.value.lastIndexOf(".");
        if (apos < 1 || dotpos - apos < 2) {
            valid = false;
        }
        if (valid == true) {
            savebtn.disabled = '';
        }
        else {
            savebtn.disabled = 'disabled';
        }

    }
}

function validate_rating(field, ratingid) {
    var rating = document.getElementById(ratingid);
    if (rating.value <= 0) {
        return false;
    }
    else {
        return true;
    }
}







/// Change Div Styling. Used for Reviews 'Display All' mode.
function showAllText(item) {
    var itm = document.getElementById(item);
    if (itm.style.height == "100%") {
        itm.style.height = "";
    }
    else {
        itm.style.height = "100%";
    }
}

function Open(item) {
    var div = document.getElementById(item);
    div.style.display = "";

}
function Close(item) {
    var div = document.getElementById(item);
    div.style.display = "none";
    flowplayer("*").each(function() {
        if (this.isLoaded())
            try {
            this.stop();
        }
        catch (Error)
            { }
    });

}

// STORE PAGE SECTION:
function swap() {
    var test = document.getElementById('storeWrapper').style.display;

    if (test == "none") {
        expand();
    }
    else {
        collapse();
    }
}

function expand() {
    var div = document.getElementById('storeWrapper');
    div.style.display = "";
    var icon = document.getElementById('lblicon');
    icon.innerHTML = "-";

}
function collapse() {
    var div = document.getElementById('storeWrapper');
    div.style.display = "none";
    var icon = document.getElementById('lblicon');
    icon.innerHTML = "+";
}
function initialize() {
    collapse();

    if (querySt("showall") == "true")
        expand();

    if (querySt("change") == "true")
        expand();
    if (querySt("select") == "true")
        expand();
}

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

