function shakeIt ( id ) {
	e = document.getElementById(id);
	if ( !e ) return false;
	var originalMarginTop = Number(e.style.marginTop);
	for (i=0; i<20; i++) {
		var bob = Math.round(originalMarginTop + 10 - 20 * Math.random()) +"px";
		setTimeout("e.style.marginTop = '"+bob+"';",i*30);
	}
	e.style.marginTop = originalMarginTop+"px";
}


function confirmDelete ( formName ) {
	if ( confirm("Are you sure that you want to delete this post?") ) {
		document.forms[formName].save.value="Delete";
		document.forms[formName].save.click();
	}
}

/**
 * Confirm that the admin wants to delete a comment
 * @param int post
 * @param int comment
 */
function confirmDeleteComment ( post, comment ) {
	if ( confirm("Are you sure that you want to delete this comment?") ) {
		location.href='read.php?post='+post+'&removeComment='+comment;
	}
	return false;
}


/**
 * The Comment Rating, set it
 * @param {Object} rating
 */ 
function setStarRating ( rating ) {
	if ( rating < 0 || rating > 5 ) return false;
	for (i=1; i<=rating; i++) {
		var star = document.getElementById("ratingStar"+i);
		star.src = "images/starLit.png";
		document.forms.comment.rating.value = i; 
	}
	for (i=i; i<=5; i++) {
		var star = document.getElementById("ratingStar"+i);
		star.src = "images/starDim.png";
	}
	return true;
}

/**
 * Make sure that everything is okay with the comment before submitting the form
 */
function saveComment () {
	if ( document.forms.comment.rating.value < 1 ) {
		alert("Doesn't this post deserve at least one star?");
		return false;
	}
	if ( document.forms.comment.body.value == "" ) {
		alert("Didn't you want to comment on this post?");
		return false;
	}
	document.forms.comment.submit();
}


/**
 * Use the stylesheet to highlight an element
 * @param {Object} e
 * @param {Object} lit
 */
function highlight ( e, lit ) {
	if ( lit == false ) {
		e.className = e.className.replace("highlight","");
	} else {
		e.className += " highlight";
	}
}


/**
 * Populate the form with information from a task
 * @param {Object} taskID
 */
function editTask ( taskID ) {
	f = document.getElementById("taskForm");
	if ( taskID == undefined ) {  // hide the form
		shakeIt("taskForm");
		f.style.display = "none";
		f.taskID.value = "";
		return true;
	} else if ( taskID == "" ) {  // show a new task
		f.deleteButton.style.display = "none";
		f.reset();
		f.style.display = "block";
		f.name.focus();
		return true;
	} else {  // show an existing task
		f.deleteButton.style.display = "inline";
		f.taskID.value = taskID;
		f.name.value = taskNames[taskID];
		f.details.value = taskDetails[taskID].replace(/<br\/>/g,"\n");
		f.startDate.value = taskStartDates[taskID];
		f.dueDate.value = taskDueDates[taskID];
		f.status.value = taskStatuses[taskID];
		f.style.display = "block";
		return true;
	}
	return false;
}


/**
 * Delete the task after confirmation
 */
function deleteTask () {
	if ( confirm("Are you sure that you want to delete this task?") ) {
		f = document.getElementById("taskForm");
		f.deleteButton.style.display = "none";
		f.save.value = "Delete";
		f.save.click();
	}
	return false;
}


// ---------- FUNCTIONS FOR THE DAILIES ---------- //
var showFormButtons = false;  // only show the form buttons if the form has changed.

/**
 * Each daily should look different when viewing vs when editing
 * @param {Object} metric
 * @param {Object} edit
 */
function toggleDailyInput ( metric, edit ) {
	if ( !metric ) return false;
	if ( edit ) {
		metric.className = "edit";
		if ( !showFormButtons ) {
			document.getElementById("formButtons").style.display = "block";
			showFormButtons = true;
		}
	}
	else metric.className = "view";
}

/**
 * Reset the daily update form
 */
function resetUpdateForm () {
	if ( confirm("Are you sure that you want to reset this form?") ) {
		document.getElementById('formButtons').style.display='none'; 
		document.forms.update.reset();
		showFormButtons = false;
	}
}

/**
 * Populate the form with information from a daily
 * @param {Object} dailyID
 */
function editDaily ( dailyID ) {
	f = document.getElementById("dailyForm");
	if ( dailyID == undefined ) {  // hide the form
		shakeIt("dailyForm");
		f.style.display = "none";
		f.dailyID.value = "";
		return true;
	} else if ( dailyID == "" ) {  // show a new daily
		f.deleteButton.style.display = "none";
		f.reset();
		f.style.display = "block";
		f.name.focus();
		return true;
	} else {  // show an existing daily
		f.deleteButton.style.display = "inline";
		f.dailyID.value = dailyID;
		f.name.value = dailyNames[dailyID];
		f.type.value = dailyTypes[dailyID];
		f.category.value = dailyCategories[dailyID];
		f.style.display = "block";
		
		// show the chart for this metric
		var chart = new FusionCharts("FCF_MSLine.swf", "ChartId", "600", "350");
		chart.setDataURL("dailyChart.php?daily="+dailyID+"&random="+Math.round(Math.random()*10));		   
		chart.render("chartdiv");
				
		return true;
	}
	return false;
}


/**
 * Delete the daily after confirmation
 */
function deleteDaily () {
	if ( confirm("Are you sure that you want to delete this daily?") ) {
		f = document.getElementById("dailyForm");
		f.deleteButton.style.display = "none";
		f.save.value = "Delete";
		f.save.click();
	}
	return false;
}