﻿

    function ReformatPrice(ProdPrice)
    {
        var newprodPrice;

        if (ProdPrice.length > 3)
        {
            newprodPrice = "";
            
            for (var z = 0; z < ProdPrice.length; z++)
            {
                if (z == (ProdPrice.length - 3) || z == (ProdPrice.length - 6)|| z == (ProdPrice.length - 9))
                {
                
                    newprodPrice += " ";
                }
                  newprodPrice += ProdPrice.charAt(z);
            }
        }
        else
        {
            newprodPrice = ProdPrice;
        }
        return (newprodPrice + " kr");
    }

    // Set onkeydown(<elementID>,event) on textbox to fire correct button.
    function kd(element, e) {

        var intKey = (window.Event) ? e.keyCode : e.keyCode;
        if (intKey == 13) { //enter key
            document.getElementById(element).click();
            //__doPostBack(element, '');

            return false;
        }
        return true;
    }

    function validate_date(date)
    {
      var pattern= new RegExp(/\b\d{1,4}[\-]\d{1,2}[\-]\d{2}\b/);
      return pattern.test(date);
    }



    function PrintElement(strid) {

        var divToPrint = document.getElementById(strid);
        var newWin = window.open('', 'PrintWindow','left=0,top=0,width=500,height=500,toolbar=0,scrollbars=0,status=0');
        newWin.document.open();
        newWin.document.write("<html><head><link rel=\"stylesheet\" href=\"StyleSheets/Main.css\" type=\"text/css\" />");
        newWin.document.write("<link rel=\"stylesheet\" href=\"StyleSheets/ControlImport.css\" type=\"text/css\" /></head>");
        newWin.document.write('<body>' + divToPrint.innerHTML + '<script type="text/javascript">window.print();</script></body></html>');
        //newWin.document.close();

    }


    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
    }


    window.size = function() {
        var w = 0;
        var h = 0;
        //IE
        if (!window.innerWidth) {
            //strict mode
            if (!(document.documentElement.clientWidth == 0)) {
                w = document.documentElement.clientWidth;
                h = document.documentElement.clientHeight;
            }
            //quirks mode
            else {
                w = document.body.clientWidth;
                h = document.body.clientHeight;
            }
        }
        //w3c
        else {
            w = window.innerWidth;
            h = window.innerHeight;
        }
        return { width: w, height: h };
    }
    window.center = function() {
        var hWnd = (arguments[0] != null) ? arguments[0] : { width: 0, height: 0 };
        var _x = 0;
        var _y = 0;
        var offsetX = 0;
        var offsetY = 0;
        //IE
        if (!window.pageYOffset) {
            //strict mode
            if (!(document.documentElement.scrollTop == 0)) {
                offsetY = document.documentElement.scrollTop;
                offsetX = document.documentElement.scrollLeft;
            }
            //quirks mode
            else {
                offsetY = document.body.scrollTop;
                offsetX = document.body.scrollLeft;
            }
        }
        //w3c
        else {
            offsetX = window.pageXOffset;
            offsetY = window.pageYOffset;
        }
        _x = ((this.size().width - hWnd.width) / 2) + offsetX;
        _y = ((this.size().height - hWnd.height) / 2) + offsetY;
        return { x: _x, y: _y };
    }

    function getInternetExplorerVersion() {

        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 checkVersion() {

        var msg = "You're not using Windows Internet Explorer.";

        var ver = getInternetExplorerVersion();

        if (ver > -1) {

            if (ver >= 8.0)

            //msg = "You're using a recent copy of Windows Internet Explorer."
                return "8";
            else
                return "lessthan8"

        }




    }

    // JQuery

    jQ.fn.centerInClient = function(options) {
    
    
            //$(document).ready(function() {
              //  $("#box").centerInClient();
            //});

            //which centers the element named box in the client window. To center the element inside of another container element you might use:

            //$("#box").centerInClient({ container: window, forceAbsolute: true });
            
        /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
        /// Ideally the selected set should only match a single element.
        /// </summary>    
        /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
        /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
        ///  and attached to the body element to ensure proper absolute positioning. 
        /// Be aware that this may cause ID hierachy for CSS styles to be affected.
        /// </param>
        /// <returns type="jQuery" />
        var opt = { forceAbsolute: false,
            container: window,    // selector of element to center in
            completeHandler: null,
            minX: 0,
            minY: 0
        };
        jQ.extend(opt, options);

        return this.each(function(i) {
            var el = jQ(this);
            var jWin = jQ(opt.container);
            var isWin = opt.container == window;

            // force to the top of document to ENSURE that 
            // document absolute positioning is available
            if (opt.forceAbsolute) {
                if (isWin)
                    el.remove().appendTo("body");
                else
                    el.remove().appendTo(jWin.get(0));
            }

            // have to make absolute
            el.css("position", "absolute");

            // height is off a bit so fudge it
            var heightFudge = isWin ? 2.0 : 1.8;

            var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
            var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

            el.css("left", x < opt.minX ? opt.minX + jWin.scrollLeft() : x + jWin.scrollLeft());
            el.css("top", y < opt.minY ? opt.minY + jWin.scrollTop() : y + jWin.scrollTop());

            // if specified make callback and pass element
            if (opt.completeHandler)
                opt.completeHandler(this);
        });
    }
    
        /// <summary>
    /// Makes prices easier to read by adding spacings.
    /// </summary>
    /// <param name="prodPrice">Add the number to be formatted.</param>
    /// <returns></returns>
    function ReformatPrice(ProdPrice)
    {
        var newprodPrice = "";

        if (ProdPrice.length > 3)
        {
            newprodPrice = "";
            for (var z = 0; z < ProdPrice.length; z++)
            {

                if (z == (ProdPrice.length - 3) || z == (ProdPrice.length - 7))
                {
                    newprodPrice = newprodPrice + ".";
                }

                newprodPrice = newprodPrice + ProdPrice.substring(z, z + 1);
            }
        }
        else
        {
            newprodPrice = ProdPrice;
        }

        return newprodPrice;
    }

    // .center();  center(true) for absolute  http://andreaslagerkvist.com/jquery/center/
    jQ.fn.center = function (absolute) {
        return this.each(function () {
            var t = jQ(this);

            t.css({
                position: absolute ? 'absolute' : 'fixed',
                left: '50%',
                top: '50%',
                zIndex: '99'
            }).css({
                marginLeft: '-' + (t.outerWidth() / 2) + 'px',
                marginTop: '-' + (t.outerHeight() / 2) + 'px'
            });

            if (absolute) {
                t.css({
                    marginTop: parseInt(t.css('marginTop'), 10) + jQ(window).scrollTop(),
                    marginLeft: parseInt(t.css('marginLeft'), 10) + jQ(window).scrollLeft()
                });
            }
        });
    };


    jQ.getDocHeight = function () {
        return Math.max(
                    jQ(document).height(),
                    jQ(window).height(),
                     /* For opera: */
                    document.documentElement.clientHeight
                );
    };

    function parseDecimal(d, zeros, trunc) {
        d = d.replace(/[a-zA-Z\!\@\#\$\%\^\&\*\(\)\_\+\-\=\{\}\|\[\]\\\:\"\;'\<\>\?\,\/\~\`]/g, "");
        while (d.indexOf(".") != d.lastIndexOf("."))
            d = d.replace(/\./, "");
        if (typeof zeros == 'undefined' || zeros == "") {
            return parseFloat(d);
        }
        else {
            var mult = Math.pow(10, zeros);
            if (typeof trunc == 'undefined' || (trunc) == false)
                return parseFloat(Math.round(d * mult) / mult);
            else
                return parseFloat(Math.floor(d * mult) / mult);
        }
    }


    function IsNumeric(input) {
        return (input - 0) == input && input.length > 0;
    }


    function IsPersonNummer(sPNum) {
        var numbers = sPNum.match(/^(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)$/);
        var checkSum = 0;

        var d = new Date();
        if (!isDate(sPNum.substring(0, 4), sPNum.substring(4, 6), sPNum.substring(6, 8))) {
            //alert("Datumet " + sPNum.substring(0, 8) + " är inte korrekt.");
            return false;
        }

        if (numbers == null) { return false; }

        var n;
        for (var i = 3; i <= 12; i++) {
            n = parseInt(numbers[i]);
            if (i % 2 == 0) {
                checkSum += n;
            } else {
                checkSum += (n * 2) % 9 + Math.floor(n / 9) * 9
            }
        }

        if (checkSum % 10 == 0) { return true; }
        return false;
    }
