
var today = new Date(); // comment out when testing
//var today = new Date(2005, 11, 31); // comment in when testing (2nd param, month, runs between 0-11)

var currentQtr = 0;

var month = today.getMonth()+1;
if (month < 4) currentQtr = 1;
else if (month < 7) currentQtr = 2;
else if (month < 10) currentQtr = 3;
else currentQtr = 4;

function whatQtr(ago) {
	var aDate = new Date(today.getYear(), today.getMonth(), today.getDate());
	aDate.setDate(aDate.getDate() + ago);
	var dispDate = aDate;
	var aDateMonth = (dispDate.getMonth() + 1);
	if (aDateMonth < 4) return 1;
	else if (aDateMonth < 7) return 2;
	else if (aDateMonth < 10) return 3;
	else return 4;
}

function get_anydayMMDDYYYY(ago)
{
	var aDate = new Date(today.getYear(), today.getMonth(), today.getDate());
	aDate.setDate(aDate.getDate() + ago);
	var dispDate = aDate;
	var aDateMonth = (dispDate.getMonth() + 1);
	var aDateNum = dispDate.getDate();
	var aDateYear = dispDate.getYear();
	if (aDateMonth < 10)
	{
		aDateMonth = "0" + aDateMonth;
	}
	if (aDateNum < 10)
	{
		aDateNum = "0" + aDateNum;
	}
	return (aDateMonth + "/" + aDateNum + "/" + aDateYear);
}


function write(item) {
	document.write(item);
}

function writeYear(ago)
{
	var theYear=today.getYear()-ago;
	document.write(theYear);
}

function demoWriteQtrEnding () {
	if (currentQtr == 1) write("3/31/"+today.getYear());
	if (currentQtr == 2) write("6/30/"+today.getYear());
	if (currentQtr == 3) write("9/30/"+today.getYear());
	if (currentQtr == 4) write("12/31/"+today.getYear());
}

function writeQuarters() {
	if (currentQtr > 3) document.write("<option value=\"\">Q4 ");writeYear(0);document.write("</option>");
	if (currentQtr > 2) document.write("<option value=\"\">Q3 ");writeYear(0);document.write("</option>");
	if (currentQtr > 1) document.write("<option value=\"\">Q2 ");writeYear(0);document.write("</option>");
	if (currentQtr > 0) document.write("<option value=\"\">Q1 ");writeYear(0);document.write("</option>");
	document.write("<option value=\"\">Q4 ");writeYear(1);document.write("</option>");
	document.write("<option value=\"\">Q3 ");writeYear(1);document.write("</option>");
	document.write("<option value=\"\">Q2 ");writeYear(1);document.write("</option>");
	document.write("<option value=\"\">Q1 ");writeYear(1);document.write("</option>");
	if (currentQtr < 4) document.write("<option value=\"\">Q4 ");writeYear(2);document.write("</option>");
	if (currentQtr < 3) document.write("<option value=\"\">Q3 ");writeYear(2);document.write("</option>");
	if (currentQtr < 2) document.write("<option value=\"\">Q2 ");writeYear(2);document.write("</option>");
	if (currentQtr < 1) document.write("<option value=\"\">Q1 ");writeYear(2);document.write("</option>");
}

function gotoReportsDetail(obj) {
	if (obj.selectedIndex != 0 && obj.selectedIndex != 1)
		alert("You are viewing our demo.\nThis selection is not available in the demo.");
	else if (obj.selectedIndex == 0)
		alert('Please select a time frame.');
	else
		document.location="CC_QuarterlyMgmtReportsDetail.html";
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}

function selectReport(obj) {
	if (obj.selectedIndex == 0)
		document.bolForm.bolSelectButton.disabled = true;
	else
		document.bolForm.bolSelectButton.disabled = false;
}

function showReport(obj) {
	if (obj.selectedIndex == 5)
		alert("You are viewing our demo.\nThis selection is not available in the demo.");
	else
		document.location=obj.value;
}

function resetCurrentQtr() {
	for (m=1; m<=4; m++)
		for (n=1; n<9; n++)
			eval("trans_totals_company_Q"+currentQtr+"_"+m)[n] = 0;
}

function monthOfTrans(ago) {
	var aDate = new Date(today.getYear(), today.getMonth(), today.getDate());
	aDate.setDate(aDate.getDate() + ago);
	var dispDate = aDate;
	var aDateMonth = (dispDate.getMonth() + 1);
	var theMonth = aDateMonth%3;
	if (theMonth == 0) theMonth = 3;
	return theMonth;
}

/*** transaction totals for Company Summary ***/
var trans_totals_company_Q1_1 = new Array("January", 0, 213.50, 0, 0, 0, 0, 134.44, 347.94);
var trans_totals_company_Q1_2 = new Array("February", 0, 0, 0, 351.95, 0, 0, 0, 351.95);
var trans_totals_company_Q1_3 = new Array("March", 0, 366.95, 0, 356.90, 0, 0, 255.53, 979.38);
var trans_totals_company_Q1_4 = new Array("1st Quarter", 0, 580.45, 0, 708.85, 0, 0, 389.97, 1679.27);
var trans_totals_company_Q2_1 = new Array("April", 0, 129.53, 0, 212.30, 0, 0, 0, 341.83);
var trans_totals_company_Q2_2 = new Array("May", 0, 0, 0, 321.30, 0, 0, 234.56, 555.86);
var trans_totals_company_Q2_3 = new Array("June", 0, 385.17, 0, 333.25, 0, 0, 195.53, 913.95);
var trans_totals_company_Q2_4 = new Array("2nd Quarter", 0, 514.70, 0, 866.85, 0, 0, 430.09, 1811.64);
var trans_totals_company_Q3_1 = new Array("July", 0, 0, 0, 213.45, 0, 0, 198.50, 411.95);
var trans_totals_company_Q3_2 = new Array("August", 0, 85.40, 0, 129.50, 0, 0, 0, 214.90);
var trans_totals_company_Q3_3 = new Array("September", 0, 435.57, 0, 323.28, 0, 0, 174.43, 933.28);
var trans_totals_company_Q3_4 = new Array("3rd Quarter", 0, 520.97, 0, 666.23, 0, 0, 372.93, 1560.13);
var trans_totals_company_Q4_1 = new Array("October", 0, 0, 0, 0, 0, 0, 0, 0);
var trans_totals_company_Q4_2 = new Array("November", 0, 0, 0, 0, 0, 0, 0, 0);
var trans_totals_company_Q4_3 = new Array("December", 0, 0, 0, 0, 0, 0, 0, 0);
var trans_totals_company_Q4_4 = new Array("4th Quarter", 0, 0, 0, 0, 0, 0, 0, 0);
var YTD_totals = new Array("YTD Totals", 0, 0, 0, 0, 0, 0, 0, 0);

/*** transactions for Joe Demo ***/
var transCounter_JD = 0;
var transactions_JD = "";
var num_of_trans_JD = 13;
var trans_JD_1  = new Array(-7, -6, "SAKE JAPANESE STEAKHOUSE", "Dining Out", 258.56, 1);
var trans_JD_2  = new Array(-9, -7, "TARGET", "Miscellaneous", 69.95, 3);
var trans_JD_3  = new Array(-10, -10, "BOB-EVANS-REST #2062", "Dining Out", 77.21, 1);
var trans_JD_4  = new Array(-12, -12, "FINANCE CHARGE ADJUSTMENT", "Miscellaneous", 23.62, 3);
var trans_JD_5  = new Array(-14, -12, "KURT'S Classic KARZ", "Auto Related", 119.54, 6);
var trans_JD_6  = new Array(-23, -22, "ULTRA CLEAN AUTO WASH", "Auto Related", 14.99, 6);
var trans_JD_7  = new Array(-25, -25, "TARGET", "Miscellaneous", 124.58, 3);
var trans_JD_8  = new Array(-34, -32, "OFFICE DEPOT #525", "Miscellaneous", 43.55, 3);
var trans_JD_9  = new Array(-44, -42, "BOB-EVANS-REST #2062", "Dining Out", 53.25, 1);
var trans_JD_10 = new Array(-54, -52, "SAKE JAPANESE STEAKHOUSE", "Dining Out", 124.56, 1);
var trans_JD_11 = new Array(-64, -62, "OFFICE DEPOT #525", "Miscellaneous", 47.95, 3);
var trans_JD_12 = new Array(-74, -72, "TARGET", "Miscellaneous", 58.95, 3);
var trans_JD_13 = new Array(-84, -82, "KURT'S Classic KARZ", "Auto Related", 139.25, 6);
var trans_totals_JD = new Array(0, 0, 0, 0, 0, 0, 0, 0);

/*** transactions for Kay Dee ***/
var transCounter_KD = 0;
var transactions_KD = "";
var num_of_trans_KD = 7;
var trans_KD_1 = new Array(-25, -25, "OFFICE DEPOT #525", "Miscellaneous", 65.23, 3);
var trans_KD_2 = new Array(-35, -33, "BOB-EVANS-REST #2062", "Dining Out", 23.25, 1);
var trans_KD_3 = new Array(-45, -43, "SAKE JAPANESE STEAKHOUSE", "Dining Out", 113.32, 1);
var trans_KD_4 = new Array(-55, -53, "OFFICE DEPOT #525", "Miscellaneous", 23.55, 3);
var trans_KD_5 = new Array(-65, -63, "OFFICE DEPOT #525", "Miscellaneous", 35.95, 3);
var trans_KD_6 = new Array(-75, -73, "TARGET", "Miscellaneous", 48.95, 3);
var trans_KD_7 = new Array(-85, -83, "KURT'S Classic KARZ", "Auto Related", 119.25, 6);
var trans_totals_KD = new Array(0, 0, 0, 0, 0, 0, 0, 0);

function calculate_trans(emp) {
	var transactions = "";
	var transCounter = 0;
	for (i=1; i<=eval("num_of_trans_"+emp); i++) {
		var trans = eval("trans_"+emp+"_"+i);
		var transDate = trans[0];
		var transQtr = whatQtr(transDate);
		if (transQtr == currentQtr) {
			transactions += "				<tr>\n";
			transactions += "					<td colspan=\"8\" class=\"spacerH5\">&nbsp;</td>";
			transactions += "				</tr>";
			transactions += "				<tr>";
			transactions += "					<td>&nbsp;</td>";
			transactions += "					<td class=\"bodyTextC\">"+get_anydayMMDDYYYY(trans[0])+"</td>";
			transactions += "					<td class=\"bodyTextC\">"+get_anydayMMDDYYYY(trans[1])+"</td>";
			transactions += "					<td class=\"bodyText\">"+trans[2]+"</td>";
			transactions += "					<td class=\"bodyTextC\">"+trans[3]+"</td>";
			transactions += "					<td align=\"right\" class=\"bodyText\">$"+formatCurrency(trans[4])+"</td>";
			transactions += "					<td>&nbsp;</td>";
			transactions += "					<td>&nbsp;</td>";
			transactions += "				</tr>";
			transactions += "				<tr>";
			transactions += "					<td colspan=\"8\" class=\"spacerH5\">&nbsp;</td>";
			transactions += "				</tr>";
			transactions += "				<tr>";
			transactions += "					<td colspan=\"8\" class=\"divider\">&nbsp;</td>";
			transactions += "				</tr>";

			eval("trans_totals_"+emp)[trans[5]] += trans[4];
			eval("trans_totals_"+emp)[7] += trans[4];

			transCounter++;

			eval("trans_totals_company_Q"+currentQtr+"_"+monthOfTrans(trans[0]))[trans[5]+1] += trans[4];
			eval("trans_totals_company_Q"+currentQtr+"_"+monthOfTrans(trans[0]))[8] += trans[4];
			eval("trans_totals_company_Q"+currentQtr+"_4")[trans[5]+1] += trans[4];
			eval("trans_totals_company_Q"+currentQtr+"_4")[8] += trans[4];
		}	
	}
	if (transCounter == 0) {
		transactions += "				<tr>\n";
		transactions += "					<td colspan=\"8\" class=\"spacerH5\">&nbsp;</td>";
		transactions += "				</tr>";
		transactions += "				<tr>";
		transactions += "					<td colspan=\"8\" align=\"center\" class=\"bodyText\">There are no transactions for this employee.</td>";
		transactions += "				</tr>";
		transactions += "				<tr>";
		transactions += "					<td colspan=\"8\" class=\"spacerH5\">&nbsp;</td>";
		transactions += "				</tr>";
		transactions += "				<tr>";
		transactions += "					<td colspan=\"8\" class=\"divider\">&nbsp;</td>";
		transactions += "				</tr>";
	}

	if (emp == "JD") {transactions_JD = transactions; transCounter_JD = transCounter;}
	if (emp == "KD") {transactions_KD = transactions; transCounter_KD = transCounter;}
}

function calculate_YTD_totals() {
	for (q=1; q<=currentQtr; q++)
		for (n=1; n<9; n++)
			YTD_totals[n] += eval("trans_totals_company_Q"+q+"_4")[n];
}

function writeCompanySummary() {
	for (q=1; q<=currentQtr; q++) {
		for (t=1; t<=4; t++) {
			write("<tr>\n");
			write("	<td>&nbsp;</td>\n");
			write("	<td class=\"summaryHeader\"><strong>"+eval("trans_totals_company_Q"+q+"_"+t)[0]+"</strong></a>&nbsp;&nbsp;</td>\n");
			write("	<td class=\"bodyTextRSm\">"+formatCurrency(eval("trans_totals_company_Q"+q+"_"+t)[1])+"</td>\n");
			write("	<td class=\"bodyTextRSm\">"+formatCurrency(eval("trans_totals_company_Q"+q+"_"+t)[2])+"</td>\n");
			write("	<td class=\"bodyTextRSm\">"+formatCurrency(eval("trans_totals_company_Q"+q+"_"+t)[3])+"</td>\n");
			write("	<td class=\"bodyTextRSm\">"+formatCurrency(eval("trans_totals_company_Q"+q+"_"+t)[4])+"</td>\n");
			write("	<td class=\"bodyTextRSm\">"+formatCurrency(eval("trans_totals_company_Q"+q+"_"+t)[5])+"</td>\n");
			write("	<td class=\"bodyTextRSm\">"+formatCurrency(eval("trans_totals_company_Q"+q+"_"+t)[6])+"</td>\n");
			write("	<td class=\"bodyTextRSm\">"+formatCurrency(eval("trans_totals_company_Q"+q+"_"+t)[7])+"</td>\n");
			write("	<td class=\"bodyTextRSm\"><strong>"+formatCurrency(eval("trans_totals_company_Q"+q+"_"+t)[8])+"</strong></td>\n");
			write("	<td>&nbsp;</td>\n");
			write("</tr>\n");
			write("<tr>\n");
			write("	<td colspan=\"11\" class=\"divider\">&nbsp;</td>\n");
			write("</tr>\n");
		}
		write("<tr>\n");
		write("	<td colspan=\"11\" class=\"spacerH20\">&nbsp;</td>\n");
		write("</tr>\n");
		write("<tr>\n");
		write("	<td colspan=\"11\" class=\"divider\">&nbsp;</td>\n");
		write("</tr>\n");
	}
	write("<tr>\n");
	write("	<td>&nbsp;</td>\n");
	write("	<td class=\"summaryHeader\"><strong>"+YTD_totals[0]+"</strong></a>&nbsp;&nbsp;</td>\n");
	write("	<td class=\"bodyTextRSm\"><strong>"+formatCurrency(YTD_totals[1])+"</strong></td>\n");
	write("	<td class=\"bodyTextRSm\"><strong>"+formatCurrency(YTD_totals[2])+"</strong></td>\n");
	write("	<td class=\"bodyTextRSm\"><strong>"+formatCurrency(YTD_totals[3])+"</strong></td>\n");
	write("	<td class=\"bodyTextRSm\"><strong>"+formatCurrency(YTD_totals[4])+"</strong></td>\n");
	write("	<td class=\"bodyTextRSm\"><strong>"+formatCurrency(YTD_totals[5])+"</strong></td>\n");
	write("	<td class=\"bodyTextRSm\"><strong>"+formatCurrency(YTD_totals[6])+"</strong></td>\n");
	write("	<td class=\"bodyTextRSm\"><strong>"+formatCurrency(YTD_totals[7])+"</strong></td>\n");
	write("	<td class=\"bodyTextRSm\"><strong>"+formatCurrency(YTD_totals[8])+"</strong></td>\n");
	write("	<td>&nbsp;</td>\n");
	write("</tr>\n");
}

function write_trans(emp) {
	document.write(eval("transactions_"+emp));
}

resetCurrentQtr();
calculate_trans("JD");
calculate_trans("KD");
calculate_YTD_totals();