var creditCalculator = function() {
    var _p = {
        getPaymentOptionFacade : function(optionType, initialPrice) {
            return function(optionType, initialPrice) {
                var _p = {
                    optionTypes : {
                        initialPrice : 0,
                        fastOption : {
                			payments : 0,
                			monthlyPayment : 0,
                			totalPaid : 0,
                			run : function() {

			                	var iCnt = 0;
			                    var dBilledInt = 0;
			                    var dNewBal = 0;
			                    var dInterestPaid = 0;
			                    var dBalance = 0;
			                    var dPmtAmt = 0;
			                    var dPurchase = 0;
			                    var dPaidPrin = 0;
			                    var dTotalPaid = 0;
			                    var bSmallPrice = false;
			
			                    for (i = 0; i <= 5; i++) {
			                    	
			                    	if (iCnt == 0) {
			                    		dPurchase = initialPrice;
			                    	} else {
			                    		dPurchase = 0;
			                    	}
			
			                        if (iCnt == 0) {
			                            dBalance = 0;
			                        } else if (iCnt == 1) {
			                            dBalance = dNewBal;
			                        } else if (iCnt == 2) {
			                            if (dBalance == dPmtAmt) {
			                                dBalance = 0;
			                            } else if (dBalance > 0.01) {
			                                dBalance = dBalance - dPaidPrin;
			                            } else {
			                                dBalance = 0;
			                            }
			                        } else {
			                            if (dBalance == dPmtAmt) {
			                                dBalance = 0;
			                            } else if (dBalance > 0.01) {
			                                dBalance = (dBalance - dPmtAmt) + dBilledInt;
			                            } else {
			                                dBalance = 0;
			                            }
			                        }
			
			                        if (iCnt == 0) {
			                        	dPmtAmt = 0.0;
			                        } else {
			                            if (dBalance <= dPmtAmt) {
			                                dPmtAmt = dBalance;
			                            } else if ((0.2585 * initialPrice) > dBalance) {
			                                dPmtAmt = dBalance;
			                            } else {
			                                dPmtAmt = (0.2585 * initialPrice);
			                            }
			                        }
			
			                        if (iCnt == 0) {
			                        	dBilledInt = 0;
			                        } else {
			                            if (dBalance - dPmtAmt <= 0) {
			                                dBilledInt = 0;
			                            } else if (((dBalance * 0.149) / 12) < 0.5) {
			                            	dBilledInt = 0.5;
			                            } else {
			                            	dBilledInt = ((dBalance * 0.149) / 12);
			                            }
			                        }
			
			                        dNewBal = ((dBalance + dPurchase) - dPmtAmt) + dBilledInt;
			                        dInterestPaid += dBilledInt;
			                        dPaidPrin = dPmtAmt - dBilledInt;
			                        iCnt += 1;
			
			                        if (iCnt == 6) {
			                            if (dBalance > 0) {
			                                bSmallPrice = true;
			                            }
			                            break;
			                        }
			                    }
			
			                    dTotalPaid = (initialPrice + dInterestPaid);
			                    var dAvgPmt = 0;
			
			                    if (bSmallPrice == true) {
			                        dAvgPmt = (dTotalPaid / 4);
			                    } else {
			                        dAvgPmt = (initialPrice * 0.2585);
			                    }
			
			                    _p.optionTypes.fastOption.payments = (iCnt - 2);
			                    _p.optionTypes.fastOption.totalPaid = (Math.round(dTotalPaid * 100) / 100).toFixed(2);
			                    _p.optionTypes.fastOption.monthlyPayment = (Math.round(dAvgPmt * 100) / 100).toFixed(2);
                			}
                		},
                		easyOption : {
                			payments : 0,
                			monthlyPayment : 0,
                			totalPaid : 0,
                			run : function() {

	                			var iCnt = 0;
	                		    var dBilledInt = 0;
	                		    var dNewBal = 0;
	                		    var dInterestPaid = 0;
	                		    var dBalance = 0;
	                		    var dPmtAmt = 0;
	                		    var dPurchase = 0;
	                		    var dPaidPrin = 0;
	                		    var dTotalPaid = 0;
	
	                		    for (i = 0; i < initialPrice; i--) {
	
	                		        if (iCnt == 0) {
	                		            dPurchase = initialPrice;
	                		        } else {
	                		            dPurchase = 0;
	                		        }
	
	                		        if (iCnt == 0) {
	                		            dPmtAmt = 0.0;
	                		        } else {
	                		            if (dBalance < dPmtAmt) {
	                		                dPmtAmt = dBalance;
	                		            } else if ((0.055 * initialPrice) <= 20.0) {
	                		                dPmtAmt = 20.0;
	                		            } else {
	                		                dPmtAmt = (0.055 * initialPrice);
	                		            }
	                		        }
	
	                		        if (iCnt == 0) {
	                		            dBalance = 0;
	                		        } else if (iCnt == 1) {
	                		            dBalance = dNewBal;
	                		        } else if (iCnt == 2) {
	                		            if (dBalance == dPmtAmt) {
	                		                dBalance = 0;
	                		            } else if (dBalance > 0.01) {
	                		                dBalance = dBalance - dPaidPrin;
	                		            } else {
	                		                dBalance = 0;
	                		            }
	                		        } else {
	                		            if (dBalance == dPmtAmt) {
	                		                dBalance = 0;
	                		            } else if (dBalance > 0.01) {
	                		                dBalance = (dBalance - dPmtAmt) + dBilledInt;
	                		            } else {
	                		                dBalance = 0;
	                		            }
	                		        }
	
	                		        if (iCnt == 0) {
	                		            dBilledInt = 0;
	                		        } else {
	                		            if (dBalance - dPmtAmt <= 0) {
	                		                dBilledInt = 0;
	                		            } else if (((dBalance * 0.199) / 12) < 0.5) {
	                		                dBilledInt = 0.5;
	                		            } else {
	                		                dBilledInt = ((dBalance * 0.199) / 12);
	                		            }
	                		        }
	
	                		        dNewBal = ((dBalance + dPurchase) - dPmtAmt) + dBilledInt;
	                		        dInterestPaid += dBilledInt;
	                		        dPaidPrin = dPmtAmt - dBilledInt;
	                		        iCnt += 1;
	
	                		        if (dNewBal <= 0 && iCnt > 1) {
	                		            break;
	                		        }
	                		    }
	
	                		    dTotalPaid = (initialPrice + dInterestPaid);
	                		    var dAvgPmt = 0;
	
	                		    if (initialPrice < 20) {
	                		        dAvgPmt = initialPrice;
	                		    } else if ((initialPrice * 0.055) < 20) {
	                		        dAvgPmt = 20.0;
	                		    } else {
	                		        dAvgPmt = (initialPrice * 0.055);
	                		    }
	
	                		    iCnt = iCnt - 1;
	
	                		    _p.optionTypes.easyOption.payments = iCnt;
			                    _p.optionTypes.easyOption.totalPaid = (Math.round(dTotalPaid * 100) / 100).toFixed(2);
			                    _p.optionTypes.easyOption.monthlyPayment = (Math.round(dAvgPmt * 100) / 100).toFixed(2);
                			}
                		},
                		visaMastercardOption : {
                			payments : 0,
                			monthlyPayment : 0,
                			totalPaid : 0,
                			run : function() {
     
	                		    var iCnt = 0;
	                		    var total = initialPrice;
	                		    var dIntDue = 0;
	                		    var dNewBal = 0;
	                		    var dInterestPaid = 0;
	                		    var dTotalPaid = 0;
	                		    var dMinDue = 0;
	
	                		    dMinDue = 10.0;
	
	                		    for (i = 0; i <= initialPrice; i--) {
	
	                		        if (iCnt == 0) {
	                		            if (total < 10.0) {
	                		                dMinDue = total;
	                		            } else if ((total * 0.01) > 10.0) {
	                		                dMinDue = total * 0.01;
	                		            } else {
	                		                dMinDue = 10.0;
	                		            }
	                		        } else {
	                		            if (total < 10.0) {
	                		                dMinDue = total;
	                		            } else if (((total * 0.01) + dIntDue) > 10.0) {
	                		                dMinDue = ((total * 0.01) + dIntDue);
	                		            } else {
	                		                dMinDue = 10.0;
	                		            }
	                		        }
	
	                		        if (total - dMinDue == 0) {
	                		            dIntDue = 0;
	                		        } else if (((total * 0.199) / 12) < 1) {
	                		            dIntDue = 1.0;
	                		        } else {
	                		            dIntDue = ((total * 0.199) / 12);
	                		        }
	
	                		        dNewBal = (total - dMinDue) + dIntDue;
	                		        dInterestPaid += dIntDue;
	                		        total = dNewBal;
	                		        iCnt += 1;
	
	                		        if (total <= 0) {
	                		            break;
	                		        }
	                		    }
	
	                		    dTotalPaid = (initialPrice + dInterestPaid);
	                		    var dAvgPmt = 0;
	
	                		    if ((dTotalPaid / iCnt) < 10) {
	                		        dAvgPmt = 10;
	                		    } else {
	                		        dAvgPmt = (dTotalPaid / iCnt);
	                		    }
	
	                		    _p.optionTypes.visaMastercardOption.payments = iCnt;
			                    _p.optionTypes.visaMastercardOption.totalPaid = (Math.round(dTotalPaid * 100) / 100).toFixed(2);
			                    _p.optionTypes.visaMastercardOption.monthlyPayment = (Math.round(dAvgPmt * 100) / 100).toFixed(2);
                			}
                		},
                		fingerhutCreditOption : {
                			payments : 0,
                			monthlyPayment : 0,
                			totalPaid : 0,
                			run : function() {
                				
                				if (initialPrice <= 5.98) {
                					_p.optionTypes.fingerhutCreditOption.monthlyPayment = initialPrice;
                				} else if (initialPrice >= 5.99 && initialPrice <= 44.99) {
                					_p.optionTypes.fingerhutCreditOption.monthlyPayment = 5.99;
                				} else if (initialPrice >= 45.00 && initialPrice <= 69.99) {
                					_p.optionTypes.fingerhutCreditOption.monthlyPayment = 6.99;
                				} else if (initialPrice >= 70.00 && initialPrice <= 99.99) {
                					_p.optionTypes.fingerhutCreditOption.monthlyPayment = 7.99;
                				} else if (initialPrice >= 100.00 && initialPrice <= 124.99) {
                					_p.optionTypes.fingerhutCreditOption.monthlyPayment = 9.99;
                				} else if (initialPrice >= 125.00 && initialPrice <= 199.99) {
                					_p.optionTypes.fingerhutCreditOption.monthlyPayment = 13.99;
                				} else if (initialPrice >= 200.00 && initialPrice <= 249.99) {
                					_p.optionTypes.fingerhutCreditOption.monthlyPayment = 16.99;
                				} else if (initialPrice >= 250.00 && initialPrice <= 299.99) {
                					_p.optionTypes.fingerhutCreditOption.monthlyPayment = 19.99;
                				} else if (initialPrice >= 300.00 && initialPrice <= 349.99) {
                					_p.optionTypes.fingerhutCreditOption.monthlyPayment = 22.99;
                				} else if (initialPrice >= 350.00 && initialPrice <= 449.99) {
                					_p.optionTypes.fingerhutCreditOption.monthlyPayment = 28.99;
                				} else if (initialPrice >= 450.00 && initialPrice <= 549.99) {
                					_p.optionTypes.fingerhutCreditOption.monthlyPayment = 33.99;
                				} else if (initialPrice >= 550.00 && initialPrice <= 799.99) {
                					_p.optionTypes.fingerhutCreditOption.monthlyPayment = 46.99;
                				} else if (initialPrice >= 800.00 && initialPrice <= 1099.99) {
                					_p.optionTypes.fingerhutCreditOption.monthlyPayment = 59.99;
                				} else if (initialPrice >= 1100.00 && initialPrice <= 1399.99) {
                					_p.optionTypes.fingerhutCreditOption.monthlyPayment = 69.99;
                				} else if (initialPrice > 1400.00) {
                					_p.optionTypes.fingerhutCreditOption.monthlyPayment = (Math.round((initialPrice * .05) * 100) / 100).toFixed(2);
                				}
                				
                			}
                		},
                        calculatePaymentDetails : function(optionType) {
                        	_p.optionTypes[optionType].run();
                        }
                    }
                };
                return {
                    init : function() {
                        _p.optionTypes.initialPrice = initialPrice;
                        _p.optionTypes.calculatePaymentDetails(optionType);
                        return this;
                    },
                    getInitialPrice : function() {
                        return initialPrice;
                    },
                    getNumberOfMonthlyPayments : function() {
                    	if (initialPrice <= 0 || isNaN(initialPrice)) {
                    		return "0";
                    	} else {
                    		return _p.optionTypes[optionType].payments;
                    	}
                    },
                    getMonthlyPayment : function() {
                        return _p.optionTypes[optionType].monthlyPayment;
                    },
                    getTotalPaid : function() {
                        return _p.optionTypes[optionType].totalPaid;
                    }
                };;
            }(optionType, initialPrice).init();
        }
    };
    return {
        calculatePayment : function(optionType, initialPrice) {
            switch(optionType) {
                case "Fast Option" :
                    return _p.getPaymentOptionFacade("fastOption", initialPrice);
                break;
                case "Easy Option" :
                    return _p.getPaymentOptionFacade("easyOption", initialPrice);
                break;
                case "Visa Mastercard" :
                    return _p.getPaymentOptionFacade("visaMastercardOption", initialPrice);
                case "Fingerhut Credit" :
                	return _p.getPaymentOptionFacade("fingerhutCreditOption", initialPrice);
                break;
            }
        }
    };
}();
