//This array is only for reference
VAR_NAMES = new Array("A-G/A-F courses taken", "Eligible admit", "Eligibility in Local Context (ELC) status", "Ethnicity", "Field of study", "First-generation status", "Gender", "High school API rank", "High school GPA", "High school location", "Honors classes", "Language spoken at home", "Overall units", "Parent education", "Parent income", "Prior college GPA", "Region of home location", "SAT scores", "Transfer units", "Type of high school", "ACT scores");

VAR_IDS = new Array(37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57);

//This arrays contains the position of variable ids in the VAR_IDS array.
applicantFresh = new Array(0,2,3,4,6,7,8,9,10,11,13,14,16,17,19,20);
applicantTrans = new Array(3,4,6,15,11,12,13,14,16,18);
studentFresh = new Array(1,2,3,4,5,6,7,9,11);
studentTrans = new Array(1,3,4,5,6,15,11);

function hideCheckboxes(characterArray)
{
    $("#applicant_characteristics").children().each(function()
	{
	    for(i = 0; i < characterArray.length; i++)
	    {
		if(Number($(this).children().eq(0).val()) == VAR_IDS[characterArray[i]])
		{
		    $(this).show();
		    return;
		}
	    }
	    $(this).children().eq(0).attr("checked", false); 
	    $(this).hide();
	});
}     

function determineVariableSet()
{
    //if persistence track
    if ($("input#studentRadio").is(":checked"))
    {
	//if freshman
	if($("input#freshmenRadio").is(":checked"))
	{
	    hideCheckboxes(studentFresh);                          
	}
	//if transfer
	else
	{
	    hideCheckboxes(studentTrans);
	}
    }
    //if applicant track
    else
    {
	//if freshman
	if($("input#freshmenRadio").is(":checked"))
	{
	    hideCheckboxes(applicantFresh);                          
	}
	//if transfer
	else
	{
	    hideCheckboxes(applicantTrans);
	}
    }
}

function validateDropDown(dropDown1, dropDown2)
{
    if(dropDown1.val() < 2)
    {
	dropDown2.children().eq(2).attr({selected:"selected"});
	dropDown1.children().eq(0).attr({selected:"selected"});
    }
    else
    {
	dropDown2.children().eq(0).attr({selected:"selected"});
    }
}

function hideShowFullYears()
{
	if ($("input#studentRadio").is(':checked') || $("input#freshmenRadio").is(':checked'))
	{		
		//Select 2006 if student is checked; 2007 otherwise
		var fallSelectedYear = $("input#studentRadio").is(':checked') ? 3 : 2;		
		$("#term_fall").children().eq(fallSelectedYear).attr({selected:"selected"});
		
		//hide fall option (first element)
		$("#term_fall").children(":lt("+fallSelectedYear+")").hide();
		
		//hide full year drop down
		$("#or").hide();
		$("#term_fullYear").hide();
		
		//display correct variable set
		determineVariableSet();
	}
    	//if applicant track
    	else
    	{
		$("#term_fall").children(":lt(2)").show();
		validateDropDown($("#term_fall"), $("#term_fullYear"));
		$("#or").show();
		$("#term_fullYear").show();
		determineVariableSet();
    	}
}

$(document).ready(function(){
    determineVariableSet();
    hideShowFullYears();
    $("input[name=track_attributes]").click(function()
	{
	   hideShowFullYears();
	});	
    $("input[name=applicant_attributes]").click(function()
	{
	    hideShowFullYears();
	    determineVariableSet();
	});
    $("#term_fall").change(function()
	{
	    validateDropDown($(this), $("#term_fullYear"));
	});
    $("#term_fullYear").change(function()
	{
	    validateDropDown($(this), $("#term_fall"));
	});
});