document.populateChildName = function() {
	var i = 1, first, last, names = [], waiverName;
	while((first = $('childFirstName'+i)) && (last = $('childLastName'+i))) {
		if(first.value != '' || last.value != '') {
			names.push(first.value+' '+last.value);
		}
		i++;
	}
	
	if(names.length && (waiverName = $('childNameWaiver'))) {
		waiverName.innerHTML = names.join(', ');
	}
}

function createExpirationDate(dateFieldID, monthFieldID, yearFieldID) {
	var dateField = document.getElementById(dateFieldID);
	var monthField = document.getElementById(monthFieldID);
	var yearField = document.getElementById(yearFieldID);

	if (dateField && monthField && yearField) {
		if (monthField.value != "" || yearField.value != "") {
			dateField.value = monthField.value + "/" + yearField.value;
		}
		else {
			dateField.value = "";
		}
		
	}

}

/* number_format Made by Mathias Bynens <http://mathiasbynens.be/> */
function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}

document.scrubValue = function(el) {
	if(el.value.search(/[^\d]/) != -1) {
		el.value = el.value.replace(/[^\d]/g, '');
	}
}

document.regDonationupdate = function(el, fee) {
	var dEl = $('regDonation'), tEl = $('regTotal');
	if(dEl && tEl) {
		var fee = parseFloat(fee);
		var donation = parseFloat(el.value);
		if(isNaN(donation)) donation = 0;
		if(isNaN(fee)) fee = 0;
		var total = fee + donation;
		dEl.innerHTML = '$'+number_format(donation, (Math.floor(donation) == donation ? 0 : 2), '.', ',');
		tEl.innerHTML = '$'+number_format(total, (Math.floor(total) == total ? 0 : 2), '.', ',');
	}
}
