﻿$(document).ready(function () {
    $('#navDefault').addClass("selected");
    $('#featuresButton').click(function () {
        window.location.href = '/SignUp.aspx';
        return false;
    });
    $(".numeric").numeric();
    $(".integer").numeric(false, function () { alert("Integers only"); this.value = ""; this.focus(); });
    $("#theForm").keypress(function (e) {
        if (e.which == 13) {
            return false;
        }
    });
    $("#employeeCalcResults").hide();

    // employee savings calculations

    required = ["intEmployees"];
    errornotice = $("#formError");
    emptyerror = "Please fill out this field.";

    // if there is a cookie, grab the number of employees

    intNumberOfEmployees = getCookie('ckEmployees');

    if (intNumberOfEmployees != null) {
        $('#intEmployees').val(intNumberOfEmployees);
    }


    $('#btnCalculate').click(function () {
        //Validate required fields
        for (i = 0; i < required.length; i++) {
            var input = $('#' + required[i]);
            if ((input.val() == "") || (input.val() == emptyerror)) {
                input.addClass("needsfilled");
                input.val(emptyerror);
                errornotice.fadeIn(750);
            } else {
                input.removeClass("needsfilled");
            }
        }

        if ($(":input").hasClass("needsfilled")) {
            return false;
        } else {
            errornotice.hide();
            $("#employeeCalcResults").show();
            // do the form calculations 
            numberOfEmployees = parseFloat($('#intEmployees').val());
            if (numberOfEmployees < 20) {
                numberOfEmployees = 20;
                $('#intEmployees').val("20");
                $('#employeeError').addClass("error");
            } else {
                $('#employeeError').removeClass();
            }


            // fill in the participants first (number of employees * normal uptake)
            CCVParticipants = Math.ceil(numberOfEmployees * .03);
            PhonesParticipants = Math.ceil(numberOfEmployees * .1);
            CycleParticipants = Math.ceil(numberOfEmployees * .04);
            ComputersParticipants = Math.ceil(numberOfEmployees * .05);
            HealthParticipants = Math.ceil(numberOfEmployees * .03);
            DriveParticipants = Math.ceil(numberOfEmployees * .05);

            $('#tblCellCCVParticpants').html(CCVParticipants);
            $('#tblCellPhonesParticipants').html(PhonesParticipants);
            $('#tblCellCycleParticipants').html(CycleParticipants);
            $('#tblCellComputersParticipants').html(ComputersParticipants);
            $('#tblCellHealthParticipants').html(HealthParticipants);
            $('#tblCellDriveParticipants').html(DriveParticipants);

            // fill out the individual amounts (static)
            CCVIndividual = 933;
            PhonesIndividual = 96;
            CycleIndividual = 224;
            ComputersIndividual = 67;
            HealthIndividual = 128;
            DriveIndividual = 1089;

            $('#tblCellCCVIndividual').html(CCVIndividual).formatCurrency({ region: 'en-GB' });
            $('#tblCellPhonesIndividual').html(PhonesIndividual).formatCurrency({ region: 'en-GB' });
            $('#tblCellCycleIndividual').html(CycleIndividual).formatCurrency({ region: 'en-GB' });
            $('#tblCellComputersIndividual').html(ComputersIndividual).formatCurrency({ region: 'en-GB' });
            $('#tblCellHealthIndividual').html(HealthIndividual).formatCurrency({ region: 'en-GB' });
            $('#tblCellDriveIndividual').html(DriveIndividual).formatCurrency({ region: 'en-GB' });

            // calculate total individual savings (participants * static amount)
            CCVEmployeeTotal = Math.ceil(CCVParticipants * CCVIndividual);
            PhonesEmployeeTotal = Math.ceil(PhonesParticipants * PhonesIndividual);
            CycleEmployeeTotal = Math.ceil(CycleParticipants * CycleIndividual);
            ComputersEmployeeTotal = Math.ceil(ComputersParticipants * ComputersIndividual);
            HealthEmployeeTotal = Math.ceil(HealthParticipants * HealthIndividual);
            DriveEmployeeTotal = Math.ceil(DriveParticipants * DriveIndividual);

            $('#tblCellCCVEmployeeTotal').html(CCVEmployeeTotal).formatCurrency({ region: 'en-GB' });
            $('#tblCellPhonesEmployeeTotal').html(PhonesEmployeeTotal).formatCurrency({ region: 'en-GB' });
            $('#tblCellCycleEmployeeTotal').html(CycleEmployeeTotal).formatCurrency({ region: 'en-GB' });
            $('#tblCellComputersEmployeeTotal').html(ComputersEmployeeTotal).formatCurrency({ region: 'en-GB' });
            $('#tblCellHealthEmployeeTotal').html(HealthEmployeeTotal).formatCurrency({ region: 'en-GB' });
            $('#tblCellDriveEmployeeTotal').html(DriveEmployeeTotal).formatCurrency({ region: 'en-GB' });

            // calculate the NIC (participants * total amount * NI rate)
            NICRate = .138;
            CCVNIC = Math.ceil(CCVParticipants * 2916 * NICRate);
            PhonesNIC = Math.ceil(PhonesParticipants * 300 * NICRate);
            CycleNIC = Math.ceil(CycleParticipants * 500 * NICRate);
            ComputersNIC = Math.ceil(ComputersParticipants * 333.33 * NICRate);
            HealthNIC = Math.ceil(HealthParticipants * 400 * NICRate);
            DriveNIC = Math.ceil(DriveParticipants * 4805 * NICRate);

            $('#tblCellCCVNIC').html(CCVNIC).formatCurrency({ region: 'en-GB' });
            $('#tblCellPhonesNIC').html(PhonesNIC).formatCurrency({ region: 'en-GB' });
            $('#tblCellCycleNIC').html(CycleNIC).formatCurrency({ region: 'en-GB' });
            $('#tblCellComputersNIC').html(ComputersNIC).formatCurrency({ region: 'en-GB' });
            $('#tblCellHealthNIC').html(HealthNIC).formatCurrency({ region: 'en-GB' });
            $('#tblCellDriveNIC').html(DriveNIC).formatCurrency({ region: 'en-GB' });

            // calculate the VAT reclaim
            VATRate = .2;
            CCVVAT = "n/a";
            PhonesVAT = Math.ceil(PhonesParticipants * 300 * VATRate);
            CycleVAT = Math.ceil(CycleParticipants * 700 * VATRate);
            ComputersVAT = Math.ceil(ComputersParticipants * 333.33 * (VATRate / 2));
            HealthVAT = "n/a";
            DriveVAT = Math.ceil(((DriveParticipants * 3930.83) * (VATRate / 2)) + (DriveParticipants * 370 * VATRate));

            $('#tblCellCCVVAT').html(CCVVAT);
            $('#tblCellPhonesVAT').html(PhonesVAT).formatCurrency({ region: 'en-GB' });
            $('#tblCellCycleVAT').html(CycleVAT).formatCurrency({ region: 'en-GB' });
            $('#tblCellComputersVAT').html(ComputersVAT).formatCurrency({ region: 'en-GB' });
            $('#tblCellHealthVAT').html(HealthVAT);
            $('#tblCellDriveVAT').html(DriveVAT).formatCurrency({ region: 'en-GB' });

            // calculate the total employer savings
            CCVEmployerTotal = CCVNIC;
            PhonesEmployerTotal = PhonesNIC + PhonesVAT;
            CycleEmployerTotal = CycleNIC + CycleVAT;
            ComputersEmployerTotal = ComputersNIC + ComputersVAT;
            HealthEmployerTotal = HealthNIC;
            DriveEmployerTotal = DriveNIC + DriveVAT;

            $('#tblCellCCVEmployerTotal').html(CCVEmployerTotal).formatCurrency({ region: 'en-GB' });
            $('#tblCellPhonesEmployerTotal').html(PhonesEmployerTotal).formatCurrency({ region: 'en-GB' });
            $('#tblCellCycleEmployerTotal').html(CycleEmployerTotal).formatCurrency({ region: 'en-GB' });
            $('#tblCellComputersEmployerTotal').html(ComputersEmployerTotal).formatCurrency({ region: 'en-GB' });
            $('#tblCellHealthEmployerTotal').html(HealthEmployerTotal).formatCurrency({ region: 'en-GB' });
            $('#tblCellDriveEmployerTotal').html(DriveEmployerTotal).formatCurrency({ region: 'en-GB' });

            // and finally the overall totals
            ParticipantsTotal = CCVParticipants + PhonesParticipants + CycleParticipants + ComputersParticipants + HealthParticipants + DriveParticipants;
            IndividualTotal = CCVIndividual + PhonesIndividual + CycleIndividual + ComputersIndividual + HealthIndividual + DriveIndividual;
            EmployeeTotal = CCVEmployeeTotal + PhonesEmployeeTotal + CycleEmployeeTotal + ComputersEmployeeTotal + HealthEmployeeTotal + DriveEmployeeTotal;
            NICTotal = CCVNIC + PhonesNIC + CycleNIC + ComputersNIC + HealthNIC + DriveNIC;
            VATTotal = PhonesVAT + CycleVAT + ComputersVAT + DriveVAT;
            EmployerTotal = CCVEmployerTotal + PhonesEmployerTotal + CycleEmployerTotal + ComputersEmployerTotal + HealthEmployerTotal + DriveEmployerTotal;

            $('#tblCellParticipantsTotal').html(ParticipantsTotal);
            $('#tblCellIndividualTotal').html(IndividualTotal).formatCurrency({ region: 'en-GB' });
            $('#tblCellEmployeeTotal').html(EmployeeTotal).formatCurrency({ region: 'en-GB' });
            $('#tblCellNICTotal').html(NICTotal).formatCurrency({ region: 'en-GB' });
            $('#tblCellVATTotal').html(VATTotal).formatCurrency({ region: 'en-GB' });
            $('#tblCellEmployerTotal').html(EmployerTotal).formatCurrency({ region: 'en-GB' });

            //finally set a cookie with the number of employees
            setCookie('ckEmployees', numberOfEmployees, 1);

            return true;
        }
    });

    // Clears any fields in the form when the user clicks on them
    $(":input").focus(function () {
        if ($(this).hasClass("needsfilled")) {
            $(this).val("");
            $(this).removeClass("needsfilled");
        }
    });

    // push the button so the table appears onload
    $('#btnCalculate').click();

    //Load the slideshow
    theRotator();
    $('div.rotator').fadeIn(1000);
    $('div.rotator ul li').fadeIn(1000); // tweek for IE

    // load the quote
    /* Pick random number between 1 and 4 */
    var randomNum = Math.ceil(Math.random() * 2);
    $.each([1, 2], function () {
        var $div = $('#quoteDiv' + this);
        if (this != randomNum) $div.hide();
    });



});


function theRotator() {
    //Set the opacity of all images to 0
    $('div.rotator ul li').css({ opacity: 0.0 });

    //Get the first image and display it (gets set to full opacity)
    var list = $("div.rotator ul li").toArray();
    var elemlength = list.length;
    var randomnum = Math.floor(Math.random() * elemlength);
    var randomitem = list[randomnum];

    $(randomitem).css({ opacity: 1.0 });

    //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds

    setInterval('rotate()', 6000);

}

function rotate() {
    //Get the first image
    var randomPic = Math.ceil(Math.random() * 9);
    var current = ($('div.rotator ul li.show') ? $('div.rotator ul li.show') : $('div.rotator ul li:first'));

    if (current.length == 0) current = $('div.rotator ul li:first');

    //Get next image, when it reaches the end, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator ul li:first') : current.next()) : $('div.rotator ul li:first'));

    //Un-comment the 3 lines below to get the images in random order

    var sibs = current.siblings();
    var rndNum = Math.floor(Math.random() * sibs.length);
    var next = $(sibs[rndNum]);


    //Set the fade in effect for the next image, the show class has higher z-index
    next.css({ opacity: 0.0 })
	        .addClass('show')
	        .animate({ opacity: 1.0 }, 1000);

    //Hide the current image
    current.animate({ opacity: 0.0 }, 1000)
	        .removeClass('show');

};

function setCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

