<!--
///////////////////////////////
// MVSD popup.js
// Version: 1.0.0 10-12-2004
// Preconditions:
//		-	The page which includes this file, has to have a cute-editor.
//		Naar de volgende files wordt in deze file verwezen.
//		-	popuphyper.asp
//		-	popupbookmark.htm
//		-	popupbookmarkmanager.asp
//		-	bookmarklist.asp
//		-	bookmarklist.htm
// Dependencies:
//		-	none
// Description:
// Provides functionality for the 3 custombuttons on the EasyWebEdit cute editor.
//	1.	Hyperlinkbutton :		showHyperlinkDialog()
//	2.	BookmarkInsertButton:	showBookmarkDialog()
//	3.	BookmarkEditButton:		showBookmarkManageDialog()
//	De andere functies zijn ondersteunend.
///////////////////////////////

            function showPopupDiv(popupTekst, objPicture)
            {
                var objPictureOffset = calculateOffset(objPicture);
                var objPictureOffsetTop = calculateOffsetTop(objPicture);
                
                document.getElementById('InfoDivContent').innerHTML = popupTekst;
                document.getElementById('InfoDiv').style.left = objPictureOffset;
                document.getElementById('InfoDiv').style.top = objPictureOffsetTop;
                document.getElementById('InfoDiv').style.visibility = 'visible';
            }
            
            function showHelpDialog(isSubFolder)
            {
                if (isSubFolder)
                {
                    window.open("../Help.aspx","_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=550, height=450");
                }
                else
                {
                    window.open("Help.aspx","_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=550, height=450");
                }
            }

			///this function pops up a modal dialog(popup.asp). This popup lets you choose and insert
			/// hyperlinks (custom & cms) 
			function showHyperlinkDialog(editor, intPageId, strPath)
			{				
			    // Request focus on the editor.
				editor.focus();	
				
				//get the current selection.
				var selection = editor.GetSelection();

				//Retrieve the rangetype Text, Control or None
				var RangeType = selection.type;

				// Creates a TextRange object from the current text selection, 
				// or a controlRange collection from a control selection. 
				var selectedRange = selection.createRange();
				
				var objParentElement;
                var imgItem;

				//If the range is a control
				if (RangeType == "Control")
				{
					// if the control is not an image
					if (selectedRange.item(0).tagName.toLowerCase() != "img")
					{
						return;
					}
					else
					{
					    // the control is an image, select the image-element
						imgItem = selectedRange.item(0);
						
						// check if a "border" property is present within the element.
						// If there is no border property set, the default setting for a image within a
						// link, is to set the border to 1. So by setting it to 0 in advance, there won`t be 
						// a border after applying the link.
						if (imgItem.border == "")
						{
							imgItem.border = 0;
						}
						
						objParentElement = imgItem.parentElement;
					}
				}
				else
				{
					//if the selected item is not a control, get the element where the text resides in.
					objParentElement = selectedRange.parentElement();
				}
				
																							
				// Find the enclosing parent link if it exists.
				var blnDone = false;
				while (objParentElement && !blnDone)
				{
				    var strNodeName = objParentElement.nodeName;
				    if ("#document" == strNodeName)
				    {
				        blnDone = true;
				        objParentElement = null;
				    }
				    else if("A" == strNodeName)
				    {
				        blnDone = true;
				    }
				    else
				    {
				        objParentElement = objParentElement.parentNode;
				    }
				}
				
				//if the selection is a bookmark, undo selection.
/*
				if(selectedRange)
				{
					if(selectedRange.htmlText)
					{
						alert('selected range = ' + selectedRange.htmlText);
						if(IsBookMark(selectedRange.htmlText))
						{
							selectedRange = selectedRange.collapse(true);
							selectedRange = null;
							objReference = null;
						}
					}
				}				
*/
				
				// Moves the text range so that the start and end positions of the range encompass 
				// the text in the given element.
				if (objParentElement && objParentElement.nodeName == "A")
				{
				    if (imgItem)
				    {
    			        // Create a dummy textrange, from the current selection
				        var textRange = selection.createRange();
				        selectedRange = textRange;
				    }
				    else
				    {
					    selectedRange.moveToElementText(objParentElement);
					}
				}
														
				var parameters = new Array();
								
				if (objParentElement)	
				{
					parameters[0] = objParentElement;
				}
				else
				{
					parameters[0] = null;
				}
				
				// get the selected text
				if(selectedRange)
				{
					if(selectedRange.text)
					{
						parameters[1] = selectedRange.text;
					}
					else
					{
						parameters[1] = null;
					}
				}
				else
				{
					parameters[1] = null;
				}
			
				// get the current page id, add to parameter
				parameters[2] = intPageId;
								
				// get the bookmarks from the current page.
				var bookmarkArrayCollection = getBookMarkArray(editor.getHTML());
				var arrBookmarks = bookmarkArrayCollection[0];
				parameters[3] = arrBookmarks;

				// get the title of the current page if the page is new
				if (-1 == intPageId)
				{
				    if (document.forms[0].elements.Title)
				    {
					    parameters[4] = document.forms[0].elements.Title.value
					    if ("" == parameters[4])
					    {
						    parameters[4] = "(huidige pagina)";
					    }
					}
				}
				else
				{
					parameters[4] = null
				}
								
				var attr = "status:no;dialogWidth:450px;dialogHeight:450px;scroll:no;resizable:no;help:no";
				var page = strPath + "InsertHyperLink.aspx";
				
				var LinkElements;
				LinkElements = showModalDialog(page, parameters, attr);
									
				if(LinkElements)
				{
					var strText;
					if (selectedRange && RangeType != "None" && (imgItem || selectedRange.htmlText != ""))
					{
					    selectedRange.execCommand('RemoveFormat');
						selectedRange.execCommand('unlink');
						
					    // Check if an image was selected.
                        if (imgItem && null != imgItem)
                        {
                            strText = imgItem.parentElement.innerHTML;
                        }
                        else if(selectedRange.text)
						{
							strText = LinkElements['title']
						}
						else
						{
							strText = selectedRange.htmlText;
						}
						selectedRange.execCommand('Delete');
					}
				    else
					{
						//no selection, first grab the title, if there is none, grab the href!
						if(LinkElements['title'] != "")
						{
							strText = LinkElements['title'];
						}
						else
						{
							strText = LinkElements['href'];
						}
					}
					var HTML = "<a href='" + LinkElements['href'] + "' target='" + LinkElements['target'] + "'>" + strText + "</a>";
					editor.focus();

                    editor.PasteHTML(HTML);
				}				
			}
						
			///pops up a modal dialog, to create bookmarks where the cursor is located.
			function showBookmarkDialog(ID)
			{
				//The id of the clientside editor object(layer <div>),
				//is the id of the serverside object + "_editBox"
				var EditorID = ID + "_editBox"; 
				var editor = document.getElementById(EditorID);							
				editor.focus();	
				
				//get the selection object.(DHTML object)
				var selection = editor.document.selection;
				
				//Creates a TextRange object from the current text selection, 
				//or a controlRange collection from a control selection. 
				var selectedRange	= selection.createRange();
				
				//Text or Control
				var RangeType = selection.type;	
												
				//If the range is a control
				if (RangeType == "Control"){		
					//if the control is not an image
					if (selectedRange.item(0).tagName.toLowerCase() != "img")
						return;
					else{
						//the control is an image, get the html text
						var oTextRange = document.body.createTextRange();
						oTextRange.moveToElementText(selectedRange.item(0).parentElement);
						selectedRange = oTextRange;
						selectedRange.select();
					}
				}
				//now I can get the htmlText for both the ranges, textRange as well as controlRange.
				var rangeTempHtml = selectedRange.htmlText;
				
				selectedRange.execCommand('Delete');											
								
				var attr = "status:no;dialogWidth:450px;dialogHeight:200px;scroll:no;resizable:no;help:no";
				var page = "popupbookmark.asp";
							
				var bookmark;
				bookmark = showModalDialog(page, "", attr);
													
				if(bookmark){					
					insertHTML(EditorID,bookmark + rangeTempHtml);	
				}
				else{
					insertHTML(EditorID, rangeTempHtml);	
				}
				editor.focus();	
			}		
			
														
			//extracts bookmarks from string
			//returns two arrays:
			//1. Array with the names of the bookmarks.
			//2. Array with the complete bookmarks.
			//todo: create generic names for variables!!!!
			function getBookMarkArray(strHTML)
			{
				
				//create an array to add the bookmarks to (later the array is sent to the modaldialog).
				var bookmarkArray = new Array();
				//array with the bookmarknames
				var bookmarkArrayNames = new Array();
								
				if(strHTML != ""){
					var count, index, blnContainsBookmarks;
					blnContainsBookmarks = true;
					
					//count: this variable keeps track of the number of items are added to the array.
					//this value is inserted in the array as a identifier, for compare purposes,
					//with the returned array.
					count = 0;
					//scan html for bookmarks
					var strHTMLUCase = strHTML.toUpperCase();						
					
					while(blnContainsBookmarks){
					
						//get the index of the first occurance of a bookmark/link(anchor)
						index = strHTMLUCase.indexOf("<A");
						
						//if there is not an occurance of "<A" then this string contains no bookmarks.
						if (index > -1){
							//cut the first piece (where there are no links) off of the string.
							strHTML = strHTML.substring(index, strHTML.length);
							strHTMLUCase = strHTMLUCase.substring(index, strHTMLUCase.length);
											
							//get the index of the links` closing tag.
							var tindex = strHTMLUCase.indexOf("</A>");
							
							//if there is no closing tag, there is no link
							if (tindex > -1){
						
							//get the link-string
							var strLINK = strHTML.substring(0, tindex+4);
							
							//remove the link part of strHTML, if not the loop will be infinite.
							strHTML = strHTML.substring(tindex+4, strHTML.length);
							strHTMLUCase = strHTMLUCase.substring(tindex+4, strHTMLUCase.length);
												
							//check the link for href, onclick and linktext(<a....>linktext</a>).
							var hrefIndex, onclIndex, linkText;
							hrefIndex = strLINK.toUpperCase().indexOf("HREF");
							onclIndex = strLINK.toUpperCase().indexOf("ONCLICK");
							//TODO : possible the hrefIndex and/or the onclIndex do exist, so you can check for values, to be cetain
							var strLINKTEXT, indexCloseBracket;
							strLINKTEXT = strLINK.substring(0, (strLINK.length - 4));
							indexCloseBracket = strLINKTEXT.indexOf(">");
							strLINKTEXT = strLINK.substring(indexCloseBracket+1, strLINKTEXT.length);
																						
							if(strLINKTEXT == "" && hrefIndex == -1 && onclIndex == -1){
								
								bookmarkArray.push(new Array(count,strLINK));
								
								//NAME
								//a bookmark is found, get the name and or id of the bookmark.
								var strName, strId, indexName, indexId
								indexName = strLINK.toUpperCase().indexOf("NAME")
																
								//remove all characters previous to "NAME"
								strName = strLINK.substring(indexName+5,strLINK.length);
								
								//remove end link tag "</A>"
								strName = strName.substring(0, (strName.length-5));
													
								//While this is a bookmark, all elements other than "NAME" and "ID" do not
								//occur within the strLINK. So check the remainder for either, and the value
								//we are looking for will remain.
								
								var indexIDInName;
								indexIDInName = strName.toUpperCase().indexOf("ID=");
								
								//if "ID" occurs within strName, remove the rest of the link from that point,if not do nothing
								if (indexIDInName > -1){
									strName = strName.substring(0, (indexIDInName-1));
								}
								else{
									//do nothing.
								}
																																			
								//ID
								indexId = strLINK.toUpperCase().indexOf("ID=");
								//remove all characters previous to "ID"
								strId = strLINK.substring(indexId+3,strLINK.length);
								//remove end link tag "</A>"
								strId = strId.substring(0, (strId.length-5));
					
								//While this is a bookmark, all elements other than "NAME" and "ID" do not
								//occur within the strLINK. So check the remainder for either, and the value
								//we are looking for will remain.
								
								var indexNAMEInstrId;
								indexNAMEInstrId = strId.toUpperCase().indexOf("NAME");
								
								//if "ID" occurs within strName, remove the rest of the link from that point,if not do nothing
								if (indexNAMEInstrId > -1){
									strId = strId.substring(0, (indexNAMEInstrId-1));
								}
								else{
									//do nothing.
								}
																
								//add to array
								if ((strName == strId) || strName != ""){
									//verwijder eventuele quotes.
									if (strName.indexOf("\"") == 0)
									{
										//there are quotes, remove the last one as well.
										strName = strName.substring(1,(strName.length-1));										
									}
									
									bookmarkArrayNames.push(new Array(count, strName));
								}					
								else if (strName == "" && strId != ""){
									if (strId.indexOf("\"") == 0)
									{
										//there are quotes, remove the last one as well.
										strId = strId.substring(1,(strId.length-1));										
									}
									bookmarkArrayNames.push(new Array(count, strId));
								}		
								else{
									bookmarkArrayNames.push(new Array(count, "bookmark with no name or id"));
								}
								//raise the identifier
								count = count + 1;
						}
						else{
							//a hyperlink is found, do nothing
						}						
							}
							else{
								blnContainsBookmarks = false;
							}
						}
						else{
							blnContainsBookmarks = false;
						}
					}
				}
				
			
				return new Array(bookmarkArrayNames,bookmarkArray);
			}	
						
						
			//the manager displays the bookmarks, the bookmarks can be removed or renamed.
			function showBookmarkManageDialog(ID)
			{
				//The id of the clientside editor object(layer <div>),
				//is the id of the serverside object + "_editBox"
				var EditorID = ID + "_editBox"; 
				var editor = document.getElementById(EditorID);							
				editor.focus();	
				
				//retrieve the bookmarks from the html
				var bookmarkArrayCollection = getBookMarkArray(editor.innerHTML);
				var bookmarkArrayNames = bookmarkArrayCollection[0];
				var bookmarkArray = bookmarkArrayCollection[1];
								
				var attr = "status:no;dialogWidth:450px;dialogHeight:300px;scroll:no;resizable:no;help:no";
				var page = "popupbookmarkmanager.asp";
							
				if (bookmarkArrayNames.length == 0){
					alert("There are no bookmarks on this page.");
				}
				else{
					//make a copy of the original array, while it could be modified by the modaldialog
					//(while it`s reference is sent as parameter)
					var arrOrig = new Array();
					for(var i =0; i<bookmarkArrayNames.length;i++){
						arrOrig.push(new Array(bookmarkArrayNames[i][0],bookmarkArrayNames[i][1]));
					}
					
					var arrReturn;
					arrReturn = showModalDialog(page, bookmarkArrayNames, attr);
															
					if(arrReturn){					
						
						//FIRST ---->>>> remove the deleted bookmarks.
						//loop through the original array
						for(var h=0;h<arrOrig.length;h++){
													
							//check for the occurance of the first value within the returned array
							var blnValueExists = false;
							for(var k=0;k<arrReturn.length;k++){
								if(arrReturn[k][0] == arrOrig[h][0]){
									blnValueExists = true;
								}
							}
																					
							//if the value isn`t found within the returned array, the bookmark is to be removed from the original array, 
							//and needs to be removed from the html as well.
							if(!blnValueExists){
															
								var html = editor.innerHTML;
								var htmlUpper = html.toUpperCase();	
								
								//bookmarkArray is the array with the complete bookmark link (this array`s length is identical to arrOrigs` length)
								//bookmarks are -supposed to be - unique, so we can use a replace function, to remove the bookmark.
								var indexOfBookmark = htmlUpper.indexOf(bookmarkArray[h][1].toUpperCase());
																							
								//if the link is found. (it should be, because it was found within the html)
								if(indexOfBookmark > -1){
									
									var linkInHTML = html.substring(indexOfBookmark, (indexOfBookmark + bookmarkArray[h][1].length));
									
									//replace the bookmark withing the editors html, with an empty string.
									html = html.replace(linkInHTML,"");
																	
									editor.innerHTML = html;
									
									//remove this bookmark from the original arrays as well.
									arrOrig.splice(h,1);
									bookmarkArray.splice(h,1);
									h = h-1;
								}
							}
						}
												
						//SECOND ---->>>> replace/rename the changed/renamed bookmarks.
						//loop through the original array (the removed bookmarks are removed from this array at this point.)
						for(var h=0;h<arrOrig.length;h++){
													
							//check for the occurance of the first value within the returned array
							var blnRenamed = false;
							for(var k=0;k<arrReturn.length;k++){
								if(arrReturn[k][0] == arrOrig[h][0]){
									if(arrReturn[k][1] != arrOrig[h][1]){
										//the name of the bookmark has been changed, apply this changes.
										
										//get the html from the editor
										var html = editor.innerHTML;
										
										//get the bookmark in the html.
										var indexOfBookmark = html.indexOf(bookmarkArray[h][1]);
										
										//extract the bookmark from the html
										var linkInHTML = html.substring(indexOfBookmark, (indexOfBookmark + bookmarkArray[h][1].length));
																		
										//Situation:
										//if no space (" ") is present in the orig-link-name, then there are no quotes, and
										//if there is a(re) space(s) within the returned orig-link-name, quotes need to be added,
										//because the editor will remove the text after the first space of the Id-property.
										if((arrOrig[h][1].indexOf(" ") == -1) && (arrReturn[k][1].indexOf(" ") > -1)){
											arrReturn[k][1] = "\"" + arrReturn[k][1] + "\"";
										}
																			
										//replace the bookmark with the new value
										//the occurance is twice (name + id), use regexp (g=global, i=caseinsensitive)
										myregexp = new RegExp(arrOrig[h][1], "gi");
										
										var newLinkInHtml = linkInHTML.replace(myregexp, arrReturn[k][1])
										
										html = html.replace(linkInHTML, newLinkInHtml);
										
										editor.innerHTML = html;
									}
								}
							}
						}
					}
				}
			}
			
			
			
			
			
			
			
			
        ///this function pops up a modal dialog(popup.asp). This popup lets you choose and insert
			/// youtoubevideos (custom & cms) 
			function showYouTubeDialog(editor, intPageId, strPath)
			{				
			    // Request focus on the editor.
				editor.focus();	
				
				//get the current selection.
				var selection = editor.GetSelection();

				//Retrieve the rangetype Text, Control or None
				var RangeType = selection.type;

				// Creates a TextRange object from the current text selection, 
				// or a controlRange collection from a control selection. 
				var selectedRange = selection.createRange();
				
				var objParentElement;
                var imgItem;

				//If the range is a control
				if (RangeType == "Control")
				{
					// if the control is not an image
					if (selectedRange.item(0).tagName.toLowerCase() != "img")
					{
						return;
					}
					else
					{
					    // the control is an image, select the image-element
						imgItem = selectedRange.item(0);
						
						// check if a "border" property is present within the element.
						// If there is no border property set, the default setting for a image within a
						// link, is to set the border to 1. So by setting it to 0 in advance, there won`t be 
						// a border after applying the link.
						if (imgItem.border == "")
						{
							imgItem.border = 0;
						}
						
						objParentElement = imgItem.parentElement;
					}
				}
				else
				{
					//if the selected item is not a control, get the element where the text resides in.
					objParentElement = selectedRange.parentElement();
				}
				
																							
				// Find the enclosing parent link if it exists.
				var blnDone = false;
				while (objParentElement && !blnDone)
				{
				    var strNodeName = objParentElement.nodeName;
				    if ("#document" == strNodeName)
				    {
				        blnDone = true;
				        objParentElement = null;
				    }
				    else if("A" == strNodeName)
				    {
				        blnDone = true;
				    }
				    else
				    {
				        objParentElement = objParentElement.parentNode;
				    }
				}
				
				//if the selection is a bookmark, undo selection.
/*
				if(selectedRange)
				{
					if(selectedRange.htmlText)
					{
						alert('selected range = ' + selectedRange.htmlText);
						if(IsBookMark(selectedRange.htmlText))
						{
							selectedRange = selectedRange.collapse(true);
							selectedRange = null;
							objReference = null;
						}
					}
				}				
*/
				
				// Moves the text range so that the start and end positions of the range encompass 
				// the text in the given element.
				if (objParentElement && objParentElement.nodeName == "A")
				{
				    if (imgItem)
				    {
    			        // Create a dummy textrange, from the current selection
				        var textRange = selection.createRange();
				        selectedRange = textRange;
				    }
				    else
				    {
					    selectedRange.moveToElementText(objParentElement);
					}
				}
														
				var parameters = new Array();
								
				if (objParentElement)	
				{
					parameters[0] = objParentElement;
				}
				else
				{
					parameters[0] = null;
				}
				
				// get the selected text
				if(selectedRange)
				{
					if(selectedRange.text)
					{
						parameters[1] = selectedRange.text;
					}
					else
					{
						parameters[1] = null;
					}
				}
				else
				{
					parameters[1] = null;
				}
			
				// get the current page id, add to parameter
				parameters[2] = intPageId;
								
				// get the bookmarks from the current page.
				var bookmarkArrayCollection = getBookMarkArray(editor.getHTML());
				var arrBookmarks = bookmarkArrayCollection[0];
				parameters[3] = arrBookmarks;

				// get the title of the current page if the page is new
				if (-1 == intPageId)
				{
				    if (document.forms[0].elements.Title)
				    {
					    parameters[4] = document.forms[0].elements.Title.value
					    if ("" == parameters[4])
					    {
						    parameters[4] = "(huidige pagina)";
					    }
					}
				}
				else
				{
					parameters[4] = null
				}
								
				var attr = "status:no;dialogWidth:450px;dialogHeight:550px;scroll:no;resizable:no;help:no";
				var page = strPath + "InsertYouTube.aspx";
				
				var LinkElements;
				LinkElements = showModalDialog(page, parameters, attr);
									
				if(LinkElements)
				{
					var strText;
					if (selectedRange && RangeType != "None" && (imgItem || selectedRange.htmlText != ""))
					{
					    selectedRange.execCommand('RemoveFormat');
						selectedRange.execCommand('unlink');
						
					    // Check if an image was selected.
                        if (imgItem && null != imgItem)
                        {
                            strText = imgItem.parentElement.innerHTML;
                        }
                        else if(selectedRange.text)
						{
							strText = LinkElements['title']
						}
						else
						{
							strText = selectedRange.htmlText;
						}
						selectedRange.execCommand('Delete');
					}
				    else
					{
						//no selection, first grab the title, if there is none, grab the href!
						if(LinkElements['title'] != "")
						{
							strText = LinkElements['title'];
						}
						else
						{
							strText = LinkElements['href'];
						}
					}
					var HTML = "<a href='" + LinkElements['href'] + "' target='" + LinkElements['target'] + "'>" + strText + "</a>";
					editor.focus();

                    editor.PasteHTML(HTML);
				}				
			}
			
			
			
				
//-->
