﻿// JScript File
function doClear(obj) {
}

function submitSearchForm() {
    var searchTerm = "ctl00_Top1_SearchText";
    if (validateSearchForm(searchTerm)) {
        window.location = window.root + 'search_results.aspx?q=' + document.getElementById(searchTerm).value;
    }
    return false;
}

function confirmHcp() {
    var bln = confirm('This information is intended for U.S. Healthcare Professionals.\nIf you are a Healthcare Professional, click ok to continue.');
    if (bln) {
        window.location.href = window.root + 'hcp/default.aspx';
    }
}

function validateAge(source, args) {
    var yearObj = document.getElementById('ctl00_ContentPlaceHolder1_UserDateOfBirthYear');
    //yearObj.selectedValue
    var now = new Date();
    //alert(now.getFullYear() + "-" + yearObj.options[yearObj.selectedIndex].value);
    if (yearObj.selectedIndex == 0) {
        args.IsValid = true;
        return;
    }
    var dif = eval(now.getFullYear() + "-" + yearObj.options[yearObj.selectedIndex].value);
    args.IsValid = dif > 17;
}

function exitSite(destination) {
    openWin(window.root + 'exit_site.aspx?url=' + destination, 'pop_up', 'width=580,height=615,scrollbars=auto, resizable=1');
}
function interstitial(destination) {
    openWin(window.root + 'interstitial.aspx?url=' + destination, 'pop_up', 'width=500,height=215,scrollbars=auto');
}

function checkMaxLength(obj, length) {
    //    var objLength = document.getElementById('ctl00_ContentPlaceHolder1_'+obj).value.length;
    //    if (eval(objLength+1) > length)
    //        window.alert("You have entered " + eval(objLength + 1) + " character(s).\nValid entries are between 4 and " + length + " character(s) for password.");
}

function numberOfMonthsValidation(source, args) {
    if (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_1_5_1').checked == true) {
        if (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_S_1_1_G').selectedIndex == 0) {
            args.IsValid = false;
            return;
        } else {
            args.IsValid = true;
            return;
        }
    }
}

function currentUnitsValidation(source, args) {
    if (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_1_5_1').checked == true) {
        var obj = document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_T_1_2_1').value;
        if (obj.length < 1) {
            args.IsValid = false;
            return;
        } else {
            args.IsValid = true;
            return
        }
    }
}

function insulinValidation(source, args) {
    if ((document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_1_5_2').checked == true) ||
            (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_1_5_3').checked == true) ||
            (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_1_5_4').checked == true) ||
            (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_1_5_8').checked == true) ||
            (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_1_5_5').checked == true)) {

        if (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_S_1_6_G').selectedIndex == 0) {
            args.IsValid = false;
            return;
        } else {
            args.IsValid = true;
            return;
        }
    }
}

function oralRadioValidation(source, args) {
    if (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_1_5_6').checked == true) {
        if (CheckRadioVal(document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_R_1_9_G')) == -1) {
            args.IsValid = false;
            return;
        } else {
            args.IsValid = true;
            return;
        }
    }
}

function apidraRadioValidation(source, args) {

    if (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_1_5_2').checked == true) {
        if (CheckRadioVal(document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_R_1_10_G')) == -1) {
            args.IsValid = false;
            return;
        } else {
            args.IsValid = true;
            return;
        }
    }
}

function CheckRadioVal(objRef) {
    if (objRef) {
        if (objRef.length > 0) {
            for (i = 0; i < objRef.length; i++)
                if (objRef[i].checked) return i;
        } else {
            if (objRef.checked) return 0;
        }
    }
    return -1
}

function validateSurvey_q1(source, args) {
    args.IsValid = true;
    args.IsValid = check_answers(1, 2);
}

function validateSurvey_q2(source, args) {
    args.IsValid = true;
    args.IsValid = check_boxes(2, 5);
}

function validateSurvey_oral(source, args) {
    args.IsValid = true;
    if (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_3_2_1').checked == true) {
        args.IsValid = check_answers(3, 3);
    }
}

function validateSurvey_q4(source, args) {
    args.IsValid = true;
    args.IsValid = check_answers(4, 4);
}

function validateSurvey_q5(source, args) {
    args.IsValid = false;

    if (check_answers(5, 7) && check_answers(6, 7) && check_answers(7, 7) && check_answers(8, 7)) {
        args.IsValid = true;
    }

}

function validateGender(source, args) {
    args.IsValid = false;
    args.IsValid = (document.getElementById('ctl00_ContentPlaceHolder1_F').checked || document.getElementById('ctl00_ContentPlaceHolder1_M').checked);
}
/*
function showDiv(element, checkBox)
{    
var div;
var check;
check = document.getElementById(checkBox);
div = document.getElementById(element);    
if(check.checked)
{
div.style.display  = "block";
}else{
div.style.display  = "none";
} 
}
*/

/*************************************/
/* Start of Optional Validation area */
/*************************************/

// checks to see if oral is checked then calls check_answers
// makes the validation effective only if oral is checked
function OptionalOral_validate(source, args) {
    if (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_3_2_1').checked == true) {
        args.IsValid = check_answers(3, 3);
    }
    else {
        args.IsValid = true;
    }
}

// checks to see if apidra is checked then calls check_answers
// makes the validation effective only if oral is checked
function OptionalApidra_validate(source, args) {

    if (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_3_9_2').checked == true) {
        args.IsValid = check_answers(11, 5);
    }
    else {
        args.IsValid = true;
    }
}
// checks to see if lantus is checked then calls check_answers
// makes the validation effective only if oral is checked
function OptionalLantus_validate(source, args) {
    if (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_3_9_1').checked == true) {
        args.IsValid = check_answers(10, 5);
    }
    else {
        args.IsValid = true;
    }
}

function Validatelantus(source, args) {
    if (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_3_9_1').checked == true) {
        args.IsValid = check_answers(10, 5);
        return;
    }
    args.IsValid = true;
}
function Validatelantus1(source, args) {
    if (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_3_9_1').checked == true) {
        args.IsValid = check_answers(15, 2);
        return;
    }
    args.IsValid = true;
}
function Validatelantus2(source, args) {
    if (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_3_9_1').checked == true) {
        args.IsValid = check_answers(16, 2);
        return;
    }
    args.IsValid = true;
}
// checks to see if Insulin is checked then calls check_boxes
// makes the validation effective only if Insulin is checked
function OptionalInsulin_validate(source, args) {

    if (document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_3_2_2').checked == true) {
        args.IsValid = document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_3_9_1').checked || document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_C_3_9_3').checked;
    }
    else {
        args.IsValid = true;
    }
}
// Cycles through radio buttons on Questionaire 3 givin the question number
// returns True if a radio button in the question is checked, false otherwise
function check_answers(index, limit) {
    var valid = false;
    for (var j = 1; j <= limit; j++) {
        valid = (valid || document.getElementById("ctl00_ContentPlaceHolder1_UserResponse_R_3_" + index + "_" + j).checked);
    }
    return valid;
}

// Cycles through Check boxes on Questionaire 3 givin the question number
// returns True if a Check box in the question is checked, false otherwise
function check_boxes(index, limit) {
    var valid = false;
    for (var j = 1; j <= limit; j++) {
        try {
            valid = (valid || document.getElementById("ctl00_ContentPlaceHolder1_UserResponse_C_3_" + index + "_" + j).checked);
        }
        catch (err)
        { }
    }
    return valid;
}


// Cycles through Check boxes on Questionaire 3 givin the question number
// clears any checked radio buttons 
function clear_answers(index, limit) {
    //alert("index" + index);
    //alert("limit" + limit);
    var valid = false;

    for (var j = 1; j <= limit; j++) {
        try {
            document.getElementById("ctl00_ContentPlaceHolder1_UserResponse_R_3_" + index + "_" + j).checked = false;

        }
        catch (err)
        { }
    }
    return;
}

// Cycles through Check boxes on Questionaire 3 givin the question number
// clears any checked check boxes
function clear_boxes(index, limit) {
    var valid = false;
    for (var j = 1; j <= limit; j++) {
        try {
            document.getElementById("ctl00_ContentPlaceHolder1_UserResponse_C_3_" + index + "_" + j).checked = false;
        }
        catch (err)
        { }
    }
    return;
}
/*
function showDiv(element, checkBox)
{    
var div;
var check;
check = document.getElementById(checkBox);
div = document.getElementById(element);    
if(check.checked)
{
div.style.display  = "block";
}else{
div.style.display  = "none";
} 
}
*/


// givin the div name that contains the children
// and the check box id (client id)
// it will clear all the children before hiding the div.
function showDivForOptional(divId, boxId) {
    if (!document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_R_3_9_1').checked && !document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_R_3_9_2').checked) {
        clear_answers(10, 5);
        clear_answers(11, 2);
        document.getElementById(divId).style.display = 'none';
    }
    else {
        document.getElementById(divId).style.display = 'block';
    
    }


}

function showDivForOptional(divId, boxId) {
    if (!document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_R_3_9_1').checked && !document.getElementById('ctl00_ContentPlaceHolder1_UserResponse_R_3_9_2').checked) {
        clear_answers(10, 5);
        clear_answers(11, 2);
        document.getElementById(divId).style.display = 'none';
    }
    else {
        document.getElementById(divId).style.display = 'block';

    }

}

function hideDiv() {
    document.getElementById('oral').style.display = 'none';
    document.getElementById('insulin').style.display = 'none';
    document.getElementById('additionalQuestions').style.display = 'none';
}

function showDiv(divId, boxId) {
    if (!document.getElementById(boxId).checked) {
        switch (boxId) {
            // oral medication
            case 'ctl00_ContentPlaceHolder1_UserResponse_C_3_2_1':
            clear_boxes(3, 2);
                break;

            // insulin      
            case 'ctl00_ContentPlaceHolder1_UserResponse_C_3_2_2':
                clear_answers(10, 5);
                clear_answers(9, 3);
                clear_answers(11, 2);

//            case 'ctl00_ContentPlaceHolder1_UserResponse_R_3_9_1':
//                clear_answers(10, 5);
//                clear_answers(11, 2);

//            case 'ctl00_ContentPlaceHolder1_UserResponse_R_3_9_2':
//                clear_answers(10, 5);
//                clear_answers(11, 2);
                
                //clear_answers(11,5);
                //document.getElementById('lantus').style.display = 'none';
                //document.getElementById('lantus1').style.display = 'none';
                //document.getElementById('apidra').style.display = 'none';
                //clear_boxes(9,3);
                //document.getElementById("ctl00_ContentPlaceHolder1_UserResponse_C_3_9_1").checked = false;
                //document.getElementById("ctl00_ContentPlaceHolder1_UserResponse_C_3_9_3").checked = false;
                break;

            // lantus  
            case 'ctl00_ContentPlaceHolder1_UserResponse_C_3_9_1':
                clear_answers(10, 5);
                clear_answers(15, 2);
                clear_answers(16, 2);
                break;

            // apidra      
            /*case 'ctl00_ContentPlaceHolder1_UserResponse_C_3_9_2' :  
            clear_answers(11,5); 
            break;
            */ 
            // other  
            case 'ctl00_ContentPlaceHolder1_UserResponse_C_3_9_3':
                break;
        }
        document.getElementById(divId).style.display = 'none';
    }
    else {
        document.getElementById(divId).style.display = 'block';
    }
    return;
}


function hideOptional(state) {
    if (state == "block") {
        document.getElementById('oral').style.display = 'block';
        document.getElementById('lantus').style.display = 'block';
        document.getElementById('lantus1').style.display = 'block';
        document.getElementById('insulin').style.display = 'block';
    } else {
        document.getElementById('oral').style.display = 'none';
        document.getElementById('lantus').style.display = 'none';
        document.getElementById('lantus1').style.display = 'none';
        document.getElementById('insulin').style.display = 'none';
    }
}

function passwordvalidator(source, args) {
    args.IsValid = false;

    var pass = document.getElementById('ctl00_ContentPlaceHolder1_UserPassword');
    var passr = document.getElementById('ctl00_ContentPlaceHolder1_UserPasswordRepeat');
    if (pass.value.length > 3 && passr.value.length > 3) {
        args.IsValid = true;
    }
    return;
}

function certify(source, args) {
    args.IsValid = false;
    if (document.getElementById("ctl00_ContentPlaceHolder1_UserResponse_C_20_5_1").checked = true) {
        args.IsValid = true;
    }
}

