﻿var allKeysRegiony = new Array();
var allValuesRegiony = new Array();
var selectedKeysRegiony = new Array();
var selectedValuesRegiony = new Array();

var allKeysProfesie = new Array();
var allValuesProfesie = new Array();
var selectedKeysProfesie = new Array();
var selectedValuesProfesie = new Array();

function selectAll(selectID)
{
  var theSel = document.getElementById(selectID);  
  if (theSel != null)
  {
    var selLength = theSel.length;    
    var i;
          
    for(i=0; i<selLength; i++)  
    {   
        theSel.options[i].selected = true;    
    }
  }
 }

function addToSelected(fromSelectID, toSelectID, mode)
{
  var theSelFrom = document.getElementById(fromSelectID);
  var theSelTo = document.getElementById(toSelectID);
  var selLength = theSelFrom.length;  
  var selectedCount = 0;
  var i;
  
  var selectedKeys;
  var selectedValues;
  
  if (mode == 'CisRegiony')
  {
    selectedKeys = selectedKeysRegiony;
    selectedValues = selectedValuesRegiony;    
  } 
  else
  {
    selectedKeys = selectedKeysProfesie;
    selectedValues = selectedValuesProfesie;
  }
  
  selectedCount = selectedKeys.length;
  
  for(i=0; i<selLength; i++)  
  {   
    if(theSelFrom.options[i].selected)
    {
      selectedValues[selectedCount] = theSelFrom.options[i].text.replace(/\./g, '');
      selectedKeys[selectedCount] = theSelFrom.options[i].value;
      selectedCount++;
    }
  }
  
  if (mode == 'CisRegiony')
  {
    selectedKeysRegiony = selectedKeys;
    selectedValuesRegiony = selectedValues;
  } 
  else
  {
    selectedKeysProfesie = selectedKeys;
    selectedValuesProfesie = selectedValues;
  }
  
  loadDataLeft(fromSelectID, mode);
  loadDataRight(toSelectID, mode);
}

function removeFromSelected(fromSelectID, toSelectID, mode)
{
    var theSelFrom = document.getElementById(fromSelectID);
    var theSelTo = document.getElementById(toSelectID);
    var selLength = theSelTo.length;  
    var selectedCount = 0;
    var i;
      
    var selectedKeys;
    var selectedValues;
      
    if (mode == 'CisRegiony')
    {
        selectedKeys = selectedKeysRegiony;
        selectedValues = selectedValuesRegiony;    
    } 
    else
    {
        selectedKeys = selectedKeysProfesie;
        selectedValues = selectedValuesProfesie;
    }
          
    selectedKeys.length = 0;
    selectedValues.length = 0;
    selectedCount = 0;
      
    for(i=0; i<selLength; i++)  
    {   
        if(theSelTo.options[i].selected == false)
        {
            selectedValues[selectedCount] = theSelTo.options[i].text;
            selectedKeys[selectedCount] = theSelTo.options[i].value;
            selectedCount++;
        }
    }
      
    if (mode == 'CisRegiony')
    {
        selectedKeysRegiony = selectedKeys;
        selectedValuesRegiony = selectedValues;
    } 
    else
    {
        selectedKeysProfesie = selectedKeys;
        selectedValuesProfesie = selectedValues;
    }
      
    loadDataLeft(fromSelectID, mode);
    loadDataRight(toSelectID, mode);
}

function isSelected(key, mode)
{
    var selectedKeys;
  
    if (mode == 'CisRegiony')
    {
        selectedKeys = selectedKeysRegiony;
    } 
    else
    {
        selectedKeys = selectedKeysProfesie;
    }
        
    for (i = 0; i < selectedKeys.length; i++)
    {
        if (selectedKeys[i] == key)
        {
            return true;
        }
    }
    return false;
}

function loadDataLeft(fromSelectID, mode)
{
    var i = 0;
    var currentLayer = 0;
    var hideBit = false;
    var priorLayer = 0;
    var priorSelectedLayer = 0;
    var currentLayer = 0;
    hideBit = false;
    priorLayer = 0;
    
    var allKeys;
    var allValues;
  
    var theSel = document.getElementById(fromSelectID);
    theSel.length = 0;
  
    if (mode == 'CisRegiony')
    {
        allKeys = allKeysRegiony;
        allValues = allValuesRegiony;
    } 
    else
    {
        allKeys = allKeysProfesie;
        allValues = allValuesProfesie;
    }
    
    for (i = 0; i < allKeys.length; i++)
    {
        var key = allKeys[i];
        var value = allValues[i];
        currentLayer = getLayer(value);
        
        if (currentLayer <= priorSelectedLayer)
        {
            hideBit = false;
        }
        
        if (isSelected(key, mode) == true)
        {
            if (hideBit == false)
            {
                priorSelectedLayer = currentLayer;
                hideBit = true;
            }
        }
        
        priorLayer = currentLayer;
        
        if (hideBit == true)
        {
            continue;
        }
        
        addOption(fromSelectID, value, key);
        
    }
}

function loadDataRight(toSelectID, mode)
{
    var selectedKeys;
    var selectedValues;
  
    var theSel = document.getElementById(toSelectID);
    theSel.length = 0;
  
    if (mode == 'CisRegiony')
    {
        selectedKeys = selectedKeysRegiony;
        selectedValues = selectedValuesRegiony;
    } 
    else
    {
        selectedKeys = selectedKeysProfesie;
        selectedValues = selectedValuesProfesie;
    }
    
    for (i = 0; i < selectedKeys.length; i++)
    {
        var key = selectedKeys[i];
        var value = selectedValues[i];
        addOption(toSelectID, value, key);
    }
}

var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);

function addOption(theSelID, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var theSel = document.getElementById(theSelID);
  var selLength = theSel.length;  
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSelID, theIndex)
{ 
  var theSel = document.getElementById(theSelID);
  var selLength = theSel.length;  
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}

function getLayer(text)
{
    var i;
    var points = 0;
    for (i = 0; i < text.length; i++)
    {
        if (text.substring(i, i + 1) == '.') 
            points++;
        else
            break;
    }
    
    if (points == 0)
    {
        return 1;
    }
    if (points == 3)
    {
        return 2;
    }
    if (points == 6)
    {
        return 3;
    }        
}

function moveOptions(fromSelectID, toSelectID)
{
  var theSelFrom = document.getElementById(fromSelectID);
  var theSelTo = document.getElementById(toSelectID);
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedIndexes = new Array();
  var toDelete = new Array();
  var toDeleteCount = 0;
  var selectedCount = 0;
  var layerCurrent = 0;
  var layerLastSelected = 0;
  var forceSelect = false;
  var selectionOpen = false;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=0; i<selLength; i++)  
  {
  
    forceSelect = false;
    if (selectedCount > 0)
    {
        layerCurrent = getLayer(theSelFrom.options[i].text);
        layerLastSelected = getLayer(selectedText[selectedCount - 1]);
        if (layerLastSelected < layerCurrent && selectionOpen == true)
        {
            forceSelect = true;
        }
        else
        {
            selectionOpen = false;
        }        
    }
        
    
    if(theSelFrom.options[i].selected)
    {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      selectedIndexes[selectedCount] = i;      
      selectedCount++;
      toDelete[toDeleteCount] = i;        
      toDeleteCount++;
      selectionOpen = true;
    }
    else if (forceSelect == true)
    {
        toDelete[toDeleteCount] = i;        
        toDeleteCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=0; i<selectedCount; i++)  
  {    
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
  
  for(i=toDeleteCount-1; i>=0; i--)  
  {    
    deleteOption(theSelFrom, toDelete[i]);       
  }
    
  if(NS4) history.go(0);
}

function setClass(element, className)
{
    element.className = className;
}

function toggleDisplay(objId, visible)
{
    var element = document.getElementById(objId);
    if(visible) element.style.display = ''; else element.style.display='none';
}

function setDefaultValueForKnowledge(idDruh, idUroven)
{
    var elementDruh = document.getElementById(idDruh);
    var elementUroven = document.getElementById(idUroven);
    
    if(elementDruh.value != '00000000-0000-0000-0000-000000000000' && elementUroven.value == '-1')
    {
        elementUroven.value = '1';
    }
    if(elementDruh.value == '00000000-0000-0000-0000-000000000000')
    {
        elementUroven.value = '-1';
    }
}

function doubleListBoxIsValid(sender, args)
  {    
    var listBox = document.getElementById('');
    args.IsValid = true;
  }
  
  function paymentTypeIsValid(sender, args)
  {  
    args.IsValid = false;
    for(var i = 0; i < document.forms[0].typPlatby.length; i++) {
        if(document.forms[0].typPlatby[i].checked == true) {      
            args.IsValid = true;
            break;
        }
    }
  }
