<script language="javascript">
function ListMoveUp(AList)
{
  var I, J, F = false;

  J = AList.options.length;
  for (I = 0; I < J; I ++)
  {
    if (! AList[I].selected) F = true;
    if (AList[I].selected && F)
      AList.insertBefore(AList[I], AList[I - 1]);
  }
}

function ListMoveDown(AList)
{
  var I, J, F = false;

  J = AList.options.length;
  for (I = J - 1; I >= 0; I --)
  {
    if (! AList[I].selected) F = true;
    if (AList[I].selected && F)
      AList.insertBefore(AList[I], AList[I + 2])
  }
}

function GetArrayIndex(R, S)
{
  var I = 0; J = R.length;
  
  for (I = 0; I < J; I ++)
    if (R[I] == S) return I;
  return -1; 
}

function GetSelectIndex(R, S)
{
  var I = 0; J = R.length;
  
  for (I = 0; I < J; I ++)
    if (R.options[I].text == S) return I;
  return -1; 
}

function GetSelectValueIndex(R, S)
{
  var I = 0; J = R.length;
  
  for (I = 0; I < J; I ++)
    if (R.options[I].value == S) return I;
  return -1; 
}

function StringReplace(S, Src, Dst)
{
  var I = 0, J = Src.length, K = Dst.length;
  var T = S;

  while (T.indexOf(Src, I) > -1)
  {
    I = T.indexOf(Src, I);
    T = T.substr(0, I) + Dst + T.substr(I + J);
    I += K;
  }
  return T;
}

function GetHTMLScript(S)
{
  var T;

  T = StringReplace(S, '<', '&lt;');
  T = StringReplace(T, '>', '&gt;');
  return StringReplace(T, '"', '&quot;');
}

function SelectAllOptions(AList, V)
{
  var I, J;

  J = AList.length;
  for (I = 0; I < J; I ++)
    AList.options[I].selected = V;
}

function MoveOptions(Src, Dst, IsAll)
{
  var I = 0, SrcLength = Src.length, SrcIndex = 0;

  if (IsAll) SelectAllOptions(Src, true);
  SrcIndex = Src.selectedIndex;
  if (SrcIndex == -1)
  {
    if (Src.length > 0) Src.options[0].selected = true;
  }
  else
  {
    var K = 0, DstLength = Dst.length, DstIndex = DstLength - 1, SelCount = 0;

    for (I = DstLength - 1; I >= 0; I --)
      if (Dst.options[I].selected)
      {
        DstIndex = I;
        break;
      }
    for (I = 0; I < SrcLength; I ++)
      if (Src.options[I].selected) SelCount ++;
    for (I = DstLength - 1; I > DstIndex; I --)
      Dst.options[I + SelCount] = new Option(Dst.options[I].text, Dst.options[I].value);
    for (I = SrcLength - 1; I >= 0; I --)
      if (Src.options[I].selected)
      {
        Dst.options[DstIndex + SelCount - K] = new Option(Src.options[I].text, Src.options[I].value);
        Src.options[I] = null;
        K ++;
      }
    if (SrcIndex < Src.length) Src.options[SrcIndex].selected = true;
    else if (Src.length > 0) Src.options[Src.length - 1].selected = true;
    DstLength = Dst.length;
    for (I = 0; I < DstLength; I ++)
      Dst.options[I].selected = I > DstIndex && I <= DstIndex + SelCount;
  }
}

function ClearOptions(SelectList)
{
  var I, J;

  J = SelectList.length;
  for (I = J - 1; I >= 0; I --) SelectList.options[I] = null;
}

function AddRow(Table, Name, Text, Value, IsRadio)
{
  var I, J, L = 0, SpanLen;
  var Row, Cell, R;

  L = Table.rows.length;
  if (IsRadio == '2')
  {  
    SpanLen = Value.substr(0, Value.indexOf('|'));
    if (Number(SpanLen) > 0)
    {
      Row = Table.insertRow(L);
      Cell = Row.insertCell(0);
      Cell.colSpan = SpanLen;
      Cell.height = '30';
      Cell.innerHTML = GetHTMLScript(Text);
    }
    else
    {
      R = new Array();
      R = GetHTMLScript(Text).split('&!^');
      I = 0;
      J = R.length;
      Row = Table.insertRow(L);
      for (I = 0; I < J; I ++)
      {      
        Cell = Row.insertCell(I);
        Cell.innerHTML = R[I];
      }
    }
  }
  else
  {
    var S = '', C = '', H;

    Row = Table.insertRow(L);
    Cell = Row.insertCell(0);  
    if (IsRadio == '1')
    {
      S = 'radio';
      if (L > 0) C = '';
      else C = ' checked';
    }  
    else
    {
      S = 'checkbox';
      C = ' checked onclick="SetCheckBox(' + Name + 'CheckAll,this,' + Name + 'Check)"';
    }
    H = GetHTMLScript(Text);
    Cell.innerHTML= '<input type="' + S + '" name="'+ Name + 'Check" value="' + GetHTMLScript(Value) + '"' + C + '>' + H;
    if (IsRadio == '0')
    {
      if (! form1.elements[Name + 'CheckAll'].checked) form1.elements[Name + 'CheckAll'].checked = true;
      if (L > 0)
        form1.elements[Name + 'CheckAll'].style.display = '';
      else form1.elements[Name + 'CheckAll'].style.display = 'none';      
    }  
  }
}

function ClearRows(Table)
{
  var I = 0, J = Table.rows.length;

  for (I = J - 1; I >= 0; I --) Table.deleteRow(I);
}

function AlphaSortMethod(A, B)
{
  var S1 = '', S2 = '';

  S1 = A.toUpperCase();
  S2 = B.toUpperCase();
  if (S1 == S2) return 0;
  else if (S1 > S2) return 1;
  else if (S1 < S2) return -1;
}

function SpecialSortMethod(A, B)
{
  var R1 = new Array(), R2 = new Array();
 
  R1 = A.split('&!^');
  R2 = B.split('&!^');
  if (Number(R1[2]) > Number(R2[2])) return 1;
  else return -1;
}

function RemoveDuplicateItems(R)
{
  var I = 0, J = R.length, K = 0, L = 0;
  var T1 = new Array(), T2 = new Array(), T3 = new Array(), T4 = new Array();
  var CanAdd = true;

  for (I = 0; I < J; I ++)
  {
    T1 = R[I].split('&!^');
    T2[I] = T1[1];
    T3[I] = T1[0] + T1[3] + R[I].substr(R[I].indexOf('|') + 1);
  }
  for (I = 0; I < J; I ++)
    if (T2[I] == '2')
    {
      T4[L] = R[I];
      L ++;
    }
    else
    {
      CanAdd = true;
      for (K = 0; K < I; K ++)
        if (T3[I] == T3[K])
        {
          CanAdd = false;
          break;
        }
      if (CanAdd)
      {
        T4[L] = R[I];
        L ++;
      }
    }
  return T4;
}

function AdjustSelect(SrcList, DstList, Data)
{
  var Duplicated = false;
  var I = 0, J = 0, M = 0, N = DstList.options.length;
  var R = new Array();
  
  Data = RemoveDuplicateItems(Data);
  Data.sort(SpecialSortMethod);
  J = Data.length;
  for (I = 0; I < J; I ++)
  {
    R = Data[I].split('&!^');
    Duplicated = false;
    for (M = 0; M < N; M ++)
      if (DstList.options[M].text == Data[I].substr(Data[I].indexOf('|') + 1) && DstList.options[M].value == R[3])
      {
        Duplicated = true;
        break;
      }
    if (! Duplicated) SrcList.add(new Option(Data[I].substr(Data[I].indexOf('|') + 1), R[3]));
  }
}

function AdjustCheck(Data)
{
  var I = 0, J = 0;
  var R = new Array();
  
  Data = RemoveDuplicateItems(Data);
  Data.sort(SpecialSortMethod);
  J = Data.length;
  for (I = 0; I < J; I ++)
  {
    R = Data[I].split('&!^');  	  
    AddRow(document.all(R[0] + 'Checks'), R[0], Data[I].substr(Data[I].indexOf('|') + 1), R[3], R[1]);
  }
}

function SetAllChecks(CheckBox, Checks)
{
  var I = 0; J = Checks.length;

  for (I = 0; I < J; I ++)
   Checks[I].checked = CheckBox.checked;
}

function SetCheckBox(CheckBox, Check, Checks)
{
  var I = 0; J = Checks.length;

  if(! Check.checked) CheckBox.checked = false;
  else
  {
    for (I = 0; I < J; I ++)
      if (! Checks[I].checked) return;
    CheckBox.checked = true;
  }
}

function CheckCheckBox(CheckBox, Checks)
{
  var I = 0; J = Checks.length;

  for (I = 0; I < J; I ++)
  {
    if (! Checks[I].checked)
    {
      CheckBox.checked = false;
      return;
    }
  }
  if (J > 1) CheckBox.checked = true;
}

function GetDateString(Edit)
{
  var Para="scroll:no;status:no;center:yes;unadorned:no;dialogHeight:240px;dialogWidth:210px";

  window.showModalDialog("../Calendar.html", Edit, Para);
}

function GetDualDigitString(Edit, Len, Interval)
{
  var Para="scroll:no;status:no;center:yes;unadorned:no;dialogHeight:360px;dialogWidth:510px";
  var R = new Array(), T = new Array();
  var I = 0, J = 0, K = 0;
  
  K = Len;
  R[1] = 1;
  R[2] = Edit;
  if (Edit.value != '') T = Edit.value.split(',');
  for (I = 0; I < Len; I ++)
    if (I % Interval == 0 && GetArrayIndex(T, I) == -1) R[R.length] = I;
	else K --;
  J = T.length;
  for (I = 0; I < J; I ++)
    R[I + 3 + K] = T[I]; 
  R[0] = K;
  window.showModalDialog("../DualBox.html", R, Para);
}

function DeleteSelOptions(Select)
{
  var I = 0, J = Select.length - 1, K = Select.selectedIndex;
  
  for (I = J; I >= 0; I --)
    if (Select.options[I].selected) Select.remove(I);
  if (K == -1) K = 0;
  if (Select.length > K) Select.options[K].selected = true;
}
</script>