var DEF_NOT_ANSWERED        = 'Not Answered' ;
var DEF_NONE_OF_THE_ABOVE   = 'none of the above' ;

/**
    not used in askRules
*/
function showQ(user) {
    try {
        if (user.style) {
            user.style.visibility = "visible";
            user.style.display = "block";
        }
    } catch (ex) { /*mask it*/ }
}

/**
    not used in askRules
*/
function hideQ(user)
{
    try {
        if( user.style ) {
            user.style.visibility = "hidden";
            user.style.display="none";
        }
    } catch( ex ) { /*mask it*/ }
}

/**
    Used in askRules.

    used for checkboxes set

    radios  - array of checkboxes or single instance
    value   - single value
    return  - true if any selected checkbox has this value

    examples:
        contains({past_rx_category_and_meds}, 'd56446')
        contains({current_meds},'d0788')
*/
function contains(radios, value)
{
    if( radios == undefined || radios == null ) {
        return false ;
    }


    if( radios.length > 0 ) {
        // It's array
        for( var i = 0; i < radios.length; i++ ) {
            if( radios[i] != null && radios[i].checked && radios[i].value == value ) {
                return true ;
            }
        }

        return false ;
    }

    if( radios.checked && radios.value == value ) {
        // It's a single (string/number) value
        return true ;
    }



    var drugList = null ;
    if( radios.value != null && radios.value.indexOf( "=" ) != -1 ) {
        // It's hidden drug list element
        drugList = radios.value ;
    }




    //
    // implicitly assume drug/sx list in format like this:
    //    ",12101=d00170:aspirin,12506=d00824:opium,12771=d01306:cocaine topical"
    if( drugList != null ) {

        var drugNames = radios.value.substring( drugList.indexOf( "=" ) + 1 ) ;
        var drugId ;
        do {
            var index = drugNames.indexOf( ":" ) ;
            if( index != -1 ) {
                drugId = drugNames.substring( 0, index ) ;
            }

            if( drugId.toLowerCase() == value.toLowerCase() ) {
                //alert(drugId + " vs." + value);
                return true ;
            }

            index = drugNames.indexOf( "," ) ;
            if( index == -1 ) {
                break ;
            }

            drugNames = drugNames.substring( index + 1 ) ;
            index = drugNames.indexOf( "=" ) ;
            if( index != -1 ) {
                drugNames=drugNames.substring( index + 1 ) ;
            }
        }
        while( drugNames.length > 0 ) ;

    }

    return false ;
}

/**
    Used in askRules.

    check for "blank" or undefined value

    value   - html input element or string

    examples:
        ! isEmpty({which_meds})
        ! isEmpty({which_menopause_rx})
*/
function isEmpty(item)
{
    if( item == undefined || item == null ) {
        return true ;
    }

    if( isString( item ) && (item == '' || item == DEF_NOT_ANSWERED ) ) {
        return true ;
    }

    return item.value == null || (item.value == '' || item.value == DEF_NOT_ANSWERED) ;
}

/**
    Used in askRules.

    convert string value to number

    examples:
        toInt({age})  == 0
        toInt({condition_duration}) < 22
*/
function toInt(value)
{
    return parseFloat(value);
}


/**
    Used in askRules.

    example:
        toBMI({height} , {weight}) > 24.9
*/
function toBMI(height, weight)
{
    // this BMI calculation is for adults only
    if ((height < 40) || (weight < 50))
    	return '';

    return (  ( weight /(height * height )) * 703 );
}

/**
    Used in askRules.

    field - array of checkboxes or single checkbox

    nb: ignore fields with 'none of the above' value

    examples:
        anySelected({condition_overview})
        anySelected({menopause_rx})

*/
function anySelected(field)
{
    if( field == undefined || field == null ) {
        return false ;
    }

    //alert("anySelected called");
    for( i = 0 ; i < field.length ; i++ ) {
        if( field[i].checked ) {
            var v1= field[i].value.toLowerCase() ;

            if( v1.indexOf( DEF_NONE_OF_THE_ABOVE ) == -1 ) {
                return true ;
            }
        }
    }

    //single one
    // Or a single value
    if( field.checked ) {

        var v1= field.value.toLowerCase() ;

        if( v1.indexOf(DEF_NONE_OF_THE_ABOVE) == -1 ) {
            return true ;
        }
    }
    //alert(ret);
    return false ;

}

/**
    Not used in askRules.

    Used by SurveyEntity for generated js to get radiobutton value

    radio - radio button or array of buttons

    example:
      if (qAnswerStyle.equals("radio") == true) {
          // the html radio button acts more like a multibox
          // so we actually have to right some code to actually get the value as a
          // string from the radio button html form object
          jsDataName = "getRadioValue(document.forms['questionForm'].elements['value("+qe.getAnswerDataName()+")'])";
      }

*/
function getRadioValue(radio)
{
//alert(radio);
    if( radio == undefined || radio == null ) {
        return '' ;
    }

    // Can be an array
    for( i = 0 ; i < radio.length ; i++ ) {
        if( radio[i].checked ) {
            //alert(radio[i].value);
            return radio[i].value ;
        }
    }



    // Or a single value
    if( radio.checked ) {
        return radio.value ;
    }

    return '' ;
}


/**
    Not used in askRules.

    Used by SurveyEntity for generated js to get value

    field - <input> element

    example:
      else {
          jsDataName = "getSelectValue(document.forms['questionForm'].elements['value("+qe.getAnswerDataName()+")'])";
      }
*/
function getSelectValue(field)
{

    if( field == undefined || field == null ) {
        return null ;
    }


    var value = null ;
    // Can be array
    if( field.length > 0 ) {
        for( var i = 0 ; i < field.length ; i++ ) {
            if( field[i].type != "hidden" ) {
                value = field[i].value ;
                break ;
            }
        }
    }
    else {
        value = field.value ;
    }

    return value ;
}



/**
    Not used in askRules.

    used by jsp to generate numeric field validator
*/
function checkIntegerInRange (theField, s, emptyOK,a,b)
{
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isIntegerInRange (theField.value, a, b)){
    	return true;
    }else{

    	return warnInvalid (theField, "Your entry must be from "+a+" to "+b);
    }
}

/**
    Not used in askRules.

    no external references were found (at 05/28/08 15:53)
*/
function warnInvalid (theField, s)
{   theField.focus()
//    theField.select()
    theField.value="";
    alert(s)
    return false
}


/**
    Not used in askRules.

    no external references were found (at 05/28/08 15:53)
*/
function isIntegerInRange (s, a, b)
{   if (isEmpty(s))
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    if (!isInteger(s, false)) return false;

    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}


/**
    Not used in askRules.

    used by Questions.jsp
*/
function radioCheck(radios, value)
{
    if( radio == undefined || radio == null ) {
        return false ;
    }

    // Can be array
    for( i = 0 ; i < radios.length ; i++ ) {
        if( radios[i].checked && radios[i].value == value ) {
            return true ;
        }
    }

    // Or single value
    if( radios.checked && radios.value == value ) {
        return true ;
    }

    return false ;
}

/**
    Not used in askRules.

    used by Questions.jsp
*/
function selectCheck(el,value)
{
    return (el.value == value);
}


/**
    Not used in askRules.

    Used by SurveyEntity for generated js
*/
function clearRadio(field)
{
    clearFields(field);
}


/**
    Not used in askRules.
*/
function clearMulti(field)
{
    clearFields(field);
}

/**
    Not used in askRules.
*/
function setMulti(field)
{
    setFields(field, true)
}


/**
    Not used in askRules.

    no external references were found (at 05/28/08 15:53)
*/
function clearFields(field)
{
    setFields(field, false)
}


/**
    Not used in askRules.

    no external references were found (at 05/28/08 15:53)
*/
function setFields(field, boolvalue)
{
    if( field == undefined || field == null ) {
        return ;
    }

    // Can be array
    for( var i = 0; i < field.length ; i++ ) {
        var v1= field[i].value.toLowerCase() ;

        if( v1.indexOf(DEF_NONE_OF_THE_ABOVE) == -1 ) {
            field[i].checked = boolvalue;
        }
        else{
            if( boolvalue == true ) {
                field[i].checked = false ;
            }
        }

    }

    // Or Single value
    if( field.checked != null ) {
        field.checked = boolvalue ;
    }
}

/**
    Not used in askRules.
*/
function clearNone(multiList)
{
    if( multiList == undefined || multiList == null ) {
        return ;
    }

    // Look for None or Dont Know
    for( var i = 0 ; i < multiList.length ; i++ ) {
        var v1 = multiList[i].value.toLowerCase();
        //alert(v1);
        //alert(v1.indexOf(DEF_NONE_OF_THE_ABOVE));
        if( v1.indexOf( DEF_NONE_OF_THE_ABOVE ) == 0 ) {
            multiList[i].checked = false;
        }
    }
}


/**
    Not used in askRules.

    Used by SurveyEntity for generated js
*/
function clearSelect(field) {
    if (field != undefined && field != null) {
        field.value = "";
    }
}


function isString(a)
{
  return typeof a == 'string';
}
