/** THESE VARIABLES SHOULD BE CHANGED BASED ON YOUR INSTALLATION!" **/
var reg_user = "webuser";
var reg_password = "webuser";
var finish_url = "http://www.mexuar.com/";
/** THESE VARIABLES SHOULD BE CHANGED BASED ON YOUR INSTALLATION!" **/

var skipToAudio = (getCookie("reachedAudioTest") != null) //Will cause tests to be skipped if true.

var communications = false;
var count = 0;
var devices = false;
var pongs = 0;
var pongTime = 0;
var server = false;
var speed = 500;
var timeoutID = null;

var OUT_COOKIE = "outputDevice";
var IN_COOKIE = "inputDevice";


function skipTests(){
  var elNames = new Array(
    "javascript",
    "java",
    "cookies",
    "communication",
    "audio"
  );
  var el;
  for (var index in elNames){
    el = document.getElementById(elNames[index]);
    el.src = "img/tickbox.png";
  }
  
  checkAudioOut();
}

function checkJavascript(){
  //Stupid really it will always say yes.
  var jsEl = document.getElementById("javascript");
  jsEl.src = "img/tickbox.png";

  if(skipToAudio){
    skipTests();
  } else {
    //Delete the audio cookies so the user is pushed through the audio tests
    //whenever visiting the wizard (but not when returning from an audio test).
    deleteAudioOutCookie();
    deleteAudioInCookie();
    timeoutID = window.setTimeout(checkJava, 1000);
  }
}

function checkJava(){
  var jsEl = document.getElementById("java");
  jsEl.src = "img/tickbox.png";

  var faceless = getFaceless();
  var version = faceless.getJavaVersion() + "";//Adding empty string coerces java.lang.String to javascript String
  var verArr = version.split(".");
  version = verArr[0] + verArr[1];
  
  if (parseInt(version) < 15){
    document.location = "doc/failure_java.html";
  }
  
  timeoutID = window.setTimeout(checkCookies, 1000);
}

function checkCookies(){
 var cookieEl = document.getElementById("cookies");
 var failed = true;
 
 if (navigator.cookieEnabled){
    var now = new Date().getTime();
    setCookie("wizardOnce", now);

    var value = getCookie("wizardOnce");

    if (value == now){
      cookieEl.src = "img/tickbox.png";
      count = 30;
      timeoutID = window.setInterval(checkServer, 1000);
      deleteCookie("wizardOnce");
      
      failed = false;
    }
  }
  
  if(failed){
    document.location = "doc/failure_cookies.html";
  }
}

function checkServer(){
    var faceless = getFaceless();

    //Get the username and password used as context for the Asterisk PBX
    faceless.setPass(reg_user);
    faceless.setUser(reg_password);
    faceless.setCallingNumber('');
    faceless.setCallingName('');

    //Configure applet so calls are outgoing only
    faceless.setWantIncoming('FALSE');

    faceless.register();
    
    server = true;

    count = 5;
    timeoutID = window.setInterval(checkCommunication, 1000);
}

function checkCommunication(){
    count --;
    if (server){
      window.clearInterval(timeoutID);
      var lcEl = document.getElementById("communication");
      lcEl.src = "img/tickbox.png";

      
      //we need to check the OS and redirect to another page.
		if (navigator.appVersion.indexOf("X11")!=-1) {
			window.location=("doc/note_linux.html");
		} else {
      	checkAudio();
      }
    } else {
      if (count < 1) {
        window.clearTimeout(timeoutID);
        document.location = "doc/failure_communications.html";
      }
    }
}


function checkAudio(){
    window.setTimeout(checkAudioDevice, speed);
    var faceless = getFaceless();
    devices = faceless.isAudioInAvailable() && faceless.isAudioOutAvailable();
}

function checkAudioDevice(){
  if (devices){
      var jsEl = document.getElementById("audio");
      jsEl.src = "img/tickbox.png";
      checkAudioOut();
  } else {
      document.location = "doc/failure_audio.html";
  }
}

function checkAudioOut(){
  var faceless = getFaceless();
  var outCook = getCookie("outputDevice");
  if (outCook == null) {
    setCookie("reachedAudioTest", "true");
    document.location = "audio_out.html";
  } else {
    var lcEl = document.getElementById("audio_out");
    lcEl.src = "img/tickbox.png";
    window.setTimeout(checkAudioIn, 1000);
  }
}

function checkAudioIn(){
  var faceless = getFaceless();
  var inCook = getCookie("inputDevice");
  if (inCook == null) {
    document.location = "audio_in.html";
  } else {
     var lcEl = document.getElementById("audio_in");
     lcEl.src = "img/tickbox.png";
     window.setTimeout(allDone, 1000);
  }
}

function allDone(){
  deleteCookie("reachedAudioTest");
    
  var adEl = document.getElementById("all-done");
  adEl.innerHTML = "<font class='font-blue'>All Done, starting Corraleta...</font>";
  
  count = 6;
  timeoutID = window.setInterval(goFinish, 1000);
}

function goFinish(){	
  count --;
  var adEl = document.getElementById("all-done_tick");
  adEl.innerHTML = "<font class='font-blue'>" + count + "</font>";
  if (count < 1){
    window.clearInterval(timeoutID);
    document.location = finish_url;
  }
}

function deleteAudioOutCookie(){
  deleteCookie(OUT_COOKIE, "/");
}

function deleteAudioInCookie(){
  deleteCookie(IN_COOKIE, "/");
}

function setCookie(name, value, expires, path, domain, secure) {
    var cook = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
    document.cookie = cook;
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function setAudioCookie(name,val){
  var nextyear = new Date();
  nextyear.setFullYear(nextyear.getFullYear() +1);
  setCookie(name,val,nextyear.toGMTString(),"/");
}

function setAudioInCookie(val){
  setAudioCookie("inputDevice",val);
}

function setAudioOutCookie(val){
  setAudioCookie("outputDevice",val);
}