/* Utility Functions */

// Currency formatting functions
String.prototype.group = function( chr, size ) {
  if ( typeof chr == 'undefined' ) chr = ",";
  if ( typeof size == 'undefined' ) size = 3;
  return this.split( '' ).reverse().join( '' ).replace( new RegExp( "(.{" + size + "})(?!$)", "g" ), "$1" + chr ).split( '' ).reverse().join( '' );	
}
 
Number.prototype.group = function( chr, size ) {
  var num = this.toString().split( "." );
  num[0] = num[0].group( chr, size );
  return num.join( "." );
}

Number.prototype.round = function( places ) {
  var rounder = Math.pow( 10, places );
  return Math.round( this * rounder ) / rounder;
}
 
Number.prototype.toCurrency = function( unit, point, unitAfter ) {
  if ( typeof unit == "undefined" ) unit = "$";
  if ( typeof point == "undefined" ) point = ".";		
  amt = this.round(2).group((point == ".") ? "," : ".").replace(".", point);
  if ( /[.,]\d$/.test( amt ) ) amt += "0";
  //if (!/\.\d/.test (amt)) amt += ".00";
  return ( unitAfter ) ? amt + " " + unit : unit + amt;
}

function isNum(value) {
  return parseInt(value) == value;
}

function calcTotalWealth(which) {
  var all = false;
  if (which == null) {
    all = true;
  }

  if (which == 'personal' || all) {
    var fields = new Array(
      'private_home', 'cars', 'home_furnish', 
      'jewellery', 'electronic_equipment', 'personal_other1_value'
    );
    var effect = $('personal_asset_total');
    effectTotalWealth(fields, effect);
  }
  
  if (which == 'investment' || all) {
    var fields = new Array(
      'cash_deposits', 'property_portfolio', 'share_portfolio', 
      'superannuation', 'business_interests', 'investment_other1_value'
    );
    var effect = $('investment_asset_total');
    effectTotalWealth(fields, effect);
  }

  if (which == 'debt' || all) {
    var fields = new Array(
      'home_mortgage', 'car_loan', 'cc_debts', 
      'personal_loans', 'property_related_debt', 'business_loans',
      'hecs_loans', 'debt_other1_value'
    );
    var effect = $('debt_asset_total');
    effectTotalWealth(fields, effect);
  }
}

function effectTotalWealth(fields, effect) {
  var total = 0;

  for (var j=0; j<fields.length; j++) {
    var field = $('edit-' + fields[j]);
    var fvalue = parseFloat(stringFilter(field.value,'$, '));
    total += fvalue > 0 ? fvalue : 0;
  }

  effect.innerHTML = total.toCurrency();
}

function stringFilter(s, filteredValues) {
  var returnString = "";
  for (var i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if (filteredValues.indexOf(c) == -1) returnString += c;
  }
  return returnString;
}

function wealth_popup(url) {
  day = new Date();
  id = day.getTime();
  eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=400,left = 640,top = 375');");
}

function wealth_updateField(target) {
  var fvalue = parseFloat(stringFilter(target.value,'$, '));
  target.value = fvalue > 0 ? fvalue.toCurrency('') : 0;
}

/*function pi_wealth_addOther(element, label, value) {
  var re = new RegExp('edit-(.+)_other([0-9]+)_(.+)$');
  var m = re.exec(element.id);

  if (element.value == 'Other') {
    element.value = '';
  }

  if (m != null) {
    var section = m[1];
    var count = parseInt(m[2]);
    var type = m[3];

    if (count >= 6 || $('edit-'+ section +'_other'+ (count+1) +'_label')) {
      return;
    }
    else {
      var insertPoint = $(section);
      var numRows = insertPoint.rows.length;
      var emptyRow = insertPoint.insertRow(numRows-1);
      emptyRow.className = numRows % 2 ? 'odd' : 'even';

      var emptyCell = emptyRow.insertCell(0);
      var otherLabel = document.createElement('INPUT');
      otherLabel.setAttribute('type', 'textfield');
      otherLabel.setAttribute('name', 'edit['+ section +'_other'+ (count+1) +'_label]');
      otherLabel.setAttribute('id', 'edit-'+ section +'_other'+ (count+1) +'_label');
      otherLabel.setAttribute('value', (label == null ? 'Other' : label));
      otherLabel.setAttribute('class', 'form-text');
      otherLabel.setAttribute('onclick', 'pi_wealth_addOther(this);');
      emptyCell.appendChild(otherLabel);

      var emptyCell = emptyRow.insertCell(1);
      emptyCell.setAttribute('class', 'field_column');
      var otherValue = document.createElement('INPUT');
      otherValue.setAttribute('type', 'textfield');
      otherValue.setAttribute('name', 'edit['+ section +'_other'+ (count+1) +'_value]');
      otherValue.setAttribute('id', 'edit-'+ section +'_other'+ (count+1) +'_value');
      otherValue.setAttribute('value', (value == null ? '' : value));
      otherValue.setAttribute('class', 'form-text');
      emptyCell.appendChild(otherValue);
    }
  }
}*/
