var C_CHOOSE = '<escolher>';
var C_VAL_NULL = '#NULL#';

//check to see if browser is IE
function isMicrosoftBrowser() {
	return navigator.appName.indexOf('Microsoft') != -1;
}

function AddOption(oSelect, lab, val)
{
		var oOption = document.createElement("OPTION");
		oOption.text = lab;
		oOption.value = val;
		if (isMicrosoftBrowser())
		{
			oSelect.add(oOption);
		}
		else
		{
			oSelect.appendChild(oOption);
		}
}

function FindLevelInstance(arr, id)
{
	for(var i=0 ; i< arr.length ; i++)
	{
		if(arr[i][1] == id)
		{
			return arr[i];
		}
	}
	throw new Error("FindLevelInstance: Couldn't find id '" + id + "'.");
}

function FillInLevelCombos(cLev1, cLev2, cLev3)
{
	//Level 1
	FillInLevel1Combo(cLev1);

	//Level 2
	FillInLevel2Combo(cLev2, C_VAL_NULL);

	//Level 3
	FillInLevel3Combo(cLev3, C_VAL_NULL, C_VAL_NULL);
}

function FillInLevel1Combo(cLev)
{
	AddOption(cLev, C_CHOOSE, C_VAL_NULL);
	for(var i=0 ; i<g_aLevel1.length ; i++)
	{
		AddOption(cLev, g_aLevel1[i][0], g_aLevel1[i][1]);
	}
}

function FillInLevel2Combo(cLev, idL1)
{
	cLev.options.length = 0;
	AddOption(cLev, C_CHOOSE, C_VAL_NULL);
	for(var i=0 ; i<g_aLevel2.length ; i++)
	{
		if( idL1 == C_VAL_NULL || idL1 == g_aLevel2[i][2] )
		{
			AddOption(cLev, g_aLevel2[i][0], g_aLevel2[i][1]);
		}
	}
}

function FillInLevel3Combo(cLev, idL1, idL2)
{
	cLev.options.length = 0;
	AddOption(cLev, C_CHOOSE, C_VAL_NULL);
	for(var i=0 ; i<g_aLevel3.length ; i++)
	{
		if( (idL1 == C_VAL_NULL || idL1 == g_aLevel3[i][3]) && (idL2 == C_VAL_NULL || idL2 == g_aLevel3[i][2]) )
		{
			AddOption(cLev, g_aLevel3[i][0], g_aLevel3[i][1]);
		}
	}
}

function ChangeLevel1Combo(cLev1, cLev2, cLev3)
{
	var idL1 = cLev1.value;
	FillInLevel2Combo(cLev2, idL1);
	FillInLevel3Combo(cLev3, idL1, C_VAL_NULL);
	
}

function ChangeLevel2Combo(cLev1, cLev2, cLev3)
{
	var idL2 = cLev2.value;
	FillInLevel3Combo(cLev3, C_VAL_NULL, idL2);

	if(idL2 != C_VAL_NULL){
		var aL2 = FindLevelInstance(g_aLevel2, idL2);
		var idL1 = aL2[2];
		cLev1.value = idL1;
	}
	
}

function ChangeLevel3Combo(cLev1, cLev2, cLev3)
{
	var idL3 = cLev3.value;
	if(idL3 != C_VAL_NULL){

		var aL3 = FindLevelInstance(g_aLevel3, idL3);
		var idL1 = aL3[3];
		var idL2 = aL3[2];
		cLev1.value = idL1;
		cLev2.value = idL2;
	}	
}

function SetLevel3Combo(cLev1, cLev2, cLev3, val3)
{
	//Must reset all combos
	cLev1.value = C_VAL_NULL;	
	ChangeLevel1Combo(cLev1, cLev2, cLev3);
	
	//Change level 3
	cLev3.value = val3;
	ChangeLevel3Combo(cLev1, cLev2, cLev3);
}


//Function to use with the ASP Custom Validator, checks if user selected a value from the theme combo
function SearchThemeValidate(source, arguments)
{
	if (arguments.Value != C_VAL_NULL)
		arguments.IsValid=true;
	else
		arguments.IsValid=false;
}

