var i;
var j;
			
function compareNumbers(a, b) 
{
	return a - b;
} 


function onSmallFormAgeChangeHandler()
{
	return SmallFormAgeChangeHandler("CHANGE")
}
	

function SmallFormAgeChangeHandler(pstrMode)
{
	var nS17;
	var nS16;
	var strHTML;
	var intFirstPaxIndex;

	if (pstrMode=="LOAD") {

		if (cgintAFNumPax == 0) {
			HideAllChildAges();
			return 0;
		}

		//Check data integrity.
		//all non-valid ages must be -1 for the 0th - 9th data entries.
		//i.e 0 is a valid age, and not an empty var.
		var intCheck=0;
		for (i=0;i<=9;i++)
			if (cgintAFPaxAge[i]>=0)
				intCheck++;
		if (!(intCheck==cgintAFNumPax)) {
			alert("Data integrity error! Code AR20050201")
			return 0;
		}
	}
					
	if (pstrMode=="CHANGE")
	{
		//Grab from form.
		nS17=docref("S17").value;
		nS16=docref("S16").value;
		intFirstPaxIndex=-1;	//mark as unused.
	}
	else
	{
		//page load - Deduce from global variables.
		nS17=0;
		nS16=0;

		for (i=0;i<=9;i++)
			if (cgintAFPaxAge[i] >=17)
				nS17++;
		nS16=cgintAFNumPax-nS17;
		//Sort it - needed.
		cgintAFPaxAge.sort(compareNumbers);
					
		//locate first valid (i.e non -1 value)
		intFirstPaxIndex=-1;
		for (i=0;i<=9;i++)
			if (cgintAFPaxAge[i]!=-1)
				{
					if (intFirstPaxIndex==-1)
						intFirstPaxIndex=i;
				}
	}

	strHTML="";

	var i;

	if (nS16 == 0)	{
		HideAllChildAges();
	}
	else {
		docref("ChildAgesRow1").style.display = 'block';
		
		if (nS16 <= 4) docref("ChildAgesRow2").style.display = 'none';
		else docref("ChildAgesRow2").style.display = 'block';

		var intAgeIndex = 0;
		var intAge;
        // set child ages using _cgintAFPaxAge[]_
        for(i=1; i<=nS16; i++) {
			var pax = document.frmForm.selPaxLX[i];
			pax.style.visibility = 'visible';

			if(pstrMode=='LOAD') {

				for(;intAgeIndex<10; intAgeIndex++) {
					intAge = cgintAFPaxAge[intAgeIndex];
					if(intAge > -1 & intAge < 17) {
						break;
					}
				}
				if (intAgeIndex == 10) {
					alert("ERROR: first_scripts.js - age not found");
				}
				else {
					pax.options[intAge+1].selected = true;
				}
				intAgeIndex++;
			}
        }
        if(nS16 < 10) {
            for(i=Number(nS16)+1; i<=10; i++) {
				document.frmForm.selPaxLX[i].style.visibility = 'hidden';
				document.frmForm.selPaxLX[i].value = -1;
            }
        }
	}

}


function HideAllChildAges() {

	docref("ChildAgesRow1").style.display = 'none';
	docref("ChildAgesRow2").style.display = 'none';

    for(i=1; i<=10; i++) {
    	document.frmForm.selPaxLX[i].style.visibility = 'hidden';
    	document.frmForm.selPaxLX[i].value = -1;
    }
}


function onAgeChangeHandler()
{
	return AgeChangeHandler("CHANGE")
}


function AgeChangeHandler(pstrMode)
{
	var nS17;
	var nS16;
	var strHTML;
	var intFirstPaxIndex;
				
	if (pstrMode=="LOAD")
	{
		//alert("LOAD!")
		if (!(cgintAFNumPax>0))
			{
				//alert("LOAD ZERO! Code ARLZ20050201")
				return 0;
			}
	}
				
	if (pstrMode=="LOAD")
	{
		//Check data integrity.
		//The all non-valid ages must be -1 for the 0th - 9th data entries.
		//i.e 0 is a valid age, and not an empty var.
		var intCheck=0;
		for (i=0;i<=9;i++)
			if (cgintAFPaxAge[i]>=0)
				intCheck++;
		if (!(intCheck==cgintAFNumPax))
			{
				alert("Data integrity error! Code AR20050201")
				return 0;
			}
	}
					
	if (pstrMode=="CHANGE")
	{
		//Grab from form.
		nS17=docref("S17").value;
		nS16=docref("S16").value;
		intFirstPaxIndex=-1;//mark as unused.
	}
	else
	{
		//page load - Deduce from global variables.
		nS17=0;
		nS16=0;
					
		for (i=0;i<=9;i++)
			if (cgintAFPaxAge[i] >=17)
				nS17++;
		nS16=cgintAFNumPax-nS17;
		//Sort it - needed.
		cgintAFPaxAge.sort(compareNumbers);
					
		//locate first valid (i.e non -1 value)
		intFirstPaxIndex=-1;
		for (i=0;i<=9;i++)
			if (cgintAFPaxAge[i]!=-1)
				{
					if (intFirstPaxIndex==-1)
						intFirstPaxIndex=i;
				}
	}
				
	strHTML="";
				
	if (nS16>0)
	{
		/* Capture ages of those under 17 */
		var icnt;
				
		strHTML=strHTML + "<BR>";
		strHTML=strHTML + "Please enter the ages for those 16 or under" + "<BR>"
		//strHTML=strHTML + "Pax 1 to " + nS17 + " : " + writePaxAge17Hard() + "<BR>"
				
		icnt=nS17;
		var intThisPaxAge;
					
		for (i=1;i<=nS16;i++)
		{	
			icnt++;
			strHTML=strHTML + "* Age : ";
						
			if (pstrMode=="LOAD")
				intThisPaxAge=cgintAFPaxAge[intFirstPaxIndex];
			else
				intThisPaxAge=-1;
							
			strHTML=strHTML + writePaxLXSelect(i,intThisPaxAge) + "<BR>";
			if 	(pstrMode=="LOAD")
				intFirstPaxIndex++;
						
		}
		strHTML=strHTML + "<BR>";
		strHTML=strHTML + "* Age at departure date";
					
		//document.writeln(strHTML)
	}
	if (docref("SPX"))
		docref("SPX").innerHTML=strHTML;
					
	//alert(docref("SPX").innerHTML);
	//docref("SPX").innerHTML=strHTML;
	//alert(strHTML)
				
				
}
			
function writePaxLXSelect(i,pintThisPaxAge)
{
	var s;
	var strSelected;
	strSelected="";
				
	//s="<Select id=selPaxLX" + i + " name=selPaxLX" + i + ">"
	s="<Select id=selPaxLX name=selPaxLX>"
	if (pintThisPaxAge==-1)
		strSelected="selected"
	else
		strSelected=""
					
	s=s + "<option value=-1" + " " + strSelected + ">----</option>"
	for (i=0;i<17;i++)
	{
		if (i==pintThisPaxAge)
			strSelected="selected"
		else
			strSelected=""
						
		s=s + "<option value=" + i + " " + strSelected + ">" + i + "</option>"
	}
	s=s+"</Select>"
	return s;	
}

function writePaxAge17Hard()
{
	var s;
	s="<Select id=sel1 name=sel1>"
	s=s + "<option value=17>17+</option>"
	s=s+"</Select>"
	return s;	
}
			
function docref(strRef)
{
				
	//return document.all(strRef)	
	return document.getElementById(strRef)
				
}
			
