function removeChildBox()
{
	var node = document.getElementById('childrow' + numberOfChildren);
	node.parentNode.removeChild(node);
	numberOfChildren--;
}

function getNewChildBox()
{
	numberOfChildren++;		
	var tr = document.createElement('tr');
	tr.setAttribute('id', 'childrow' + numberOfChildren);
	var td = document.createElement('td');
	td.style.paddingLeft = '15px';
	tr.appendChild(td);
	txt = document.createTextNode('Child ' + numberOfChildren + ': ');
	td.appendChild(txt);
	var td2 = document.createElement('td');
	tr.appendChild(td2);
	var input = document.createElement('input');
	input.setAttribute('type', 'text');
	input.setAttribute('id', 'child' + numberOfChildren);
	input.setAttribute('name', 'child' + numberOfChildren);
	input.setAttribute('maxlength', '4');
	input.style.width = '30px';
	
	td2.appendChild(input);
	
	var s = document.getElementById('childplaceholder');
	s.parentNode.parentNode.parentNode.insertBefore(tr, s.parentNode.parentNode);
}

qbf.onsubmit = function ()
{	
	
	var error = '';
	// get infants via id to avoid name collision
	inf = document.getElementById("infants");
	
	total_pax = 0;
	if(this.adults.value > 0)
	{
		total_pax += parseInt(this.adults.value);
	}
	else
	{
		error = error + "There must be at least one adult in the booking. \n";
	}
	
	if(this.children.value > 0)
	{
		total_pax += parseInt(this.children.value);
	}
	
	if(inf.value > 0)
	{
		total_pax += parseInt(inf.value);
	}
	
	if(total_pax > max_pax)
	{
		error = error + "Only a maximum of nine passengers may be in a single booking.\n";
	}
	
	for(var i = 1; i <= this.children.value; i++)
	{
		age = eval('this.child' + i + '.value');
		if(age < min_child_age || age > max_child_age)
		{
			error = error + "Children must be aged between " + min_child_age + " and " + max_child_age + " \n";
			break;
		}
	}
	
	
	if(this.airportgroupfromid && this.airportgroupfromid.value == '')
	{
		error = error + "Please select where you are travelling from.\n";	
	}
	
	if(this.airportfromid && this.airportfromid.value == '')
	{
		error = error + "Please choose a departure airport.\n";	
	}
	
	if(this.airportcombinedfromid && this.airportcombinedfromid.value.substring(0,1) != '|')
	{
		error = error + "Please choose a departure airport.\n";	
	}
	
	
	if((this.airportgrouptoid && this.airportgrouptoid.value == '-1')
		||
	  (this.resortgroupid && this.resortgroupid.value == ''))
	{
		error = error + "Please choose a destination.\n";	
	}
	if(this.airporttoid && this.airporttoid.value == '-1')
	{
		error = error + "Please choose a destination airport.\n";	
	}
	
	if(this.boardbasis && this.boardbasis.value == '-1')
	{
		error = error + "Please choose a board basis.\n";	
	}
	/*
		if(this.airportcombinedtoid && this.airportcombinedtoid.value.substring(0,1) != '|')		
		{
			error = error + "Please choose a destination airport.\n";	
		}
		*/
	
	if(this.airportcombinedtoid && this.airportcombinedtoid.value == -1)		
	{
		error = error + "Please choose a destination airport.\n";	
	}
	

	if(this.duration && this.duration.value == '-1')
	{
		error = error + "Please choose a duration.\n";
	}

	if(error.length > 0)
	{
		alert(error);
		return false;
	}
	
	return saveFormValues(this, "AO", true, false);
}	

hasjs = document.getElementById("hasjavascript");
hasjs.value = 1;

var i1 = document.getElementById("children");
		
i1.onchange = redrawChildBoxes;
i1.onkeyup = redrawChildBoxes;

function redrawChildBoxes() 
{
	var val = this.value + 0;
	var i = 0;
	if(this.value < 0)
	{
		return;
	}
	else if(this.value > numberOfChildren)
	{
		while(this.value > numberOfChildren && numberOfChildren < 8 && i < 8)
		{
			getNewChildBox();
			i++;
		}
	}
	else
	{
		while(this.value < numberOfChildren && numberOfChildren > 0 && i < 8)
		{
			removeChildBox();
			i++;
		}
	}
	
	if ( typeof(childheader) != 'undefined' )
	{
		if(numberOfChildren > 0)
		{
			childheader.style.display = ''; // default
		}
		else
		{
			childheader.style.display = 'none';
		}
	}
};
