﻿var formready = false;

function testURL() {
	if (document.getElementById("ContentURL") == null ||
      document.getElementById("ContentURL").value.length == 0) {
		alert("You must enter a URL");
		return;
	}
	window.open(document.getElementById("ContentURL").value, "testwin");
}

function saveContent() {
	formready = true;
	validateInputs();
	if (formready) {
		document.econtent.id.value = ccontentid;
		AssociateTopics.SaveAssociatedTopics(ccontentid);
		AssociateEnvironments.SaveAssociatedEnvironments(ccontentid);
		document.econtent.submit();
	}
}

function htmlTopicEnvSave() {
   AssociateTopics.SaveAssociatedTopics(ccontentid);
   AssociateEnvironments.SaveAssociatedEnvironments(ccontentid);
}

function cancelContent(targeturl) {
	var checknewurl = "ContentEdit.aspx?action=checknewcancel&id=" + ccontentid;
	ajaxLookup(checknewurl);
	if (targeturl != null && targeturl.length > 0) {
		document.location.href = targeturl;
	} else {
		history.go(-1);
	}
}

function deleteContent() {
	if (confirm("Are you sure you want to delete this record?\nIt, along with any comments, etc, will be completely removed.")) {
		var checknewurl = "ContentEdit.aspx?action=delete&id=" + ccontentid;
		ajaxLookup(checknewurl);
		history.go(-1);
	}
}

function validateInputs() {
	var allflds = document.econtent.elements;
	for (var xaf = 0; xaf < allflds.length; xaf++) {
		validateFld(allflds[xaf]);
	}
}

function validateFld(fld) {
	switch (fld.getAttribute("fldtype")) {
		case "int":
			if (fld.value.length > 0 && isNaN(fld.value)) {
				alert("You must enter a number");
				fld.value = 0;
				formready = false;
				fld.focus();
			}
			break;
		case "date":
			if (fld.value.length > 0 && !validateDate(fld)) {
				fld.focus();
			}
			break;
		case "vidfile":
			if (fld.value.length > 0 && fld.value.toLowerCase().indexOf(".wmv") < 0 &&
             fld.value.toLowerCase().indexOf(".mpg") < 0 && fld.value.toLowerCase().indexOf(".mpeg") < 0 &&
             fld.value.toLowerCase().indexOf(".mov") < 0 && fld.value.toLowerCase().indexOf(".avi") < 0) {
				alert("You must select an AVI, MOV, MPEG or WMV file");
				formready = false;
				fld.focus();
			}
			if (fld.value.length > 0 && document.econtent != null && document.econtent.ContentURL != null) {
				document.econtent.ContentURL.value = "";
			}
			break;
		case "audiofile":
			if (fld.value.length > 0 && fld.value.toLowerCase().indexOf(".mp3") < 0 &&
             fld.value.toLowerCase().indexOf(".wav") < 0) {
				alert("You must select an MP3 or WAV file");
				formready = false;
				fld.focus();
			}
			if (fld.value.length > 0 && document.econtent != null && document.econtent.ContentURL != null) {
				document.econtent.ContentURL.value = "";
			}
			break;
		case "imgfile":
			if (fld.value.length > 0 && fld.value.toLowerCase().indexOf(".gif") < 0 &&
             fld.value.toLowerCase().indexOf(".jpg") < 0 && fld.value.toLowerCase().indexOf(".jpeg") < 0 &&
             fld.value.toLowerCase().indexOf(".png") < 0) {
				alert("You must select a GIF, JPEG or PNG file");
				formready = false;
				fld.focus();
			}
			if (fld.value.length > 0 && document.econtent != null && document.econtent.ContentURL != null) {
				document.econtent.ContentURL.value = "";
			}
			break;
		case "flsfile":
			if (fld.value.length > 0 && fld.value.toLowerCase().indexOf(".swf") < 0) {
				alert("You must select a flash file");
				formready = false;
				fld.focus();
			}
			if (fld.value.length > 0 && document.econtent != null && document.econtent.ContentURL != null) {
				document.econtent.ContentURL.value = "";
			}
			break;
		case "txtfile":
			if (fld.value.length > 0 && fld.value.toLowerCase().indexOf(".txt") < 0) {
				alert("You must select a text file");
				formready = false;
				fld.focus();
			}
			if (fld.value.length > 0 && document.econtent != null && document.econtent.ContentURL != null) {
				document.econtent.ContentURL.value = "";
			}
			break;
		case "docfile":
			if (fld.value.length > 0 && (fld.value.toLowerCase().indexOf(".doc") < 0 &&
										fld.value.toLowerCase().indexOf(".xls") < 0 &&
										fld.value.toLowerCase().indexOf(".pdf") < 0)) {
				alert("You must select a .doc, .xls, or .pdf file");
				formready = false;
				fld.focus();
			}
			if (fld.value.length > 0 && document.econtent != null && document.econtent.ContentURL != null) {
				document.econtent.ContentURL.value = "";
			}
			break;
		case "file":
			if (fld.value.length > 0 && document.econtent != null && document.econtent.ContentURL != null) {
				document.econtent.ContentURL.value = "";
			}
			break;
		case "url":
			if (fld.value.length > 0 && document.econtent != null && document.econtent.ContentLocalFile != null) {
				document.econtent.ContentLocalFile.value = "";
			}
			if (fld.value.length > 0 && fld.value.toLowerCase().indexOf("http") < 0) {
				var tmpurlval = "http://" + fld.value;
				fld.value = tmpurlval;
			}
			break;
		case "rtext":
			if (fld.value.length == 0) {
				alert("You must enter some text");
				formready = false;
				fld.focus();
			}
			break;
	}
}

function validateDate(fld) {
	var dt = fld.value;
	var day;
	var month;
	var year;

	if (dt.length > 0) {
		var invalid = true;
		var msg = "Invalid date format";

		//check for valid short date formats
		if (dt.indexOf("/") > 0) {
			var fs = dt.indexOf("/");
			var ls = dt.lastIndexOf("/");
			month = dt.substring(0, fs);
			day = dt.substring(fs + 1, ls);
			year = dt.substring(ls + 1, dt.length);
			if (isNaN(month) || month < 1 || month > 12) {
				msg = "Invalid month";
			}
			var maxday;
			switch (month - 1) {
				case 1:
					maxday = 28;
					if (year % 4 == 0) {
						maxday = 29;
						if (year % 100 == 0) {
							maxday = 28;
							if (year % 400 == 0) {
								maxday = 29;
							}
						}
					}
					break;
				case 0:
				case 2:
				case 4:
				case 6:
				case 7:
				case 9:
				case 11:
					maxday = 31;
					break;
				default:
					maxday = 30;
					break;
			}
			if (isNaN(day) || day < 1 || day > maxday || day.length > 2) {
				msg = "Invalid day";
			}
			if (isNaN(year) || year < 1000 || year > 9999) {
				msg = "Invalid year\rUse 4 digit year";
			}
			var date = new Date(month + "/" + day + "/" + year);
			if (date.getMonth() + 1 != month && msg == "Invalid date format") {
				msg = "Invalid day";
			}
			if (!isNaN(date) && msg == "Invalid date format") {
				invalid = false;
			}
		}

		//check for valid medium date format
		if (dt.indexOf("-") > 0) {
			var fd = dt.indexOf("-");
			var ld = dt.lastIndexOf("-");
			day = dt.substring(0, fd);
			month = dt.substring(fd + 1, ld);
			year = dt.substring(ld + 1, dt.length);
			switch (month.toLowerCase()) {
				case 'jan':
					month = 1;
					break;
				case 'feb':
					month = 2;
					break;
				case 'mar':
					month = 3;
					break;
				case 'apr':
					month = 4;
					break;
				case 'may':
					month = 5;
					break;
				case 'jun':
					month = 6;
					break;
				case 'jul':
					month = 7;
					break;
				case 'aug':
					month = 8;
					break;
				case 'sep':
					month = 9;
					break;
				case 'oct':
					month = 10;
					break;
				case 'nov':
					month = 11;
					break;
				case 'dec':
					month = 12;
					break;
				default:
					month = 0;
					break;
			}
			var maxday;
			switch (month - 1) {
				case 1:
					maxday = 28;
					if (year % 4 == 0) {
						maxday = 29;
						if (year % 100 == 0) {
							maxday = 28;
							if (year % 400 == 0) {
								maxday = 29;
							}
						}
					}
					break;
				case 0:
				case 2:
				case 4:
				case 6:
				case 7:
				case 9:
				case 11:
					maxday = 31;
					break;
				default:
					maxday = 30;
					break;
			}
			if (isNaN(day) || day < 1 || day > maxday || day.length > 2) {
				msg = "Invalid day";
			}
			if (isNaN(month) || month < 1 || month > 12) {
				msg = "Invalid month";
			}
			if (isNaN(year) || year < 1000 || year > 9999) {
				msg = "Invalid year\rUse 4 digit year";
			}
			var date = new Date(month + "/" + day + "/" + year);
			if (date.getMonth() + 1 != month) {
				msg = "Invalid day";
			}
			if (!isNaN(date) && msg == "Invalid date format") {
				invalid = false;
			}
		}

		if (invalid) {
			alert(msg);
			fld.focus();
			return false;
		}
	}
	return true;
}

function addComment() {
	var cfld = document.getElementById("comments");
	var commurl = "ShowContent.aspx?action=comment&id=" + ccontentid + "&comment=" + escape(cfld.value) + "&type=" + cdbtype;
	ajaxLookup(commurl);
	document.location.reload();
}

function saveComment() {
	var cfld = document.getElementById("comments");
	var commurl = "ShowContent.aspx?action=savecomment&id=" + ccontentid + "&commentid=" + ccommentid + "&comment=" + escape(cfld.value) + "&type=" + cdbtype;
	ajaxExecute(commurl);
	var commentcell = document.getElementById("ct" + ccommentid);
	commentcell.innerText = cfld.value;
	commentcell.textContent = cfld.value;
	cfld.value = "";
	hideCommentEdits();
}

function deleteComment(commentid) {
	if (confirm("Are you sure you want to delete this comment?")) {
		var commentrow = document.getElementById("commentrow" + commentid);
		var commurl = "ShowContent.aspx?action=deletecomment&id=" + ccontentid + "&commentid=" + commentid + "&type=" + cdbtype;
		ajaxExecute(commurl);
		commentrow.style.display = "none";
		hideCommentEdits();
	}
}

function editComment(commentid) {
	var commentcell = document.getElementById("ct" + commentid);
	var cfld = document.getElementById("comments");
	cfld.value = commentcell.innerText;
	if (cfld.value == null || cfld.value == "undefined") {
		cfld.value = commentcell.textContent;
	}
	ccommentid = commentid;
	showCommentEdits();
}

function cancelComment() {
	var cfld = document.getElementById("comments");
	cfld.value = "";
	hideCommentEdits();
}

function showCommentEdits() {
	var btnpost = document.getElementById("btnaddcomment");
	var btnsave = document.getElementById("btnsavecomment");
	var btncancel = document.getElementById("btncancelcomment");
	btnpost.style.display = "none";
	btnsave.style.display = "";
	btncancel.style.display = "";
}

function hideCommentEdits() {
	var btnpost = document.getElementById("btnaddcomment");
	var btnsave = document.getElementById("btnsavecomment");
	var btncancel = document.getElementById("btncancelcomment");
	btnpost.style.display = "";
	btnsave.style.display = "none";
	btncancel.style.display = "none";
	ccommentid = 0;
}

function wasSolved(fld) {
	if (fld.getAttribute("curselected") == "true") {
		return;
	}
	var solvedurl = "ShowContent.aspx?action=solved&id=" + ccontentid + "&solved=" + fld.value + "&type=" + cdbtype;
	ajaxExecute(solvedurl);
	fld.setAttribute("curselected", "true");
	var ofld = document.getElementById("solved");
	if (fld.id == "solved") {
		ofld = document.getElementById("notsolved");
	}
	ofld.setAttribute("curselected", "false");
	var solvedthanks = document.getElementById("solvedthankyou");
	if (solvedthanks != null) {
		solvedthanks.style.display = "";
	}
}

function publishContent() {
	var puburl = "ShowContent.aspx?action=publish&id=" + ccontentid + "&type=" + cdbtype;
	ajaxExecute(puburl);
	var pfld = document.getElementById("pubrow");
	if (pfld != null) {
		pfld.style.display = "none";
		alert("Content has been published");
	}
}

function addFavorite() {
	var favurl = "ShowContent.aspx?action=favorite&id=" + ccontentid + "&type=" + cdbtype;
	ajaxExecute(favurl);
	var favbtn = document.getElementById("favbtn");
	favbtn.src = "../images/ai_minus.png";
	var favlnk = document.getElementById("favlink");
	favlnk.setAttribute("title", "Remove from my favorites");
	favlnk.setAttribute("href", "JavaScript:removeFavorite()");
	favlnk.innerText = "Remove from favorites";
	favlnk.textContent = "Remove from favorites";
}

function removeFavorite() {
	var favurl = "ShowContent.aspx?action=unfavorite&id=" + ccontentid + "&type=" + cdbtype;
	ajaxExecute(favurl);
	var favbtn = document.getElementById("favbtn");
	favbtn.src = "../images/ai_cross.png";
	var favlnk = document.getElementById("favlink");
	favlnk.setAttribute("title", "Add to my favorites");
	favlnk.setAttribute("href", "JavaScript:addFavorite()");
	favlnk.innerText = "Add to favorites";
	favlnk.textContent = "Add to favorites";
}

function rateContent(x) {
	var rateurl = "ShowContent.aspx?action=rate&id=" + ccontentid + "&rating=" + x + "&type=" + cdbtype;
	ajaxExecute(rateurl);
	for (var i = 1; i <= 5; i++) {
		var curstar = document.getElementById("rate" + i);
		if (i <= x) {
			curstar.setAttribute("curfilled", "yes");
		} else {
			curstar.setAttribute("curfilled", "no");
		}
	}
	resetRating();
}

function contentRating(x) {
	for (var i = 1; i <= 5; i++) {
		var curstar = document.getElementById("rate" + i);
		if (i <= x) {
			curstar.src = "../images/filledstar.gif";
		} else {
			curstar.src = "../images/emptystar.gif";
		}
	}
}

function resetRating() {
	for (var i = 1; i <= 5; i++) {
		var curstar = document.getElementById("rate" + i);
		if (curstar.getAttribute("curfilled") == "yes") {
			curstar.src = "../images/filledstar.gif";
		} else {
			curstar.src = "../images/emptystar.gif";
		}
	}
}




