﻿// Java script

// reference local blank image
Ext.BLANK_IMAGE_URL = 'ext20/resources/images/default/s.gif';

var comboStates;
var storeStates;

var comboInstitutions;
var storeInstitutions;

var comboSubjects;
var storeSubjects;
var cbSNL;

var gridCourses;
var storeCourses;
var pagingBar;
var pagingButton;

// Create application
Ext.onReady(function() {
  Ext.QuickTips.init();
  Ext.QuickTips.enable();

  //Private properties
  var uri='services/getData.ashx';
  
  storeStates = new Ext.data.Store({
    //load using HTTP
    // url: 'getData.ashx',
    url: uri,
    baseParams: {"cmd": 'state'},
     
    reader: new Ext.data.XmlReader(
      {record: 'Row',
       id: 'ID'}
      , ['ID', 'DESCR']
    )
        
  });  // end of storeStates
  
  storeStates.on('load', fixIllinoisOnLoad);
      
  storeStates.load();
      
  comboStates = new Ext.form.ComboBox({
     store: storeStates,
     fieldLabel: "Choose State: ",
     disableKeyFilter: false,
     displayField: 'DESCR',
     loadingText: 'Loading...',
     valueField: 'ID',
     //typeAhead: true,
     mode:'local',
     triggerAction: 'all',
     emptyText: 'Select a state...',
     applyTo: 'combo-states'
  });
  
  //comboStates.on("expand", expandStates);
  comboStates.on("select", updateFromState);
  
  storeInstitutions = new Ext.data.Store({
    //url: 'getData.ashx',
    url: uri,
    baseParams: {cmd: 'inst'},
    reader: new Ext.data.XmlReader(
      {record: 'Row',
       id: 'EXT_ORG_ID' }
       , [ {name: 'ID', mapping: 'EXT_ORG_ID'},
           {name: 'DESCR', mapping: 'DESCR50'}]
    )
  });
  
  comboInstitutions = new Ext.form.ComboBox({
     store: storeInstitutions,
     disabled: true,
     displayField: 'DESCR',
     valueField: 'ID',
     typeAhead: true,
     mode: 'local',
     triggerAction: 'all',
     emptyText: 'Select an Institution...',
     applyTo: 'combo-institutions'
  });
  //comboInstitutions.on("expand", expandInstitutions);
  comboInstitutions.on("select", updateFromInstitution);
  
  storeSubjects = new Ext.data.Store({
    //url: 'getData.ashx',
    url: uri,
    baseParams: {cmd: 'subj'},
    reader: new Ext.data.XmlReader(
      {record: 'Row',
       id: 'COMP_SUBJECT_AREA' }
       , [ {name: 'ID', mapping: 'COMP_SUBJECT_AREA'},
           {name: 'DESCR'},
           {name: 'SRC_TRM_TYPE', mapping: 'EXT_TERM_TYPE'}]
    )
  });
  
  comboSubjects = new Ext.form.ComboBox({
     store: storeSubjects,
     fieldLabel: "Course Subject",
     disabled: true,
     displayField: 'DESCR',
     loadingText: 'Loading...',
     valueField: 'ID',
     typeAhead: true,
     mode: 'local',
     triggerAction: 'all',
     emptyText: 'Select a Subject...',
     applyTo: 'combo-subjects'
  });
  
  //comboSubjects.on("expand", expandSubjects);
  comboSubjects.on("select", updateFromSubjects);
  
  cbSNL = new Ext.form.Checkbox({
     disabled: true,
     applyTo: 'cb-SNL'
  });
  
  cbSNL.on("check", checkSNL);
  
   storeCourses = new Ext.data.Store({
    //url: 'getData.ashx',
    url: uri,
    baseParams: {cmd: 'crsep'},
    remoteSort: true,
    reader: new Ext.data.XmlReader(
      {record: 'Row',
       totalRecords: 'CNTROWS'},
      [ {name: 'DU_CRSE_ID'},
        {name: 'TRNSFR_SRC_ID'},
        {name: 'SRC_CRSE_SUBJ'},
        {name: 'SRC_CRSE_NBR'},
        {name: 'SRC_CRSE_DESCR'},
        {name: 'SRC_CRSE_UNITS'},
        {name: 'EXT_TERM_TYPE'},
        {name: 'DU_CRSE_SUBJ'},
        {name: 'DU_CRSE_NBR'},
        {name: 'DU_CRSE_DESCR'},
        {name: 'DU_CRSE_DESCRLONG'},
        {name: 'DU_CRSE_DE'},
        {name: 'DU_CRSE_DE_DESCR'}]
    ) // end of reader:
  });
  
   storeCourses.setDefaultSort('src_crse_subj', 'asc');
   
  pagingBar = new Ext.PagingToolbar({
            pageSize: 25,
            store: storeCourses,
            displayInfo: true,
            displayMsg: "Displaying courses {0} - {1} of {2}",
            emptyMsg: "No courses to display",
            items: ['-',{
                   handler: clickPrintButton,
                   text: 'Print',
                   tooltip: "Opens a PDF version of Course Equivalency Guide with all rows for the current institution and subject."
                   }]
            }
         );
  
  gridCourses = new Ext.grid.GridPanel({
    store: storeCourses,
    loadMask: true,
    cm: new Ext.grid.ColumnModel([
      {header: "<p>Trns</p><p>Subj</p>", width:44, dataIndex: 'SRC_CRSE_SUBJ', sortable: true},
      {header: "<p>Trns</p><p>Num</p>", width: 40, dataIndex: 'SRC_CRSE_NBR'},
      {header: "<p>Transfer Course</p><p>Title</p>", width: 120, dataIndex: 'SRC_CRSE_DESCR', sortable: true, renderer: renderCrseTitle},
      {header: "<p>Trns</p><p>Units</p>", width: 34, dataIndex: 'SRC_CRSE_UNITS'},
      {header: '<p>Term</p><img id="headerTerm" src="images/icon-info.gif"/>', width: 40, dataIndex: 'EXT_TERM_TYPE'},
      {header: "<p>DPU</p><p>Subj</p>", width: 40, dataIndex: 'DU_CRSE_SUBJ', sortable: true},
      {header: "<p>DPU</p><p>Num</p>", width: 42, dataIndex: 'DU_CRSE_NBR', sortable: true},
      {header: "<p>DePaul Course</p><p>Title</p>", width: 120, dataIndex: 'DU_CRSE_DESCR', sortable: true, renderer: renderCrseLink},
      {header: '<p>Gen</p><p>Educ</p><img id="headerGenEd" src="images/icon-info.gif"/>', width: 46, dataIndex: 'DU_CRSE_DE', sortable: true, renderer: renderCrseCategory}]),
    renderTo: 'grid-courses',
    width: 546,
    height: 325,
    bbar: pagingBar
  });
  
  new Ext.ToolTip({
    target: 'headerTerm',
    title: 'System at Transfer Institution',
    html: '<p>SEM = Semester System</p><p>QTR = Quarter System</p>',
    autoHide: true,
    closable: false});
	
  new Ext.ToolTip({
    target: 'headerGenEd',
    title: 'Sorting',
    html: '<p>The best way to sort by this column is to click the dropdown arrow and select "descending."</p>',
    autoHide: true,
    closable: false});
  
  gridCourses.hide();
  gridCourses.disable();
  
  
  setTimeout(function(){
     Ext.get('loading').remove();
     Ext.get('loading-mask').fadeOut({remove:true});
  }, 350);
  
}); // end of onReady

var reloadInstitutions=true;
var reloadSubjects=true;

function expandInstitutions(cmb) {
  if (reloadInstitutions) {
    storeInstitutions.load({params: {state: comboStates.value}});
    reloadInstitutions=false;
  }
}

function expandSubjects(cmb) {
  if (reloadSubjects) {
    storeSubjects.load({params: {institution: comboInstitutions.value}});
    reloadSubjects=false;
  }
}

function renderCrseLink(crseID, p, record) {
   var crseTitle;
   var crseDescr;
   var contents;
   var crseSubj;
   var crseNbr;
   
   crseTitle = record.get('DU_CRSE_DESCR');
   crseDescr = record.get('DU_CRSE_DESCRLONG');
   crseSubj = record.get('DU_CRSE_SUBJ');
   crseNbr = record.get('DU_CRSE_NBR');
   
   if (crseDescr != '') {
     contents =  '<div style="white-space:normal;">' + crseTitle ;
     contents += '<img src="images/icon-info.gif" '; 
     contents += 'ext:qtip="' + crseDescr + ' "';
     contents += 'ext:qtitle="' + crseSubj + ' ' + crseNbr + ': ' + crseTitle + '"';
     contents += ' /></div>';
   } else {
     contents = '<div  style="white-space:normal;">' + crseTitle + '</div>';
   }
   
   return contents;
}

function renderCrseCategory(value, p, record) {
   var crseTitle;
   var crseDescr;
   var contents;
   
   crseTitle = record.get('DU_CRSE_DE');
   crseDescr = record.get('DU_CRSE_DE_DESCR');
   
   if (crseDescr != '') {
     contents =  '<div>' + crseTitle + '<img src="images/icon-info.gif" ext:qtip="' + crseDescr + '" /></div>';
   }
   
   return contents;
}

function renderCrseTitle(value, p, record) {
  var contents;
  
  contents = '<div  style="white-space:normal;">' + value + '</div>';
  
  return contents;
}

function updateFromState(cmb, data, idx){
    cbSNL.disable();
    comboSubjects.disable();
    comboSubjects.clearValue();
    gridCourses.disable();
    gridCourses.hide();
    storeCourses.removeAll();
    
    //reloadInstitutions=true;
    storeInstitutions.load({params: {state: comboStates.value}});
    comboInstitutions.clearValue();
    comboInstitutions.enable();
}

function updateFromInstitution(cmb, data, idx){
    gridCourses.disable();
    gridCourses.hide();
    storeCourses.removeAll();
    snl = 'no';
  
    if (cbSNL.getValue()) {
      snl='yes';
    }
    //reloadSubjects=true;
    storeSubjects.load({params: {institution: comboInstitutions.value, snl: snl}});
    comboSubjects.clearValue();
    comboSubjects.enable();
    cbSNL.enable();
}

function updateFromSubjects(cmb, data, idx) {
  var snl;
  
  currentState=cmb.value;
  snl = 'no';
  
  if (cbSNL.getValue()) {
    snl='yes';
  }
  
  storeCourses.removeAll();
  gridCourses.enable();
  gridCourses.show();
  storeCourses.baseParams = {cmd: 'crsep',
                             snl: snl,
                             institution: comboInstitutions.value,
                             subject: cmb.value};
  storeCourses.load({params: {start: 0,
                              limit: 25}});
  
}

function checkSNL(cbx, cbxchecked) {
  if (comboInstitutions.value != '') updateFromInstitution(comboInstitutions);
}

function clickPrintButton(button, e) {
  var url;
  var loadParams;
  
  loadParams = storeCourses.lastOptions.params;
  
    
  url = 'services/pdfGrid.ashx?institution=' + loadParams.institution;
  url += '&subject=' + loadParams.subject;
  url += '&snl=' + loadParams.snl;
  url += '&sort=' + loadParams.sort;
  url += '&dir=' + loadParams.dir;
  url += '&srcinst=' + storeInstitutions.getById(loadParams.institution).get('DESCR');
  window.open(url)
}

function fixIllinoisOnLoad(s, r, o) {
   var rToRemove;
   
   rToRemove = s.getById('IL');
   s.remove(rToRemove);
   s.insert(0, rToRemove);
}