﻿        function formatCurrency(num) {
            num = num.toString().replace(/\$|\,/g,'');
            if(isNaN(num))
            num = "0";
            sign = (num == (num = Math.abs(num)));
            num = Math.floor(num*100+0.50000000001);
            cents = num%100;
            num = Math.floor(num/100).toString();
            if(cents<10)
            cents = "0" + cents;
            for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
            num = num.substring(0,num.length-(4*i+3))+','+
            num.substring(num.length-(4*i+3));
            return (((sign)?'':'-') + '$' + num + '.' + cents);
        }

        function onSelectedIndexChanged_1_2_ddl1(sender, eventArgs)
        {
            var ddlOption2 = $find("ctl00_cphContent_ddlOption2");
            var currOption1;
            var minDiscountPrice = 99999;
            var minRetailPrice = 99999;
            ddlOption2.enable();
            currOption1 = sender.get_text();

            ddlOption2.clearItems();
            ddlOption2.clearSelection();
            var comboItem = new Telerik.Web.UI.RadComboBoxItem();
            comboItem.set_text("select a " + option2Text);
            ddlOption2.get_items().add(comboItem);
            ddlOption2.commitChanges();

            if (currOption1 != "select a " + option1Text)
            {
                var imageMatch = -1;
                for(i = 0; i < skuList.length; i++)
                {
                    if (currOption1 == skuList[i][0])
                    {
                        var comboItem = new Telerik.Web.UI.RadComboBoxItem();
                        comboItem.set_text(skuList[i][1]);
                        ddlOption2.get_items().add(comboItem);
                        ddlOption2.commitChanges();
                        imageMatch = i;
                        if (parseFloat(minDiscountPrice) > parseFloat(skuList[i][6]))
                        {
                            minDiscountPrice = skuList[i][6];
                            minRetailPrice = skuList[i][5];
                        }
                    }
                }
                changePrice(minRetailPrice, minDiscountPrice);
                SwapImage(imageMatch, 0);
                var item = ddlOption2.findItemByText("select a " + option2Text);
                if(item)
                {
                  item.select();
                }            
                ddlOption2.showDropDown();            
            }
            else
            {
                var item = ddlOption2.findItemByText("select a " + option2Text);
                if(item)
                  item.select();
                ddlOption2.disable();
                ResetImage();
            }
        }       
        function onSelectedIndexChanged_1_2_ddl2(sender, eventArgs)
        {
            var currOption1;
            var currOption2;
            var ddlOption1 = $find("ctl00_cphContent_ddlOption1");
            currOption1 = ddlOption1.get_text();
            currOption2 = sender.get_text();
                        
            if (currOption2 != "select a " + option2Text)
            {
                for(i = 0; i < skuList.length; i++)
                {
                    if (currOption1 == skuList[i][0] && currOption2 == skuList[i][1])
                    {
                        changePrice(skuList[i][5], skuList[i][6]);
                        i = skuList.length;
                    }
                }
                $find("ctl00_cphContent_txtQty").focus();                
            }
        }    
        function onSelectedIndexChanged_1_0_ddl1(sender, eventArgs)
        {
            var currOption1;
            currOption1 = sender.get_text();
            var imageMatch;
                        
            if (currOption1 != "select a " + option1Text)
            {
                for(i = 0; i < skuList.length; i++)
                {
                    if (currOption1 == skuList[i][0])
                    {
                        changePrice(skuList[i][4], skuList[i][5]);
                        imageMatch = i;
                        i = skuList.length;
                    }
                }
                SwapImage(imageMatch, 1);   
                $find("ctl00_cphContent_txtQty").focus();
            }
        }               
        function changePrice(minRetailPrice, minDiscountPrice)
        {
            if (minDiscountPrice == minRetailPrice) {
                if ($('#ctl00_cphContent_listPrice').length) {
                    document.getElementById("ctl00_cphContent_ltlPrice").innerHTML = formatCurrency(minRetailPrice);
                    document.getElementById('ctl00_cphContent_listPrice').style.visibility = "hidden";
                    document.getElementById('ctl00_cphContent_youSave').style.visibility = "hidden";
                }
                else {
                    document.getElementById("ctl00_cphContent_ltlPrice").innerHTML = formatCurrency(minRetailPrice);
                }
            }
            else {
                var priceDiff = minRetailPrice - minDiscountPrice;
                if ($('#ctl00_cphContent_listPrice').length) {
                    document.getElementById('ctl00_cphContent_listPrice').style.visibility = "visible";
                    document.getElementById('ctl00_cphContent_youSave').style.visibility = "visible";
                   
                    document.getElementById("ctl00_cphContent_ltlListPrice").innerHTML = "<span class='strike'>" + formatCurrency(minRetailPrice) + "</span>";
                    document.getElementById("ctl00_cphContent_ltlSave").innerHTML = formatCurrency(priceDiff) + " (" + Math.round((priceDiff / minRetailPrice) * 100) + "%)";
                    document.getElementById("ctl00_cphContent_ltlPrice").innerHTML = "<span class='sale'>" + formatCurrency(minDiscountPrice) + "</span>";
                }
                else {
                    document.getElementById("ctl00_cphContent_ltlPrice").innerHTML = "<span class='strike'>" + formatCurrency(minRetailPrice) + "</span><span class='sale'>&nbsp;" + formatCurrency(minDiscountPrice) + "</span><span class='savings'> You Save: " + formatCurrency(priceDiff) + " (" + Math.round((priceDiff / minRetailPrice) * 100) + "%)</span>";
                }
            }
        }             
        function SwapImage(imageMatch, offset)
        {
            var current = jQuery('#prodImage img').attr("src");

            if (current.indexOf(skuList[imageMatch][4 - offset]) == -1 && skuList[imageMatch][4 - offset] != '') {
                if (quickView == 0)
                {
                    jQuery(".jqZoomWindow").remove();
                    jQuery(".jqZoomPup").remove();
                    jQuery(".jqzoom").remove();  

                    jQuery('#prodImage').html('<a href="/assets/ProductImages/'+skuList[imageMatch][3 - offset]+'" class="jqzoom"><img src="/assets/ProductImages/'+skuList[imageMatch][4 - offset]+'" /></a>');
			        var options =
			        {
				        zoomWidth: 600,
				        zoomHeight: 400,
                        zoomType:'reverse'				    
			        }

                    jQuery(".jqzoom").jqzoom(options);
                }
                else
                {
                    jQuery('#prodImage').html('<img src="/assets/ProductImages/'+skuList[imageMatch][4 - offset]+'" width="200" height="200" style="border: 1px solid #CCC;" />');
                }
            }
        } 

