﻿function sortSelect(obj) 
{
    var o = new Array();
    if (obj.options==null) { return; }
    for (var i=0; i<obj.options.length; i++) 
    {
	    o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
	}
    if (o.length==0) { return; }
    o = o.sort( 
	    function(a,b) { 
		    if ((a.text+"") < (b.text+"")) { return -1; }
		    if ((a.text+"") > (b.text+"")) { return 1; }
		    return 0;
		    } 
	    );

    for (var i=0; i<o.length; i++) 
    {
	    obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	}
}

function tripleDD(pFrm,pDD1,pDD2,pDD3,pLevel)
{
	var i = 0;
	var middleArray, bottomArray;
	var middleLabel = (pDD1.options[pDD1.selectedIndex].text == "Flame Pharma") ? "Job Sector" : "Job Discipline";
	var bottomLabel = "Job Role";
	// middle sometimes changes
	if(pLevel == 1)
	{	
		
		middleArray = eval("TDDmiddle[pDD1.options['"+pDD1.selectedIndex+"'].value]");
		if(isArray(middleArray[0]))
		{
			pDD2.options.length = middleArray.length;
			for (i=0; i < middleArray.length; i++)
			{
				if(i == 0)
				{
				    pDD2.options[i].text = "-- " + middleLabel;
				}
				else
				{
				    pDD2.options[i].text = middleArray[i][0];
				}
				pDD2.options[i].value = middleArray[i][1];
			}
		}
		else
		{
			// NEEDS TO CHANGE
			pDD2.options.length = 2;
			pDD2.options[0].text = "-- " + middleLabel;
			pDD2.options[0].value = "0";
			pDD2.options[1].text = "No " + middleLabel + "s found";
			pDD2.options[1].value = "0";
		}
		pDD2.selectedIndex = 0
		sortSelect(pDD2);
	}
	
	// bottom changes

	if(pLevel == 1 || pLevel == 2)
	{
		middleArray = eval("TDDmiddle[pDD1.options['"+pDD1.selectedIndex+"'].value]");
		
		if(isArray(middleArray[0]))
		{
			bottomArray = eval("TDDbottom[pDD2.options['"+pDD2.selectedIndex+"'].value]");
			if(isArray(bottomArray[0]))
			{
				pDD3.options.length = bottomArray.length;
				for (i=0; i < bottomArray.length; i++)
				{
					if(i == 0)
				    {
				        pDD3.options[i].text = "-- " + bottomLabel;
				    }
				    else
				    {   
				        pDD3.options[i].text = bottomArray[i][0];
				    }
					pDD3.options[i].value = bottomArray[i][1];
				}
			}
			else
			{
				pDD3.options.length = 2;
				pDD3.options[0].text = "-- " + bottomLabel;
				pDD3.options[0].value = "0";
				pDD3.options[1].text = "No " + bottomLabel + "s found";
				pDD3.options[1].value = "0";
				
			}
		}
		else
		{
			pDD3.options.length = 2;
			pDD3.options[0].text = "-- " + bottomLabel;;
			pDD3.options[0].value = "0";
			pDD3.options[1].text = "No " + bottomLabel + "s found";
			pDD3.options[1].value = "0";
		}
		pDD3.selectedIndex = 0;
		sortSelect(pDD3);
	}
}

function doubleDD(pFrm,pDD1,pDD2)
{
	var i = 0;
	var bottomArray;
	// middle sometimes changes
	bottomArray = eval("TDDbottom[pDD1.options['"+pDD1.selectedIndex+"'].value]");
	if(isArray(bottomArray[0]))
	{
		pDD2.options.length = bottomArray.length;
		for (i=0; i < bottomArray.length; i++)
		{
			pDD2.options[i].text = bottomArray[i][0];
			pDD2.options[i].value = bottomArray[i][1];
		}
	}
	else
	{
		pDD2.options.length = 2;
		pDD2.options[0].text = "-- Role";
		pDD2.options[0].value = "0";
		pDD2.options[1].text = "No Roles found";
		pDD2.options[1].value = "0";
		
	}
	pDD2.selectedIndex = 0;
	sortSelect(pDD2);
}

function isArray(obj) 
{
	if(typeof(obj) == "object")
	{	
		if (obj.constructor.toString().indexOf("Array") == -1)
			return false;
		else
			return true;
	}
	else
		return false;
}	

function selectedDDValueToHidden(ddElem, hdnElem)
{
    if(ddElem.selectedIndex != -1)
        hdnElem.value = ddElem.options[ddElem.selectedIndex].value;
}

function selectedDDTextToHidden(ddElem, hdnElem)
{
    if(ddElem.selectedIndex != -1)
    {
        if(ddElem.options[ddElem.selectedIndex].value != "0")
        {
            hdnElem.value = ddElem.options[ddElem.selectedIndex].text;
        }
    }
}

