    //Variables used for fixing Transparent PNG files in IE.
    var strRootPath = '/';
    
    $(document).ready(function () {
        //Does buttons with prefetch in all browsers
        SetupButtons();

        //Fix times
        FixTimes();

        //Makes sure that the slide shows run.
        $('a[rel*=lyteshow]').lightBox();
    });

    function AddItemToCart(ProductID, Model, Name, Path, Units, Price, ReferenceID) {        
        var LockPrice = false;
        var json = JSON.stringify({ Product: ProductID, Units: Units, ProductName: Name, ProductModel: Model, Path: Path, Price: Price, ReferenceID: ReferenceID, LockPrice: LockPrice });

        $.ajax({
            type: "POST",
            url: "/AddToCart.svc/AddToCart",
            data: json,
            processData: true,
            contentType: "application/json; charset=utf-8",
            dataType: "text",
            success: AddToCartSuccess,
            async: false,
            error: AddToCartFail
        });
    }

    function AddToCartSuccess(response) {
        response = response.substring(6, response.lastIndexOf("\""));
        var Items = response.split("|");
        $("#CartItems").text(Items[0] + " Items");
        var CartTotal = parseFloat(Items[1].replace(',', ''));
        $("#CartTotal").text(num2money(CartTotal));
        $("#UnitsAdded").text(Items[2]);

        var UnitsTotal = parseFloat(Items[3].replace(',', ''));
        $("#UnitsTotal").text(num2money(UnitsTotal));

        if (Items[4] != null && Items[4] != '')
            ReferenceID = Items[4];

        $('#ItemAddToCart').show();
    }

    function AddToCartFail(response) {
        debugger;
    }

    function num2money(n_value) {
        // validate input
        if (isNaN(Number(n_value)))
            return 'ERROR';



        // save the sign
        var b_negative = Boolean(n_value < 0);
        n_value = Math.abs(n_value);
        
        // round to 1/100 precision, add ending zeroes if needed
        var s_result = String(Math.round(n_value * 1e2) % 1e2 + '00').substring(0, 2);



        // separate all orders
        var b_first = true;
        var s_subresult;
        while (n_value >= 1) {
            s_subresult = (n_value >= 1e3 ? '00' : '') + Math.floor(n_value % 1e3);
            s_result = s_subresult.slice(-3) + (b_first ? '.' : ',') + s_result;
            b_first = false;
            n_value = n_value / 1e3;
        }

        // add at least one integer digit
        if (b_first)
            s_result = '0.' + s_result;

        // apply formatting and return
        return b_negative ? '($' + s_result + ')' : '$' + s_result;
    }
    
    function FixTimes() {
        var spanElements = document.getElementsByTagName('span');
        
        for(var i = 0; i < spanElements.length; i++) {
            var e = spanElements[i];
            if (e.title && e.title == "time") {
                
                var OldDate = new Date('Jan 01, 1900 02:00');
                var offset = OldDate.getTimezoneOffset();
                var NewDate = new Date(e.innerHTML);
                NewDate.setMinutes(NewDate.getMinutes() - offset);
                e.innerHTML = NewDate.toLocaleString();
            }            
        }    
    }
          
    function SetupButtons() {
        var ImgElements = document.getElementsByTagName('img');
        var BtnElements = document.getElementsByTagName('input');
        
        for(var i = 0; i < ImgElements.length; i++) {
            AssignButtonProperties(ImgElements[i]);
        }    
        
        for(var i = 0; i < BtnElements.length; i++) {
            if (BtnElements[i].getAttribute("type") == "image") {
                AssignButtonProperties(BtnElements[i]);    
            }
        }     
    }
       
    function AssignButtonProperties(Element) {
        if (Element.getAttribute("srcdown") || Element.getAttribute("srcover")) {
            //We have a winner
            //Pre-load the images and set the mouse events.
            
            Element.onmouseup = ButtonUp;
            Element.onmouseout = ButtonOut;
            Element.onmouseover = ButtonOver;

            if (Element.getAttribute("srcdown")) {
                Element.onmousedown = ButtonDown;
                var img = new Image();
                img.src = GetRootPath(Element.getAttribute("srcdown"));
            }

            if (Element.getAttribute("srcover")) {
                var img = new Image();
                img.src = GetRootPath(Element.getAttribute("srcover"));
            }               
        }
    }
      
    function ButtonDown(e) {
        if (!this.onmousedown) return;
        
        SetButtonImage(this, GetRootPath(this.getAttribute("srcdown")));        
    }
    
    function ButtonUp(e) {
        if (!this.onmouseup) return;
        
        if (this.getAttribute("srcup")) //Only need to call it if the srcup exists, otherwise it hasn't been changed anyhow.
            SetButtonImage(this, GetRootPath(this.getAttribute("srcup")));    
    }
    
    function ButtonOut(e) {
        if (!this.onmouseout) return;
        
        if (this.getAttribute("srcup")) //Only need to call it if the srcup exists, otherwise it hasn't been changed anyhow.
            SetButtonImage(this, GetRootPath(this.getAttribute("srcup")));    
    }
    
    function ButtonOver(e) {
        if (!this.onmouseover) return;
        
        var ButtonDown = false;
        
        //This one is a little trickier, because we have to figure out if the mouse button is down or now.
        if (!e) var e = window.event;
        var ButtonDown = GetMouseButton(e);       
        
        if (ButtonDown > 0 && this.getAttribute("srcdown")) {
            SetButtonImage(this, GetRootPath(this.getAttribute("srcdown")));
        } else {
            if (this.getAttribute("srcover")) {
                SetButtonImage(this, GetRootPath(this.getAttribute("srcover")));
            } else if (this.getAttribute("srcup")) {
                SetButtonImage(this, GetRootPath(this.getAttribute("srcup")));
            } 
        }
    }
      
    function SetButtonImage(e, Src) {
        if (e.style.filter && navigator.appVersion.indexOf("MSIE") != -1) {
  	        e.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + Src  + "', sizingMethod='image')";
        } else {
            if (!e.getAttribute("srcup"))
                e.setAttribute("srcup", e.src);
            e.src = Src;
        }
    }
    
    function checkIt(string)
    {
	    place = detect.indexOf(string) + 1;
	    thestring = string;
	    return place;
    }


    function GetRootPath(Path) {
        if (Path.indexOf('~/') > -1) {
            return strRootPath + Path.substring(2, Path.length);
        } else {
            return Path;    
        }
    }
        
     ///Returns the button that is down in all browsers consistantly. Amazing.
    function GetMouseButton(theEvent) {
        if (theEvent.which && theEvent.which == 1) {
            ButtonDown = true;
        } else if (navigator.appVersion.indexOf("MSIE") != -1) {
            if (theEvent.button) {
                return theEvent.button;
            } else {
                return 0;
            }
        } else {
            if (theEvent.button) {
                if (theEvent.button == 0) {
                    return 1;
                } else if (theEvent.button == 1) {
                    return 4
                } else if (theEvent.button == 2) {
                    return 2;
                } else {
                    return 0;
                }
            } else {
                return 0;
            }
        }
    }
    
    function uppercase(e) {
        var key;
        if (e.keyCode)
            key = e.keyCode;
        else if (e.which) 
            key = e.which;

        
        if ((key > 0x60) && (key < 0x7B)) {
            if (navigator.appVersion.indexOf("MSIE") != -1)
                e.keyCode = key-0x20;
        }
    }
    
    function uppercaseexit(e) {
        e.value = e.value.toUpperCase();
    }
