﻿var webSiteRoot = window.location.protocol + "//" + window.location.host + "/";

function confirmDelete(msg)
{
	return confirm(msg); 
}

function openTelerikWindow(page, query, window, width, height, title)
{
	var qry = (query) ? '?' + query : '';
	var win = radopen(webSiteRoot + page + qry, window);
	//win.set_height(height);
	//win.set_width(width);
	win.SetTitle(title);
	win.set_modal(true);
	win.center();
}

function openTelerikWindowXY(page, query, window, width, height, title, xVal, yVal, isModal)
{
	var qry = (query) ? '?' + query : '';
	var win = radopen(webSiteRoot + page + qry, window);
	//win.set_height(height);
	//win.set_width(width);
	win.SetTitle(title);
	win.set_modal(isModal);
	win.MoveTo(xVal, yVal);
}

function EditProfile(type) 
{
	var win = radopen(webSiteRoot + "Modals/Portals/EditProfiles.aspx?type=" + type.toLowerCase(), null);
	if (type.toLowerCase() == "user") 
	{
		win.set_height(650);
		win.set_width(850);
		win.SetTitle('Editing User Profile');
	}
	else if (type.toLowerCase() == "faculty")
	{
		win.set_height(650);
		win.set_width(850);
		win.SetTitle('Editing Faculty Profile');
	}
	else if (type.toLowerCase() == "student")
	{
		win.SetTitle('Editing Student Profile');
		win.set_height(650);
		win.set_width(800);
	}
	
	win.set_modal(true);
	win.center();
}

// text block create button
function SubmitMediaBlock(type, classid, textid) 
{
	var win = radopen(webSiteRoot + "Modals/Portals/SubmitMediaBlock.aspx?type=" + type + "& cid=" + classid + "&tid=" + textid, null);
	//win.set_height(600);
	//win.set_width(850);
	win.set_modal(true);
	win.center();
}

// text block delete button
function OnDockDeleteCommand(sender, eventArgs)
{
	if (!confirm("Are you sure you want to delete this text block?"))
	{
		eventArgs.set_cancel(true);
	}
	else
	{
		// confirmed delete of text block, load page, delete, refresh window
		var idToDelete = sender.get_id().indexOf("_RadDock");
		idToDelete = sender.get_id().substring(idToDelete + 8, sender.get_id().length)
		var win = radopen(webSiteRoot + "pages/SubmitMediaBlock.aspx?dmb=" + idToDelete, null);
		win.set_height(600);
		win.set_width(850);
		win.set_modal(true);
		win.center();
	}
}

function OnDockEditCommand(sender, eventArgs)
{
	var idToEdit = sender.get_id().indexOf("_RadDock");
	idToEdit = sender.get_id().substring(idToEdit + 8, sender.get_id().length)
	var win = radopen(webSiteRoot + "pages/SubmitMediaBlock.aspx?mb=" + idToEdit, null);
	win.set_height(600);
	win.set_width(850);
	win.set_modal(true);
	win.center();
}

function SubmitArticle() {
	var win = radopen(webSiteRoot + "pages/SubmitArticle.aspx", "SubmitArticle");
	win.set_height(600);
	win.set_width(850);
	win.set_modal(true);
	win.center();
}

function ShowClassNote(noteId) {
	var win = radopen(webSiteRoot + "pages/ClassNotes.aspx?noteid=" + noteId, "ClassNotes");
	win.set_height(500);
	win.set_width(450);
	win.set_modal(true);
	win.center();
}
	
/* Display the assignement window for the specified class - if the assignement id is provided then its an update */
function EditAssignment(mode, classid, facultyid, assignmentid) {
	var win = radopen(webSiteRoot + "Modals/Portals/ClassAssignments.aspx?mode=" + mode + "&cid=" + classid + "&fid=" + facultyid + "&id=" + assignmentid, null);
	win.set_height(600);
	win.set_width(850);
	if (mode == 'create') win.SetTitle('Adding an Assignment');
	else if (mode == 'edit') win.SetTitle('Editing an Assignment');
	win.set_modal(true);
	win.center();
}

function ViewAssignment(assignmentid) {
	var win = radopen(webSiteRoot + "Modals/Portals/ViewAssignment.aspx?aid=" + assignmentid, null);
	win.set_height(500);
	win.set_width(565);
	win.SetTitle('Viewing an Assignment');
	win.set_modal(true);
	win.center();
}

function EditCourse(mode, classid) {
	var win = radopen(webSiteRoot + "Modals/School/EditCourse.aspx?mode=" + mode + "&cid=" + classid, null);
	win.set_height(600);
	win.set_width(565);
	if (mode == 'create') win.SetTitle('Adding an Assignment');
	else if (mode == 'edit') win.SetTitle('Editing a Course Information');
	win.set_modal(true);
	win.center();
}

/* The assignment window is done and needs to be closed and we need to update */
function AssignmentUpdate() {
	InitiateAjaxRequest("Nothing");
}

/* Show the list of students for the specified class id */
function StudentClassList(classid) {
	var win = radopen(webSiteRoot + "Modals/Portals/ClassStudents.aspx?cid=" + classid, null);
	win.set_height(500);
	win.set_width(565);
	win.SetTitle('Class Roster');
	win.set_modal(true);
	win.center();
}

/* Toggle the display of a div - attached to the Hide button on the portal panels */
function ToggleDivDisplay(name1, name2, sender) 
{
	var div1 = document.getElementById(name1);
	var div2 = document.getElementById(name2);
	if ( div1 != null && div1.style.display == 'none' ) div1.style.display = 'block';
	else if ( div1 != null ) div1.style.display = 'none';
	if ( div2 != null && div2.style.display == 'none' ) div2.style.display = 'block';
	else if ( div2 != null ) div2.style.display = 'none';
	if (sender)
	{
		if (sender.innerHTML == "–") sender.innerHTML = "+";
		else if (sender.innerHTML == "+") sender.innerHTML = "–";
	}
}

function openWindow(url,name,options)
{
	var newWindow = window.open(url,name,options);
	newWindow.focus();
}

function closeEdit(isModal, backUrl)
{
	if(isModal)
	{
		CloseOnReload(); 
		return false;
	}
	else
	{
		window.location = backUrl;
	}
}

function getWidth()
{
      var x = 0;
      if (self.innerHeight)
      {
              x = self.innerWidth;
      }
      else if (document.documentElement && document.documentElement.clientHeight)
      {
              x = document.documentElement.clientWidth;
      }
      else if (document.body)
      {
              x = document.body.clientWidth;
      }
      return x;
}

function getHeight()
{
      var y = 0;
      if (self.innerHeight)
      {
              y = self.innerHeight;
      }
      else if (document.documentElement && document.documentElement.clientHeight)
      {
              y = document.documentElement.clientHeight;
      }
      else if (document.body)
      {
              y = document.body.clientHeight;
      }
      return y;
}

function create_link(editorID, mediaID)
{
    var editor = $find(editorID);
    if (editor == null) {
        alert("Could not find the edit window:" + editorID);
        return;
    }
    if ( editor.getSelectionHtml().length == 0 )
    {
        alert("Please select some text first");
        return;
    }
    var url = "<a href=\"javascript:OpenMediaFile('" + mediaID + "')\">" + editor.getSelectionHtml() + "</a>";
    editor.pasteHtml(url);
}

function insert_image(editorID, mediaID, imageLoc) {
    var editor = $find(editorID);
    if ( editor.getSelectionHtml().length > 0 )
    {
        alert("Please don't select text for this function");
        return;
    }
    var url = "<img src=\"" + imageLoc + "\" border=\"0\" alt=\"MediaLibrary#" + mediaID + "\" class=\"TextImg\">";
    
    editor.pasteHtml(url);
}

function insert_gallery(editorID, flashvars, width, height) 
{
	var editor = $find(editorID);
    if ( editor.getSelectionHtml().length > 0 )
    {
        alert("Please don't select text for this function");
        return;
    }
    
    var url = "<embed type=\"application/x-shockwave-flash\" src=\"/flash/gallery.swf\" width=\"" + width + "\" height=\"" + height + "\" id=\"gallery\" name=\"gallery\" bgcolor=\"#ffffff\" quality=\"high\" scale=\"exactfit\" menu=\"true\" FlashVars=\"" + flashvars + "\" align=\"top\" salign=\"lt\" allowfullscreen=\"true\" />";
    
    editor.pasteHtml(url);
}

function get_object(obj)
{
    if (document.getElementById) return document.getElementById(obj);
    else if (document.layers) return document.layers[obj];
    else if (document.all) return document.all[obj];
}

function show_div(obj)
{
	obj.style.display = 'block';
}

function close_div(obj)
{
	obj.style.display = 'none';
}

function toggle_div(name)
{
	var div = get_object(name);
	if (div.style.display == 'none') 
	{
		show_div(div);
	}
	else 
	{
		close_div(div);
	}
}

function showItem(name, show)
{
	var item = get_object(name);

	if (show)
	{
		item.style.display = "block";
	}
	else
	{
		item.style.display = "none";
	}
}

function showItemByValue(curValue, name, compareValue, equal)
{
	var item = get_object(name);
	
	if (equal)
	{
		item.style.display = (curValue == compareValue) ? "block" : "none";
	}
	else
	{
		item.style.display = (curValue != compareValue) ? "block" : "none";
	}
}

function switch_tabs(curTab, newTab, curDiv, newDiv, activeStyle, inactiveStyle)
{
	get_object(curTab).setAttribute("class", inactiveStyle);
	get_object(curTab).setAttribute("className", inactiveStyle);
	get_object(newTab).setAttribute("class", activeStyle);
	get_object(newTab).setAttribute("className", activeStyle); 
	
	close_div(get_object(curDiv));
	show_div(get_object(newDiv));
}