
function IsAngleBracket(value) { return (value.indexOf('<') > -1 || value.indexOf('>') > -1) ? true : false; }

// validate the sign up / update signup (Employment form)
function validateSignUpForm(form, mode, rcbOrganization, rcbSupervisor) 
{
    var resultForm = ValidateForm(form, mode);

    var resultSupervisor = validateSupervisor(rcbSupervisor);

    var resultOrganization = validateOrganization(rcbOrganization);

    var validateLicense = ValidateLicenseNumber();

    var validateRole = ValidateRole();

    return resultForm && resultSupervisor && resultOrganization && validateLicense && validateRole;
}


    // validate the orgainzaition selection
function validateOrganization(rcbOrganization) 
{
    var org_sel = true;
    var orgname = document.getElementById(rcbOrganization + "_Input");

    if (orgname != null && !orgname.disabled) {

        var errormsg = document.getElementById("errRcbOrganization");
        var txt
        if (orgname.value == '' || orgname.value == '--- PLEASE TYPE YOUR ORGANIZATION ---' || orgname.value == 'Organization not found. Please type again') 
        {
            errormsg.style.color = 'red';
            errormsg.style.fontSize = '11px';
            errormsg.style.fontFamily = 'arial';

            if (errormsg.style.display != "block") 
            {
                errormsg.innerHTML += "<br/>";
            }

            errormsg.style.display = "block";
            org_sel = false;
            if (focusControl) {
                if (focusControl.sourceIndex > orgname.sourceIndex) {
                    orgname.focus();
                }
            }
            else {
                orgname.focus();
            }
        }
        else 
        {
            errormsg.style.display = "none";
        }
    }
    return org_sel;
}
    
     
     // validate the supervisor selection
function validateSupervisor(rcbSupervisor) 
{
    var sup_sel = true;
    var supname = document.getElementById(rcbSupervisor + "_Input");

    if (supname != null && !supname.disabled) 
    {
        var errormsg = document.getElementById("errRcbSupervisor");

        if (supname.value == '' || supname.value == '--- PLEASE ENTER SUPERVISOR LAST NAME ---' || supname.value == 'Supervisor not found. Please type again') 
        {
            errormsg.style.color = 'red';
            errormsg.style.fontSize = '11px';
            errormsg.style.fontFamily = 'arial';

            if (errormsg.style.display != "block") 
            {
                errormsg.innerHTML += "<br/>";
            }
            errormsg.style.display = "block";
            sup_sel = false;
            if (focusControl) {
                if (focusControl.sourceIndex > supname.sourceIndex) {
                    supname.focus();
                }
            }
            else {
                supname.focus();
            }            
        } else 
        {
            errormsg.style.display = "none";
        }
    }
    return sup_sel;
}
    
    
// check wheather any radiobutton is selected in given roleType table
function checkRole()
{
    var roleType = document.getElementById('roleType');
    var inputTags = roleType.getElementsByTagName("input");
    var tagsLength = inputTags.length;

    for (var i = 0; i < tagsLength; i++) 
    {
        if (inputTags[i].checked) 
        {
            return true;
        }
    }
    alert('Please Select Your Role!');
    return false;
}

function ValidateSignInForm(frm, mode, errorControl) 
{
    if (errorControl != undefined)
    {
        errorControl.style.visibility = "hidden";
        errorControl.innerHTML = '';
    }

    var focusSignIn = null;
    memberId = document.getElementById(GetClientId("txtMemberId"));
    password = document.getElementById(GetClientId("txtPassword"));

    result = true;

    // check user name
    if (memberId.value == '') 
    {
        errorControl.innerHTML = 'Please enter User ID';
        if (focusSignIn == null) { focusSignIn = memberId }
        result = false;
    }
    else if (IsAngleBracket(memberId.value)) 
    {
        errorControl.innerHTML = "<br/>Angle brackets (< >) are not allowed";
        if (focusSignIn == null) { focusSignIn = memberId }
        result = false;
    }

    // check password
    if (password.value == '') 
    {
        errorControl.innerHTML += '<br/>Please enter Password';
        if (focusSignIn == null) { focusSignIn = password }
        result = false;
    }
    else if (IsAngleBracket(password.value)) 
    {
        errorControl.innerHTML += '<br/>Angle brackets (< >) are not allowed';
        if (focusSignIn == null) { focusSignIn = password }
        result = false;
    }

    if (!result) 
    {
        if (errorControl != undefined) 
        {
            errorControl.style.visibility = 'visible';
            focusSignIn.focus();
        }
    }
    return result;
}


function clearLicenseNumber(txt) {
    if (txt.value.trim() == "License#")
        txt.value = "";
}

function ShowLicenseNumber(cbMember) {
    var txtLicenseNumber = document.getElementById((cbMember.id).replace(/cbLicenseType/gi, "txtLicenseNumber"));

    if (cbMember.checked) {
        txtLicenseNumber.style.display = "block";

        if (txtLicenseNumber.value == "") {
            txtLicenseNumber.value = "License#";
        }
    }
    else if (!cbMember.checked) {
        txtLicenseNumber.style.display = "none";
    }
}

function ValidateRole() {
    var errTxtOther = document.getElementById(GetClientId("errTxtOther"));
    var txtOther = document.getElementById(GetClientId("txtOther"));
    var rbOther = document.getElementById(GetClientId("rbOther"));
    var result = true;

    if (rbOther) {
        if (rbOther.checked) {
            errTxtOther.innerHTML = "";
            if (txtOther.value.trim() == "") {
                errTxtOther.innerHTML = "Please enter role";
                result = false;
            }
            else if (IsAngleBracket(txtOther.value.trim())) {
                errTxtOther.innerHTML = "Please enter a valid role. Angle brackets ( < > ) are not allowed";
                if (focusControl.sourceIndex > txtOther.sourceIndex) {
                    txtOther.focus();
                }
                result = false;
            }
        }
    }
    return result;
}

function ValidateLicenseNumber() {
    var gvLicenseTypeBehaviour = document.getElementById(GetClientId("gvLicenseTypeBehaviour"));
    var gvLicenseTypeHealing = document.getElementById(GetClientId("gvLicenseTypeHealing"));
    var gvLicenseTypeNursing = document.getElementById(GetClientId("gvLicenseTypeNursing"));
    var txtLicenseTypeOther = document.getElementById(GetClientId("txtLicenseTypeOther"));
    var errLicenseTypeOther = document.getElementById(GetClientId("errLicenseTypeOther"));
    var errTxtLicenseNumberOther = document.getElementById(GetClientId("errTxtLicenseNumberOther"));
    var errTxtLicenseTypeOther = document.getElementById(GetClientId("errTxtLicenseTypeOther"));  
    var txtLicenseNumberOther = document.getElementById(GetClientId("txtLicenseNumberOther"));
    var licenseTypeError = document.getElementById("licenseTypeError");
    var result = false;
    var index = 2;
    var cbType = null;

    if (errLicenseTypeOther && errTxtLicenseNumberOther && errTxtLicenseTypeOther) {
        errTxtLicenseNumberOther.innerHTML = "";
        errTxtLicenseTypeOther.innerHTML = "";
        errLicenseTypeOther.innerHTML = "";
    }
    if (gvLicenseTypeBehaviour) {
        for (index; index <= gvLicenseTypeBehaviour.rows.length; index++) {
            cbType = document.getElementById(gvLicenseTypeBehaviour.id + "_ctl" + Math.floor(index / 10) + Math.floor(index % 10) + "_cbLicenseType");
            if (cbType.checked) result = true;
        }
    }

    if (gvLicenseTypeHealing) {
        index = 2;
        for (index; index <= gvLicenseTypeHealing.rows.length; index++) {
            cbType = document.getElementById(gvLicenseTypeHealing.id + "_ctl" + Math.floor(index / 10) + Math.floor(index % 10) + "_cbLicenseType");
            if (cbType.checked) result = true;
        }
    }

    if (gvLicenseTypeNursing) {
        index = 2;
        for (index; index <= gvLicenseTypeNursing.rows.length; index++) {
            cbType = document.getElementById(gvLicenseTypeNursing.id + "_ctl" + Math.floor(index / 10) + Math.floor(index % 10) + "_cbLicenseType");
            if (cbType.checked) result = true;
        }
    }
   
    if (txtLicenseTypeOther && txtLicenseNumberOther) {
        if (!result && txtLicenseTypeOther.value.trim() == "") {
            errLicenseTypeOther.innerHTML = "Please select a license type below or type in as Other License";
            if (focusControl) {
                if (focusControl.sourceIndex > licenseTypeError.sourceIndex) {
                    document.location.href = "#licenseTypeError";
                }
            }
            else {
                document.location.href = "#licenseTypeError";
            }
            result = false;
        }
        else if (result && txtLicenseTypeOther.value.trim() == "") {
            if (txtLicenseNumberOther.value.trim() == "License#") {
                result = true;
            }
            else if (txtLicenseNumberOther.value.trim() == "") {
                result = true;
            }
            else {
                if (focusControl) {
                    if (focusControl.sourceIndex > licenseTypeError.sourceIndex) {
                        document.location.href = "#licenseTypeError";
                    }
                }
                else {
                    document.location.href = "#licenseTypeError";
                }
                errLicenseTypeOther.innerHTML = "Please select a license type below or type in as Other License";
                document.location.href = "#licenseTypeError";
                result = false;
            }
        }
        else if (IsAngleBracket(txtLicenseNumberOther.value.trim())) {
            errTxtLicenseNumberOther.innerHTML = "<> are not allowed";
            if (focusControl.sourceIndex > licenseTypeError.sourceIndex) {
                document.location.href = "#licenseTypeError";
            }
            result = false;
        }
        else if (IsAngleBracket(txtLicenseTypeOther.value.trim())) {
            errTxtLicenseTypeOther.innerHTML = "Please enter a valid License. Angle brackets ( < > ) are not allowed";
            if (focusControl.sourceIndex > licenseTypeError.sourceIndex) {
                document.location.href = "#licenseTypeError";
            }
            result = false;
        }
        else {            
            result = true;
        }
    }
    else {
        result = true;
    }

    return result;
}
