var sH = {
  // initialisation function, call with load
  init: function() {
    if (!document.getElementById) return;

		// define elements
		sH.monthly_income = document.getElementById('monthly_income');
		sH.monthly_childcare = document.getElementById('monthly_childcare');
		
		sH.weekly_income = document.getElementById('weekly_income');
		sH.weekly_childcare = document.getElementById('weekly_childcare');
		
		sH.formc = document.getElementById('formc');

    var all_input = document.getElementsByTagName('input');
		// loop though inputs
		for (var i = 0; i < all_input.length; i++) {
			// get the row element
			var inp = all_input[i];
			if (inp.name == 'monthly_weekly') {
		      	sH.addEvent(inp, 'click', sH.cliked, false);
			}
  	}
		
		// add event listeners
		if (sH.monthly_income)
    sH.addEvent(sH.monthly_income, 'change', sH.startup, false);
		if (sH.monthly_childcare)
    sH.addEvent(sH.monthly_childcare, 'change', sH.startup, false);

		if (sH.weekly_income)
    sH.addEvent(sH.weekly_income, 'change', sH.startup, false);
		if (sH.weekly_childcare)
    sH.addEvent(sH.weekly_childcare, 'change', sH.startup, false);

    sH.startup();
  },


	getRadioID: function(radioName) {
	  var collection;
	  collection = document.all[radioName];

	  for (i=0;i<collection.length;i++) {
		if (collection[i].checked)
	   		return(collection[i].id);
	   }
	},
  
  startup: function() {

		if (sH.monthly_income)
    sH.monthly_income.value = sH.format(sH.monthly_income.value);
		if (sH.monthly_childcare)
    sH.monthly_childcare.value = sH.format(sH.monthly_childcare.value);

		if (sH.weekly_income)
    sH.weekly_income.value = sH.format(sH.weekly_income.value);
		if (sH.weekly_childcare)
    sH.weekly_childcare.value = sH.format(sH.weekly_childcare.value);
  },
  
  cliked: function() {

    sH.formc.submit();
  },
  
  
	
	// function to add event listener, also caches events so they can be removed when the
	// page unloads to avoid memory leaks in IE
  addEvent: function(elm, evType, fn, useCapture) {
		// for W3C DOM complience
    if (elm.addEventListener) {
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } 
		// for IE...
		else if (elm.attachEvent) {
      var r = elm.attachEvent('on' + evType, fn);
      //EventCache.add(elm, evType, fn);
      return r;
    } else {
			// for anyone else not IE or Moz... Safari etc
      elm['on' + evType] = fn;
    }
  },
	
	// cross-browser get target
	find_target: function(e) 	{
		var target; 	
		// for IE, target is held in window.event array
		if (window.event && window.event.srcElement) 
			target = window.event.srcElement;
		else if (e && e.target)
			target = e.target;
		if (!target)
			return null;
			
		return target;
	},

  format: function (num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));

    //return (((sign)?'':'-') + num + '.' + cents);
    return (((sign)?'':'-') + num);
  },

  format_pence: function (num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));

    return (((sign)?'':'-') + num + '.' + cents);
    //return (((sign)?'':'-') + num);
  }
	
	
}

sH.addEvent(window, 'load', sH.init, false);
// flush events on unload to avoid memory probs in IE
//sH.addEvent(window, 'unload', EventCache.flush, false);
