cl.setLibraryPath('/slinq/scripts/charlotte/src/');

cl.inc('cbe.core.*');


var currentForm = '';

var shootBoxes = [];

var hasFormBeenModified = false;

var activePopup = null;

function windowOnLoad()
{
}

function windowOnUnload()
{
    if (hasFormBeenModified)
    {
        event.returnValue = 'Je hebt gegevens gewijziged en deze wijzigingen gaan verloren.';
    }
}

function handleFormModification()
{
    if (top.hasFormBeenModified != undefined)
    {
        top.hasFormBeenModified = true;
    }
}

function showForm( strId )
{
    if ( strId != currentForm )
    {
        if ( currentForm != '' )
        {
            document.getElementById( currentForm + 'Selected' ).style.display = 'none';
            document.getElementById( currentForm + 'Unselected' ).style.display = 'block';
            document.getElementById( currentForm + 'Contents' ).style.display = 'none';
        }
        currentForm = strId;

        document.getElementById( currentForm + 'Unselected' ).style.display = 'none';
        document.getElementById( currentForm + 'Selected' ).style.display = 'block';
        document.getElementById( currentForm + 'Contents' ).style.display = 'block';
    }
}

function togglePopup(idPrefix)
{
    popup = document.getElementById(idPrefix + "-popup");
    if (popup.style.display == 'block')
    {
        popup.style.display = 'none';
        activePopup = null;
    }
    else
    {
        if (activePopup != null)
        {
            activePopup.style.display = 'none';
        }
        popup.style.display = 'block';
        activePopup = popup;
    }

    return false;
}

function setTime( strId, strValue )
{
    document.getElementById( strId ).value = strValue;
    togglePopup( strId );
    hasFormBeenModified = true;

    return false;
}


function handleSubmit()
{
    for (var i = 0; i < shootBoxes.length; i++)
    {
        var selectedItemsList = document.getElementById(shootBoxes[i]);

        for (var j = 0; j < selectedItemsList.length; j++)
        {
            selectedItemsList[j].selected = true;
        }
    }

    hasFormBeenModified = false;

    return true;

}

function copyData(name, fieldsToBeConfirmedForCopy)
{
    fieldsNotToCopy = '';
    if (fieldsToBeConfirmedForCopy != undefined)
    {
        label = '';
        for (i = 0; i < (fieldsToBeConfirmedForCopy.length / 2); i++)
        {
            if (i > 0)
            {
                label += ', ';
            }
            label += fieldsToBeConfirmedForCopy[i * 2];
        }
        if (!window.confirm('De volgende velden bevatten gevoelige informatie waarmee voorzichtig omgesprongen moet worden:\n'
                +  label
                + '\nKlik op \'OK\' om deze velden ook te kopi' + String.fromCharCode(235) + 'ren. \'Annuleren\' kopieert alleen de velden met niet-gevoelige informatie.'))
        {
            for (i = 0; i < (fieldsToBeConfirmedForCopy.length / 2); i++)
            {
                if (i > 0)
                {
                    fieldsNotToCopy += ',';
                }
                fieldsNotToCopy += fieldsToBeConfirmedForCopy[i * 2 + 1];
            }
        }
    }
    if (name != 'none')
    {
        document.getElementById('hiddenAction').value = 'copy' + name.substr(0, 1).toUpperCase() + name.substr(1);
        document.getElementById('fieldsNotToCopy').value = fieldsNotToCopy;
        handleSubmit();
        document.getElementById('mainForm').submit();
    }
}

function selectAvailabilitiesForWeek(index)
{
    selectAvailabilities('monday', index);
    selectAvailabilities('tuesday', index);
    selectAvailabilities('wednesday', index);
    selectAvailabilities('thursday', index);
    selectAvailabilities('friday', index);
    selectAvailabilities('saturday', index);
    selectAvailabilities('sunday', index);
}

function selectAvailabilities(name, index)
{
    elements = document.getElementsByName(name + 'Availability.morningAvailability');
    if (elements.length > 0)
    {
        elements.item(index).checked = true;
    }

    elements = document.getElementsByName(name + 'Availability.afternoonAvailability');
    if (elements.length > 0)
    {
        elements.item(index).checked = true;
    }

    elements = document.getElementsByName(name + 'Availability.eveningAvailability');
    if (elements.length > 0)
    {
        elements.item(index).checked = true;
    }
}

function registerShootBox(name)
{
    shootBoxes.push(name);
}

function containsOption(list, option)
{
    var result = false;

    if (list.length)
    {
        for (var i=0; i < list.length; i++)
        {
            if (list[i].value == option.value)
            {
                result = true;
            }
        }
    }

    return result;
}

function shootBoxSelect(name)
{
    var availableItemsList = document.getElementById(name + 'Available');
    var selectedItemsList = document.getElementById(name);

    if (availableItemsList.selectedIndex != -1)
    {
        for (var i=0; i < availableItemsList.length; i++)
        {
            if (availableItemsList.options[i].selected == true)
            {
                if (!containsOption(selectedItemsList, availableItemsList.options[i]))
                {
                    option = new Option(availableItemsList.options[i].text, availableItemsList.options[i].value);
                    selectedItemsList.options[selectedItemsList.length] = option;
                }
            }
        }
    }
}

function shootBoxDeselect(name)
{
    var selectedItemsList = document.getElementById(name);
    if (selectedItemsList.selectedIndex != -1)
        deleteOption(selectedItemsList);
}


function deleteOption(list)
{
    var index = list.length;

    if (index != 0 && list.selectedIndex != -1)
    {
        list.options[list.selectedIndex] = null;
        deleteOption(list);
    }
}

function selectDayOfWeek(dayNumber)
{
    var i = 0;
    var checkbox = document.getElementById(dayNumber + '-' + i);
    while (checkbox != undefined || i == 0)
    {
        if (checkbox != undefined)
        {
            checkbox.checked = true;
        }
        i++;
        var checkbox = document.getElementById(dayNumber + '-' + i);
    }
}

function selectAllStudentsForApproval()
{
    var inputs = document.getElementsByTagName('input');
    var i;
    for (i = 0; i < inputs.length; i++)
    {
        if (inputs[i].type == 'checkbox')
        {
            var inputName = inputs[i].name;
            if (inputName.length > '.approvedByStudentlinq'.length && inputName.substring(inputName.length - '.approvedByStudentlinq'.length) == '.approvedByStudentlinq')
            {
                inputs[i].checked = true;
            }
            else if (inputName.length > '.approveHiddenStudents'.length && inputName.substring(inputName.length - '.approveHiddenStudents'.length) == '.approveHiddenStudents')
            {
                inputs[i].checked = true;
            }
        }
    }
}

function selectAllStudentsForClientHours()
{
    var inputs = document.getElementsByTagName('input');
    var i;
    for (i = 0; i < inputs.length; i++)
    {
        if (inputs[i].type == 'checkbox')
        {
            var inputName = inputs[i].name;
            if (inputName.length > '.approvedByClient'.length && inputName.substring(inputName.length - '.approvedByClient'.length) == '.approvedByClient')
            {
                inputs[i].checked = true;
            }
        }
    }
}

function setCheckedState(name, newState)
{
    var inputs = document.getElementsByTagName('input');
    var i;
    for (i = 0; i < inputs.length; i++)
    {
        if (inputs[i].type == 'checkbox')
        {
            var inputName = inputs[i].name;
            if (inputName == name)
            {
              inputs[i].checked = newState;
            }
        }
    }
}

function isSpaceOrControl(charCode)
{
  return charCode <= 32 || (127 <= charCode && charCode <= 160);
}

function normalizeSmsText(text)
{
  var result = "";

  var startIndex = 0;
  while (startIndex < text.length && isSpaceOrControl(text.charCodeAt(startIndex)))
  {
    startIndex += 1;
  }

  var endIndex = text.length;
  while (0 < endIndex && isSpaceOrControl(text.charCodeAt(endIndex - 1)))
  {
    endIndex -= 1;
  }

  var wasPreviousSpace = false;
  for (var charIndex = startIndex; charIndex < endIndex; charIndex += 1)
  {
      var char = text.charAt(charIndex);
      if (isSpaceOrControl(text.charCodeAt(charIndex)))
      {
        if (!wasPreviousSpace)
        {
          result += " ";
          wasPreviousSpace = true;
        }
      }
      else
      {
        result += char;
        wasPreviousSpace = false;
      }
  }

  return result;
}

function updateSmsInfo(textarea, name)
{
  var text = normalizeSmsText(textarea.value);
  var numberOfCharacters = text.length;
  var numberOfMessages;
  if (numberOfCharacters == 0)
  {
    numberOfMessages = 0;
  }
  else if (numberOfCharacters <= 160)
  {
    numberOfMessages = 1;
  }
  else
  {
    numberOfMessages = Math.floor((numberOfCharacters + 152) / 153);
  }

  document.getElementById(name + "SmsInfo").innerHTML = numberOfMessages + " sms'jes (" + numberOfCharacters + " tekens)" ;
}
