// cursor ######################################################################
var cursorDivId = "cursorDiv200807080901"; // static
function followCursor(e) {
  var div = document.getElementById(cursorDivId);
  if (!div) return;
  var xcoord = 15; // offset
  var ycoord = -15; // offset
  if (typeof e != "undefined") {
    xcoord += e.pageX;
    ycoord += e.pageY;
  }
  else if (typeof window.event != "undefined") {
    var body = (!window.opera && document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
    xcoord += body.scrollLeft + event.clientX;
    ycoord += body.scrollTop + event.clientY;
  }
  div.style.visibility = "visible"; // show now that it's positioned
  div.style.left = xcoord + "px";
  div.style.top = ycoord + "px";
}
function showCursorImage(imgUrl, e) {
  var div = document.createElement("div");
  div.id = cursorDivId;
  div.style.position = "absolute";
  div.style.visibility = "hidden"; // hide until it's positioned
  div.style.left = "0px";
  div.style.top = "0px";
  e.parentNode.appendChild(div);
  var img = document.createElement("img");
  img.src = imgUrl;
  img.alt = "";
  img.style.border = "1px solid #000000";
  div.appendChild(img);
  document.onmousemove = followCursor;
}
function hideCursorImage() {
  var div = document.getElementById(cursorDivId);
  if (div) {
    div.style.visibility = "hidden";
    div.parentNode.removeChild(div);
  }
}
// tree2 checkboxes ############################################################
// Call this method with the onclick event of a checkbox inside a tomahawk:tree2 component.
// If the checkbox is not checked yet, check it and all its parents.
// If the checkbox is already checked, uncheck it and all its children.
function checkUp(checkbox) {
    if (checkbox == null || !checkbox.type || checkbox.type != "checkbox") return;
    if (checkbox.checked) {
        checkAllParentCheckboxes(checkbox);
    }
    else {
        uncheckAllChildCheckboxes(checkbox);
    }
}
// Call this method with the onclick event of a checkbox inside a tomahawk:tree2 component.
// If the checkbox is not checked yet, check it and all its children.
// If the checkbox is already checked, uncheck it and all its children.
function checkDown(checkbox) {
    if (checkbox == null || !checkbox.type || checkbox.type != "checkbox") return;
    if (checkbox.checked) {
        checkAllChildCheckboxes(checkbox);
    }
    else {
        uncheckAllChildCheckboxes(checkbox);
    }
}
// Gets the parent checkbox to a checkbox.
function getParentCheckbox(checkbox) {
    if (checkbox == null || !checkbox.type || checkbox.type != "checkbox") return null;
    var array = checkbox.id.split(":");
    var newId = "";
    for (var i = 0; i < array.length - 3; i++) {
        newId += array[i] + ":";
    }
    newId += array[array.length - 2] + ":" + array[array.length - 1];
    if (document.getElementById(newId)) {
        var parentCheckbox = document.getElementById(newId);
        if (parentCheckbox.type && parentCheckbox.type == "checkbox") {
            return parentCheckbox;
        }
    }
    return null;
}
// Gets the child checkboxes to a checkbox as an array.
function getChildCheckboxes(checkbox) {
    if (checkbox == null || !checkbox.type || checkbox.type != "checkbox") return new Array();
    var array = checkbox.id.split(":");
    var preId = "";
    for (var i = 0; i < array.length - 2; i++) {
        preId += array[i] + ":";
    }
    var postId = ":" + array[array.length - 2] + ":" + array[array.length - 1];
    var result = new Array();
    for (var i = 0; document.getElementById(preId + i + postId) != null; i++) {
        var childCheckbox = document.getElementById(preId + i + postId);
        if (childCheckbox.type && childCheckbox.type == "checkbox") {
            result[i] = childCheckbox;
        }
    }
    return result;
}
// Checks a checkbox, if it isn't already checked.
function checkCheckbox(checkbox) {
    if (!checkbox.checked) {
        checkbox.checked = true;
    }
}
// Unchecks a checkbox, if it isn't already unchecked.
function uncheckCheckbox(checkbox) {
    if (checkbox.checked) {
        checkbox.checked = false;
    }
}
// Check all parent checkboxes to a checkbox, if they aren't already checked.
function checkAllParentCheckboxes(checkbox) {
    if (checkbox == null || !checkbox.type || checkbox.type != "checkbox") return;
    var parent = getParentCheckbox(checkbox);
    if (parent != null) {
        checkCheckbox(parent);
        checkAllParentCheckboxes(parent);
    }
}
// Uncheck all parent checkboxes to a checkbox, if they are checked.
function uncheckAllParentCheckboxes(checkbox) {
    if (checkbox == null || !checkbox.type || checkbox.type != "checkbox") return;
    var parent = getParentCheckbox(checkbox);
    if (parent != null) {
        uncheckCheckbox(parent);
        uncheckAllParentCheckboxes(parent);
    }
}
// Check all child checkboxes to a checkbox, if they aren't already checked.
function checkAllChildCheckboxes(checkbox) {
    if (checkbox == null || !checkbox.type || checkbox.type != "checkbox") return;
    var children = getChildCheckboxes(checkbox);
    for (var i = 0; i < children.length; i++) {
        checkCheckbox(children[i]);
        checkAllChildCheckboxes(children[i]);
    }
}
// Uncheck all child checkboxes to a checkbox, if they are checked.
function uncheckAllChildCheckboxes(checkbox) {
    if (checkbox == null || !checkbox.type || checkbox.type != "checkbox") return;
    var children = getChildCheckboxes(checkbox);
    for (var i = 0; i < children.length; i++) {
        uncheckCheckbox(children[i]);
        uncheckAllChildCheckboxes(children[i]);
    }
}
// visibility ##################################################################
function toggleElement(id) {
    var element = document.getElementById(id);
    if (element != null) {
        if (element.style.display == "") {
            element.style.display = "none";
        }
        else {
            element.style.display = "";
        }
    }
}
function showElement(id) {
    var element = document.getElementById(id);
    if (element != null) {
        element.style.display = "";
    }
}
function hideElement(id) {
    var element = document.getElementById(id);
    if (element != null) {
        element.style.display = "none";
    }
}
// countdown ###################################################################
function disableDelay(element, delay, wt) {
    element.disabled = true;
    var originalText = element.value; 
    var elapsed = 0;
    var waitText = (!wt) ? 'Sending' : wt;
    __animateText();
    function __animateText() {
        waitText = waitText + '.';
        element.value = waitText;
        elapsed += 500;
        if (elapsed >= delay) {
            window.clearTimeout();
            elapsed = 0;
            element.disabled = false;
            element.value = originalText;
        }
        else {
            window.setTimeout(__animateText, 500);
        }
    }
}
/**
 * Counts down seconds. Once <code>0</code> is reached, a click event is fired
 * on the <code>b</code> element. While counting down, innerHTML of <code>e</code>
 * is set with the current number of seconds left. The count down starts with
 * the innerHTML value of <code>e</code>. If this value isn't a usable number,
 * this method will fire the click event right away instead.
 * @param e an element, whos innerHTML value is used as starting point for the
 * count down and whos innerHTML value is updated each second, while the count
 * down takes place
 * @param b an element, whos click event is triggered once the count down reach
 * <code>0</code> or if an invalid starting point for the count down was detected;
 * this element probably should be limited to an input button or submit button
 */
function countDownToClick(e, b) {
    var s = e.innerHTML;
    if (!(s >= 0)) {
        s = 0;
    }
    __countDown();
    function __countDown() {
        if(s == 0) {
            b.click();
        }
        else{
            e.innerHTML = s--;
            window.setTimeout(__countDown, 1000);
        }
    }
}