pc = {
    price: 30000,
    downPayment: 0,
    tradeInAllowance: 0,
    loanTermInMonths: 60,
    annualInterestRate: .085,
    monthlyPayment: 0,
    currencyOffset: 1,
    box: {
        main: null,
        init: function() {
            pc.box.main = document.getElementById("PaymentCalculator");
            pc.addShowHide(pc.box.main);
        }
    },
    fld: {
        price: null,
        downPaymentInDollars: null,
        tradeInAllowance: null,
        loanTermInMonths: null,
        annualInterestRate: null,
        monthlyPayment: null,
        monthlyPaymentAlt: null,
        getEntryFields: function() { var f = pc.fld; return new Array(f.price, f.downPaymentInDollars, f.tradeInAllowance, f.loanTermInMonths, f.annualInterestRate); },
        init: function() {
            pc.fld.price = document.getElementById("pcPrice_Dollars");
            pc.fld.downPaymentInDollars = document.getElementById("pcDownPayment_Dollars");
            pc.fld.tradeInAllowance = document.getElementById("pcTradeIn_Dollars");
            pc.fld.loanTermInMonths = document.getElementById("pcLoanTerm_Months");
            pc.fld.annualInterestRate = document.getElementById("pcAnnualInterestRate");
            pc.fld.monthlyPayment = document.getElementById("pcMonthlyPayment");
            pc.fld.monthlyPaymentAlt = document.getElementById("pcMonthlyPaymentAlt");
            var flds = pc.fld.getEntryFields();
            for (var x = 0; x < flds.length; x++) {
                flds[x].onfocus = pc.onSetFocus;
                flds[x].onblur = pc.onSetBlur;
            }
            pc.fld.price.onchange = function(e) {
                pc.setPrice(this.value);
                pc.calculate();
            }
            pc.fld.downPaymentInDollars.onchange = function(e) {
                pc.setDownPaymentInDollars(this.value);
                pc.calculate();
            }
            pc.fld.tradeInAllowance.onchange = function(e) {
                pc.setTradeInAllowance(this.value);
                pc.calculate();
            }
            pc.fld.loanTermInMonths.onchange = function(e) {
                pc.setLoanTermInMonths(this.value);
                pc.calculate();
            }
            pc.fld.annualInterestRate.onchange = function(e) {
                pc.setAnnualInterestRate(this.value);
                pc.calculate();
            }
            pc.fld.monthlyPayment.onchange = function(e) {
                pc.calculate();
            }
            pc.fld.monthlyPaymentAlt.onchange = function(e) {
                pc.calculate();
            }
        }
    },
    cmd:
    {
        refresh: null,
        close: null,
        init: function() {
            pc.cmd.refresh = document.getElementById("pcCalculateButton");
            pc.cmd.close = document.getElementById("pcCloseButton");
            pc.cmd.close.onclick = pc.hide;
            pc.cmd.refresh.onclick = pc.calculate;
        }
    },
    icons:
    {
        flag: null,
        flagAlt: null,
        init: function() {
            pc.icons.flag = document.getElementById("Flag");
            pc.icons.flagAlt = document.getElementById("FlagAlt");
        }
    },
    setPrice: function(num) {
        pc.price = pc.parseCurrency(num);
        pc.fld.price.value = pc.formatCurrency(num);
        if (pc.price < pc.downPayment) {
            pc.setDownPaymentInDollars(pc.price);
        }
    },
    setDownPaymentInDollars: function(num) {
        pc.downPayment = pc.parseCurrency(num);
        if (pc.price < (pc.downPayment + pc.tradeInAllowance)) {
            if (pc.tradeInAllowance > pc.price) {
                pc.downPayment = 0;
            }
            else {
                pc.downPayment = pc.price - pc.tradeInAllowance;
            }
        }
        pc.fld.downPaymentInDollars.value = pc.formatCurrency(pc.downPayment);
    },
    setTradeInAllowance: function(num) {
        pc.tradeInAllowance = pc.parseCurrency(num);
        if (pc.price < (pc.downPayment + pc.tradeInAllowance)) {
            if (pc.tradeInAllowance > pc.price) {
                pc.downPayment = 0;
            }
            else {
                pc.setDownPaymentInDollars(pc.price - pc.tradeInAllowance);
            }
        }
        pc.fld.tradeInAllowance.value = pc.formatCurrency(num);
    },
    setLoanTermInMonths: function(num) {
        num = Math.round(num);
        if (isNaN(num) || num == 0) {
            num = 1;
        }
        pc.loanTermInMonths = num;
        pc.fld.loanTermInMonths.value = pc.loanTermInMonths;
    },
    setAnnualInterestRate: function(num) {
        num = num.toString().replace(/\$|\%|\,/g, '');
        num = ((Math.round(num * Math.pow(10, 5))) / Math.pow(10, 5));
        if (isNaN(num) || num == 0) {
            num = 1;
        }
        pc.annualInterestRate = num;
    },
    setMonthlyPayment: function(num) {
        var annualRate = (pc.annualInterestRate / 12);
        var monthlyPayment = pc.parseCurrency(num);
        pc.fld.monthlyPayment.value = monthlyPayment;
        var salesPrice = (monthlyPayment / ((annualRate + (annualRate / ((Math.pow((annualRate + 1), pc.loanTermInMonths)) - 1))))) + ((pc.tradeInAllowance + pc.downPayment) * (1 + (pc.salesTax / Math.pow(10, 2))));
        pc.setPrice(salesPrice);
    },
    init: function(flagSource, flagAltSource, currencyOffset) {
        pc.box.init();
        pc.fld.init();
        pc.cmd.init();
        pc.icons.init();
        pc.icons.flag.src = flagSource;
        pc.icons.flagAlt.src = flagAltSource;
        pc.currencyOffset = currencyOffset;
        pc.refresh('100000', '20', '120', '8');
    },
    refresh: function(numPrice, numLoanTermInMonths, numAnnualInterestRate) {
        pc.setPrice(numPrice);
        pc.setDownPaymentInDollars(0);
        pc.setLoanTermInMonths(numLoanTermInMonths);
        pc.setAnnualInterestRate(numAnnualInterestRate);
        pc.calculate();
    },
    calculate: function() {
        var annualRate = ((pc.annualInterestRate / Math.pow(10, 2)) / 12);
        var modifiedPrice = (pc.price < pc.tradeInAllowance) ? 0 : (pc.price - pc.tradeInAllowance);
        modifiedPrice = (modifiedPrice - pc.downPayment);
        var result = ((annualRate + (annualRate / ((Math.pow((annualRate + 1), pc.loanTermInMonths)) - 1))) * (modifiedPrice))
        pc.fld.downPaymentInDollars.value = pc.formatCurrency(pc.downPayment);
        pc.fld.tradeInAllowance.value = pc.formatCurrency(pc.tradeInAllowance);
        pc.fld.loanTermInMonths.value = pc.loanTermInMonths;
        pc.fld.annualInterestRate.value = ((Math.round(pc.annualInterestRate * Math.pow(10, 5))) / Math.pow(10, 5));
        pc.fld.monthlyPayment.value = pc.formatCurrency(result);
        pc.fld.monthlyPaymentAlt.value = pc.formatCurrency(result * pc.currencyOffset);
    },
    onSetFocus: function() {
        pc.insertClass(this, "focus");
    },
    onSetBlur: function() {
        pc.removeClass(this, "focus");
    },
    addShowHide: function(element) {
        if (element != null) {
            element.show = function() { pc.showIt(this); };
            element.hide = function() { pc.hideIt(this); };
        }
    },
    showIt: function(element) {
        pc.removeClass(element, "Hide");
    },
    hideIt: function(element) {
        pc.insertClass(element, "Hide");
    },
    insertClass: function(element, className) {
        if (typeof element.className != "undefined" && element.className.indexOf(className) == -1)
            element.className += " " + className;
    },
    removeClass: function(element, className) {
        if (typeof element.className != "undefined")
            element.className = element.className.replace(new RegExp(" " + className + "\\b"), "");
    },
    elementOffset: function(obj) {
        var curleft = curtop = 0;
        if (typeof obj != 'undefined' && obj != null && obj.offsetParent) {
            curleft = obj.offsetLeft;
            curtop = obj.offsetTop;
            while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft;
                curtop += obj.offsetTop;
            }
        }
        return { x: curleft, y: curtop };
    },
    show: function(element) 
    {
        pc.move(element, pc.box.main);
        pc.box.main.show()
        return false;
    },
    hide: function() {
        pc.box.main.hide();
        return false;
    },
    move:function(clicked, target) {
        var pos = $(clicked).position();
        var ctx = { t:$(document).scrollTop(), l:$(document).scrollLeft(), 
            h:$(window).height(), w:$(window).width(), 
            b:0, r:0, fh:319, fw:310 };
        ctx.b = ctx.t + ctx.h;
        ctx.r = ctx.l + ctx.w;
        pos.top = pos.top - (ctx.fh / 2);
        pos.left = pos.left - ctx.fw;
        pos.top = (pos.top + ctx.fh > ctx.b) ? ctx.b - ctx.fh : pos.top;
        pos.left = (pos.left + ctx.fw > ctx.r) ? ctx.r - ctx.fw : pos.left;
        pos.top = (pos.top < ctx.t) ? ctx.t : (pos.top < 0) ? 0 : pos.top;
        pos.left = (pos.left < ctx.l) ? ctx.l : (pos.left < 0) ? 0 : pos.left;
		$(target).css({left:""+pos.left+"px",top:""+pos.top+"px"});
	},
    formatPercent: function(num) {
        num = num.toString().replace(/\$|\%|\,/g, '');
        num *= Math.pow(10, 2);
        num = Math.round(Math.abs(num));
        num /= Math.pow(10, 2);
        if (isNaN(num)) {
            num = "0";
        }
        return num;
    },
    formatCurrency: function(num) {
        num = num.toString().replace(/\$|\%|\,/g, '');
        if (isNaN(num)) {
            num = "0";
        }
        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num * 100 + 0.50000000001);
        cents = num % 100;
        num = Math.floor(num / 100).toString();
        if (cents < 10) {
            cents = "0" + cents;
        }
        for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
            num = num.substring(0, num.length - (4 * i + 3)) + ',' +
            num.substring(num.length - (4 * i + 3));
        }
        return (((sign) ? '' : '-') + '$' + num + '.' + cents);
    },
    parseCurrency: function(num) {
        num = num.toString().replace(/\$|\%|\,/g, '');
        if (isNaN(num)) {
            num = "0";
        }
        return Math.abs(num);
    }
}


