
/**
 * Returns the host name
 *
 */
function getHostName() {
  var host = document.location.hostname;
  var port = document.location.port;
  var protocol = document.location.protocol;
  if (port.length <= 0)
    port = "";
  else
    port = ":" + port;
  return protocol + '//' + host + port + '/'; 
}

function useHttp(address) {
  if (address.substring(0, 5) == "https") {
    return "http" + address.substring("https".length);
  }
  // default 
  return address;
}

function trim(str) {
  return str.replace(/^\s+|\s+$/g, '');
}

/**
 * Fetches and displays a hint for a particular question (q_id)
 *
 * @param q_id
 * @user
 */
function fetchHint(q_id) {
  var cur_hint = $("span#hint_holder" + q_id).html();
  if (cur_hint == '') {
    /* get hint from backend */
    $.post(getHostName() + 'fetch/question/hint/', 
      {
        question_id : q_id
      },
      function (data, status) {
        if (data != 'False') {
          /* update hint contents */ 
          $("span#hint_holder" + q_id).text(data);
          toggleHint(q_id); 
        }
      }
    );
  } else {
    /* we already fetched the hint, toggle its display */ 
    toggleHint(q_id); 
  }
}

/**
 * Shows and hides a hint for a particular question (q_id)
 *
 * @param q_id
 * @used
 */
function toggleHint(q_id) {
  var h = $("span#hint_holder" + q_id); 
  var img = h.parent().find("img"); 
  if (!h.is(":visible")) {
    img.attr("src", "/resources/images/icons/icon_bulb.png"); 
    h.fadeIn("slow"); 
  } else {
    img.attr("src", "/resources/images/icons/icon_bulb-grey.png"); 
    h.hide(); 
  }
}

/**
 * Fetches all answers for all multiple-choice questions in 
 * the current lesson
 *
 * @used
 */ 
function lessonFetchAllAnswers() {
  /**
   * 1. Handle multiple choice answers 
   */
  /* clear existing checked answers in DOM */
  $("input[type=radio]").each(function () {
    $(this).attr("checked", false); 
  }); 
  $("input[type=checkbox]").each(function () {
    $(this).attr("checked", false); 
  }); 
  /* fetch a list of questions */
  q_ids = new Array();
  $("div.question").each(function (i, val) {
    q_ids[i] = $(val).attr("id").substring("question".length);
  }); 
  /* construct question id strings */
  question_ids = q_ids.join(" "); 
  /* fetch answers for all questions */
  $.post(getHostName() + 'edit/question/answers/all/', 
    {
      question_ids : question_ids
    }, 
    function (data, status) {
      if (status != 'False') { 
        /* get the answer ids */
        var a_ids = data.split(" "); 
        /* set all returned answers to be 'checked' */ 
        for (i in a_ids) {
          $("input[value=answer" + a_ids[i] + "]").attr("checked", true); 
        }
      }
    }
  );
  /**
   * 2. Handle openended questions 
   */ 
  $("textarea.answer_openended").each(function () {
    var str_q = $(this).attr("name");
    var str_q_id = str_q.substring("question".length);
    lessonQuestionFetchOpenendedAnswer(str_q_id); 
  });
}

/**
 * Fetches answers for an open-ended question (q_id)
 * 
 * @param q_id
 * @used
 */
function lessonQuestionFetchOpenendedAnswer(q_id) {
  /* get answers from backend */
  $.post(getHostName() + 'fetch/question/openendedanswer/', 
    {
      question_id : q_id
    }, 
    function (data, status) {
      if (status != 'False') { 
        $("textarea[name=question" + q_id + "]").html(data); 
      }
    }
  );
}

/**
 * Validates an email address
 *
 * @param value
 * @used
 */
function isValidEmail(value) { 
    return /.+@.+\..+/.test(value);
}


