//
// Admin Navigation
// Bret Sheeley
// Moisture Mapper
//
// This document contains scripts that navigates users to
// one of the data tables.
//
var projectsDemoMenu=new Array();
projectsDemoMenu[0]='<a href="/site/projects/demos/loadProjectDemo1.php">Load Demo Project #1</a>';
projectsDemoMenu[1]='<a href="/site/projects/demos/loadProjectDemo2.php">Load Demo Project #2</a>';

var projectsAttachmentsMenu=new Array();
projectsAttachmentsMenu[0]='<a href="/site/invoiceUpload.php">Estimate Upload</a>';
projectsAttachmentsMenu[1]='<a href="/site/projects/attachmentUploads.php">Project Attachments</a>';

var projectsGraphsMenu = new Array();
projectsGraphsMenu[0] = '<a href="/site/projectAnalysis.php">Project Analysis</a>';
projectsGraphsMenu[1] = '<a href="/site/projectReport.php">Graph Data</a>';

var projectsProjectMenu = new Array();
projectsProjectMenu[0] = '<a href="/site/projectViewDetails.php">Admin Details</a>';
projectsProjectMenu[1] = '<a href="/site/projectViewOpDetails.php">Operational Details</a>';
projectsProjectMenu[2] = '<a href="/site/projects/projectView.php?show=workPlan">Work Plan</a>';
projectsProjectMenu[3] = '<a href="/site/projects/projectView.php?show=images">Big Picture</a>';
projectsProjectMenu[4] = '<a href="/site/projects/projectView.php?show=rooms">Drying Areas</a>';
projectsProjectMenu[5] = '<a href="/site/projects/resourceSummary/invoiceSummary.php">Resource Summary</a>';
projectsProjectMenu[6] = '<a href="/site/projects/contractorComments.php">Contact Log</a>';
projectsProjectMenu[7] = '<a href="/site/projects/projectView.php?show=forms">Work in Progress</a>';
projectsProjectMenu[8] = '<a href="/site/projectClose">Work in Progress</a>';

var projectsProjectSUMenu = new Array();
projectsProjectSUMenu[0] = '<a href="/site/projectViewDetails.php">Admin Details</a>';
projectsProjectSUMenu[1] = '<a href="/site/projectViewOpDetails.php">Operational Details</a>';
projectsProjectSUMenu[2] = '<a href="/site/projects/projectView.php?show=workPlan">Work Plan</a>';
projectsProjectSUMenu[3] = '<a href="/site/projects/projectView.php?show=images">Big Picture</a>';
projectsProjectSUMenu[4] = '<a href="/site/projects/projectView.php?show=rooms">Drying Areas</a>';
projectsProjectSUMenu[5] = '<a href="/site/projects/resourceSummary/invoiceSummary.php">Resource Summary</a>';
projectsProjectSUMenu[6] = '<a href="/site/projects/contractorComments.php">Contact Log</a>';
projectsProjectSUMenu[7] = '<a href="/site/projects/projectView.php?show=forms">Work in Progress</a>';
projectsProjectSUMenu[8] = '<a href="/site/delmhorstImport.php">Import Data</a>';
projectsProjectSUMenu[9] = '<a href="/site/projectClose">Work in Progress</a>';

var projectsToolsMenu = new Array();
projectsToolsMenu[0] = '<a href="/site/projects/amperageCosts/amperageCosts.php">Electrical Costs</a>';
projectsToolsMenu[1] = '<a href="/site/company/corporateAnalysis/projectAnalysisAlerts.php">Alerts</a>';

var projectsSUToolsMenu = new Array();
projectsSUToolsMenu[0] = '<a href="/site/projects/amperageCosts/amperageCosts.php">Electrical Costs</a>';
projectsSUToolsMenu[1] = '<a href="/site/company/corporateAnalysis/projectAnalysisAlerts.php">Alerts</a>';


//=========================================
//
// Project Operational Details
//
function projectViewOpDetailscopyFieldFromTo(copyName1,copyName2,copyName3,copyName4,copyName5,pasteField) {
	
	var pasteFieldObj = window.document.getElementsByName(pasteField);
	
	// Get values
	var copy1Obj = window.document.getElementsByName(copyName1);
	var copy1;
	if(copy1Obj.length > 0) {
		copy1 = copy1Obj[0].value;
	} else {
		copy1 = "";	
	}
	var copy2Obj = window.document.getElementsByName(copyName2);
	var copy2;
	if(copy2Obj.length > 0) {
		copy2 = copy2Obj[0].value;
	} else {
		copy2 = "";	
	}
	var copy3Obj = window.document.getElementsByName(copyName3);
	var copy3;
	if(copy3Obj.length > 0) {
		copy3 = copy3Obj[0].value;
	} else {
		copy3 = "";
	}
	var copy4Obj = window.document.getElementsByName(copyName4);
	var copy4;
	if(copy4Obj.length > 0) {
		copy4 = copy4Obj[0].value;
	} else {
		copy4 = "";
	}
	var copy5Obj = window.document.getElementsByName(copyName5);
	var copy5;
	if(copy5Obj.length > 0) {
		copy5 = copy5Obj[0].value;
	} else {
		copy5 = "";
	}


	// If the first value is legit
	// copy into the paste field
	var copyValue;
	if(copy1 != "" && copy1 != "0000-00-00") {
		copyValue = copy1;
		pasteFieldObj[0].value = copyValue;
		
	// If the first value is not legit and the second is
	// copy legit value into the paste field AND the first copy field
	} else if(copy2 != "" && copy2 != "0000-00-00") {
		copyValue = copy2;
		pasteFieldObj[0].value = copyValue;
		copy1Obj[0].value = copyValue;
		
	// If the first two values are not legit and the third is
	// copy the legit value into the paste field AND the first two copy fields
	} else if(copy3 != "" && copy3 != "0000-00-00") {
		copyValue = copy3;
		pasteFieldObj[0].value = copyValue;
		copy1Obj[0].value = copyValue;
		copy2Obj[0].value = copyValue;
		
	// If the first three values are not legit and the fourth is
	// copy the legit value into the paste field AND the first three copy fields
	} else if(copy4 != "" && copy4 != "0000-00-00") {
		copyValue = copy4;
		pasteFieldObj[0].value = copyValue;
		copy1Obj[0].value = copyValue;
		copy2Obj[0].value = copyValue;
		copy3Obj[0].value = copyValue;
	// If the first four values are not legit and the fifth is
	// copy the legit value into the paste field AND the first four copy fields
	} else if(copy5 != "" && copy5 != "0000-00-00") {
		copyValue = copy5;
		pasteFieldObj[0].value = copyValue;
		copy1Obj[0].value = copyValue;
		copy2Obj[0].value = copyValue;
		copy3Obj[0].value = copyValue;
		copy4Obj[0].value = copyValue;
	}
	
}



//=========================================
//
// Project Operational Details
//
function projectViewOpDetailsCopyDateTimes(date1,time1,date2,time2,date3,time3,date4,time4,date5,time5,pasteDate,pasteTime) {
	
	
	var pasteDateObj = $("#" + pasteDate);
	var pasteTimeObj = $("#" + pasteTime);
	
	// Get values
	date1Obj = window.document.getElementsByName(date1);
	time1Obj = window.document.getElementsByName(time1);
	var copyDate1;
	var copyTime1;
	if(date1Obj.length > 0) {
		copyDate1 = date1Obj[0].value;
		copyTime1 = time1Obj[0].value;
	} else {
		copyDate1 = "";
		copyTime1 = "";	
	}
	
	date2Obj = window.document.getElementsByName(date2);
	time2Obj = window.document.getElementsByName(time2);
	var copyDate2;
	var copyTime2;
	if(date2Obj.length > 0) {
		copyDate2 = date2Obj[0].value;
		copyTime2 = time2Obj[0].value;
	} else {
		copyDate2 = "";
		copyTime2 = "";	
	}
	
	
	
	date3Obj = window.document.getElementsByName(date3);
	time3Obj = window.document.getElementsByName(time3);
	var copyDate3;
	var copyTime3;
	if(date3Obj.length > 0) {
		copyDate3 = date3Obj[0].value;
		copyTime3 = time3Obj[0].value;
	} else {
		copyDate3 = "";
		copyTime3 = "";	
	}
	
	
	
	date4Obj = window.document.getElementsByName(date4);
	time4Obj = window.document.getElementsByName(time4);
	var copyDate4;
	var copyTime4;
	if(date4Obj.length > 0) {
		copyDate4 = date4Obj[0].value;
		copyTime4 = time4Obj[0].value;
	} else {
		copyDate4 = "";
		copyTime4 = "";	
	}
	
	
	
	date5Obj = window.document.getElementsByName(date5);
	time5Obj = window.document.getElementsByName(time5);
	var copyDate5;
	var copyTime5;
	if(date5Obj.length > 0) {
		copyDate5 = date5Obj[0].value;
		copyTime5 = time5Obj[0].value;
	} else {
		copyDate5 = "";
		copyTime5 = "";	
	}
	
	
	

	// If the first value is legit
	// copy into the paste field
	var dateValue;
	var timeValue;
	if(copyDate1 != "" || copyTime1 != "") {
		dateValue = copyDate1;
		timeValue = copyTime1;
		pasteDateObj[0].value = dateValue;
		pasteTimeObj[0].value = timeValue;
		
	// If the first value is not legit and the second is
	// copy legit value into the paste field AND the first copy field
	} else if(copyDate2 != "" || copyTime2 != "") {
		
		dateValue = copyDate2;
		timeValue = copyTime2;
		pasteDateObj[0].value = dateValue;
		date1Obj[0].value = dateValue;
		pasteTimeObj[0].value = timeValue;
		time1Obj[0].value = timeValue;
		
	// If the first two values are not legit and the third is
	// copy the legit value into the paste field AND the first two copy fields
	} else if(copyDate3 != "" || copyTime3 != "") {
		
		dateValue = copyDate3;
		timeValue = copyTime3;
		pasteDateObj[0].value = dateValue;
		date1Obj[0].value = dateValue;
		date2Obj[0].value = dateValue;
		pasteTimeObj[0].value = timeValue;
		time1Obj[0].value = timeValue;
		time2Obj[0].value = timeValue;
		
	// If the first three values are not legit and the fourth is
	// copy the legit value into the paste field AND the first three copy fields
	} else if(copyDate4 != "" || copyTime4 != "") {
		
		dateValue = copyDate4;
		timeValue = copyTime4;
		pasteDateObj[0].value = dateValue;
		date1Obj[0].value = dateValue;
		date2Obj[0].value = dateValue;
		date3Obj[0].value = dateValue;
		pasteTimeObj[0].value = timeValue;
		time1Obj[0].value = timeValue;
		time2Obj[0].value = timeValue;
		time3Obj[0].value = timeValue;
		
	// If the first four values are not legit and the fifth is
	// copy the legit value into the paste field AND the first four copy fields
	} else if(copyDate5 != "" || copyTime5 != "") {
		
		dateValue = copyDate5;
		timeValue = copyTime5;
		pasteDateObj[0].value = dateValue;
		date1Obj[0].value = dateValue;
		date2Obj[0].value = dateValue;
		date3Obj[0].value = dateValue;
		date4Obj[0].value = dateValue;
		pasteTimeObj[0].value = timeValue;
		time1Obj[0].value = timeValue;
		time2Obj[0].value = timeValue;
		time3Obj[0].value = timeValue;
		time4Obj[0].value = timeValue;
		
	}
	
	
	
	
}


//========================================
//
// Updates
//

// Update the Residential Commercial Mode
function projectResidentialCommercialSetByMenu() {

	var projectMode = window.document.getElementsByName('projectMode')[0].value;
	projectResidentialCommercialSet(projectMode);
}


// Update the Residential Commercial Mode (CORP)
function projectResidentialCommercialSet(newMode) {

	var obj = window.document.getElementsByName('commercialResidential');

	var radioLength = obj.length;
	for(var i=0;i<radioLength;i++) {
		if(obj[i].value == newMode) {
			obj[i].checked = true;	
		} else {
			obj[i].	checked = false;
		}
	}
	var rowObj = window.document.getElementsByName('propertyNameRow');
	
	if(rowObj.length > 0) {
		if(newMode == 1) {
			var display = 'table-row';
		} else {
			var display = 'none';
		}
		rowObj[0].style.display = display;	
	}
	
}


// Update the project listing for an office
function updateContractorProjectTableDueToStatus() {
	
	var selectedIndexObj = window.document.getElementsByName('statusTableSelect')[0];
	var selectedIndex = selectedIndexObj.options.selectedIndex;
	var statusValue = selectedIndexObj.options[selectedIndex].value;
	
	var url = new String(window.document.location.href);
	var requestVar = getURLVar(url);
	var numRequests = requestVar.length;
	
	var requestBegin = url.indexOf('?');
	if(requestBegin != -1) {
		url = url.substr(0,requestBegin);
	}
	
	
	var breakVar = "?";
	for(var i=0;i<numRequests;i++) {
		if(	requestVar[i][0] != "page" &&
			requestVar[i][0] != "status") {
			
			
			url = url + breakVar + requestVar[i][0] + '=' + requestVar[i][1];
			breakVar = '&';
		}
	}
	
	var address = url + breakVar + 'page=1&status=' + statusValue;
	window.location = address;
	
}



// Update the project listing for an office
function updateProjectTableDueToPageNumber(pageNumberValue) {
	
	var url = new String(window.document.location.href);
	var requestVar = getURLVar(url);
	var numRequests = requestVar.length;
	
	var requestBegin = url.indexOf('?');
	if(requestBegin != -1) {
		url = url.substr(0,requestBegin);
	}
	
	
	var breakVar = "?";
	for(var i=0;i<numRequests;i++) {
		if(	requestVar[i][0] != "page") {
			url = url + breakVar + requestVar[i][0] + '=' + requestVar[i][1];
			breakVar = '&';
		}
	}
	
	var address = url + breakVar + 'page=' + pageNumberValue;
	window.location = address;
	
}



// Update Project Status in database
function updateProjectStatus(projectID) {
	
	var prevID = "prevProjectStatus_" + projectID;
	var id = "projectStatus_" + projectID;
	var prevProjectStatus = window.document.getElementById(prevID);
	var projectStatus = window.document.getElementById(id);
	var newValue = projectStatus.value;
	var refreshPage = false;
	
	
	// If the project is being closed,
	// warn the user first to make sure they want to close it.
	//
	// Either way, set the flag to refresh the page after the question because either:
	// 1) The project is closed and the drop-down status menu needs to be replaced by 
	// the static field,
	// -or-
	// 2) The drop-down status menu needs to return to its previous value.
	//
	if(newValue == 2) {
		answer = confirm("If you close this project, the project will be PERMANENTLY locked and no further changes can be made.\n\nAre you ABSOLUTELY certain you wish to close this project?");
		if(answer == false) {
			projectStatus.value = prevProjectStatus.value;
			return false;
		}
		refreshPage = true;
	}
	
	
	// If the value is to be updated, update the field
	// otherwise, return the old value to the field and stop.
	prevProjectStatus.value = projectStatus.value;
	
	
	
	// Create a hidden frame
	createFrame(window,'hiddenFrame');
	var myForm = addFormToWindow(window,'myForm');
	
	// Update the field through a second window
	myForm.action ='/site/projects/updateProjectStatus.php';
	if(refreshPage == true) {
		myForm.target = '_top';
	} else {
		myForm.target = 'hiddenFrame';
	}
	myForm.method = 'post';
	addPostToForm(myForm,'id',projectID);
	addPostToForm(myForm,'statusID',newValue);
	myForm.submit();
	
}

//
// Update Work Plan
// (For Multi-line Update)
//
function workPlanUpdateMultiline() {
	window.document.workPlanForm.action = '/site/projects/projectWorkPlanUpdateMultiline.php';
	window.document.workPlanForm.method = 'post';
	window.document.workPlanForm.target = '_top';
	window.document.workPlanForm.submit();
}


//===========================================
//
// Work Plan Functionality
//
function addWorkPlanItem() {
	var selectedIndex = window.document.workPlanForm.categoryTypeToAdd.options.selectedIndex;
	var category = window.document.workPlanForm.categoryTypeToAdd.options[selectedIndex].value;
	if(category > 0) {
		window.location = '/site/projects/projectWorkPlanAdd.php?cat=' + category;
	} else {
		alert("Choose category before adding item.");	
	}
}

function updateWorkPlan(wpID,field) {
	var answer = true;
	var elementName = "workPlan" + field + "_" + wpID;
	if(field != "Delete") {
		var element = window.document.getElementsByName(elementName);
		if(field == "Status") {
			var selectedIndex = element[0].options.selectedIndex;
			var newValue = element[0].options[selectedIndex].value;
		} else {
			var newValue = element[0].value;
		}
	} else {
		answer = confirm("Are you sure you wish to delete this item?");
		if(answer == false) {
			return false;	
		}
	}
	if(field != "") {
		var action = "/site/projects/projectWorkPlanUpdate.php?field=" + field + "&id=" + wpID;
		window.document.workPlanForm.action = action;
		if(field != "Delete") {
			window.document.workPlanForm.target = "hiddenFrame";
		} else {
			window.document.workPlanForm.target = "_top";	
		}
		window.document.workPlanForm.submit();
	} else {
		alert("Invalid data entered.");	
	}

}


//=======================================
//
// Work Plan Calendar Functions
//

// CONSTRUCTOR for the CalendarPopup Object
function workPlanCalendarPopup(wpid) {

	var c;
	if (arguments.length>1) {
		c = new PopupWindow(arguments[1]);
		}
	else {
		c = new PopupWindow();
		c.setSize(150,175);
	}

//	var c;
//	c = new PopupWindow();
	c.setSize(150,175);
	c.offsetX = -152;
	c.offsetY = 25;
	c.autoHide();
	// Calendar-specific properties
	c.monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	c.monthAbbreviations = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	c.dayHeaders = new Array("S","M","T","W","T","F","S");
	c.returnFunction = "CP_tmpReturnFunction_workPlanDateCompleted";
	c.returnMonthFunction = "CP_tmpReturnMonthFunction";
	c.returnQuarterFunction = "CP_tmpReturnQuarterFunction";
	c.returnYearFunction = "CP_tmpReturnYearFunction";
	c.weekStartDay = 0;
	c.isShowYearNavigation = false;
	c.displayType = "date";
	c.disabledWeekDays = new Object();
	c.disabledDatesExpression = "";
	c.yearSelectStartOffset = 2;
	c.currentDate = null;
	c.todayText="Today";
	c.cssPrefix="";
	c.isShowNavigationDropdowns=false;
	c.isShowYearNavigationInput=false;
	window.CP_calendarObject = null;
	window.CP_targetInput = null;
	window.CP_dateFormat = "MM/dd/yy";
	c.CP_workPlanID = wpid;
	// Method mappings
	c.copyMonthNamesToWindow = CP_copyMonthNamesToWindow;
	c.setReturnFunction = CP_setReturnFunction;
	c.setReturnMonthFunction = CP_setReturnMonthFunction;
	c.setReturnQuarterFunction = CP_setReturnQuarterFunction;
	c.setReturnYearFunction = CP_setReturnYearFunction;
	c.setMonthNames = CP_setMonthNames;
	c.setMonthAbbreviations = CP_setMonthAbbreviations;
	c.setDayHeaders = CP_setDayHeaders;
	c.setWeekStartDay = CP_setWeekStartDay;
	c.setDisplayType = CP_setDisplayType;
	c.setDisabledWeekDays = CP_setDisabledWeekDays;
	c.addDisabledDates = CP_addDisabledDates;
	c.setYearSelectStartOffset = CP_setYearSelectStartOffset;
	c.setTodayText = CP_setTodayText;
	c.showYearNavigation = CP_showYearNavigation;
	c.showCalendar = CP_showCalendar;
	c.hideCalendar = CP_hideCalendar;
	c.getStyles = getCalendarStyles;
	c.refreshCalendar = CP_refreshCalendar;
	c.getCalendar = CP_getCalendar_workPlan;
	c.select = CP_select;
	c.setCssPrefix = CP_setCssPrefix;
	c.showNavigationDropdowns = CP_showNavigationDropdowns;
	c.showYearNavigationInput = CP_showYearNavigationInput;
	c.copyMonthNamesToWindow();
	// Return the object
	return c;
}




// Temporary default functions to be called when items clicked, so no error is thrown
function CP_tmpReturnFunction_workPlanDateCompleted(y,m,d,wpid) { 
	if (window.CP_targetInput!=null) {
		var dt = new Date(y,m-1,d,0,0,0);
		if (window.CP_calendarObject!=null) { window.CP_calendarObject.copyMonthNamesToWindow(); }
		window.CP_targetInput.value = formatDate(dt,window.CP_dateFormat);
		updateWorkPlan(wpid,'DateCompleted');
	} else {
		alert('Use setReturnFunction() to define which function will get the clicked results!'); 
	}
}


// Return a string containing all the calendar code to be displayed
function CP_getCalendar_workPlan() {
	var now = new Date();
	// Reference to window
	if (this.type == "WINDOW") { var windowref = "window.opener."; }
	else { var windowref = ""; }
	var result = "";
	// If POPUP, write entire HTML document
	if (this.type == "WINDOW") {
		result += "<HTML><HEAD><TITLE>Calendar</TITLE>"+this.getStyles()+"</HEAD><BODY MARGINWIDTH=0 MARGINHEIGHT=0 TOPMARGIN=0 RIGHTMARGIN=0 LEFTMARGIN=0>\n";
		result += '<CENTER><TABLE WIDTH=100% BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>\n';
		}
	else {
		result += '<TABLE CLASS="'+this.cssPrefix+'cpBorder" WIDTH=144 BORDER=1 BORDERWIDTH=1 CELLSPACING=0 CELLPADDING=1>\n';
		result += '<TR><TD ALIGN=CENTER>\n';
		result += '<CENTER>\n';
		}
	// Code for DATE display (default)
	// -------------------------------
	if (this.displayType=="date" || this.displayType=="week-end") {
		if (this.currentDate==null) { this.currentDate = now; }
		if (arguments.length > 0) { var month = arguments[0]; }
			else { var month = this.currentDate.getMonth()+1; }
		if (arguments.length > 1 && arguments[1]>0 && arguments[1]-0==arguments[1]) { var year = arguments[1]; }
			else { var year = this.currentDate.getFullYear(); }
		var daysinmonth= new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
		if ( ( (year%4 == 0)&&(year%100 != 0) ) || (year%400 == 0) ) {
			daysinmonth[2] = 29;
			}
		var current_month = new Date(year,month-1,1);
		var display_year = year;
		var display_month = month;
		var display_date = 1;
		var weekday= current_month.getDay();
		var offset = 0;
		
		offset = (weekday >= this.weekStartDay) ? weekday-this.weekStartDay : 7-this.weekStartDay+weekday ;
		if (offset > 0) {
			display_month--;
			if (display_month < 1) { display_month = 12; display_year--; }
			display_date = daysinmonth[display_month]-offset+1;
			}
		var next_month = month+1;
		var next_month_year = year;
		if (next_month > 12) { next_month=1; next_month_year++; }
		var last_month = month-1;
		var last_month_year = year;
		if (last_month < 1) { last_month=12; last_month_year--; }
		var date_class;
		if (this.type!="WINDOW") {
			result += "<TABLE WIDTH=144 BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>";
			}
		result += '<TR>\n';
		var refresh = windowref+'CP_refreshCalendar';
		var refreshLink = 'javascript:' + refresh;
		if (this.isShowNavigationDropdowns) {
			result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="78" COLSPAN="3"><select CLASS="'+this.cssPrefix+'cpMonthNavigation" name="cpMonth" onmouseup="CP_stop(event)"  onChange="'+refresh+'('+this.index+',this.options[this.selectedIndex].value-0,'+(year-0)+');">';
			for( var monthCounter=1; monthCounter<=12; monthCounter++ ) {
				var selected = (monthCounter==month) ? 'SELECTED' : '';
				result += '<option value="'+monthCounter+'" '+selected+'>'+this.monthNames[monthCounter-1]+'</option>';
				}
			result += '</select></TD>';
			result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="10">&nbsp;</TD>';

			result += '<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="56" COLSPAN="3"><select CLASS="'+this.cssPrefix+'cpYearNavigation" name="cpYear" onmouseup="CP_stop(event)"  onChange="'+refresh+'('+this.index+','+month+',this.options[this.selectedIndex].value-0);">';
			for( var yearCounter=year-this.yearSelectStartOffset; yearCounter<=year+this.yearSelectStartOffset; yearCounter++ ) {
				var selected = (yearCounter==year) ? 'SELECTED' : '';
				result += '<option value="'+yearCounter+'" '+selected+'>'+yearCounter+'</option>';
				}
			result += '</select></TD>';
			}
		else {
			if (this.isShowYearNavigation) {
				result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="10"><A CLASS="'+this.cssPrefix+'cpMonthNavigation" HREF="'+refreshLink+'('+this.index+','+last_month+','+last_month_year+');">&lt;</A></TD>';
				result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="58"><SPAN CLASS="'+this.cssPrefix+'cpMonthNavigation">'+this.monthNames[month-1]+'</SPAN></TD>';
				result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="10"><A CLASS="'+this.cssPrefix+'cpMonthNavigation" HREF="'+refreshLink+'('+this.index+','+next_month+','+next_month_year+');">&gt;</A></TD>';
				result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="10">&nbsp;</TD>';

				result += '<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="10"><A CLASS="'+this.cssPrefix+'cpYearNavigation" HREF="'+refreshLink+'('+this.index+','+month+','+(year-1)+');">&lt;</A></TD>';
				if (this.isShowYearNavigationInput) {
					result += '<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="36"><INPUT NAME="cpYear" CLASS="'+this.cssPrefix+'cpYearNavigation" SIZE="4" MAXLENGTH="4" VALUE="'+year+'" onBlur="'+refresh+'('+this.index+','+month+',this.value-0);"></TD>';
					}
				else {
					result += '<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="36"><SPAN CLASS="'+this.cssPrefix+'cpYearNavigation">'+year+'</SPAN></TD>';
					}
				result += '<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="10"><A CLASS="'+this.cssPrefix+'cpYearNavigation" HREF="'+refreshLink+'('+this.index+','+month+','+(year+1)+');">&gt;</A></TD>';
				}
			else {
				result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="22"><A CLASS="'+this.cssPrefix+'cpMonthNavigation" HREF="'+refreshLink+'('+this.index+','+last_month+','+last_month_year+');">&lt;&lt;</A></TD>\n';
				result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="100"><SPAN CLASS="'+this.cssPrefix+'cpMonthNavigation">'+this.monthNames[month-1]+' '+year+'</SPAN></TD>\n';
				result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="22"><A CLASS="'+this.cssPrefix+'cpMonthNavigation" HREF="'+refreshLink+'('+this.index+','+next_month+','+next_month_year+');">&gt;&gt;</A></TD>\n';
				}
			}
		result += '</TR></TABLE>\n';
		result += '<TABLE WIDTH=120 BORDER=0 CELLSPACING=0 CELLPADDING=1 ALIGN=CENTER>\n';
		result += '<TR>\n';
		for (var j=0; j<7; j++) {

			result += '<TD CLASS="'+this.cssPrefix+'cpDayColumnHeader" WIDTH="14%"><SPAN CLASS="'+this.cssPrefix+'cpDayColumnHeader">'+this.dayHeaders[(this.weekStartDay+j)%7]+'</TD>\n';
			}
		result += '</TR>\n';
		for (var row=1; row<=6; row++) {
			result += '<TR>\n';
			for (var col=1; col<=7; col++) {
				var disabled=false;
				if (this.disabledDatesExpression!="") {
					var ds=""+display_year+LZ(display_month)+LZ(display_date);
					eval("disabled=("+this.disabledDatesExpression+")");
					}
				var dateClass = "";
				if ((display_month == this.currentDate.getMonth()+1) && (display_date==this.currentDate.getDate()) && (display_year==this.currentDate.getFullYear())) {
					dateClass = "cpCurrentDate";
					}
				else if (display_month == month) {
					dateClass = "cpCurrentMonthDate";
					}
				else {
					dateClass = "cpOtherMonthDate";
					}
				if (disabled || this.disabledWeekDays[col-1]) {
					result += '	<TD CLASS="'+this.cssPrefix+dateClass+'"><SPAN CLASS="'+this.cssPrefix+dateClass+'Disabled">'+display_date+'</SPAN></TD>\n';
					}
				else {
					var selected_date = display_date;
					var selected_month = display_month;
					var selected_year = display_year;
					if (this.displayType=="week-end") {
						var d = new Date(selected_year,selected_month-1,selected_date,0,0,0,0);
						d.setDate(d.getDate() + (7-col));
						selected_year = d.getYear();
						if (selected_year < 1000) { selected_year += 1900; }
						selected_month = d.getMonth()+1;
						selected_date = d.getDate();
						}
					result += '	<TD CLASS="'+this.cssPrefix+dateClass+'"><A HREF="javascript:'+windowref+this.returnFunction+'('+selected_year+','+selected_month+','+selected_date+',' + this.CP_workPlanID + ');'+windowref+'CP_hideCalendar(\''+this.index+'\');" CLASS="'+this.cssPrefix+dateClass+'">'+display_date+'</A></TD>\n';
					}
				display_date++;
				if (display_date > daysinmonth[display_month]) {
					display_date=1;
					display_month++;
					}
				if (display_month > 12) {
					display_month=1;
					display_year++;
					}
				}
			result += '</TR>';
			}
		var current_weekday = now.getDay() - this.weekStartDay;
		if (current_weekday < 0) {
			current_weekday += 7;
			}
		result += '<TR>\n';
		result += '	<TD COLSPAN=7 ALIGN=CENTER CLASS="'+this.cssPrefix+'cpTodayText">\n';
		if (this.disabledDatesExpression!="") {
			var ds=""+now.getFullYear()+LZ(now.getMonth()+1)+LZ(now.getDate());
			eval("disabled=("+this.disabledDatesExpression+")");
			}
		if (disabled || this.disabledWeekDays[current_weekday+1]) {
			result += '		<SPAN CLASS="'+this.cssPrefix+'cpTodayTextDisabled">'+this.todayText+'</SPAN>\n';
			}
		else {
			result += '		<A CLASS="'+this.cssPrefix+'cpTodayText" HREF="javascript:'+windowref+this.returnFunction+'(\''+now.getFullYear()+'\',\''+(now.getMonth()+1)+'\',\''+now.getDate()+'\');'+windowref+'CP_hideCalendar(\''+this.index+'\');">'+this.todayText+'</A>\n';
			}
		result += '		<BR>\n';
		result += '	</TD></TR></TABLE></CENTER></TD></TR></TABLE>\n';
	}

	// Code common for MONTH, QUARTER, YEAR
	// ------------------------------------
	if (this.displayType=="month" || this.displayType=="quarter" || this.displayType=="year") {
		if (arguments.length > 0) { var year = arguments[0]; }
		else { 
			if (this.displayType=="year") {	var year = now.getFullYear()-this.yearSelectStartOffset; }
			else { var year = now.getFullYear(); }
			}
		if (this.displayType!="year" && this.isShowYearNavigation) {
			result += "<TABLE WIDTH=144 BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>";
			result += '<TR>\n';
			result += '	<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="22"><A CLASS="'+this.cssPrefix+'cpYearNavigation" HREF="javascript:'+windowref+'CP_refreshCalendar('+this.index+','+(year-1)+');">&lt;&lt;</A></TD>\n';
			result += '	<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="100">'+year+'</TD>\n';
			result += '	<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="22"><A CLASS="'+this.cssPrefix+'cpYearNavigation" HREF="javascript:'+windowref+'CP_refreshCalendar('+this.index+','+(year+1)+');">&gt;&gt;</A></TD>\n';
			result += '</TR></TABLE>\n';
			}
		}
		
	// Code for MONTH display 
	// ----------------------
	if (this.displayType=="month") {
		// If POPUP, write entire HTML document
		result += '<TABLE WIDTH=120 BORDER=0 CELLSPACING=1 CELLPADDING=0 ALIGN=CENTER>\n';
		for (var i=0; i<4; i++) {
			result += '<TR>';
			for (var j=0; j<3; j++) {
				var monthindex = ((i*3)+j);
				result += '<TD WIDTH=33% ALIGN=CENTER><A CLASS="'+this.cssPrefix+'cpText" HREF="javascript:'+windowref+this.returnMonthFunction+'('+year+','+(monthindex+1)+');'+windowref+'CP_hideCalendar(\''+this.index+'\');" CLASS="'+date_class+'">'+this.monthAbbreviations[monthindex]+'</A></TD>';
				}
			result += '</TR>';
			}
		result += '</TABLE></CENTER></TD></TR></TABLE>\n';
		}
	
	// Code for QUARTER display
	// ------------------------
	if (this.displayType=="quarter") {
		result += '<BR><TABLE WIDTH=120 BORDER=1 CELLSPACING=0 CELLPADDING=0 ALIGN=CENTER>\n';
		for (var i=0; i<2; i++) {
			result += '<TR>';
			for (var j=0; j<2; j++) {
				var quarter = ((i*2)+j+1);
				result += '<TD WIDTH=50% ALIGN=CENTER><BR><A CLASS="'+this.cssPrefix+'cpText" HREF="javascript:'+windowref+this.returnQuarterFunction+'('+year+','+quarter+');'+windowref+'CP_hideCalendar(\''+this.index+'\');" CLASS="'+date_class+'">Q'+quarter+'</A><BR><BR></TD>';
				}
			result += '</TR>';
			}
		result += '</TABLE></CENTER></TD></TR></TABLE>\n';
		}

	// Code for YEAR display
	// ---------------------
	if (this.displayType=="year") {
		var yearColumnSize = 4;
		result += "<TABLE WIDTH=144 BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>";
		result += '<TR>\n';
		result += '	<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="50%"><A CLASS="'+this.cssPrefix+'cpYearNavigation" HREF="javascript:'+windowref+'CP_refreshCalendar('+this.index+','+(year-(yearColumnSize*2))+');">&lt;&lt;</A></TD>\n';
		result += '	<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="50%"><A CLASS="'+this.cssPrefix+'cpYearNavigation" HREF="javascript:'+windowref+'CP_refreshCalendar('+this.index+','+(year+(yearColumnSize*2))+');">&gt;&gt;</A></TD>\n';
		result += '</TR></TABLE>\n';
		result += '<TABLE WIDTH=120 BORDER=0 CELLSPACING=1 CELLPADDING=0 ALIGN=CENTER>\n';
		for (var i=0; i<yearColumnSize; i++) {
			for (var j=0; j<2; j++) {
				var currentyear = year+(j*yearColumnSize)+i;
				result += '<TD WIDTH=50% ALIGN=CENTER><A CLASS="'+this.cssPrefix+'cpText" HREF="javascript:'+windowref+this.returnYearFunction+'('+currentyear+');'+windowref+'CP_hideCalendar(\''+this.index+'\');" CLASS="'+date_class+'">'+currentyear+'</A></TD>';
				}
			result += '</TR>';
			}
		result += '</TABLE></CENTER></TD></TR></TABLE>\n';
		}
	// Common
	if (this.type == "WINDOW") {
		result += "</BODY></HTML>\n";
		}
	return result;
	}


//===============================
//
// hhmm2hhmmss
//
function hhmm2hhmmss(timeValue) {



	var trimmedTimeValue = trim(timeValue);
	var timeArray = trimmedTimeValue.split(':');
	var hour = parseInt(timeArray[0],10);
	var timeArrayPart2 = timeArray[1].split(' ');
	var minute = parseInt(timeArrayPart2[0],10);
	var meridian = timeArrayPart2[1];
	
	if(hour == 12 && (meridian == "am" || meridian == "AM")) {
		hour = 0;	
	} else if(hour < 12 && (meridian == "pm" || meridian == "PM")) {
		hour += 12;	
	}
	
	var hourString = "";
	if(hour < 10) {
		hourString = "0" + hour;
	} else {
		hourString = hour;
	}
	
	var minuteString = "";
	if(minute < 10) {
		minuteString = "0" + minute;	
	} else {
		minuteString = minute;	
	}
	
	
	var timeString = hourString + ":" + minuteString + ':00';
	
	
	return timeString;
	
}

//===================================================
//
// Project Add/Edit
//
function updateTimeOnJob() {

	var startDate = window.document.projectForm.prelimReportDate.value;
	var startTime = window.document.projectForm.prelimReportTime.value;
	
	var endDate = window.document.projectForm.jobEndDate.value;
	var endTime = window.document.projectForm.jobEndTime.value;
	
	
	
	
	if((startDate > "0000-00-00") && (endDate > "0000-00-00")) {
		
		
		if( (startTime == "") || (endTime == "")) {
			var startTimeUsed = "00:00:00";	
			var endTimeUsed = "00:00:00";	
		} else {
			var startTimeUsed = startTime;
			var endTimeUsed = endTime;
			
			startTimeUsed = hhmm2hhmmss(startTime);
			endTimeUsed = hhmm2hhmmss(endTime);
	
		}
		
		
		// Convert from mm/dd/yy to yyyy-mm-dd if needed
		var slashDate = startDate.indexOf('/');
		if(slashDate > -1) {
			startDate = mmddyy2mysqldate(startDate);	
		}
		var slashDate = endDate.indexOf('/');
		if(slashDate > -1) {
			endDate = mmddyy2mysqldate(endDate);	
		}
		
		var startDateArray = startDate.split('-');
		var endDateArray = endDate.split('-');
	
		
		var startTimeArray = startTimeUsed.split(':');
		var endTimeArray = endTimeUsed.split(':');
		
		
		var startDateVal = new Date(parseInt(startDateArray[0],10), parseInt(startDateArray[1],10) - 1, parseInt(startDateArray[2],10),
									parseInt(startTimeArray[0],10),parseInt(startTimeArray[1],10),parseInt(startTimeArray[2],10));
		var endDateVal = new Date(parseInt(endDateArray[0],10), parseInt(endDateArray[1],10)-1, parseInt(endDateArray[2],10),
								parseInt(endTimeArray[0],10),parseInt(endTimeArray[1],10),parseInt(endTimeArray[2],10));
		
		
		
		var deltaTimeMS = endDateVal.getTime() - startDateVal.getTime();
		
		
		if(deltaTimeMS >= 0) {
			var deltaDays = Math.floor(deltaTimeMS / (86400000));
		} else {
			var deltaDays = Math.ceil(deltaTimeMS / (86400000));
		}
		var deltaHours = (deltaTimeMS % 86400000)/3600000;
		
		if( (startTime == "") || (endTime == "")) {
			deltaHours = "";	
		}
		
		
		var deltaMinutes;
		if(deltaHours == "") {
			deltaMinutes = "";	
		} else if(deltaHours == Math.floor(deltaHours)) {
			deltaMinutes = "";	
		} else {
			deltaMinutes = Math.round((deltaHours - Math.floor(deltaHours)) * 60);	
		}
		
		
	} else {
		var deltaDays = "";
		var deltaHours = "";
		var deltaMinutes = "";
	}
	
	window.document.projectForm.daysOnJob.value = deltaDays;
	
	var minutesObj = window.document.getElementsByName('minutesOnJob');
	if(minutesObj.length > 0) {
		minutesObj[0].value = deltaMinutes;	
		window.document.projectForm.hoursOnJob.value = Math.floor(deltaHours);
	} else {
		window.document.projectForm.hoursOnJob.value = deltaHours;	
	}
	
	
	// Alerts
	var timeOnJobLabelObj = window.document.getElementsByName('timeOnJobLabel')[0];
	var timeOnJobEntryObj = window.document.getElementsByName('timeOnJobEntry')[0];
	var negativeTimeWarning = window.document.getElementsByName('negativeTimeWarning')[0];
	
	if(((deltaDays <= 0 && deltaHours <= 0 && !(deltaDays == 0 && deltaHours == 0)) || startDate > endDate) && (startDate != "" && endDate != "") ) {
		timeOnJobLabelObj.style.color = 'red';
		timeOnJobEntryObj.style.color = 'red';
		negativeTimeWarning.value = 'Warning: Project Ended Before It Began!';
	} else {
		timeOnJobLabelObj.style.color = 'black';
		timeOnJobEntryObj.style.color = 'black';
		negativeTimeWarning.value = '';
	}

}


//=======================================
//
// Work Plan Calendar Functions
//

// CONSTRUCTOR for the CalendarPopup Object
function projectEndDateCalendarPopup() {

	var c;
	if (arguments.length>0) {
		c = new PopupWindow(arguments[0]);
		}
	else {
		c = new PopupWindow();
		c.setSize(150,175);
	}

	c.setSize(150,175);
	c.offsetX = -152;
	c.offsetY = 25;
	c.autoHide();
	// Calendar-specific properties
	c.monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	c.monthAbbreviations = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	c.dayHeaders = new Array("S","M","T","W","T","F","S");
	c.returnFunction = "CP_tmpReturnFunction_projectEndDate";
	c.returnMonthFunction = "CP_tmpReturnMonthFunction";
	c.returnQuarterFunction = "CP_tmpReturnQuarterFunction";
	c.returnYearFunction = "CP_tmpReturnYearFunction";
	c.weekStartDay = 0;
	c.isShowYearNavigation = false;
	c.displayType = "date";
	c.disabledWeekDays = new Object();
	c.disabledDatesExpression = "";
	c.yearSelectStartOffset = 2;
	c.currentDate = null;
	c.todayText="Today";
	c.cssPrefix="";
	c.isShowNavigationDropdowns=false;
	c.isShowYearNavigationInput=false;
	window.CP_calendarObject = null;
	window.CP_targetInput = null;
	// Method mappings
	c.copyMonthNamesToWindow = CP_copyMonthNamesToWindow;
	c.setReturnFunction = CP_setReturnFunction;
	c.setReturnMonthFunction = CP_setReturnMonthFunction;
	c.setReturnQuarterFunction = CP_setReturnQuarterFunction;
	c.setReturnYearFunction = CP_setReturnYearFunction;
	c.setMonthNames = CP_setMonthNames;
	c.setMonthAbbreviations = CP_setMonthAbbreviations;
	c.setDayHeaders = CP_setDayHeaders;
	c.setWeekStartDay = CP_setWeekStartDay;
	c.setDisplayType = CP_setDisplayType;
	c.setDisabledWeekDays = CP_setDisabledWeekDays;
	c.addDisabledDates = CP_addDisabledDates;
	c.setYearSelectStartOffset = CP_setYearSelectStartOffset;
	c.setTodayText = CP_setTodayText;
	c.showYearNavigation = CP_showYearNavigation;
	c.showCalendar = CP_showCalendar;
	c.hideCalendar = CP_hideCalendar;
	c.getStyles = getCalendarStyles;
	c.refreshCalendar = CP_refreshCalendar;
	c.getCalendar = CP_getCalendar_projectEndDate;
	c.select = CP_select;
	c.setCssPrefix = CP_setCssPrefix;
	c.showNavigationDropdowns = CP_showNavigationDropdowns;
	c.showYearNavigationInput = CP_showYearNavigationInput;
	c.copyMonthNamesToWindow();
	// Return the object
	return c;
}




// Temporary default functions to be called when items clicked, so no error is thrown
function CP_tmpReturnFunction_projectEndDate(y,m,d,wpid) { 
	if (window.CP_targetInput!=null) {
		var dt = new Date(y,m-1,d,0,0,0);
		if (window.CP_calendarObject!=null) { window.CP_calendarObject.copyMonthNamesToWindow(); }
		window.CP_targetInput.value = formatDate(dt,window.CP_dateFormat);
		updateTimeOnJob();
	} else {
		alert('Use setReturnFunction() to define which function will get the clicked results!'); 
	}
}


// Return a string containing all the calendar code to be displayed
function CP_getCalendar_projectEndDate() {
	var now = new Date();
	// Reference to window
	if (this.type == "WINDOW") { var windowref = "window.opener."; }
	else { var windowref = ""; }
	var result = "";
	// If POPUP, write entire HTML document
	if (this.type == "WINDOW") {
		result += "<HTML><HEAD><TITLE>Calendar</TITLE>"+this.getStyles()+"</HEAD><BODY MARGINWIDTH=0 MARGINHEIGHT=0 TOPMARGIN=0 RIGHTMARGIN=0 LEFTMARGIN=0>\n";
		result += '<CENTER><TABLE WIDTH=100% BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>\n';
		}
	else {
		result += '<TABLE CLASS="'+this.cssPrefix+'cpBorder" WIDTH=144 BORDER=1 BORDERWIDTH=1 CELLSPACING=0 CELLPADDING=1>\n';
		result += '<TR><TD ALIGN=CENTER>\n';
		result += '<CENTER>\n';
		}
	// Code for DATE display (default)
	// -------------------------------
	if (this.displayType=="date" || this.displayType=="week-end") {
		if (this.currentDate==null) { this.currentDate = now; }
		if (arguments.length > 0) { var month = arguments[0]; }
			else { var month = this.currentDate.getMonth()+1; }
		if (arguments.length > 1 && arguments[1]>0 && arguments[1]-0==arguments[1]) { var year = arguments[1]; }
			else { var year = this.currentDate.getFullYear(); }
		var daysinmonth= new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
		if ( ( (year%4 == 0)&&(year%100 != 0) ) || (year%400 == 0) ) {
			daysinmonth[2] = 29;
			}
		var current_month = new Date(year,month-1,1);
		var display_year = year;
		var display_month = month;
		var display_date = 1;
		var weekday= current_month.getDay();
		var offset = 0;
		
		offset = (weekday >= this.weekStartDay) ? weekday-this.weekStartDay : 7-this.weekStartDay+weekday ;
		if (offset > 0) {
			display_month--;
			if (display_month < 1) { display_month = 12; display_year--; }
			display_date = daysinmonth[display_month]-offset+1;
			}
		var next_month = month+1;
		var next_month_year = year;
		if (next_month > 12) { next_month=1; next_month_year++; }
		var last_month = month-1;
		var last_month_year = year;
		if (last_month < 1) { last_month=12; last_month_year--; }
		var date_class;
		if (this.type!="WINDOW") {
			result += "<TABLE WIDTH=144 BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>";
			}
		result += '<TR>\n';
		var refresh = windowref+'CP_refreshCalendar';
		var refreshLink = 'javascript:' + refresh;
		if (this.isShowNavigationDropdowns) {
			result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="78" COLSPAN="3"><select CLASS="'+this.cssPrefix+'cpMonthNavigation" name="cpMonth" onmouseup="CP_stop(event);" onChange="'+refresh+'('+this.index+',this.options[this.selectedIndex].value-0,'+(year-0)+');">';
			for( var monthCounter=1; monthCounter<=12; monthCounter++ ) {
				var selected = (monthCounter==month) ? 'SELECTED' : '';
				result += '<option value="'+monthCounter+'" '+selected+'>'+this.monthNames[monthCounter-1]+'</option>';
				}
			result += '</select></TD>';
			result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="10">&nbsp;</TD>';

			result += '<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="56" COLSPAN="3"><select CLASS="'+this.cssPrefix+'cpYearNavigation" name="cpYear" onmouseup="CP_stop(event)"  onChange="'+refresh+'('+this.index+','+month+',this.options[this.selectedIndex].value-0);">';
			for( var yearCounter=year-this.yearSelectStartOffset; yearCounter<=year+this.yearSelectStartOffset; yearCounter++ ) {
				var selected = (yearCounter==year) ? 'SELECTED' : '';
				result += '<option value="'+yearCounter+'" '+selected+'>'+yearCounter+'</option>';
				}
			result += '</select></TD>';
			}
		else {
			if (this.isShowYearNavigation) {
				result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="10"><A CLASS="'+this.cssPrefix+'cpMonthNavigation" HREF="'+refreshLink+'('+this.index+','+last_month+','+last_month_year+');">&lt;</A></TD>';
				result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="58"><SPAN CLASS="'+this.cssPrefix+'cpMonthNavigation">'+this.monthNames[month-1]+'</SPAN></TD>';
				result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="10"><A CLASS="'+this.cssPrefix+'cpMonthNavigation" HREF="'+refreshLink+'('+this.index+','+next_month+','+next_month_year+');">&gt;</A></TD>';
				result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="10">&nbsp;</TD>';

				result += '<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="10"><A CLASS="'+this.cssPrefix+'cpYearNavigation" HREF="'+refreshLink+'('+this.index+','+month+','+(year-1)+');">&lt;</A></TD>';
				if (this.isShowYearNavigationInput) {
					result += '<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="36"><INPUT NAME="cpYear" CLASS="'+this.cssPrefix+'cpYearNavigation" SIZE="4" MAXLENGTH="4" VALUE="'+year+'" onBlur="'+refresh+'('+this.index+','+month+',this.value-0);"></TD>';
					}
				else {
					result += '<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="36"><SPAN CLASS="'+this.cssPrefix+'cpYearNavigation">'+year+'</SPAN></TD>';
					}
				result += '<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="10"><A CLASS="'+this.cssPrefix+'cpYearNavigation" HREF="'+refreshLink+'('+this.index+','+month+','+(year+1)+');">&gt;</A></TD>';
				}
			else {
				result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="22"><A CLASS="'+this.cssPrefix+'cpMonthNavigation" HREF="'+refreshLink+'('+this.index+','+last_month+','+last_month_year+');">&lt;&lt;</A></TD>\n';
				result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="100"><SPAN CLASS="'+this.cssPrefix+'cpMonthNavigation">'+this.monthNames[month-1]+' '+year+'</SPAN></TD>\n';
				result += '<TD CLASS="'+this.cssPrefix+'cpMonthNavigation" WIDTH="22"><A CLASS="'+this.cssPrefix+'cpMonthNavigation" HREF="'+refreshLink+'('+this.index+','+next_month+','+next_month_year+');">&gt;&gt;</A></TD>\n';
				}
			}
		result += '</TR></TABLE>\n';
		result += '<TABLE WIDTH=120 BORDER=0 CELLSPACING=0 CELLPADDING=1 ALIGN=CENTER>\n';
		result += '<TR>\n';
		for (var j=0; j<7; j++) {

			result += '<TD CLASS="'+this.cssPrefix+'cpDayColumnHeader" WIDTH="14%"><SPAN CLASS="'+this.cssPrefix+'cpDayColumnHeader">'+this.dayHeaders[(this.weekStartDay+j)%7]+'</TD>\n';
			}
		result += '</TR>\n';
		for (var row=1; row<=6; row++) {
			result += '<TR>\n';
			for (var col=1; col<=7; col++) {
				var disabled=false;
				if (this.disabledDatesExpression!="") {
					var ds=""+display_year+LZ(display_month)+LZ(display_date);
					eval("disabled=("+this.disabledDatesExpression+")");
					}
				var dateClass = "";
				if ((display_month == this.currentDate.getMonth()+1) && (display_date==this.currentDate.getDate()) && (display_year==this.currentDate.getFullYear())) {
					dateClass = "cpCurrentDate";
					}
				else if (display_month == month) {
					dateClass = "cpCurrentMonthDate";
					}
				else {
					dateClass = "cpOtherMonthDate";
					}
				if (disabled || this.disabledWeekDays[col-1]) {
					result += '	<TD CLASS="'+this.cssPrefix+dateClass+'"><SPAN CLASS="'+this.cssPrefix+dateClass+'Disabled">'+display_date+'</SPAN></TD>\n';
					}
				else {
					var selected_date = display_date;
					var selected_month = display_month;
					var selected_year = display_year;
					if (this.displayType=="week-end") {
						var d = new Date(selected_year,selected_month-1,selected_date,0,0,0,0);
						d.setDate(d.getDate() + (7-col));
						selected_year = d.getYear();
						if (selected_year < 1000) { selected_year += 1900; }
						selected_month = d.getMonth()+1;
						selected_date = d.getDate();
						}
					result += '	<TD CLASS="'+this.cssPrefix+dateClass+'"><A HREF="javascript:'+windowref+this.returnFunction+'('+selected_year+','+selected_month+','+selected_date+',' + this.CP_workPlanID + ');'+windowref+'CP_hideCalendar(\''+this.index+'\');" CLASS="'+this.cssPrefix+dateClass+'">'+display_date+'</A></TD>\n';
					}
				display_date++;
				if (display_date > daysinmonth[display_month]) {
					display_date=1;
					display_month++;
					}
				if (display_month > 12) {
					display_month=1;
					display_year++;
					}
				}
			result += '</TR>';
			}
		var current_weekday = now.getDay() - this.weekStartDay;
		if (current_weekday < 0) {
			current_weekday += 7;
			}
		result += '<TR>\n';
		result += '	<TD COLSPAN=7 ALIGN=CENTER CLASS="'+this.cssPrefix+'cpTodayText">\n';
		if (this.disabledDatesExpression!="") {
			var ds=""+now.getFullYear()+LZ(now.getMonth()+1)+LZ(now.getDate());
			eval("disabled=("+this.disabledDatesExpression+")");
			}
		if (disabled || this.disabledWeekDays[current_weekday+1]) {
			result += '		<SPAN CLASS="'+this.cssPrefix+'cpTodayTextDisabled">'+this.todayText+'</SPAN>\n';
			}
		else {
			result += '		<A CLASS="'+this.cssPrefix+'cpTodayText" HREF="javascript:'+windowref+this.returnFunction+'(\''+now.getFullYear()+'\',\''+(now.getMonth()+1)+'\',\''+now.getDate()+'\');'+windowref+'CP_hideCalendar(\''+this.index+'\');">'+this.todayText+'</A>\n';
			}
		result += '		<BR>\n';
		result += '	</TD></TR></TABLE></CENTER></TD></TR></TABLE>\n';
	}

	// Code common for MONTH, QUARTER, YEAR
	// ------------------------------------
	if (this.displayType=="month" || this.displayType=="quarter" || this.displayType=="year") {
		if (arguments.length > 0) { var year = arguments[0]; }
		else { 
			if (this.displayType=="year") {	var year = now.getFullYear()-this.yearSelectStartOffset; }
			else { var year = now.getFullYear(); }
			}
		if (this.displayType!="year" && this.isShowYearNavigation) {
			result += "<TABLE WIDTH=144 BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>";
			result += '<TR>\n';
			result += '	<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="22"><A CLASS="'+this.cssPrefix+'cpYearNavigation" HREF="javascript:'+windowref+'CP_refreshCalendar('+this.index+','+(year-1)+');">&lt;&lt;</A></TD>\n';
			result += '	<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="100">'+year+'</TD>\n';
			result += '	<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="22"><A CLASS="'+this.cssPrefix+'cpYearNavigation" HREF="javascript:'+windowref+'CP_refreshCalendar('+this.index+','+(year+1)+');">&gt;&gt;</A></TD>\n';
			result += '</TR></TABLE>\n';
			}
		}
		
	// Code for MONTH display 
	// ----------------------
	if (this.displayType=="month") {
		// If POPUP, write entire HTML document
		result += '<TABLE WIDTH=120 BORDER=0 CELLSPACING=1 CELLPADDING=0 ALIGN=CENTER>\n';
		for (var i=0; i<4; i++) {
			result += '<TR>';
			for (var j=0; j<3; j++) {
				var monthindex = ((i*3)+j);
				result += '<TD WIDTH=33% ALIGN=CENTER><A CLASS="'+this.cssPrefix+'cpText" HREF="javascript:'+windowref+this.returnMonthFunction+'('+year+','+(monthindex+1)+');'+windowref+'CP_hideCalendar(\''+this.index+'\');" CLASS="'+date_class+'">'+this.monthAbbreviations[monthindex]+'</A></TD>';
				}
			result += '</TR>';
			}
		result += '</TABLE></CENTER></TD></TR></TABLE>\n';
		}
	
	// Code for QUARTER display
	// ------------------------
	if (this.displayType=="quarter") {
		result += '<BR><TABLE WIDTH=120 BORDER=1 CELLSPACING=0 CELLPADDING=0 ALIGN=CENTER>\n';
		for (var i=0; i<2; i++) {
			result += '<TR>';
			for (var j=0; j<2; j++) {
				var quarter = ((i*2)+j+1);
				result += '<TD WIDTH=50% ALIGN=CENTER><BR><A CLASS="'+this.cssPrefix+'cpText" HREF="javascript:'+windowref+this.returnQuarterFunction+'('+year+','+quarter+');'+windowref+'CP_hideCalendar(\''+this.index+'\');" CLASS="'+date_class+'">Q'+quarter+'</A><BR><BR></TD>';
				}
			result += '</TR>';
			}
		result += '</TABLE></CENTER></TD></TR></TABLE>\n';
		}

	// Code for YEAR display
	// ---------------------
	if (this.displayType=="year") {
		var yearColumnSize = 4;
		result += "<TABLE WIDTH=144 BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>";
		result += '<TR>\n';
		result += '	<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="50%"><A CLASS="'+this.cssPrefix+'cpYearNavigation" HREF="javascript:'+windowref+'CP_refreshCalendar('+this.index+','+(year-(yearColumnSize*2))+');">&lt;&lt;</A></TD>\n';
		result += '	<TD CLASS="'+this.cssPrefix+'cpYearNavigation" WIDTH="50%"><A CLASS="'+this.cssPrefix+'cpYearNavigation" HREF="javascript:'+windowref+'CP_refreshCalendar('+this.index+','+(year+(yearColumnSize*2))+');">&gt;&gt;</A></TD>\n';
		result += '</TR></TABLE>\n';
		result += '<TABLE WIDTH=120 BORDER=0 CELLSPACING=1 CELLPADDING=0 ALIGN=CENTER>\n';
		for (var i=0; i<yearColumnSize; i++) {
			for (var j=0; j<2; j++) {
				var currentyear = year+(j*yearColumnSize)+i;
				result += '<TD WIDTH=50% ALIGN=CENTER><A CLASS="'+this.cssPrefix+'cpText" HREF="javascript:'+windowref+this.returnYearFunction+'('+currentyear+');'+windowref+'CP_hideCalendar(\''+this.index+'\');" CLASS="'+date_class+'">'+currentyear+'</A></TD>';
				}
			result += '</TR>';
			}
		result += '</TABLE></CENTER></TD></TR></TABLE>\n';
		}
	// Common
	if (this.type == "WINDOW") {
		result += "</BODY></HTML>\n";
		}
	return result;
	}


//============================================
//
// Drying Area List
//
function dryingAreaDelete(id) {
	var answer = confirm("Are you sure you wish to permanently delete this drying area?");
	if(answer == true) {
		window.location = "/site/projects/dryingArea/dryingAreaDelete.php?id=" + id;	
	}
} 


//=============================================
//
// Equipment Usage AutoFlip
// (EquipRowAddEdit)
//
function equipmentUsageAutoFlip(eqNumber,dateNumber,yesNo) {


	// Number of days
	var numDaysObj = window.document.getElementsByName('numDays');
	var numDays = parseInt(numDaysObj[0].value);
	

	// Loop over all days from current day to end
	for(var i=dateNumber;i<numDays;i++) {
		
		if(eqNumber != null) {
			var fieldName = "used_" + eqNumber + "_" + i;
		} else {
			var fieldName = "used_" + i;	
		}
		var fieldObject = window.document.getElementsByName(fieldName);
		var numObjects = parseInt(fieldObject.length);
		if(numObjects == 2) {
			
			for(var j=0;j<2;j++) {
				if(fieldObject[j].value == yesNo) {
					fieldObject[j].checked = true;
				} else {
					fieldObject[j].checked = false;	
				}
			}	
			
		} else {
			// If a day is not in use, stop the auto-flip
			i = numDays + 1;	
		}
	}
}



//==================================================
//
// Contacts
//
//==================================================

//
// Add Contact
//
function projectContactsAdd(contactType,projectID) {
	
	// The Form
	var myForms = window.document.getElementsByName('contactsForm');
	var myForm = myForms[0];
	
	// Contact Type
	addPostToForm(myForm,'contactType',contactType);
	
	// Project ID
	addPostToForm(myForm,'projectID',projectID);
	
	// Add New Contact
	myForm.method = 'POST';
	myForm.action = '/site/projects/contacts/contactsAdd.php';
	myForm.target = '_top';
	myForm.submit();
	
}

//
// Update Contact
//
function projectContactsUpdate(contactType,id,visible) {
	
	
	// The Form
	var myForms = window.document.getElementsByName('contactsForm');
	var myForm = myForms[0];
	
	
	// Get the name
	if(contactType == 'client') {
		var lastName = window.document.getElementsByName('clientLastName_' + id)[0].value;
		var firstName = window.document.getElementsByName('clientFirstName_' + id) [0].value;
		var itemName = firstName + ' ' + lastName;
	} else {
		var itemName = window.document.getElementsByName('clientCompanyName_' + id)[0].value;
	}

	if(visible == 'No') {
		var answer = confirm('Are you sure you wish to permanently delete:\n' + itemName + '?');
		if(answer == false) {
			return;	
		}
	}

	// contactType
	addPostToForm(myForm,'contactType',contactType);

	// id
	addPostToForm(myForm,'id',id);
	
	// visible
	addPostToForm(myForm,'visible',visible);
	
	
	// Update Contact
	if(visible == 'No') {	
		myForm.target = '_top';
	} else {
		createFrame(window,'hiddenFrame');
		myForm.target = 'hiddenFrame';
	}
	myForm.method = 'POST';
	myForm.action = '/site/projects/contacts/contactsUpdate.php';
	myForm.submit();

}



//
// Update Contact
//
function projectContactProjectUpdate(projectID) {
	
	
	// The Form
	var myForm = window.document.getElementsByName('contactsForm')[0];


	// ProjectID
	addPostToForm(myForm,'projectID',projectID);
	
	// Update Contact
	createFrame(window,'hiddenFrame');
	myForm.target = 'hiddenFrame';	
	myForm.method = 'POST';
	myForm.action = '/site/projects/contacts/contactsProjectUpdate.php';
	myForm.submit();

}



//========================================
//
// Generate Report
//
function reportModuleSelectorGenerateReport() {
	
	var myForm = window.document.getElementsByName('reportSelect')[0];
	
	//
	// Submit
	//
	myForm.action = '/site/genReport.php';
	myForm.target = '_top';
	myForm.method = 'POST';
	myForm.submit();

}


//========================================
//
// Generate Report
//
function reportModuleSelectorGenerateBrowserReport() {
	
	var myForm = window.document.getElementsByName('reportSelect')[0];
	
	//
	// Submit
	//
	myForm.action = '/site/genReport.php?var=print';
	myForm.target = '_blank';
	myForm.method = 'POST';
	myForm.submit();

}


//========================================
//
// Contractor Comments (Contact Log)
//
//========================================

function contractorCommentsEdit(postID,visible) {
	
	var myForm = addFormToWindow(window,'contractorCommentsForm');
	myForm.method = 'POST';
	myForm.target = '_top';
	
	if(visible == 'No') {
		var answer = confirm('Are you sure you wish to delete this post?');
		if(answer == false) {
			return;	
		}
	}
	
	addPostToForm(myForm,'visible',visible);
	addPostToForm(myForm,'postID',postID);
	
	if(visible == 'No') {
		myForm.action = '/site/projects/contactLog/contractorCommentsUpdate.php';
	} else {
		myForm.action = '/site/projects/contactLog/contractorCommentsEdit.php';
	}
	myForm.submit();
	
}



//========================================
//
// chooseOfficeToUnassigned 
//
//========================================
function chooseOfficeToUnassigned() {
	var myForm = addFormToWindow(window,'myForm');
	
	var officeID = window.document.getElementsByName('officeSelect')[0].value;
	addPostToForm(myForm, 'setOffice', 'set');
	addPostToForm(myForm, 'officeID', officeID);
	
	var testValue = window.document.getElementsByName('officeID')[0].value;
	myForm.method = 'POST';
	myForm.target = '_top';
	myForm.action = '/site/projects/selectOfficeError.php';
	myForm.submit();
}


//=========================================
//=========================================
//
// Amperage Cost
//
//=========================================
//=========================================
function updateAmperageCostPowerRate() {
	
	var myForm = window.document.getElementsByName('amperageCostForm')[0];
	
	var powerRateObj = window.document.getElementsByName('powerRate')[0];
	if(isNaN(powerRateObj.value)) {
		powerRateObj.value = 0.000;	
	}
	var powerRate = powerRateObj.value;
	
	var powerOneLessDay = window.document.getElementsByName('powerOneLessDaySelect')[0].value;
	
	
	//
	// Update the table
	//
	var numTypes = window.document.getElementsByName('numTypes')[0].value;
	var totalCost = 0;
	for(var i=0;i<numTypes;i++) {
		var numEquipment = window.document.getElementsByName('numEquipment_' + i)[0].value;
		var typeCost = new Number(0);
		for(var j=0;j<numEquipment;j++) {
			// get the power (kWh)
			var powerObj = window.document.getElementsByName('power_' + i + '_' + j)[0];
			var power = powerObj.value;
			
			// get the number of days in use
			var daysUsedObj = window.document.getElementsByName('daysUsed_' + i + '_' + j)[0];
			var daysUsedAll = window.document.getElementsByName('daysUsedAll_' + i + '_' + j)[0].value;
			var daysUsedLess = window.document.getElementsByName('daysUsedLess_' + i + '_' + j)[0].value;
			var daysUsed = daysUsedAll;
			if(powerOneLessDay == 'Yes') {
				var daysUsed = daysUsedLess;
			} 
			daysUsedObj.value = daysUsed;
			
			var costObj = window.document.getElementsByName('cost_' + i + '_' + j)[0];
			var cost = power * powerRate * daysUsed * 24;
			
			costObj.value = cost.toFixed(2);
			
			typeCost = typeCost + cost;
		}
		
		// get the type cost
		if(numEquipment > 0) {
			var typeCostObj = window.document.getElementsByName('typeCost_' + i)[0];
			typeCostObj.value = typeCost.toFixed(2);
			
			totalCost = totalCost + typeCost;
		}
	}
	
	// total cost
	var totalCostObj = window.document.getElementsByName('totalCost')[0];
	totalCostObj.value = totalCost.toFixed(2);
	
	
	//
	// Save the power rate value
	//
	addPostToForm(myForm,'powerRate',powerRate);
	addPostToForm(myForm,'powerOneLessDay',powerOneLessDay);
	createFrame(window,'hiddenFrame');
	myForm.method = 'post';
	myForm.target = 'hiddenFrame';
	myForm.action = '/site/projects/amperageCosts/updatePowerCosts.php';
	myForm.submit();
}


//===================================
//
// Project List Delete
//
function projectListDelete(projectID) {

	var projectNumber = window.document.getElementsByName('projectNumber_' + projectID)[0].value;
	var answer = confirm('Are you sure you wish to permanently delete:\n' + projectNumber + ' ?');
	if(answer == false) {
		return false;	
	}

	var myForm = addFormToWindow(window,'myForm');
	addPostToForm(myForm,'projectID',projectID);
	myForm.method = 'post';
	myForm.target = '_top';
	myForm.action = '/site/projects/deleteProject.php';
	myForm.submit();
}
