// JavaScript Document
if (document.getElementById('surveyHereDiv')) {
    var allInputs = document.getElementById('surveyHereDiv').getElementsByTagName('input');
    var inputSets = {};
    var otherInput = [];

    for(var i=0;i<allInputs.length;i++)
    {
        if (allInputs[i].type == 'radio' || allInputs[i].type == 'checkbox')
        {
            if (allInputs[i].name != '' && !inputSets[allInputs[i].name] && !allInputs[i].name.match(/_other$/))
            {
                inputSets[allInputs[i].name] = [];
            }
            if (allInputs[i].id != '' && !allInputs[i].name.match(/_other$/))
            {
                inputSets[allInputs[i].name].push(allInputs[i].id);
            }
        }
    }
    var allTextarea = document.getElementById('surveyHereDiv').getElementsByTagName('textarea');
    for(var i=0;i<allTextarea.length;i++)
    {
        if (allTextarea[i].name.match(/_other$/))
        {
            otherInput.push(allTextarea[i].name.replace(/_other$/,''));
        }
    }
}
function checkChecked()
{
    var aStr = '';
    // testing
    var thatTest = true;
    var thisTest = false;
    var checkThis = [];
    var allChecked = {};
    var otherWarned = false;
    var otherTest = {};
    for (var i in inputSets)
    {
        thisTest = false;
        // aStr += i + ' ' + inputSets[i] + '\n';
        checkThis = inputSets[i];
        if (checkThis.length > 0)
        {
            for(var j=0;j<checkThis.length;j++)
            {
                if (document.getElementById(checkThis[j]).type == 'radio' || document.getElementById(checkThis[j]).type == 'checkbox')
                {
                    if (document.getElementById(checkThis[j]).checked)
                    {
                        thisTest = true;
                    }
                }
            }
        }
        allChecked[i] = thisTest;
    }
    thisTest = true;
    for(var i in allChecked)
    {
        // alert('checked: ' + i + ' ' + allChecked[i]);
        if (allChecked[i] == false)
        {
            thisTest = false;
            break;
        }
    }
    thatTest = thisTest;
    if (thatTest)
    {
        if (otherInput.length > 0)
        {
            thisTest = false;
            // aStr += '\nbreak\n\n';
            for (var i=0;i<otherInput.length;i++)
            {
                // aStr += i + ' ' + otherInput[i] + '\n';
                // aStr += otherInput[i] + ' ' + document.getElementById(otherInput[i]).checked + ' ' + document.getElementById(otherInput[i] + '_other').value + '\n';
                if (document.getElementById(otherInput[i]).checked && document.getElementById(otherInput[i] + '_other').value == '')
                {
                    otherTest[otherInput[i]] = false;
                    if (otherWarned == false)
                    {
                        aStr += 'Please tell us more in the box(es) with the red triangle.\n';
                        otherWarned = true;
                    }
                }
            }
        }
    }
    else
    {
        aStr += 'Please choose at least one answer for each question.\n';
    }
    if (thatTest)
    {
        thisTest = true;
        for(var i in otherTest)
        {
            if (otherTest[i] == false)
            {
                thisTest = false;
                break;
            }
        }
        thatTest = thisTest;
    }
    if (thatTest == false)
    {
        alert(aStr);
    }
    else
    {
        // pretend it worked
        // alert('rock on');
        document.surveyForm.submit();
    }
}
function radioClick(thisRadio)
{
    var tRadio = thisRadio.split('_');
    var myQuestion = tRadio[0] + '_' + tRadio[1];
    var tQ;
    for(var i=0; i<otherInput.length; i++)
    {
        tQ = otherInput[i].split('_');
        if (tQ[0] == tRadio[0] && tQ[1] == tRadio[1])
        {
            document.getElementById(otherInput[i]+'_other').disabled='disabled';
            document.getElementById(otherInput[i]+'_other').style.display='none';
        }
    }
    if (document.getElementById(thisRadio+'_other'))
    {
        document.getElementById(thisRadio+'_other').disabled='';
        document.getElementById(thisRadio+'_other').style.display='block';
        document.getElementById(thisRadio+'_other').focus();
    }
}

