/*

This Javascript Source Code is Intellectual Property owned by FrostyLabs LLC. 
You may not do any of the following without prior written permission by FrostyLabs LLC, and not limited to:

duplicate or reuse the Service, Source Code, or any portion of it.
sell, resell, trade, or transfer ownership of the Service or any portion of it. 
take advantage of the Source Code for any profitable purposes.

This property of FrostyLabs LLC is protected through all appropriate laws.

If you have any questions please contact www.FrostyLabs.com

*/

function getCalendar(){}

// function to show the calendar in a 
// pop up window
function showCalendar(newMonth, newYear, frm){
	var leftPos=0;
        if(screen){
	    	leftPos = (screen.width/2)-140;  
          	topPos =(screen.height/2)-80;
        }
	var loc = "calendar.jsp?frm=" + frm + "&showYear="+newYear+"&showMonth="+newMonth;
	subWindow=window.open(loc,'','toolbar=no,location=no,scrollbar=no,width=280, height=200,left='+leftPos+',top='+topPos);
	subWindow.focus();
}
// returns the date from the
// calendar to the form object
function returnDate(day,objFrm){

	var y = document.CalendarStats.hdnYear.value;
	var m = document.CalendarStats.hdnMonth.value;
	m++;
	if(parseInt(m)< 10){
		m = "0" + m;
	}
	if(parseInt(day)<10){
		day = "0" + day;
	}
	var t = m + "/" + day + "/" + y;
	objFrm.value = t;
	self.close();

}

// creates the calendar
// on the html page
function buildCalendar(diffMonth, diffYear, objFrm){
var hi_select = "sub_menu_hi";

//  SET ARRAYS
var day_of_week = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
var month_of_year = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

//  DECLARE AND INITIALIZE VARIABLES
var Calendar = new Date();

var year = Calendar.getFullYear();	    // Returns year
year = year + diffYear;
var month = Calendar.getMonth();    // Returns month (0-11)

month = month + diffMonth;

if(month < 0){
	month = Math.abs(month);
	month = 12 - (month%12);
}
if(month > 11){
	month = Math.abs(month);
	month = 0 + (month%12);
}
if((year >= 100)&&(year < 1500)){
	year = year - 100 + 2000;
}else if(year < 100){
	year = 2000 + (-year + 100);
}

Calendar.setYear(year);
Calendar.setMonth(month);

var today = Calendar.getDate();    // Returns day (1-31)
var weekday = Calendar.getDay();    // Returns day (1-31)
var DAYS_OF_WEEK = 7;    // "constant" for number of days in a week
var DAYS_OF_MONTH = 31;    // "constant" for number of days in a month
var cal;    // Used for printing

Calendar.setDate(1);    // Start the calendar day at '1'
Calendar.setMonth(month);    // Start the calendar month at now

	document.write("<form name='CalendarStats'>");
	document.write("<input type='hidden' name='hdnYear' value='"+year+"'>");
	document.write("<input type='hidden' name='hdnMonth' value='"+month+"'>");
	document.write("<input type='hidden' name='frmField' value='blank'>");
	document.write("</form>");

	var LK_start = "<a class='lnk' href='#' onClick='returnDate(";
	var Fc_end = ", " + objFrm + ");'>";
	var LK_end = "</a>";

/* VARIABLES FOR FORMATTING
NOTE: You can format the 'BORDER', 'BGCOLOR', 'CELLPADDING', 'BORDERCOLOR'
      tags to customize your caledanr's look. */

var TR_start = '<TR>';
var TR_end = '</TR>';
var highlight_start = "<TD WIDTH='35'><TABLE CELLSPACING=0 BORDER=1 class='" + hi_select + "'><TR><TD WIDTH=20><B><CENTER>";
var highlight_end   = "</CENTER></b></TD></TR></TABLE>";
var TD_start = "<TD WIDTH='35'><CENTER>";
var TD_end = "</CENTER></TD>";

/* BEGIN CODE FOR CALENDAR
NOTE: You can format the 'BORDER', 'BGCOLOR', 'CELLPADDING', 'BORDERCOLOR'
tags to customize your calendar's look.*/

var tmpDOW = DAYS_OF_WEEK - 4;
var prevMonth = month - 1;
var nextMonth = month + 1;
var prevYear = diffYear;
var nextYear = diffYear;
if(prevMonth < 0){
	prevMonth = 11;
	prevYear--;
}
if(nextMonth > 11){
	nextMonth = 0;
	nextYear++;
}
cal =  "<center><TABLE BORDER=1 CELLSPACING=0 CELLPADDING=0 BORDERCOLOR=BBBBBB><TR><TD>";
cal += "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=2>" + TR_start;
cal += "<TD COLSPAN='1' class='otherMonth' size='1'>";
cal += "<a title='Go Back 10 Years' class='lnk' href='calendar.jsp?frm=" + objFrm + "&showMonth=" + eval(diffMonth - (12)) + "&showYear=" + eval(diffYear - 10)+ "'><<</a>&nbsp;&nbsp;";
cal += "<a title='Go Back 1 Year' class='lnk' href='calendar.jsp?frm=" + objFrm + "&showMonth=" + eval(diffMonth - (12)) + "&showYear=" + eval(diffYear - 1)+ "'><</a>&nbsp;&nbsp;</td>";
cal += "<td title='Go Back 1 Month' class='otherMonth'><a class='lnk' href='calendar.jsp?frm=" + objFrm + "&showMonth=" + eval(diffMonth-1) + "&showYear=" + prevYear + "'>" + month_of_year[prevMonth] + "</a></td>";
cal += "<TD class='thisMonth' COLSPAN='" +  tmpDOW + "'><CENTER><B>" + month_of_year[month] + "   " + year + "</B>" + TD_end;
cal += "<TD COLSPAN='1' class='otherMonth' size='1' align='right'>";
cal += "<a title='Go Foward 1 Month' class='lnk' href='calendar.jsp?frm=" + objFrm + "&showMonth=" + eval(diffMonth+1) + "&showYear=" + nextYear + "'>" + month_of_year[nextMonth] + "</a></td>";
cal += "<td title='Go Forward 1 Year' class='otherMonth' align='right'><a class='lnk' href='calendar.jsp?frm=" + objFrm + "&showMonth=" + eval(diffMonth) + "&showYear=" + eval(diffYear + 1) + "'>></a>&nbsp;&nbsp;";
cal += "<a title='Go Forward 10 Years' class='lnk' href='calendar.jsp?frm=" + objFrm + "&showMonth=" + eval(diffMonth) + "&showYear=" + eval(diffYear + 10) + "'>>></a></td>";
cal += TR_end;
cal += TR_start;

//   DO NOT EDIT BELOW THIS POINT  //

// LOOPS FOR EACH DAY OF WEEK
for(index=0; index < DAYS_OF_WEEK; index++)
{

// BOLD TODAY'S DAY OF WEEK
if(weekday == index)
cal += TD_start + "<span class='days'><B>" + day_of_week[index] + "</B></span>" + TD_end;

// PRINTS DAY
else
cal += TD_start + "<span class='days'>" + day_of_week[index] + "</span>" + TD_end;
}

cal += TD_end + TR_end;
cal += TR_start;

// FILL IN BLANK GAPS UNTIL TODAY'S DAY
for(index=0; index < Calendar.getDay(); index++)
cal += TD_start + '  ' + TD_end;

// LOOPS FOR EACH DAY IN CALENDAR
for(index=0; index < DAYS_OF_MONTH; index++)
{
if( Calendar.getDate() > index )
{
  // RETURNS THE NEXT DAY TO PRINT
  week_day =Calendar.getDay();

  // START NEW ROW FOR FIRST DAY OF WEEK
  if(week_day == 0)
  cal += TR_start;

  if(week_day != DAYS_OF_WEEK)
  {

  // SET VARIABLE INSIDE LOOP FOR INCREMENTING PURPOSES
  var day  = Calendar.getDate();

  // HIGHLIGHT TODAY'S DATE
  if( today==Calendar.getDate() )
 	cal += highlight_start + LK_start + day + Fc_end + day + LK_end + highlight_end + TD_end;

  // PRINTS DAY
  else
  	cal += TD_start + LK_start + day + Fc_end + day + LK_end + TD_end;
  }

  // END ROW FOR LAST DAY OF WEEK
  if(week_day == DAYS_OF_WEEK)
  cal += TR_end;
  }

  // INCREMENTS UNTIL END OF THE MONTH
  Calendar.setDate(Calendar.getDate()+1);

}// end for loop

cal += '</center></table><br></TD></TR></TABLE></center>';

//  PRINT CALENDAR
document.write(cal);
}

function timeValidate(str){
	var hr;
	var mn;
	var pIntHr;
	var pIntMn;
	
	if(str.length != 5){
		return false;
	}
	hr = str.substring(0,2);
	mn = str.substring(3,5);

	pIntHr = parseInt(hr);
	if(isNaN(pIntHr)){
		return false;
	}
	pIntMn = parseInt(mn);
	if(isNaN(pIntMn)){
		return false;
	}
	hr = parseInt(hr);
	mn = parseInt(mn);
	if((hr < 0)||(hr >= 24)){
		// alert(" Correct Format HH:MM ");
		return false;
	}
	if((mn < 0)||(mn > 59)){
		// alert(" Correct Format HH:MM ");
		return false;
	}
	return true;
}
function isDate(str) {

	if (str.length != 10) { 
		return false;
	}

        for (i=0; i<str.length; i++) {
        	if ((i == 2) || (i == 5)) {
                	if (!((str.charAt(i) == "/")||(str.charAt(i) == "-"))){ 
				return false; 
			}
            }else{
                    if ((str.charAt(i) < "0") || (str.charAt(i) > "9")){
				return false; 
		}
            }
      	}
        return true;
}

// Function will return true if date 1 is less than date 2
// otherwise it returns false
function compareDate(date1, date2){
	try{
		var arrDate1 = date1.split("/");
		var arrDate2 =  date2.split("/");
		arrDate1[0] = arrDate1[0]-1;
		arrDate2[0] = arrDate2[0]-1;
		var beginDate = new Date(arrDate1[2],arrDate1[0],arrDate1[1]);
		var endDate = new Date(arrDate2[2], arrDate2[0],arrDate2[1]);
		
		var begSecs = beginDate.getTime();
		var endSecs = endDate.getTime();
				
		if(begSecs <= endSecs){
			return true;
		}else{
			return false;
		}		
	}catch(e){
		alert(e);
		return false;
	}
}
