//global variable to hold returned list
var list = null;
var cco_path = "http://www.rctc.edu/cco/";

function update_options()
{
  $("#list").html("<dd><br>retrieving semester course and instructor lists...<br/><br/><dd><img src='http://www.rctc.edu/catalog/assets/images/ajax-loader.gif'></dd></dd><br/><br/>");
  
  get_subject_options();
  get_instructor_options(); 
  
  $("input:checked").each(function () { this.checked = false });
  
  return false;
}

function get_subject_options()
{  
  var url = "get_subject_options_z.php?yrtr=" + $("#yrtr").val() + "&access_time=" + Number(new Date()); 
  
  $.getJSON(url, function(data){
  
    var str = "<option value='All'> - All Departments - </option>";    
   
    $.each(data, function(i,val){
      str +=("<option value='"+val+"'>"+val+"</option>"); 
    });  
    
    $("#subj_id").html(str);       
    $("#subj_id").val("All");
  });
  
  return false;
}

function get_instructor_options()
{
  var url = "get_instructor_options_z.php?yrtr=" + $("#yrtr").val() + "&access_time=" + Number(new Date()); 

  $.getJSON(url, function(data){
  
    var str = "";
    
    $.each(data, function(i,val){
      if(i)
        str +=("<option value='"+$.trim(i)+"'>"+val+"</option>"); 
    });  
    
    $("#tech_id").html(str); 
    $("#tech_id>option").tsort(); 
    
         
    $("#tech_id").val("All");
     
    $('#list').html("");
  });
    
  return false;
}

function get_course_list()
{
  $("#optionnote").html("");
  
  $("#list").html("<dd><br>retrieving requested course list...<br/><br/><dd><img src='http://www.rctc.edu/catalog/assets/images/ajax-loader.gif'></dd></dd><br/><br/>");
  
  $("#submit").focus(); 
  
  var subj_id = $("#subj_id").val();
  var tech_id = $("#tech_id").val();
  
  //use cnt to ensure that some limitation is set on search -do not allow attempts to pull entire catalog
  var cnt = 0;
  
  if(subj_id != "All" || tech_id != "All")
    cnt++;
    
  cnt += $("input:checked").length; //number of checkboxes checked
 
  if(cnt < 0) 
  {
    $("#list").html("<br><br>You need to refine your search by selecting either a <b>Department</b> or an <b>Instructor</b>, or by limiting with the <b>Restrict To:</b> options.");
  }
  else
  {
    var url = "get_class_list.php?";  
  
    url += "&yrtr=" + $("#yrtr").val();
    url += "&subj=" + subj_id;
    url += "&tech_id=" + tech_id;
    url += "&open=1";  //only show open courses
    url += "&pending=0";  
    url += "&suspended=0";  
    url += "&canceled=0";  
    url += "&customized=0";   
    url += "&ind_study=0"; 
    url += "&dept_id=0";
     
    $("#online").attr('checked')       ? url += "&online=1"      : url += "&online=0";
    $("#flex_lab").attr('checked')     ? url += "&flex_lab=1"    : url += "&flex_lab=0";
    $("#heintz").attr('checked')       ? url += "&heintz=1"      : url += "&heintz=0";
    $("#honors").attr('checked')       ? url += "&honors=1"      : url += "&honors=0";
    $("#evening").attr('checked')      ? url += "&evening=1"     : url += "&evening=0";
    $("#accelerated").attr('checked')  ? url += "&accelerated=1" : url += "&accelerated=0";
    
    //delayed classes are all classes starting after day 5
    $("#delayed").attr('checked')      ? url += "&delayed=1"     : url += "&delayed=0";
    
   /*   
    $("#open").attr('checked')         ? url += "&open=0"        : url += "&open=1";
    $("#pending").attr('checked')      ? url += "&pending=0"     : url += "&pending=1";
    $("#suspended").attr('checked')    ? url += "&suspended=0"   : url += "&suspended=1";
    $("#canceled").attr('checked')     ? url += "&canceled=0"    : url += "&canceled=1";
    $("#customized").attr('checked')   ? url += "&customized=0"  : url += "&customized=1";
   */ 
  
    if($("#yrtr").val() != -1)
      make_request(url);  
    else
      $("#list").html("You need to select a <b>session</b> first.");
  }
  return false;
}

function make_request(url) 
{
  list = null;
  
  $.getJSON(url, function(data){
    list = data;
    
    if(list.length == 0)
      $("#list").html("<br /><span class='couREDTEXT'>No courses are currently available for this department and term</span><br/><br/><strong>Make sure you have not 'selected' or 'checked' any unintended restrictions!</strong><br/><br/><br/><br/>");
    else
      build_list()
  });    
  
  return false;   
}


function build_list()
{
  var cur_cou_id = "";

  var str = "<table width='100%' cellpadding='2' cellspacing='0' class='row'>";

  $.each(list, function(i, val) {
   
    if(val.COU_ID != cur_cou_id)	
    {
      str += "<tr bgcolor='#336699'><td colspan='4' class='tiny_txt'>&nbsp;</td></tr>";
      str += "<tr bgcolor='#FFCC66' class='td_border'>";
      str += "<td colspan='2' bgcolor='#FFCC66' class='td_border'><span class='couREDTEXT'>Course ID: " + val.COU_ID + "</span> - ";
      str +=  val.SUBJ+" "+val.COU_NBR+"-"+val.SECT_NBR;
      if(val.FLEX == 1)
        str += " (Flex Lab) ";
  
      str += ": <a href='get_course_desc.php?title="+val.TITLE+"&subj="+val.SUBJ+"&cou_nbr="+val.COU_NBR+"&crhr="+val.CRHR+"&curric_id="+val.CURRIC_ID+"' onclick=\"window.open(this.href,'','scrollbars=yes,width=510,height=250,menubar=yes'); return false;\">"+val.TITLE+"</a>";  
 
      str += " - Credits: "+val.CRHR+" - <a href='javascript:get_cco_pdf(\""+val.SUBJ+"_"+val.COU_NBR+"_CCO.pdf\");'>CCO</a></td> ";
	        

      str += "<td bgcolor='#FFFFCC' class='td_border'>Seats: "+val.MAX_SIZE +"</td>";
      str += "<td bgcolor='#FFFFCC' class='td_border'>Enrolled: ";
      
      if(val.NBR_ENR)
        str += val.NBR_ENR;
      else
        str += "0";
        
      str += "</td>";
      str += "</tr>";
    
      if(val.SPCL_MSG)
      {
        str += "<tr bgcolor='#C5C5D6'>";
        str += "<td colspan='4'>Notes: "+val.SPCL_MSG+"</td>";
        str += "</tr>";
      }
 
      cur_cou_id = val.COU_ID;
    }  
    else
      str += "<tr><td colspan='4'></td></tr>";
  
    str += "<tr class='row'>";
    str += "<td bgcolor='#eeeeee' width='35%'>";
    
    if(val.SECT_NBR > '90' && val.SECT_NBR <= '97')
      str += "<img src='../assets/images/moon.gif' border='0'> Evening<br/>";
    if(val.ROOM_NBR == "WWW")    
      str += "<img src='../assets/images/web.gif' border='0'> WWW online<br/>";
    if(val.INSTR_UNIT_TYPE == "5")    
      str += "<img src='../assets/images/honors.gif' border='0'> Honors";
    str += "&nbsp;</td>";
    str += "<td bgcolor='#eeeeee' width='40%'>";
    str += "Dates: "+(val.BEGIN_DATE).substring(4,6)+"/"+(val.BEGIN_DATE).substring(6,8)+" - "+(val.END_DATE).substring(4,6)+"/"+(val.END_DATE).substring(6,8)+"<br/>";
  
    if(val.BEGIN_TIME != 'ARR')
    {
      str += "Days: "+val.DAYS+"<br/>Time: "+to_am_pm(val.BEGIN_TIME);
      str += " - "+to_am_pm(val.END_TIME);
    }
    else
      str += "Days: Arranged<br/>Time: Arranged";
      
    str += "</td>";
    str += "<td colspan='2' bgcolor='#eeeeee' width='25%'>";
  
    if(val.LAST_NAME)
      str += "Instructor: <!--<a href='http://www.rctc.edu/catalog/course_schedule/get_office_hours.cfm?first_name="+val.FIRST_NAME+"&last_name="+val.LAST_NAME+"' onclick=\"window.open(this.href,'','scrollbars=yes,width=800,height=400,menubar=yes'); return false;\">-->"+val.LAST_NAME+", "+val.FIRST_NAME+"<!--</a>--><br/>";
    else
      str += "Instructor: unassigned<br/>";
      
    if(val.BLDG_CODE == "OC")    
      str += "Building: Off Campus"; 
    else
      str +=  "Building: "+val.BLDG_NAME+"<br/>Room: <a href='http://www.rctc.edu/campustour/maps/buildingmaps.html' target='_blank'>"+val.BLDG_CODE+" "+val.ROOM_NBR+"</a>";  

    str += "</td>";
    str += "</tr>";  
  });

  str += "</table>";
  
  $("#list").html(str);
  
  return false;
}

function get_desc(url)
{
  window.open(url,'','scrollbars=yes,width=510,height=250,menubar=yes');
  return false;
}

function set_flex()
{
  $("#flex_lab").attr('checked') ? $("#optionnote").html("Flex Lab is a way to deliver instructions to students who are unable to attend classes during daytime hours.  Flex Lab education offers a number of accounting, secretarial, and technical courses using an individualized method.  There are no formal lectures or classes.  The students progress at his/her own rates with help from the instructors who staff the lab.  Students may attend as many class hours of scheduled time as they need to complete their course work.  All curriculum offered in the Flex Lab mode adheres to Minnesota State Colleges and Universities approved curriculum in content, credit value, testing standards, and the amount of student effort required.  See notes in individual courses to get orientation dates and location. Attendance at the orientation is mandatory.  An instructor will be available at the Flex Lab Mondays and Wednesdays from 5-8 p.m.  The amount of time the flex lab is open may change due to enrollment in the flex lab classes.  Any change in the time of flex lab will be given in the flex lab.") : $("#optionnote").html("");
  return false;
}

function is_numeric(s)
{
  var valid_chars = "0123456789.";
  var is_number=true;
  var c;
   
  for (i=0; i<s.length && is_number == true; i++) 
  { 
    c = s.charAt(i); 
    if (valid_chars.indexOf(c) == -1) 
    {
      is_number = false;
    }
  }
  return is_number;
}

function to_am_pm(t)
{
  var str = "";
  
  if(t && is_numeric(t))  //if numeric
  {
	var d = new Array("12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12");
  
	var h = parseInt(t.substring(0,2),10);

	var tag = " AM";
	
	if(h > 11) tag = " PM";

    str = (d[h] + ":" + t.substring(2,4).toString() + tag);
  }
  else
    str = $.trim(t);
    
  return str;
}

function get_cco_pdf(file_name) {

  var fname = cco_path + file_name;
  
  $.ajax({
    url:fname,
    type:'HEAD',
    error:function (XMLHttpRequest, textStatus) {
      if(XMLHttpRequest.status == 404)
        alert("A Common Course Outline for this course is not currently available."); 
      else  
        alert("A Common Course Outline for this course is not available."); 
      return false;     
    },
    success:function (){
      //location.href=fname;
      window.open(fname);
      return false;
    } 
  });
}


$(document).ready(function() {
  $("#yrtr").val(-1);
  $("#subj_id").val("All");
  $("#tech_id").val("All");
  $("input:checked").each(function () { this.checked = false });
  //$("#subj_id").change(function() {});
 // $("#tech_id").change(function() {});
});


