﻿        //-- Calculator code
        // provided by errumm ltd : http://www.errumm.co.uk
        
//common functions ...
        function uz(o)
        {
        
            if(isNaN(o))
            {
                alert("Please only use numeric values");
                return 0
            }
                
            return o == null ? 0 : Number(o);
        }


        function GetValueById(id)
        {
           return uz(document.getElementById(id).value);                
        }
        
        function SetDocText(id, value){
            document.getElementById(id).innerHTML = value;
        }
        
        function DecimalValue(value)
        {
            return Math.round(value*100)/100  
        }
        
        function DecimalAsString(value)
        {
	        var i = parseFloat(value);
	        if(isNaN(i)) { i = 0.00; }
	        var minus = '';
	        if(i < 0) { minus = '-'; }
	        i = Math.abs(i);
	        i = parseInt((i + .005) * 100);
	        i = i / 100;
	        s = new String(i);
	        if(s.indexOf('.') < 0) { s += '.00'; }
	        if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	        s = minus + s;
	        return s;
        }
//end common funcitons

//Step 1 Definitition
        function Step1()
        {
            this.OfficeRent, this.Travel, this.Office, this.Software, this.Communications, this.Insurance;
            this.LegalFees, this.Accounts, this.OfficeSupplies, this.Ads, this.Step1Other; 
            
            this.Total;
        }
        
        Step1.prototype.getValues = function()
        {
            this.OfficeRent = GetValueById("txtOfficeRent");
            this.Travel = GetValueById("txtTravel");
            this.Office = GetValueById("txtOffice");
            this.Software = GetValueById("txtSoftware");
            this.Communications = GetValueById("txtCommunications");
            this.Insurance = GetValueById("txtInsurance");
            this.LegalFees = GetValueById("txtLegalFees");
            this.Accounts = GetValueById("txtAccounts");
            this.OfficeSupplies = GetValueById("txtOfficeSupplies");
            this.Ads = GetValueById("txtAds");
            this.Step1Other = GetValueById("txtStep1Other");  
        }
        
        Step1.prototype.getTotal = function()
        {
            this.getValues();

            this.Total =  
                this.OfficeRent + this.Travel + this.Office + this.Software + this.Communications + this.Insurance +
                this.LegalFees + this.Accounts + this.OfficeSupplies + this.Ads + this.Step1Other;
            
            SetDocText("litTotalBusinessCosts", DecimalAsString(DecimalValue(this.Total)));
        }

//End Step 1

//Step 2 Definiton
        function Step2()
        {
            this.Rent, this.DailyExpenses, this.Retirement, this.OccasionalExpenses, this.AnythingElse;
            
            this.Total;
        }
        
        Step2.prototype.getValues = function()
        {
            this.Rent = GetValueById("txtRent");
            this.DailyExpenses = GetValueById("txtDailyExpenses");
            this.Retirement = GetValueById("txtRetirement");
            this.OccasionalExpenses = GetValueById("txtOccasionalExpenses");
            this.AnythingElse = GetValueById("txtAnythingElse");
        }
        
        Step2.prototype.getTotal = function()
        {
            this.getValues();

            this.Total =  
                this.Rent + this.DailyExpenses + this.Retirement + this.OccasionalExpenses + this.AnythingElse;
            
            SetDocText("litTotalPersonalExpenses", DecimalAsString(DecimalValue(this.Total)));
        }
//End Step 2

//Step 3 Definiton
        function Step3()
        {
            this.DaysAWeek, this.DaysVacation, this.SickDays, this.PublicHolidays, this.HoursADay, this.BilliableHoursPercent;
            
            this.DaysPerYear ;
            
            this.IsValid 
            
            this.Total;
        }
        
        Step3.prototype.getValues = function()
        {
            this.IsValid = true;
            this.DaysAWeek = GetValueById("txtDaysAWeek");
            this.DaysVacation = GetValueById("txtDaysVacation"); 
            this.SickDays = GetValueById("txtSickDays");
            this.PublicHolidays = GetValueById("txtPublicHolidays"); 
            this.HoursADay = GetValueById("txtHoursADay");
            this.BilliableHoursPercent = GetValueById("txtBilliableHoursPercent");
         
            // SOME VALIDATION            
            if(this.DaysAWeek > 7){
                this.DaysAWeek == 0;
                alert("You can't really work " + this.DaysAWeek + " days a week!");
                this.IsValid = false;
            }
            
            if(
                this.DaysVacation > 365 || 
                this.SickDays > 365 ||
                this.PublicHolidays  > 365
                )
            {
                this.DaysVacation  = 0; 
                this.SickDays = 0;
                this.PublicHolidays  = 0;            
                alert("There aren't that many days in a year!")
                this.IsValid = false;
            }
            
            if(this.HoursADay > 24)
            {
                this.HoursADay = 0;
                alert("There aren't that many hours in a day!")
                this.IsValid = false;
            }
            
            if(this.BilliableHoursPercent != 0){
                if(this.BilliableHoursPercent > 100 || this.BilliableHoursPercent < 0)
                {
                    this.BilliableHoursPercent == 100;
                    alert("Please make sure that you specify your percentage of billable hours correctly!")
                    this.IsValid = false;
                }
            }
            //END VALIDATION 
            if(this.IsValid)
                this.DaysPerYear = (this.DaysAWeek * 52) - (this.DaysVacation + this.SickDays + this.PublicHolidays);
        }
        
        Step3.prototype.getTotal = function()
        {
            this.getValues();

            if(this.BilliableHoursPercent == null){
                this.BilliableHoursPercent = 100;
                this.IsValid = false;
            }
            
            if(this.IsValid)
            {
                this.Total =  
                    ((this.DaysPerYear * this.HoursADay) / 100) *  this.BilliableHoursPercent;
            }
            else
            {
                this.DaysPerYear = 0;
                this.Total = 0;
            }
            
            SetDocText("litDaysAYear", this.DaysPerYear);
            
            SetDocText("litBillableHours", Math.round(this.Total));
        }
//End Step 3

// the main bit!
        var Step_1 = new Step1;
        var Step_2 = new Step2;
        var Step_3 = new Step3;
        
        function CalculateStep(step)
        {
            if(step == 1)            
                Step_1.getTotal();

            if(step == 2)            
                Step_2.getTotal();
            
            if(step == 3)                            
                Step_3.getTotal();
        }
        
        function CalculateProfit()
        {
            Step_1.getTotal();
            Step_2.getTotal();
            Step_3.getTotal();
            
            var Profit = GetValueById("txtProfit");
            var Costs = Step_1.Total + Step_2.Total; 
            var BreakEven = Costs / Step_3.Total;
            var Ideal = (Costs + Profit) / Step_3.Total;
            
            //alert("Costs = " + Costs);
            //alert("Step_3.Total = " + Step_3.Total);
            
            if(Step_3.Total <= 0)
            {
                alert("You haven't specified how many hours you are working in Step 3!");                
            }
            else
            {
                SetDocText("litBreakEven", DecimalAsString(DecimalValue(BreakEven)));
                SetDocText("litIdeal", DecimalAsString(DecimalValue(Ideal)));
            }

            
        }
//End 
        //-->    

