var global_module = 0;
var sect_div_id = 1;
var divIdName = 'my1Div';
var span_id = "";

function sects_selected()
{
  document.view_form.elements['sections'].value = '';
  
  var buf = '';
  var len = document.sect_form.sect_select.options.length;
  for (var i = 0; i < len; ++i)
  {
    if (document.sect_form.sect_select.options[i].selected)
    {
      if (buf != '')
        buf += ',';
      buf += document.sect_form.sect_select.options[i].value;  
    }  
  }
  document.view_form.elements['sections'].value = buf;
}

function classes_selected()
{
  document.view_form.elements['classes'].value = '';
  
  var buf = '';
  var len = document.class_form.class_select.options.length;
  for (var i = 0; i < len; ++i)
  {
    if (document.class_form.class_select.options[i].selected)
    {
      if (buf != '')
        buf += ',';
      buf += document.class_form.class_select.options[i].value;  
    }  
  }
  document.view_form.elements['classes'].value = buf;
}

function GetXmlHttpObject()
{
  if (window.XMLHttpRequest)
  {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  }
  if (window.ActiveXObject)
  {
    // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  return null;
} 
   
function stateDivChangedSections()
{
  if (xmlhttp_sect.readyState == 4)
  {
    var inner = xmlhttp_sect.responseText;
    select_innerHTML(document.getElementById("sect_select"),inner);        
  }
}


function stateDivChangedClasses()
{
  if (xmlhttp_class.readyState == 4)
  {
    var inner = xmlhttp_class.responseText;
    select_innerHTML(document.getElementById("class_select"),inner);        
  }
}

function eraseSections()
{
  if (xmlhttp_erase_sections.readyState == 4)
  {
    var inner = xmlhttp_erase_sections.responseText;
    select_innerHTML(document.getElementById("divs_select"),inner);        
  }
}

function add_sections()
{
  var ni = document.getElementById('sections_div');
//var numi = document.getElementById('theValue');
//var num = (document.getElementById('theValue').value -1)+ 2;
//numi.value = sect_div_id; // num;
  ++sect_div_id;
  var newdiv = document.createElement('div');
  divIdName = 'my'+sect_div_id+'Div';
  newdiv.setAttribute('id',divIdName);
  
  var sect_html = '<form name="sect_form">';
  sect_html += '<table border=1 cellpadding=5 cellspacing=5 width=100 align=center>';
  sect_html += '  <tr>';
  sect_html += '    <td class=hsc_hdr1>';
  sect_html += '      Step 3, Select Sections';
  sect_html += '    </td>';
  sect_html += '  </tr>';
  sect_html += '  <tr>';  
  sect_html += '    <td class=hsc_hdr1>';
  sect_html += '      <select id="sect_select" name="sect_select" multiple="multiple" size=12 onchange="sects_selected()">';
  sect_html += '      </select>';  
  sect_html += '    </td>';
  sect_html += '  </tr>';
  sect_html += '</table>';
  sect_html += '</form>';
  newdiv.innerHTML = sect_html;
  ni.appendChild(newdiv);
  document.getElementById('class_step_no').innerHTML = '4';
  document.getElementById('class_inst').innerHTML = 'Select Individual Classes';
  document.getElementById('button_step_no').innerHTML = '5';
}

function remove_sections(div_num)
{
  var d = document.getElementById('sections_div');
  var old_div = document.getElementById(div_num);
  d.removeChild(old_div);
  document.getElementById('class_step_no').innerHTML = '3';
  document.getElementById('class_inst').innerHTML = 'Select Classes';
  document.getElementById('button_step_no').innerHTML = '4';
}

function stateModuleChanged()
{
  if (xmlhttp_divs.readyState == 4)
  {
    var inner = xmlhttp_divs.responseText;
    select_innerHTML(document.getElementById("divs_select"),inner);        
  }
}

function mod_selected($show_no)
{
  document.view_form.elements['sections'].value = '';
  document.view_form.elements['classes'].value = '';
  xmlhttp_divs = GetXmlHttpObject();
  if (xmlhttp_divs == null)
  {
    alert ("Your browser does not support XMLHTTP!");
    return;
  }
  var selection = document.module_form.module_select.selectedIndex;
  var selected_module = document.module_form.module_select.options[selection].value; 
//alert('global_module = ' + global_module + '   selected_module = ' + selected_module);  
  if (global_module != 0)
  {
    if (global_module == 1 && selected_module != 1)
    {
      remove_sections(divIdName);  
    } 
    else
    {
      if (global_module != 1 && selected_module == 1)
      {
        add_sections();
      }
    } 
  }
  else
  {
    if (selected_module == 1)
      add_sections();
  }
//alert('global_module = ' + global_module + '   selected_module = ' + selected_module);  
  global_module = selected_module;
  var params = "?show="+$show_no+"&mod=" + selected_module;
  var sid = "&sid=" + Math.random();
  xmlhttp_divs.onreadystatechange = stateModuleChanged;
  var url="get_divs.php";
  url = url + params + sid;
  xmlhttp_divs.open("GET",url,true);
  xmlhttp_divs.send(null);
  
  params = "?show="+$show_no+"&div=0";
  sid = "&sid=" + Math.random();
  xmlhttp_class = GetXmlHttpObject();
  sid = "&sid=" + Math.random();
  url2="get_classes.php" + params + sid;
  xmlhttp_class.onreadystatechange = stateDivChangedClasses;
  xmlhttp_class.open("GET",url2,true);
  xmlhttp_class.send(null);
  
}


function divs_selected($show_no)
{
  document.view_form.elements['sections'].value = '';
  document.view_form.elements['classes'].value = '';
  xmlhttp_sect = GetXmlHttpObject();
  if (xmlhttp_sect == null)
  {
    alert ("Your browser does not support XMLHTTP!");
    return;
  }
  var selection = document.div_form.divs_select.selectedIndex;
  
  var buf = '';
  var len = document.div_form.divs_select.options.length;
  for (var i = 0; i < len; ++i)
  {
    if (document.div_form.divs_select.options[i].selected)
    {
      if (buf != '')
        buf += ',';
      buf += document.div_form.divs_select.options[i].value;  
    }  
  }
  var params = "?show="+$show_no+"&div=" + buf;
  var sid = "&sid=" + Math.random();
  xmlhttp_sect.onreadystatechange = stateDivChangedSections;
  var url="get_sects.php";
  url = url + params + sid;
  xmlhttp_sect.open("GET",url,true);
  xmlhttp_sect.send(null);
  
  xmlhttp_class = GetXmlHttpObject();
  sid = "&sid=" + Math.random();
  url2="get_classes.php" + params + sid;
  xmlhttp_class.onreadystatechange = stateDivChangedClasses;
  xmlhttp_class.open("GET",url2,true);
  xmlhttp_class.send(null);
}

function get_html_translation_table (table, quote_style)
{
  // Returns the internal translation table used by htmlspecialchars and htmlentities  
  // 
  // version: 909.322
  // discuss at: http://phpjs.org/functions/get_html_translation_table    // +   original by: Philip Peterson
  // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   bugfixed by: noname
  // +   bugfixed by: Alex
  // +   bugfixed by: Marco    // +   bugfixed by: madipta
  // +   improved by: KELAN
  // +   improved by: Brett Zamir (http://brett-zamir.me)
  // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
  // +      input by: Frank Forte    // +   bugfixed by: T.Wild
  // +      input by: Ratheous
  // %          note: It has been decided that we're not going to add global
  // %          note: dependencies to php.js, meaning the constants are not
  // %          note: real constants, but strings instead. Integers are also supported if someone    // %          note: chooses to create the constants themselves.
  // *     example 1: get_html_translation_table('HTML_SPECIALCHARS');
  // *     returns 1: {'"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;'}
  
  var entities = {}, hash_map = {}, decimal = 0, symbol = '';    var constMappingTable = {}, constMappingQuoteStyle = {};
  var useTable = {}, useQuoteStyle = {};
  
  // Translate arguments
  constMappingTable[0]      = 'HTML_SPECIALCHARS';    constMappingTable[1]      = 'HTML_ENTITIES';
  constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
  constMappingQuoteStyle[2] = 'ENT_COMPAT';
  constMappingQuoteStyle[3] = 'ENT_QUOTES';
   useTable       = !isNaN(table) ? constMappingTable[table] : table ? table.toUpperCase() : 'HTML_SPECIALCHARS';
  useQuoteStyle = !isNaN(quote_style) ? constMappingQuoteStyle[quote_style] : quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT';

  if (useTable !== 'HTML_SPECIALCHARS' && useTable !== 'HTML_ENTITIES') {
      throw new Error("Table: "+useTable+' not supported');        // return false;
  }

  entities['38'] = '&amp;';
  if (useTable === 'HTML_ENTITIES') {        entities['160'] = '&nbsp;';
      entities['161'] = '&iexcl;';
      entities['162'] = '&cent;';
      entities['163'] = '&pound;';
      entities['164'] = '&curren;';        entities['165'] = '&yen;';
      entities['166'] = '&brvbar;';
      entities['167'] = '&sect;';
      entities['168'] = '&uml;';
      entities['169'] = '&copy;';        entities['170'] = '&ordf;';
      entities['171'] = '&laquo;';
      entities['172'] = '&not;';
      entities['173'] = '&shy;';
      entities['174'] = '&reg;';        entities['175'] = '&macr;';
      entities['176'] = '&deg;';
      entities['177'] = '&plusmn;';
      entities['178'] = '&sup2;';
      entities['179'] = '&sup3;';        entities['180'] = '&acute;';
      entities['181'] = '&micro;';
      entities['182'] = '&para;';
      entities['183'] = '&middot;';
      entities['184'] = '&cedil;';        entities['185'] = '&sup1;';
      entities['186'] = '&ordm;';
      entities['187'] = '&raquo;';
      entities['188'] = '&frac14;';
      entities['189'] = '&frac12;';        entities['190'] = '&frac34;';
      entities['191'] = '&iquest;';
      entities['192'] = '&Agrave;';
      entities['193'] = '&Aacute;';
      entities['194'] = '&Acirc;';        entities['195'] = '&Atilde;';
      entities['196'] = '&Auml;';
      entities['197'] = '&Aring;';
      entities['198'] = '&AElig;';
      entities['199'] = '&Ccedil;';        entities['200'] = '&Egrave;';
      entities['201'] = '&Eacute;';
      entities['202'] = '&Ecirc;';
      entities['203'] = '&Euml;';
      entities['204'] = '&Igrave;';        entities['205'] = '&Iacute;';
      entities['206'] = '&Icirc;';
      entities['207'] = '&Iuml;';
      entities['208'] = '&ETH;';
      entities['209'] = '&Ntilde;';        entities['210'] = '&Ograve;';
      entities['211'] = '&Oacute;';
      entities['212'] = '&Ocirc;';
      entities['213'] = '&Otilde;';
      entities['214'] = '&Ouml;';        entities['215'] = '&times;';
      entities['216'] = '&Oslash;';
      entities['217'] = '&Ugrave;';
      entities['218'] = '&Uacute;';
      entities['219'] = '&Ucirc;';        entities['220'] = '&Uuml;';
      entities['221'] = '&Yacute;';
      entities['222'] = '&THORN;';
      entities['223'] = '&szlig;';
      entities['224'] = '&agrave;';        entities['225'] = '&aacute;';
      entities['226'] = '&acirc;';
      entities['227'] = '&atilde;';
      entities['228'] = '&auml;';
      entities['229'] = '&aring;';        entities['230'] = '&aelig;';
      entities['231'] = '&ccedil;';
      entities['232'] = '&egrave;';
      entities['233'] = '&eacute;';
      entities['234'] = '&ecirc;';        entities['235'] = '&euml;';
      entities['236'] = '&igrave;';
      entities['237'] = '&iacute;';
      entities['238'] = '&icirc;';
      entities['239'] = '&iuml;';        entities['240'] = '&eth;';
      entities['241'] = '&ntilde;';
      entities['242'] = '&ograve;';
      entities['243'] = '&oacute;';
      entities['244'] = '&ocirc;';        entities['245'] = '&otilde;';
      entities['246'] = '&ouml;';
      entities['247'] = '&divide;';
      entities['248'] = '&oslash;';
      entities['249'] = '&ugrave;';        entities['250'] = '&uacute;';
      entities['251'] = '&ucirc;';
      entities['252'] = '&uuml;';
      entities['253'] = '&yacute;';
      entities['254'] = '&thorn;';        entities['255'] = '&yuml;';
  }

  if (useQuoteStyle !== 'ENT_NOQUOTES') {
      entities['34'] = '&quot;';    }
  if (useQuoteStyle === 'ENT_QUOTES') {
      entities['39'] = '&#39;';
  }
  entities['60'] = '&lt;';    entities['62'] = '&gt;';


  // ascii decimals to real symbols
  for (decimal in entities) {        symbol = String.fromCharCode(decimal);
      hash_map[symbol] = entities[decimal];
  }
  
  return hash_map;
}
function html_entity_decode (string, quote_style)
{
  // Convert all HTML entities to their applicable characters  
  // 
  // version: 912.1423
  // discuss at: http://phpjs.org/functions/html_entity_decode    // +   original by: john (http://www.jd-tech.net)
  // +      input by: ger
  // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   bugfixed by: Onno Marsman    // +   improved by: marc andreu
  // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +      input by: Ratheous
  // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
  // +      input by: Nick Kolosov (http://sammy.ru)    // +   bugfixed by: Fox
  // -    depends on: get_html_translation_table
  // *     example 1: html_entity_decode('Kevin &amp; van Zonneveld');
  // *     returns 1: 'Kevin & van Zonneveld'
  // *     example 2: html_entity_decode('&amp;lt;');    // *     returns 2: '&lt;'
  var hash_map = {}, symbol = '', tmp_str = '', entity = '';
  tmp_str = string.toString();
  
  if (false === (hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style))) {        return false;
  }

  // fix &amp; problem
  // http://phpjs.org/functions/get_html_translation_table:416#comment_97660    delete(hash_map['&']);
  hash_map['&'] = '&amp;';

  for (symbol in hash_map) {
      entity = hash_map[symbol];        tmp_str = tmp_str.split(entity).join(symbol);
  }
  tmp_str = tmp_str.split('&#039;').join("'");
  
  return tmp_str;
}


function select_innerHTML(object,innerHTML)
{
  if (object == null)
    return;
  object.innerHTML = "";
  var selTemp = document.createElement("micoxselect")
  var opt;
  selTemp.id = "micoxselect1"
  document.body.appendChild(selTemp)
  selTemp = document.getElementById("micoxselect1")
  selTemp.style.display="none"
  if (innerHTML.indexOf("<option") < 0)
  {
    innerHTML = "<option>" + innerHTML + "</option>"
  }
  innerHTML = innerHTML.replace(/<option/g,"<span").replace(/<\/option/g,"</span")
  selTemp.innerHTML = innerHTML;

  for (var i=0;i<selTemp.childNodes.length;i++)
  {
    var spantemp = selTemp.childNodes[i];

    if(spantemp.tagName)
    {     
      opt = document.createElement("OPTION")
  
      if (document.all)
      { //IE
        object.add(opt)
      }
      else
      {
        object.appendChild(opt)
      }       
  
      //getting attributes
      for(var j=0; j<spantemp.attributes.length ; j++)
      {
        var attrName = spantemp.attributes[j].nodeName;
        var attrVal = spantemp.attributes[j].nodeValue;
        if (attrVal)
        {
          try
          {
            opt.setAttribute(attrName,attrVal);
            opt.setAttributeNode(spantemp.attributes[j].cloneNode(true));
          }
          catch(e){}
        }
      }
      //getting styles
      if(spantemp.style)
      {
        for(var y in spantemp.style)
        {
          //try{opt.style[y] = spantemp.style[y];}catch(e){}
        }
      }      
      //value and text
      opt.value = spantemp.getAttribute("value")
      opt.text = spantemp.innerHTML
      opt.text = opt.text.replace(/&amp;/g,"&");
      //IE
      opt.selected = spantemp.getAttribute('selected');
      opt.className = spantemp.className;
    }   
  }    
  document.body.removeChild(selTemp)
  selTemp = null
}    
function stateSearchTypeChanged()
{
  if (xmlhttp_search.readyState == 4)
  {
  }
}

function stateResultsChanged()
{
  if (xmlhttp_results.readyState == 4)
  {
    var inner = xmlhttp_results.responseText;
    document.getElementById(span_id).innerHTML = inner;
  }
}
/*
function stateShowsChanged()
{
  if (xmlhttp_results.readyState == 4)
  {
    var inner = xmlhttp_results.responseText;
    document.getElementById(span_id).innerHTML = inner;
  }
}
*/
function show_check_clicked(obj,show_no,item_no,item_var,v)
{
  span_id = 'show_' + show_no + '_data';
  if (! obj.checked)
  {
    document.getElementById(span_id).innerHTML = '';

    params = "?show="+show_no+'&action=remove';
    sid = "&sid=" + Math.random();
    xmlhttp_shows = GetXmlHttpObject();
    url="selected_shows.php" + params + sid;
//  xmlhttp_shows.onreadystatechange = stateShowsChanged;
    xmlhttp_shows.open("GET",url,true);
    xmlhttp_shows.send(null);
  }
  else
  {
    params = "?show="+show_no+"&id=" + item_no + "&var=" + item_var + "&v=" + v;
    sid = "&sid=" + Math.random();
    xmlhttp_results = GetXmlHttpObject();
    url="get_show_results.php" + params + sid;
    xmlhttp_results.onreadystatechange = stateResultsChanged;
    xmlhttp_results.open("GET",url,true);
    xmlhttp_results.send(null);
    
    params = "?show="+show_no+'&action=add';
    sid = "&sid=" + Math.random();
    xmlhttp_shows = GetXmlHttpObject();
    url="selected_shows.php" + params + sid;
//  xmlhttp_shows.onreadystatechange = stateShowsChanged;
    xmlhttp_shows.open("GET",url,true);
    xmlhttp_shows.send(null);
  }
}


function print_window(url)
{
  window.open(url,'');
}

/*
function search_selected()
{
  var $search_type = document.search_form.elements['search_select'].value; 

  xmlhttp_search = GetXmlHttpObject();
  if (xmlhttp_search == null)
  {
    alert ("Your browser does not support XMLHTTP!");
    return;
  }
  var params = "?search="+$search_type;
  var sid = "&sid=" + Math.random();
  var url="sr_proxy.php";
  url = url + params + sid;
  xmlhttp_search.onreadystatechange = stateSearchTypeChanged;
  xmlhttp_search.open("GET",url,true);
  xmlhttp_search.send(null);
//dhtmlx_search.clearAll(true);
}
*/

// Modal Dialog Box
// copyright 8th July 2006 by Stephen Chapman
// http://javascript.about.com/
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (including these
// comments) is used without any alteration

/*
function pageWidth() {return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;}
function pageHeight() {return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;}
function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement && document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;}
function posTop() {return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;}
function $(x){return document.getElementById(x);}
function scrollFix(){var obol=$('ol');obol.style.top=posTop()+'px';obol.style.left=posLeft()+'px'}
function sizeFix(){var obol=$('ol');obol.style.height=pageHeight()+'px';obol.style.width=pageWidth()+'px';}
function kp(e){ky=e?e.which:event.keyCode;if(ky==88||ky==120)hm();return false}
function inf(h){tag=document.getElementsByTagName('select');for(i=tag.length-1;i>=0;i--)tag[i].style.visibility=h;tag=document.getElementsByTagName('iframe');for(i=tag.length-1;i>=0;i--)tag[i].style.visibility=h;tag=document.getElementsByTagName('object');for(i=tag.length-1;i>=0;i--)tag[i].style.visibility=h;}
function sm(obl, wd, ht){var h='hidden';var b='block';var p='px';var obol=$('ol'); var obbxd = $('mbd');obbxd.innerHTML = $(obl).innerHTML;obol.style.height=pageHeight()+p;obol.style.width=pageWidth()+p;obol.style.top=posTop()+p;obol.style.left=posLeft()+p;obol.style.display=b;var tp=posTop()+((pageHeight()-ht)/2)-12;var lt=posLeft()+((pageWidth()-wd)/2)-12;var obbx=$('mbox');obbx.style.top=(tp<0?0:tp)+p;obbx.style.left=(lt<0?0:lt)+p;obbx.style.width=wd+p;obbx.style.height=ht+p;inf(h);obbx.style.display=b;return false;}
function hm(){var v='visible';var n='none';$('ol').style.display=n;$('mbox').style.display=n;inf(v);document.onkeypress=''}
function initmb(){var ab='absolute';var n='none';var obody=document.getElementsByTagName('body')[0];var frag=document.createDocumentFragment();var obol=document.createElement('div');obol.setAttribute('id','ol');obol.style.display=n;obol.style.position=ab;obol.style.top=0;obol.style.left=0;obol.style.zIndex=998;obol.style.width='100%';frag.appendChild(obol);var obbx=document.createElement('div');obbx.setAttribute('id','mbox');obbx.style.display=n;obbx.style.position=ab;obbx.style.zIndex=999;var obl=document.createElement('span');obbx.appendChild(obl);var obbxd=document.createElement('div');obbxd.setAttribute('id','mbd');obl.appendChild(obbxd);frag.insertBefore(obbx,obol.nextSibling);obody.insertBefore(frag,obody.firstChild);
window.onscroll = scrollFix; window.onresize = sizeFix;}
window.onload = initmb;
*/
