﻿
$(document).ready(function() {
if ($(".jsShowControl").length > 0) {

$(".jsShowControl").show();
    var loandays = $(".jqhdloandays").val();
    var loanamt = $(".jqhdloanamount").val();   
    var offset = $(".jqhdoffsettop").val();
    var isIE7 = getIEVersionNumber() == 7;

    positionSlider(loanamt);
    positionDaySliderDefault(10);
    $(".dayslider-control").find(".ui-slider-handle").addClass("day-slider-handle");
    positionDaySlider(loandays);

    $(".slider-amount").position({
        my: "left",
        at: "top",
        of: ".ui-slider-handle",
        offset: "-15 -12"
    });
   
    if ((isIE7) && (offset == '60')) {        
        $(".slider-days").position({
            my: "left",
            at: "top",
            of: ".day-slider-handle",
            offset: "-15 60"
        });


    }
    else {
        $(".slider-days").position({
            my: "left",
            at: "top",
            of: ".day-slider-handle",
            offset: "-15 -12"
        });
    }


};
});

function positionSlider(currentVal) {
    var creditlimit = $(".creditlimit").val();
    $(".slider-control").slider({
        value: currentVal,
        animate: true,
        min: 50,
        max: creditlimit,
        step: 5,
        slide: function(event, ui) {
            calculateIntrest();

            $(".slider-amount").position({
                my: "left",
                at: "top",
                of: ".ui-slider-handle",
                offset: "-15 -12"
            });

        }
        ,change: function(event, ui) {
        calculateIntrest();
        $(".nonworking-error").hide(); 
        $(".slider-amount").position({
        my: "left",
        at: "top",
        of: ".ui-slider-handle",
        offset: "-15 -12"
        });

        }
    });
    
    calculateIntrest();
    $(".slider-amount").position({
        my: "left",
        at: "top",
        of: ".ui-slider-handle",
        offset: "-15 -12"
    });
    
}

function positionDaySliderDefault(currentVal) {
    var daylimit = $(".numberofdays").val();
   
    $(".dayslider-control").slider({
        value: currentVal,
        animate: true,
        min: 1,
        max: daylimit,
        step: 1,
        slide: function(event, ui) {
            calculateIntrest();
            
            $(".slider-days").position({
                my: "left",
                at: "top",
                of: ".ui-slider-handle",
                offset: "-15 -12"
            });

        }
    });
    
    calculateIntrest();
    $(".slider-days").position({
        my: "left",
        at: "top",
        of: ".ui-slider-handle",
        offset: "-15 -12"
    });

}


function positionDaySlider(currentVal) {
    var daylimit = $(".numberofdays").val();
    
    $(".dayslider-control").slider({
        value: currentVal,
        animate: true,
        min: 1,
        max: daylimit,
        step: 1,
        slide: function(event, ui) {
            calculateIntrest();           
            $(".slider-days").position({
                my: "left",
                at: "top",
                of: ".day-slider-handle",
                offset: "-15 -12"
            });

        },
        change: function(event, ui) {
        calculateIntrest();
        $(".nonworking-error").hide();             
            $(".slider-days").position({
                my: "left",
                at: "top",
                of: ".day-slider-handle",
                offset: "-15 -12"
            });

        }
    });
   
    calculateIntrest();
    $(".slider-days").position({
        my: "left",
        at: "top",
        of: ".day-slider-handle",
        offset: "-15 -12"
    });

}

function decreaseval() {

    var currentVal = $(".slider-control").slider("value");
    if (currentVal > 50) {
        currentVal = currentVal - 5;
        positionSlider(currentVal);
    }
       
}

function increaseval() {

    var currentVal = $(".slider-control").slider("value");
    var creditlimit = $(".creditlimit").val();
    if (currentVal < creditlimit) {
        currentVal = currentVal + 5;
        positionSlider(currentVal);
    }
}

function decreasedayval() {

    var currentVal = $(".dayslider-control").slider("value");
    if (currentVal > 1) {
        currentVal = currentVal - 1;
        positionDaySlider(currentVal);
    }

}

function increasedayval() {

    var currentVal = $(".dayslider-control").slider("value");
    var Daylimit = $(".numberofdays").val();
    if (currentVal < Daylimit) {
        currentVal = currentVal + 1;
        positionDaySlider(currentVal);
    }
}


function calculateIntrest() {

    var currentVal = $(".slider-control").slider("value");
    $(".slider-amount").val("£" + currentVal);

    var currentDayVal = $(".dayslider-control").slider("value");
    
    $(".slider-days").val(currentDayVal + "days");
    $(".jqhdloandays").val(currentDayVal);
    $(".jqhdloanamount").val(currentVal);
    var interest = ((1 * currentVal / 100) * currentDayVal);
    interest = interest + 3.56;
    var formated_interest = interest;    
    var total = currentVal + formated_interest;
    $(".jsdisplayAmt").val("£" + currentVal);
    $(".jstotalRepayable").val(total);
    $(".jstotalRepayablenote").val(total);
    $(".jschargesnote").val(formated_interest);
    $(".jstotalRepayable").format({ format: "#,###.00", locale: "gb" });
    $(".jstotalRepayablenote").format({ format: "#,###.00", locale: "gb" });
    $(".jstotalRepayablenote").attr("width", 62);
    $(".jstotalRepayablenote").width(62);
    if (total > 999)
        $(".jstotalRepayablenote").width(75);
    $(".jschargesnote").format({ format: "#,###.00", locale: "gb" });
    $(".jschargesnote").width(50);
    $(".jschargesnote").attr("width", 50);
    if (interest > 99)
        $(".jschargesnote").width(62);
   
}

function getIEVersionNumber() {
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    
    if (MSIEOffset == -1) {
        return 0;
    } else {
        return 7;
    }
}

