var countA = 2;
var countB = 2;
var countX = 2;

function addChild(lid) {
	if (lid=='A') {
		$("#listA").append("<p id='rowA" + countA + "'><label for='nameA" + countA + "' style='margin-right: 10px;'>Name of Child</label> <input type='text' size='20' name='nameA[]' id='nameA" + countA + "'><br /><label for='nricA" + countA + "' style='margin-right: 5px;'>Child NRIC No.</label> <input type='text' size='20' name='nricA[]' id='nricA" + countA + "'></p>");
		
		$("#rowA" + countA).highlightFade({
		speed:1500
		});
		
		countA = countA+1;
	}
	if (lid=='B') {
		$("#listB").append("<p id='rowB" + countB + "'><label for='nameB" + countB + "' style='margin-right: 10px;'>Name of Child</label> <input type='text' size='20' name='nameB[]' id='nameB" + countB + "'><br /><label for='nricB" + countB + "' style='margin-right: 5px;'>Child NRIC No.</label> <input type='text' size='20' name='nricB[]' id='nricB" + countB + "'></p>");
		
		$("#rowB" + countB).highlightFade({
		speed:1500
		});
		
		countB = countB+1;
	}
}

/**
 * param validate boolean
 *    If set to false, it will return the total number of children successfully registered in both groups. Otherwise, it will act as a validator.
 *    Only use false to prompt user to confirm # of children entered on form submit after children is validated.
 * return int
 *    If validate is false, return value is total number of children successfully registered.
 *    Otherwise, return value is:
 * 		0 = No error found
 * 		1 = Found error in Group A
 *    	2 = Found error in Group B
 */
function checkChild(validate) {
	var a = $("div#listA p").length;
	var b = $("div#listB p").length;
	var total = 0;
	var error = 0;
	
	for (i = 1; i <= a; i++) {
		if (($("input#nameA"+i).val() && !$("input#nricA"+i).val()) || (!$("input#nameA"+i).val() && $("input#nricA"+i).val())) {
			error = 1;
			if (validate) return error;
		} else if ($("input#nameA"+i).val() && $("input#nricA"+i).val()) {
			total++;
		}
	}
	for (i = 1; i <= b; i++) {
		if (($("input#nameB"+i).val() && !$("input#nricB"+i).val()) || (!$("input#nameB"+i).val() && $("input#nricB"+i).val())) {
			error = 2;
			if (validate) return error;
		} else if ($("input#nameB"+i).val() && $("input#nricB"+i).val()) {
			total++;
		}
	}
	if (validate) {
		return error;
	}
	return total;
}

// To make sure a parent registers him/herself as well if a spouse is registered.
// A spouse must be accompanied by the parent registrant.
function checkParent() {
	if (!$('input[name=parents]').attr('checked')) $('input[name=spouse]').attr('checked', false);
}
function checkSpouse() {
	if ($('input[name=spouse]').attr('checked')) $('input[name=parents]').attr('checked', $('input[name=spouse]').attr('checked'));
}

function resetType() {
if ($("#cust_type").val() == "D") 
			$("#tr-bin").toggle().highlightFade();
		else 
			$("#tr-bin").fadeOut("fast");
}
function resetChildrenAge() {
	$("#listX").empty();
	$("#listX").html('<table id="ageTable"></table>');
	var numChildren = parseInt($("#noChildren").val());
	if (numChildren > 0) {
		for (var i = 1; i <= numChildren; i++) {
			var option = '';
			for (var n = 1; n <= 20; n++) {
				option = option + '<option value="' + n + '">' + n + '</option>';
			}
			
			switch (i) {
				case 1: name = 'First'; break;
				case 2: name = 'Second'; break;
				case 3: name = 'Third'; break;
				case 4: name = 'Fourth'; break;
				case 5: name = 'Fifth'; break;
				case 6: name = 'Sixth'; break;
				case 7: name = 'Seventh'; break;
				case 8: name = 'Eigth'; break;
			}
		$("#ageTable").append(
			'<tr><td width="150"><label for="age' + i + '" style="margin-right:10px;">Age of ' + name + ' Child</label></td>' + 
			'<td><select name="age[]" id="age' + i + '">' + option + '</select></td></tr>'
			).fadeIn("fast").highlightFade({ speed:1500 });
		}
	}
}

$(document).ready(function(){ 
	$("#cust_type").change(function () {
		resetType();
	});
	$("#noChildren").change(function () {
		resetChildrenAge();
	});
	// Firefox fix upon page refresh
	resetType();
	resetChildrenAge();
});
