// Author Geoff Golliher (ggolliher@lcer.org)
//


var bName = navigator.appName;
var shown = false;

function createCookie(name, value, days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function menuHide(name) {

  document.getElementById(name).style.display = 'none';
}

function menuShow(name) {
  document.getElementById(name).style.display = 'block';
}

function handleMenu(name) {

  menuShow(name);
  if(shown && shown != name) {
    menuHide(shown);
    setShown(name);
  } else if(shown == name) {
    menuHide(name);
    setShown(false);
    return;
  }
  setShown(name);
}

function setShown(name) {

  shown = name;
}

function calendarHide() {
  document.getElementById('sc').style.visibility = 'hidden';
  document.getElementById('ch').style.visibility = 'visible';
}

function calendarShow() {
  document.getElementById('sc').style.visibility = 'visible';
  document.getElementById('ch').style.visibility = 'hidden';
}

function handleClick(cDate, j) {
  var dialog = document.getElementById('cs');
  var element = document.getElementById(j);
  var tPosTop = element.offsetTop + element.parentNode.offsetTop;
  var tPosLeft = element.offsetLeft + element.parentNode.offsetLeft;
  dialog.style.left = tPosLeft+80;
  dialog.style.top = tPosTop+60;
  dialog.style.visibility = 'visible';  
  document.getElementById('e_date').value=cDate;
}

function handleCancel(d) {
  document.getElementById(d).style.visibility = 'hidden';
}

if(bName === "Netscape") {
  Document.prototype.loadXML = function(s) {

    var doc2 = (new DOMParser()).parseFromString(s, "text/xml");

    while (this.hasChildNodes())
      this.removeChild(this.lastChild);

    for (var i = 0; i < doc2.childNodes.length; i++) {
      this.appendChild(this.importNode(doc2.childNodes[i], true));
    }
  };
}

function importXML() {
	
  if (bName === "Netscape") {
    xmlDoc = document.implementation.createDocument("", "", null);
  } else {
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  }
  xmlDoc.loadXML(xmlDocument);
}

function getXMLFromServer() {

  if(bName === "Netscape") {
    xmlHttp = new XMLHttpRequest();
  } else {
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(E) {
        xmlHttp = false;
      }
    }
  }
  xmlHttp.open("GET", xmlpage, false);
  xmlHttp.send(null);
  xDoc = xmlHttp.responseText;
  xmlDocument = xDoc;
}


function sGet(sp) {

  if(bName === "Netscape") {
    xmlHttp = new XMLHttpRequest();
  } else {
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(E) {
        xmlHttp = false;
      }
    }
  }
  xmlHttp.open("GET", sp, false);
  xmlHttp.send(null);
  return xmlHttp.responseText;
}

function sGetWithDivArgs(sp, div) {

  if(bName === "Netscape") {
    xmlHttp = new XMLHttpRequest();
  } else {
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(E) {
        xmlHttp = false;
      }
    }
  }
  onSumResponse = function() {
    if (xmlHttp.readyState == 4) { 
      div.innerHTML = xmlHttp.responseText;
    }
    xmlHttp.responseText = null;
  };
  xmlHttp.open("GET", sp, true);
  xmlHttp.onreadystatechange = onSumResponse;
  xmlHttp.send(null);
}

function sPost(s, d) {

  if(bName === "Netscape") {
    xmlHttp = new XMLHttpRequest();
  } else {
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(E) {
        xmlHttp = false;
      }
    }
  }
  var formData = d
  xmlHttp.open('POST', s, true);
  xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlHttp.setRequestHeader("Content-length", formData.length);
  xmlHttp.send(formData);
}

function SetCss(element, className, prevID, callback) {

  document.getElementById(prevID).setAttribute("class", "");
  document.getElementById(prevID).setAttribute("id", "");
  element.setAttribute("class", className);
  element.setAttribute("id", prevID);
  eval(callback);
}

function GetAjaxCall(id, page, hs) {

  dWidth = document.getElementById(id).offsetWidth;
  dHeight = document.getElementById(id).offsetHeight;
  $("#" + id).html("<div style='height:"+ dHeight +"px;width:"+ dWidth +"px'>Loading ...</div>");
  $("#" + id).load(page);
  if(hs) {
    handleMenu(id);
  }
}

function GetAjaxCallAndAppend(id, page, extra) {
  
  document.getElementById(id).innerHTML = extra;
  $.get(page, function(data){
    $(data).prependTo('#' + id);
  });
}

function buttonHref(link) {
  document.location.href=link;
}

function HandleQuizSubmit(quiz_id) {
  var form = document.getElementById('quiz-form');
  var aids = new Array();
  var qids = new Array();
  count = form.elements.length;
  for (i=0; i < count; i++) {
    if(form.elements[i].checked) {
      aids.push(form.elements[i].value);
      qids.push(form.elements[i].name);
    }
  }

  poststr = "questions=" + encodeURI(qids);
  poststr += "&answers=" + encodeURI(aids);
  poststr += "&quiz_id=" + encodeURI(quiz_id);
  $("#quiz-container").load("/ajax/quiz_grader.php?" + poststr);
}

function LinkVid(id, page, sid) {
  obj = document.getElementById(sid);
  var selIndex = obj.selectedIndex;
  var mid = obj.options[selIndex].value;
  alert(page +"&mid=" + mid);
  if(mid != "") {
    GetAjaxCall(id, page +"&mid=" + mid, false);
  }
}

function handleVideoInfo(obj) {
  var selIndex = obj.selectedIndex;
  var mid = obj.options[selIndex].value;
  if(mid != "") {
    GetAjaxCall("video-info", "/ajax/video-info.php?mid=" + mid, false);
  }
}

var lesson_category = "";

function handleNewLesson(part) {

  switch(part) {
    case 1:
      var lesson_name = document.getElementById('lesson_name').value;
      var duration = document.getElementById('duration').value;
      var grade = document.getElementById('grade').value;
      var selObj = document.getElementById('catid');
      var selIndex = selObj.selectedIndex;
      var category = selObj.options[selIndex].value;
      lesson_category = category;
      lname = encodeURI(lesson_name);
      var page = "/ajax/lesson-create-video-select.php?category=" + category + "&lesson_name=" + lname + "&do=create&duration=" + encodeURI(duration) + "&grade=" + encodeURI(grade);
      var gvids = true;
      document.getElementById("lesson-create-menu").style.visibility = 'visible';
    break;
    case 2:
      var page = "";
    break;
  }
  GetAjaxCall("lesson-creator-main", page, false);
  /*
if(gvids) {
      GetAjaxCallAndAppend("vid-list", "/ajax/video-list.php", "<br /><input value='Upload Video' type='button' onClick='GetAjaxCall(\"vid-list\", \"/ajax/video-uploader.php?category=" + category + "\")'>");
  }
*/
  $("#question-list").html("");
}

function handleLessonSelect(selObj) {

  var selIndex = selObj.selectedIndex;
  var lid = selObj.options[selIndex].value;
  document.getElementById('lesson-create-menu').style.visibility = 'visible';
  lesson_id = lid;
  if(lid != "") {
    GetAjaxCall("lesson-creator-main", "/ajax/lesson-create-video-select.php?lid=" + lid, false);
  }
  $("#question-list").html("");
}

var lesson_id = "";
var quiz_id = "";

function handleLessonObjectiveClick() {

  if(lesson_id != "") {
    GetAjaxCall("lesson-creator-main", "/ajax/lesson-create-video-select.php?lid=" + lesson_id, false);
  }
  $("#question-list").html("");
}

function handleLessonVideoClick() {

  if(lesson_category == "") {
    lesson_category = document.getElementById('v-category').value;
  }

  GetAjaxCall("lesson-creator-main", "/ajax/video-list.php?lid=" + lesson_id + "&category=" + lesson_category);
  $("#question-list").html("");
}

function handleLessonVideoTranscriptClick() {
  if(lesson_category == "") {
    lesson_category = document.getElementById('v-category').value;
  }

  GetAjaxCall("lesson-creator-main", "/ajax/video-transcript-list.php?lid=" + lesson_id + "&category=" + lesson_category);
  $("#question-list").html("");
}

function handleLessonTeacherBrackgroundClick() {

  if(lesson_category == "") {
    lesson_category = document.getElementById('v-category').value;
  }

  GetAjaxCall("lesson-creator-main", "/ajax/teacher-background-list.php?lid=" + lesson_id + "&category=" + lesson_category);
  $("#question-list").html("");
}

function handleLessonLabObjClick() {

  if(lesson_category == "") {
    lesson_category = document.getElementById('v-category').value;
  }

  GetAjaxCall("lesson-creator-main", "/ajax/lab-metadata-edit.php?lid=" + lesson_id + "&category=" + lesson_category);
  $("#question-list").html("");
}

function handleLessonLabClick() {

  if(lesson_category == "") {
    lesson_category = document.getElementById('v-category').value;
  }

  GetAjaxCall("lesson-creator-main", "/ajax/lab-pdf-list.php?lid=" + lesson_id + "&category=" + lesson_category);
  $("#question-list").html("");
}

function handleLessonLabVideoClick() {

  if(lesson_category == "") {
    lesson_category = document.getElementById('v-category').value;
  }

  GetAjaxCall("lesson-creator-main", "/ajax/video-list.php?lid=" + lesson_id + "&lab_id=true");
  $("#question-list").html("");
}

function handleLessonQuizClick() {

  if(lesson_category == "") {
    lesson_category = document.getElementById('v-category').value;
  }

  GetAjaxCall("lesson-creator-main", "/ajax/quiz-create-main.php?lid=" + lesson_id + "&category=" + lesson_category);
  $("#question-list").html("");
}

function handleEditQuestions(qid) {
  
  quiz_id = qid;
  shown = false;

  GetAjaxCall("lesson-creator-main", "/ajax/quiz-question-edit.php?qid=" + quiz_id);
  GetAjaxCall("question-list", "/ajax/quiz-question-list.php?qid=" + quiz_id);
}

function handleNewQuiz() {
  
  q_name = document.getElementById('q_name').value;
  q_description = document.getElementById('quiz-description').value;
  //alert("/ajax/quiz-create-stub.php?q_name=" + encodeURI(q_name) + "&q_description=" + encodeURI(q_description) + "&lid=" + lesson_id);
  GetAjaxCall("lesson-creator-main", "/ajax/quiz-create-stub.php?q_name=" + encodeURI(q_name) + "&q_description=" + encodeURI(q_description) + "&lid=" + lesson_id);
  
}

function handleUpdateQuiz(quiz_id) {
  
  q_name = document.getElementById('q_name').value;
  q_description = document.getElementById('quiz-description').value;
  //alert("/ajax/quiz-create-stub.php?q_name=" + encodeURI(q_name) + "&q_description=" + encodeURI(q_description) + "&qid=" + quiz_id);
  GetAjaxCall("lesson-creator-main", "/ajax/quiz-update-meta.php?q_name=" + encodeURI(q_name) + "&q_description=" + encodeURI(q_description) + "&qid=" + quiz_id);
  GetAjaxCall("question-list", "/ajax/quiz-question-list.php?qid=" + quiz_id);
}

function handleEditQuiz() {

  quiz_id = document.getElementById('quiz_id').value;
  for (var i=0; i<document.quizQuestionEdit.type.length; i++)  {
    if (document.quizQuestionEdit.type[i].checked)  {
      q_type = document.quizQuestionEdit.type[i].value;
    }
  }
  q_text = document.getElementById('q_text').value;
  question_id = document.getElementById('question_id').value;
  
  if(q_text == '' || q_type == '') {
    alert("missing fields");
    return;
  }

  if(question_id != undefined) {
    GetAjaxCall("lesson-creator-main", "/ajax/quiz-answer-edit.php?quiz_id=" + quiz_id + "&q_text=" + encodeURI(q_text) + "&q_type=" + q_type + "&question_id=" + question_id);
  } else {
    GetAjaxCall("lesson-creator-main", "/ajax/quiz-answer-edit.php?quiz_id=" + quiz_id + "&q_text=" + encodeURI(q_text) + "&q_type=" + q_type);
    if(q_type == 3) {
      GetAjaxCall("question-list", "/ajax/quiz-question-list.php?qid=" + quiz_id);
    }
  }
}

function handleQuestionEdit(qid, quiz_id) {

  GetAjaxCall("lesson-creator-main", "/ajax/quiz-question-edit.php?question_id=" + qid + "&qid=" + quiz_id);
  GetAjaxCall("question-list", "/ajax/quiz-answers-list.php?qid=" + qid);
}

function handleAnswerEdit(aid, qid) {

  //alert(qid);
  //shown = false;

  
  GetAjaxCall("lesson-creator-main", "/ajax/quiz-answer-update.php?question_id=" + qid + "&aid=" + aid + "&op=edit");
  $("#question-list").html("");
  $("#answers-list").html("");
}

function handleUpdateAnswer(aid) {
  
  var correct = 0;
  var a_text = document.getElementById('a_text').value;
  //var quiz_id = document.getElementById('quiz_id').value;
  correct = document.getElementById('correct').value;
 
  var qid = document.getElementById('qid').value;
  GetAjaxCall("lesson-creator-main", "/ajax/quiz-answer-update.php?op=update&question_id=" + qid + "&correct=" + correct + "&a_text=" + encodeURI(a_text) + "&aid=" + aid);
  $("#question-list").html("");
}

function handleUpdateAnswerTF(aid) {
  
  var correct = 0;
  //var quiz_id = document.getElementById('quiz_id').value;
  
  correct = 0;

  if (document.quizQuestionEdit.correct[0].checked)  {
    correct = 1;
  }
  
  //look at updatetf to make sure we are getting things right and not deleting the previous correct ans.
  var qid = document.getElementById('qid').value;
  //alert("/ajax/quiz-answer-update.php?op=updatetf&question_id=" + qid + "&correct=" + correct + "&aid=" + aid);
  GetAjaxCall("lesson-creator-main", "/ajax/quiz-answer-update.php?op=updatetf&question_id=" + qid + "&correct=" + correct + "&aid=" + aid);
  //GetAjaxCall("question-list", "/ajax/quiz-question-list.php?qid=" + quiz_id);
}

function handleAddAnswer() {
  
  var correct = 0;
  var a_text = document.getElementById('a_text').value;
  var quiz_id = document.getElementById('quiz_id').value;

  if (document.getElementById('correct').checked)  {
    correct = document.getElementById('correct').value;
  }

  var qid = document.getElementById('qid').value;
  GetAjaxCall("lesson-creator-main", "/ajax/quiz-answer-edit.php?op=add&qid=" + qid + "&correct=" + correct + "&a_text=" + encodeURI(a_text) + "&quiz_id=" + quiz_id);
  GetAjaxCall("question-list", "/ajax/quiz-answers-list.php?qid=" + qid);
}

function handleDeleteAnswer(aid) {
  
  var quiz_id = document.getElementById('quiz_id').value;
  var qid = document.getElementById('qid').value;
  //alert("/ajax/quiz-answer-update.php?op=delete&aid=" + aid + "&question_id=" + qid + "&quiz_id=" + quiz_id);
  GetAjaxCall("lesson-creator-main", "/ajax/quiz-answer-update.php?op=delete&aid=" + aid + "&question_id=" + qid + "&quiz_id=" + quiz_id);
}

function handleAddAnswerTF() {
  
  var correct = 0;
  var quiz_id = document.getElementById('quiz_id').value;
  
  correct = "False";

  if (document.quizQuestionEdit.correct[0].checked)  {
    correct = "True";
  }
  
  var qid = document.getElementById('qid').value;
  GetAjaxCall("lesson-creator-main", "/ajax/quiz-answer-edit.php?op=addtf&qid=" + qid + "&correct=" + correct + "&quiz_id=" + quiz_id);
  GetAjaxCall("question-list", "/ajax/quiz-question-list.php?qid=" + quiz_id);
}

function handleDeleteQuestion(qid) {
  
  var quiz_id = document.getElementById('quiz_id').value;
  //alert("/ajax/quiz-question-edit.php?op=delete&question_id=" + qid + "&qid=" + quiz_id);
  GetAjaxCall("lesson-creator-main", "/ajax/quiz-question-edit.php?op=delete&question_id=" + qid + "&qid=" + quiz_id);
  $("#question-list").html("Loading ... ");
  GetAjaxCall("question-list", "/ajax/quiz-question-list.php?qid=" + quiz_id);
  
}

function handleMaterialsClick() {

  GetAjaxCall("lesson-creator-main", "/ajax/related-materials-add.php");
  GetAjaxCall("question-list", "/ajax/related-materials-add.php?op=list");
}

function adminHome() {
  document.location.href = "/admin/";
}

function handleCategoryCreate() {

  GetAjaxCall("lesson-creator-main", "/ajax/category-create.php");
}

function handleMoonMinClick() {

  GetAjaxCall("lesson-creator-main", "/ajax/moon-minutes-uploader.php");
  GetAjaxCall("question-list", "/ajax/moon-minutes-uploader.php?op=list");
}

