﻿// JScript File
var Modules
var mouseX;
var mouseY;


function noMapFix()
{
    var maps = document.getElementsByTagName("map");
    var ImageList = document.getElementsByTagName("img");
    
    for (i = 0; i < maps.length; i++)
    {
        var isFound = false;
        if (maps[i])
        {
                for (x = 0; x < ImageList.length; x++)
                {
                    if (ImageList[x])
                    {
                        if (ImageList[x].useMap.replace("#","") == maps[i].id)
                        {
                            isFound = true;
                        }
                    }
        
                }
                if (isFound == false)
                {
                    var parentElement = maps[i].parentNode;
                    parentElement.removeChild(maps[i]);                
                }
        }    
    }
}

function ImageURLFix()
{
    var ImageList = document.getElementsByTagName("img");
    for (i = 0; i < ImageList.length; i++)
    {
        if (ImageList[i])
        {
            if (ImageList[i].getAttribute("elementtype") == "Image")
            {
                var ImageURL = ImageList[i].src;
                
                if (ImageURL.indexOf("http") != -1)
                {
                    var ImageURLArray = ImageURL.split("/");
                    
                    var NewImageURL = "";
                    for(x=3;x<ImageURLArray.length;x++)
                    {
                        NewImageURL += "/" + ImageURLArray[x];
                    }
                    ImageList[i].src = NewImageURL;
                }
            }
        }
    }
    //exec('save',null)
    alert('You can save now');
}


if (document.layers) 
{
    document.captureEvents(Event.MOUSEMOVE);
}
document.onmousemove=mtrack;

function mtrack(e) 
{
    if (document.layers)
    {
        mouseX = e.pageX;
        mouseY = e.pageY;
    }
    else
    {
        mouseX = event.x;
        mouseY = event.y;
    }
}
function tableEditor(cmd,Order,TableCourseID)
{
    var updater
     if (!document.getElementById('updater'))
	 {
		updater = document.createElement("<iframe id='updater' style='display:none;'></iframe>");
        document.body.appendChild(updater);
	 }
	 else
	 {
	    updater = document.getElementById('updater');
	 }
	 //alert("tableModify.asp?cmd="+cmd+"&Order="+Order+"&CourseTableID="+TableCourseID);
	 updater.src = "tableModify.asp?cmd="+cmd+"&Order="+Order+"&CourseTableID="+TableCourseID
	 
}

function tableEditorRowStyle(CourseTableRowID,buttonElement)
{

    if (buttonElement.src == "/imgs/editor/table/underline.png")
    {
        buttonElement.src = "/imgs/editor/table/nounderline.png"
    }
    else
    {
        buttonElement.src = "/imgs/editor/table/underline.png"
    }
     var updater
     if (!document.getElementById('updater'))
	 {
		updater = document.createElement("<iframe id='updater' style='disply:none;'></iframe>");
        document.body.appendChild(updater);
	 }
	 else
	 {
	    updater = document.getElementById('updater');
	 }
	 updater.src = "rowStyle.asp?CourseTableRowID="+CourseTableRowID
}

function loadTableMenu(elementID)
{
    var menuItems = new Array("Save","Add Row Below","Add Column Left","Delete Row","Delete Column","Close")
    var elementEditor = document.getElementById(elementID);
    var menuElementID = "menu" + elementID
    var parentElement = elementEditor.parentNode;
    
    if (!document.getElementById(menuElementID))
    {
        var menuElement = document.createElement("DIV");
    
       menuElement.id = menuElementID
       
       for(menuItem in menuItems)
       {
           var menuItemElement = document.createElement("img");
           menuItemElement.alt = menuItems[menuItem];
           menuItemElement.src = "/imgs/editor/table/"+menuItems[menuItem]+".gif";
           menuItemElement.onclick = "javascript:tableEditor('"+menuItems[menuItem]+"');";
           
           menuItemElement.cssClass = "tableMenuItem";
           menuElement.appendChild(menuItemElement);
       }
       alert(menuElement.innerHTML);
       parentElement.insertBefore(menuElement,elementEditor);
       
    }
    

}



function formatbutton(e)
{
	if (!e) var e = window.event;
	var action = e.type;
	//debug(action);
	if (e.srcElement) var element = e.srcElement
	else if (e.target) var element = e.target
	//debug(element);
	element.className = "btn_"+action;
}
function debug(strText)
{
    if (!document.getElementById("debugTxt"))
    {
       var oDebug = document.createElement("<TEXTAREA id='debugTxt' cols='100' rows='20' style='display:none;'>DEBUG:</TEXTAREA>");
       document.body.appendChild(oDebug);
     
    }
      document.getElementById("debugTxt").value += '\n' + strText;
}
function notSaved()
{
if (Modules){
alert('Your changes will not  be saved.');
}
}

function ImageDoubleClick()
{
    var clickedElement = event.srcElement;
    
    var argsArray = new Array();
    argsArray[0] = clickedElement;
    argsArray[1] = window;
    if (clickedElement.useMap != "")
    {
        argsArray[2] = document.getElementById(clickedElement.useMap.replace("#",""));
    }
    else
    {
        argsArray[2] = "";
    }
    var Stamp = new Date();
    showModalDialog('/admin/dialogs/importImage.aspx?now='+Stamp,argsArray, 'dialogWidth:600px;dialogHeight:600px;scroll:no; status:no;help:no;');

    
}

function attachDoubleClickToImages()
{
    var ImageList = document.getElementsByTagName("img");
    for (i = 0; i < ImageList.length; i++)
    {
        if (ImageList[i])
        {
            if (ImageList[i].getAttribute("elementtype") == "Image")
            {
                var ImageItem = ImageList[i];
                
                ImageItem.attachEvent('onclick',ImageDoubleClick);
            }
        }
    }
}

function attachEvents()
{
//debug("Called");
moduleID = window.event.srcElement.id;
document.getElementById(moduleID).attachEvent('onkeypress',edit);
document.getElementById(moduleID).attachEvent('oncut',edit);
document.getElementById(moduleID).attachEvent('onpaste',edit);
document.getElementById(moduleID).detachEvent('onfocus',attachEvents);
document.getElementById(moduleID).onfocus=null;
attachDoubleClickToImages();
}
function haxModules(moduleID)
{
    if (Modules==undefined)
    {
        Modules = new Array(1);
        Modules[0] = moduleID;
    }
}
function edit(oModule)
{
//debug(window.onunload);
//debug('firing')
moduleID = oModule.srcElement.id;
document.getElementById(moduleID).detachEvent('onkeypress',edit);
document.getElementById(moduleID).detachEvent('oncut',edit);
document.getElementById(moduleID).detachEvent('onpaste',edit);

if (Modules==undefined){
Modules = new Array(1);
Modules[0] = moduleID;
document.title = '*'+ document.title;

if (window.onunload !='')window.attachEvent('onunload',notSaved);
}
else
{
var isThere = false
for (Module in Modules)
	{
	//debug(Modules[Module]==moduleID);
	if (Modules[Module] == moduleID)
		{
		var isThere = true;
		break;	
		}
	}
	if (!isThere)
	{
	 Modules[Modules.length] = moduleID;
	}
}
}



function exec(cmd,opt)
{
	//debug(LastModuleID);
	//document.execCommand('ForeColor',null,'#ff0000');
	debug(cmd);
	debug(opt);
	debug(document.getElementById(ModuleID));
	try{
		switch (cmd)
		{

			default:	var ModuleID = Modules[0]
						var oModule	= document.getElementById(ModuleID);
						var Type	= oModule.getAttribute('type');
		}
	}
	catch(err)
	{
	    try
	    {
	        debug("looking for a range");
	        var selection = document.selection;
            var range = selection.createRange();
            var oElement = range.parentElement();
	        debug("found an :"+oElement.tagName);
	       
	        //fetchcontainingdiv(oElement)
	              
	       
	    } 
	    catch(err)
	    {
	     alert('No Modules have been edited/selected since your last save!\nYou need to select a module before trying to execute a command.')
        return null;
	    }	
	}
	
	//var view = document.getElementById(Window).getAttribute('view');
	//if (view=='html')
	//{
	//EditWindow = document.getElementById(Window)
	Stamp = new Date();
		switch(cmd)
		{   
		    case 'urlfix':
		                        
                                ImageURLFix();
		                        break;
		    case 'coursetable':
		                        showModalDialog('/admin/dialogs/add_course_table.asp?now='+Stamp,document, 'dialogWidth:700px;dialogHeight:600px;scroll:no; status:no;help:no;');refresh();break;
		                        break;
		    case 'savetable':
		                        if (!document.getElementById('updater'))
			                    {
			                        var updater = document.createElement("<iframe id='updater' style='displa:none;'></iframe>");
                                    document.body.appendChild(updater);
			                    }
			                    document.getElementById('updater').src = '/admin/courses/savecells.asp';
		                        break;
			case 'save': 
			                noMapFix();
			                if (!document.getElementById('updater'))
			                {
			                    var updater = document.createElement("<iframe id='updater' style='display:none;'></iframe>");
                                document.body.appendChild(updater);
			                }
			                debug('Type = ' + Type);
			                switch(Type)
							{
							case 'text'		: document.getElementById('updater').src = '/_cms/_update/update_module.asp?';break;
							default: debug('Error no type!');break;
						   	}
						   	break;
	
			case 'addpage':	    showModalDialog('/admin/dialogs/add_page.asp?now='+Stamp,document, 'dialogWidth:700px;dialogHeight:600px;scroll:no; status:no;help:no;');refresh();break;
			case 'delpage':	    showModalDialog('/admin/dialogs/del_page.asp?now='+Stamp,document, 'dialogWidth:700px;dialogHeight:300px;scroll:no; status:no;help:no;');refresh();break;
			case 'movepage':	showModalDialog('/admin/dialogs/move_page.asp?now='+Stamp,document, 'dialogWidth:700px;dialogHeight:300px;scroll:no; status:no;help:no;');refresh();break;
			case 'hidepage':	showModalDialog('/admin/dialogs/hide_page.asp?now='+Stamp,document, 'dialogWidth:700px;dialogHeight:300px;scroll:no; status:no;help:no;');refresh();break;
			case 'showpage':	showModalDialog('/admin/dialogs/show_page.asp?now='+Stamp,document, 'dialogWidth:700px;dialogHeight:300px;scroll:no; status:no;help:no;');refresh();break;			   	
						   	
		    case 'addflash':
		                    var quicktime = showModalDialog('/admin/dialogs/addflashmovie.asp?now='+Stamp,document, 'dialogWidth:700px;dialogHeight:500px;scroll:no; status:no;help:no;');

		                    break;
		    case 'addquicktime':
		                    var quicktime = showModalDialog('/admin/dialogs/addquicktime.asp?now='+Stamp,document, 'dialogWidth:700px;dialogHeight:500px;scroll:no; status:no;help:no;');
		                    break; 	  	
			case 'createimage':
							
							var Middles
							var myObject = new Object();

							Middles = Modules[Modules.length-1]
							Middeh = document.getElementById(Middles)
							myObject.moo = Middeh
							myObject.moop = document
							

							cimage = showModalDialog('/admin/dialogs/insert_image.asp?now='+Stamp,myObject, 'dialogWidth:700px;dialogHeight:500px;scroll:no; status:no;help:no;');
							if (cimage)
							{
								cimage = cimage.split("¬");
								//alert(cimage[0])
								if ((cimage[0].toLowerCase() != 'none') && (cimage[0] != ''))
								{
									NewHyperlink = document.createElement(cimage[0]);
									NewImage = document.createElement(cimage[1]);
									NewImage.src = NewImage.src.replace("http://"+window.location.hostname,"");
									
									NewHyperlink.appendChild(NewImage);
									Middeh.appendChild(NewHyperlink);
									NewImage.src = NewImage.src.replace("http://"+window.location.hostname,"");
								}
								else
								{
									NewImage = document.createElement(cimage[1]);
									NewImage.src = NewImage.src.replace("http://"+window.location.hostname,"");
									/*alert(NewImage.src.replace("http://"+window.location.hostname,""));
									alert(NewImage.src);
									alert("http://"+window.location.hostname);*/
									Middeh.appendChild(NewImage);
									NewImage.src = NewImage.src.replace("http://"+window.location.hostname,"");
								}
								
								
								
							}
							
							break;
			case 'removeparent':
							var sText = document.selection.createRange();
							sParent = sText.parentElement();
							
							if (sParent.tagName == "P")
							{
								

								sParentText = sParent.innerHTML;


								NewChild = "<mid>"+sParentText+"</mid>";
								//debug(NewChild);
								InsertChild = document.createElement(NewChild);

								ParentParent = sParent.parentElement;
								ParentParent.removeChild(sParent);
								//TNode = document.createTextNode(escape(sParentText));
								sText.pasteHTML(sParentText)
								//TNode = document.createTextNode(escape(sParentText));
								//debug(sParentText);
								//ParentParent.appendChild(TNode);
								
							}
							else
							{
								alert("Can not remove the parent element of this text");
							}
							//debug(sParent.tagName);
							break;
			case 'download' :
							Stamp = new Date();
							showModalDialog('/admin/dialogs/add_download.asp?now='+Stamp, document, 'dialogWidth:700px;dialogHeight:300px;scroll:no; status:no;help:no;');
							break;

			case 'View':	toggleSource(LastModule);
							break;
			case 'Lock':	toggleMode(LastModule);
							break;
			case 'palette':	//togglePalette(LastModule);
							PosY = event.offsetY;
							PosX = event.offsetX;
							LMode = document.getElementById(Modules[Modules.length-1]);
							
							togglePalette(PosY,PosX);
							break;	
			case 'Spell':	EditWindow.focus();
							toggleSource(LastModule);
							spell(LastModule);
							break;
			case 'otherstaff'	:window.location.href='/staff/other/addnew/';
			break;
			case 'link':Stamp = new Date();
							showModalDialog('/admin/dialogs/add_link.asp?now='+Stamp, document, 'dialogWidth:700px;dialogHeight:300px;scroll:no; status:no;help:no;');
							break;
			default:
							//if (view == 'source')
							//{
							//	if (confirm('You cannot format the text in source code view. Click OK to Switch to HTML mode?'))
							//	{
							//		toggleSource(LastModule);
							//		break;
							//	}	
							//	else return;
							//}
							//else
							//{
							document.execCommand(cmd,false,opt);
						//}
			
		//}
	}
}	

function addHref(target,link,title,hrefTarget)
{
    var stringA = 'A';
    var hrefElement = document.createElement(stringA);
                    
    hrefElement.setAttribute("href",link);
    hrefElement.setAttribute("title",title);
    hrefElement.setAttribute("target",hrefTarget);
    
    var blankTextNode = document.createTextNode("placeholder");
    hrefElement.appendChild(blankTextNode);
    
    target.parentNode.insertBefore(hrefElement,target);
    
    hrefElement.replaceChild(target,blankTextNode);
                   
}

function addImageMap(id,image)
{
    var imageMapElement = document.createElement("<map name='map_"+id+"' />");
    imageMapElement.id = "map_"+id;
    imageMapElement.name = imageMapElement.id;
    imageMapElement.setAttribute("name",imageMapElement.id);
    
    var imageParent = image.parentNode;
    imageParent.appendChild(imageMapElement);

}

function noWay(parentElement,element)
{
    parentElement.appendChild(element);
}

function removeImageMap(id)
{
    var parentElement = id.parentNode;
    alert(parentElement);
    parentElement.removeChild(id);
}
function checkExists(id)
{
    if (document.getElementById(id))
    {
        alert('exists');
    }
}
function removeChildren(id)
{

    var element = document.getElementById(id);
    if (element.childNodes.length)
    {
        while(element.childNodes.length != 0)
        {
            element.removeChild(element.childNodes[0]);
        }
    }
 

}
function addImageMapArea(id,href,title,coords)
{
    //alert('Image Map Area');
    var mapElement = document.getElementById(id);
    var imageMapArea = document.createElement("area");
    imageMapArea.href = href;
    imageMapArea.alt = title;
    imageMapArea.coords = coords;
    imageMapArea.shape = "rect";
    //alert(mapElement.childNodes.length);
    
    mapElement.appendChild(imageMapArea);

    

}

function togglePalette(Y,X)

{
	//alert(LastModule);
//	alert(Y);
//	alert(X);
	if (document.getElementById('Palette').style.display == "")
	{
	document.getElementById('Palette').style.display = "none";
	} 
	else
	{
	
	//alert(document.getElementById('Palette').style.position);
	//document.getElementById('Palette').style.position = "relative";
	Y -= 8;
	//X += <%=12 * 26%>;
//	alert(X);
	//document.getElementById('Palette').style.top = Y+"px";
	//document.getElementById('Palette').style.left = X+"px";
document.getElementById('Palette').style.display = "";
	
	}
}