var error = 0;
var error_lease_amount = 0;
var error_nb_payment = 0;
var error_interest = 0;
var error_payment_amount = 0;
var error_residual_amount = 0;
var residual_amount = 0;


  
var arrayMonths = new Array();

arrayMonths[1] = new Array("January");
arrayMonths[2] = new Array("February");
arrayMonths[3] = new Array("March");
arrayMonths[4] = new Array("April");
arrayMonths[5] = new Array("May");
arrayMonths[6] = new Array("June");
arrayMonths[7] = new Array("July");
arrayMonths[8] = new Array("August");
arrayMonths[9] = new Array("September");
arrayMonths[10] = new Array("October");
arrayMonths[11] = new Array("November");
arrayMonths[12] = new Array("December");

function validate_loan_form() {
    var skiptxt = "";
	var payment_amount = document.getElementById("payment_amount").value;
	document.getElementById("payment_amount").value = cast_currency(document.getElementById("payment_amount").value);
	var residual_amount = 0
	var interest = document.getElementById("interest").value;
	var nb_payment = document.getElementById("nb_payment").value;
	var lease_amount = document.getElementById("lease_amount").value;
	var regExp = new RegExp(",","g");
		myLeaseAmount = lease_amount.replace(regExp,"");
		myPaymentAmount = payment_amount.replace(regExp,"");
		
	var the_error = "";
	if (document.getElementById('the_calc_field_lease_amount').checked) {
		//nb_payment
		if (!isPositiveNumeric(document.getElementById('nb_payment').value)) {
			the_error = the_error + '-The Number of Periods must be a numeric value.<br />';
			document.getElementById('l_nb_payment').className = "error";
		}else{
			if (!isPositiveInteger(document.getElementById('nb_payment').value)) {
				the_error = the_error + '-The Number of Periods must be an integer greater than zero.<br />';
				document.getElementById('l_nb_payment').className = "error";
			}else{
				document.getElementById('l_nb_payment').className = "";
				nb_payment = parseFloat(nb_payment);
			}
		}
		
		//interest
		if (!isPositiveNumeric(document.getElementById('interest').value)) {
			the_error = the_error + '-The Interest Rate must be a numeric value between 0 and 50.<br />';
			document.getElementById('l_interest').className = "error";
		} else {
			document.getElementById('l_interest').className = "";
			interest = parseFloat(interest);
			if (document.getElementById('interest').value >= 50) {
				the_error = the_error + '-The Interest Rate must be a numeric value between 0 and 50.<br />';
				document.getElementById('l_interest').className = "error";
			}
		}
		//payment_amount
		if (!isPositiveNumeric(myPaymentAmount)) {
			the_error = the_error + '-The Monthly Payment must be a numeric value greater than 0.<br />';
			document.getElementById('l_payment_amount').className = "error";
		} else {
			document.getElementById('l_payment_amount').className = "";
			payment_amount = parseFloat(myPaymentAmount);
		}
		if (myPaymentAmount >= 50000000) {
			the_error = the_error + '-The Monthly Payment must be a less than $ 50,000,000.00.<br />';
			document.getElementById('l_payment_amount').className = "error";
		} else {
			document.getElementById('l_payment_amount').className = "";
			payment_amount = parseFloat(myPaymentAmount);
		}

	}
	else if (document.getElementById('the_calc_field_payment_amount').checked) {
		//lease_amount
		if (!isPositiveNumeric(myLeaseAmount)) {
			the_error = the_error + '-The Finance Amount must be a numeric value greater than 0.<br />';
			document.getElementById('l_lease_amount').className = "error";
		} else {
			document.getElementById('l_lease_amount').className = "";
			lease_amount = parseFloat(myLeaseAmount);
		}
		if (myLeaseAmount >= 500000000) {
			the_error = the_error + '-The Finance Amount must be less than $ 500,000,000.00.<br />';
			document.getElementById('l_lease_amount').className = "error";
		} else {
			document.getElementById('l_lease_amount').className = "";
			lease_amount = parseFloat(myLeaseAmount);
		}
		
		//nb_payment
		if (!isPositiveNumeric(document.getElementById('nb_payment').value)) {
			the_error = the_error + '-The Number of Periods must be a numeric value.<br />';
			document.getElementById('l_nb_payment').className = "error";
		}else{
			if (!isPositiveInteger(document.getElementById('nb_payment').value)) {
				the_error = the_error + '-The Number of Periods must be an integer greater than zero.<br />';
				document.getElementById('l_nb_payment').className = "error";
			}else{
				document.getElementById('l_nb_payment').className = "";
				nb_payment = parseFloat(nb_payment);
			}
		}
		//interest
		if (!isPositiveNumeric(document.getElementById('interest').value)) {
			the_error = the_error + '-The Interest Rate must be a numeric value between 0 and 50.<br />';
			document.getElementById('l_interest').className = "error";
		} else {
			document.getElementById('l_interest').className = "";
			interest = parseFloat(interest);
			if (document.getElementById('interest').value >= 50) {
				the_error = the_error + '-The Interest Rate must be a numeric value between 0 and 50.<br />';
				document.getElementById('l_interest').className = "error";
			}
		}
		

	} else {
		//lease_amount
		if (!isPositiveNumeric(myLeaseAmount)) {
			the_error = the_error + '-The Finance Amount must be a numeric value greater than 0.<br />';
			document.getElementById('l_lease_amount').className = "error";
		} else {
			document.getElementById('l_lease_amount').className = "";
			lease_amount = parseFloat(myLeaseAmount);
		}
		
		//nb_payment
		if (!isPositiveNumeric(document.getElementById('nb_payment').value)) {
			the_error = the_error + '-The Number of Periods must be a numeric value.<br />';
			document.getElementById('l_nb_payment').className = "error";
		}else{
			if (!isPositiveInteger(document.getElementById('nb_payment').value)) {
				the_error = the_error + '-The Number of Periods must be an integer greater than zero.<br />';
				document.getElementById('l_nb_payment').className = "error";
			}else{
				document.getElementById('l_nb_payment').className = "";
				nb_payment = parseFloat(nb_payment);
			}
		}
		//interest
		if (!isPositiveNumeric(document.getElementById('interest').value)) {
			the_error = the_error + '-The Interest Rate must be a numeric value between 0 and 50.<br />';
			document.getElementById('l_interest').className = "error";
		} else {
			document.getElementById('l_interest').className = "";
			interest = parseFloat(interest);
			if (document.getElementById('interest').value >= 50) {
				the_error = the_error + '-The Interest Rate must be a numeric value between 0 and 50.<br />';
				document.getElementById('l_interest').className = "error";
			}
		}
		//payment amount
		if (!isPositiveNumeric(myPaymentAmount)) {
			the_error = the_error + '-The Monthly Payment must be a numeric value greater than 0.<br />';
			document.getElementById('l_payment_amount').className = "error";
		} else {
			document.getElementById('l_payment_amount').className = "";
			payment_amount = parseFloat(myPaymentAmount);
		}
		if (myPaymentAmount >= 50000000) {
			the_error = the_error + '-The Monthly Payment must be less than $ 50,000,000.00.<br />';
			document.getElementById('l_payment_amount').className = "error";
		} else {
			document.getElementById('l_payment_amount').className = "";
			payment_amount = parseFloat(myPaymentAmount);
		}
	}
	if ( !document.getElementById("disclaimer").checked) {
        the_error = the_error + '<p>- Please agree to the disclaimer by checking the box above.</p><br />';
		document.getElementById('l_disclaimer').className = "error";
    } else {
		document.getElementById('l_disclaimer').className = "";
    }	

            	
	var beg_end = 0;


    selected_year = document.getElementById("start_year").value;
	selected_month = document.getElementById("start_month").value;
	
	var selected_day = "01";
	if (isValidDate(selected_month, selected_day, selected_year)) {
		
		selected_date = new Date();
		
		selected_date.setFullYear(selected_year);
		selected_date.setMonth(selected_month-1);
		selected_date.setDate(selected_day);
		selected_month = selected_date.getMonth();
		
	} else {
		
		cur_date = new Date();
		selected_year = cur_date.getFullYear();
		selected_month = cur_date.getMonth();
		the_month = cur_date.getMonth() +1 ;
		selected_day = "01";
		
		
		selected_date = new Date();
		selected_date.setFullYear(selected_year);
		selected_date.setMonth(selected_month);
		selected_date.setDate(selected_day);
		document.getElementById("start_year").value = selected_year;
			
		if(the_month  < 10){
		    the_month = "0" + (the_month)
		}
		
	    document.getElementById("start_month").value = the_month;
	}
	
	error_skip = 0
	if(document.getElementById('l_nb_payment').className == ""){
        error_skip = 1
    
        if(nb_payment >= 12){
            for (var b = 1; b <= 12; b++){
                if(document.getElementById("skip_month_"+b).checked){
                    if(skiptxt == "")
                        skiptxt = arrayMonths[b]
                    else
                        skiptxt = skiptxt + ", " + arrayMonths[b]
                }
                else
                    error_skip = 0
            }
        }
        else{
            if(beg_end){
                var iter 
                for (var b = selected_month; b <= selected_month + nb_payment -1; b++){
                    iter = b % 12
    	    
                    if(iter == 0)
                        iter = 12

                    if(document.getElementById("skip_month_"+iter).checked){
                        if(skiptxt == "")
                            skiptxt = arrayMonths[iter]
                        else
                            skiptxt = skiptxt + ", " + arrayMonths[iter]
                    }
                    else
                        error_skip = 0
                }
            }
            else{
                var iter 
                for (var b = selected_month + 1; b <= selected_month + nb_payment; b++){
                    iter = b % 12
    	    
                    if(iter == 0)
                        iter = 12
                    if(document.getElementById("skip_month_"+iter).checked){
                        if(skiptxt == "")
                            skiptxt = arrayMonths[iter]
                        else
                            skiptxt = skiptxt + ", " + arrayMonths[iter]
                    }
                    else
                        error_skip = 0
                }        
            }
        }   
    }
    
	if (the_error.length > 0) {
		document.getElementById('form_error').innerHTML = "<h3>Error</h3>" + the_error;
		document.getElementById('form_error').style.display = 'block';
		document.getElementById('loan_summary_result').style.display = 'none';
        document.getElementById('detailed_result').style.display = 'none';
        document.getElementById('buttons').style.display = 'none';
		return false;
	} else {

	    document.getElementById('form_error').style.display = 'none';
		calculate_loan(lease_amount,nb_payment,interest,payment_amount,residual_amount,beg_end,selected_date);
	}
}


function calculate_loan(lease_amount,nb_payment,interest,payment_amount,residual_amount,beg_end,selected_date) {
    
    
    
    var skip = 0;
    var z;
    var ad_nb_payment;
    var months_disp;
    var actual_nb_payment=0;
    var myMonthsSkipped = "";
    
    if(beg_end){
        z = 0;
        ad_nb_payment = nb_payment - 1;
        months_disp = selected_date.getMonth() + 1;
    }    
    else{
        z = 1;
        ad_nb_payment = nb_payment;
        months_disp = selected_date.getMonth() + 2;
    }
    
    for(i=1;i<13;i++){
		if(document.getElementById("skip_month_"+i).checked){
			myMonthsSkipped = myMonthsSkipped + arrayMonths[i] + "<br />"
		}
    }
    
    while(z <= ad_nb_payment){
        if(months_disp == 13){
            months_disp = 1
        }
        if(!document.getElementById("skip_month_"+months_disp).checked){
            skip = skip + 1/(Math.pow(1+(interest/1200),z));
            actual_nb_payment = actual_nb_payment + 1
        }
        months_disp = months_disp + 1;
        z = z + 1;
    }

	if (the_calc_field.search("payment_amount") > -1) {
	    ballon_factor = (1/(Math.pow(1+interest/1200,nb_payment)))* residual_amount;
		payment_amount = (lease_amount-ballon_factor)/skip;
		payment_amount = Math.round(payment_amount*100)/100;

	} else if (the_calc_field.search("lease_amount") > -1) {
	    ballon_factor = (1/(Math.pow(1+interest/1200,nb_payment)))* residual_amount;
		lease_amount = payment_amount * skip + ballon_factor;
		lease_amount = Math.round(lease_amount*100)/100;
	} else {
		residual_amount = (lease_amount - payment_amount * skip)* (Math.pow(1+interest/1200,nb_payment));
		residual_amount = Math.round(residual_amount*100)/100;
	}

	document.getElementById("lease_amount").value = cast_currency(lease_amount);
	build_loan_summary_result(selected_date,beg_end,parseFloat(interest),payment_amount,lease_amount,residual_amount,nb_payment,actual_nb_payment,myMonthsSkipped);
	build_loan_detailed_result(selected_date,beg_end,lease_amount,interest,payment_amount,lease_amount,residual_amount,nb_payment);
}

/*------------------------------------------------*/
/* FUNCTION build_loan_summary_result				  */
/* build the summary result based on the value 
	calculated by function calculate_loan()	  */
/*------------------------------------------------*/
function build_loan_summary_result(selected_date,beg_end,interest,payment_amount,lease_amount,residual_amount,nb_payment,actual_nb_payment,myMonthsSkipped) {
	the_summary_div = document.getElementById('summary_result');
	
	the_html = '';
	
	the_html = '<h3>Summary</h3>'
	the_html = the_html + '<table cellspacing="0" cellpadding="2" border="0">';
	the_html = the_html + '</td></tr>';
	the_html = the_html + '<tr><td><p>Loan Start Date:</p></td><td style="text-align:right;"><p>'+ (selected_date.getMonth()+1) + '/1/' + selected_date.getFullYear()+'</p></td></tr>';
	the_html = the_html + '<tr><td><p>Interest Rate:</p></td><td style="text-align:right;"><p>'+interest+'%</p></td></tr>';
	the_html = the_html + '<tr><td><p>Number of Periods (in months):</p></td><td style="text-align:right;"><p>'+nb_payment+'</p></td></tr>';
	if(myMonthsSkipped != ""){
		the_html = the_html + '<tr><td style="vertical-align:top;"><p>Payments skipped:</p></td><td style="text-align:right;"><p>'+myMonthsSkipped+'</p></td></tr>';
	}
	the_html = the_html + '<tr><td><p>Total Amount Financed:</p></td><td style="text-align:right;"><p>$ '+cast_currency(lease_amount)+'</p></td></tr>';
	the_html = the_html + '<tr><td><p>Total Payments:</p></td><td style="text-align:right;"><p>$ '+cast_currency(payment_amount*actual_nb_payment)+'</p></td></tr>';
	the_html = the_html + '<tr><td><p>Monthly Payment:</p></td><td style="text-align:right;"><p>$ '+cast_currency(payment_amount)+'</p></td></tr>';
	the_html = the_html + '</table>';
		
	the_summary_div.innerHTML = the_html;
	the_summary_div.style.display = 'block';
	
}

/*------------------------------------------------*/
/* FUNCTION build_loan_detailed_result				  */
/* build the table result based on the value 
	calculated by function calculate_loan()	  */
/*------------------------------------------------*/
function build_loan_detailed_result(selected_date,beg_end,real_lease_amount,interest,payment_amount,lease_amount,residual_amount,nb_payment) {
	the_detailed_div = document.getElementById('detailed_result');
	the_html = '';
	this_month_balance = lease_amount;
	subtotal_year_payment = 0;
	subtotal_year_interest = 0;
	subtotal_year_principal = 0;
	grandtotal_year_payment = 0;
	grandtotal_year_interest = 0;
	grandtotal_year_principal = 0;

	//TABLE + HEADER
	the_html = the_html + '<h3>Payment Schedule</h3>';
	the_html = the_html + '<table cellspacing="0" cellpadding="0" border="0" class="detail_result" width="100%" style="border-top:1px #ccc solid;border-right:1px #ccc solid;">';
	the_html = the_html + '<thead><tr><th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p><strong>Event</strong></p></th><th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p><strong>Date</strong></p></th><th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p><strong>Payment</strong></p></th><th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p><strong>Interest</p></strong></th><th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p><strong>Principal</p></strong></th><th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p><strong>Balance</strong></p></th></tr></thead>';
	//FIRST LINE
	the_html = the_html + '<tr style="background-color:#eee;">';
	the_html = the_html + '		<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>Loan</p></td>';
	the_html = the_html + '		<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>'+get_formated_date(selected_date)+'</p></td>';
	the_html = the_html + '		<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>&nbsp;</p></td>';
	the_html = the_html + '		<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>&nbsp;</p></td>';
	the_html = the_html + '		<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>&nbsp;</p></td>';
	the_html = the_html + '		<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(this_month_balance)+'&nbsp;</p></td>';
	the_html = the_html + '	</tr>';

	color = 0
	//Add one month if payment in arrears
	if (beg_end == 0) {
	  
			old_year = selected_date.getFullYear();
			selected_date.setFullYear(selected_date.getFullYear(),selected_date.getMonth()+1,selected_date.getDate());
			if (old_year != selected_date.getFullYear()) {
				//display a year total with 0!!!
				the_html = the_html + '<tr'; if (color%2 ==1) {the_html = the_html + ' style="background-color:#eee;"';}the_html = the_html + '>';
				the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>'+old_year+' Total</p></th>';
				the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>&nbsp;</p></th>';
				the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>&nbsp;</p></th>';
				the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>&nbsp;</p></th>';
				the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ 0.00</p></th>';
				the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(this_month_balance)+'</p></th>';
				the_html = the_html + '</tr>';
				color = color + 1;
			}
	}

    months_disp = selected_date.getMonth()+1;
  
    if((months_disp == 13)||(months_disp == 0)){
        months_disp = 1
    }
   
    var lastpayments = calcLastPayment(nb_payment + months_disp - 1);
    if (beg_end == 0){
        lastpayments = lastpayments - 1;
    }
	
    

var cum_interest = 0;
for (var period = 0; period <= nb_payment - 1;period++) {

    if((months_disp == 13)||(months_disp == 0)){
        months_disp = 1
    }

	if (period != 0 || beg_end == 0) {
		this_month_interest =Math.round((this_month_balance)*interest/1200*100)/100;
		//if Arrears, should add one month
	} else {
		this_month_interest = 0;
	}
	
	
	//if the last payment and no residual amount: should adjust the last payment
	if (period == lastpayments - 1  &&  residual_amount == 0) {
		
		payment_amount = this_month_interest + this_month_balance;
		this_month_balance = (this_month_balance - payment_amount) + this_month_interest;
		
		subtotal_year_payment = subtotal_year_payment + parseFloat(payment_amount);
		subtotal_year_interest = subtotal_year_interest + this_month_interest;
		subtotal_year_principal = subtotal_year_principal + (payment_amount - this_month_interest);
		the_html = the_html + '<tr'; if (color%2 ==1) {the_html = the_html + ' style="background-color:#eee;"';}the_html = the_html + '>';
	
		the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>'+(period+1)+'&nbsp;</p></td>';
		the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>'+get_formated_date(selected_date)+'&nbsp;</p></td>';
		the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(payment_amount)+'&nbsp;</p></td>';
		the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ ';if (period != 0) {the_html = the_html + cast_currency(this_month_interest);}else{the_html = the_html + "0.00"}the_html = the_html + '&nbsp;</p></td>';
		the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(payment_amount - this_month_interest)+'&nbsp;</p></td>';
		the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(this_month_balance)+'&nbsp;</p></td>';
	the_html = the_html + '</tr>';
	
	} else {
	
	    if(!document.getElementById("skip_month_"+months_disp).checked){
		    this_month_balance = (this_month_balance - payment_amount) + this_month_interest;
    	
		    subtotal_year_payment = subtotal_year_payment + payment_amount;
		    subtotal_year_interest = subtotal_year_interest + this_month_interest + cum_interest;
		    subtotal_year_principal = subtotal_year_principal + (payment_amount - this_month_interest - cum_interest);

		    the_html = the_html + '<tr'; if (color%2 ==1) {the_html = the_html + ' style="background-color:#eee;"';}the_html = the_html + '>';
		    the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>'+(period+1)+'&nbsp;</p></td>';
		    the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>'+get_formated_date(selected_date)+'&nbsp;</p></td>';
		    the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(payment_amount)+'&nbsp;</p></td>';
		    the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ ';if (this_month_interest != 0) {the_html = the_html + cast_currency(this_month_interest + cum_interest);}else{the_html = the_html + "0.00"}the_html = the_html + '&nbsp;</p></td>';
		    the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(payment_amount - this_month_interest - cum_interest)+'&nbsp;</p></td>';
		    the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(this_month_balance)+'&nbsp;</p></td>';
		    the_html = the_html + '</tr>';
		    cum_interest = 0
		}
		
        else{
            this_month_balance = (this_month_balance - 0) + this_month_interest;
    		cum_interest = cum_interest + this_month_interest;
    		
	        the_html = the_html + '<tr'; if (color%2 ==1) {the_html = the_html + ' style="background-color:#eee;"';}the_html = the_html + '>';
		    the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>'+(period+1)+'&nbsp;</p></td>';
		    the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>'+get_formated_date(selected_date)+'&nbsp;</p></td>';
		    the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>skipped</p></td>';
		    the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>skipped</p></td>';
		    the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>skipped</p></td>';
		    the_html = the_html + '<td style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(this_month_balance)+'&nbsp;</p></td>';
		    the_html = the_html + '</tr>';						      
		}
	}
	old_year = selected_date.getFullYear();
	selected_date.setFullYear(selected_date.getFullYear(),selected_date.getMonth() + 1,selected_date.getDate());
	if (old_year == selected_date.getFullYear()) {
		color = color + 1;
		
	} else  {
		
		//new year ==> display year summary
		color = color + 1
		the_html = the_html + '<tr'; if (color%2 ==1) {the_html = the_html + ' style="background-color:#eee;"';}the_html = the_html + '>';
		the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>'+old_year+' Total</p></th>';
		the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>&nbsp;</p></th>';
		the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(subtotal_year_payment)+'&nbsp;</p></th>';
		the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(subtotal_year_interest)+'&nbsp;</p></th>';
		the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(subtotal_year_principal)+'&nbsp;</p></th>';
		the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(this_month_balance)+'&nbsp;</p></th>';
		the_html = the_html + '</tr>';
		
		grandtotal_year_payment = grandtotal_year_payment + subtotal_year_payment;
		grandtotal_year_interest = grandtotal_year_interest + subtotal_year_interest;
		grandtotal_year_principal = grandtotal_year_principal + subtotal_year_principal;
		subtotal_year_payment = 0;
		subtotal_year_interest = 0;
		subtotal_year_principal = 0;
		color = color + 1;
		
	}
	months_disp = months_disp + 1;
} //end for
	
	
	subtotal_year_interest = subtotal_year_interest;
	subtotal_year_principal = subtotal_year_principal;
	//THE Residual 
	if (residual_amount > 0) {
		if (beg_end == 1) {
			Residual_interest = (this_month_balance)*interest/1200
			Residual_principal = this_month_balance
		} else {
			Residual_interest = 0
			//REMOVE ONE month
			if (isValidDate(selected_date.getMonth()-2,selected_date.getDate(), selected_date.getFullYear())) {
					selected_date.setFullYear(selected_date.getFullYear(),selected_date.getMonth()-1,selected_date.getDate());
			} else {
				selected_date.setFullYear(selected_date.getFullYear()-1,1,1);
			}
		}
		subtotal_year_payment = subtotal_year_payment + Residual_interest + this_month_balance;
		subtotal_year_interest = subtotal_year_interest + Residual_interest;
		subtotal_year_principal = subtotal_year_principal + this_month_balance;
	
		the_html = the_html + '<tr'; if (color%2 ==1) {the_html = the_html + ' style="background-color:#eee;"';}the_html = the_html + '>';
		the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;">Purchase Option</th>';
		the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;">$ '+get_formated_date(selected_date)+'&nbsp;</th>';
		the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;">$ '+cast_currency(Residual_interest + this_month_balance)+'&nbsp;</th>';
		the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;">$ '+cast_currency(Residual_interest)+'&nbsp;</th>';
		the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;">$ '+cast_currency(this_month_balance)+'&nbsp;</th>';
		the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;">&nbsp;</th>';
		the_html = the_html + '</tr>';
	
		color = color + 1
	}
	//THE LAST YEAR SUMMARY
	the_html = the_html + '<tr'; if (color%2 ==1) {the_html = the_html + ' ' + ' style="background-color:#eee;"';}the_html = the_html + '>';
	the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>'+selected_date.getFullYear()+' Total</p></th>';
	the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>&nbsp;</p></th>';
	the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(subtotal_year_payment)+'&nbsp;</p></th>';
	the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(subtotal_year_interest)+'&nbsp;</p></th>';
	the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(subtotal_year_principal)+'&nbsp;</p></th>';
	the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(this_month_balance)+'&nbsp;</p></th>';
	the_html = the_html + '</tr>';
	
	//FINAL TOTAL
	color = color + 1
	the_html = the_html + '<tr'; if (color%2 ==1) {the_html = the_html + ' ' + ' style="background-color:#eee;"';}the_html = the_html + '>';
	the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>Grand Total</p></th>';
	the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>&nbsp;</p></th>';
	the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(grandtotal_year_payment + subtotal_year_payment)+'&nbsp;</p></th>';
	the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(grandtotal_year_interest + subtotal_year_interest)+'&nbsp;</p></th>';
	the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ '+cast_currency(grandtotal_year_principal + subtotal_year_principal)+'&nbsp;</p></th>';
	the_html = the_html + '<th style="border-bottom: 1px #ccc solid;border-left:1px #ccc solid;margin:0;padding:3px;text-align:right;"><p>$ 0.00&nbsp;</p></th>';
	the_html = the_html + '</tr>';
	the_html = the_html + '</table>';


	the_detailed_div.innerHTML = the_html

	the_detailed_div.style.display = 'block';
	document.getElementById('buttons').style.display = 'block';
	

}


//utility functions
	function cast_currency(price) {
		return CommaFormatted(CurrencyFormatted(price));
	}
	
	function CurrencyFormatted(amount)
	{
		var i = parseFloat(amount);
		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;
	}

function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}


function isPositiveInteger(totest) {
	return totest.match(/^\d+$/);
}

function isPositiveNumeric(totest) {
	//var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
	var objRegExp  =  /(^\d\d*\.\d*$)|(^\d\d*$)|(^\.\d\d*$)/;
	return totest.match(objRegExp);
}

//month: between 1 and 12
function isValidDate(month, day, year) {
	var oDate = new Date();
	oDate.setFullYear(year,month-1,day);
	return !(	   isNaN(oDate.getMonth()) 
		|| isNaN(oDate.getDate())
		|| isNaN(oDate.getMonth())
		|| isNaN(oDate.getFullYear())
		
		|| (oDate.getDate() != day)
		|| (oDate.getMonth()+1 != month)
		|| (!(oDate.getFullYear() == year || oDate.getYear() == year))
		)
}
var the_calc_field = "lease_amount";

function toggleSkip() {
    if(document.getElementById("skip_payment").checked)
	    document.getElementById("skip_payment_div").style.display = "block";
    else{
        document.getElementById("skip_payment_div").style.display = "none";
        for(i=1;i<=12;i++){
            document.getElementById("skip_month_" + i).checked = false;
        }
    }			
}


function change_calc_field(the_field) {
	if (document.getElementById(the_calc_field)) {
		document.getElementById(the_calc_field).disabled = false;
		document.getElementById(the_calc_field).style.backgroundColor = "#fff";
		document.getElementById(the_calc_field).style.backgroundImage = "";
	}
	the_calc_field = the_field;
	if (document.getElementById(the_calc_field)) {
		document.getElementById(the_calc_field).disabled = "disabled";
		document.getElementById(the_calc_field).style.backgroundColor = "#EEE";
		document.getElementById(the_calc_field).style.backgroundImage = "url(/images/disabled_pattern.gif)";
		document.getElementById("l_"+the_calc_field).className = "";
	}
}
function get_formated_date(dateobj) {
	return dateobj.getDate() + '/' + (dateobj.getMonth() + 1)  + '/' + dateobj.getFullYear() ;
}

function calcLastPayment(lastpayment){

    lastPaymentMonth = lastpayment % 12;
    
    
    if (lastPaymentMonth == 0){ 
        lastPaymentMonth = 12;
    }

   if(document.getElementById("skip_month_"+lastPaymentMonth).checked){

        the_result = calcLastPayment(lastpayment-1);
   }        
    else{
       the_result = lastpayment - selected_month;
    }
    return  the_result ;                  
}

function resetFields(){
	the_form = document.getElementById("lease");
	for (i=0;i < the_form.elements.length;i++){
		the_form.elements[i].value = "";
	}
}
