﻿var dialogPath = rootPath + "tdas/modules/dialog.aspx";

function ToggleComponentVisibility(toggle, panelId) {
	var panel = document.getElementById(panelId);
	
	panel.style.display = (panel.style.display == 'none' ? 'block':'none');
	toggle.src = (toggle.src.indexOf('minus.gif') > 0 ? toggle.src.replace('minus','plus'):toggle.src.replace('plus','minus'));
}

function DisplayCalendarEvent(rootPath, eventId) {
	window.open(rootPath + "content/event.aspx?eventId=" + eventId, "calendar", "width=300, height=400, status=yes");
}

function LaunchCampusSite(sectionId) {
	if(sectionId != "") {
		location.href = rootPath + "content/campus.aspx?sectionId=" + sectionId;
	} else {
		alert("This campus does not have a new website yet.");
	}
}

function OpenUrl(url) {
	location.href = url;
}

function OpenPage(sectionId, topicId, documentId) {
	var bIsTopic = (topicId != void(0) && topicId != ''),
		bIsDocument = (documentId != void(0) && documentId != ''),
		bIsSection = (sectionId != void(0) && sectionId != '');
		
	if(bIsSection) {
		location.href = rootPath + 'content/page.aspx?sectionId=' + sectionId;
	} else if(bIsTopic) {
		location.href = rootPath + 'content/page.aspx?topicId=' + topicId;
	} else if(bIsDocument) {
		location.href = rootPath + 'content/document.aspx?documentId=' + documentId;
	} else {
		alert("This link does not point to a valid resource.");
	}
}

function OpenEmailEditor(sectionId, employeeId) {
	var win = window.open(rootPath + "support/email.aspx?sectionId=" + sectionId + "&employeeId=" + employeeId, "email", "width=500, height=400");
	if(win == void(0)) {
		alert('The email editor appears to have been blocked by a pop-up blocker.  Please allow pop-ups for this site if you wish to send an email to our employees.');
	}
}

function SubmitForm(action, argument) {
	try {
		if(action != void(0)) {
			document.forms[0].__TISDFormAction.value = action;
		}
		
		if(argument != void(0)) {
		    document.forms[0].__TISDFormArgument.value = argument;
		}

		document.forms[0].submit();
	} catch(e) { alert(e.description); }
}

function OpenModalDialog(url, attributes, header, footer) {
	attributes = (attributes != void(0) && attributes.length > 0) ? getModalOptions(attributes):"dialogWidth:300px;dialogHeight:300px";
	if(header == void(0) || header == null) {
		header = true;
	}
	if(footer == void(0) || footer == null) {
		footer = true;
	}

	return window.showModalDialog(dialogPath + "?type=" + escape(url) + "&header=" + header + "&footer=" + footer, "MODALDLG", "help: no; center: yes; status: yes;" + attributes);
}

function getModalOptions(attribute_string) {
	var attributes = new String(),
		attribute_name = new String(),
		attribute_value = new String();
	attribute_string = attribute_string.split(',');
	
	for(var i = 0; i < attribute_string.length; i++) {
		attribute_name = Trim(attribute_string[i].split('=')[0]);
		attribute_value = Trim(attribute_string[i].split('=')[1]);
		
		switch(attribute_name) {
			case "dialogWidth":
			case "width":
				attributes += "dialogWidth:" + attribute_value + "px;";
				break;
			case "dialogHeight":
			case "height":
				attributes += "dialogHeight:" + attribute_value + "px;";
				break;
			case "dialogLeft":
			case "left":
				attributes += "dialogLeft:" + attribute_value + ";";
				break;
			case "dialogTop":
			case "top":
				attributes += "dialogTop:" + attribute_value + ";";
				break;
			case "status":
			case "statusbar":
				attributes += "status:" + attribute_value + ";";
				break;
			case "scroll":
			case "scrollbar":
				attributes += "scroll:" + attribute_value + ";";
				break;
			case "resizable":
			case "resize":
				attributes += "resizable:" + attribute_value + ";";
				break;
			case "center":
			case "centered":
				attributes += "center:" + attribute_value + ";";
				break;
			default:
				alert("Unknown option:" + attribute_name);
				break;
		}
	}
	
	return attributes;
}

function Trim(string) {
	return string.replace(/^\W*|\W*$/gi,"");
}

function RTrim(string) {
	return string.replace(/\W*$/,"");
}

function LTrim(string) {
	return string.replace(/^\W*/,"");
}