function checkFinanceStatus(oid) {

    $("#checkstatus").val("checking...");
    $.post("/common/ajax/checkfinance.php","id="+oid, function(data) {
        if (data.agreement != '') {
        	if (data.status == 'AcceptInPrincipal') {
        		status = 'Referred';
        	} else {
        		status = data.status;
        	}
            $("#financestatus").remove();
            $("#checkstatus").before('<span>'+data.agreement+'</span>');
            $("#checkstatus").parent().after('<p class="col2 indent"><strong>Finance Status : </strong>'+status+'</p>');
            $("#checkstatus").hide();
        } else {
            $("#checkstatus").val('check status');
        }
    },'json');
}

function genPath(str) {



    str = str.replace(/[^a-zA-Z +0-9]/g,'');

    str = str.replace(/\+/g,"-");

    str = str.replace(/  /g," ");

    str = str.replace(/ /g,"-");



    return str.toLowerCase();



}



var fixHelper = function(e, ui) {

    ui.children().each(function() {

        $(this).width($(this).width());

    });

    return ui;

 };



function serializeTable(parent) {



    var order = [];



    var i = 0;

    $(parent).children("tr").each(function() {

        order[i] = $(this).attr("id").substring(1);

        i++;

    });



    return order;



}





$(document).ready(function() {



    $("#addfoot").bind("click", function() {

       $("#newfoot").toggle();

    });

    $("#addcolour").bind("click", function() {

       $("#newcolour").toggle();

    });

    $("#addrelated").bind("click", function() {

       $("#newrelated").toggle();

    });

    $("#addnote").bind("click", function() {

       $("#newnote").toggle();

    });

    $(".modal .titlebar span").bind("click", function() {

        $(".modal").hide();

    });

    $("#btnFinance").bind("click", function() {

       $("#financeExtras").toggle();

       return false;

    });



    $("#whyscs").bind("click", function() {

       $(".slider").hide();

       $(".introtext").show();

    });



    $("#nav ul li").hover(function() {

        $(".sub-menu").hide(); // hide any open sub menus

        if ($(this).children(".sub-menu").length) {

            var pos = $(this).position();

            var h = $(this).height();

            $(this).children(".sub-menu").css("top", (pos.top+h)+"px").css("left", pos.left+"px").show();

        }

    }, function() {

        $(this).children(".sub-menu").hide(); // hide sub menu

    });



    if ($(".slider").length) {

        $(".slider").easySlider({controlsShow:false, auto:true, continuous:true});

    }

    //$("#product").slimbox();



    // Ensure all heights match

    var pc = $(".panels").length;

    for (i=1; i<pc+1; i++) {

        var crmax = 0;

        $("#cat-row"+i+" .panel .info").each(function() {

            if ($(this).height() > crmax) {crmax = $(this).height();}

        });

        $("#cat-row"+i+" .panel .info").css("height",crmax+"px");

    }



    $(".toolbar ul li a").bind("click", function() {

       $(".toolbar-panel").hide();

       var id = $(this).attr("id");

       $("#"+id+"panel").show();

       $(".toolbar ul li a").removeClass("active");

       if ($(this).hasClass("active")) {$(this).removeClass("active")} else {$(this).addClass("active");}



       var pc = $(".panels").length;

       

        for (i=1; i<pc+1; i++) {

            var crmax = 0;

            $("#cat-row"+i+" .panel .info").each(function() {

                if ($(this).height() > crmax) {crmax = $(this).height();}

            });

            $("#cat-row"+i+" .panel .info").css("height","57px");

        }



       return false;

    });



    $(".removeItem").bind("click", function() {

       $("#remove").val($(this).attr("id"));

       $("#update").val('0');

       $("#basket").submit();

    });



    $(".question a").bind("click", function() {

        $(this).parent().next("div").slideToggle();

        return false;

    });



    $("input[name='payment_card']").bind("click", function() {

        return checkGuardsman('card');

    });

    $("input[name='payment_finance']").bind("click", function() {

        return checkGuardsman('finance');

    });

    $(".removeRelated").live("click", function() {

       removeRelated(this);

       return false;

    });



    $("#filters input[type=radio]").bind("click", function() {

       $("#filters").submit();

    });

    $("#filters a").removeAttr('href').addClass("disabled");



    $(".inner-tabs li").bind("click", function() {

       $(".inner-tabs li").removeClass("active");

       $(this).addClass("active");

       var id = $(this).attr("id");

       $(".inner-tabs-panel").hide();

       $("#pn"+id).show();

    });



    $(".colour-tabs li").bind("click", function() {

       var id = $(this).attr("id");

       $(".colour-panel").hide();

       $(this).parent().children("li").removeClass("active");

       $(this).addClass("active");

       $("#"+id+"panel").show();

       $("#price-info").html('<br />');

       $("#productPrice").val('');

       $("#frmColour").val('');



       if ($("#x_img").val() != '') {

           $("#mainproduct").attr("src",$("#x_img").val());

           $(".colour-swatch").removeClass("highlight");

           $(".colour-swatch img").removeClass("active");

       }

       setupSliders();

    });



    if ($("#slider-code1").length || $(".slider-code-active").length) {

        setupSliders();

    }

    

    $('.colour-swatch').live("click", function() {

       $(".colour-swatch").removeClass("highlight");

       $(this).addClass('highlight');

       $(".colour-swatch img").removeClass("active");

       $(this).children("img").addClass("active");

       var colour = $(this).attr("id").substring(6);

       $("#frmColour").val(colour);



       $(".magnify").hide();

       $(this).children(".magnify").show();



       var useimg = 1;

       if ($("#useimg").val() == 1) {

           useimg = 1;

       }



       if ($(this).hasClass("basic")) {

           var extra = 'noprice=1';

       }



       var pid = $("#productID").val();

       $.post("/common/ajax/prices.php","pid="+pid+"&colour="+colour+"&noprice=&useimg="+useimg+"&"+extra,function(data) {



           if (data.prices == ''){

               //$("#price-info").html('<span class="app">'+data.colour+'</span>'+data.prices);

           } else {

               $("#price-info").html('<span class="app">COLOUR: '+data.colour+'</span>'+data.prices+'<span class="pricenow">NOW &pound;'+data.price+'</span>');

           }

           

          

           $("#productPrice").val(data.price);



           if (data.image != '') {

               $("#mainproduct").attr("src",data.image);

           }

       },"json");



    });



    $('.foot-swatch').live("click", function() {

       $(".foot-swatch").removeClass("highlight");

       $(this).addClass('highlight');

       $(".foot-swatch img").removeClass("active");

       $(this).children("img").addClass("active");

       var colour = $(this).attr("id").substring(6);

       $("#frmfoot").val(colour);



       $(".magnify").hide();

       $(this).children(".magnify").show();



    });



    $(".addSwatch").bind("click", function() {

        var id = $(this).attr("id").substring(2);

       $.post("/common/ajax/swatch.php", "id="+id, function(data) {

           if ($("#chosen-swatches li").length == 3) {

               alert("You can only choose up to 3 swatches");

           } else {

               $("#chosen-swatches").append(data.item);

           }

           if ($("#chosen-swatches li").length > 0) {

               $("#noswatches").hide();

           }

       }, "json");

    });

	//switch out options for voucher codes based on type
	$('#chooseType').live('change', function(){

            switch ($(this).val()) {

                case '1':
                    $('.vactive').fadeOut(500,function(){
                        $('.discvalue').fadeIn(500).addClass("vactive");
                    });
                    break;
                case '2':
                    $('.vactive').fadeOut(500,function(){
                        $('.discvalue').fadeIn(500).addClass("vactive");
                    });
                    break;
                case '3':
                    $('.vactive').fadeOut(500,function(){
                        $('.freeproduct').fadeIn(500).addClass("vactive");
                    }).removeClass("vactive");
                    break;
                case '4':
                    $('.vactive').fadeOut(500,function(){
                        $('.discvalue').fadeIn(500).addClass("vactive")
                        $('.range').fadeIn(500).addClass("vactive");
                    }).removeClass("vactive");
                    break;
                case '5':
                    $('.vactive').fadeOut(500,function(){
                        $('.discvalue').fadeIn(500).addClass("vactive")
                        $('.brand').fadeIn(500).addClass("vactive");
                    }).removeClass("vactive");
                    break;
                case '6':
                    $('.vactive').fadeOut(500,function(){
                        $('.discvalue').fadeIn(500).addClass("vactive")
                        $('.type').fadeIn(500).addClass("vactive");
                    }).removeClass("vactive");
                    break;

            }

            

	});

});


function getProductsByRange(){
	var id = $('.chooseRange').val();

	$.post("/common/ajax/getprods.php", "id="+id, function(data) {

            $(".chooseProduct option").remove();

	        $(".chooseProduct").attr("disabled","disabled");
	
	        if (data.length > 0) {
	
	            $(".chooseProduct").append(data).attr("disabled","");
	
				$(".chooseProduct").fadeIn();
				
	        }
        
       });
}




function setupSliders() {

    if ($("#slider-code1").length) { $("#slider-code1").tinycarousel({display:3}) };

    if ($("#slider-code2").length) { $("#slider-code2").tinycarousel({display:3}) };

    if ($("#slider-code3").length) { $("#slider-code3").tinycarousel({display:3}) };

    if ($("#slider-code4").length) { $("#slider-code4").tinycarousel({display:3}) };

    if ($("#slider-code5").length) { $("#slider-code5").tinycarousel({display:3}) };

    if ($("#slider-code6").length) { $("#slider-code6").tinycarousel({display:3}) };

    if ($("#slider-code7").length) { $("#slider-code7").tinycarousel({display:3}) };

    if ($("#slider-code8").length) { $("#slider-code8").tinycarousel({display:3}) };

    if ($("#slider-code9").length) { $("#slider-code9").tinycarousel({display:3}) };

    if ($("#slider-code10").length) { $("#slider-code10").tinycarousel({display:3}) };

    if ($("#slider-code11").length) { $("#slider-code11").tinycarousel({display:3}) };

    if ($("#slider-code12").length) { $("#slider-code12").tinycarousel({display:3}) };

    if ($("#slider-code13").length) { $("#slider-code13").tinycarousel({display:3}) };

    if ($("#slider-code14").length) { $("#slider-code14").tinycarousel({display:3}) };

}



function toggleGP(status, togglecb) {

    switch (status) {

    case 1:

        $("#guardsmanrow").show();

        $("#withguardsman").show();

        $("#withoutguardsman").hide();

        break;

    case 2:

        $("#guardsmanrow").hide();

        $("#withguardsman").hide();

        $("#withoutguardsman").show();

        break;

    }



    if (togglecb == 1) {

        $("input[name='guardsman']")[0].checked = true;

    }

}



function modal(modal) {

    $("#"+modal).show();

}



function toggle(id) {

    if ($("#"+id).css("visibility") == "hidden") {

        $("#"+id).css("visibility","");

    } else {

        $("#"+id).css("visibility","hidden");

    }

}



function showProgress() {

    $("#progress").show();

}



function checkGuardsman(button) {

    var gp = $("input[name='guardsman']:checked").val();

    if (gp != 1) {

        $("#gmconfirm").show();

        $("#gmyes").bind("click", function() {toggleGP(1,1);$("#gmconfirm").hide();$("#basket").append('<input type="hidden" name="payment_'+button+'_x" value="1" />').submit();});

        $("#gmno").bind("click", function() {$("#gmconfirm").hide();$("#basket").append('<input type="hidden" name="payment_'+button+'_x" value="1" />').submit();});

        return false;

    }

}



function addRelated() {

    var pid = $("input[name='product_id']").val();

    var rid = $("#related_id").val();



    $.post('/common/ajax/related.php?action=add','product_id='+pid+'&related_id='+rid, function(data) {

        if (data.html) {

            $("#related tbody").append(data.html);

        }

    },"json");

}



function removeRelated(obj) {

    var id = $(obj).attr("id");

    $.post('/common/ajax/related.php?action=del','product_id=1&related_id='+id, function(data) {

        $("#"+id).parent().parent().remove();

    },"json");

}



function getRangeProducts(obj) {

    var id = $(obj).val();



    $.post("/common/ajax/range.php?id="+id, '', function(data) {

        $("#product_id option").remove();

        $("#product_id").attr("disabled","disabled");

        if (data.length > 0) {

            $("#product_id").append(data).attr("disabled","");

        }

    });

}



function getProductPricing(obj) {

    var id = $(obj).val();



    //there are no grades so use the default pricing

    $.post("/common/ajax/productnew.php?id="+id, '', function(data) {

            $("input[name=price1]").val(data.price1);

            $("input[name=price2]").val(data.price2);

            $("input[name=price3]").val(data.price3);

            $("input[name=price4]").val(data.price4);

            $("input[name=price5]").val(data.price5);

			$("input[name=productpriceid]").val(data.product_price_id);

			$("input[name=productpriceid1]").val(data.product_price_id_1);

			$("input[name=productpriceid2]").val(data.product_price_id_2);

			$("input[name=productpriceid3]").val(data.product_price_id_3);

			$("input[name=productpriceid4]").val(data.product_price_id_4);

			$("input[name=productpriceid5]").val(data.product_price_id_5);

            $("select[name=price1type]").val(data.price1type);

            $("select[name=price2type]").val(data.price2type);

            $("select[name=price3type]").val(data.price3type);

            $("select[name=price4type]").val(data.price4type);

            $("select[name=price5type]").val(data.price5type);

            $("p.gradetitle").text(data.gradetitle+' (this will be used as from price)');



            $("input[name=price1_1]").val(data.price1_1);

            $("input[name=price2_1]").val(data.price2_1);

            $("input[name=price3_1]").val(data.price3_1);

            $("select[name=price1type_1]").val(data.price1type_1);

            $("select[name=price2type_1]").val(data.price2type_1);

            $("select[name=price3type_1]").val(data.price3type_1);

            $("p.gradetitle_1").text(data.gradetitle_1);



            $("input[name=price1_2]").val(data.price1_2);

            $("input[name=price2_2]").val(data.price2_2);

            $("input[name=price3_2]").val(data.price3_2);

            $("select[name=price1type_2]").val(data.price1type_2);

            $("select[name=price2type_2]").val(data.price2type_2);

            $("select[name=price3type_2]").val(data.price3type_2);

            $("p.gradetitle_2").text(data.gradetitle_2);



            $("input[name=price1_3]").val(data.price1_3);

            $("input[name=price2_3]").val(data.price2_3);

            $("input[name=price3_3]").val(data.price3_3);

            $("select[name=price1type_3]").val(data.price1type_3);

            $("select[name=price2type_3]").val(data.price2type_3);

            $("select[name=price3type_3]").val(data.price3type_3);

            $("p.gradetitle_3").text(data.gradetitle_3);

            

            $("input[name=price1_4]").val(data.price1_4);

            $("input[name=price2_4]").val(data.price2_4);

            $("input[name=price3_4]").val(data.price3_4);

            $("select[name=price1type_4]").val(data.price1type_4);

            $("select[name=price2type_4]").val(data.price2type_4);

            $("select[name=price3type_4]").val(data.price3type_4);

            $("p.gradetitle_4").text(data.gradetitle_4);

            

            $("input[name=price1_5]").val(data.price1_5);

            $("input[name=price2_5]").val(data.price2_5);

            $("input[name=price3_5]").val(data.price3_5);

            $("select[name=price1type_5]").val(data.price1type_5);

            $("select[name=price2type_5]").val(data.price2type_5);

            $("select[name=price3type_5]").val(data.price3type_5);

            $("p.gradetitle_5").text(data.gradetitle_5);



            if(data.gradetitle.length) {

                $('.gradesornot').addClass("gradestrue");

            } else {

                $('.gradesornot').removeClass("gradestrue");

            }

    },'json');

}



function toggleOperation(obj) {

    var id = $(obj).val();



    $(".operation").hide();

    $("#op"+id).show();

}



function switchImage(img) {

    $("#mainproduct").attr("src", img);

}



function validateColour() {

    if ($("#frmColour").val() != '') {

        return 1;

    } else {

        $("#colourError").show().delay(5000).fadeOut(1000);

        return false;

    }

}



function validateColourFoot() {

    if ($("#frmColour").val() != '' && $("#frmfoot").val() != '') {

        return 1;

    } else {

        $("#colourError").show().delay(5000).fadeOut(1000);

        return false;

    }

}



function removeSwatch(obj) {

    $(obj).parent().parent().fadeOut().remove();

    if ($("#chosen-swatches li").length == 0) {

        $("#noswatches").show();

    }

    return false;

}



function validateStoreCollection() {



    var store = $("#store_collection_id").val();

    if (store != '') {

        return true;

    } else {

        alert('Please choose your preferred store for item collection');

        return false;

    }

}



function addRugDimension(pid) {



    var size = $("#size_id").val();

    var width = $("#width").val();

    var length = $("#length").val();

    var price1 = $("#price1").val();

    var price1t = $("#price1t").val();

    var price2 = $("#price2").val();

    var price2t = $("#price2t").val();

    var price3 = $("#price3").val();

    var price3t = $("#price3t").val();

    var next = $(".product-attrib").length;



    if (size != '' && width != '' && length != '' && price1 != '' && price1t != '') {

        $.post("/common/ajax/rug_sizes.php?p="+pid,'next='+next+'&size='+size+'&width='+width+'&length='+length+'&p1='+price1+'&p1t='+price1t+'&p2='+price2+'&p2t='+price2t+'&p3='+price3+'&p3t='+price3t, function(data) {

            if (data.success == 1) {

                $(".product-attrib.new").before(data.html);

                clearAttribs();

            }

        },"json");

    } else {

        alert("Please complete all required fields");

    }

}



function clearAttribs() {

    $("#size_id").val('');

    $("#width").val('');

    $("#length").val('');

    $("#price1").val('');

    $("#price1t").val('');

    $("#price2").val('');

    $("#price2t").val('');

    $("#price3").val('');

    $("#price3t").val('');

    $("#rug_colour").val('');

    $("#rug_swatch").val('');

}



function removeRugDimension(pid, id) {

    

    var response = confirm('Are you sure you want to delete this dimension / price?');



    if (response == true) {

        //$.post("/common/ajax/rug_sizes_del.php?p="+pid,'id='+id, function(data) {

        //    if (data.success == 1) {

                $("#rugattrib"+id).fadeOut(1000, function() {

                  $(this).remove();

                });

        //    }

        //},"json");

    }



    return false;

}



function addRugColour(pid) {



    var colour = $("#rug_colour").val();

    var swatch = $("#rug_swatch").val();

    var next = $(".product-rug-attrib").length;



    if (colour != '' && swatch != '') {

        $.post("/common/ajax/rug_colours.php?p="+pid,'next='+next+'&colour='+colour+'&swatch='+swatch, function(data) {

            if (data.success == 1) {

                $(".product-rug-attrib.new").before(data.html);

                clearAttribs();

            }

        },"json");

    } else {

        alert("Please enter a colour name and select a swatch from the dropdown list.");

    }

}

function removeRugColour(pid, id) {



    var response = confirm('Are you sure you want to delete this colour option?');



    if (response == true) {

        $("#rugcolour"+id).fadeOut(1000, function() {

            $(this).remove();

        });

    }



    return false;

}
