function CB_ExtraResponseChecked(CB, TXT, RFV, DDL1, DDL2, TR, HF) {
    var CBD = document.getElementById(CB);
    var TXTD = document.getElementById(TXT);
    var RFVD = document.getElementById(RFV);
    var DDDL1 = document.getElementById(DDL1);
    var DDDL2 = document.getElementById(DDL2);
    var TRD = document.getElementById(TR);
    var HF_Response13 = document.getElementById(HF);

    TRD.style.display = '';
    HF_Response13.value = 'e';
    if (CBD.checked == true) {
        TXTD.disabled = false;
        RFVD.disabled = false;
        DDDL1.disabled = false;
        DDDL2.disabled = false;
    }
    else {
        TXTD.disabled = true;
        RFVD.disabled = true;
        DDDL1.disabled = true;
        DDDL2.disabled = true;
    }
}

function CB_EnableQueueTimeout(CB, TXT, RFV, REV, DDL, TRS, TXTS, TRF, DIV) {
   
    var CBQ = document.getElementById(CB);
    var TXTQ = document.getElementById(TXT);
    var RFVQ = document.getElementById(RFV);
    var REVQ = document.getElementById(REV);
    var DDLQ = document.getElementById(DDL);
    var TRSQ = document.getElementById(TRS);
    var TRFQ = document.getElementById(TRF);
    var TXTSQ = document.getElementById(TXTS);
    var DIVQ = document.getElementById(DIV);

    TXTQ.disabled = true;
    RFVQ.disabled = true;
    REVQ.disabled = true;

    setTimeout(function () { DDLQ.selectedIndex = 0; $(DDLQ).trigger('change'); }, 500);
    toggleCombobox(DDLQ.id);
    DDLQ.disabled = true;
    TXTQ.value = '';
    TRSQ.style.display = 'none';
    TRFQ.style.display = 'none';
    TXTSQ.value = '';
    DIVQ.style.display = 'none';

    if (CBQ.checked == true) {
        TXTQ.disabled = false;
        RFVQ.disabled = false;
        REVQ.disabled = false;
        DDLQ.disabled = false;
        TXTQ.value = '10';
        DIVQ.style.display = '';
    }
}

function DDL_EnableSIPTimeOut(DDL, TR, RFV, TRFQ, LBFormat) {
    var DDLQ = document.getElementById(DDL);
    var TRQ = document.getElementById(TR);  // sip domain row
    var RFVQ = document.getElementById(RFV);
    var TRFQQ = document.getElementById(TRFQ);  // Forward to queue row
    var LBFormatQ = document.getElementById(LBFormat);

    TRQ.style.display = 'none';
    TRFQQ.style.display = 'none';
    RFVQ.disabled = true;
    LBFormatQ.innerHTML = '';

    // Options: 1-Disconnect, 2-Voice mail, 3-telephone, 4-sip address, 5-queue
    if (DDLQ.options[DDLQ.selectedIndex].value == "1") {
        LBFormatQ.innerHTML = '';
    }
    else if (DDLQ.options[DDLQ.selectedIndex].value == "2") {
        TRQ.style.display = '';
        RFVQ.disabled = false;
        LBFormatQ.innerHTML = 'Example: mail@somedomain.com';
    }
    else if (DDLQ.options[DDLQ.selectedIndex].value == "3") {
        TRQ.style.display = '';
        RFVQ.disabled = false;
        LBFormatQ.innerHTML = 'Example: number@somedomain.com';
    }
    else if (DDLQ.options[DDLQ.selectedIndex].value == "4") {
        TRQ.style.display = '';
        RFVQ.disabled = false;
        LBFormatQ.innerHTML = 'Example: mail@somedomain.com';
    }
    else if (DDLQ.options[DDLQ.selectedIndex].value == "5") {
        TRFQQ.style.display = '';
    }

}

function CB_EnableQueueOverflow(CB, TXT, RFV, DDLFC, DDLCA, TRS, TXTS, TRF, DIV) {
    var CBQ = document.getElementById(CB);
    var TXTQ = document.getElementById(TXT);
    var RFVQ = document.getElementById(RFV);
    var DDLFCQ = document.getElementById(DDLFC);
    var DDLCAQ = document.getElementById(DDLCA);
    var TRSQ = document.getElementById(TRS);
    var TRFQ = document.getElementById(TRF);
    var TXTSQ = document.getElementById(TXTS);
    var DIVO = document.getElementById(DIV);

    TXTQ.disabled = true;
    RFVQ.disabled = true;

    setTimeout(function () { DDLFCQ.selectedIndex = 0; $(DDLFCQ).trigger('change'); }, 500);
    setTimeout(function () { DDLCAQ.selectedIndex = 0; $(DDLCAQ).trigger('change'); }, 500);
    toggleCombobox(DDLFCQ.id);
    toggleCombobox(DDLCAQ.id);
    DDLFCQ.disabled = true;
    DDLCAQ.disabled = true;
    TXTQ.value = '';
    TRSQ.style.display = 'none';
    TRFQ.style.display = 'none';
    TXTSQ.value = '';
    DIVO.style.display = 'none';

    if (CBQ.checked == true) {
        TXTQ.disabled = false;
        RFVQ.disabled = false;
        DDLFCQ.disabled = false;
        DDLCAQ.disabled = false;
        TXTQ.value = '0';
        DIVO.style.display = '';
    }
}

function DDL_EnableAgent(DDL, TR1, TR2) {
    var DDLQ = document.getElementById(DDL);
    var TR1Q = document.getElementById(TR1);
    var TR2Q = document.getElementById(TR2);
    //var RFVQ = document.getElementById(RFV);

    TR1Q.style.display = 'none';
    TR2Q.style.display = 'none';
    //RFVQ.disabled = true;
    if (DDLQ.options[DDLQ.selectedIndex].value == "1") {
        TR1Q.style.display = '';
    }
    else {
        TR2Q.style.display = '';
        //RFVQ.disabled = false;
    }
}

function DDL_EnableDDLAction(DDL, TR, RFV, Ltl) {
    var DDLWF = document.getElementById(DDL);
    var TRWF = document.getElementById(TR);
    var RFVWF = document.getElementById(RFV);
    var LtlWF = document.getElementById(Ltl);

    TRWF.style.display = 'none';
    RFVWF.disabled = true;
    LtlWF.style.display = 'none';
    LtlWF.innerHTML = '';

    if (DDLWF.options[DDLWF.selectedIndex].value != "1") {
        TRWF.style.display = '';
        RFVWF.disabled = false;
        LtlWF.style.display = '';
        LtlWF.innerHTML = 'Example: sales@contoso.co';
    }
    if (DDLWF.options[DDLWF.selectedIndex].value == "4") {
        LtlWF.style.display = '';
        LtlWF.innerHTML = 'Example: +14255550165@contoso.com';
    }
}
function DDL_FormatPhone(DDLP, TXTP) {
    var DDLWF = document.getElementById(DDLP);
    var TXTWF = document.getElementById(TXTP);

    if (DDLWF.options[DDLWF.selectedIndex].value != "0" && DDLWF.options[DDLWF.selectedIndex].value != "-1") {
        var res = DDLWF.options[DDLWF.selectedIndex].text.split(" ");
        TXTWF.value = res[0]; //.replace(/(\d{1})(\d{3})(\d{3})/, "$1 ($2) $3");
    }
    else { TXTWF.value = ''; }
}
function CB_EnableLinkedUser(CB, TXTOS, RFVOS, TXTP, TXTCP, RFVP, RFVCP, CVCP, LBG) {
    var CBL = document.getElementById(CB);
    var TXTOSL = document.getElementById(TXTOS);
    var RFVOSL = document.getElementById(RFVOS);
    var TXTPL = document.getElementById(TXTP);
    var TXTCPL = document.getElementById(TXTCP);
    var RFVPL = document.getElementById(RFVP);
    var RFVCPL = document.getElementById(RFVCP);

    var CVCPL = document.getElementById(CVCP);
    var LBGL = document.getElementById(LBG);
    TXTOSL.disabled = true;
    RFVOSL.disabled = true;
    TXTPL.disabled = false;
    TXTCPL.disabled = false;
    ValidatorEnable(RFVPL, true);
    ValidatorEnable(RFVCPL, true);

    CVCPL.disabled = false;
    try {
        LBGL.style.display = "";
    }
    catch (err) {
        //Handle errors here
    }
    if (CBL.checked == true) {
        TXTOSL.disabled = false;
        RFVOSL.disabled = false;
        TXTPL.disabled = true;
        TXTCPL.disabled = true;
        ValidatorEnable(RFVPL, false);
        ValidatorEnable(RFVCPL, false);

        CVCPL.disabled = true;
        try {
            LBGL.style.display = "none";
        }
        catch (err) {
            //Handle errors here
        }
    }
}

function CB_EnableBulkLinkedUser(CB, TXTOS, RFVOS) {
    var CBL = document.getElementById(CB);
    var TXTOSB = document.getElementById(TXTOS);
    var RFVOSB = document.getElementById(RFVOS);
    TXTOSB.disabled = true;
    RFVOSB.disabled = true;
    if (CBL.checked == true) {
        TXTOSB.disabled = false;
        RFVOSB.disabled = false;
    }
}

function CB_BHoursShowHide(CB_Id, DDL, ROW) {
    var TR_Enable = document.getElementById(DDL);
    var TR_Disable = document.getElementById(ROW);
    var CB = document.getElementById(CB_Id);
    TR_Enable.style.display = '';
    TR_Disable.style.display = '';

    if (CB.checked == true) {
        TR_Disable.style.display = 'none';
    }

}

// Hide row if checkbox/radio is checked
function CB_ShowHideRow(CB, TR) {
    var TRG = document.getElementById(TR);
    var CBG = document.getElementById(CB);
    TRG.style.display = '';

    if (CBG.checked == true) {
        TRG.style.display = 'none';
    }

}
function CB_ShowHideCreatorRow(CB, TR, CBAccess, TRAccess) {
    var TRG = document.getElementById(TR);
    var CBG = document.getElementById(CB);
    var CBA = document.getElementById(CBAccess);
    var TRA = document.getElementById(TRAccess);
    TRG.style.display = '';

    if (CBG.checked == true) {
        TRG.style.display = 'none';
        CBA.checked = true;
        TRA.style.display = 'none';
    }

}

// Show Control if checkbox/radio is checked
function CB_ShowHideControl(CB, CT) {
    var CBG = document.getElementById(CB);
    var CTG = document.getElementById(CT);
    $(CTG).fadeOut(200);
    //CTG.style.display = 'none';
    if (CBG.checked == true) {
        //CTG.style.display = '';
        $(CTG).fadeIn(200);
    }

}

function CB_EnableDisableControl(CB, CR) {
    var CBG = document.getElementById(CB);
    var CRG = document.getElementById(CR);
    CRG.disabled = false;

    if (CBG.checked == true) {
        CRG.value = '';
        try {
            CRG.checked = false;
        }
        catch (err) {
            //Handle errors here
        }
        CRG.disabled = true;
    }

}


// used in bulk user data bound
function DDL_EnableExtension(DDL, TXT) {
    if (TXT != null) {
        var BDDL = document.getElementById(DDL);
        var BTXT = document.getElementById(TXT);
        if (BTXT != null) {
            BTXT.value = '';
            BTXT.disabled = true;
            if ((BDDL.options[BDDL.selectedIndex].value != "-1") && (BDDL.options[BDDL.selectedIndex].value != "0")) {
                BTXT.disabled = false;
            }
        }
    }
}

function DDL_EnableExtensionEdit(DDL, TXTExt, TXTUM) {
    var BDDL = document.getElementById(DDL);
    if (TXTExt != null) {
        var BTXT = document.getElementById(TXTExt);
        if (BTXT != null) {
            BTXT.value = '';
            BTXT.disabled = true;
            if ((BDDL.options[BDDL.selectedIndex].value != "-1") && (BDDL.options[BDDL.selectedIndex].value != "0")) {
                BTXT.disabled = false;
            }
        }
    }

    var BTXTUM = document.getElementById(TXTUM);
    if (BTXTUM != null) {
        BTXTUM.value = BDDL.options[BDDL.selectedIndex].text.split(' ')[0].trim().replace('+', '');
        BTXTUM.focus();
    }
}

function CB_AllowAccess(CBCreator, RBLAccess, CBEntireAccess) {
    var CBC = document.getElementById(CBCreator);   // Creator check box
    var RBLA = document.getElementById(RBLAccess);  // Access radio button list with Allow/Deny
    var CBEA = document.getElementById(CBEntireAccess);  // Access header check box
    if (CBEA.checked == false) {
        if (CBC.checked == true) {
            RBLA.rows[0].cells[0].childNodes[0].checked = true;
            ChangeRBListItemState(RBLA, true);
        }
        else {
            ChangeRBListItemState(RBLA, false);
        }
    }
}

function ChangeRBListItemState(RBLA, disable) {
    var rbItems = RBLA.getElementsByTagName('input');
    for (var itemIndex = 0; itemIndex < rbItems.length; itemIndex++) {
        rbItems[itemIndex].disabled = disable;
    }
}

function Img_ResetAccess(RBLAccess) {
    var RBLA = document.getElementById(RBLAccess);  // Access radio button list with Allow/Deny
    var rbItems = RBLA.getElementsByTagName('input');
    for (var itemIndex = 0; itemIndex < rbItems.length; itemIndex++) {
        if (rbItems[itemIndex].disabled == false)
            rbItems[itemIndex].checked = false;
    }
}

function CB_GroupsAllowAccess(CBCreator, RBLAccess) {
    var CBC = document.getElementById(CBCreator);   // Creator check box
    var RBLA = document.getElementById(RBLAccess);  // Access radio button list with Allow/Deny
    if (CBC.checked == true) {
        RBLA.rows[0].cells[0].childNodes[0].checked = true;
        ChangeRBListItemState(RBLA, true);
    }
    else {
        ChangeRBListItemState(RBLA, false);
    }
}

function Img_ResetRoomUsers(CBManager, CBMember, CBPresenter) {
    var CBMr = document.getElementById(CBManager);
    var CBMm = document.getElementById(CBMember);
    var CBP = document.getElementById(CBPresenter);

    if (CBMr.disabled == false)
        CBMr.checked = false;
    if (CBMm.disabled == false)
        CBMm.checked = false;
    if (CBP.disabled == false)
        CBP.checked = false;
}

function CB_AssignRoles(CBManager, CBMember, CBPresenter) {
    var CBMr = document.getElementById(CBManager);
    var CBMm = document.getElementById(CBMember);
    var CBP = document.getElementById(CBPresenter);

    if (CBMr.checked) {
        if (CBMm.disabled == false)
            CBMm.checked = true;
        if (CBP.disabled == false)
            CBP.checked = true;
    }

}

function SelectImportUser(CB, RB1, RB2, DDL1, DDL2) {
    var sCB = document.getElementById(CB);
    var sRB1 = document.getElementById(RB1);
    var sRB2 = document.getElementById(RB2);
    var sDDL1 = document.getElementById(DDL1);
    var sDDL2 = document.getElementById(DDL2);

    sRB1.checked = false;
    sRB2.checked = false;

    if (sCB.checked) {
        sRB1.disabled = false;
        sRB1.checked = true;
        sDDL1.disabled = false;
        if (sDDL2.options[sDDL2.selectedIndex].value != "0") {
            sRB2.disabled = false;
            sDDL2.disabled = false;
        }
    }
    else {
        sRB1.disabled = true;
        sRB2.disabled = true;
        sDDL1.disabled = true;
        sDDL2.disabled = true;
    }
}

function ShowHideDiv(objDiv) {
    var div = document.getElementById(objDiv);
    if (div.style.display == 'none') {
        div.style.display = '';
    }
    else {
        div.style.display = 'none';
    }
}

function CB_EnableOnSelectionChange(RB, CR) {
    var CBG = document.getElementById(RB);
    var CRG = document.getElementById(CR);

    try {
        if (CBG.checked == true) {
            CRG.disabled = false;


        }
        else {

            CRG.checked = false;
            CRG.disabled = true;
        }

    }
    catch (e) { }

}

function ShowHideConferencingOptions(DDLProvider, TRTollNumber, RFVTollNUmber, TRTollFreeNumber, TRPassCode, RFVPassCode) {
    var DDL = document.getElementById(DDLProvider);
    var TR1 = document.getElementById(TRTollNumber);
    var TR2 = document.getElementById(TRTollFreeNumber);
    var TR3 = document.getElementById(TRPassCode);

    var RF1 = document.getElementById(RFVTollNUmber);
    var RF2 = document.getElementById(RFVPassCode);

    TR1.style.display = '';
    TR2.style.display = '';
    TR3.style.display = '';
    ValidatorEnable(RF1, true);
    ValidatorEnable(RF2, true);

    if (DDL.options[DDL.selectedIndex].value == "0") {
        TR1.style.display = 'none';
        TR2.style.display = 'none';
        TR3.style.display = 'none';
        ValidatorEnable(RF1, false);
        ValidatorEnable(RF2, false);
    }
}

function CreateExpression(StartingDigits, LengthOption, LengthValue, DigitsToRemove, PatternExpression) {
    var CtlStartingDigits = document.getElementById(StartingDigits)
    var CtlLengthOption = document.getElementById(LengthOption)
    var CtlLengthValue = document.getElementById(LengthValue)
    var CtlDigitsToRemove = document.getElementById(DigitsToRemove)
    var CtlPatternExpression = document.getElementById(PatternExpression)

    CtlLengthValue.disabled = false;

    // check for valid input 
    // if not set to default values

    // starting digits
    if (!CtlStartingDigits.value.length == 0) {
        if (isNaN(CtlStartingDigits.value)) {
            CtlStartingDigits.value = '1'
        }
        else {
            CtlStartingDigits.value = CtlStartingDigits.value * 1
            if (CtlStartingDigits.value.indexOf(".") >= 0) {
                CtlStartingDigits.value = CtlStartingDigits.value.substring(0, CtlStartingDigits.value.indexOf("."))
            }
            if (CtlStartingDigits.value < 0) {
                CtlStartingDigits.value = '1'
            }
        }
    }

    // Length
    if (!CtlLengthValue.value.length == 0) {
        if (isNaN(CtlLengthValue.value)) {
            CtlLengthValue.value = '11'
        }
        else {
            CtlLengthValue.value = CtlLengthValue.value * 1
            if (CtlLengthValue.value.indexOf(".") >= 0) {
                CtlLengthValue.value = CtlLengthValue.value.substring(0, CtlLengthValue.value.indexOf("."))
            }
            if (CtlLengthValue.value < 0) {
                CtlLengthValue.value = '11'
            }
        }
    }
    else
        CtlLengthValue.value = '11';

    // Digits to remove
    if (!CtlDigitsToRemove.value.length == 0) {
        if (isNaN(CtlDigitsToRemove.value)) {
            CtlDigitsToRemove.value = '0'
        }
        else {
            CtlDigitsToRemove.value = CtlDigitsToRemove.value * 1
            if (CtlDigitsToRemove.value.indexOf(".") >= 0) {
                CtlDigitsToRemove.value = CtlDigitsToRemove.value.substring(0, CtlDigitsToRemove.value.indexOf("."))
            }
            if (CtlDigitsToRemove.value < 0) {
                CtlDigitsToRemove.value = '0'
            }
        }
    }
    else
        CtlDigitsToRemove.value = '0';

    // create the expression from input
    var expression = '^';
    var DigitsToRemove = parseInt(CtlDigitsToRemove.value);
    var StartingDigits = CtlStartingDigits.value;
    var StartingDigitsCount = CtlStartingDigits.value.length;
    var TotalLength = parseInt(CtlLengthValue.value);

    switch (parseInt(CtlLengthOption.options[CtlLengthOption.selectedIndex].value)) {
        case 0:     // At Least 
            if (DigitsToRemove <= StartingDigitsCount)
                expression += StartingDigits.substr(0, DigitsToRemove);
            else
                expression += StartingDigits + '\\d{' + parseInt(DigitsToRemove - StartingDigitsCount) + '}';

            expression += '(';

            if (DigitsToRemove < StartingDigitsCount)
                expression += StartingDigits.substr(DigitsToRemove, parseInt(StartingDigitsCount - DigitsToRemove));

            expression += '\\d{' + (TotalLength - Math.max(StartingDigitsCount, DigitsToRemove) - 1) + '}';
            expression += '\\d+)$';
            break;
        case 1:     // Exactly
            if (DigitsToRemove <= StartingDigitsCount)
                expression += StartingDigits.substr(0, DigitsToRemove);
            else
                expression += StartingDigits + '\\d{' + (DigitsToRemove - StartingDigitsCount) + '}';

            expression += '(';

            if (DigitsToRemove < StartingDigitsCount)
                expression += StartingDigits.substr(DigitsToRemove, (StartingDigitsCount - DigitsToRemove));

            expression += '\\d{' + (TotalLength - Math.max(StartingDigitsCount, DigitsToRemove)) + '}';
            expression += ')$';
            break;
        case 2:     // Any
            CtlLengthValue.disabled = true;
            if (DigitsToRemove <= StartingDigitsCount)
                expression += StartingDigits.substr(0, DigitsToRemove);
            else
                expression += StartingDigits + '\\d{' + (DigitsToRemove - StartingDigitsCount) + '}';

            expression += '(';

            if (DigitsToRemove <= StartingDigitsCount)
                expression += StartingDigits.substr(DigitsToRemove, (StartingDigitsCount - DigitsToRemove));
            //else
                //expression += '\d{' + (DigitsToRemove - StartingDigitsCount) + "}";

            expression += '\\d*)$';
            break;

    }

    CtlPatternExpression.value = expression;
    
}

function CreateTranslationRule(DigitsToAdd, TranslationRule) {
    var CtlDigitsToAdd = document.getElementById(DigitsToAdd)
    var CtlTranslationRule = document.getElementById(TranslationRule)

    CtlTranslationRule.value = CtlDigitsToAdd.value + '$1';
}

function SelectMe(RbSelectMe)
{
    var CtlRbSelectMe = document.getElementById(RbSelectMe)
    $('[id*=Rb_SelectMe]').attr('checked', false);
    CtlRbSelectMe.checked = true;
}

function SetDialPlanSimpleName(Name, SimpleName)
{
    var txtName = document.getElementById(Name);
    var txtSimpleName = document.getElementById(SimpleName);
    if (txtSimpleName.value == '' && txtName.value != '')
    {
        txtSimpleName.value = txtName.value.split(' ').join('_');
    }
}