﻿// JScript File
var vComplete = false;

function ReceiveServerData(arg,context,selValue)
{
    //if (arg != '' && context != '')
    {
        var sels = selValue.split('|');
        var pars = context.split('#'); 
        var val = pars[1].split('|');
        var lst = pars[0].split('|');
        for (var i = 0; i < val.length; ++i)
        {
            var valID = arg.split(':');
            if (arg != "2:Teaching" && val[i] == "SUBJECT")
            {
                toggle_visibility(lst[i],false);
            }
            else if (val[i] == "SUBJECT") 
            {
                toggle_visibility(lst[i],true);
            }
            fillCombobox(document.getElementById(lst[i]),val[i] + valID[0],sels[i+1]);
        }
    }
    //else 
}

function alertnodata(oList,parent)
{
    if (oList != "undefined" && oList != null && oList.options.length <= 1 && parent != '' )
    {
        alert ('Please select value from ' + parent + ' first.');
    }
}

function addCommandTo(fAnon, sNewCommand)
{
    var sScript;
    if (fAnon)
    {
	    sScript = new String(fAnon);
	    if (sScript.indexOf('{') > 0)
	    {
		    sScript = sScript.substring(sScript.indexOf('{') + 1, sScript.lastIndexOf('}') - 1);
		    sScript += sNewCommand;
	    }
    } 
    else 
    {
	    sScript = new String(sNewCommand);
    }
    return new Function(sScript);
}

function fillCombobox(oList, vValue, selectedValue)
{
  if (oList == "undefined" || oList == null) return;
  var selIndex;
  clearComboOrList(oList);
  if (vValue != '') 
  {
    if (assocArray[vValue])
    {
      var isID = false;
      if (typeof(selectedValue) != "undefined" && selectedValue != '' && selectedValue.indexOf(':') <= 0) isID = true;
      oList.options[0] = new Option('All', '');
      var arrX = assocArray[vValue];
      for (var i = 0; i < arrX.length; i = i + 2)
      {
        if (arrX[i] != 'EOF') 
        {
          oList.options[oList.options.length] = new Option(arrX[i + 1], arrX[i]);
          if (isID)
          {
            if (selectedValue != null && selectedValue != ''  && arrX[i].substring(0,selectedValue.length+1) == selectedValue + ":") 
            {
                selIndex = oList.options.length-1;
            }
          }
          else
          {
            if (selectedValue != null && selectedValue != ''  && arrX[i] + '' == selectedValue) selIndex = oList.options.length-1;
          }
        }
      }
      /*if (oList.options.length == 1)
      {
        oList.options[0] = new Option('None found', '');
      } 
      else if (oList.options.length == 2)
      {
        oList.selectedIndex=1;
      }*/
    }
    else
    {
        oList.options[0] = new Option('All', '');
    } 
    /*else 
    {
      oList.options[0] = new Option('None found', '');
    }*/
  }
  //selectedValue = "<%=Session['Sector']%>";
  //alert (selectedValue);
  if (selectedValue != null && selectedValue != "" )
  {
    oList.selectedIndex = parseFloat(selIndex);
    if (oList.onchange) 
    {
        oList.onchange();
    }
    //oList.selectedIndex = parseFloat(selectedValue);
  }
}

function comboItemSelected(oList1,oHidden,oList2)
{
	if (vComplete && oHidden)
	{
		if (oList1.selectedIndex <= 0)
		{
			oHidden.value='';
		} 
		else 
		{
			oHidden.value=oList1.options[oList1.selectedIndex].value;
		}
	}
	if (oList1 && oList2!=null)
	{
		clearComboOrList(oList2);
		if (oList1.selectedIndex <= 0)
		{
			oList2.options[oList2.options.length] = new Option('Please make a selection from the list', '');
		} 
		else 
		{
			fillCombobox(oList2, oList2.id + oList1.id + '=' + oList1.options[oList1.selectedIndex].value);
		}
	}
}

function clearComboOrList(oList)
{
    if (oList != null && typeof(oList) != "undefined") 
    {
        oList.selectedIndex = -1;
        for (var i = oList.options.length - 1; i >= 0; i--)
        {
	        oList.options[i] = null;
	    }
	    oList.selectedIndex = -1;
	    if (oList.onchange)	oList.onchange();
    }
}

//function comboOnChange(oList,oHidden)
//{
//    oHidden.value = oList1.options[oList1.selectedIndex].value;
//}

function toggle_visibility (id, visible) 
{
    var e = document.getElementById(id);
    if (e != null)
    {
        if (visible)
            e.style.display = 'block';
        else
            e.style.display = 'none';
    }
}