/*********************************************/
/*                 CLASSES                   */
/*********************************************/
function select_class(disabledBoxId,boxId,aBoxes) {
	var disabledBox = document.getElementById(disabledBoxId);
	var box = document.getElementById(boxId);
	
	/* Hide all boxes */
	for (i in aBoxes) {
		var oneBox = document.getElementById(aBoxes[i]);
		oneBox.style.display ="none";
	}
		
	/* Display Active Box */
	disabledBox.style.display = "none";
	box.style.display = "block";
	return;
}

function add_teacher(stageId,newTeacherCodeId) {
	var stageDiv = document.getElementById(stageId);
	var codeDiv = document.getElementById(newTeacherCodeId);
	
	stageDiv.innerHTML += codeDiv.innerHTML;
	
}

function new_class(aClassCode,aGrades) {
	var classCodeLabel = document.getElementById(aClassCode[0]);
	var classCode = document.getElementById(aClassCode[1]).value;
	var gradesLabel = document.getElementById(aGrades[0]);
	var grades = document.getElementById(aGrades[1]).value;
	var gradeRegex = /^[0-9,\,]+$/g;
	
	classCodeLabel.style.color = "black";
	gradesLabel.style.color = "black";
	
	if (classCode == "") {
		classCodeLabel.style.color = "red";
		alert("Please provide a name for the class.");
		return false;
	}
	
	if (!grades.match(gradeRegex)) {
		gradesLabel.style.color = "red";
		alert("The grades you have entered are invalid.");
		return false;
	}
	
	return true;
}

/*********************************************/
/*                 SETTINGS                  */
/*********************************************/
function check_password(pass1,pass2) {
	if (pass1 == pass2)
		return true;
	return false;
}

function create_new_user(aShortName,aLongName,aPass1,aPass2) {
	var shortName = document.getElementById(aShortName[0]).value;
	var longName = document.getElementById(aLongName[0]).value;
	var pass1 = document.getElementById(aPass1[0]).value;
	var pass2 = document.getElementById(aPass2[0]).value;
	var labelShortName = document.getElementById(aShortName[1]);
	var labelLongName = document.getElementById(aLongName[1]);
	var labelPass1 = document.getElementById(aPass1[1]);
	var labelPass2 = document.getElementById(aPass2[1]);
	
	/* Resets all labels */
	labelShortName.style.color = "#000000";
	labelLongName.style.color = "#000000";
	labelPass1.style.color = "#000000";
	labelPass2.style.color = "#000000";
	
	var shortNameExp = /\s/;
	
	if (shortName == "") {
		labelShortName.style.color = "red";
		alert("Please provide a screen name for the new user.");
		return false;
	}
	
	if (shortName.match(shortNameExp)) {
		labelShortName.style.color = "red";
		alert("The screen name cannot contain spaces.");
		return false;
	}
	
	if (longName == "") {
		labelLongName.style.color = "red";
		alert("Please provide a name for the new user.");
		return false;
	}
	
	if (!check_password(pass1,pass2)) {
		labelPass1.style.color = "red";
		labelPass2.style.color = "red";
		alert("The passwords do not match.");
		return false;
	}
		
	if (pass1 == "") {
		labelPass1.style.color = "red";
		labelPass2.style.color = "red";
		alert("Please provide a password for the new user.");
		return false;
	}
	
	return true;
}

function edit_user(aLongName,aPass1,aPass2,oldPasswordId) {
	var longName = document.getElementById(aLongName[0]).value;
	var pass1 = document.getElementById(aPass1[0]).value;
	var pass2 = document.getElementById(aPass2[0]).value;
	var oldPass = document.getElementById(oldPasswordId).value;
	var labelLongName = document.getElementById(aLongName[1]);
	var labelPass1 = document.getElementById(aPass1[1]);
	var labelPass2 = document.getElementById(aPass2[1]);
	
	/* Resets all labels */
	labelLongName.style.color = "#000000";
	labelPass1.style.color = "#000000";
	labelPass2.style.color = "#000000";
	
	if (longName == "") {
		labelLongName.style.color = "red";
		alert("Please provide a name.");
		return false;
	}
	
	if (oldPass != "" && pass1 == "") {
		labelPass1.style.color = "red";
		labelPass2.style.color = "red";
		alert("Please provide a password.");
		return false;
	}

	if (!check_password(pass1,pass2)) {
		labelPass1.style.color = "red";
		labelPass2.style.color = "red";
		alert("The passwords do not match.");
		return false;
	}
	
	return true;
}

function admin_edit_user(aLongName,aPass1,aPass2) {
	var longName = document.getElementById(aLongName[0]).value;
	var pass1 = document.getElementById(aPass1[0]).value;
	var pass2 = document.getElementById(aPass2[0]).value;
	var labelLongName = document.getElementById(aLongName[1]);
	var labelPass1 = document.getElementById(aPass1[1]);
	var labelPass2 = document.getElementById(aPass2[1]);
	
	/* Resets all labels */
	labelLongName.style.color = "#000000";
	labelPass1.style.color = "#000000";
	labelPass2.style.color = "#000000";
	
	if (longName == "") {
		labelLongName.style.color = "red";
		alert("Please provide a name.");
		return false;
	}
	
	if (pass1 == "") {
		labelPass1.style.color = "red";
		labelPass2.style.color = "red";
		alert("Please provide a password.");
		return false;
	}

	if (!check_password(pass1,pass2)) {
		labelPass1.style.color = "red";
		labelPass2.style.color = "red";
		alert("The passwords do not match.");
		return false;
	}
	
	return true;
}

function check_numerical_field(array) {
	var label = document.getElementById(array[0]);
	var txtField = document.getElementById(array[1]);
	
	if (isNaN(txtField.value)) {
		label.style.color = "red";
		alert('The number you have entered is not valid.');
		return false;
	}
	
	return true;
}

/*********************************************/
/*                 EDIT HTML                 */
/*********************************************/
function editHTML_expand(expandedDivId,makeChangesDivId) {
	var expandDiv = document.getElementById(expandedDivId);
	var makeChangesDiv = document.getElementById(makeChangesDivId);
	expandDiv.style.display = "block";
	makeChangesDiv.style.display = "none";
}

function editHTML_collapse(expandedDivId,makeChangesDivId) {
	var expandDiv = document.getElementById(expandedDivId);
	var makeChangesDiv = document.getElementById(makeChangesDivId);
	expandDiv.style.display = "none";
	makeChangesDiv.style.display = "block";
}

function submit_changes_to_html(pageTitleId,labelId) {
	var pageTitle = document.getElementById(pageTitleId);
	var label = document.getElementById(labelId);
	
	if (pageTitle.value == "") {
		label.style.color = "red";
		alert('Please enter a name for this Webpage.');
		return false;
	}
	
	return confirm("Are you sure you want to save changes to this page? This action cannot be reversed.");
}

/*********************************************/
/*                FILE UPLOADS               */
/*********************************************/
function validate_file_upload(fileId,labelId) {
	var label = document.getElementById(labelId);
	var file = document.getElementById(fileId);
	
	label.style.color = "black";
	
	if (file.value == "") {
		label.style.color = "red";
		alert('Please choose a file to upload.');
		return false;
	}
	return true;
}

/*********************************************/
/*                  REPORTS                  */
/*********************************************/
function validate_report(oldDate,aDates,year,labelDateId) {
	var i;
	var newYear = document.getElementById(year).value;
	var newDate = newYear;
	var el_labelDate = document.getElementById(labelDateId);
	/* Date hasn't changed so it's not an error that it would be the same */
	if (oldDate == newDate)	
		return true;
	for (i in aDates) {
		if (newDate == aDates[i]) {
			el_labelDate.style.color = "red";
			alert("A report already exists for "+newDate+", please remove the old one first or select a different year.");
			return false;
		}
	}
	return true;
}

function submit_new_report(file_id,labelFileId,labelDateId,aDates,year) {
	var el_file = document.getElementById(file_id);
	var el_label = document.getElementById(labelFileId);
	/* Validates file */
	if (el_file.value == "" || el_file.value == null) {
		el_label.style.color = "red";
		alert("Please choose a file to upload.");
		return false;
	}
	/* Validates date */
	if (!validate_report("nil",aDates,year,labelDateId))
		return false;
	/* Else */
	return true;
}

function delete_report(title,formId) {
	var form = document.getElementById(formId);
	
	if (confirm("Are you sure you want to delete the "+title+" annual report?"))
		form.submit();
}


/*********************************************/
/*                 NEWSLETTER                */
/*********************************************/
function validate_newsletter(oldDate,aDates,day,month,year,labelDateId) {
	var i;
	var newDay = document.getElementById(day).value;
	var newMonth = document.getElementById(month).value;
	var newYear = document.getElementById(year).value;
	var newDate = newYear+"-"+newMonth+"-"+newDay;
	var el_labelDate = document.getElementById(labelDateId);
	/* Date hasn't changed so it's not an error that it would be the same */
	if (oldDate == newDate)	
		return true;
	for (i in aDates) {
		if (newDate == aDates[i]) {
			el_labelDate.style.color = "red";
			alert("A newsletter already exists for the "+newDate+", please remove the old one first or select a different date.");
			return false;
		}
	}
	return true;
}

function submit_new_newsletter(file_id,labelFileId,labelDateId,aDates,day,month,year) {
	var el_file = document.getElementById(file_id);
	var el_label = document.getElementById(labelFileId);
	/* Validates file */
	if (el_file.value == "" || el_file.value == null) {
		el_label.style.color = "red";
		alert("Please choose a file to upload.");
		return false;
	}
	/* Validates date */
	if (!validate_newsletter("nil",aDates,day,month,year,labelDateId))
		return false;
	/* Else */
	return true;
}

function delete_newsletter(title,formId) {
	var form = document.getElementById(formId);
	
	if (confirm("Are you sure you want to delete the "+title+" newsletter?"))
		form.submit();
}

/*********************************************/
/*                VIEW NOTICE                */
/*********************************************/
function change_article(stageId,commentId) {
	var stage = document.getElementById(stageId);
	var comment = document.getElementById(commentId);
	
	stage.innerHTML = comment.innerHTML;
}

/*********************************************/
/*                 NEW NOTICE                */
/*********************************************/
function verify_new_article(title) {
	var el_title = document.getElementById(title);
	
	if (el_title.value == ""){
		return 1;
	}
	
	return 0;
}

function verify_new_article_extra(title,comment,picture) {
	var el_title = document.getElementById(title);
	var el_comment = document.getElementById(comment);
	var el_picture = document.getElementById(picture);
	if (el_title.value == "" || el_comment.value == "" || el_picture.value == "")
		return 1;
	/* Else */
	return 0;
}

function new_article_more(divId,callerId) {
	var extendSpace = document.getElementById(divId);
	var caller = document.getElementById(callerId);
	extendSpace.style.display = "block";
	caller.onclick = function() { new_article_less(divId,callerId) };
}

function new_article_less(divId,callerId) {
	var extendSpace = document.getElementById(divId);
	var caller = document.getElementById(callerId);
	extendSpace.style.display = "none";
	caller.onclick = function() { new_article_more(divId,callerId) };
}

function submit_new_article(moreOptionsId,aTitle,aComment,aPicture,down1,down2,down3) {
	var more_opt_chkbx = document.getElementById(moreOptionsId);
	var label_title = document.getElementById(aTitle[1]);
	var label_comment = document.getElementById(aComment[1]);
	var label_picture = document.getElementById(aPicture[1]);
	var download = new Array("",new Array(),new Array(),new Array());
	download[1][0] = document.getElementById(down1[0]); // File
	download[1][1] = document.getElementById(down1[1]); // Name
	download[1][2] = document.getElementById(down1[2]); // Label
	download[2][0] = document.getElementById(down2[0]);
	download[2][1] = document.getElementById(down2[1]);
	download[2][2] = document.getElementById(down2[2]);
	download[3][0] = document.getElementById(down3[0]);
	download[3][1] = document.getElementById(down3[1]);
	download[3][2] = document.getElementById(down3[2]);
	
	/* Reset label colours */
	label_title.style.color = "#000000";
	label_comment.style.color = "#000000";
	label_picture.style.color = "#000000";
	download[1][2].style.color = "#000000";
	download[2][2].style.color = "#000000";
	download[3][2].style.color = "#000000";
	
	if (more_opt_chkbx.checked == true) {
		if (verify_new_article_extra(aTitle[0],aComment[0],aPicture[0]) < 1) {
			/* Checks downloads */
			for (var i=1; i<=3; i++) {
				if (download[i][0].value != "" && (download[i][1].value == "" || download[i][1].value == "<Name for file>")) {
					download[i][2].style.color = "red";
					alert("Please provide a name for the file.");
					return false;
				}
			}
			
			return true;
		} else {
			label_title.style.color = "red";
			label_comment.style.color = "red";
			label_picture.style.color = "red";
			alert("Please provide a title, a comment and a picture.");
			return false;
		}
	} else {
		if (verify_new_article(aTitle[0]) < 1) {
			return true;
		} else {
			label_title.style.color = "red";
			alert("Please provide a title.");
			return false;
		}
	}
}

/*********************************************/
/*                EDIT NOTICE                */
/*********************************************/
function submit_edit_article(art_id,title,comment,label,picture_label,has_comment,has_picture,down1,down2,down3,label_comment) {
	var label_title = document.getElementById(label);
	var label_comm = document.getElementById(label_comment);
	var label_pic = document.getElementById(picture_label);
	var download = new Array("",new Array(),new Array(),new Array());
	download[1][0] = document.getElementById(down1[0]); // File
	download[1][1] = document.getElementById(down1[1]); // Name
	download[1][2] = document.getElementById(down1[2]); // Label
	download[2][0] = document.getElementById(down2[0]);
	download[2][1] = document.getElementById(down2[1]);
	download[2][2] = document.getElementById(down2[2]);
	download[3][0] = document.getElementById(down3[0]);
	download[3][1] = document.getElementById(down3[1]);
	download[3][2] = document.getElementById(down3[2]);
	
	if (verify_new_article(title) < 1) {
		if (has_comment == "true" && verify_new_article(comment) > 0)
			return confirm("All optional information (picture, downloads, etc.) in this article will be deleted because you have not provided a comment. Are you sure you want to proceed?");
		else if (verify_new_article(comment) < 1 && document.getElementById(has_picture).value == "" && has_comment == "false") {
			label_pic.style.color = "red";
			alert("Please choose a picture.");
			return false;
		} else if (verify_new_article(comment) > 0 && (document.getElementById(has_picture).value != "" || download1.value != "" || download2.value != "" || download3.value != "")) {
			label_comm.style.color = "red";
			alert("To attach a picture or file to a notice it must have a comment.");
			return false;
		}
		
		/* Checks downloads */
		for (var i=1; i<=3; i++) {
			if (download[i][0].value != "" && (download[i][1].value == "" || download[i][1].value == "<Name for file>")) {
				download[i][2].style.color = "red";
				alert("Please provide a name for the file.");
				return false;
			}
		}
	} else {
		label_title.style.color = "red";
		alert("Please provide a title.");
		return false;
	}
}

function editNoticeShowPanel(noticeRowId,noticeMasterRowId,noticeLinkId) {
	var noticeRow = document.getElementById(noticeRowId);
	var noticeMasterRow = document.getElementById(noticeMasterRowId);
	var noticeLink = document.getElementById(noticeLinkId);
	
	noticeRow.style.display = "";	// displays row
	noticeMasterRow.style.backgroundColor = "#3867F8";
	noticeMasterRow.style.color = "#FFFFFF";
	noticeLink.href = "javascript:editNoticeHidePanel('"+noticeRowId+"','"+noticeMasterRowId+"','"+noticeLinkId+"');";
	return;
}

function editNoticeHidePanel(noticeRowId,noticeMasterRowId,noticeLinkId) {
	var noticeRow = document.getElementById(noticeRowId);
	var noticeMasterRow = document.getElementById(noticeMasterRowId);
	var noticeLink = document.getElementById(noticeLinkId);
	
	noticeRow.style.display = "none";
	noticeMasterRow.style.backgroundColor = "#B8D0FE";
	noticeMasterRow.style.color = "#000000";
	noticeLink.href = "javascript:editNoticeShowPanel('"+noticeRowId+"','"+noticeMasterRowId+"','"+noticeLinkId+"');";
	return;
}

/*********************************************/
/*              DELETE NOTICE                */
/*********************************************/
function delete_article(article_name,form_id) {
	var form = document.getElementById(form_id);
	
	if (confirm("Are you sure you want to delete \""+article_name+"\"?"))
		form.submit();
}

/*********************************************/
/*                   STAFF                   */
/*********************************************/
function submitNewStaff(aName) {
	var nameLabel = document.getElementById(aName[0]);
	var nameField = document.getElementById(aName[1]);
	
	if (nameField.value == "") {
		nameLabel.style.color = "red";
		alert("Please provide a name.");
		return false;
	}
	
	return true;
}

/*********************************************/
/*                  GENERAL                  */
/*********************************************/
function clear_text(inputEl,defaultStr) {
	if (inputEl.value == defaultStr)
		inputEl.value = "";
}

function enable_text_field(callerEl,txtFieldId) {
	var txtField = document.getElementById(txtFieldId);
	
	if (callerEl.value != "")
		txtField.disabled = false;
	return;
}

function change_background(canvasId,caller,newColor,originalColor) {
	document.getElementById(canvasId).style.backgroundColor = newColor;
	caller.onchange = function () { change_background(canvasId,caller,originalColor,newColor); };
}