
/******************************************************************
* File: shedule.js
* Encapsulates Functionality for display/manipulation of calendar/timezone/programming block
* information.
* create date: 06/01/2005
* authors:s
* 1. Greg Jones gjones@turner.com
*******************************************************************/
/*******************************************************************
* Define timeZone Here. Currently Using EASTERN as Base Time.
* For examples of use, see /schedule/daily.jsp
* todo: modify to use UTC as base time
*
********************************************************************/
var EASTERN = 0;
//EASTERN IS THE BASE TIME
//The integer value is EASTERN - timezone. ex EASTERN - CENTRAL = -1
var CENTRAL = -1;
var MOUNTAIN = -2;
var PACIFIC = -3;
var ATLANTIC = +1;
var HAWAII = -5;
/*******************************************************************
* function: TimeZone: encapsulates TimeZone logic
* params : id unique internal identifier for this object
*	name - Name of this TimeZone ex. "EST"	
* returns : newly created TimeZone object
*
********************************************************************/
function TimeZone(id,name)
{
this.id = id;
this.name = name;
this.lookup = function(identifier)
{
for(ii=0; ii < TIMEZONES.length;ii++)
{
zone = TIMEZONES[ii];
if(zone.id == identifier)
{
DEFAULT_ZONE = zone;	
break;
}
}
return(DEFAULT_ZONE);
}
this.reverseLookup = function(name)
{
for(ii=0; ii < TIMEZONES.length;ii++)
{
zone = TIMEZONES[ii];
if(zone.name == name)
{
DEFAULT_ZONE = zone;	
break;
}
}
return(DEFAULT_ZONE);
}
return(this);
}
/*******************************************************************
* function: Block: encapsulates Block logic
* params : label - display label for this time block
*	block - time span of block in civillian format ex. 11:00 AM - 12:00 PM
* theZone - time Zone for localization 
* returns : newly created Block object
*
********************************************************************/
function Block(label,block,theZone,programSpansXBlocks)
{
this.label = label;
this.block = block;
this.begin_and_end_time = block.split("-");
this.cStartTime = this.begin_and_end_time[0];
this.cEndTime = this.begin_and_end_time[1];
this.mStartTime ="";
this.mEndTime="";
this.zone=theZone;
this.programSpansXBlocks = programSpansXBlocks != null ? programSpansXBlocks : null;
this.add = function(nblock)
{
_PROGRAM_BLOCKS[_PROGRAM_BLOCKS.length] = nblock;
}
this.write = function()
{
for(uu=0;uu< _PROGRAM_BLOCKS.length;uu++)
{
b = _PROGRAM_BLOCKS[uu];
b.mStartTime = convertCivillianToMilitaryTime(b.cStartTime);
b.mEndTime = convertCivillianToMilitaryTime(b.cEndTime);
b.cStartTime = localizeCivillianTime(b.cStartTime,b.zone);
b.cEndTime = localizeCivillianTime(b.cEndTime,b.zone);
z ="<option value='" 
+ b.mStartTime
+ "-" 
+ b.mEndTime
+ "'>\n"
+ b.label + " " 
+ b.cStartTime	
+ " - " 
+ b.cEndTime
+ "</option>\n";
//alert(z);
document.write(z);
}
}
}
var DEFAULT_BLOCK = new Block("Programming Day","6:00 AM - 6:00 AM",EASTERN);
var _PROGRAM_BLOCKS = new Array();
var DEFAULT_ZONE = new TimeZone(EASTERN,"EST");
var TIMEZONES = new Array();
TIMEZONES[TIMEZONES.length] = new TimeZone(EASTERN,"EST");
TIMEZONES[TIMEZONES.length] = new TimeZone(CENTRAL,"CST");
TIMEZONES[TIMEZONES.length] = new TimeZone(MOUNTAIN,"MST");
TIMEZONES[TIMEZONES.length] = new TimeZone(PACIFIC,"PST");
TIMEZONES[TIMEZONES.length] = new TimeZone(ATLANTIC,"AST");
TIMEZONES[TIMEZONES.length] = new TimeZone(HAWAII,"HST");
/********************************************************
* Methods below are used to perform page actions on user input
* Tracing can be toggled on/off by calling enableTracing() with 
* value param equal to true or false...see schedule/daily.jsp
*********************************************************/
function _highlightCalendarWeekSelectionHandle()
{
}
/********************************************************
* function _onCalendarClickDayHandle : sets start and end date after user
* the calendar
* Also defaults day to todays date if user accesses a date that is greater than 7 days ago
* or greater than 14 days in the future
* Override this function for specialized processing...ie. week format
* params : day clicked passed in mm/dd/yyyy format.
* returns : none
* branches : _onGO()
********************************************************/
var _onCalendarClickDayHandle = function(day)
{
//log.debug("Called _onCalendarClickDay with arg(s) day =" + day);
if(day)
{
if(isValidDate(day))
{
document.forms[_scheduleForm].startDate.value = day;
document.forms[_scheduleForm].endDate.value = day;
_onGO();
}
}
//Override This Function to Do Specialized Processing When CalendarDay is Clicked
}
function formatDate(date) {
mymonth = arrM[date.getMonth()];
var myweekday = date.getDate();
return (mymonth + " " + myweekday);
}
function printWeek() {
/** Variable Now defined in schedule_common.jspf */
var nowDayOfWeek = now.getDay();
var nowDay = now.getDate();
var nowMonth = now.getMonth();
var nowYear = now.getYear();
nowYear += (nowYear < 2000) ? 1900 : 0;
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
document.write(formatDate(weekStartDate) + " - " + formatDate(weekEndDate));
}
function _setBeginningOfWeek(url)
{
//Get Start of Week to Display Week Long List
thisWeek = new Date();
thisWeek.setDate( thisWeek.getDate() - (thisWeek.getDay()+7-0)%7 );
window.location = url + "?oid="+ (thisWeek.getMonth() <= 11 ? thisWeek.getMonth() +1 : thisWeek.getMonth()) + "/" + thisWeek.getDate() + "/" + thisWeek.getFullYear() + "&startDate=" + (thisWeek.getMonth() <= 11 ? thisWeek.getMonth() +1 : thisWeek.getMonth()) + "/" + thisWeek.getDate() + "/" + thisWeek.getFullYear();
}
/********************************************************
* function _onGO() : submits the page after user input
* params: none
********************************************************/
function _onGO()
{
//log.debug("Called Form Submit()");
//Look Diff For Caching Server
document.forms[_scheduleForm].action += "?oid="
+document.forms[_scheduleForm].startDate.value
+document.forms[_scheduleForm].timeZone.value
+document.forms[_scheduleForm].block.selectedIndex;
document.forms[_scheduleForm].submit();
}
/********************************************************
* function isValidDate : determines if the date is valid according to busines rules
* the calendar
* Also defaults day to todays date if user accesses a date that is greater than 7 days ago
* or greater than 14 days in the future
* params : day clicked passed in mm/dd/yyyy format.
* returns : none
*
********************************************************/
function isValidDate(day, baseDay)
{
today = baseDay == null ? new Date() : new Date(baseDay);
schedule_date = new Date(day);
diffXY = Math.round(today - schedule_date)/86400000;
diffXYPrecision = diffXY.toPrecision(2);
todayAsString = (today.getMonth() + 1) + "/" + today.getDate() + "/" + today.getFullYear();
delta = diffDates(todayAsString,day);
lookBack = delta >=0 ? true : false;
lookForward = delta < 0 ?true : false;	
if(lookBack && Math.abs(delta) > 7 || lookForward && Math.abs(delta) > 14)
{
alert("Programming Schedule is only available for 1 week in the past or 2 weeks into the future.");
return false;
}
return true;
}
/********************************************************
* function diffDates : returns difference two between dates as fraction ex. 1.0, 1.5, .49
*
* params : d1 date string in mm/dd/yyyy format (base date)
* d2 second date in mm/dd/yyyy format
* returns : difference in dates as number
*
********************************************************/
function diffDates(d1, d2)
{
//log.debug("Called diffDates with arg(s) d1 =" + d1 + " d2 =" + d2);
base_date = new Date(d1);
compare_date = new Date(d2);
diffXY = Math.round(base_date - compare_date)/86400000;
diffXYPrecision = diffXY.toPrecision(2);
//log.debug(Math.abs(diffXYPrecision));
return(diffXYPrecision);
}
/********************************************************
* function _onTimeZoneClick : sets timeZone based on user input
* params : timeZone user chose from the page
* returns : none
* branches : _onGO()
*********************************************************/
function _onTimeZoneClick(tz)
{
//log.debug("Called _onTimeZone with arg(s) timezone =" + DEFAULT_ZONE.lookup(tz).name);
exitWithoutSubmit = true;
document.forms[_scheduleForm].timeZone.value = DEFAULT_ZONE.lookup(tz).name;
//Call _onSelectBlockChange To Record New Time Block in case user chose block and then new time zone without 
//hitting GO button
_onSelectBlockChange(document.forms[_scheduleForm].block[document.forms[_scheduleForm].block.selectedIndex].value,exitWithoutSubmit)
_onGO();
}
function getTimeZone()
{
return(document.forms[_scheduleForm].timeZone.value);
}
/********************************************************
* function _onSelectBlockChange : sets the block of hours based on user input
* - the unique id can be passed by the select-box
* params : time block (unique id for the time block) user chose from the page
* returns : none
* branches : _onGO()
*********************************************************/
function _onSelectBlockChange(block, exitWithoutSubmit)
{
//log.debug("Called _onSelectBlockChange with arg(s) block =" + block);
time_span = block.split("-");
document.forms[_scheduleForm].startTime.value = time_span[0];
document.forms[_scheduleForm].endTime.value = time_span[1];
//log.debug("Called _onSelectBlockChange startTime =" 
//	+ document.forms[_scheduleForm].startTime.value 
//	+ " endTime = " + document.forms[_scheduleForm].endTime.value);
if(exitWithoutSubmit == null || exitWithoutSubmit != true)
_onGO();
}
/********************************************************
* function DayObj : encapsulates DayObj methods and attributes
* params : id - the unique object for the DayObj
* val - date in mm/dd/yyyy format
* returns : DayObj
* branches : none
*********************************************************/
var dayArr = new Array();
function DayObj(id,val)
{
this.id = id;
this.val = val;
this.getDay = function(id)
{
obj = "";
//log.info("Looking for date for this id " + id);
for(i=0; i < dayArr.length; i++)
{
if(dayArr[i].id == id)
{
obj = dayArr[i];
//log.debug("Found this date " + obj.val + " for id " + id); 
break;
}
}
return(obj.val);
}
this.setDate = function(id,date)
{
found = false;
//log.info("Trying to Set date for id " + id + " date is " + date);
for(i=0; i < dayArr.length; i++)
{
if(dayArr[i].id == id)
{
dayArr[i].val = date;
found = true;
if(i > 10 && i < 14)
break;
}
}
if(!found)
{
dayArr[dayArr.length] = new DayObj(id,date);
//log.info("Successfully Set the date to " + dayArr[dayArr.length -1].val + " for id " + id); 
}
}
return(this);
}
var dayObj = new DayObj("finder","finder");
/********************************************************
* function previousWeek : moves a javascript date object back in time 1 week on each call
* params : dateIn - date object
* format - flag to determine if return should be in mm/dd/yyyy format or a date object
* returns : date in javascript date object format or date in mm/dd/yyyy format
* branches : 
*********************************************************/
function previousWeek(dateIn,days,format)
{ 
//log.debug("nextDay func was called with params dateIn " + dateIn.getDate() + " days " + days);
dateIn.setDate(dateIn.getDate() -7);
if(format)
return((dateIn.getMonth() + 1) + "/" + dateIn.getDate() + "/" + dateIn.getFullYear());
else
return(dateIn);
}
/********************************************************
* function nextWeek : moves a javascript date object forward in time by X days
* params : dateIn - date object
* days - number of days to move the date object forward
* format - flag to determine if return should be in mm/dd/yyyy format or a date object
* returns : date in javascript date object format or date in mm/dd/yyyy format
* branches : 
*********************************************************/
function nextWeek(dateIn,days,format)
{
//log.debug("nextDay func was called with params dateIn " + dateIn.getDate() + " days " + days);
dateIn.setDate(dateIn.getDate()+(7+ days-dateIn.getDay())%7)
dateIn.setDate(dateIn.getDate()-(7+dateIn.getDay()-days)%7)
if(format)
return((dateIn.getMonth() +1) + "/" + dateIn.getDate() + "/" + dateIn.getFullYear());
else
return(dateIn);
}
/********************************************************
* function maxDays : determines the maximum number of days in a given month
* params : mm - month to determine max days
* yyyy - year to determine if it is a leap year
* returns : days in the month
* branches : 
*********************************************************/
function maxDays(mm, yyyy){
if((mm == 3) || (mm == 5) || (mm == 8) || (mm == 10)){
mDay = 30;
}
else{
mDay = 31
if(mm == 1){
if (yyyy/4 - parseInt(yyyy/4) != 0){
mDay = 28
}
else{
mDay = 29
}
}
}
return mDay;
}
function changeBg(id){
if (eval(id).style.backgroundColor != "yellow"){
eval(id).style.backgroundColor = "yellow"
}
else{
eval(id).style.backgroundColor = "#ffffff"
}
}
/**
* Used By Weekly.jsp to determine chosenWeek to highlight
***/
var chosenWeekRange = new Array();
var week1 = new Array(1,7);
var week2 = new Array(8,14);
var week3 = new Array(15,21);
var week4 = new Array(22,31);
//var week5 = new Array(29,31);
/********************************************************
* function writeCalendar : draws a html calendar to screen
* params : numOfMonths - number of Months to draw to screen
* 
* returns : draws calendar inline
* branches : none 
*********************************************************/
function writeCalendar(numOfMonths,highlightCalendarNumber){
//Enables show/hide of Month >> incrementer
var navMonth = false;
var daysInMonth = 1
var text = "<table border=0><tr>";
//log.debug("Number of Months = " + numOfMonths);
for(_wcIndex = 0; _wcIndex < numOfMonths; _wcIndex++)
{
var workingStorageDt = new Date();
workingStorageDt.setDate(now.getDate());
//log.debug("workingStorageDt is cool" + workingStorageDt.getDate());
//now variable is created and initialized in schedule_common.jsp
var dd = workingStorageDt.getDate();
//Set Date forward by Number Of Months
workingStorageDt.setMonth(now.getMonth() + _wcIndex);
var mm = workingStorageDt.getMonth();
var dow = workingStorageDt.getDay()
var yyyy = workingStorageDt.getFullYear()
//log.debug("Month is " + mm);
var arrY = new Array()
for (ii=0;ii<=4;ii++){
arrY[ii] = yyyy - 2 + ii
}
var arrD = new Array("S","M","T","W","T","F","S")
text += "<TD id='calendar' valign='top' >"
text += "<table width='122' border='0' cellpadding='0' cellspacing='0'>"
text += "<tr><td >"
text += "<table cellpadding='0' align='center' cellspacing='0' border='0'><tr>"
text += "<td colspan='2' class='month'>"
if(navMonth)
text += "<a href='javascript:_onCalendarClickDayHandle(rollMonth(\"" + (workingStorageDt.getMonth() +1)+ "/" + workingStorageDt.getDate() + "/" + workingStorageDt.getFullYear() +"\",-1))'>&lt;&lt;</a>" 
text += arrM[mm]
if(navMonth)
text += "<a href='javascript:_onCalendarClickDayHandle(rollMonth(\"" +( workingStorageDt.getMonth() + 1) + "/" + workingStorageDt.getDate() + "/" + workingStorageDt.getFullYear() +"\",1))'>&gt;&gt;</a>"
text += "<input type='hidden' name='selMonth" + _wcIndex + "' value='" + mm + "' onChange='changeCal()'>"
text += "<input type='hidden' name='selYear" + _wcIndex + "' value='" + arrY[2] + "' onChange='changeCal()'>"
text += "</td>"
text += "</tr></table>"
text += "</td></tr>"
text += "<tr><td>"
text += "<table align='center' border=0 cellpadding='0' width='130' cellspacing='0'>"
text += "<tr>"
for (ii=0;ii<=6;ii++){
text += "<td class='weekDay' >" + arrD[ii] + "</td>"
}
text += "</tr>"
aa = 0
highlight_class = "";
for (kk=0;kk<=5;kk++){
text += "<tr>"
for (ii=0;ii<=6;ii++){
text += "<td align=center id=sp" + _wcIndex + aa + " onClick='_onCalendarClickDayHandle(dayObj.getDay(this.id))'>" + daysInMonth + "</div></td>"
aa += 1
}
text += "</tr>"
}
text += "</table>"
text += "</td></tr>"
text += "</table>"
text += "</TD>"
document.write(text)
text = "";
if(highlightCalendarNumber == null)
{
changeCal(_wcIndex, _wcIndex < 1,workingStorageDt);
}else {
changeCal(_wcIndex, _wcIndex ==( highlightCalendarNumber - 1),workingStorageDt);
}
//End Loop
}
document.write("</TR></TABLE>");
}
/*****************************************************************************
* function roleMonth : rolls the calendar back/forward 1 calendar month to beginning of month
* params : dateAsString - date in mm/dd/yyyy format
* increment - number of months to roll / forward or back
* returns : date string in dd/mm/yyyy format
* branches : notes
******************************************************************************/
function rollMonth(dateAsString,numOfMonths)
{
//log.debug("Entering rollMonth with Date " + dateAsString);
var calendar = new Date(dateAsString);
dateOutAsString = "";
calendar.setDate(1);
calendar.setMonth(calendar.getMonth() + numOfMonths +1);
dateOutAsString = calendar.getMonth() + "/" + calendar.getDate() + "/" + calendar.getFullYear();
//log.debug("Leaving rollMonth with date " + dateOutAsString);
return(dateOutAsString);
}
/**************************************************************************
* function changeCal : fills in calendar dates and blocks out invalid dates for the month
* params : calendarNumber - id of current calendar being computed 
* highlightDay - flag to determine if current date should be highlighted
* returns : date in javascript date object format or date in mm/dd/yyyy format
* branches : none
*********************************************************/
function changeCal(calendarNumber, highlightDay,workingStorage){
var dd = now.getDate() > 30 ?(32 - new Date(workingStorage.getFullYear(), workingStorage.getMonth(), 32).getDate()) : now.getDate();
var mm = workingStorage.getMonth()
var dow = workingStorage.getDay();
var yyyy = workingStorage.getFullYear()
var currM = parseInt(eval("document.forms[_scheduleForm].selMonth" + calendarNumber + ".value"));
var prevM
if (currM!=0){
prevM = currM - 1
}
else{
prevM = 11
}
var currY = parseInt(eval("document.forms[_scheduleForm].selYear" + calendarNumber + ".value"));
var mmyyyy = new Date()
mmyyyy.setFullYear(currY)
mmyyyy.setMonth(currM)
mmyyyy.setDate(1)
var day1 = mmyyyy.getDay()
if (day1 == 0){
day1 = 7
}
var arrN = new Array(41)
var aa
for (ii=0;ii<day1;ii++){
arrN[ii] = maxDays((prevM),currY) - day1 + ii + 1
}
aa = 1
for (ii=day1;ii<=day1+maxDays(currM,currY)-1;ii++){
arrN[ii] = aa
aa += 1
}
aa = 1
for (ii=day1+maxDays(currM,currY);ii<=41;ii++){
arrN[ii] = aa
aa += 1
}
for (ii=0;ii<=41;ii++){
//eval("sp"+ii).style.backgroundColor = "#FFFFFF"
}
var dCount = 0
for (ii=0;ii<=41;ii++){
if (((ii<7)&&(arrN[ii]>20))||((ii>27)&&(arrN[ii]<20))){
//Hide Bad Days
eval("sp" + calendarNumber +ii).innerHTML = "";
//eval("sp"+ii).className = "blankDays"
}
else{
dayObj.setDate("sp" + calendarNumber + ii,(mm + 1) + "/" + arrN[ii] + "/" + yyyy);
eval("sp"+ calendarNumber + ii).innerHTML = arrN[ii]
if ((dCount==0)||(dCount==6)){
eval("sp"+ calendarNumber + ii).className = "c1 day"
}
else{
eval("sp"+ calendarNumber + ii).className = "c2 day"
}
//Highlight Week overrides this injection point (weekly.jsp)
//Injection Point for Selecting Days from Calendar
highlightDay&&_highlightCalendarWeekSelectionHandle(arrN[ii],dd,eval("sp"+ calendarNumber + ii));
if ((arrN[ii]==dd)&&(mm==currM)&&(yyyy==currY) && highlightDay == true){
eval("sp"+ calendarNumber + ii).className="selectedDay"
//log.debug("Week for Selected Day is " + chosenWeek);
}
}
dCount += 1
if (dCount>6){
dCount=0
}
}
}
/********************************************************
* function _initSelect : initializes a select-box to a given value
* params : selectobj - select-box javascript object
* v - the value to set
* returns : none
* branches :none 
*********************************************************/
function _initSelect(selectobj,v)
{
elems = selectobj.options;
for(ii=0; ii < elems.length; ii++)
{
if(elems[ii].value == v)
{
elems[ii].selected = true;
break;
}
}
}
/***** Various Time Functions ********************/
function localizeCivillianTime(cTime,timeZone)
{
timeout = "";
_mTime = convertCivillianToMilitaryTime(cTime);
_mTime = localizeMilitaryTime(_mTime,timeZone);
timeout = convertMilitaryToCivillianTime(_mTime);
return(timeout);
}
/********************************************************
* function convertMilitaryToCivillianTime : converts Military time string
* to Civillian Time String
* params : mTime - time string in H24:MM a format ex. 1500
* 
* returns : time converted to civillian format ex. 3:00 PM
* branches : none 
*********************************************************/
function convertMilitaryToCivillianTime(mTime)
{
//mTime = 1545 or 3:40 PM
//log.debug("Entering _convertMilitaryToCivillianTime with time " + mTime); 
//log.debug("parseFloat(mTime) / 100)" + parseFloat(mTime) / 100); 
if(parseFloat(mTime) > 0)
hour_and_minute = parseFloat(mTime / 100); //15.4
else
hour_and_minute = 12;
//log.debug("hour_and_minute after first conversion " + hour_and_minute);
AM_PM = hour_and_minute >= 12 && hour_and_minute != 24 ? " PM" : " AM"; //PM
hour_and_minute = hour_and_minute > 12 ? hour_and_minute - 12 : hour_and_minute; //3.4	
//log.debug("hour_and_minute after second conversion " + hour_and_minute);
hour_and_minute_as_string = ""+ hour_and_minute;
hour_and_minute_as_string += hour_and_minute_as_string.indexOf('.') > -1 ? "" : ".";
hour_and_minute_as_string = hour_and_minute_as_string.replace('.',':'); //3:4
//log.debug("hour_and_minute after third conversion " + hour_and_minute_as_string);
hour_part = hour_and_minute_as_string.substring(0,hour_and_minute_as_string.indexOf(':'));
minute_part = hour_and_minute_as_string.substring(hour_and_minute_as_string.indexOf(':')+1);
hour_and_minute_as_string = hour_part + ":" + pad_with_zeros(minute_part,2); //3:40
//log.debug("hour_and_minute after fourth conversion (pad with zeroes if necessary " + hour_and_minute_as_string);
hour_and_minute_as_string += AM_PM; //3:40 PM
//log.debug("hour_and_minute after last conversion " + hour_and_minute_as_string);
return(hour_and_minute_as_string);
}
/********************************************************
* function localizeMilitaryTime : localizes time
* 
* params : mTime - time string in H24 format
* timeZone - EASTERN,CENTRAL,PACIFIC,MOUNTAIN,ATLANTIC,HAWAII
* returns : localized time in military format
* branches: none 
*********************************************************/
function localizeMilitaryTime(mTime,timeZone)
{
//log.debug("localizeTime was called with params mTime= " + mTime + " timeZone=" + timeZone);
mTime = mTime.charAt(0) == '0' ? mTime.substring(1) : mTime;
time_out = parseFloat(mTime) + parseInt(timeZone * 100);
//If before MIDNIGHT and NOT NOON 
if(time_out <= 0 )
{
time_out += 2400;
}
return(time_out);	
}
/********************************************************
* function convertCivillianToMilitaryTime : converts Civillian time string
* to Military Time String
* params : cTime - time string in H12:MM a format ex. 3:00 PM
* 
* returns : time converted to military format ex. 0300
* branches : none 
*********************************************************/
function convertCivillianToMilitaryTime(cTime)
{
//log.debug("convertCivillianToMilitaryTime " + cTime);
hour_part = cTime.substring(0,cTime.indexOf(':'));
minute_part = cTime.substring(cTime.indexOf(':') +1);
AM_PM = cTime.indexOf('A') > -1 ? " AM" : " PM";
var mhour = 0;
var mtime = "";
if(AM_PM.indexOf('P') > -1)
{
if(hour_part != "12")
{
mhour = parseFloat(hour_part) + 12;
}
else {
mhour = 12;
}
} 
//When AM
else {
if(hour_part != "12")
{
mhour = parseFloat(hour_part);
}
else 
{
mhour = 24;
}
}
mhour = parseFloat(mhour * 100);
hour_and_minutes = parseFloat(mhour) + parseFloat(minute_part);
if(mhour < 1000)
{
mtime = "0" + hour_and_minutes;
} else
{
mtime = ""+ hour_and_minutes;
}
//log.debug((mtime) + " AM_PM= " + AM_PM);
return((mtime));
}
/********************************************************
* function pad_with_zeroes : pads a value to x places with appended zeroes
* params : val - the value to inspect
* padding - the number of significant digits
* returns : value with appropriate number of padded zeroes
* branches : none 
*********************************************************/
function pad_with_zeros(val,padding)
{
ret = val + "";
significant_digits = 0;
for(ii=0;ii<ret.length; ii++)
{
if(ret.charAt(ii) >= '0' && ret.charAt(ii) <='9')
{
significant_digits++;
}
}
var numberOfZeros = parseInt(padding - significant_digits);
for(ii=0; ii < numberOfZeros && numberOfZeros > 0; ii++)
{
ret += "0";
}
return(ret);
}
/********************************************************
* function writeEndTime : computes end time for a program given a start time and franchise length
* params : mTime - start time for the program
* franchiseLength - length of the show in seconds ie. 3600 seconds = 1 hour
* returns : date in javascript date object format or date in mm/dd/yyyy format
* branches : 
*********************************************************/
function writeEndTime(mTime,franchiseLength)
{
//log.debug("Clock date is " + mTime);
var t = mTime * 10;
(t = parseFloat(t/1000) );
//Append AM_PM Designator
designator = t + franchiseLength /60/60 < 12 ? " AM" : " PM";
//Test for Rollover Past NOON
designator = t + franchiseLength /60/60 >= 24 ? " AM" : designator;
//Subtract 12 from Hours
t = t >= 12 ? t - 12 : t;
//t = t.toPrecision(3);
t = t.toPrecision(2);
var temp_time = t + "";
hours = temp_time.indexOf('.') > -1 ? temp_time.substring(0,temp_time.indexOf('.')) : temp_time;
//log.debug("writeEndTime:hours " + hours);
clock = new Date();
clock.setHours(hours);
minutes = temp_time.indexOf('.') > -1 ? temp_time.substring(temp_time.indexOf('.') + 1) : "00";
minutes = pad_with_zeros(minutes,2);
//log.debug("writeEndTime:minutes " + minutes);
clock.setMinutes(minutes);
increment = franchiseLength / 60;
//log.debug("increment = franchiseLength / 60 " + (increment = franchiseLength / 60));
//log.debug("Clock get Hours and minutes " + clock.getHours() + " " + clock.getMinutes());
clock.setMinutes(clock.getMinutes() + increment);
//log.debug("Clock get Hours and minutes " + clock.getHours() + " " + clock.getMinutes());
{
if(clock.getHours() >= 12)
{	
//clock.setHours(clock.getHours() + 1);
chours = clock.getHours() > 12 ? clock.getHours() - 12 : clock.getHours();
//designator = clock.getHours() > 12 ? " PM" : " AM";
clock.setHours(chours);	
}
ct = clock.getHours() + ':' + pad_with_zeros(clock.getMinutes(),2) + designator;
alert
return(ct);
}
}
/*****************************************************************************
* function writeProgramBlockTD : Figures out the COLSPAN of PROGRAM BLOCK TD 
* Needed for cases where the program overlaps from beyond the beginning of a program block
* params : blockTime - start time for the program block
* programTime - start time of the program (may be less than program block time)
*	programLength - length of Program in Blocks (ie. 2 blocks equal 1 hour)
*	increment - value for # of minutes per block (ie. 30 or 15)
* returns : document.write of td tag with correct colspan (see schedule/weekly.jsp)
* branches : 
*********************************************************/
function writeProgramBlockTD(blockStartTime,programTime,programLength,increment,type)
{
mBlockTime = convertCivillianToMilitaryTime(blockStartTime);
mProgramTime = convertCivillianToMilitaryTime(programTime);
mProgramTime = mProgramTime.charAt(0) == '0' ? mProgramTime.substring(mProgramTime.indexOf('0') + 1) : mProgramTime; 
//alert("TNT Block Time " + mBlockTime);
if(mBlockTime.charAt(0) == '0')
{
mBlockTime = mBlockTime.substr(1);
}
//alert("Program StartTime " + mProgramTime + " Length of Program " + programLength );
//2. Convert to Seconds of the Day
mBlockHourOfDay = parseInt(parseInt(mBlockTime) / 100);
//alert("mBlockHourOfDay " + mBlockHourOfDay);
mProgramHourOfDay = parseInt(mProgramTime / 100);
mBlockMinuteOfDay = parseInt(mBlockTime) % 100;
mProgramMinuteOfDay = parseInt(mProgramTime) % 100;
//alert("Program Minute As Seconds " + parseInt(mProgramTime) / 100);
mBlockHourOfDayAsSeconds = mBlockHourOfDay + ((mBlockMinuteOfDay )/60);
mProgramHourOfDayAsSeconds = mProgramHourOfDay + ((mProgramMinuteOfDay )/60);
//alert("Program Block Hour As Seconds " + mBlockHourOfDayAsSeconds + " MINUS " + " Program Hour As Seconds " + mProgramHourOfDayAsSeconds);
overLapSeconds = (mBlockHourOfDayAsSeconds - mProgramHourOfDayAsSeconds);
minutes = (overLapSeconds * 3600);
//alert("Minutes " + minutes);
programHasUsedXBlocks = minutes/60/increment;
colspan = programLength - programHasUsedXBlocks;
//alert("Use this many columns " + colspan);
document.write('<td valign="middle" colspan="' + colspan + '" bgcolor="#ffffcc" class="text ' + type + '">');
}

