

    $(document).ready(function(){

        // If this is a version of IE browser
        if($.browser.msie){
               $("select.prmType").css("width", "auto");
        }

        $("select.prmType").change(function(){
            var row = this.name.substr(this.name.lastIndexOf("_")+1,this.name.length);
            var f = this.form;
            var gpf = f.elements["gpf_"+row].value
            var initialExcPrice = f.elements["entitlementInitialExcPrice_"+row].value
            var initialIncPrice = f.elements["entitlementInitialIncPrice_"+row].value
            var discountType = f.elements["discountType_"+row].value
            var discount = f.elements["discount_"+row].value
            var dblPrice, dblYouPay
            var sChosenProduct =  this.options[this.selectedIndex].value;

            f.elements["productCode_"+row].value = "";

            $.get("/basket/entitlement.asp", {productCode:sChosenProduct, statusFlag:"type"}, function(xml){

                if(gpf == 'ExVat') {
                    dblPrice = parseFloat(initialExcPrice).toFixed(2)
                    $("#entitlementExcPrice_"+row).text("£" + dblPrice)

                    if(discountType == 'Percent') {
                        dblYouPay = parseFloat(initialExcPrice - (Math.floor((initialExcPrice / 100)*(discount)* 100) / 100))
                        dblYouPay = (Math.floor((dblYouPay*100))/100).toFixed(2)
                        $("#entitlementPercentExcYouPay_"+row).text("£" + dblYouPay)
                    }
                    else {
                        dblYouPay = parseFloat(initialExcPrice - discount).toFixed(2)
                        $("#entitlementValueExcYouPay_"+row).text("£" + dblYouPay)
                    } 
                }
                else {
                    dblPrice = parseFloat(initialIncPrice).toFixed(2)
                    $("#entitlementIncPrice_"+row).text("£" + dblPrice)

                    if(discountType == 'Percent') {
                        dblYouPay = parseFloat(initialIncPrice - (Math.floor((initialIncPrice / 100)*(discount)* 100) / 100))
                        dblYouPay = (Math.floor((dblYouPay*100))/100).toFixed(2)
                        $("#entitlementPercentIncYouPay_"+row).text("£" + dblYouPay)
                    }
                    else {
                        dblYouPay = parseFloat(initialIncPrice - discount).toFixed(2)
                        $("#entitlementValueIncYouPay_"+row).text("£" + dblYouPay)
                    } 
                }

                if($("Product",xml).length == 1){
                    f.elements["productCode_"+row].value = sChosenProduct;

                    if(gpf == 'ExVat') {
                        dblPrice = parseFloat($("Product",xml).attr("Price_Exc_Vat")).toFixed(2)
                        $("#entitlementExcPrice_"+row).text("£" + dblPrice)

                        if(discountType == 'Percent') {
                            dblYouPay = parseFloat($("Product",xml).attr("Price_Exc_Vat") - (Math.floor(($("Product",xml).attr("Price_Exc_Vat") / 100)*(discount)* 100) / 100))
                            dblYouPay = (Math.floor((dblYouPay*100))/100).toFixed(2)
                            $("#entitlementPercentExcYouPay_"+row).text("£" + dblYouPay)
                        }
                        else {
                            dblYouPay = parseFloat($("Product",xml).attr("Price_Exc_Vat") - discount).toFixed(2)
                            $("#entitlementValueExcYouPay_"+row).text("£" + dblYouPay)
                        } 
                    }
                    else {
                        dblPrice = parseFloat($("Product",xml).attr("Price_Inc_Vat")).toFixed(2)
                        $("#entitlementIncPrice_"+row).text("£" + dblPrice)

                        if(discountType == 'Percent') {
                            dblYouPay = parseFloat($("Product",xml).attr("Price_Inc_Vat") - (Math.floor(($("Product",xml).attr("Price_Inc_Vat") / 100)*(discount)* 100) / 100))
                            dblYouPay = (Math.floor((dblYouPay*100))/100).toFixed(2)
                            $("#entitlementPercentIncYouPay_"+row).text("£" + dblYouPay)
                        }
                        else {
                            dblYouPay = parseFloat($("Product",xml).attr("Price_Inc_Vat") - discount).toFixed(2)
                            $("#entitlementValueIncYouPay_"+row).text("£" + dblYouPay)
                        } 
                    }
                }
            },"xml");
        });
        
        //--- register on change for Size Filter and process if changed ---//
        $("select.prmSize, select.prmColour").change(function(){
            var row = this.name.substr(this.name.lastIndexOf("_")+1,this.name.length);
            var type = this.name.substr(3,this.name.indexOf("_")-3)
             var f = this.form;
            var sizeExists = f.elements["prmSize_"+row]!=undefined
            var colourExists = f.elements["prmColour_"+row]!=undefined

            if(this.selectedIndex==0){
                $("#productCode_"+row).val("");
            }
            /*
            if(type=="Colour" && sizeExists){
                if(f.elements["prmSize_"+row].selectedIndex==0){
                    alert("Please select a size before choosing a colour");
                    this.selectedIndex=0;
                }
            } else {
            */

                var sStyleCode = $("#styleCode_"+row).attr("value");
                var iPromoId = $("#promoId_"+row).attr("value");
                var sSize = (sizeExists)?f.elements["prmSize_"+row].value:"";
                
                var sColour = (type=="Colour" && colourExists)?f.elements["prmColour_"+row].value:"";
                var gpf = f.elements["gpf_"+row].value
                var initialExcPrice = f.elements["entitlementInitialExcPrice_"+row].value
                var initialIncPrice = f.elements["entitlementInitialIncPrice_"+row].value
                var discountType = f.elements["discountType_"+row].value
                var discount = f.elements["discount_"+row].value
                var dblPrice, dblYouPay
                
                $.get("/basket/entitlement.asp", {styleCode:sStyleCode, promoId:iPromoId, size:sSize, colour:sColour}, function(xml){

                    if(gpf == 'ExVat') {
                        dblPrice = parseFloat(initialExcPrice).toFixed(2)
                        $("#entitlementExcPrice_"+row).text("£" + dblPrice)

                        if(discountType == 'Percent') {
                            dblYouPay = parseFloat(initialExcPrice - (Math.floor((initialExcPrice / 100)*(discount)* 100) / 100))
                            dblYouPay = (Math.floor((dblYouPay*100))/100).toFixed(2)
                            $("#entitlementPercentExcYouPay_"+row).text("£" + dblYouPay)
                        }
                        else {
                            dblYouPay = parseFloat(initialExcPrice - discount).toFixed(2)
                            $("#entitlementValueExcYouPay_"+row).text("£" + dblYouPay)
                        } 
                    }
                    else {
                        dblPrice = parseFloat(initialIncPrice).toFixed(2)
                        $("#entitlementIncPrice_"+row).text("£" + dblPrice)

                        if(discountType == 'Percent') {
                            dblYouPay = parseFloat(initialIncPrice - (Math.floor((initialIncPrice / 100)*(discount)* 100) / 100))
                            dblYouPay = (Math.floor((dblYouPay*100))/100).toFixed(2)
                            $("#entitlementPercentIncYouPay_"+row).text("£" + dblYouPay)
                        }
                        else {
                            dblYouPay = parseFloat(initialIncPrice - discount).toFixed(2)
                            $("#entitlementValueIncYouPay_"+row).text("£" + dblYouPay)
                        } 
                    }

                    if(type=="Colour" && sizeExists){
                        if(f.elements["prmSize_"+row].selectedIndex==0 && f.elements["prmSize_"+row].length > 1){
                            alert("Please select a size before choosing a colour");
                            this.selectedIndex=0;
                            return false;
                        }
                    } 
                    if($("Product",xml).length == 1){
                        f.elements["productCode_"+row].value = $("Product",xml).attr("ProductCode");
                        //alert("From XML" + $("Product",xml).attr("ProductCode"))
                        //alert(f.elements["productCode_"+row].value)

                        if(gpf == 'ExVat') {
                            dblPrice = parseFloat($("Product",xml).attr("Price_Exc_Vat")).toFixed(2)
                            $("#entitlementExcPrice_"+row).text("£" + dblPrice)

                            if(discountType == 'Percent') {
                                dblYouPay = parseFloat($("Product",xml).attr("Price_Exc_Vat") - (Math.floor(($("Product",xml).attr("Price_Exc_Vat") / 100)*(discount)* 100) / 100))
                                dblYouPay = (Math.floor((dblYouPay*100))/100).toFixed(2)
                                $("#entitlementPercentExcYouPay_"+row).text("£" + dblYouPay)
                            }
                            else {
                                dblYouPay = parseFloat($("Product",xml).attr("Price_Exc_Vat") - discount).toFixed(2)
                                $("#entitlementValueExcYouPay_"+row).text("£" + dblYouPay)
                            } 
                        }
                        else {
                            dblPrice = parseFloat($("Product",xml).attr("Price_Inc_Vat")).toFixed(2)
                            $("#entitlementIncPrice_"+row).text("£" + dblPrice)

                            if(discountType == 'Percent') {
                                dblYouPay = parseFloat($("Product",xml).attr("Price_Inc_Vat") - (Math.floor(($("Product",xml).attr("Price_Inc_Vat") / 100)*(discount)* 100) / 100))
                                dblYouPay = (Math.floor((dblYouPay*100))/100).toFixed(2)
                                $("#entitlementPercentIncYouPay_"+row).text("£" + dblYouPay)
                            }
                            else {
                                dblYouPay = parseFloat($("Product",xml).attr("Price_Inc_Vat") - discount).toFixed(2)
                                $("#entitlementValueIncYouPay_"+row).text("£" + dblYouPay)
                            } 
                        }

                        if(type=="Size"){
                            $("Product", xml).each(function(){
                                $("#prmColour_"+row).html('<option value="' + $(this).attr("ProductColourCode") + '">' + $(this).attr("ProductColour") + '</option>');
                            });
                        }
                    } else {
                        //alert("Rewrite the colour select box")
                        if(colourExists){
                            var options = '<option value="">Choose Colour...</option>';
                            $("Colour", xml).each(function(){
                                options+='<option value="' + $(this).attr("ProductColourCode") + '">' + $(this).attr("ProductColour") + '</option>'
                            });
                            $("#prmColour_"+row).html(options);
                            f.elements["prmColour_"+row].selectedIndex=0;
                        }
                        
                    }
                      
                      //   $("#messageContainer").text($("Detail",xml).attr("ErrorMessage")).css("display","block");

                },"xml");
            //}

        });

    $(".entitlementAdd").click(function() {

        var url = this.href;
        var row = this.id.substr(this.id.lastIndexOf("_")+1,this.id.length);
        var f = document.basket;
        var sizeExists = f.elements["prmSize_"+row]!=undefined
        var colourExists = f.elements["prmColour_"+row]!=undefined
        
        if(url.lastIndexOf("prmPPC") > 0){
            url += "&prmQuantity=" +   f.elements["prmQuantity_" + row].value
            this.href = url;
            return true;
        } else {
            if(sizeExists && f.elements["prmSize_"+row].options.length > 1 && f.elements["prmSize_"+row].selectedIndex == 0){
                alert("Please select a size");
                return false
            } else if(colourExists && f.elements["prmColour_"+row].options.length > 1 && f.elements["prmColour_"+row].selectedIndex == 0){
                alert("Please select a colour")
                return false
            } else if(f.elements["productCode_"+row].value==""){
                alert("Please select an option")
                return false;
            } else {
                // Both selected
                
                //Add the product code and quantity to the URL
                
                url += "&prmPPC=" + f.elements["productCode_" + row].value
                url += "&prmQuantity=" +   f.elements["prmQuantity_" + row].value
                
                this.href = url;
                return true;
            }
            return false;
        }
    });


    });
