// JavaScript Document
function load_questions(){
  var num = 0;
  while(document.getElementById("q"+ ++num)){
    total_questions++;
  }
  if(document.getElementById('intro')){
    document.getElementById('intro').style.visibility = 'visible';
    current_question = 0;
  }
  else{
    document.getElementById('question_1').style.visibility = 'visible';
  }
}

function question_answered(current_question){
  var answer_type = (document.getElementById("q"+current_question) ? document.getElementById("q"+current_question).type : document.getElementById("q"+current_question + "a").type);
  switch(answer_type){
    case "checkbox":
       var alphabet = new Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','aa');        
           for(i=0;i<alphabet.length;i++)
           {
             if(document.getElementById("q"+current_question+alphabet[i]))
             {
               if(document.getElementById("q"+current_question+alphabet[i]).checked)
               {
                 return true;
               }
             }
           }
           return false;    
    break;
    case "radio":
         if(getValues(current_question) == "&null")
           return false;
         else
           return true;
    break;
    case "textarea":
      if(document.getElementById("q"+current_question).value == "")
        return false;
      else
        return true;
    default:
       return true;
  }
}

function answer_list(){
  var temp_answers = "";
  var inputs=document.getElementsByTagName('input');
  for (var i=0;i<inputs.length;i++){
    if(inputs[i].name !== undefined && inputs[i].name !== "" && /^q/.test(inputs[i].name))
      temp_answers += inputs[i].name + "&";
  }
  var textareas=document.getElementsByTagName('textarea');
  for (var i=0;i<textareas.length;i++){
    if(textareas[i].name !== undefined && textareas[i].name !== "" && /^q/.test(textareas[i].name))
      temp_answers += textareas[i].name + "_text&";
  }
  document.getElementById("the_answer_list").value = temp_answers;
  return true;
}
 function getValues(question_number) {
  var values = "";
  if(document.getElementById("q"+question_number)){
    values += "&"+getRadioValue(document.survey["q"+question_number]);
  }
  else if(document.getElementById("q"+question_number+"a")){
    var alphabet = new Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','aa');
    for(i=0;i<alphabet.length;i++){
      if(document.getElementById("q"+current_question+alphabet[i])){
        if(document.getElementById("q"+current_question+alphabet[i]).checked){
          values += "&"+ (i+1);
        }
      }
    }
  }
  else{
    return false;
  }
 return values;
}

function getRadioValue (radioObj)
{
  for (var i=0; i < radioObj.length; i++) {
    if (radioObj[i].checked) {
      return radioObj[i].value;
    }
  }
  return null;
}

 //gives arrays the ability to shuffle its contents
Array.prototype.shuffle = function(){
  for (var i = 0; i < this.length; i++){
    // Random item in this array.
    var r = parseInt(Math.random() * this.length);
    var obj = this[r];
    // Swap.
    this[r] = this[i];
    this[i] = obj;
  }
};
 Array.prototype.inArray = function (value,caseSensitive){
  var i;
  for (i=0; i < this.length; i++) {
    if(caseSensitive){ //performs match even the string is case sensitive
      if (this[i].toLowerCase() == value.toLowerCase())
        return true;
    }
    else{
      if (this[i] == value)
        return true;
    }
  }
  return false;
};

