//Calculator class Calculator = function(strFormID) { //Validate fields this.validate = function() { //Init local variables var strErrorMessage = ''; var strInvalidItem = ''; var blnIsValid = true; var form = this.getForm(); //Validate step by step if (form.WeeklyHours.value.length == 0) { strErrorMessage = 'Please enter your weekly hours!'; strInvalidItem = 'WeeklyHours'; } else if (isNaN(form.WeeklyHours.value)) { strErrorMessage = 'Please check your weekly hours - non numeric value!'; strInvalidItem = 'WeeklyHours'; } else if (form.HourlyRate.value.length == 0) { strErrorMessage = 'Please enter your hourly rate!'; strInvalidItem = 'HourlyRate'; } else if (isNaN(form.HourlyRate.value)) { strErrorMessage = 'Please check your hourly rate - non numeric value!'; strInvalidItem = 'HourlyRate'; } else if (form.WeeklyExpenses.value.length == 0) { strErrorMessage = 'Please enter your weekly expenses!'; strInvalidItem = 'WeeklyExpenses'; } else if (isNaN(form.WeeklyExpenses.value)) { strErrorMessage = 'Please check your weekly expenses - non numeric value!'; strInvalidItem = 'WeeklyExpenses'; } //Validate if(strErrorMessage) { //Write the error this.writeError(strErrorMessage); //Set the focus to invalid item form.elements[strInvalidItem].focus(); //Set Invalid blnIsValid = false; } //Return return blnIsValid; } //Calculate the amounts and display their values this.calculate = function() { //Hide errors this.hideErrors(); //Validate the fields if(!this.validate()) { return false; } //Get the forms var form = this.getForm(); var illustrationForm = this.getIllustrationForm(); //Init local variables var floatWeeklyHours = this.cleanFloat(form.WeeklyHours.value); var floatHourlyRate = this.cleanFloat(form.HourlyRate.value); var floatWeeklyExpenses = this.cleanFloat(form.WeeklyExpenses.value); var floatFee = this.cleanFloat(form.Fee.value); var floatPension = this.cleanFloat(form.Pension.value); var intTaxCodeNumber = form.TaxCodeNumber.value; var strTaxCodeLetter = form.TaxCodeLetter.value; var intPAYEWeeks = form.PAYEWeeks.value; //Set illustrations hidden values illustrationForm.WeeklyHours.value = floatWeeklyHours; illustrationForm.HourlyRate.value = floatHourlyRate; illustrationForm.Fee.value = floatFee; illustrationForm.WeeklyExpenses.value = floatWeeklyExpenses; illustrationForm.Pension.value = floatPension; illustrationForm.TaxCodeNumber.value = intTaxCodeNumber; illustrationForm.TaxCodeLetter.value = strTaxCodeLetter; illustrationForm.PAYEWeeks.value = intPAYEWeeks; //Update description values $('#idPAYEWeeksForDesc').attr('innerHTML',intPAYEWeeks); $('#idTaxCodeForDesc').attr('innerHTML',intTaxCodeNumber + '' + strTaxCodeLetter); if(floatPension > 0) { $('#idPensionContainerForDesc').show(); $('#idPensionForDesc').attr('innerHTML',floatPension); } //Calculate with JSON-AJAX $.getJSON('http://www.ibalance.co.uk/calcs/calculator.asp?in_callback=?', {in_calcType:'umbrellaVPAYE',in_hours:floatWeeklyHours,in_rate:floatHourlyRate,in_expenses:floatWeeklyExpenses,in_fee:floatFee,pension:floatPension,TaxCodeNo:intTaxCodeNumber,TaxCodeLetter:strTaxCodeLetter,NoPeriods:intPAYEWeeks,autocalc:'true'}, function(data) { //Calculator object var objCalculator = new Calculator(); //Init values var floatPayeTakeHome = objCalculator.cleanFloat(data.ag_takeHome); var floatOurTakeHome = objCalculator.cleanFloat(data.um_takeHome); var floatOurVsPaye = objCalculator.cleanFloat(data.increasePrecent); var floatOurSaving = objCalculator.cleanFloat(data.increasePerYear); var floatPaye = objCalculator.cleanFloat(data.um_paye); var floatNIC = objCalculator.cleanFloat(data.um_nic); var floatWeeklyHours = objCalculator.cleanFloat(form.WeeklyHours.value); var floatGross = floatWeeklyHours * form.HourlyRate.value; if(data.um_expensesRestriction == 'TRUE') { $('#idExpensesNotes > td').html('Due to National Minimum Wage a maximum of £' + data.um_expensesLimit + ' of expenses can be considered.'); $('#idExpensesNotes').fadeIn(); } else { $('#idExpensesNotes').hide(); } $('#idOurValues').animate({ 'opacity': 0 },250 ); $('#idOurValues').animate({ 'opacity': 1 },250 ); //Write illustration details $('#idHourlyRateForIllustration').attr('innerHTML','£' + objCalculator.formatNumber(floatHourlyRate)); $('#idWeeklyHoursForIllustration').attr('innerHTML',objCalculator.formatNumber(floatWeeklyHours)); $('#idGross').attr('innerHTML','£' + objCalculator.formatNumber(floatGross)); $('#idPAYE').attr('innerHTML','-£' + objCalculator.formatNumber(Math.abs(floatPaye))); $('#idNIC').attr('innerHTML','-£' + objCalculator.formatNumber(Math.abs(floatNIC))); $('#idOurFee').attr('innerHTML','-£' + objCalculator.formatNumber(Math.abs(floatFee))); $('#idTakeHomePay').attr('innerHTML','£' + objCalculator.formatNumber(floatOurTakeHome)); $('#idTakeHomePayPercent').attr('innerHTML',objCalculator.formatNumber(floatOurTakeHome/floatGross*100)); //Calculate illustration percentages var floatTakeHomePayPercentage = parseInt(floatOurTakeHome/floatGross*100); var floatNICPercentage = parseInt(floatNIC/floatGross*100); var floatOurFeePercentage = parseInt(floatFee/floatGross*100); //Get the illustration chart //var strChartUrl = 'http://chart.apis.google.com/chart?cht=p3&chs=520x190&chf=bg,s,f3efef&chma=0,0,0,0&chd=t:'+floatTakeHomePayPercentage+','+floatNICPercentage+','+floatOurFeePercentage+'&chl=Take Home Pay|NIC|Redego Fee'; var strChartUrl = 'http://chart.apis.google.com/chart?cht=bhg&chs=520x130&chf=bg,s,f3efef&chm=N,000000,0,-1,12,,l:-40&chco=aca4a4|cc0000|b8b5b5&chxt=x,y&chxr=0,0,' + (floatGross + 50) + '&chxl=1:|Agency*|Redego|Gross&chd=t:' + floatGross + ',' + floatOurTakeHome + ',' + floatPayeTakeHome + '&chds=0,' + (floatGross + 50); //Insert the chart image $('#idIllustrationChart').attr('src',strChartUrl); } ); //Rename calculate button $('#idCalculateButton').val('Recalculate'); //Display illustration this.displayIllustration(); //Go to anchor window.location = '#Calculator'; } //Init email field this.initEmailField = function() { //Check current value if($('#idEmail').val() == '' || $('#idEmail').val() == $('#idEmail').attr('title')) //No email address entered or it has the default value { $('#idEmail').val( $('#idEmail').attr('title') ); $('#idEmail').addClass('watermark'); } else //Email address already entered { $('#idEmail').removeClass('watermark'); } } //Todos onfocus email field this.onfocusEmailField = function() { //Check current value if($('#idEmail').val() == $('#idEmail').attr('title')) //No email address entered or it has the default value { $('#idEmail').val(''); } //Remove watermark $('#idEmail').removeClass('watermark'); } //Display illustration this.displayIllustration = function() { $('#idCalculatorIllustration').show(); this.displayChart(); } //Display chart with animation this.displayChart = function() { $('#idIllustrationChart').hide(); $('#idIllustrationChart').fadeIn(2000); } //Send illustration as an email this.sendIllustration = function() { //Init local variables var strEmail = $('#idEmail').val(); //Check email address if(strEmail.length = 0) { this.writeEmailError('Please enter your email address!'); return false; } else if(!isEmailAddress(strEmail)) { this.writeEmailError('Please enter a valid email address!'); return false; } //Everything is ok return true; } //Hide email sent message (and display email field again) this.hideEmailSent = function(strMethod) { if(!strMethod) { strMethod = 'slow'; } $('#idIllustrationSent').fadeOut(strMethod); } //Clean float this.cleanFloat = function(mixNumber) { return parseFloat(mixNumber.replace(',','')); } //Format number as we like this.formatNumber = function(floatNumber,intDecimalDigits) { if(isNaN(intDecimalDigits)) { intDecimalDigits = 2; } return this.addCommas(floatNumber.toFixed(intDecimalDigits)); } //Add commas as thousand separator this.addCommas = function(nStr) { nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; } //Write the error this.writeError = function(strErrorMessage,strErrorContainerID) { //Get the container if(!strErrorContainerID) { strErrorContainerID = 'CalculatorError'; } //Set the errormessage $('#' + strErrorContainerID + ' > td:first').attr('innerHTML',strErrorMessage); //Show error the container $('#' + strErrorContainerID).show(); } //Email error happened this.writeEmailError = function(strErrorMessage) { this.writeError(strErrorMessage,'EmailError'); $('#idEmail').focus(); } //Hide error this.hideErrors = function() { $('#CalculatorError').hide(); $('#EmailError').hide(); } //Get the calculator form this.getForm = function() { return document.forms[this.formID]; } //Get the illustration form this.getIllustrationForm = function() { return document.forms['idCalculatorIllustration']; } //Constructor this.construct = function() { this.formID = strFormID; } //Run the constructor automatically on object creating this.construct(); }