﻿/*** menu ***/
        /*
        var menuCurrentlySelected;

        function menuSelectItem(n) {
            if (n != menuCurrentlySelected) {
                $("#menuitem" + n + "_on").animate({ top: "0px" }, 500, "easeOutExpo");
                $("#menuitem" + menuCurrentlySelected + "_on").animate({ top: "48px" }, 500, "easeOutExpo");
                menuCurrentlySelected = n;
            }
        }

        $(document).ready(function() {
            $("#menuitem1_off").mouseover(function() { menuSelectItem(1); });
            $("#menuitem2_off").mouseover(function() { menuSelectItem(2); });
        });
        */



/*** dropdownlist ***/
        $.fn.selectedValue = function(value) {
            this.each(
                    function() {
                        if (this.nodeName.toLowerCase() != "select") return;

                        var optionsLength = this.options.length;

                        for (var i = 0; i < optionsLength; i++) {
                            if (this.options[i].value == value) this.options[i].selected = true;
                        }
                    }
                )
            return this;
        }



/*** pager ***/
        function updatePager(page, pages, posts) {
            $("#pager #currentPage").html(page);
            $("#pager #totalPages").html(pages);
            $("#pager #posts").html(posts);

            // clear
            $("#pager #next").unbind("click");
            $("#pager #last").unbind("click");
            $("#pager #first").unbind("click");
            $("#pager #prev").unbind("click");

            if (page < pages) {
                $("#pager #next").bind("click", function(e) { submitSearch(new Number(page) + 1) });
                $("#pager #last").bind("click", function(e) { submitSearch(pages) });
                if (!$("#pager #next").hasClass("sel")) $("#pager #next").addClass("sel");
                if (!$("#pager #last").hasClass("sel")) $("#pager #last").addClass("sel");
            } else {
                $("#pager #next").removeClass("sel");
                $("#pager #last").removeClass("sel");
            }

            if (page > 1) {
                $("#pager #first").bind("click", function(e) { submitSearch(1) });
                $("#pager #prev").bind("click", function(e) { submitSearch(new Number(page) - 1) });
                if (!$("#pager #first").hasClass("sel")) $("#pager #first").addClass("sel");
                if (!$("#pager #prev").hasClass("sel")) $("#pager #prev").addClass("sel");
            } else {
                $("#pager #first").removeClass("sel");
                $("#pager #prev").removeClass("sel");
            }
        }



/*** loading ***/
        function loadingShow(s, objText, objLoader) {
            if (s != undefined) {
                objText.innerHTML = s;
            } else {
                objText.innerHTML = "Laddar...";
            }
            objLoader.style.display = 'block';
        }

        function loadingHide(objLoader) {
            objLoader.style.display = 'none';
        }



/*** vehicle ***/
        function getModelNamesByBrandForDropdown1(brandDropId, modelDropId, sizeDropId) {
            var brandId = $(brandDropId).val();
            getModelNamesByBrandForDropdown2(brandId, modelDropId, sizeDropId);
        }
        function getModelNamesByBrandForDropdown2(brandId, modelDropId, sizeDropId) {
            loadingShow("Hämtar bilmodeller...",document.getElementById('loadingtxt'),document.getElementById('loading'));
            $(modelDropId).html('<option value="-1">Laddar...</option>');
            $(sizeDropId).html('<option value="-1">Laddar...</option>');
            $(modelDropId).addSelectOptions("../Common/VehicleDataHandler.ashx?action=vehicleModelsByBrandForDropdown&brandId=" + brandId, function() {
                loadingHide(document.getElementById('loading'));
                getModelInchesForDropdown($(modelDropId).val(), sizeDropId);
            });
        }

        function getModelInchesForDropdown(modelId, sizeDropId) {
            loadingShow("Hämtar storlekar...",document.getElementById('loadingtxt'),document.getElementById('loading'));
            $(sizeDropId).html('<option value="-1">Laddar...</option>');
            $(sizeDropId).addSelectOptions("../Common/VehicleDataHandler.ashx?action=vehicleModelInchesForDropdown&modelId=" + modelId, function() {
                $(sizeDropId).prepend('<option value="0" selected="selected">[ALLA]</option>');
                loadingHide(document.getElementById('loading'));
            });
        }
        
        
        
/*** info page ***/
        function loadText(path, n) {
            loadingShow("Hämtar texter...",document.getElementById('loadingtxt'),document.getElementById('loading'));
            $.get("../Static/" + path + "_" + n + ".html", function(result) {
                $("#ctl00_cphContent_txt" + n).html(result);
                var curHeight = $("#ctl00_cphContent_txt" + n).outerHeight({ margin: true });
                if (curHeight > maxHeight) {
                    $("#txtplhdiv").css("height", curHeight + "px");
                    maxHeight = curHeight;
                }
                loadingHide(document.getElementById('loading'));
            });
        }

        function showText(n) {
            if (n != currentlyVisible) {
                $("#ctl00_cphContent_lnk" + n).addClass("sel");
                $("#ctl00_cphContent_lnk" + currentlyVisible).removeClass("sel");
                $("#ctl00_cphContent_txts").animate({ left: -(n * 937) + "px" }, 1000, "easeOutExpo");
                currentlyVisible = n;
            }
        }
        
      // Validate decimal input value in the form xxx.
      // Param minValue indicates that the given value cannot be small that the minValue. Can be null to ignore
      // Param maxValue indicates that the given value cannot be larger that the maxValue. Can be null to ignore
      function validateDecimal(value, minValue, maxValue)
      {
         var valid = true;
         if (value == '')
            valid = false;
         else
         {
            var ValidChars = '-0123456789';
            var Char;
            
            // Remove all special characters like linefeed, spaces, non-breaking spaces, etc.
            var tempValue = value.replace('\u00a0', '').replace(/\s/g, '');
               
            for (var i = 0; i < tempValue.length && valid == true; i++)
            {
               Char = tempValue.charAt(i);
               if (ValidChars.indexOf(Char) == -1)
                  valid = false;
            }
         }
         
         if (valid == true && (minValue != null || maxValue != null))
         {
            var num = new Number(value);
            if (minValue != null && num.valueOf() < minValue)
               valid = false;
               
            if (maxValue != null && num.valueOf() > maxValue)
               valid = false;
         }
         
         return valid;
      }
      
      // Validate validateSocialSecurityNumber.
      // Should be in one of the formats:
      // YYMMDD-NNNN
      // YYYYMMDD-NNNN
      // YYMMDDNNNN
      // YYYYMMDDNNNN
      function validateSocialSecurityNumber(value)
      {
         var valid = true;
         if (value == '')
            valid = false;
         else
         {
            var ValidChars = '-0123456789';
            var Char;
            
            for (var i = 0; i < value.length && valid == true; i++)
            {
               Char = value.charAt(i);
               if (ValidChars.indexOf(Char) == -1)
                  valid = false;
            }
         }
         
         if (valid == true)
         {
            var len = value.length;
            if (len == 11 && value.indexOf('-') != 6)
                valid = false;
            else if (len == 13 && value.indexOf('-') != 8)
                valid = false;
            else if ((len == 10 || len == 12) && value.indexOf('-') > -1)
                valid = false;
         }
         
         return valid;
     }

     // Validates organization number.
     // Should be in one of the formats:
     // dddddd-dddd
     // dddddddddd
     function validateOrganizationNumber(orgNo) {
         //var orgNoRegExp = new RegExp("(\d{6}\-\d{4})|(\d{10})");

         if (orgNo == null)
             return false;
             
         var valid = /\d{6}\-\d{4}|\d{10}/.test(orgNo);
         if (valid)
         {
            var len = orgNo.length;
            if ((len == 10 || len == 11) && (orgNo.charAt(2) == '0' || orgNo.charAt(2) == '1'))
                valid = false;
            else if ((len == 12 || len == 13) && (orgNo.charAt(4) == '0' || orgNo.charAt(4) == '1'))
                valid = false;
         }
         
         return valid;
     }
     
     function validateZipCode(zipCode)
     {
        var valid = true;
        if (zipCode == '')
            valid = false;
        else if (zipCode.length != 5 && zipCode.length != 6)
            valid = false;
        else
        {
            var ValidChars = ' 0123456789';
            var Char;
            
            for (var i = 0; i < zipCode.length && valid == true; i++)
            {
               Char = zipCode.charAt(i);
               if (ValidChars.indexOf(Char) == -1)
                  valid = false;
            }
        }
        
        if (valid == true)
        {
            var len = zipCode.length;
            if (zipCode.length == 6 && zipCode.indexOf(' ') != 3)
                valid = false;
        }
        
        return valid;
     }

     function numeric(x)
     {
         x = ("" + x).replace(/,/g, ".");
         return x !== "" && !isNaN(x);
     }

     function positive(x) {
         return parseFloat(x) >= 0;
     };

     function setTransparencyBackground(elemID, topLevel)
     {
         var obj = document.getElementById(elemID);
         if (typeof (obj) == "undefined") {
             return false;
         }

         var width = -1;
         var height = -1;

         if (topLevel.innerHeight) {/*all except ie*/
             width = topLevel.innerWidth;
             height = topLevel.innerHeight;
         }
         else if (document.documentElement && document.documentElement.clientHeight) {/*ie strict*/
             width = document.documentElement.clientWidth;
             height = document.documentElement.clientHeight;
         }
         else if (document.body) {/*last trial*/
             width = document.body.clientWidth;
             height = document.body.clientHeight;
         }

         /* Adjust background element width and height */
         topLevel._x = width;
         topLevel._y = height;

         if (obj.offsetHeight < height) {
             var intDifference = 0;
             var intCssHeight = 0;

             if (obj.offsetHeight != "defined") {
                 obj.style.height = height + "px";
                 intDifference = (obj.offsetHeight - height);
             }

             if (!numeric(intDifference)) {
                 intDifference = 0;
             }

             intCssHeight = height - intDifference;

             if (positive(intCssHeight)) {
                 obj.style.height = intCssHeight + "px";
             }
         }

         obj.style.display = "block";

         return true;
    }

    /*** collapsible box ***/
    function expCol(rutid, accgrp, callback) {
        if (accgrp != '') {
            var arr = eval('boxIds_' + accgrp);
            for (var i = 0; i < arr.length; i++) {
                if (arr[i] == rutid) {
                    exp(arr[i]);
                    if (callback != null) callback(true, document.getElementById(arr[i] + '_h'), document.getElementById(arr[i] + '_hsv'), document.getElementById(arr[i] + '_hcsv'), document.getElementById(arr[i] + '_cnt'));
                } else {
                    col(arr[i]);
                    if (callback != null) callback(false, document.getElementById(arr[i] + '_h'), document.getElementById(arr[i] + '_hsv'), document.getElementById(arr[i] + '_hcsv'), document.getElementById(arr[i] + '_cnt'));
                }
            }
        } else {
            var h = document.getElementById(rutid + '_h');
            var hsv = document.getElementById(rutid + '_hsv');
            var hcsv = document.getElementById(rutid + '_hcsv');
            var cnt = document.getElementById(rutid + '_cnt');
            if (cnt.style.display == 'none') {
                exp(rutid);
                if (callback != null) callback(true, h, hsv, hcsv, cnt);
            } else {
                col(rutid);
                if (callback != null) callback(false, h, hsv, hcsv, cnt);
            }
        }
    }
    function exp(rutid) {
        var c = document.getElementById(rutid + '_cnt');
        c.style.display = '';
    }
    function col(rutid) {
        var c = document.getElementById(rutid + '_cnt');
        c.style.display = 'none';
    }



/*** tooltip ***/
        function showHover(elem, hoverid, topoffset, leftoffset) {
            // for backwards compability
            showHover0(elem, hoverid, topoffset, leftoffset);
        }
        function showHover0(elem, hoverid, topoffset, leftoffset) {
            var h = $('#' + hoverid).outerHeight({ margin: true });
            var pos = $(elem).offset();
            $('#' + hoverid).css('top', (pos.top-h+topoffset) + 'px');
            $('#' + hoverid).css('left', (pos.left+leftoffset) + 'px');
        }
        function showHover1(elem, hoverid, topoffset, leftoffset) {
            var h = $(elem).outerHeight({ margin: true });
            var pos = $(elem).offset();
            $('#' + hoverid).css('top', (pos.top+h+topoffset) + 'px');
            $('#' + hoverid).css('left', (pos.left+leftoffset) + 'px');
        }
        function hideHover(hoverid,b) {
            if (b==false) {
                $('#' + hoverid).css('top', '-1000px');
                $('#' + hoverid).css('left', '-1000px');
            }
        }



/*** msgbox ***/
        var msgBoxTimer;
        /*
         * if autoHideAfter is not provided, the standard wait interval of 5000 ms is used
         * if autoHideAfter is -1, then the messagebox is not closed until clicked
         *
         * the icons (for now) are:
         *    1 ok (green check)
         *    2 warning (yellow triangle)
         *    3 error (red stop sign)
         *    4 information (blue i-sign)
         */
        function msgBoxShow(message, icon, autoHideAfter) {
            if (!(autoHideAfter > 0) && !(autoHideAfter == -1)) autoHideAfter = 5000;

            switch (icon) {
                case 1: case 2: case 3: case 4: // witih the valid range
                    break;
                default: // out of range - defaults to 4
                    icon = 4;
                    break;
            }
            
            $("#msgbox span").html(message);
            $("#msgbox img").attr("src", "../Img/icons/msgbox_" + icon + ".png");
            $("#msgbox").show();
            
            // if the height of the text is higher than 40px
            var textHeight = $("#msgbox span").outerHeight();
            if (textHeight > 40) {
                // resize the msgbox container, to fit the contained text span
                $("#msgbox").css("height", (textHeight + 20) + "px");
            } else {
                // else, vertical-align:middle the text
                $("#msgbox span").css("top", (((40 - textHeight) / 2) + 10) + "px");
                $("#msgbox").css("height", "60px");
            }
            
            if (autoHideAfter > 0) {
                $.timer(autoHideAfter, function(timer) {
                    msgBoxTimer = timer;
                    msgBoxHide();
                    timer.stop();
                });
            }
        }
        function msgBoxHide() {
            $("#msgbox").fadeOut(500);
        }



/*** Check IE version ***/
        function getInternetExplorerVersion()
        // Returns the version of Windows Internet Explorer or a -1
        // (indicating the use of another browser).
        {
            var rv = -1; // Return value assumes failure.
            if (navigator.appName == 'Microsoft Internet Explorer') {
                var ua = navigator.userAgent;
                var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
                if (re.exec(ua) != null)
                    rv = parseFloat(RegExp.$1);
            }
            return rv;
        }


        function checkIEVersion(version) {
            // Valid IE versions: 8.0, 7.0, 6.0
            var IEver = getInternetExplorerVersion();
            if (IEver == version)
                return true;
            else
                return false;
        }