
var CR = "\n";
var monthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var dayNames = new Array('S','M','T','W','R','F','S');
var daysInMonth = new Array(31,29,31,30,31,30,31,31,30,31,30,31);

var now = new Date();
var iDate = now.getDate();
var iDay = now.getDay();
var sDay = dayNames[iDay];
var iYear = now.getFullYear();
var iMonth = now.getMonth();
var sMonth = monthNames[iMonth];
var browser='ie';
initToolTips();




//Makes the locations for the list of courses at a given location
function makeListLocations(){
   var sLocations = "Restrict To: <select onchange=\"restrictListLoc(this)\" name=locationid id=locationid><option value='all'>&#60;select location&#62;</option><option value='all'>All</option>";
   var aDistLoc = new Array();

   
   // New Code
   for (var x = 0; x < classdays.length; x++)
   {
      var splitValues = classdays[x].split("||");
      if( !inArray(splitValues[2], aDistLoc) )
      {
         sLocations += "<option value=\"" + splitValues[2] + "\">" + splitValues[2] + "</option>";
         aDistLoc[ aDistLoc.length ] = splitValues[2];
      }  
   }
   
   sLocations = sLocations + "</select>";
   return sLocations;
   
}

function inArray ( strSearch, aArray)
{
	for(i =0; i < aArray.length; i++)
	{
		if ( strSearch == aArray[i] )
		{
			return true;
		}
	}
	return false;
}

//Restricts the locations based on the user's choice as far as the drop down list goes
function restrictListLoc(loc)
{
   if(loc.value == "all")
   {
      showAllCalList('calendar1');
   }
   else
   {
      showCalList('calendar1', loc.value);
   }
}

//Shows a filtered list based on a location
function showCalList(div, loc)
{
   var html = "";
   html = makeListLocations();
   html = html.replace("<option value=\"" + loc + "\">" + loc + "</option>", "<option SELECTED value=\"" + loc + "\">" + loc + "</option>");
   html += "<table border=\"0\"><tr><td><br>";
   var prevLoc = "";
   var x = 0;
   
   for (x = 0; x < classdays.length; x++)
   {
      var splitValues = classdays[x].split("||");
      if(splitValues[2] == loc)
      {
         //if we're at a new location then put a divider
         /*for (var y = 0; y < splitValues.length; y++)
         {
            alert("splitValues[" + y + "]: " + splitValues[y]);
         }*/
         var id = splitValues[4];
         var text = splitValues[3];
         if (LMSQueryString('ClassID') == id) {
             html += "<input checked type=\"radio\" id=\"rclassid\" " + "name=\"rclassid\" value=\"" + id + "\">";
         } else {
             html += "<input type=\"radio\" id=\"rclassid\" " + "name=\"rclassid\" value=\"" + id + "\">";
         }
         html += text + "</input><br>";
         prevLoc = splitValues[2];
      }
   }
   html += "</td></tr></table>";
   document.getElementById(div).innerHTML = html;
}

//Shows the complete list of locations and courses
function showAllCalList(div)
{

   var html = "";
   html = makeListLocations();
   html = html.replace("<option value='all'>All</option>", "<option SELECTED value='all'>All</option>");
   html += "<table border=\"0\"><tr><td><br>";
   var prevLoc = "";
   var x = 0;
   for (x = 0; x < classdays.length; x++)
   {
      var splitValues = classdays[x].split("||");
      
      /* if we're at a new location then put a divider
      if(splitValues[2] != prevLoc && x != 0)
      {
         html += "<hr>";
      }
      }*/
      
      /*for (var y = 0; y < splitValues.length; y++)
      {
         alert("splitValues[" + y + "]: " + splitValues[y]);
      }*/
      var id = splitValues[4];
      var text = splitValues[3];
      if (LMSQueryString('ClassID') == id) {
          html += "<input checked type=\"radio\" id=\"rclassid\" " + "name=\"rclassid\" value=\"" + id + "\">";
      } else {
          html += "<input type=\"radio\" id=\"rclassid\" " + "name=\"rclassid\" value=\"" + id + "\">";
      }
      html += text + "</input><br>";
      prevLoc = splitValues[2];
   }
   html += "</td></tr></table>";
   document.getElementById(div).innerHTML = html;
}

//Shows the complete list of locations and courses
function showMDSAllCalList(div)
{

   var html = "<table border=\"0\"><tr><td><br>";
   var prevLoc = "";
   var x = 0;
   for (x = 0; x < classdays.length; x++)
   {
      var splitValues = classdays[x].split("||");
      
      /* if we're at a new location then put a divider
      if(splitValues[2] != prevLoc && x != 0)
      {
         html += "<hr>";
      }
      }*/
      
      /*for (var y = 0; y < splitValues.length; y++)
      {
         alert("splitValues[" + y + "]: " + splitValues[y]);
      }*/
      var id = splitValues[4];
      var text = splitValues[3];
      html += "<input type=\"radio\" id=\"rclassid\" " + "name=\"rclassid\" value=\"" + id + "\">";
      html += text + "</input><br>";
      prevLoc = splitValues[2];
   }
   html += "</td></tr></table>";
   document.getElementById(div).innerHTML = html;
}






/*******************************************
 * Function to create both calendars
 *******************************************/
 
function showCalendar(sDiv){
   var sCalendarTable;
   
   sCalendarTable = "<table class=\"outerTable\" cellspacing=\"5\"><tr><TD colspan=2>"
   sCalendarTable = sCalendarTable + makeLocations() + "</td></tr><tr><td valign=\"top\">"
   sCalendarTable = sCalendarTable + makeCalendar(sDiv,'1') + "</td><td valign=\"top\">";
   sCalendarTable = sCalendarTable + makeCalendar(sDiv,'2') + "</td></tr></table>";

   document.getElementById(sDiv).innerHTML = sCalendarTable ;
   document.getElementById(sDiv).style.visibility = "visible";
   
   setActiveDates();
}

/*******************************************
 * Function to create the html for a calendar
 * view. iCal defines if it is the first or 
 * second calendar
 *******************************************/
function makeCalendar(sDiv,iCal){   
   if(iCal == 2){
      if(iMonth == 11){
         iMonth=0;
         iYear = iYear + 1;
      }else{
         iMonth = iMonth + 1;
      }
      sMonth = monthNames[iMonth];
   }
   
   var sTable;
   if(iCal == 1){
      sTable =  "<table border=0 cellspacing=0 cellpadding=0><tr>"+CR+
                "<td colspan=7><table border=0 cellspacing=0 cellpadding=0><tr>"+
                "<td class=\"buttonCell\"><a href=\"javascript:prevMonth('"+sDiv+"');\"><img src=\"/images/classcalendar/arrowLeft.gif\" border=0></a></td>"+CR+
                "<td class=\"monthCell\" colspan=6 align=center>"+sMonth+" "+iYear+"</td>"+CR+
                "</tr></table></td></tr><tr class=\"daysRow\">"+CR;
   }else{
      sTable = "<table border=0 cellspacing=0 cellpadding=0><tr>"+CR+
                "<td colspan=7><table border=0 cellspacing=0 cellpadding=0><tr>"+
                "<td class=\"monthCell\" colspan=6 align=center>"+sMonth+" "+iYear+"</td>"+CR+
                "<td class=\"buttonCell\"><a href=\"javascript:nextMonth('"+sDiv+"');\"><img src=\"/images/classcalendar/arrowRight.gif\" border=0></a></td>"+CR+
                "</tr></table></td></tr><tr class=\"daysRow\">"+CR;
   }  

   for (var i=0; i<dayNames.length; i++){
      sTable = sTable + "<td class=\"daysCell\">" + dayNames[i] + "</td>";
      colCount++;
   }

   sTable = sTable + "<tr class=\"datesRow\">";

   var colCount = 0;
   var oFirst = new Date(iYear,iMonth,1);
   var iFirstDay = oFirst.getDay();
   while (colCount != iFirstDay){
      sTable = sTable + "<td class=\"datesCell\">&nbsp;</td>";
      colCount++;
   }
   for (var i=1; i<=daysInMonth[iMonth]; i++){
      if (colCount == 7){
         sTable = sTable + "</tr>"+CR+"<tr class=\"datesRow\">";
         colCount = 0;
      }
      
      sTable = sTable + "<td ><div class=\"datesCell\" onclick='changeCalText("+(parseInt(iMonth)+1)+i+iYear+")' id="+(parseInt(iMonth)+1)+i+iYear+">" + i + "</div>";
      
      colCount++;
   }
   while (colCount != 7){
      sTable = sTable + "<td class=\"datesCell\">&nbsp;</td>";
      colCount++;
   }

   sTable = sTable + "</tr>"+CR;
   sTable = sTable + "</table>";
   
   return sTable;
}

/*******************************************
 * Function to create location drop down
 *******************************************/

function makeLocations(){
   var oHash = {};
   var sLocations = "Restrict To: <select onchange=\"restrictLoc(this)\" name=locationid id=locationid><option value='all'>&#60;select location&#62;</option>";
   for(var i=0;i<classdays.length;i++){
      var sLoc = classdays[i].split("||");
      oHash[(sLoc[1])] = sLoc[2];
   }
   
   for (var n in oHash) {
    sLocations = sLocations + "<option value='" + n + "'>"+oHash[n]+"</option>";
   }

   
   sLocations = sLocations + "</select>";
   return sLocations;
}

/*******************************************
 * Function to decrease the caledar month(s)
 *******************************************/
function setActiveDates(){
   var oDate;
   for(var i=0;i<classdays.length;i++){
      oDate = cleanDate(classdays[i]);
      try{
         setInstance(oDate);
      }catch(er){}
   }
}

/*******************************************
 * Function to decrease the caledar month(s)
 *******************************************/
function prevMonth(sDiv){
   iMonth = iMonth -3;
   if (iMonth==-1){
      iMonth=11;
      iYear--;
   }else if(iMonth==-2){
      iMonth=10;
      iYear--;
   }else if(iMonth==-3){
      iMonth=9;
      iYear--;
   }
   sMonth = monthNames[iMonth];
   showCalendar(sDiv);
}

/*******************************************
 * Function to increment the caledar month(s)
 *******************************************/
function nextMonth(sDiv){
   iMonth++;
   if (iMonth==12){
      iMonth=0;
      iYear++;
   }
   sMonth = monthNames[iMonth];
   showCalendar(sDiv);
}

/*******************************************
 * Function to display class text
 *******************************************/
function changeCalText(oDay){
   var sValue=getRadioText(oDay);
   if(sValue.length > 0)
      //document.getElementById("calendartext").innerHTML = "<form name=df>"+sValue+"</form>";
      document.getElementById("calendartext").innerHTML = sValue;
 
   setClass();
}

/*******************************************
 * Function to combine text from a selected
 * date and location id with radio buttons.
 *******************************************/
function getRadioText(vValue){
   var oDate;
   var bCheck = false;
   var oClassText="";
   var oLoc = document.getElementById("locationid").value;
   for(var i=0;i<classdays.length;i++){
      if(oLoc != 'all'){
         oDate = cleanDateLoc(classdays[i],oLoc);
      }else{
         oDate = cleanDate(classdays[i]);
      }
      
      if((oDate != -1) && (oDate == vValue)){
         if(bCheck == false){
            oClassText=oClassText + "<input type=radio onclick=\"setClass()\" id=rclassid name=rclassid value='" + cleanClass(classdays[i]) + "' checked>&nbsp;" + cleanText(classdays[i]) + "<BR>";
            bCheck = true;
         }else{
            oClassText=oClassText + "<input type=radio onclick=\"setClass()\" id=rclassid name=rclassid value='" + cleanClass(classdays[i]) + "'>&nbsp;" + cleanText(classdays[i]) + "<BR>";
         }
      }
   }
   return oClassText;
}


/*******************************************
 * Function to combine text from a selected
 * date and location id to be display in a
 * tooltip.
 *******************************************/
function getToolTipText(vValue){
   var oDate;
   var bCheck = false;
   var oClassText="";
   var oLoc = document.getElementById("locationid").value;
   for(var i=0;i<classdays.length;i++){
      if(oLoc != 'all'){
         oDate = cleanDateLoc(classdays[i],oLoc);
      }else{
         oDate = cleanDate(classdays[i]);
      }
      
      if((oDate != -1) && (oDate == vValue)){
            if(bCheck==false){
               oClassText=oClassText + cleanText(classdays[i]);
               bCheck=true;
            }else{
               oClassText=oClassText + "\n" + cleanText(classdays[i]);
            }
      }
   }
   return oClassText;
}

/*******************************************
 * Function to return date for class
 *******************************************/
function cleanDate(vText){
   
   var sClass = vText.split("||");
   
   var vTemp = new Date(sClass[0]);
   vText = (vTemp.getMonth()+1).toString() + vTemp.getDate().toString() +  vTemp.getFullYear().toString();
   
   return vText;   
}

/*******************************************
 * Function to return class dates if equal
 *  to location id else returns -1
 *******************************************/
function cleanDateLoc(vText, vLocId){
   
   var sClass = vText.split("||");
   
   if(vLocId == sClass[1]){
      var vTemp = new Date(sClass[0]);
      vText = (vTemp.getMonth()+1).toString() + vTemp.getDate().toString() +  vTemp.getFullYear().toString();
   }else{
      vText=-1;
   }
   
   return vText;   
}

/*******************************************
 * Function to return text for a givin class
 *******************************************/
function cleanText(vText){
   var sClass = vText.split("||");
   var sReturn = sClass[3];
   return sReturn;   
}

/****************************************************
 * Function to return the class id for a givin class
 ****************************************************/
function cleanClass(vText){
   var sClass = vText.split("||");
   var sReturn = sClass[4];
   return sReturn;   
}

/*******************************************
 * Function to select dates per location id
 *******************************************/
function restrictLoc(oLoc){
   var iId = oLoc.value;
debugger;   
   if(iId == 'all'){
      setActiveDates();
   }else{
   
      //For Loop to clear current selected dates
      for(var i=0;i<classdays.length;i++){
         oDate = cleanDate(classdays[i]);
         try{
            remInstance(oDate);
         }catch(er){}
      }
      
      //For Loop to set dates red for location iId
      for(var i=0;i<classdays.length;i++){
         oDate = cleanDateLoc(classdays[i],iId);
         try{
            if(oDate != -1)
               setInstance(oDate);
         }catch(er){}
      }
   }
}

/*******************************************
 * Function used to set custom style tags to
 * show selected date
 *******************************************/
function setInstance(oDate){
   document.getElementById(oDate).style.background="#8D0000";
   document.getElementById(oDate).style.color="white";
   document.getElementById(oDate).style.cursor = 'hand'
   document.getElementById(oDate).style.height=10;
   document.getElementById(oDate).style.width=10;
   document.getElementById(oDate).title=getToolTipText(oDate);
}

/*******************************************
 * Function used to remove custom style tags 
 *******************************************/
function remInstance(oDate){
   document.getElementById(oDate).style.background="white";
   document.getElementById(oDate).style.color="black";
   document.getElementById(oDate).style.cursor = ''
   document.getElementById(oDate).title="";
}


/*******************************************
 * Function used to set hidden field with classid 
 *******************************************/
function setClass(){
   var oClass = document.forms[0].rclassid;
   if(typeof oClass.length == 'undefined'){
      document.getElementById("classid").value = oClass.value;
   }else{
      for(var i=0; i< oClass.length; i++){
         if(oClass[i].checked ==true){
            document.getElementById("classid").value = oClass[i].value
         }
      }
   }
}

// Define default delay and style for tooltips.

var ttDefDelay = 0;
var ttDefStyle = 'tooltip';

// variable holding current tooltip node.
var tooltip = null;

/* Function: 	displayToolTip
   Arguments: 	none
	Renders the current tooltip visible.
*/
function displayToolTip()
{ if(tooltip)
     { tooltip.style.display='block';
       var onTime=tooltip.getAttribute('ontime');
       if(onTime) setTimeout('LMSdeleteToolTip()',onTime);       
     }
}

/* Function: 	constructToolTip
   Arguments: 	event object
	Constructs tooltip node.
*/
function constructToolTip(e)
{ var target=e.target;
    if(target.nodeName=='#text') target=target.parentNode;
    if(target.getAttribute('tooltip'))
      { if(tooltip) LMSdeleteToolTip(e);
        tooltip = document.createElement('DIV');
        document.body.appendChild(tooltip);
        var ttClass=target.getAttribute('ttclass');
        if(! ttClass) ttClass=ttDefStyle;
        tooltip.setAttribute('class',ttClass);
        var ttOnTime=target.getAttribute('ttontime');
        if(ttOnTime) tooltip.setAttribute('ontime',ttOnTime);        
        tooltip.appendChild(
                       document.createTextNode(target.getAttribute('tooltip')));
        moveToolTip(e);
        var ttDelay=target.getAttribute('ttdelay');
        if(! ttDelay) 
            ttDelay=ttDefDelay;
        setTimeout('displayToolTip()',ttDelay);
      }    
}

/* Function: 	constructIEToolTip
   Arguments: 	event object
	Constructs Internet Explorer compatible tooltip node.
*/
function constructIEToolTip(e)
{ var target=e.srcElement;
    if(! target.getAttribute('tooltip')) return;
    if(tooltip) LMSdeleteToolTip(e);
    tooltip = document.createElement('DIV');
    document.body.appendChild(tooltip);
    var ttClass=target.getAttribute('ttclass');
    if(! ttClass) ttClass=ttDefStyle;
    tooltip.className=ttClass;
    var ttOnTime=target.getAttribute('ttontime');
    if(ttOnTime) tooltip.setAttribute('ontime',ttOnTime);        
    tooltip.appendChild(
                       document.createTextNode(target.getAttribute('tooltip')));
    moveIEToolTip(e);
    var ttDelay=target.getAttribute('ttdelay');
    if(! ttDelay) 
        ttDelay=ttDefDelay;
    setTimeout('displayToolTip()',ttDelay); 
    return;
}

/* Function: 	LMSdeleteToolTip
   Arguments: 	none
	Deletes tooltip node.
*/
function LMSdeleteToolTip()
{   if(tooltip)
      { document.body.removeChild(tooltip);
        tooltip=null;
      }
}

/* Function: 	moveToolTip
   Arguments: 	event object
	Moves tooltip to follow the mouse.
*/
function moveToolTip(e)
{   if(tooltip)
      { var dw=document.width;
        var scrollLeft=e.pageX - e.clientX;
        if(e.clientX < 0.5 * dw)
	  { tooltip.style.left=(e.pageX + 15) + 'px';
            tooltip.style.right='';
            tooltip.style.marginLeft='';
            tooltip.style.marginRight=(20 - scrollLeft) + 'px';
          }
        else
          { tooltip.style.right=(dw-(e.pageX - 45)) + 'px';
            tooltip.style.left='';
            tooltip.style.marginLeft=(scrollLeft + 20) + 'px';
            tooltip.style.marginRight='';
          }
        tooltip.style.top=(e.pageY + 5) + 'px';
      }
    return;
}


/* Function: 	moveIEToolTip
   Arguments: 	event object
	Moves tooltip to follow the mouse in Internet Explorer 
        compatible manner.
*/
function moveIEToolTip(e)
{   if(tooltip)
      { var dw=document.body.clientWidth;
        var mpX=e.clientX+document.body.scrollLeft;
        var mpY=e.clientY+document.body.scrollTop;
        if(e.clientX < 0.5 * dw)
	  { tooltip.style.left=(mpX + 15) + 'px';
            tooltip.style.right=(20 - document.body.scrollLeft) + 'px';
          }
        else
          { tooltip.style.right=(dw-(e.clientX-15)) + 'px';
            tooltip.style.left='';
          }
        tooltip.style.top=(mpY + 5) + 'px';
      }
}


/* Function: 	initIEToolTips
   Arguments: 	document node
	For Internet Explorer iterates attachs events to the node if tooltip
        attribute is defined and then iterates node children calling itself. 
*/
function initIEToolTips(node)
{   if(node.nodeType==1 && node.getAttribute('tooltip'))
      {   node.attachEvent('onmouseover',constructIEToolTip);
          node.attachEvent('onmouseout',deleteToolTip);
          node.attachEvent('onmousemove',moveIEToolTip);
      }
    for(var i=0; i<node.childNodes.length; i++)
        initIEToolTips(node.childNodes[i]);
    
}

/* Function: 	initToolTips
   Arguments: 	none
	If browser is Internet Explorer calls IE specific initialization
        function, otherwise adds event listeners to the document. 
*/
function initToolTips()
{   if(browser=='ie')
      { initIEToolTips(document.body);
      }
    else
      { document.addEventListener('mouseover',constructToolTip,true);
        document.addEventListener('mouseout',deleteToolTip,true);
        document.addEventListener('mousemove',moveToolTip,true);
      }
}
