
var f;
var num_fields = ['total_dev_cost','total_redev_cost','total_reno_cost','total_expan_cost','total_dev_cost_2009','total_redev_cost_2009','total_reno_cost_2009','total_expan_cost_2009','cost_1','cost_2','cost_3','cost_4','cost_5'];

function addCommas(nStr)
{
        nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	} 
	return x1 + x2;
}

function format_money(v) {
    return "$ "+addCommas(v);
}

function format_number(v) {
    return v;
    //return addCommas(v);
}

function get_number(f, name) {
    var v = f[name].value;
    var fv = parseFloat(v.replace(',',''));
    if (fv+'' == 'NaN') {
	return 0;
    }
    return (v == '') ? 0 : fv;
}

function survey_init() {
   f = $('survey');
   if (f != null) {
     Event.observe(f,'submit',survey_submit);
     for (var i=0; i < num_fields.length; i++) {
        var k = num_fields[i];
        Event.observe(f[k+'_hund'],'change', survey_change);
        Event.observe(f[k+'_thou'],'change', survey_change);
        Event.observe(f[k+'_mil'],'change', survey_change);
     }
   }
}

function survey_change(e) {
   recalculate_form(this);
}

function recalculate_form(field) {
   for (var i=0; i < num_fields.length; i++) {
      var k = num_fields[i];
      var v = 0;
      v += get_number(f,k+'_hund');
      v += get_number(f,k+'_thou') * 1000;
      v += get_number(f,k+'_mil') * 1000000;
      f[k].value = v;
   }
}




function survey_init_old() {
    f = $('survey');
    Event.observe(f,'submit',survey_submit);
    Event.observe(f.total_cost_mil, 'keyup', concat_field);
    Event.observe(f.total_cost_thou, 'keyup', concat_field);
    Event.observe(f.total_cost_hund, 'change', concat_field);

    Event.observe(f.total_cost_2008_mil, 'keyup', concat_field);
    Event.observe(f.total_cost_2008_thou, 'keyup', concat_field);
    Event.observe(f.total_cost_2008_hund, 'change', concat_field);

    Event.observe(f.cost_1_mil, 'keyup', concat_field);
    Event.observe(f.cost_1_thou, 'keyup', concat_field);
    Event.observe(f.cost_1_hund, 'change', concat_field);

    Event.observe(f.cost_2_mil, 'keyup', concat_field);
    Event.observe(f.cost_2_thou, 'keyup', concat_field);
    Event.observe(f.cost_2_hund, 'change', concat_field);

    Event.observe(f.cost_3_mil, 'keyup', concat_field);
    Event.observe(f.cost_3_thou, 'keyup', concat_field);
    Event.observe(f.cost_3_hund, 'change', concat_field);

    Event.observe(f.cost_4_mil, 'keyup', concat_field);
    Event.observe(f.cost_4_thou, 'keyup', concat_field);
    Event.observe(f.cost_4_hund, 'change', concat_field);

    Event.observe(f.cost_5_mil, 'keyup', concat_field);
    Event.observe(f.cost_5_thou, 'keyup', concat_field);
    Event.observe(f.cost_5_hund, 'change', concat_field);
}

function grid_refresh() {
    var total;
    var client = get_number(f,'client_space_managed_sq_ft');
    var company = get_number(f,'company_space_managed_sq_ft');

    total = client + company;
    $('total_space_managed_text').update(format_number(total)); 
    f.total_space_managed_sq_ft.value = total;

    if (f.client_space_managed_sq_ft.value != '') { f.client_space_managed_sq_ft.value = format_number(client);}
    if (f.company_space_managed_sq_ft.value != '') {f.company_space_managed_sq_ft.value = format_number(company);}
}

function concat_field(e) {
    var concat_values = new Array();
    var total = new Array();
    var field_name = Event.element(e).name;
    var hidden_field = field_name.slice(0, field_name.lastIndexOf('_'));
    var field_place = field_name.slice(field_name.lastIndexOf('_')+1);

    switch(hidden_field) {
       case 'total_cost': {
          concat_values = ['mil', 'thou', 'hund'];
          break;
       }
       case 'total_cost_2008': {
          concat_values = ['mil', 'thou', 'hund'];
          break;
       }
       default: {
          concat_values = ['mil', 'thou', 'hund'];
          break;
       }
    }

    for (var i=0; i < concat_values.length; i++) {
      v_field = hidden_field + '_' + concat_values[i];
      total[i] = f[v_field].value;
    }

    f[hidden_field].value = total.join();

    if (field_place == concat_values[concat_values.length-1]) {
       return;
    }

    if (f[field_name].value.length == 3) {
      if (e.keyCode != 9 && e.keyCode != 16) {
        var next_elem = Event.element(e).next();
        $(next_elem).focus();
     }
   }
}



function survey_submit(e) {
    if (!f.signature_name.checked) {
	alert('Please check the verification box at the bottom of the page to continue.');
	Event.stop(e);
    }
  
}


Event.observe(window,'load',survey_init);