﻿// JScript File
function HideRadioButton()
{
    //alert('starting');
    var i = 0;
    var max = 0;
    if (MyRows)
    {
        max = MyRows.length;
    }
    while (i < max)
    {
        var Row = MyRows[i];
        if (Row)
        {
            //alert('row ' + i);
            var x = 0;
            while (x < Row.requiredControls.length)
            {
                var cid = Row.requiredControls[x];
                if (cid.length > 0)
                {
                    //alert(cid);
                    var req = document.getElementById(cid);
                    if (req)
                    {
                        HideRadio(req);
                    }
                }
                x++;
            }
        }
        i++;
    }
    //alert('hiding done');
}
function ClientValidateEachRow()
{
    //alert('starting.. with validation as: ' + Page_ValidationActive);
    //Page_IsValid = ValidatorOnSubmit();
    var allValid = true;
    if (Page_IsValid == true)
    {
        //alert('validating');
        var i = 0;
        var max = 0;
        if (MyRows)
        {
            max = MyRows.length;
        }
        while (i < max)
        {
            var Row = MyRows[i];
            if (Row)
            {
                var control = document.getElementById(Row.controlToValidate);
                if (control.value.length > 0)
                {
                    var reqid = 'req_warning_' + control.id;
                    //alert(reqid);
                    var reqWarningLabel = document.getElementById(reqid);
                    if (reqWarningLabel)
                    {
                        control.parentNode.removeChild(reqWarningLabel);
                    }
                    var x = 0;
                    //alert('fields ' + Row.requiredControls.length);
                    while (x < Row.requiredControls.length)
                    {
                        var cid = Row.requiredControls[x];
                        var Con = document.getElementById(cid);
                        //alert(Con); alert('Disabled:'+Con.disabled);
                        if (cid.length > 0 && Con!=null && !Con.disabled)
                        {
                            //alert(cid);
                            var req = document.getElementById(cid);
                            if (req)
                            {
                                //alert('req \n' + req.type);
                                var id = 'well_warning_' + i + '_row_' + x;
                                var warningLabel = document.getElementById(id);
                                if (HasValue(req))
                                {
                                    if (warningLabel)
                                    {
                                        req.parentNode.removeChild(warningLabel);
                                    }
                                }
                                else
                                {
                                    //alert('field is empty \n' + cid);
                                    if (control.value > 0)
                                    {
                                        allValid = false;
                                        if (!warningLabel)
                                        {
                                            //alert('warning label not found for \n\n' + cid);
                                            var warningNode = document.createElement('span');
                                            warningNode.id = id;
                                            warningNode.innerHTML = '<font color="RED">* Required</font>';
                                            req.parentNode.appendChild(warningNode);
                                        }
                                    }
                                    else
                                    {
                                        if (warningLabel)
                                        {
                                            req.parentNode.removeChild(warningLabel);
                                        }
                                    }
                                }
                            }
                        }
                        x++;
                    }
                }
                else
                {
                    /*
                    for (var cI = 0; cI < Page_Validators.length; cI++)
                    {
                        if (control.id == Page_Validators[cI].controltovalidate)
                        {
                            //alert(Page_Validators[cI].evaluationfunction == RequiredFieldValidatorEvaluateIsValid);
                            //alert(Page_Validators[cI].errormessage);
                            //alert(Page_Validators[cI].evaluationfunction);
                            
                            if (Page_Validators[cI].evaluationfunction == RequiredFieldValidatorEvaluateIsValid)
                            {
                                var reqid = 'req_warning_' + control.id;
                                var reqWarningLabel = document.getElementById(reqid);
                                if (!warningLabel)
                                {
                                    var warningNode = document.createElement('span');
                                    warningNode.id = reqid;
                                    warningNode.innerHTML = '<font color="RED">'+ Page_Validators[cI].errormessage +'</font>';
                                    control.parentNode.appendChild(warningNode);
                                }
                            }
                            
                        }
                    }
                    */
                    //alert('Missing Value!');
                    allValid = false;
                }
            }
            i++;
        }
        //alert('all done');
        if (Page_IsValid == true)
        {
            return allValid;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;        
    }
    //}
    /*else
    {
        alert('Page is not valid');
        return false;
    }*/
}
function HideRadio(ele)
{
    //alert('id ' + ele.id);
    var vFld = document.getElementById(ele.id + "_0");
    if (vFld)
    {
        if (vFld.checked)
        {
            var val = vFld.value;
            if (val)
            {
                //alert('value is ' + vFld.value);
            }
            else
            {
                //alert('hiding the parent node');
                vFld.parentNode.style.display = 'none';
            }
        }
    }
}
function HasValue(ele)
{
    var hasValue = false;
    if (ele.value && ele.value.length > 0)
    {
        return true;
    }
    else
    {
        var found = false;
        var cDone = false;
        if (typeof(tlrkComboBoxes) == "undefined")
        {
            cDone = true;
        }
        for (var cI = 0; !cDone; cI++)
        {
            if (tlrkComboBoxes[cI])
            {
                //alert('combo exists');
                if (tlrkComboBoxes[cI].ClientID == ele.id)
                {
                    found = true;
                    var val = tlrkComboBoxes[cI].GetText();
                    //alert(val);
                    if (val && val.length > 0)
                    {
                        hasValue = true;
                    }
                    else
                    {
                        hasValue = false;
                    }
                }
            }
            else
            {
                cDone = true;
            }
        }
        if (!found)
        {
            var vDone = false;
            for (var vI = 0; !vDone; vI++)
            {
                var vFld = document.getElementById(ele.id + "_" + vI);
                if (vFld)
                {
                    if (vFld.checked && vFld.value.length > 0)
                    {
                        hasValue = true;
                        vDone = true;
                    }
                }
                else
                {
                    vDone = true;
                }
            }
        }
        return hasValue;
    }
}

