﻿// Copyright Basement.nl, 2009

var FXmlHttp;

function btnNewClick()
{
  if (confirm("are you sure you want to clear the test?"))
    document.frmMain.list_plain.value = "";
  document.frmMain.list_plain.focus();
}

function btnOpenClick()
{
  document.frmMain.action = "open.php";
  document.frmMain.submit();
}

function btnSaveClick()
{
  var LOldAction = document.frmMain.action;
  document.frmMain.action = "action/process_save_to_teach.php";
  document.frmMain.submit();
  // Restore action
  document.frmMain.action = LOldAction;
}

function btnOpenInWrtsClick()
{
  var LOldAction = document.frmMain.action;
  document.frmMain.action = "action/process_save_to_wrts.php";
  document.frmMain.target = "_blank";
  document.frmMain.submit();
  // Restore action
  document.frmMain.target = "_self";
  document.frmMain.action = LOldAction;
}

function btnHelpClick()
{
  document.frmMain.action = "help.php";
  document.frmMain.submit();
}

function btnPrintClick()
{
  LWindow = window.open("", "LWinPrint", "width=600,height=300,scrollbars=1,resizable=1");
  var LOldAction = document.frmMain.action;
  document.frmMain.action = "print.php";
  document.frmMain.target = "LWinPrint";
  document.frmMain.submit();
  // Restore action
  document.frmMain.target = "_self";
  document.frmMain.action = LOldAction;
}

function btnSymbolClick()
{
  alert("symbols only work in the edit window");
}

function btnTestClick()
{
  var InputText = document.frmMain.list_plain.value;
  InputText = InputText.replace(/^\s*|\s*$/g,"");
  if (InputText == "")
  {
    alert("you didn't enter any questions.");
    document.frmMain.list_plain.focus();
  }
  else if (InputText.indexOf("=") == -1)
  {
    alert("use the symbol '=' to separate questions from their answers.");
    document.frmMain.list_plain.focus();
  }
  else    
    document.frmMain.submit();
}

function btnVoteClick(AListID, AVote)
{
  FXmlHttp = null;
  // Code for Mozilla, etc.
  if (window.XMLHttpRequest)
    FXmlHttp = new XMLHttpRequest()
  // Code for IE
  else if (window.ActiveXObject)
    FXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  if (FXmlHttp != null)
  {
    FXmlHttp.onreadystatechange = AjaxCallback;
    LUrl = "action/process_vote.php?chapter=" + encodeURIComponent(AListID) + "&vote=" + encodeURIComponent(AVote);
    FXmlHttp.open("GET", LUrl, true);
    FXmlHttp.send(null);
  }
}

function btnUpdateClick()
{
  if (confirm("save the changes?"))
  {
    document.frmMain.action = "action/process_list_update.php";
    document.frmMain.submit();
  }
}

function btnDeleteClick()
{
  if (confirm("are you sure you want to delete this test?"))
  {
    document.frmMain.action = "action/process_list_delete.php";
    document.frmMain.submit();
  }
}

function AjaxCallback()
{
  var LResponseText;
  var LResponseArray = new Array();
  if (FXmlHttp.readyState == 4)
  {
    if (FXmlHttp.status == 200)
    {
      LResponseText = FXmlHttp.responseText;
      if (LResponseText != "")
      {
	    LResponseArray = LResponseText.split("|");
	    // waardering 4.0 (aantal stemmen: 3) --> je stem is gewijzigd|3|3.0
        document.getElementById("idVote").innerHTML = "waardering " + LResponseArray[2] + " (aantal stemmen: " + LResponseArray[1] + ")";
		document.getElementById("idVoteResult").innerHTML = LResponseArray[0];
      }
    }
  }
}

function EditableContentClick(AClear, ADiv, AName, AType, AClass)
{
  var AContent = "";
  
  document.getElementById(ADiv).onclick = null;
  document.getElementById(ADiv).className = "";
  
  if (AClear == false)
    AContent = document.getElementById(ADiv).innerHTML;

  if (AType == "input")
    document.getElementById(ADiv).innerHTML = '<input class="' + AClass + '" name="' + AName + '" value="' + AContent + '" size="100" maxlength="100" type="text">'
  else
    document.getElementById(ADiv).innerHTML = '<textarea class="' + AClass + '" name="' + AName + '">' + AContent + '</textarea>';

  document.getElementsByName(AName)[0].select();
  document.getElementsByName(AName)[0].focus();
}

