﻿
window.document.onkeydown = DetectEnterPressed;
var defaultButtonID;
var initialMessageLength;

function get_object(id) {
    var object = null;
    if (document.layers) {
        object = document.layers[id];
    } else if (document.all) {
        object = document.all[id];
    } else if (document.getElementById) {
        object = document.getElementById(id);
    }
    return object;
}

function DetectEnterPressed(e) {
    var characterCode
    if (e && e.which) { // NN4 specific code
        e = e
        characterCode = e.which
    }
    else {
        e = event
        characterCode = e.keyCode // IE specific code
    }
    if (characterCode == 13) // Enter key is 13
    {
        if (defaultButtonID) {
            var btn = get_object(defaultButtonID);
            if (btn) {
                addClickFunction(btn);
                if (btn && typeof (btn.click) != "undefined") {
                    btn.click();
                    event.cancelBubble = true;
                    if (event.stopPropagation) event.stopPropagation();
                    return false;
                }
            }
        }
    }
}

function addClickFunction(btn) {    
    {
        if (btn && typeof (btn.click) == 'undefined') btn.click = function () {
            {
                var result = true; if (btn.onclick) result = btn.onclick();
                if (typeof (result) == 'undefined' || result) {
                    if (btn.constructor != Array) {
                        try {
                            eval(btn.getAttribute('href'));
                        }
                        catch (ex) {
                            document.location = btn.getAttribute('href');
                        }
                    }
                }
            }
        } 
    }
};

function setDisplayStyle(actionControlID, displayCondition, targetControlID) {
    try {
        var actionCtrl = get_object(actionControlID);
        var index = actionCtrl.selectedIndex;
        var selectedValue = actionCtrl.options[index].value;
        var targetCtrl = get_object(targetControlID);

        if (targetCtrl != null) {
            if (selectedValue == displayCondition) {
                targetCtrl.style.display = '';
            }
            else {
                targetCtrl.style.display = 'none';
            }
        }
    }
    catch (ex) { }
}

function setEnabledStatus(actionControlID, disableValue, targetControlID) 
{
    try 
    {
        var actionCtrl = get_object(actionControlID);
        var index = actionCtrl.selectedIndex;
        var selectedValue = actionCtrl.options[index].value;
        var targetCtrl = get_object(targetControlID);

        if (targetCtrl != null) 
        {
            if (selectedValue == disableValue) 
            {
                targetCtrl.disabled = true;
            }
            else 
            {
                targetCtrl.disabled = false;
            }
        }
    }
    catch (ex) {}
}

function setVisibilityStatusByDropdown(actionControlID, visibilityValue, targetControlID) 
{
    try 
    {
        var actionCtrl = get_object(actionControlID);
        var index = actionCtrl.selectedIndex;
        var selectedValue = actionCtrl.options[index].value;

        if (targetControlID != null) 
        {
            if (selectedValue == visibilityValue) 
            {
                setVisibilityToElement(targetControlID, true);
            }
            else 
            {
                setVisibilityToElement(targetControlID, false);
            }
        }
    }
    catch (ex) {}
}

function setVisibilityStatusByCheckbox(actionControlID, targetControlID) 
{
    try 
    {
        var actionCtrl = get_object(actionControlID);

        if (targetControlID != null && actionCtrl != null) 
        {
            setVisibilityToElement(targetControlID, actionCtrl.checked);
        }
    }
    catch (ex) {}
}

function setVisibilityToElement(item, show) 
{
    if (show) 
    {
        get_object(item).style.display = "inline";
    }
    else 
    {
        get_object(item).style.display = "none";
    }
}

function setVisibilityToUsageList(ctrlID, ctrlListID, ctrlImgID) 
{
    var ctrl = get_object(ctrlID);
    var ctrlLst = get_object(ctrlListID);
    var ctrlImg = get_object(ctrlImgID);

    var lstIndex = ctrlLst.selectedIndex;
    var lstCount = ctrlLst.length;

    if (ctrl.style.display == "inline") 
    {
        ctrl.style.display = "none";
        ctrlImg.title = "Website element is used in " + (lstCount*1) + " pages. Click to see pages list.";
    }
    else 
    {
        if ((lstIndex * 1) > -1) 
        {
            ctrl.style.display = "inline";
            ctrlImg.title = "Click here to close pages list.";
        }
    }
}

function setHiddenValueFromDropdown(actionControlID, hiddenControlID) 
{
    try 
    {
        var actionCtrl = get_object(actionControlID);
        var targetCtrl = get_object(hiddenControlID);

        if (targetCtrl != null && actionCtrl != null) 
        {
            var idxValue = actionCtrl.selectedIndex;
            var selValue = actionCtrl.options[idxValue].value;

            if (selValue != null) 
            {
                targetCtrl.value = selValue;
            }
        }
    }
    catch (ex) {}
}

function SetSelectionByIdsAndStatus(repeaterControlID, targetControlIDs, checkboxControlID) 
{
    var repeaterControl = get_object(repeaterControlID);
    var checkboxControl = get_object(checkboxControlID);

    var ids             = targetControlIDs.split(',');
    var idsLength       = (ids.length * 1);

    for (i = 0; i < idsLength; i++) 
    {
        var tempControl = get_object(ids[i]);
        if (tempControl != null) 
        {
            tempControl.checked = checkboxControl.checked;
        } 
    }
}

function setEnabledToElement(item, enabled) 
{
    if (enabled) 
    {
        get_object(item).disabled = false;
    }
    else 
    {
        get_object(item).disabled = true;
    }
}

function setControlEnabledStatus(actionControlID, targetControlID) 
{
    try 
    {
        var actionCtrl = get_object(actionControlID);

        if (targetControlID != null && actionCtrl != null) 
        {
            setEnabledToElement(targetControlID, actionCtrl.checked);
        }
    }
    catch (ex) {}
}

function setValidatorEnabledStatus(validatorControlID, status) 
{
    var validatorCtrl = get_object(validatorControlID);
    ValidatorEnable(myVal, status);
}

function setClassName(target, className, oldCssClassName) {
    var trg = document.getElementById(target);
    if (trg) {
        if (oldCssClassName) {
            if (trg.className != oldCssClassName) {
                trg.className = className;
            }
        }
        else {
            trg.className = className;
        }
    }
}

function controlFocus(target) { 
    var trg = document.getElementById(target);
    if (trg) {
        trg.focus();
    }
}
// Validate date
// Declaring valid date character, minimum year and maximum year
var dtCh = "-";
var minYear = 1900;
var maxYear = 2100;

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary(year) {
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}
function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
        if (i == 2) { this[i] = 29 }
    }
    return this
}

function isDate(dtStr, separator) {
    if (separator != '') {
        dtCh = separator;
    }
    var daysInMonth = DaysArray(12)
    var pos1 = dtStr.indexOf(dtCh)
    var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
    var strDay = dtStr.substring(0, pos1)
    var strMonth = dtStr.substring(pos1 + 1, pos2)
    var strYear = dtStr.substring(pos2 + 1)
    strYr = strYear
    if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
    if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
    }
    month = parseInt(strMonth)
    day = parseInt(strDay)
    year = parseInt(strYr)

    if (pos1 == -1 || pos2 == -1) {
        return false
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {
        return false
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        return false
    }
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
        return false
    }
    if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
        return false
    }
    return true
}

// Changes the cursor to an hourglass
function cursor_wait() {
    document.body.style.cursor = 'wait';
}

// Returns the cursor to the default pointer
function cursor_clear() {
    document.body.style.cursor = 'default';
}

function treeViewCheck(e)
{
    var src;

    if (e) 
    {
        //Netscape
        src = e.target;
    }
    else 
    {
        //IE
        if (window.event) { e = window.event; };
        src = e.srcElement ? e.srcElement : e.target;
    }

    var isChkBoxClick = (src.tagName.toLowerCase() == "input" && src.type == "checkbox");

    if (isChkBoxClick) 
    {
        var parentTable = GetParentByTagName("table", src);
        var nxtSibling = parentTable.nextSibling;

        if (nxtSibling && nxtSibling.nodeType == 1) 
        {
            if (nxtSibling.tagName.toLowerCase() == "div") //if node has children         
            {
                //Check or uncheck childrens             
                CheckUncheckChildren(parentTable.nextSibling, src.checked);

                if (src.checked) 
                {
                    //Check parent nodes
                    CheckUncheckParents(parentTable.nextSibling, src.checked);
                }
                return;
            }
            else if (nxtSibling.tagName.toLowerCase() == "table") 
            {
                if (src.checked) 
                {
                    //Check parent nodes
                    CheckUncheckParents(parentTable.nextSibling, src.checked);
                    return;
                }
            }
        }

        if (src.checked) 
        {
            CheckUncheckParents(parentTable, src.checked);
        }
    }
}

function CheckUncheckChildren(childContainer, check) 
{
    if (!check) 
    {
        var childChkBoxes = childContainer.getElementsByTagName("input");
        var childChkBoxCount = childChkBoxes.length;
        for (var i = 0; i < childChkBoxCount; i++) 
        {
            childChkBoxes[i].checked = check;
        }
    }
}

function CheckUncheckParents(srcChild, check) 
{
    var parentDiv = GetParentByTagName("div", srcChild);
    var parentNodeTable = parentDiv.previousSibling;

    if (parentNodeTable) 
    {
        var inpElemsInParentTable = parentNodeTable.getElementsByTagName("input");

        if (inpElemsInParentTable.length >= 0) 
        {
            var parentNodeChkBox = inpElemsInParentTable[0];
            parentNodeChkBox.checked = check;

            //Recursively set parent status
            CheckUncheckParents(parentNodeChkBox, check);
        }
    }
}

function GetParentByTagName(parentTagName, childElementObj) 
{
    var parent = childElementObj.parentNode;
    while (parent.tagName.toLowerCase() != parentTagName.toLowerCase()) 
    {
        parent = parent.parentNode;
    }

    return parent;
}

function registerTreeEvent(ctrlID) {
    try {
        var ctrl = get_object(ctrlID);
        if (ctrl != null) {
            ctrl.addEventListener('click', treeViewCheck, false);
        }
    } catch (e) { }
}

function hideProgressDiv() {
    try {
        document.body.style.cursor = 'auto';
        var updateProgressDiv = $get('updateProgressDiv');
        updateProgressDiv.style.display = 'none';
    }
    catch (ex) { }
}

function addTooltipToSelect() {
    var total = $('select option').length;
    var cur;
    for (var i = 0; i < total; i++) {
        cur = $('select option:eq(' + i + ')');
        cur.attr('title', cur.html());
    }
}


function FCKUpdateLinkedField(id) {
    try {
        if (typeof (FCKeditorAPI) == "object") {
            FCKeditorAPI.GetInstance(id).UpdateLinkedField();
        }
    }
    catch (err) {
    }
}

function onUpdating() {
    var updateProgressDiv = $get('updateProgressDiv');
    updateProgressDiv.style.display = '';
    var imgProgress = $get('imgProgress');
    imgProgress.style.top = $(window).height() / 2 + $(window).scrollTop() + "px";
    updateProgressDiv.style.height = $(document).height() + "px";
    updateProgressDiv.style.width = $(document).width() + "px";
}

function onUpdated() {
    var updateProgressDiv = $get('updateProgressDiv');
    updateProgressDiv.style.display = 'none';
}

function replacePlaceholderInControlWithValue(replacementText, controlId, placeholder) {
    var controlWhereToReplace = document.getElementById(controlId);
    if (controlWhereToReplace != null) {
        var txt = controlWhereToReplace.value;
        controlWhereToReplace.value = txt.replace(placeholder, replacementText);
    }
}

//updates the string at the specified position taking in consideration the previous inserted string
//using the initial length
function updateStringAtPositionInControlWithValue(replacementText, controlId, position) {
    var controlWhereToReplace = document.getElementById(controlId);
    if (controlWhereToReplace != null) {
        var txt = controlWhereToReplace.value;
        if (initialMessageLength == null)
            initialMessageLength = txt.length;
        var removeLength = txt.length - initialMessageLength;
        txt = txt.substring(0, position) + replacementText + txt.substring(position + removeLength, txt.length);
        controlWhereToReplace.value = txt;
    }
}

function removeDefaultButton() {
    if (defaultButtonID) {
        defaultButtonID = '';
    }
}

function changeTelecomMarket(sender, target) {
    var cssOn = 'market-on';
    var cssOff = 'market-off';
    var divNet = get_object('sc_net');
    var divTel = get_object('sc_tel');
    var divTv = get_object('sc_tv');

    var validCombination = false;

    var ddl = get_object('ddlMarketSelector')
    if (divNet && divTel && divTv && ddl) {
        var isNet = divNet.className == cssOn;
        var isTel = divTel.className == cssOn;
        var isTv = divTv.className == cssOn;

        //check valid combination
        if (sender == "net") {
            if (isNet) {
                //want to uncheck net
                if (!isTel && isTv) {
                    validCombination = true;
                    divNet.className = cssOff;
                    ddl.value = "9"; //digitalTV
                }
            }
            else {
                //want to check net
                validCombination = true;
                divNet.className = cssOn;
                if (isTel) {
                    if (isTv) {
                        ddl.value = "25"; //netTelTV
                    }
                    else {
                        ddl.value = "21"; //netTel
                    }
                }
                else {
                    if (isTv) {
                        ddl.value = "26"; //netTV
                    }
                    else {
                        ddl.value = "5"; //net
                    }
                }
            }
        }
        else if (sender == "tel") {
            if (isTel) {
                //want to uncheck tel
                validCombination = true;
                divTel.className = cssOff;
                if (isTel) {
                    if (isTv) {
                        ddl.value = "26"; //netTV
                    }
                    else {
                        ddl.value = "5"; //net
                    }
                }
                else {
                    if (isTv) {
                        ddl.value = "9"; //digitalTV
                    }
                }
            }
            else {
                //want to check tel
                if (isNet) {
                    validCombination = true;
                    divTel.className = cssOn;
                    if (isTv) {
                        ddl.value = "25"; //netTelTV
                    }
                    else {
                        ddl.value = "21"; //netTel
                    }
                }
                else {
                    validCombination = false;
                }
            }
        }
        else if (sender == "tv") {
            if (isTv) {
                //want to uncheck tv
                if (isNet) {
                    validCombination = true;
                    divTv.className = cssOff;
                    if (isTel) {
                        ddl.value = "21"; //netTel
                    }
                    else {
                        ddl.value = "5"; //net
                    }
                }
                else {
                    validCombination = false;
                }
            }
            else {
                //want to check tv
                if (isNet) {
                    validCombination = true;
                    divTv.className = cssOn;
                    if (isTel) {
                        ddl.value = "25"; //netTelTV
                    }
                    else {
                        ddl.value = "26"; //netTV
                    }
                }
                else {
                    if (!isTel) {
                        validCombination = true;
                        divTv.className = cssOn;
                        ddl.value = "9"; //digitalTV
                    }
                    else {
                        validCombination = false;
                    }
                }
            }
        }
    }

    if (validCombination) {
        __doPostBack(target,'');
    }
}
