﻿function ForceClick(delegateID) {
    //click the delegate
    var delegateNode = $get(delegateID);
    if (delegateNode != null) {
        delegateNode.click()
    } else {
        return false;
    }
}

function AddWaterMarks() {
    for (var i = 0; i < document.forms[0].elements.length; i++) {
        var aFrmElement = document.forms[0].elements[i];
        if (aFrmElement.getAttribute('WatermarkText') != null) {
            AddWaterMark(aFrmElement.id);
        }
    }
}

function AddWaterMark(txtID) {
    var aTxt = $get(txtID);
    if (aTxt != null) {
        if (aTxt.value == "") {
            var aDiv = document.getElementById(txtID + "watermark");
            if (aDiv == null) {
                aDiv = document.createElement('div');
                aDiv.id = txtID + "watermark";
                aDiv.className = aTxt.getAttribute('WatermarkCssClass');
                aDiv.innerHTML = aTxt.getAttribute('WatermarkText');
                aDiv.style.left = findPosX(aTxt) + "px";
                aDiv.style.top = findPosY(aTxt) + "px";
                aDiv.onclick = function() { RemoveWaterMark(txtID, true); };
                aTxt.onfocus = function() { RemoveWaterMark(txtID); };
                aTxt.onblur = function() { AddWaterMark(txtID); };
                aTxt.parentNode.insertBefore(aDiv, aTxt);
            }
        }
    }
}

function RemoveWaterMark(txtID, setFocus) {
    var aTxt = $get(txtID);
    if (aTxt != null) {
        var aDiv = document.getElementById(txtID + "watermark");
         if(aDiv != null){
             aTxt.parentNode.removeChild(aDiv);
         }
         if (setFocus) { aTxt.focus(); }
     }

     aTxt.select();
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function SetExpandCollapseCssClass(divID, chkID) {
    var aDiv = $get(divID);
    var aCheckbox = $get(chkID);
    if (aDiv != null) {
        if (aDiv.className == 'TLDCategoryClosed') {
            aDiv.className = 'TLDCategory';
            aCheckbox.checked = true;
        } else {
            aDiv.className = 'TLDCategoryClosed';
            aCheckbox.checked = false;
        }
    }
}

function EnterKeyHandler(e, targetID) {
    if (e.keyCode == 13) {
        var btn = document.getElementById(targetID);
        if (btn.href == "" || btn.href == null) {
            btn.click();
        }
        else {
            window.location = btn.href;
        }
        return false;
    }
    else {
        return true;
    }
}

function CheckAll(divID) {
    var oWrapper = document.getElementById(divID);
    var divs = oWrapper.getElementsByTagName('div');
    var allChecked = true;
    
    for (var h = 0; h < divs.length; h++) {
        if (divs[h].className == 'TLDCategory') {
            var checkBoxes = divs[h+1].getElementsByTagName('input');
            for (var i = 0; i < checkBoxes.length; i++) {
                if (checkBoxes[i].type == 'checkbox') {
                    if (!(checkBoxes[i].checked) && !(checkBoxes[i].disabled)) {
                        allChecked = false;
                    }
                }
            }
        }
    }
    
    for (var h = 0; h < divs.length; h++) {
        if (divs[h].className == 'TLDCategory') {
            var checkBoxes = divs[h+1].getElementsByTagName('input');
            for (i = 0; i < checkBoxes.length; i++) {
                if (checkBoxes[i].type == 'checkbox') {
                    if (!(checkBoxes[i].disabled)) {
                        if (allChecked) {
                            if (checkBoxes[i].checked) {
                                checkBoxes[i].click();
                            }
                        } else {
                            if (!checkBoxes[i].checked) {
                                checkBoxes[i].click();
                            }
                        }
                    }
                }
            }
        }
    }
}