var winObj = '';

var counter;
counter = 0;
function show_resource(url,wwidth)
{
/* Trying to resusing the window doesn't really work in IE, unexplained behaviour and hangs
in Firefor reuse causes no hangs, but pdf won't jump to new destination. Therefore just popup new window
for every click */
/*
if (counter == 0 ) { url = "http://slashdot.org"; }
if (counter == 1 ) { url = "http://www.yahoo.com"; }
if (counter == 2 ) { url = "http://www.google.ca"; }
if (counter == 3 ) { url = "http://www.mit.edu"; }
        */

//if (counter == 1 ) { alert(winObj.location.href); }
/*if (!winObj.closed && winObj.location) {
alert('reusing ' + url);
winObj.location = url;
counter++;
}
else {*/
//alert('opening' + url); 

if (typeof wwidth == "undefined") {  wwidth = 650; }

winObj = window.open(url,"reswin" + counter, "resizable=yes,scrollbars=yes,status=yes,width=" + wwidth +"px");


//winObj.location = url;  //alert(winObj.location.href);
counter++;

//}

//winObj.focus();
return false;
}

function toggle_grey(node) { return; }

function show_level_profile(i,job_title)
{
 if (i == 0 ) { return; }

 url = 'resources/job_profiles/'+ job_title +'/JOB PROFILE '+ i +'.doc';
 show_resource(url,1000);
 return;
 //alert('hello');

}
function setup_qunits()
{
  divs = document.getElementsByTagName("div");

  for(i = 0; i < divs.length; i++) {

     if (divs[i].className == 'qunit') {
        divs[i].onclick = show_details;
     }

  }
}


function show_level_detail(node,level_id,job_id)
{

  prevdetail = document.getElementById("leveldetail");
  if (prevdetail != null) {
    /* prevdetail.id = '';
     prevdetail.style.display = 'none';
    */
    parentNode = prevdetail.parentNode;
    parentNode.removeChild(prevdetail);
  }

    n = document.createElement("div");

    n.className = "levelbox";
    n.id = "leveldetail";
    node.appendChild(n);


    n.style.pixelLeft = node.style.pixelLeft + 40;

    /*
    n.style.pixelTop = node.style.pixelTop -250;
    alert(level_id);
    eval('n.style.top = "' + (135  - (level_id * (6) + ((level_id - 3) * 1.8) ) ) + '%";');  //fancy bullshit. come up with a better formula if you can
    n.style.top = "90%";
    alert(node.style.posTop);
     */


    /* Get level info */

  url = "level_detail_lite.php?level_id=" + level_id + "&job_id=" + job_id;


  var xmlhttp = ajax_init();
  if (!xmlhttp) { // older opera or another browser that doesn't support xmlhttp

    n.style.display = 'none';
    w = window.open(url,'details',"resizable=yes,scrollbars=yes,status=yes,width=400px,height=400px");
    w.focus();
    return;
  }
  else {
/*     alltext = '';
    alltext = ajax_sync_get(xmlhttp,url);
    n.innerHTML = alltext; */
    ajax_async_get_html(xmlhttp,url,n.id)
  }

    return;

}

//setup the details box
//only one details box will show at a time. It will have the id of "qunitdetails" to give it a Singleton pattern
//(Don't know if I'm saying that right)
function show_details(node,level_id,track_id,job_id)
{
    //node = this;  // this line only works because we registered the onclick in function setup(). else you have to pass this in as an argument

  childdivs = node.getElementsByTagName("div");


  // Hide the old detail box, we can reuse it later.
  prevdetail = document.getElementById("qunitdetail");
      //alert('wipe it' + prevdetail.style.position );
  if (prevdetail != null) {
     prevdetail.id = '';
     prevdetail.style.display = 'none';


     //Enabling this code block means that reclicking the active qunit will hide the details box
/*        if (childdivs.length != 0 ) {
          n = childdivs[0];
          if (n == prevdetail) { return; } // same node as last time so toggle it off
       } */ 
  }


  // reuse hidden detail box if the qualification unit box has been clicked before in this user session
  if (childdivs.length != 0 ) {
                        // alert(node.innerHTML);
       n = childdivs[0];
       if (n.style.display == 'none') { n.style.display = 'block'; n.id = "qunitdetail"; }
            //alert('reusing');

  }
  else {   //qualification unit never clicked before so create new details box

    n = document.createElement("div");

    n.className = "detailsbox";
    n.id = "qunitdetail";
    node.appendChild(n);
    get_details(n,level_id,track_id,job_id);   // get the text

  }

  return
}

function ajax_init()
{
var xmlhttp;

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                                                      //alert('In firefox or opera browser');
  xmlhttp = new XMLHttpRequest();
}
     // alert('hiis');
return xmlhttp;
}


function ajax_async_get_html(xmlhttp,url,node_id)
{
 var node = document.getElementById(node_id);
 if ( node == undefined) { alert("Error: Can't get element with id "+ node_id); return false; }
  var alltext = '';

xmlhttp.open("GET", url,true);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   alltext =  xmlhttp.responseText;
     if (node_id != undefined) {
        node.innerHTML = xmlhttp.responseText;
     }
    //alert('alltext' + alltext);
  }
 }

 xmlhttp.send(null);
 return alltext;

}

/* function ajax_async_get_html(xmlhttp,url,node_id)
{

 if ( document.getElementById(node_id) == undefined) { alert("can't get qunitdetail node"); return; }
  var alltext = '';

xmlhttp.open("GET", url,true);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   alltext =  xmlhttp.responseText;
     if (node_id != undefined) {
        document.getElementById(node_id).innerHTML = xmlhttp.responseText;
     }
    //alert('alltext' + alltext);
  }
 }

 xmlhttp.send(null);
 return alltext;

} */

//grab the the information about the qualification unit (the coloured boxes) (level, track title, competency titles remotely
//Transparent for the user
function get_details(node_id,level_id,track_id,job_id)
{
  url = "qunitlite.php?level_id=" + level_id + "&track_id=" + track_id + "&job_id=" + job_id;

  node_id = 'qunitdetail';

  var xmlhttp = ajax_init();
if (!xmlhttp) { // older opera or another browser that doesn't support xmlhttp
//alert('opera');
document.getElementById(node_id).style.display = 'none';
w = window.open(url,'details',"resizable=yes,scrollbars=yes,status=yes,width=400px,height=400px");
w.focus();
return;
}
else {
 /*alltext = ajax_sync_get(xmlhttp,url);
 n.innerHTML = alltext; */
 ajax_async_get_html(xmlhttp,url,node_id);

}


}





function close_box(node_id)
{           //alert('close' + node.id);
    // Hide the old detail box, we can reuse it later.
  prevdetail = document.getElementById(node_id);
  if (prevdetail != null) {

     prevdetail.innerHTML = prevdetail.innerHTML; //needed cuz dom bug. in both IE and mozilla
     prevdetail.style.display = 'none';
     prevdetail.id = '';
  }
  return false;
}

function ajax_sync_get(xmlhttp,url)
{
  var alltext = '';

xmlhttp.open("GET", url,true);
/*the 'false' in the line above means a sync call and is considered bad practice, they say true for async instead.
without sync though you would have to keep this function inline in the calling function, because you'd have to assign
the received info after the line that said alltext =  xmlhttp.responseText;. So if you are having problems with this
function, replicate this code within the calling function and set xmlhttp.open("GET", url,false); to xmlhttp.open("GET", url,true)*/
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   alltext =  xmlhttp.responseText;
   alert(alltext);
  }
 }

 xmlhttp.send(null);
 //alert('cc');
 return alltext;

}

function ajax_sync_get_html(xmlhttp,url,node_id)
{
 var node = document.getElementById(node_id);
 if ( node == undefined) { alert("Error: Can't get element with id "+ node_id); return false; }
  var alltext = '';

xmlhttp.open("GET", url,false);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   alltext =  xmlhttp.responseText;
     if (node_id != undefined) {
        node.innerHTML = xmlhttp.responseText;
     }
    //alert('alltext' + alltext);
  }
 }

 xmlhttp.send(null);
 return alltext;

}
