// Copyright (c) 2002-2009 SureCode Technologies. All Rights Resvered.

	var dDate = new Date();
	var dCurMonth = dDate.getMonth();
	var dCurDayOfMonth = dDate.getDate();
	var dCurYear = dDate.getFullYear();
	var objPrevElement = new Object();
	var aMonth = new Array();
	var CurMon, CurYear, OutputBuffer, fFadeInValue;
	var scCal2TargetField, scCal2RetFrmt, scCal2Top, scCal2Left;

	// First row is always the days of the week
	scCal2RetFrmt = 1;
	aMonth[0] = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
	aMonth[1] = new Array(7);
	aMonth[2] = new Array(7);
	aMonth[3] = new Array(7);
	aMonth[4] = new Array(7);
	aMonth[5] = new Array(7);
	aMonth[6] = new Array(7);

	var aMonL = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
  var aDayL = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
  var aMNames = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  var aYear = new Array("1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015","2016","2017","2018");
	var qSet = new Array("1 Day","3 Days","1 Week","2 Weeks","4 Weeks","90 Days","Clear");
	var mDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

// Send it to the buffer
function doOutput(sData) { OutputBuffer = OutputBuffer + sData; }

// Get top position of an element
function findTopPos(obj) {
    var curtop = 0;
    if (obj.offsetParent) { do { curtop += obj.offsetTop; } while (obj = obj.offsetParent); }
    return curtop;
} // findTopPos

// Pop open and init the calendar
function DoCalV2(thisField, thisFormat) {

	// Grab user params
	scCal2RetFrmt = thisFormat;
	scCal2TargetField = thisField;
	scCal2Top = findTopPos(thisField);
	
	var dCurDate;
	if (scCal2TargetField.value == '') {
		dCurDate = new Date();
	} else if (fCurValue(thisFormat, scCal2TargetField.value) == '00/00/0000') {
		dCurDate = new Date();
	} else {
		dCurDate = new Date(fCurValue(thisFormat, scCal2TargetField.value));
	} // length
	
	fDrawCal(dCurDate.getFullYear(), dCurDate.getMonth() + 1);
} // DoCalV2

// Pop open and init the calendar
function DoTime(thisField, thisFormat) {

	// Grab user params
	scCal2RetFrmt = thisFormat;
	scCal2TargetField = thisField;
	scCal2Top = findTopPos(thisField);
	fDrawTime(1);
	
} // DoTime

// Returns the number of days in the month
function fGetMonth(DisplayString) {
	var i;
	
	for (i = 0; i < 12; i++) {
		if (DisplayString.indexOf(aMonL[i]) > -1) {return (i + 1); }
	}  // long format

	for (i = 0; i < 12; i++) {
		if (DisplayString.indexOf(aMNames[i]) > -1) { return (i + 1); }
	}  // long format
	
	// Should never get here..
	return 1;
	
} // fGetMonth

// Returns the number of days in the month
function fGetDaysInMonth (iMonth, iYear) {

	var RetValue = mDays[iMonth-1];
	if (iMonth == 2 && iYear == 2000) {RetValue = 29;}
	if (iMonth == 2 && iYear == 2004) {RetValue = 29;}
	if (iMonth == 2 && iYear == 2008) { RetValue = 29; }
	if (iMonth == 2 && iYear == 2012) { RetValue = 29; }
	if (iMonth == 2 && iYear == 2016) { RetValue = 29; }
	if (iMonth == 2 && iYear == 2020) { RetValue = 29; }
	
	return RetValue;
}

// lil helpers Returns 2 wide string
function fW (iInt) { if (iInt < 10) { return ("0"+iInt); } else return (iInt);}
function fY(iInt) { if (iInt < 50) { return ("20" + iInt); } else return ("19" + iInt); }

// Return the standard format base on the current format
function fCurValue(iFormat, iCurValue) {

	var ReturnString;
	var Sep = "/";
	
	if (iFormat == 1) { ReturnString = iCurValue.substr(0, 2) + Sep + iCurValue.substr(3, 2) + Sep + fY(iCurValue.substr(6, 2)); }
	else if (iFormat == 2) { ReturnString = iCurValue; }
	else if (iFormat == 3) { ReturnString = iCurValue.substr(5, 2) + Sep + iCurValue.substr(8, 2) + Sep + iCurValue.substr(0, 4); }
	else if (iFormat == 4) { ReturnString = iCurValue.substr(4, 2) + Sep + iCurValue.substr(6, 2) + Sep + iCurValue.substr(0, 4); }
	else if (iFormat == 5) { ReturnString = iCurValue.substr(0, 2) + Sep + iCurValue.substr(3, 2) + Sep + fY(iCurValue.substr(6, 2)); }
	else if (iFormat == 6) { ReturnString = iCurValue.substr(0, 2) + Sep + iCurValue.substr(3, 2) + Sep + iCurValue.substr(6, 4); }
	else if (iFormat == 7) { ReturnString = fW(fGetMonth(iCurValue)) + Sep + "01" + Sep + iCurValue.substr(iCurValue.length-4); } 
	else if (iFormat == 8) { ReturnString = iCurValue.substr(4, 2) + Sep + iCurValue.substr(7, 2) + Sep + fY(iCurValue.substr(10, 2)); }
	else if (iFormat == 9) { ReturnString = fW(fGetMonth(iCurValue)) + Sep + "01" + Sep + iCurValue.substr(iCurValue.length - 4); }
	else if (iFormat == 10) { ReturnString = fW(fGetMonth(iCurValue)) + Sep + "01" + Sep + iCurValue.substr(iCurValue.length - 4); } 
	else if (iFormat == 11) { ReturnString = fW(fGetMonth(iCurValue)) + Sep + "01" + Sep + iCurValue.substr(iCurValue.length - 4); } 

	else ReturnString = '';

	return ReturnString;
	
} // fCurValue

// Date picked, Make date string and close window
function fSetDate (iOffset) {

	var ReturnString,Sep,Mon,Day,YrL,YrS,wDay;
	var dSelDate = new Date();

	// If negative make date based on a delta of todays date
	if (iOffset == -1) {dSelDate.setTime(dSelDate.getTime() + (1000*60*60*24*1));}
	else if (iOffset == -2) {dSelDate.setTime(dSelDate.getTime() + (1000*60*60*24*3));}
	else if (iOffset == -3) {dSelDate.setTime(dSelDate.getTime() + (1000*60*60*24*7));}
	else if (iOffset == -4) {dSelDate.setTime(dSelDate.getTime() + (1000*60*60*24*14));}
	else if (iOffset == -5) {dSelDate.setTime(dSelDate.getTime() + (1000*60*60*24*28));}
	else if (iOffset == -6) {dSelDate.setTime(dSelDate.getTime() + (1000*60*60*24*90));}
	else if (iOffset > 0) {dSelDate = new Date(CurYear, CurMon-1, iOffset);}
	else if (iOffset == -7) { scCal2TargetField.value = ''; document.getElementById("scCal2Div").style.visibility = "hidden"; return; }

	Day = dSelDate.getDate();
	YrL = dSelDate.getFullYear();
	YrS = String(YrL).substr(2,2);
	Mon = dSelDate.getMonth()+1;
	wDay = dSelDate.getDay();
	Sep = "-";

	if (scCal2RetFrmt == 1) { Sep = "/"; ReturnString = fW(Mon) + Sep + fW(Day) + Sep + YrS; }
	else if (scCal2RetFrmt == 2) { Sep = "/"; ReturnString = fW(Mon) + Sep + fW(Day) + Sep + YrL; }
	else if (scCal2RetFrmt == 3) { Sep = ""; ReturnString = YrL + Sep + fW(Mon) + Sep + fW(Day); }
	else if (scCal2RetFrmt == 4) { ReturnString = YrL + Sep + fW(Mon) + Sep + fW(Day); }	
	else if (scCal2RetFrmt == 5) { ReturnString = fW(Mon) + Sep + fW(Day) + Sep + YrS; }
	else if (scCal2RetFrmt == 6) { ReturnString = fW(Mon) + Sep + fW(Day) + Sep + YrL; }
	else if (scCal2RetFrmt == 7) { ReturnString = Day + " " + aMonL[Mon - 1] + ", " + YrL; }
	else if (scCal2RetFrmt == 8) { ReturnString = aMonth[0][wDay] + " " + fW(Mon) + Sep + fW(Day) + Sep + YrS; }
	else if (scCal2RetFrmt == 9) { ReturnString = aMonth[0][wDay] + " " + aMNames[Mon - 1] + " " + Day + ", " + YrL; }
	else if (scCal2RetFrmt == 10) { ReturnString = aDayL[wDay] + " " + aMonL[Mon - 1] + " " + Day + ", " + YrL; }
	else if (scCal2RetFrmt == 11) { ReturnString = aMonL[Mon - 1] + " " + Day + ", " + YrL; }

	// Return value and hide
	scCal2TargetField.value = ReturnString;
	document.getElementById("scCal2Div").style.visibility = "hidden";
	if (scCal2TargetField.onchange != null) { scCal2TargetField.onchange(); }

} // fSetDate


// Time picked, Set the string and close window
function fSetTime(SelectedValue) {

	var ReturnString;

	// HH:MM AMPM
	var Sep = ":";
	var ampm = SelectedValue.substr(6, 2);
	var hh = SelectedValue.substr(0, 2);
	var mm = SelectedValue.substr(3, 2);

	// Special cases
	if (hh == 12 & ampm == "AM") { hh = "00"; }
	else if (hh == 12 & ampm == "PM") { hh = "12"; }
	else if (ampm == "PM") { hh = parseInt(hh) + 12; }

	if (scCal2RetFrmt == 1) { ReturnString = hh + Sep + mm + Sep + "00"} // HH:MM:SS
	else if (scCal2RetFrmt == 2) { ReturnString = SelectedValue; } // HH:MM AMPM
	else if (scCal2RetFrmt == 3) { ReturnString = (3600 * parseInt(hh) ) + (60 * parseInt(mm) ); } // Seconds since midnight
	else if (scCal2RetFrmt == 4) { ReturnString = hh + mm + "00"; } // 
	else { ReturnString = "ERROR"; }

	// Return value and hide
	scCal2TargetField.value = ReturnString;
	document.getElementById("scCal2Div").style.visibility = "hidden";
	if (scCal2TargetField.onchange != null) { scCal2TargetField.onchange(); }
	
} // fSetTime


// Changes the display month
function fMoveCal (iOption) {

	var iMonth, iYear; 

	// Set the current month and year
	iMonth = CurMon;
	iYear = CurYear;

	if (iOption == 1)
		if (iMonth == 12) { iMonth=1; ++iYear;} else { ++iMonth; }
 
	else if (iOption == -1) 
		if (iMonth == 1) { iMonth=12; --iYear;} else { --iMonth; }

	else if (iOption == 0) { var dCurDate = new Date(); iYear=dCurDate.getFullYear(); iMonth=dCurDate.getMonth()+1; }

	else if (iOption == -8) { document.getElementById("scCal2Div").style.visibility = "hidden"; return; }


	// Hard limit the boundaries
	if (iYear < aYear[0]) { iYear = aYear[0]; iMonth=1; }
	if (iYear > aYear[aYear.length-1]) { iYear = aYear[aYear.length-1]; iMonth=12; }

	fDrawCal (iYear, iMonth);
	
} 


// Builds and data that will be stuffed into the calendar table
function fBuildCal (iYear, iMonth) {			

	var dCalDate = new Date(iYear, iMonth-1, 1);
	var iDayOfFirst = dCalDate.getDay();
	var iDaysInMonth = fGetDaysInMonth(iMonth, iYear);
	var iVarDate = 1;
	var i, d, w;

	// Init
	for (w = 1; w < 7; w++) {
		for (d = 0; d < 7; d++) {		 
				aMonth[w][d] = "&nbsp;";				
		}
	}

	// Next, fill the next row based on which
	for (d = iDayOfFirst; d < 7; d++) {
		aMonth[1][d] = iVarDate;
		iVarDate++;
	}

	for (w = 2; w < 7; w++) {
		for (d = 0; d < 7; d++) {
			if (iVarDate <= iDaysInMonth) {
				aMonth[w][d] = iVarDate;
				iVarDate++;
			}
		}
	}

}


// Use the table data to draw the calendar
function fDrawCal (iYear, iMonth) {

	// Inits
	var Pre, Post, i;
	OutputBuffer = "";
	
	// onChange event from dropdowns.
	if (iYear == 0) {
		var Offset = document.getElementById("scCal2Form").tbSelMonth.options.selectedIndex;
		iMonth = document.getElementById("scCal2Form").tbSelMonth.options[Offset].value;
		Offset = document.getElementById("scCal2Form").tbSelYear.options.selectedIndex;
		iYear = document.getElementById("scCal2Form").tbSelYear.options[Offset].value;
	}

	fBuildCal (iYear, iMonth);
	var dCalDate = new Date(iYear, iMonth-1, 1);
	var iDayOfFirst = dCalDate.getDay();	

	// Here is the outer table
	doOutput("<form id=scCal2Form name=scCal2Form style=\"display:inline;\"><table border=0 cellpadding=4 align=center><tr><td><table align=center border=1 bgcolor=#ffffff><tr bgcolor=#c0c0c0><td>");
	doOutput("<select name=tbSelMonth onChange='fDrawCal(0,0)'>");
	
	// The select month pulldown
	Pre = "<option value=";
	Post = "</option>";
	for (i=1; i<=12; i++)
		doOutput(Pre + i + ">" + aMNames[i-1] + Post);
	doOutput("</select>");	 

	// The select Year pulldown
	doOutput("<select name=tbSelYear onChange='fDrawCal(0,0)'>");

	Pre = "<option value=";
	Post = "</option>";
	for (i=0; i<aYear.length; i++)
		doOutput(Pre + aYear[i] + ">" + aYear[i] + Post);
	doOutput("</select>&nbsp;&nbsp;&nbsp;&nbsp;");

	// Nav buttons
	doOutput("<input value=\"<<\"type=button name=prev onClick='fMoveCal(-1)'>");
	doOutput("<input value=\"Today\"type=button name=today onClick='fMoveCal(0)'>");
	doOutput("<input value=\">>\"type=button name=next onClick='fMoveCal(1)'>");
	doOutput("<input value=\" x \"type=button name=next onClick='fMoveCal(-8)'>");

	// Next row is the calendar and quick-sets table
	doOutput("</td></tr><tr><td><table border=0><tr><td>");

	Pre = "<td align=center width=20><font color=#003366 size=1 face=Arial>";
	Post = "</font></td>";	

	doOutput("<table border=1>");
	doOutput("<tr bgcolor=#eeeeee>");
	for (i=0; i<7; i++) {
	 doOutput(Pre + aMonth[0][i] + Post);
	}


	var Pre = "<td onmouseover='this.style.background=\"#f2b7bb\";' onmouseout='this.style.background=\"\";' align=left><a href=\"javascript:fSetDate(";
	var Pre2 = ");\"><font color=#000000 size=1 face=Arial>";
	var PreTd = "<td align=left bgcolor=#cccccc><a href=\"javascript:fSetDate(";
	var PreTd2 = ");\"><font color=#ff0000 size=1 face=Arial>"; 

	var Post = "</a></font></td>";
	var ThisPre,ThisPre2;
	
	doOutput("</tr>");
	for (w = 1; w < 7; w++) {
		doOutput("<tr>")
		for (d = 0; d < 7; d++) {
			if ( ((w-1)*7+d == (dCurDayOfMonth+iDayOfFirst-1)) && (dCurMonth+1==iMonth) && (dCurYear==iYear)) {ThisPre=PreTd;ThisPre2=PreTd2;} else {ThisPre=Pre;ThisPre2=Pre2;}

			if (aMonth[w][d]!="&nbsp;")
				doOutput(ThisPre + aMonth[w][d] + ThisPre2 + aMonth[w][d] + Post);
			else
				doOutput("<td align=left><font color=#000000 size=1 face=Arial>&nbsp;</font></td>");

		}
		doOutput("</tr>");
	}

	// Close the calendar table open quicklinks 
	doOutput("</table></td><td valign=top> <table border=0 width=100% cellspacing=4>");

	// Make the quicksets... 
	Pre = "<tr><td><a href=\"javascript:fSetDate(";		
	Pre2 = ");\"><font color=#000000 size=1 face=Arial>"; 
	Post = "</a></font></td></tr>";

	for (d = 0; d < 7; d++)
		doOutput(Pre + -(d+1) + Pre2 + qSet[d] + Post);

	doOutput("</table></td></table>");

	// Close the outer table and form
	doOutput("</td></tr></table></td></tr></table></form>");
	
	// Sendit and show it
	document.getElementById("scCal2Div").innerHTML = OutputBuffer;
	
	// Center it
	scCal2Left = (document.body.clientWidth - document.getElementById("scCal2Div").offsetWidth) / 2;

  // Set position
	document.getElementById("scCal2Div").style.top = scCal2Top + 'px';
	document.getElementById("scCal2Div").style.left = scCal2Left + 'px';

	// Set the menu to reflect the current date
	document.getElementById("scCal2Form").tbSelMonth.options.selectedIndex = iMonth - 1;
	document.getElementById("scCal2Form").tbSelYear.options.selectedIndex = iYear - aYear[0];

	// Set the current month and year
	CurMon = iMonth;
	CurYear = iYear;

	// Show it
	fShowIt("scCal2Div");

} // fDrawCal


// Show a list of dates to pick
function fDrawTime( index ) {

	// Inits
	var Pre, Post, i, ampm, ampmV;
	
	OutputBuffer = "";	
	if (index == 1) { ampm = ' AM'; } else { ampm = ' PM'; }
	if (index == 1) { ampmV = '&nbsp;AM'; } else { ampmV = '&nbsp;PM'; }

	// Here is the outer table
	doOutput("<form id=scCal2Form name=scCal2Form style=\"display:inline;\"><table border=0 cellpadding=4 align=center><tr><td><table align=center border=1 bgcolor=#ffffff><tr bgcolor=#c0c0c0><td align=right>");
	
	// Nav buttons
	doOutput("<input value=\"am\"type=button name=prev onClick='fDrawTime(1)'>");
	doOutput("<input value=\"pm\"type=button name=next onClick='fDrawTime(2)'>");
	doOutput("<input value=\" x \"type=button name=next onClick='fMoveCal(-8)'>");

	// Show the date options
	doOutput("</td></tr><tr><td>");
	doOutput("<table border=1>");

	var Pre = "<td onmouseover='this.style.background=\"#f2b7bb\";' onmouseout='this.style.background=\"\";' align=left><a href=\"javascript:fSetTime('";
	var Pre2 = "');\"><font color=#000000 size=1 face=Arial>";
	var Post = "</a></font><img border=0 src=/images/cleardot.gif hspace=8></td>";
	
	for (hh=0; hh < 12; hh++) {
		doOutput("<tr>")

		if (hh == 0) {h = 12;} else {h = hh;} 
		for (m = 0; m < 60; m+=15) {
			doOutput(Pre + fW(h) + ":" + fW(m) + ampm + Pre2 + fW(h) + ":" + fW(m) + ampmV + Post);
		} // minutes
		doOutput("</tr>");
	} // hours
	
	// close out the time tables
	doOutput("</table>");
	
	// Close the outer table and form
	doOutput("</td></tr></table></td></tr></table></form>");

	// Sendit and show it
	document.getElementById("scCal2Div").innerHTML = OutputBuffer;

	// Center it
	scCal2Left = (document.body.clientWidth - document.getElementById("scCal2Div").offsetWidth) / 2;

	// Set position
	document.getElementById("scCal2Div").style.top = scCal2Top + 'px';
	document.getElementById("scCal2Div").style.left = scCal2Left + 'px';

	// Show it
	fShowIt("scCal2Div");
	
} // fDrawTime

function fShowIt(divID) {

	var ie = document.all;
	if (document.getElementById(divID).style.visibility == 'hidden') {
	if (ie) {
	
		document.getElementById(divID).style.filter = "alpha(opacity=0)";
		document.getElementById(divID).filters.alpha.opacity = 0;
		document.getElementById(divID).style.visibility = "visible";
		fFadeInValue = 0;
		fShowFadeIn_IE(divID);

	} else {
	
		document.getElementById(divID).style.opacity = 0;
		document.getElementById(divID).style.visibility = "visible";
		fFadeInValue = 0;
		fShowFadeIn(divID);

	} // ie
	} // show it	
	
} // fShowIt

function fShowFadeIn(divID) {

	var fDelta = 0.04;
	fFadeInValue += fDelta;

	// If we are done - leave
	if (fFadeInValue > 1) { return; }
	document.getElementById(divID).style.opacity = fFadeInValue;

	// Set the callback for nest time
	setTimeout("fShowFadeIn('scCal2Div')", 10);
	
} // fShowFadeIn

function fShowFadeIn_IE(divID) {

	var fDelta = 4;
	fFadeInValue += fDelta;

	// If we are done - leave
	if (fFadeInValue > 100) { return; }
	document.getElementById(divID).filters.alpha.opacity = fFadeInValue;

	// Set the callback for nest time
	setTimeout("fShowFadeIn_IE('scCal2Div')", 10);
	
} // fShowFadeIn_IE


