// CALCULATOR
function NormalizeCurrency(Price) {
  Price = Math.round(Price*100)/100;
  Price = Price.toString();
  if (Price.indexOf('.')==-1) {
        return Price+ ".00";
  } else if(Price.indexOf('.')==Price.length-2) {
                return Price + "0"
         } else if(Price.indexOf('.')==Price.length-1) {
                        return Price + "00";
                } else {
                        return Price;
                }
}

function CalcLoanMonthly(v_price,term,annual_interest) {
  if (annual_interest <=0) {
  return (v_price/term);
  } else {
  var interest = annual_interest/1200;
  return (v_price*Math.pow(interest+1,term)*interest)/(Math.pow(interest+1,term) - 1);
  }
}

function CalcMonthly() {
  var price = $('calc-price').value-0;
  var sales_tax =  document.CalcForm.sales_tax.options[document.CalcForm.sales_tax.selectedIndex].text;
  var loan_balance = price*(1+sales_tax/100) - document.CalcForm.loan_down.value;
  loan_balance = NormalizeCurrency(loan_balance);

  var loan_term = document.CalcForm.loan_term.options[document.CalcForm.loan_term.selectedIndex].text;
  var loan_interest = document.CalcForm.loan_interest.value;
  document.CalcForm.loan_final.value = "$" + NormalizeCurrency(CalcLoanMonthly(loan_balance,loan_term,loan_interest));

    // ENABLE LOAN LENGTH OF ZERO starts
    var loan_final = $('loan_final');

    if (loan_final) {
      if(!$defined(loan_final.value) || loan_final.value == "" || loan_final.value == "$Infinity.00") {
	  loan_final.value = "$0";
      }
    }
    // ENABLE LOAN LENGTH OF ZERO ends

  return;

}


function NormalizeCurrencyBiweekly(Price) {
  Price = Price / 2; // DIVIDE TOTAL PRICE BY 2 before rounding
  Price = Math.round(Price*100)/100;
  Price = Price.toString();
  if (Price.indexOf('.')==-1) {
        return Price+ ".00";
  } else if(Price.indexOf('.')==Price.length-2) {
                return Price + "0"
         } else if(Price.indexOf('.')==Price.length-1) {
                        return Price + "00";
                } else {
                        return Price;
                }
}

function CalcLoanBiweekly(v_price,term,annual_interest) {
  if (annual_interest <=0) {
  return (v_price/term);
  } else {
  var interest = annual_interest/2600; // 26 payments a year rather than 12
  return (v_price*Math.pow(interest+1,term)*interest)/(Math.pow(interest+1,term) - 1);
  }
}

function CalcBiweekly() {
  var price = $('calc-price').value-0;
  var sales_tax =  document.CalcForm.sales_tax.options[document.CalcForm.sales_tax.selectedIndex].text;
  var loan_balance = price*(1+sales_tax/100) - document.CalcForm.loan_down.value;
  loan_balance = NormalizeCurrency(loan_balance);

  var loan_term = document.CalcForm.loan_term.options[document.CalcForm.loan_term.selectedIndex].text;
  var loan_interest = document.CalcForm.loan_interest.value;
  document.CalcForm.loan_final.value = "$" + NormalizeCurrencyBiweekly(CalcLoanBiweekly(loan_balance,loan_term,loan_interest));
  
    // ENABLE LOAN LENGTH OF ZERO starts
    var loan_final = $('loan_final');

    if (loan_final) {
      if(!$defined(loan_final.value) || loan_final.value == "" || loan_final.value == "$Infinity.00") {
	  loan_final.value = "$0";
      }
    }
    // ENABLE LOAN LENGTH OF ZERO ends  
  
  return;
}


// PRINT BTN
function open_printer_friendly(v_id, d_id, group) {
  window.open('/dcm2/printable_brochure.html?d_id='+d_id+'&v_id='+v_id+'&t=print&group='+group,"PrinterFriendly","top=50,left=50,width=640,height=550,buttons=no,scrollbars=yes,location=no,menubar=yes,resizable=yes,status=no,directories=no,toolbar=yes");
}

// ENLARGE PIC
function open_picture(url,winName,features) { //v2.0
   picture_window = window.open(url,winName,features);
    if (window.focus) {picture_window.focus()};
}

// OFFER FORM
function validateOffer(){
       var v = true;
       if($('FIRST_NAME').value == ""){
               v=false;
       }
       if($('EMAIL').value == ""){
               v=false;
       }
       if($('OFFER_AMOUNT').value == ""){
               v=false;
       }
       return v;
}

function postOffer(){
       if(validateOffer()){
               var offerPost = new Request({
                       url:'/dcm-search/post_lead.pl',
                       method:'post',
                       data:$('offer_form'),
                       onSuccess: function(resp){
                               if(resp != '0'){
                                       $('offer_form').set('html',resp);
                               }
                       }
               }).send();
       }else{
               alert('Please fill in all required fields marked by the *');
       }
}

function validateRequest(){
       var v = true;
       if($('FIRST_NAME').value == ""){
               v=false;
       }
       if($('EMAIL').value == ""){
               v=false;
       }
       return v;
}

function postRequest(){
       if(validateRequest()){
               var requestPost = new Request({
                       url:'/dcm-search/post_lead.pl',
                       method:'post',
                       data:$('contact_form'),
                       onSuccess: function(resp){
                               if(resp != '0'){
                                       $('contact_form').set('html',resp);
                               }
                       }
               }).send();
       }else{
               alert('Please fill in all required fields marked by the *');
       }
}
