function ClearOptions(OptionList) {

   // Always clear an option list from the last entry to the first
   for (x = OptionList.length; x >= 0; x = x - 1) {
      OptionList[x] = null;
   }
	return true;
}

function AddToOptionList(OptionList, OptionValue, OptionText) {
   // Add option to the bottom of the list
   OptionList[OptionList.length] = new Option(OptionText, OptionValue);
}

function PopulateAge(StatusList) {
    //should rename function to ChangeAge to match rest
	//alert(StatusList.options[StatusList.selectedIndex].text);
	var AgeList = document.frmMain[StatusList.name.replace(/status/,"age")];
    var BoatList = document.frmMain[StatusList.name.replace(/status/,"boat")];
    
	//alert(AgeList);
	if(StatusList.options[StatusList.selectedIndex].text == "Vet") {
		ClearOptions(AgeList);
		AgeList.disabled = false;
		AddToOptionList(AgeList, "A", "A");
		AddToOptionList(AgeList, "B", "B");
 	   	AddToOptionList(AgeList, "C", "C");
 	   	AddToOptionList(AgeList, "D", "D");
 	   	AddToOptionList(AgeList, "E", "E");
 	   	AddToOptionList(AgeList, "F", "F");
 	   	AddToOptionList(AgeList, "G", "G");
 	   	AddToOptionList(AgeList, "H", "H");
 	   	AddToOptionList(AgeList, "I", "I");
 	   	BoatIm(BoatList);
 	} else if (StatusList.options[StatusList.selectedIndex].text == "Junior") {
 	   	ClearOptions(AgeList);
 	   	AgeList.disabled = false;
 	   	AddToOptionList(AgeList, "J14", "J14");
 	   	AddToOptionList(AgeList, "J16", "J16");
 	   	BoatJunior(BoatList);
 	} else if (StatusList.value == "Senior") {
 	    ClearOptions(AgeList);
 	   	AgeList.disabled = true;
 	   	BoatSenior(BoatList);
 	} else if (StatusList.value.substring(0,4)=="Inte" || StatusList.value=="Novice") {
 	    ClearOptions(AgeList);
 	   	AgeList.disabled = true;
 	   	BoatIm(BoatList);
 	} else if (StatusList.value.substing(0,4)=="Mays") {
 	    ClearOptions(AgeList);
 	   	AgeList.disabled = true;
 	   	BoatStudent(BoatList);
 	} else {
 	   	ClearOptions(AgeList);
 	   	AgeList.disabled = true;
        ResetBoat(BoatList);
 	}
}

function SexChange(SexList) {
    var AffList = document.frmMain[SexList.name.replace(/sex/,"cra")];
    var BoatList = document.frmMain[SexList.name.replace(/sex/,"boat")];
    var StatusList = document.frmMain[SexList.name.replace(/sex/,"status")];
    
    if(SexList.options[SexList.selectedIndex].text == "Mixed") {
        //alert("Mixed");
        for(var i=0;i<AffList.length;i++) {
            if(AffList.options[i].value=="cra") AffList.selectedIndex =i;
        }
        ClearOptions(BoatList);
        AddToOptionList(BoatList, "8+", "8+");
        AddToOptionList(BoatList, "4+", "4+");
        AddToOptionList(BoatList, "2x", "2x");
        ClearOptions(StatusList);
        AddToOptionList(StatusList, "n/a", "n/a");
    } else if (AffList.value == "student") {
        BoatStudent(BoatList);
        StatusStudent(StatusList);
                
    } else {
        //put things back to normal
        ResetBoat(BoatList);
        ResetStatus(StatusList);
    }
}

function AgeChange(AgeList) {
    var BoatList = GetList(AgeList, 'boat');
    //alert(AgeList.value[0]);
    if(AgeList.value.substring(0,1)=="J") {
        BoatJunior(BoatList);
    }
}

function GetList(List, name) {
    return document.frmMain[List.name.replace(/.*\[/,name+"[")];
}

function BoatStudent(List) {
    	ClearOptions(List);
		AddToOptionList(List, "8+", "8+");
}

function BoatSenior(List) {
    	ClearOptions(List);
		AddToOptionList(List, "8+", "8+");
		AddToOptionList(List, "4+", "4+");
}

function BoatIm(List) {
        ClearOptions(List);
	    AddToOptionList(List, "8+", "8+");
	    AddToOptionList(List, "4+", "4+");
	    AddToOptionList(List, "2x", "2x");
    	AddToOptionList(List, "1x", "1x");
}

function BoatJunior(List) {
        var AgeList = GetList(List, 'age');
        ClearOptions(List);
        if(AgeList.value=="J14") {
            AddToOptionList(List, "4x+", "4x+");
        	AddToOptionList(List, "2x", "2x");
        	AddToOptionList(List, "1x", "1x");
        } else if (AgeList.value="J16") {
            AddToOptionList(List, "8+", "8+");
            AddToOptionList(List, "4+", "4+");
            AddToOptionList(List, "4x+", "4x+");
        	AddToOptionList(List, "2x", "2x");
        	AddToOptionList(List, "1x", "1x");
        } else {
            AddToOptionList(List, "8+", "8+");
            AddToOptionList(List, "4+", "4+");
            AddToOptionList(List, "4x+", "4x+");
        	AddToOptionList(List, "2x", "2x");
        	AddToOptionList(List, "1x", "1x");
        }
}

function StatusStudent(List) {
    	ClearOptions(List);
    	AddToOptionList(List, "Mays 1st", "Mays 1st");
		AddToOptionList(List, "Mays 2nd", "Mays 2nd");
		AddToOptionList(List, "Mays 3rd", "Mays 3rd");
		AddToOptionList(List, "Mays Lower", "Mays Lower");
}

function AffiliationChange(AffList) {
	var StatusList = document.frmMain[AffList.name.replace(/cra/,"status")];
	var BoatList = document.frmMain[AffList.name.replace(/cra/,"boat")];
	var AgeList = document.frmMain[AffList.name.replace(/cra/,"age")];
	var SexList = document.frmMain[AffList.name.replace(/cra/,"sex")];
	
	//ClearOptions(StatusList);
	//BoatList.disabled = false;
	if(AffList.options[AffList.selectedIndex].text == "Student") {
	    ClearOptions(AgeList);
		AgeList.disabled = true;
        BoatStudent(BoatList);
        StatusStudent(StatusList);
        ResetSex(SexList);
	} else if((AffList.options[AffList.selectedIndex].text == "CRA") || (AffList.options[AffList.selectedIndex].text == "ARA")){
        ResetStatus(StatusList);
        ResetBoat(BoatList);
        ResetSex(SexList);
		ClearOptions(AgeList);
		AgeList.disabled = true;
	    if((AffList.value == "ara")) {
            ClearOptions(SexList);
            AddToOptionList(SexList, "m", "Open");
            AddToOptionList(SexList, "w", "Womens");
	    }
	} else {
		ClearOptions(AgeList);
		ClearOptions(BoatList);
		ClearOptions(StatusList);
	}
}

function ResetBoat(List) {
    ClearOptions(List);
    AddToOptionList(List, "", "");
    AddToOptionList(List, "8+", "8+");
	AddToOptionList(List, "4+", "4+");
	AddToOptionList(List, "2-", "2-");
	AddToOptionList(List, "2+", "2+");
	AddToOptionList(List, "4x", "4x");
	AddToOptionList(List, "4x+", "4x+");
	AddToOptionList(List, "2x", "2x");
	AddToOptionList(List, "1x", "1x");
}

function ResetStatus(List) {
    ClearOptions(List);
    AddToOptionList(List, "", "");
    AddToOptionList(List, "Senior","Senior");
	AddToOptionList(List, "Intermediate 1","Intermediate 1");
	AddToOptionList(List, "Intermediate 2","Intermediate 2");
	AddToOptionList(List, "Intermediate 3","Intermediate 3");
	AddToOptionList(List, "Novice","Novice");
	AddToOptionList(List, "Junior","Junior");
	AddToOptionList(List, "Vet","Vet");
}

function ResetSex(List) {
    ClearOptions(List);
    AddToOptionList(List, "", "");
    AddToOptionList(List, "m", "Open");
    AddToOptionList(List, "w", "Womens");
    AddToOptionList(List, "mx", "Mixed");
}

function ClearOptionList() {
	for (i = 1; i < 11; i=i+1) {
		var AgeList = document.frmMain['age['+i+']'];
		var AgeListText = AgeList.options[AgeList.selectedIndex].text;
		//alert(AgeList.options[AgeList.selectedIndex].text);
		if( AgeListText == ""){
	    ClearOptions(AgeList);
		AgeList.disabled = true;
		}
	}
	return true;
}

function ResetForm() {
	for (i = 1; i < 11; i=i+1) {
		var AgeList = document.frmMain['age['+i+']'];
		var StatusList = document.frmMain['status['+i+']'];
		var BoatList = document.frmMain['boat['+i+']'];
		var SexList = document.frmMain['sex['+i+']'];
		ClearOptions(AgeList);
		ClearOptions(BoatList);
		ClearOptions(StatusList);
        ClearOptions(SexList);
	}
	return true;
}
