<!--

/*
[
THINKSPACE Funtions
Robert J. Parizek
Last updated: March 27/08
]
*/

function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789.,").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}

function CalculateTotal(formvalue) {
    var order_total = 0;
    var field1 = 0;
    var field2 = 0;
    var field3 = 0;
    var field4 = 0;
    
    if (document.thinkspaceform.planning_reno_cost_1.value == '') {
      field1 = 0;
    } else {
      field1 = replace(document.thinkspaceform.planning_reno_cost_1.value,',','');
      field1 = parseFloat(field1);      
    }
    
    if (document.thinkspaceform.planning_reno_cost_2.value == '') {
      field2 = 0;
    } else {
      field2 = replace(document.thinkspaceform.planning_reno_cost_2.value,',','');
      field2 = parseFloat(field2);
    }
    
    
    if (document.thinkspaceform.planning_reno_cost_3.value == '') {
      field3 = 0;
    } else {
      field3 = replace(document.thinkspaceform.planning_reno_cost_3.value,',','');
      field3 = parseFloat(field3);
    }
    
    
    if (document.thinkspaceform.planning_6months_cost_1.value == '') {
      field4 = 0;
    } else {
      field4 = replace(document.thinkspaceform.planning_6months_cost_1.value,',','');
      field4 = parseFloat(field4);
    }

    order_total = field1 + field2 + field3 + field4;
    
    // Display the total rounded to two decimal places
    document.thinkspaceform.planning_totalcost.value = round_decimals(order_total, 2);
    
}

// Replace function
function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}


// Calculate on Equity Form

function equityFormCalculateTotal(formvalue) {
    var order_total = 0;
    var field1 = 0;
    var field2 = 0;
    var field3 = 0;
    var field4 = 0;
    
    if (document.form1.planning_reno_cost_1.value == '') {
      field1 = 0;
    } else {
      field1 = replace(document.form1.planning_reno_cost_1.value,',','');
      field1 = parseFloat(field1);
    }
    
    if (document.form1.planning_reno_cost_2.value == '') {
      field2 = 0;
    } else {
      field2 = replace(document.form1.planning_reno_cost_2.value,',','');
      field2 = parseFloat(field2);
    }
    
    
    if (document.form1.planning_reno_cost_3.value == '') {
      field3 = 0;
    } else {
      field3 = replace(document.form1.planning_reno_cost_3.value,',','');
      field3 = parseFloat(field3);
    }
    
    
    if (document.form1.planning_6months_cost_1.value == '') {
      field4 = 0;
    } else {
      field4 = replace(document.form1.planning_6months_cost_1.value,',','');
      field4 = parseFloat(field4);
    }

    order_total = field1 + field2 + field3 + field4;
    
    // Display the total rounded to two decimal places
    document.form1.planning_totalcost.value = round_decimals(order_total, 2);
    
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

// Define print or transfer action
 function OnSubmitForm(x) {
  if(x == 'print')
  {
    document.thinkspaceform.action ="thinkspace-print-page.php";
    win = window.open('', 'thinkSpacePrintPage',
    'dialog,modal,scrollbars=yes,toolbar=yes,resizable=yes,width=750,height=540,left=150,top=75');
    document.thinkspaceform.target='thinkSpacePrintPage';
    document.thinkspaceform.submit();
    blur();
  }
  else
  {
    // document.thinkspaceform.action ="home_equity_loan.php?link=thinkspace";
    document.thinkspaceform.action ="https://www.capitaldirect.ca/home_equity_loan.php?link=thinkspace";
    document.thinkspaceform.target='_self';
    document.thinkspaceform.submit();
  }
  return false;
}

function onPrintPage() {
  win = window.open('', this.target,
  'dialog,modal,scrollbars=yes,resizable=no,width=30 0,height=200,left=362,top=284');
  document.thinkspaceform.target='myWin';
  document.thinkspaceform.submit();
}
//-->