function validatePicture(f) {
	if (f.picturefile.value == "" && f.picture.value == "") {
		return confirm("You haven't selected a picture. Is this OK?\n(You can come back and add one later)");
	} else if (f.picturefile.value == "" && f.picture.value != "") {
		// use picture because it exists in database already
		return true;
	} else {
		// use picturefile - even if image exists in database
		return true;
	}
	alert("Error in image validation logic");
	return false;
}

function validateClass(f) {
	if (f.status.value == 99) {
		return confirm("Are you sure you want to delete this class?");
	}
	if (f.title.value == "") {
		alert("Please give this class a title.");
		f.title.focus();
		return false;
	}
	if (f.summary.value == "") {
		alert("Please give this class a summary.");
		f.summary.focus();
		return false;
	}
	if (f.desc.value == "") {
		alert("Please give this class a description.");
		f.desc.focus();
		return false;
	}
	return validatePicture(f);
}

/*
function updateTime(f) {
	var thisHour = parseInt(f.hour.value) + parseInt(f.ampm.value);

	if (f.hour.value == "12" && f.ampm.value == "12") {
		thisHour = "12";
	}
	else if (f.hour.value == "12" && f.ampm.value == "0") {
		thisHour = "00";
	}	

	f.time.value = thisHour + ":" + f.minute.value + ":00";
}
*/

// use this when the new multi-day scheduler is ready
function updateTime(num) {
	f = document.forms['calform'];
	var thisHour = parseInt(f["hour"+num].value) + parseInt(f["ampm"+num].value);
	
	if (f["hour"+num].value == "12" && f["ampm"+num].value == "12") {
		thisHour = "12";
	}
	else if (f["hour"+num].value == "12" && f["ampm"+num].value == "0") {
		thisHour = "00";
	}	

	f["time"+num].value = thisHour + ":" + f["minute"+num].value + ":00";
}

function validateCalForm(f) {
	var validChars = /20\d\d-\d\d-\d\d/;
	var dateFields = getArray(f, 'date[]');

	if(f.theclass.value == "") {
		alert("Please choose a class to schedule.");
		f.theclass.focus();
		return false;
	} else if (dateFields.length > 0) {
		
		for(var i=0; i<dateFields.length; i++) {
			if (dateFields[i] == '') {
				alert("Please enter all dates for this class.");
				$('#dateselect'+(i+1)).focus();
				return false;
			}
			if (!validChars.test(dateFields[i])) {
				alert("The date must be in the format YYYY-MM-DD");
				$('#dateselect'+(i+1)).focus();
				return false;
			}
		}
	/*
		if (!f.date.value) {
		alert("Please enter a date for this class.");
		f.date.focus();
		return false;
	} else if (!validChars.test(f.date.value)) {
		alert("The date must be in the format YYYY-MM-DD");
		f.date.focus();
		return false;
	*/
	} else {
		return true;
	}
	
	
}

function getArray(f, arrayName) {
	var list = new Array();
	for(var i=0; i < f.elements.length; i++) {
		var el = f.elements[i];
		if(el.name == arrayName) {
			list.push(el.value);
		}
	}
	return list;
}	


function validateMail(f) {
	return true;
}

function validateContact(f, rand) {
	if (!f["realname"+rand].value) {
		alert("Please enter your name.");
		f["realname"+rand].focus();
		return false;
	}
	if (!f["email"+rand].value || f["email"+rand].value.indexOf("@") < 0 || f["email"+rand].value.indexOf(".") < 0) {
		alert("Please enter a valid email address.");
		f["email"+rand].focus();
		return false;
	}
	if (!f["Comment"+rand].value) {
		alert("Please enter a question or comment.");
		f["Comment"+rand].focus();
		return false;
	}
	f.email.value = f["email"+rand].value;
	return true;
}

function validateTeacher(f) {
	if (f.status.value == 99) {
		return confirm("Are you sure you want to delete this teacher?");
	}
	if (f.website.value.indexOf("http://") < 0 && f.website.value != "") {
		f.website.value = "http://" + f.website.value;
	}
	if (f.name.value == "") {
		alert("Please fill in the teacher's name.");
		return false;
	}
	return validatePicture(f);
}

function validateLogin(f) {
	if (f.adminname.value == "") {
		alert("Please type your admin login name.");
		f.adminname.focus();
		return false;
	}
	if (f.secretkey.value == "") {
		alert("Please type your password.");
		f.secretkey.focus();
		return false;
	}
	return true;
}

function validateLoginUpdate(f) {
	if (f.adminname.value == "") {
		alert("Please enter a login name for this user.");
		f.adminname.focus();
		return false;
	}
	if (f.password.value == "" && f.passwordmatch.value == "") {
		// it's okay if both are blank - it just means we're not changing it
		return true;
	}
	if (f.password.value == "") {
		alert("Please enter a password for this user.");
		f.password.focus();
		return false;
	}
	if (f.password.value != f.passwordmatch.value) {
		alert("The passwords do not match. Please try again.");
		f.password.value = "";
		f.passwordmatch.value = "";
		f.password.focus();
		return false;
	}
	return true;
}

function validateLoginNew(f) {
	if (f.adminname.value == "") {
		alert("Please enter a login name for this user.");
		f.adminname.focus();
		return false;
	}
	if (f.password.value == "") {
		alert("Please enter a password for this user.");
		f.password.focus();
		return false;
	}
	if (f.password.value != f.passwordmatch.value) {
		alert("The passwords do not match. Please try again.");
		f.password.value = "";
		f.passwordmatch.value = "";
		f.password.focus();
		return false;
	}
	return true;
}

function validateFreeReg(f) {
	if (f.fname.value == "") {
		alert("Please type your first name.");
		f.fname.focus();
		return false;
	}
	if (f.lname.value == "") {
		alert("Please type your last name.");
		f.lname.focus();
		return false;
	}
	if (!f.email.value || f.email.value.indexOf("@") < 0 || f.email.value.indexOf(".") < 0) {
		alert("Please enter a valid email address.");
		f.email.focus();
		return false;
	}
	return true;
}

function validateFeature(f) {
	if (f.title.value == "") {
		alert("Please enter a feature title.");
		f.title.focus();
		return false;
	}
	if (f.link.value.indexOf("http://") < 0 && f.link.value != "") {
		f.link.value = "http://" + f.link.value;
	}

	return true;
}

function validateRegistration(f) {
	if (f.fname.value == "") {
		alert("Please enter a the student's first name.");
		f.fname.focus();
		return false;
	}
	if (f.lname.value == "") {
		alert("Please enter a the student's last name.");
		f.lname.focus();
		return false;
	}
	if ((!f.email.value || f.email.value.indexOf("@") < 0 || f.email.value.indexOf(".") < 0) && (f.phone.value == "")) {
		alert("A valid email address OR phone number is required.");
		f.email.focus();
		return false;
	}
	return true;
}

function validate_mailform(f) {
	if (!f.ea.value || f.ea.value.indexOf("@") < 0 || f.ea.value.indexOf(".") < 0) {
		alert("Please enter a valid email address.");
		f.ea.focus();
		return false;
	}
	return true;
}

function initMCE() {
	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		plugins : "fullscreen",
		content_css : "/style/tinymce_custom.css",
		theme_advanced_buttons1 : "bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,outdent,indent,|,undo,redo,link,unlink,|,fullscreen",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_path_location : "bottom",
		theme_advanced_resize_horizontal : false,
		theme_advanced_resizing : true,
		invalid_elements : "span,div",
//		theme_advanced_styles : "Quote=quote;Header 2=header2;Header 3=header3;Table Row=tableRow1",
		extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"
	});
}

function toggleEditor(id) {
	var elm = document.getElementById(id);

	if (tinyMCE.getInstanceById(id) == null)
		tinyMCE.execCommand('mceAddControl', false, id);
	else
		tinyMCE.execCommand('mceRemoveControl', false, id);
}
