
/*
 * Get a form element
 * Reusable
 */
function getElement( _form, _element ) {
    return document.forms[_form].elements[_element];
}

/*
 * Return the length.
 * This is intended to be used with a select box - no check
 * Reusable
 */
function getLength( _element ) {
    return _element.options.length;
}

/*
 * Return the selected index.
 * This is intended to be used with a select box - no check
 * Reusable
 */
function getSelectedIndex( _element ) {
    return _element.selectedIndex;
}

/*
 * Clear a box.
 * This is intended to be used with a select box - no check
 * Reusable
 */
function clearElement( _element ) {
    _element.options.length = 0;
    return true;
    
    /*
        // Another way of doing it
        while( _element.options.length > 0 ) {
            _element.options[1] = null;
        }
    */
}

/*
 * Populate our question box with questions.
 * eTAMS specific
 */
function loadQuestions( _element ) {   
    // Clear the box
    clearElement( _element );
    
    // Fill it with our questions
    for( i = 0; i < data.length; i++ ) {
        _element.options[i] = new Option( data[i].nm, data[i].i );
        if( data[i].i == '-1' ) {
            _element.options[i].className = 'header';
        }
    }
    
    return true;
}

/*
 * Populate the second box, with the responses to
 * what was clicked on in the first box
 * eTAMS specific
 */
function loadResponses( _src, _dst, _fname ) {
    
    // Clear the second box
    clearElement( _dst );
    
    // Clear the backed up filter information
    if( document.forms[_fname].filter_second ) {
        document.forms[_fname].filter_second.bak = 'undefined';
    }
    if( document.forms[_fname].downaxis_second ) {
        document.forms[_fname].downaxis_second.bak = 'undefined';
    }
    if( document.forms[_fname].acrossaxis_second ) {
        document.forms[_fname].acrossaxis_second.bak = 'undefined';
    }
    
    // Which question is selected from the first box
    var __index = _src.options[getSelectedIndex( _src )].value;
    if( __index == -1 ) return true;
    var selected_question = data[__index].id;
    if( selected_question == -1 ) {
        return true;
    }
    
    // Add each Response from the response data array
    var index = 0;
    for( i = 0; i < subdata.length; i++ ) {
        if( subdata[i].id == selected_question ) {
            _dst.options[index] = new Option( subdata[i].nm, subdata[i].i );
            index++;
        }
    }
    
    return true;
     
}

function displayFilter( _filter ) {
 
    clearElement( _filter );
    var _count = 0;
    
    for( var _index in filterdata ) {
        if( filterdata[_index] != undefined ) {
            
            var _ids = _index.split('_');
            var q_id = _ids[0];
            var r_id = _ids[1];
            
            var response_qid = subdata[r_id].id;
            var response_rid = subdata[r_id].rid;
            var response_text = subdata[r_id].resp;
            var response_var = subdata[r_id].rvar;
            
            var question_text = data[q_id].nm;
            var question_id = data[q_id].id;
            
            var op = filterdata[_index];
            var op_text = '';
            if( op == '|' ) {
                op_text = ' or ';
            } else if ( op == '&' ) {
                op_text = ' and ';
            } else if ( op == '!' ) {
                op_text = ' not ';
            }
            
            _filter.options[_count] = new Option( question_text, question_id + "@" + response_qid + "@" + response_rid + "@" + response_var + "@" + op );
            _count++;
            _filter.options[_count] = new Option( op_text + response_text, question_id + "@" + response_qid + "@" + response_rid + "@" + response_var + "@" + op );
            _count++;
        }
    }
     
    return true;
}

function clearFilter( _questions, _responses, _filter ) {
    
    // We can completely clear the _responses and _filter
    clearElement( _responses );
    clearElement( _filter );
    
    // Clear data
    filterdata = null;
    filterdata = new Array();
    
    // Unselect the selected question
    var index = getSelectedIndex( _questions );
    if( index != -1 ) {
        _questions.options[0].selected = true;
    }
    
    return true;
}



//AUTHOR:       Enrique Rojas
//FUNCTION NAME:  clearSelect
//PARAMETERS:    selectBox name
//DESC:        Clears the contents of a select box  

function clearSelect(){
  for (i = 0; i < document.forms['form'].second.options.length; i++)
  {
      document.forms['form'].second.options[i] = null;
  }
}


function fillbox2(textinfo){
document.forms[0].second.options[0] = new Option(textinfo);
document.forms[0].second.options[1] = new Option(textinfo);
document.forms[0].second.options[2] = new Option(textinfo);
document.forms[0].second.options[3] = new Option(textinfo);
document.forms[0].second.options[4] = new Option(textinfo);
document.forms[0].second.options[5] = new Option(textinfo);
document.forms[0].second.options[6] = new Option(textinfo);
document.forms[0].second.options[7] = new Option(textinfo);
document.forms[0].second.options[8] = new Option(textinfo);
fillbox('');
}

function fillbox22(textinfo){
document.forms[0].second.options[0] = new Option(textinfo);
document.forms[0].second.options[1] = new Option(textinfo);
document.forms[0].second.options[2] = new Option(textinfo);
document.forms[0].second.options[3] = new Option(textinfo);
document.forms[0].second.options[4] = new Option(textinfo);
document.forms[0].second.options[5] = new Option(textinfo);
document.forms[0].second.options[6] = new Option(textinfo);
document.forms[0].second.options[7] = new Option(textinfo);
document.forms[0].second.options[8] = new Option(textinfo);

}

function populate()
{
  if (!optionTest) return;
  var box = document.forms[0].first;
  var number = box.options[box.selectedIndex].value;
  if (!number) return;
  var list = store[number];
  var box2 = document.forms[0].second;
  box2.options.length = 0;
  for(i=0;i<list.length;i+=2)
  {
    box2.options[i/2] = new Option(list[i],list[i+1]);
  }
}


/*
  - Give Credit Where Its Due -
  Please acknowledge this article and its author, at
  least in code comments, when using this code.

  Author: Justin Whitford
  Source: www.evolt.org

  Thank you.
*/

function filtery(pattern, list){
  /*
  if the dropdown list passed in hasn't
  already been backed up, we'll do that now
  */
  if (!list.bak || list.bak == 'undefined'){
    /*
    We're going to attach an array to the select object
    where we'll keep a backup of the original dropdown list
    */
    list.bak = new Array();
    for (n=0;n<list.length;n++){
      list.bak[list.bak.length] = new Array(list[n].value, list[n].text);
    }
  }

  /*
  We're going to iterate through the backed up dropdown
  list. If an item matches, it is added to the list of
  matches. If not, then it is added to the list of non matches.
  */
  match = new Array();
  nomatch = new Array();
  for (n=0;n<list.bak.length;n++){
    if(list.bak[n][1].toLowerCase().indexOf(pattern.toLowerCase())!=-1){
      match[match.length] = new Array(list.bak[n][0], list.bak[n][1]);
    }else{
      nomatch[nomatch.length] = new Array(list.bak[n][0], list.bak[n][1]);
    }
  }

  /*
  Now we completely rewrite the dropdown list.
  First we write in the matches, then we write
  in the non matches
  */
  for (n=0;n<match.length;n++){
    list[n].value = match[n][0];
    list[n].text = match[n][1];
  }
  for (n=0;n<nomatch.length;n++){
    list[n+match.length].value = nomatch[n][0];
    list[n+match.length].text = nomatch[n][1];
  }

  /*
  Finally, we make the 1st item selected - this
  makes sure that the matching options are
  immediately apparent
  */
  list.selectedIndex=0;
}


function clearall(){
  pop();
  fillbox('');
  fillbox2('');
}

function clearall2(){
  pop();
  fillbox22('');
}

function cookieIsSet(name)
{
    name += "=";
    var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; i++)
    {
        var c = cookies[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(name) == 0) return true;
    }
    return false;
}

function setCookie(name, value)
{
    document.cookie = name + "=" + value + "; path=/";
}

// -->


