
  /**
  * @desc hype your home specific functions
  * should only be loaded when hyping a home
  **/
  // default value for max addresses
  var maxAddresses = 25;
  // max addresses can be changed with this function
  function setMaxAddresses(nrOfAdresses)
  {
    maxAddresses = nrOfAdresses;
  }

  /**
  * @desc create an new address input set
  * @param object optional content contains e-mail and name imported relation
  * @return void
  */
  function addEmailAddressInput(content)
  {
    var inputObj        = this;
    var curEmailAddress = '';
    var cbRequest       = false;

    if(content && typeof(content) == "object")
    {
      var lastInputSet = null;

      // check if address is added by import
      if(content.email)
      {
        var hypeaddresseinputHolder = document.getElementById('hypeaddresseinput');

        if(hypeaddresseinputHolder)
        {
          lastAddressInputSet = document.getElementById('hype_address_' + hypeaddresseinputHolder.copyIndex);
          lastNameInputSet    = document.getElementById('hype_name_' + hypeaddresseinputHolder.copyIndex);
        }

        if(lastAddressInputSet && lastNameInputSet)
        {
          lastAddressInputSet.value    = content.email;
          lastNameInputSet.value       = content.name;
          lastAddressInputSet.imported = true;
          inputObj                     = lastAddressInputSet;
          cbRequest                    = new Object();
          cbRequest.pageValidation     = false;
          cbRequest.calledFrom         = lastAddressInputSet.id;
        }
      }
    }

    // in some cases the onchage is called before onBlur (IE)
    // in that case the radiobox is not set becase de checkBox (cb) data is not yet set and cbNotComplete is set to true
    if(inputObj && inputObj.cbNotComplete)
    {
      cbRequest                  = new Object();
      cbRequest.pageValidation   = false;
      cbRequest.calledFrom       = inputObj.id;
      inputObj.cbNotComplete     = null;
    }

    // if no valid e-mail addres, no new input field !!!
    if(inputObj)
      curEmailAddress = validator.getValue(inputObj);

    if(!inputObj || curEmailAddress.length == 0 || validator.validation_email(curEmailAddress) == false)
      return false;

    // remove the onblur from the current input node
    inputObj.onblur    = '';

    // set data for the validator::addCheckBox function (call|addCheckBox)
    inputObj.cbId      = 'addressesTohype' + inputObj.rowIndex;
    inputObj.cbName    = 'addressesTohype[]';
    inputObj.cbValue   = inputObj.rowIndex - 1;

    // when the address is added by the import option we shoud set the checkbox
    // we also have to set the checkbox if cbNotComplete=true (in this case the cbRequest is set)
    if(cbRequest)
    {
      addCheckBox(cbRequest);
      cbRequest = null;
    }

    if(inputObj.rowIndex < maxAddresses)
    {
      // find the parent holder to put the new fields in
      var nodeToCopy     = getParentByClass(inputObj, 'form-columnsholder');
      var toCopyInHolder = nodeToCopy.parentNode;

      // clone the stored copy
      var new_node       = toCopyInHolder.nodeCopy.cloneNode(true);

      // put the copy in the parent holder
      toCopyInHolder.appendChild(new_node);

      // increase the copy index
      toCopyInHolder.copyIndex++;
      newRowIndex        = toCopyInHolder.copyIndex;

      // cleanup the new nodes an set attributes for the new fields
      var newAddres      = getChildById(new_node, 'input', 'hype_address[]');
      newAddres.value    = '';
      newAddres.id       = 'hype_address_' + newRowIndex;
      newAddres.rowIndex = newRowIndex;
      newAddres.onblur   = addEmailAddressInput;
      newAddres.onchange = validator.validate;

      if(toCopyInHolder.checkId)
        newAddres.checkId = toCopyInHolder.checkId;

      var newName        = getChildById(new_node, 'input', 'hype_name[]');
      newName.value      = '';
      newName.rowIndex   = newRowIndex;
      newName.id         = 'hype_name_' + newRowIndex;

      toCopyInHolder.addresses[toCopyInHolder.addresses.length] = newAddres;
    }
  }

  /**
  * @desc check if adresses in array not already present in send list
  * @param array with e-mal addresses
  * @return array with filtered e-mal addresses
  **/
  function filterHypeAdresses(emailArray)
  {
    var returnValue = new Array();
    if(!emailArray || typeof(emailArray) != "object")
      return returnValue;

    var emailToAdd = new Array();
    var index      = 0;
    var inputDiv   = document.getElementById('hypeaddresseinput');

    if(inputDiv && inputDiv.addresses && emailArray.length > 0)
    {
      var index = 0;
      for(index in emailArray)
      {
        if(!inHypeAddresArray(inputDiv.addresses, emailArray[index]))
          returnValue[returnValue.length] = emailArray[index];
      }
    }
    return returnValue;
  }

  function inHypeAddresArray(objectArray, testObj)
  {
    var testVal = new String(testObj.email);
    var index   = 0;
    for(index in objectArray)
    {
      var inputObject  = objectArray[index];
      var currentValue = new String(inputObject.value);

      if(testVal.trim() == currentValue.trim())
        return true;
    }
    return false;
  }

  /**
  * @desc initialize address / name input by making a copy of the original input fields and setting event handlers
  * @param void
  * @return void
  **/
  function initEmailAddressInput()
  {
    var hypeAddress            = document.getElementById('hype_address[]');
    var hypeName               = document.getElementById('hype_name[]');

    if(hypeAddress)
    {
      hypeAddress.checkId         = 'checkAllNone';
      // store a original copy of the node in the the parent of the form-columnsholder
      var nodeToCopy              = getParentByClass(hypeAddress, 'form-columnsholder');
      var toCopyInHolder          = nodeToCopy.parentNode;
      var nodeCopy                = nodeToCopy.cloneNode(true);

      toCopyInHolder.nodeCopy     = nodeCopy;
      toCopyInHolder.copyIndex    = 1;
      toCopyInHolder.addresses    = new Array();
      toCopyInHolder.addresses[0] = hypeAddress;
      toCopyInHolder.checkId      = 'checkAllNone';

      hypeAddress.onblur          = addEmailAddressInput;
      hypeAddress.rowIndex        = 1;
      hypeAddress.id              = 'hype_address_1';
    }

    if(hypeName)
      hypeName.id              = 'hype_name_1';
  }

  // get the hype message text from the textarea
  function getHypeMessageText(format)
  {
    var text = '';
    var textareaobj = document.getElementById('hype_tip_bericht');

    if(textareaobj)
      text = textareaobj.value;

    if(format == 'html')
    {
      rExp      = /\n/gi;
      newString = new String(text);
      htmltext  = newString.replace(rExp, '<br />');
      if(htmltext)
        text = htmltext;
    }
    return text;
  }

  function fillObjWithHypeText(divId)
  {
    var obj = document.getElementById(divId);
    // fill innerHTML when object is a div
    if(obj && obj.tagName == 'DIV')
    {
      var text      = getHypeMessageText('html');
      obj.innerHTML = text;
    }
    // fiil value attribute when object in input field
    if(obj && obj.tagName == 'INPUT')
    {
      var text   = getHypeMessageText();
      obj.value  = text;
    }
  }

  /**
  * @desc  validator callback function for placing checkboxes in the icon div
  * @param object standard validator callback request
  *
  * can be called by the rule="call|addCheckBox" attribute in an input field
  * the checkstatus of the checkbox can be forced with the folowing settings rule="call|addCheckBox|checked" and rule="call|addCheckBox|unchecked"
  **/
  function addCheckBox(request)
  {
    var pageValidation  = request.pageValidation;
    var calledFrom      = request.calledFrom;
    var xtraParameters  = request.xtraParameters;
    var dataComplete    = false;

    // do nothing on init fase!!
    if(isInitForm)
      return true;

    // find the icon div
    if (inputObj = document.getElementById(calledFrom))
    {
      var inputObjValue       = new String(validator.getValue(inputObj));
      var inputObjFormitem    = getParentByClass(inputObj, 'formitem');
      var iconNodeObj         = getChildByClass(inputObjFormitem, 'div', 'form-icon');
      var cbValue             = new String(inputObj.cbValue);

      if(inputObj.cbId && inputObj.cbName && cbValue.length)
        dataComplete = true;
    }

    // create checkbox when icondiv found and data complete
    if(iconNodeObj && dataComplete)
    {
      var newCheckBox = document.createElement('input');
      newCheckBox.setAttribute('type', 'checkbox');
      newCheckBox.setAttribute('id', inputObj.cbId);
      newCheckBox.setAttribute('name', inputObj.cbName);
      newCheckBox.setAttribute('value', inputObj.cbValue);
      newCheckBox.setAttribute('class', 'checkbox');
      newCheckBox.setAttribute('title', 'Verwijder het vinkje indien je dit adres niet wilt gebruiken');

      var newDiv = document.createElement('div');
      newDiv.setAttribute('class', 'icon_check_box');

      // first link the node to the parent before setting some attributres
      // in IE, some attributes set before linking new node to parent node, do not work !!
      iconNodeObj.appendChild(newDiv);
      newDiv.appendChild(newCheckBox);
      newCheckBox.checkId  = inputObj.checkId;
      newCheckBox.rowIndex = inputObj.rowIndex;

      newDiv.style.height = '100%';
      newDiv.style.width = '100%';

      // determine if the checkbox should be checked or not
      var setChecked = true;

      if(inputObj.imported)
        setChecked = false;

      if(inputObj.cbChecked === false)
        setChecked = false;

      // set onclick event
      newCheckBox.onclick = setToggleBox;
      if(inputObj.checkId)
        newCheckBox.checkId = inputObj.checkId;

      // activate checkbox when field filled
      if(inputObjValue && inputObjValue.length > 0)
      {
        var checkValue = (setChecked) ? true : false;

        newCheckBox.setAttribute('checked', checkValue);
        newCheckBox.checked = checkValue;
        inputObj.cbChecked  = checkValue;
        // set the toggleBox
        if(inputObj.checkId)
        {
          var toggleBox = document.getElementById(inputObj.checkId);
          if(toggleBox)
            toggleBox.checked = checkValue;
        }
      }

      // reset the imported param
      inputObj.imported = false;

      if(xtraParameters == 'checked')
      {
        // force check box to checked
        newCheckBox.setAttribute('checked', true);
        newCheckBox.checked = true;
        inputObj.cbChecked  = true;
      }

      if(xtraParameters == 'unchecked')
      {
        // force check box to unchecked
        newCheckBox.setAttribute('checked', false);
        newCheckBox.checked = false;
        //newCheckBox.removeAttribute('checked');
        inputObj.cbChecked  = false;
      }
    }

    if(!dataComplete && inputObj)
      inputObj.cbNotComplete = true;

    return true;
  }

  /**
  * @desc set togglebox to true if a checkbox is checked
  * @param void
  * @return void
  **/
  function setToggleBox()
  {
    var checkBox  = this;
    var toggleBox = false;
    var inputObj  = false;

    if(checkBox.checkId && checkBox.checked)
      toggleBox = document.getElementById(checkBox.checkId);

    if(checkBox.checked && toggleBox)
      toggleBox.checked = true;

    if(checkBox.rowIndex)
      inputObj  = document.getElementById('hype_address_' + checkBox.rowIndex);

    if(inputObj)
      inputObj.cbChecked  = checkBox.checked;
  }


  /**
  * @desc set all checkBoxes in the parrent div to the state of the triggerBox
  * @param string triggerBox
  * @param string id parent div
  * @return void
  **/
  function setCheckBoxes(triggerBoxId, parentDivId)
  {
    var inputObjects = new Array();
    var triggerBox = document.getElementById(triggerBoxId);
    var parentDiv  = document.getElementById(parentDivId);

    if(parentDiv)
      inputObjects = parentDiv.getElementsByTagName('input');

    if(triggerBox && parentDiv && inputObjects && inputObjects.length > 0)
    {
      var status = (triggerBox.checked) ? true : false;
      var index = 0;
      for(index in inputObjects)
      {
        var currentInput = inputObjects[index];
        if(currentInput && currentInput.type == 'checkbox' && currentInput.id != triggerBoxId)
        {
          currentInput.checked = status;
          if(currentInput.rowIndex)
            inputObj  = document.getElementById('hype_address_' + currentInput.rowIndex);

          if(inputObj)
            inputObj.cbChecked  = status;
        }
      }
    }
  }
