


function createLink(instance)
{
	var editorobj = getActiveField(instance);
	
	//var DoMe = editorobj.document.selection.createRange();
	//var theRange = editorobj.document.selection.createRange(); 
	var theRange = editorobj.document.selection.createRange(); 
	
	

	// Variable declaration
	var highlightedText = ""; 
	var linkText = ''; 
	var href_attribute = ''; 
	var tar_attribute = ''; 
	var elmSelectedImage; 
	var htmlSelectionControl = "Control"; 
	
	if (editorobj.document.selection.type == htmlSelectionControl) 
	{ 
	
		// actully we have an image. 
		elmSelectedImage = theRange.item(0); 
		highlightedText = elmSelectedImage.outerHTML; 

		//convert the ControlRange to a TextRange 
		theRange = editorobj.body.createTextRange(); 
		theRange.moveToElementText(elmSelectedImage); 
		theRange.select(); 
	} 
	else 
	{ 
		if (theRange.text != '') 
		{ 
			highlightedText = new String(theRange.htmlText);
			//highlightedText = "Her var et link";
			//alert("highlightedText before: " + highlightedText);
			
			if (highlightedText.indexOf("<P>")!=-1 || highlightedText.indexOf("<p>")!=-1 || highlightedText.indexOf("<li>")!=-1 || highlightedText.indexOf("<LI>")!=-1)
			{
			xpos1 = highlightedText.indexOf(">"); 
			xpos2 = highlightedText.lastIndexOf("<"); 
			highlightedText = highlightedText.substring(xpos1+1,xpos2); 
			}
		
	//	alert("highlightedText After: " + highlightedText);
		} 
		
		fullElement = theRange.parentElement().outerHTML; 
		//fullElement = "Her var et link";
		//alert("fullElement: " + fullElement);
	} 

			
	//in case we happen to select the link itself! 
	if (highlightedText.search(/^\<[A|a]/) != -1) 
	{ 
		fullElement = highlightedText; 
	} 

	//extract attributes from HTML 
	if (fullElement.search(/^\<[A|a]/) != -1) 
	{ 

		fullElement = fullElement.replace(/\"/g, ""); 
		fullElement = fullElement.replace(/\'/g, ""); 
		
		

		// here, we have an <a> tag. Now let's extract... 
		// 1. the href attribute 
		var href_value = fullElement.split(/href=/); 
		href_value2 = href_value[1].split(/\s|>/); 
		href_attribute = href_value2[0]; 

		// 2. the target attribute 
		if (fullElement.search(/target=/) != -1) 
		{ 
			var tar = fullElement.split(/target=/); 
			tar2 = tar[1].split(/\s|>/); 
			tar_attribute = tar2[0]; 
		} 

		// 3. the link text (more robust as includes all html code aswell) 
		pos1 = fullElement.indexOf(">"); 
		pos2 = fullElement.lastIndexOf("<"); 
		linkText = fullElement.substring(pos1+1,pos2); 
		
	} 

	var myValues = new Object(); 
	myValues.highlightedText = highlightedText; 
	myValues.tar_attribute = tar_attribute; 
	myValues.href_attribute = href_attribute; 
	myValues.linkText = linkText; 
				
	// Open Link chooser dialog
	var myText = showModalDialog("/dfdscommon/media/ArticlePlaceHolder/popups/custom2.html", myValues, "resizable: no; help: no; status: no; scroll: no; center: yes; edge: raised; unadorned: yes; dialogHeight: 190px; dialogWidth: 400px;"); 
				
	var st = new String(myText);
	
	if (st.length == 0)
	{ 
			theRange.execCommand("RemoveFormat");
			theRange.execCommand("Unlink");
			return;
	}
					
	if (linkText != '') 
	{ 
		if (myText) 
		{ 
			theRange.parentElement().outerHTML = ''; 
			insertLink(instance, unescape( myText) ); 
		} 
	} 
	else 
	{ 
		if (myText) 
		{ 
			insertLink(instance, unescape(myText) ); // this function ALWAYS puts in an absolute link 
		} 
	}
	
}



function insertLink(objname, str1,str2, reqSel) 
{



  var editor_obj = getActiveField(objname);    // editor object

  if (str1 == null) { str1 = ''; }
  if (str2 == null) { str2 = ''; }

 

  // error checking
  if (editor_obj == null) { return alert("Unable to insert HTML.  Invalid object name '" +objname+ "'."); }

  // editor_focus(editor_obj);

  var tagname = editor_obj.tagName.toLowerCase();
 
// alert(tagname);
 
  var sRange;

 // insertHTML for wysiwyg iframe
  
 
    editor_obj.focus();
    sRange  = editor_obj.document.selection.createRange();
    var sText   = sRange.text;
	
//	alert("Text: " + sText );

	var editdoc = editor_obj;
	
    sRange  = editdoc.document.selection.createRange();
    
    var sHtml   = sRange.htmlText;

    // check for control ranges
    if (sRange.length) 
    { 
		return alert("Unable to insert HTML.  Try highlighting content instead of selecting it."); 
	}

    // insert HTML
    var oldHandler = window.onerror;
    
   // window.onerror = function() { alert("Unable to insert HTML for current selection."); return true; } // partial table selections cause errors
    
    if (sHtml.length) 
    {                                 // if content selected
		if (str2) 
		{ 
			sRange.pasteHTML(str1 +sHtml+ str2) 
		} // surround
		else      
		{ 
//			alert("str1: " + str1);
			sRange.pasteHTML(str1); 
		}             // overwrite
    } 
    else 
    {                                            // if insertion point only
		if (reqSel) 
		{ 
			return alert("Unable to insert HTML.  You must select something first."); 
		}
		sRange.pasteHTML(str1 + str2);                    // insert strings
    }
    
    window.onerror = oldHandler;
    
     

  // move to end of new content
  
  sRange.select();        // re-select

}





function getText(obj)
{
	obj.focus();
	return obj.document.selection.createRange();
}




function bold(instance)
{
	var editorobj = getActiveField(instance);
	var DoMe = getText(editorobj);
	DoMe.execCommand("Bold");
	DoMe.select();
}


function italic(instance)
{
	var editorobj = getActiveField(instance);
	var DoMe = getText(editorobj);
	DoMe.execCommand("Italic");
	DoMe.select();
}




function underline(instance)
{
	var editorobj = getActiveField(instance);
	var DoMe = getText(editorobj);
	DoMe.execCommand("Underline");
	DoMe.select();
}


function numberedList(instance)
{
	var editorobj = getActiveField(instance);
	var DoMe = getText(editorobj);
	DoMe.execCommand("InsertUnorderedList");
	DoMe.select();
}

function orderedList(instance)
{
	var editorobj = getActiveField(instance);
	var DoMe = getText(editorobj);
	DoMe.execCommand("InsertOrderedList");
	DoMe.select();
}

function alignLeft(instance)
{
	var editorobj = getActiveField(instance);
	var DoMe = getText(editorobj);
	DoMe.execCommand("justifyleft", false, null);
	DoMe.select();
}

function alignCenter(instance)
{
	var editorobj = getActiveField(instance);
	var DoMe = getText(editorobj);
	DoMe.execCommand("justifycenter", false, null);
	DoMe.select();
}

function alignRight(instance)
{
	var editorobj = getActiveField(instance);
	var DoMe = getText(editorobj);
	DoMe.execCommand("justifyright", false, null);
	DoMe.select();
}



// Helper methods

function getActiveField(instance)
{
	var editorfield = getActive(instance);
	return eval("document.all['" + editorfield + "']");
}






// Paste event handlers

var lastKeycode;

function CtrlVCheck()
{
	
		
	var ev = this.parentWindow.event
	
	
	if(lastKeycode == 17)
	{
		if(ev.keyCode == 86)
		{
			
			//goStrip(); // Do nothing 
			lastKeycode = 17
		}
	}
	else
	{
		lastKeycode = ev.keyCode
	}
}

function CtrlVCheck2()
{

	var ev = this.parentWindow.event
	if(ev.keyCode == 17)
		lastKeycode = 17;
}


// Methods for cleaning HTML from paste operations

theText = "";
function startit(){
	if(theText.indexOf("style=\"") >= 0){
		window.status = "Stripping style-tags...";
		styleStripper(theText);
	}else if(theText.indexOf(" class=MsoNormal") >= 0 || theText.indexOf("<o:p>") >= 0 || theText.indexOf("<#P") >= 0 || theText.indexOf("</o:p>") >= 0 || theText.indexOf("<#/P>") >= 0 || theText.indexOf("<SPAN>") >= 0 || theText.indexOf("</SPAN>") >= 0){
		officeStripper(theText);
		window.status = "Stripping officetags...";
	}else if(theText.indexOf("<H1") >= 0 || theText.indexOf("<H2") >= 0 || theText.indexOf("<H3") >= 0 || theText.indexOf("<H4") >= 0 || theText.indexOf("<H5") >= 0 || theText.indexOf("<H6") >= 0 || theText.indexOf("<H7") >= 0 || theText.indexOf("</H") >= 0){
		HStripper(theText);
		window.status = "Stripping H-tags...";
	}else if(theText.indexOf("<?xml:") >= 0){
		XMLStripper(theText);
		window.status = "Stripping xml-tags...";
	}else if(theText.indexOf("<font") >= 0){
		FontStripper(theText);
		window.status = "Stripping font-tags...";
	}else if(theText.indexOf("<FONT") >= 0){
		FontStripperUC(theText);
		window.status = "Stripping FONT-tags...";
	}else{
		doneStripping();
	}
}

function HStripper(tekst){
	tekst = tekst.replace("<H1", "<strong");
	tekst = tekst.replace("<H2", "<strong");
	tekst = tekst.replace("<H3", "<strong");
	tekst = tekst.replace("<H4", "<strong");
	tekst = tekst.replace("<H5", "<strong");
	tekst = tekst.replace("<H6", "<strong");
	tekst = tekst.replace("<H7>", "<strong>");
	tekst = tekst.replace("</H1>", "</strong><br>");
	tekst = tekst.replace("</H2>", "</strong><br>");
	tekst = tekst.replace("</H3>", "</strong><br>");
	tekst = tekst.replace("</H4>", "</strong><br>");
	tekst = tekst.replace("</H5>", "</strong><br>");
	tekst = tekst.replace("</H6>", "</strong><br>");
	tekst = tekst.replace("</H7>", "</strong><br>");
	theText = tekst;
	setTimeout("startit()",5);
}

function FontStripper(tekst){
	newTekst = "";
	startPoint = tekst.indexOf("<font");
	if(startPoint > -1){
		endPoint = tekst.indexOf(">", startPoint+5);
		newTekst += tekst.substring(0, startPoint);
		newTekst += tekst.substring(endPoint+1, tekst.length); 
	}else{
		newTekst = tekst;
	}
	newTekst = newTekst.replace("</font>", "");
	theText = newTekst;
	setTimeout("startit()",5);
}

function FontStripperUC(tekst){
	newTekst = "";
	startPoint = tekst.indexOf("<FONT");
	if(startPoint > -1){
		endPoint = tekst.indexOf(">", startPoint+5);
		newTekst += tekst.substring(0, startPoint);
		newTekst += tekst.substring(endPoint+1, tekst.length); 
	}else{
		newTekst = tekst;
	}
	newTekst = newTekst.replace("</FONT>", "");
	theText = newTekst;
	setTimeout("startit()",5);
}

function XMLStripper(tekst){
	newTekst = "";
	startPoint = tekst.indexOf("<?xml:");
	if(startPoint > -1){
		endPoint = tekst.indexOf("/>", startPoint+6);
		newTekst += tekst.substring(0, startPoint);
		newTekst += tekst.substring(endPoint+2, tekst.length); 
		theText = newTekst;
	}else{
		theText = tekst;
	}
	setTimeout("startit()",5);
}

function officeStripper(tekst){
	tekst = tekst.replace(" class=MsoNormalTable", " ");
	tekst = tekst.replace(" class=MsoNormal", " ");
	tekst = tekst.replace("<o:p>", "");
	tekst = tekst.replace("</o:p>", "");

	newTekst = "";
	startPoint = tekst.indexOf("<SPAN");
	if(startPoint > -1){
		endPoint = tekst.indexOf(">", startPoint+5);
		newTekst += tekst.substring(0, startPoint);
		newTekst += tekst.substring(endPoint+1, tekst.length); 
		tekst = newTekst;
	}

	tekst = tekst.replace("</SPAN>", "");
	//tekst = tekst.replace("<P", "<div");
	//tekst = tekst.replace("</P>", "</div>");

	theText = tekst;
	setTimeout("startit()",5);
}

function styleStripper(tekst){
	newTekst = "";
	startPoint = tekst.indexOf("style=\"");
	if(startPoint > -1){
		endPoint = tekst.indexOf("\"", startPoint+7);
		newTekst += tekst.substring(0, startPoint-1);
		newTekst += tekst.substring(endPoint+1, tekst.length); 
	}else{
		newTekst = tekst;
	}
	theText = newTekst;
	setTimeout("startit()",5);
}


function goStrip(){
	goStrip('')
}

imgDirPublic = ""
function goStrip(imgDir){
	window.status = "Stripping...";
	
	imgDirPublic = imgDir;
	if(document.all.xmlbody.style.display != ""){
		theText = editor.document.body.innerHTML;
		startit()
	}else{
		theText = document.all.xmlbody.value;
		startit()
	}
}

function doneStripping(){
	if(document.all.xmlbody.style.display != ""){
		editor.document.body.innerHTML = theText;
	}else{
		document.all.xmlbody.value = theText;
	}
	window.status = "";
}