﻿function insertAtCursor(myFieldId, myValue) {
    //IE support
    var myField = $get(myFieldId);
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)
                  + myValue
                  + myField.value.substring(endPos, myField.value.length);
    } else {
        myField.value += myValue;
    }
}

function setOpaque(button) {
    button.style.opacity = 0.5;
    button.style.filter = "alpha('opacity=50')";
    button.style.backgroundColor = "#ffff00";

    // change text to ... 
    //if (button.hasChildNodes())
    //{
    //    while (button.childNodes.length > 0)
    //        button.removeChild(button.firstChild);
    //}
    //button.appendChild(document.createTextNode("..."));
    button
    // button.disabled = 'true';
    return true;
}

function treeUp(obj) {
    obj.style.zIndex = 1000;
    obj.style.border = "1px solid #FF0000";
    obj.style.backgroundColor = "#CCCCCC";

    if (obj.childNodes != null && obj.childNodes.length > 0) {
        for (var i = 0; i < obj.childNodes.length; i++)
            if (obj.childNodes[i].classList != null && obj.childNodes[i].classList.length > 0 && obj.childNodes[i].classList[0] == "hidden-tree-buttons")
                obj.childNodes[i].style.display = "block";
    }
}

function treeDown(obj, index) {
    obj.style.zIndex = index;
    obj.style.border = "";
    obj.style.backgroundColor = "";

    if (obj.childNodes != null && obj.childNodes.length > 0) {
        for (var i = 0; i < obj.childNodes.length; i++)
            if (obj.childNodes[i].classList != null && obj.childNodes[i].classList.length > 0 && obj.childNodes[i].classList[0] == "hidden-tree-buttons")
                obj.childNodes[i].style.display = "none";
    }
}

function updateCountdown (cont, targetDate) {

    var container = document.getElementById(cont);

    
  var currentTime = new Date ( );
  var sec = (1000);

  var diff = targetDate - currentTime;

  if (diff < 0)
      container.innerHTML = "*";
  else {
      var days = Math.floor(diff / (sec * 3600 * 24));

      if (days > 0)
          diff -= (days * sec * 3600 * 24);

      var hours = Math.floor(diff / (sec * 3600));

      if (hours > 0)
          diff -= (hours * sec * 3600);

      var minutes = Math.floor(diff / (sec * 60));

      if (minutes > 0)
          diff -= (minutes * sec * 60);

      var seconds = Math.floor(diff / sec);

      if (days > 0)
          container.innerHTML = days + "d";
      else if (hours > 0)
          container.innerHTML = hours + "h " + minutes + "m";
      else
          container.innerHTML = (minutes < 10 ? "0":"") + minutes + ":" + (seconds < 10 ? "0":"") + seconds;
  }
}


