/************************************************
*
* shared.js
*
* General (Java) Scripts 
* for use with MSCMS 
*
* Snohomish County
* Department of Information Systems
* Web Services Team
*
* @created 2003-09-17
* @version 1.00
* @author Michael VonWald
*
************************************************/
/************************************************
*
* showHideKids()
*
* Hides or shows divs in tree view
*
* @param elemid (string)
* @param imageid (string)
* @return void
*
************************************************/
function showHideKids(elemid,imageid) {

    var parentDiv = document.getElementById(elemid);
    var img = document.getElementById(imageid);

    // swap images
    if(img.src.indexOf('collapsible')>0) {
        img.src = img.src.replace('collapsible','expandable');
    } else {
        img.src = img.src.replace('expandable','collapsible');
    }

    // loop through the children
    if(parentDiv.childNodes.length > 0)
    {   
        var divKids = parentDiv.children.tags('div');
        
        for(var i=0; i<divKids.length; i++)
        {  
            var currentCssClass = divKids[i].className;
                        
            if(currentCssClass != undefined)
            {                
                if(currentCssClass.indexOf('-off') > 0) {
                     divKids[i].className = currentCssClass.replace('-off','-on');
                } else {
                     divKids[i].className = currentCssClass.replace('-on','-off');
                }
            }         
        }
    }
}

/************************************************
*
* onFormSubmit()
* for template submit to check Search box
*
************************************************/
function onFormSubmit()
{
    var returnVal = true;
    var searchBox = document.getElementById("txtSearch");
    var searchTerm = searchBox.value;
    if(searchTerm.length > 0 && searchTerm != 'Enter Keywords')
    {
        gotoGoogleAppliance(searchTerm);
        returnVal = false;  
    } 
    
    return returnVal;
}

/************************************************
*
* gotoGoogleAppliance(searchTerm)
*
************************************************/
function gotoGoogleAppliance(searchTerm)
{
    /*var googleUrl = 'http://search.co.snohomish.wa.us/search?btnG=Snoco+Search&site=Snoco&client=Snoco&proxystylesheet=Snoco&output=xml_no_dtd&q=';*/
    var googleUrl = 'http://search.co.snohomish.wa.us/search?site=Snoco&client=Snoco&proxystylesheet=Snoco&output=xml_no_dtd&q=';
    var encodedTerm = searchTerm.replace('&'," and ");
    encodedTerm = encodeURI(encodedTerm);
    googleUrl += encodedTerm;    
    window.location = googleUrl;        
}

/************************************************
*
* googgleButtonClick(elemId)
*
************************************************/
function googleButtonClick(elemId)
{
    var searchBox = document.getElementById(elemId);
    var searchTerm = searchBox.value;
    if(searchTerm.length > 0 && searchTerm != 'Enter Keywords')
    {
        gotoGoogleAppliance(searchBox.value);
    }
}


/************************************************
*
* goToQuickLink()
*
* Navigate to a quicklink
*
* @param elemId
* @return void
*
************************************************/
function goToQuickLink(elemId) {
    var elemQuickLink = document.getElementById(elemId);
    var selectedItem = elemQuickLink.selectedIndex;
        
    var pagePath = elemQuickLink.options[selectedItem].value;
    window.location = pagePath;
}

/************************************************
*
* goBackOnePage()
*
* @param void
* @return void
*
************************************************/
function goBackOnePage() {
    window.history.back(1);
}

/************************************************
*
* goToParentDir()
*
* Navigate to homepage
*
* @param void
* @return void
*
************************************************/
function goToParentDir() {
    var currentPath = window.location.href;
    var pagePath = currentPath.substring(0,currentPath.lastIndexOf('/')+1);
    window.location = pagePath;
}

/************************************************
*
* printPagePopOpen()
*
* Creates a new window 440x660
*
* @param (string) url of new window
* @return void
*
************************************************/
function printPagePopOpen(targetUrl) {
    // open in new window
    window.open(targetUrl,'SnohomishCounty','height=440,width=660,toolbar=yes,status=no,location=no,menubar=yes,resizable=yes,scrollbars=yes');

}


/************************************************
*
* popOpen()
*
* Creates a new window 440x660
*
* @param (string) url of new window
* @return void
*
************************************************/
function popOpen(targetUrl) {
   
    // open in new window
    //
    window.open(targetUrl,'Information','height=440,width=660,toolbar=no,status=yes,location=no,menubar=no,resizable=yes,scrollbars=yes');

}

/************************************************
*
* miniPopOpen()
*
* Creates a new window 400x500
*
* @param (string) url of new window
* @return void
*
************************************************/
function miniPopOpen(targetUrl) {

    // open in new window
    window.open(targetUrl,'Information','height=400,width=500,toolbar=no,status=no,location=no,menubar=no,resizable=yes,scrollbars=no');

}

/************************************************
*
* resizeForAuthoring()
*
* Resizes window for Authoring mode
*
* @param void
* @return void
*
************************************************/
function resizeForAuthoring() {
    window.resizeTo(800,600);	
}


/************************************************
*
* mOver()
*
* MouseOver function for changing classnames
* facilitates different look for some elements
* of a Snohomish County web page or web application
*
* @param (string) id attribute value of element
* @return void
*
************************************************/
function mOver(elemId) {

    // note:
    // all mouseover classnames should be the
    // standard classname with the addition
    // of '-over' on the end
    //
    var OVER = "-over";

    // get document element
    var objElem = document.getElementById(elemId);

    // add '-over' to className
    objElem.className = objElem.className + OVER;
}


/************************************************
*
* mOut()
*
* MouseOut function for changing classnames
* facilitates different look for some elements
* of a Snohomish County web page or web application
*
* @param (string) id attribute value of element
* @return void
*
************************************************/
function mOut(elemId) {

    // note:
    // all mouseover classnames should be the
    // standard classname with the addition
    // of '-over' on the end
    //
    // create regular expression with this
    var regEx = /-over/;

    // get document element
    var objElem = document.getElementById(elemId);

    // replace '-over' with nothing
    objElem.className = objElem.className.replace(regEx,'');
}


/** Sample function */
function CreateSnocoBold(objDom) {
    var selectedText = objDom.selection;
    var rngText = selectedText.createRange();
    var returnHTML = "";
    var elemParent = rngText.parentElement();
    
    if(elemParent != null)
    {
        if(elemParent.tagName.toLowerCase() == "strong")
        {
            returnHTML = elemParent.innerText;
            elemParent.outerHTML = returnHTML;
        }
        else
        {
            returnHTML = "<strong>" + rngText.text + "</strong>";
            rngText.pasteHTML(returnHTML);
        }
    }        
}


// function was heavily borrowed from MS
// also makes use of specific associated 
// MSCMS core scripting
// from CMS platform files 
function createSnocoDocumentLink(pDOM) {
	
	// IE DOM fully-qualifies any href with relative path or #bookmark, 
	// we need to strip out the full path, which screws up relative paths
	//WBC_cleanHtml( pDOM );
  
	var elA;
	var sel = pDOM.selection;
	
	// check against case that there is no highlighted selection
	if (sel.type=="None")
	{
		alert("There is no selected text or object for hyperlinking.");
		return;
	}
	
	var rng = sel.createRange();
	
	// componseate for IE selecting trailing space as part of the range
	if (sel.type == "Text")
		rng = fixTrailingSpace(rng);

	elA = getAncestorA( sel, rng );
	
	// construct arguments to be passed to the dialog, in the format 
	// of "Href;Title;Target;Name"
	var strHref = elA ? elA.href : "http:\/\/";
	var strTitle = elA ? (elA.title ? elA.title : "") : "";
	var strTarget = elA ? (elA.target ? elA.target : "" ) : "";
	var strName = elA ? (elA.name ? elA.name : "" ) : "";
	
	var args = strHref + ";" + strTitle + ";" + strTarget + ";" + strName;

	// show hyperlink dialog
	// modified to show selection dialog from cmssupport site
	//  returned string s/b = "HRef;Title;Target;Name"
	//
	var strPath = "http://websrv15m/cmssupport/selectSnocoDocument.aspx";
	var strDlgRet = window.showModalDialog( strPath, args, "dialogWidth:600px;dialogHeight:600px;scroll:yes;status:no;help:no" );

    //window.alert(strDlgRet);

	pDOM.body.focus();
						
	if ( typeof(strDlgRet) == "undefined" )
		return;
		
	if (strDlgRet == "Cancel")
		return;
		
	// before setting new value, remove all old hyperlinks within the range
	rng.execCommand("Unlink");

	// create new <A> only if strDlgRet is not a blank
	if ( strDlgRet != "")
	{
		// split returned string "HRef;Title;Target;Name"
		var aAttributes = strDlgRet.split(";");
		strHref = aAttributes[0];
		strTitle = aAttributes[1];
		strTarget = aAttributes[2];
		strName = aAttributes[3];

		// get IE to set the hyperlink through execCommand
		if ( rng.execCommand("CreateLink", "", strHref) )
		{
			// apply attributes for <a> tag
			elA = getAncestorA( sel, rng );
			if (elA)
			{
				// element <a> found in ancestors chain,
				// next step is to modify it
				modifyAnchor(elA, strTitle, strTarget, strName);
			}
			else
			{
				// element <a> not found in ancestors chain, try decendents
				// of the parent element.

				// From rng, look for all parent's decendents with <a> tags with 
				// href attributes equal to strHref
				colA = getParentEl( sel, rng ).all.tags("A");
				for (var i = colA.length - 1; i >= 0; i--)
				{
					modifyAnchor(colA[i], strTitle, strTarget, strName);
				}
			}

			// for some unknown reason, need to refresh placeholder 
			// for images to show up properly
			if (sel.type == "Control")
			{
				pDOM.body.innerHTML = pDOM.body.innerHTML;
				// previous line resulting in entire content 
				// selected, need to compensate by emptying selection
				pDOM.selection.empty();
			}
		}
		else
		{
			alert("Failed to insert hyperlink!");
		}
	}
}

/************************************************
*
* creates a link to a CMS posting 
* created from the popup template
* link opens in new window via 
* DHTML window.open
*
************************************************/
function createSnocoPopupLink(pDOM) {
	
	var POPUP_DIALOG_VPATH = "/misc/selectSnocoPopup.aspx";
	
	// IE DOM fully-qualifies any href with relative path or #bookmark, 
	// we need to strip out the full path, which screws up relative paths
	//WBC_cleanHtml( pDOM );
  
	var elA;
	var sel = pDOM.selection;
	
	// check against case that there is no highlighted selection
	if (sel.type=="None")
	{
		alert("There is no selected text or object for hyperlinking.");
		return;
	}
	
	var rng = sel.createRange();
	
	// componseate for IE selecting trailing space as part of the range
	if (sel.type == "Text")
		rng = fixTrailingSpace(rng);

	elA = getAncestorA( sel, rng );
	
	// construct arguments to be passed to the dialog, in the format 
	// of "Href;Title;Target;Name"
	var strHref = elA ? elA.href : "http:\/\/";
	var strTitle = elA ? (elA.title ? elA.title : "") : "";
	var strTarget = elA ? (elA.target ? elA.target : "" ) : "";
	var strName = elA ? (elA.name ? elA.name : "" ) : "";
	
	var args = strHref + ";" + strTitle + ";" + strTarget + ";" + strName;

	// show hyperlink dialog
	// modified to show selection dialog from cmssupport site
	//  returned string s/b = "HRef;Title;Target;Name"
	//
	var strPath = window.location.protocol;
	
	strPath += "//";
	strPath += window.location.hostname;
	strPath += POPUP_DIALOG_VPATH;	
	
	
	var strDlgRet = window.showModalDialog(strPath, args, "dialogWidth:450px;dialogHeight:600px;scroll:yes;status:no;help:no" );

    //window.alert(strDlgRet);

	pDOM.body.focus();
						
	if ( typeof(strDlgRet) == "undefined" )
		return;
		
	if (strDlgRet == "Cancel")
		return;
		
	// before setting new value, remove all old hyperlinks within the range
	rng.execCommand("Unlink");

	// create new <A> only if strDlgRet is not a blank
	if ( strDlgRet != "")
	{
		// split returned string "HRef;Title;Target;Name"
		var aAttributes = strDlgRet.split(";");
		strHref = aAttributes[0];
		strTitle = aAttributes[1];
		strTarget = aAttributes[2];
		strName = aAttributes[3];

		// get IE to set the hyperlink through execCommand
		if ( rng.execCommand("CreateLink", "", strHref) )
		{
			// apply attributes for <a> tag
			elA = getAncestorA( sel, rng );
			if (elA)
			{
				// element <a> found in ancestors chain,
				// next step is to modify it
				modifySnocoPopupAnchor(elA, strTitle, strTarget, strName);
			}
			else
			{
				// element <a> not found in ancestors chain, try decendents
				// of the parent element.

				// From rng, look for all parent's decendents with <a> tags with 
				// href attributes equal to strHref
				colA = getParentEl( sel, rng ).all.tags("A");
				for (var i = colA.length - 1; i >= 0; i--)
				{
					modifySnocoPopupAnchor(colA[i], strTitle, strTarget, strName);
				}
			}

			// for some unknown reason, need to refresh placeholder 
			// for images to show up properly
			if (sel.type == "Control")
			{
				pDOM.body.innerHTML = pDOM.body.innerHTML;
				// previous line resulting in entire content 
				// selected, need to compensate by emptying selection
				pDOM.selection.empty();
			}
		}
		else
		{
			alert("Failed to insert hyperlink!");
		}
	}
}

// function was heavily borrowed from MS
// also makes use of specific associated 
// MSCMS core scripting
// from CMS platform files 
function createSnocoDocumentLink(pDOM) {
	
	// IE DOM fully-qualifies any href with relative path or #bookmark, 
	// we need to strip out the full path, which screws up relative paths
	//WBC_cleanHtml( pDOM );
  
	var elA;
	var sel = pDOM.selection;
	
	// check against case that there is no highlighted selection
	if (sel.type=="None")
	{
		alert("There is no selected text or object for hyperlinking.");
		return;
	}
	
	var rng = sel.createRange();
	
	// componseate for IE selecting trailing space as part of the range
	if (sel.type == "Text")
		rng = fixTrailingSpace(rng);

	elA = getAncestorA( sel, rng );
	
	// construct arguments to be passed to the dialog, in the format 
	// of "Href;Title;Target;Name"
	var strHref = elA ? elA.href : "http:\/\/";
	var strTitle = elA ? (elA.title ? elA.title : "") : "";
	var strTarget = elA ? (elA.target ? elA.target : "" ) : "";
	var strName = elA ? (elA.name ? elA.name : "" ) : "";
	
	var args = strHref + ";" + strTitle + ";" + strTarget + ";" + strName;

	// show hyperlink dialog
	// modified to show selection dialog from cmssupport site
	//  returned string s/b = "HRef;Title;Target;Name"
	//
	var strPath = "http://websrv15m/cmssupport/selectSnocoDocument.aspx";
	var strDlgRet = window.showModalDialog( strPath, args, "dialogWidth:600px;dialogHeight:600px;scroll:yes;status:no;help:no" );

    //window.alert(strDlgRet);

	pDOM.body.focus();
						
	if ( typeof(strDlgRet) == "undefined" )
		return;
		
	if (strDlgRet == "Cancel")
		return;
		
	// before setting new value, remove all old hyperlinks within the range
	rng.execCommand("Unlink");

	// create new <A> only if strDlgRet is not a blank
	if ( strDlgRet != "")
	{
		// split returned string "HRef;Title;Target;Name"
		var aAttributes = strDlgRet.split(";");
		strHref = aAttributes[0];
		strTitle = aAttributes[1];
		strTarget = aAttributes[2];
		strName = aAttributes[3];

		// get IE to set the hyperlink through execCommand
		if ( rng.execCommand("CreateLink", "", strHref) )
		{
			// apply attributes for <a> tag
			elA = getAncestorA( sel, rng );
			if (elA)
			{
				// element <a> found in ancestors chain,
				// next step is to modify it
				modifyAnchor(elA, strTitle, strTarget, strName);
			}
			else
			{
				// element <a> not found in ancestors chain, try decendents
				// of the parent element.

				// From rng, look for all parent's decendents with <a> tags with 
				// href attributes equal to strHref
				colA = getParentEl( sel, rng ).all.tags("A");
				for (var i = colA.length - 1; i >= 0; i--)
				{
					modifyAnchor(colA[i], strTitle, strTarget, strName);
				}
			}

			// for some unknown reason, need to refresh placeholder 
			// for images to show up properly
			if (sel.type == "Control")
			{
				pDOM.body.innerHTML = pDOM.body.innerHTML;
				// previous line resulting in entire content 
				// selected, need to compensate by emptying selection
				pDOM.selection.empty();
			}
		}
		else
		{
			alert("Failed to insert hyperlink!");
		}
	}
}


	
/************************************************
*
* modifies existing link attributes
* replaces Microsoft script modifyAnchor
* for this implementation
*
************************************************/
function modifySnocoPopupAnchor(elA, strTitle, strTarget, strName)
{
	// apply attributes for the <a> tag
	if (strTitle == "") {
		elA.removeAttribute("title");
	} else {
		elA.title = '"' + strTitle + '"';
    }

	if (strTarget == "") {
		elA.removeAttribute("target");
	} else {
		elA.target = '"' + strTarget + '"';
    }

	if (strName == "")
	{
		elA.removeAttribute("name");
		elA.removeAttribute("id");
	}
	else
	{
		elA.name = '"' + strName + '"';
		elA.id = '"' + strName + '"';
	}
	
	// lastly, remove HREF attribute if it is empty
	if ( elA.href == "" || elA.href == "http://" )
	{
		elA.removeAttribute("href");	
	}
	
	// add onclick to anchor tag
	// for better compliance
	elA.setAttribute("onclick", "printPagePopOpen('" + elA.href + "');return false;");
		
}

function modifyAnchor(elA, strTitle, strTarget, strName)
{
	// apply attributes for the <a> tag
	if (strTitle == "")
		elA.removeAttribute("title");
	else
		elA.title = strTitle;

	if (strTarget == "")
		elA.removeAttribute("target");
	else
		elA.target = strTarget;

	if (strName == "")
	{
		elA.removeAttribute("name");
		elA.removeAttribute("id");
	}
	else
	{
		elA.name = strName;
		elA.id = strName;
	}
	
	// lastly, remove HREF attribute if it is empty
	if ( elA.href == "" || elA.href == "http://" )
	{
		elA.removeAttribute("href");	
	}
}


/************************************************
*
* All functions below are specialized for use
* with Telerik(r) RAD Editor MSCMS control
*
* Snohomish County
* Department of Information Systems
* Web Services Team
*
* @created 2004-07-26   
* @version 1.0.01
* @author Web Services Team - sismjv
*
************************************************/

function SnocoBold(radEditorContentArea, radEditorToolImage) {
    var returnHTML = "";
    var selSelection = radEditorContentArea.document.selection;
	
	if(selSelection.type != "None")
	{
	    var rngSelection = selSelection.createRange();
	    if(selSelection.type == "Text")
	    {
	        rngSelection = fixTrailingSpaceRAD(rngSelection);
	    } 
	    
	     var elemParent = rngSelection.parentElement();
        
        if(elemParent != null)
        {
            if(elemParent.tagName.toLowerCase() == "strong")
            {
                returnHTML = elemParent.innerText;
                elemParent.outerHTML = returnHTML;
            }
            else
            {
                returnHTML = "<strong>" + rngSelection.text + "</strong>";
                rngSelection.pasteHTML(returnHTML);
            }
        }        
	
	} 
	else
	{
	    // show error message
	    window.alert("There is no selected text!");
	    return;
	}    
   
}

function LinkToSnocoDocument(radEditorContentArea, radEditorToolImage) {
	
	// get selection
	var selSelection = radEditorContentArea.document.selection;
	if(selSelection.type != "None")
	{
	    var rngSelection = selSelection.createRange();
	    if(selSelection.type == "Text")
	    {
	        rngSelection = fixTrailingSpaceRAD(rngSelection);
	    }    
	        
	    var elA = getAncestorAnchorRAD( selSelection, rngSelection);
	    
	    // construct arguments to be passed to the dialog, in the format 
	    // of "Href;Title;Target;Name"
	    var strHref = elA ? elA.href : "http:\/\/";
	    var strTitle = elA ? (elA.title ? elA.title : "") : "";
	    var strTarget = elA ? (elA.target ? elA.target : "" ) : "";
	    var strName = elA ? (elA.name ? elA.name : "" ) : "";
        
	    var args = strHref + ";" + strTitle + ";" + strTarget + ";" + strName;

	    // show hyperlink dialog
	    //  modified to show selection dialog from cmssupport site
	    //  returned string s/b = "HRef;Title;Target;Name"
	    //
	    var selectSnocoDocUrl = "http://websrv15m/cmssupport/selectSnocoDocument.aspx";
	    var dialogReturn = window.showModalDialog( selectSnocoDocUrl, args, "dialogWidth:600px;dialogHeight:600px;scroll:yes;status:no;help:no" );
            
        if( typeof(dialogReturn) == "undefined" )
        {
		    return;
        }	
        
	    if (dialogReturn == "Cancel")
		{
		    return;  
	    }
	
	    rngSelection.execCommand("Unlink");
	    
	    if(dialogReturn != "")
	    {
	        // get results from dialog
	        var aAttributes = dialogReturn.split(";");
	        
	        strHref = aAttributes[0];
		    strTitle = aAttributes[1];
		    strTarget = aAttributes[2];
		    strName = aAttributes[3];

		    // get IE to set the hyperlink through execCommand
		    if(rngSelection.execCommand("CreateLink", "", strHref))
		    {
		        // apply attributes for <a> tag
			    elA = getAncestorAnchorRAD( selSelection, rngSelection );
			    if (elA)
			    {
				    // element <a> found in ancestors chain,
				    // next step is to modify it
				    modifyAnchorRAD(elA, strTitle, strTarget, strName);
			    }
			    else
			    {
				    // element <a> not found in ancestors chain, try decendents
				    // of the parent element.

				    // From rng, look for all parent's decendents with <a> tags with 
				    // href attributes equal to strHref
				    colA = getParentElementRAD(selSelection, rngSelection).all.tags("A");
    				
				    for (var i = colA.length - 1; i >= 0; i--)
				    {
					    modifyAnchorRAD(colA[i], strTitle, strTarget, strName);
				    }
			    }

			    // for some unknown reason, need to refresh placeholder 
			    // for images to show up properly
			    if (selSelection.type == "Control")
			    {
				    rngSelection.innerHTML = rngSelection.innerHTML;
				    // previous line resulting in entire content 
				    // selected, need to compensate by emptying selection
				    selSelection.empty();
			    }
			}
		}
		else
		{
			alert("Failed to insert hyperlink!");
		}	    
	    	    
	}
	else
	{
	    window.alert("There is no selected text or object for hyperlinking!");
	    return;
	}	
}


function LinkToSnocoPopup(radEditorContentArea, radEditorToolImage)
{    
    var POPUP_DIALOG_VPATH = "/misc/selectSnocoPopup.aspx";
	var selSelection = radEditorContentArea.document.selection;
	
	if(selSelection.type != "None")
	{
	    var rngSelection = selSelection.createRange();
	    if(selSelection.type == "Text")
	    {
	        rngSelection = fixTrailingSpaceRAD(rngSelection);
	    }    
	        
	    var elA = getAncestorAnchorRAD( selSelection, rngSelection);
	
	    // construct arguments to be passed to the dialog, in the format 
	    // of "Href;Title;Target;Name"
	    var strHref = elA ? elA.href : "http:\/\/";
	    var strTitle = elA ? (elA.title ? elA.title : "") : "";
	    var strTarget = elA ? (elA.target ? elA.target : "" ) : "";
	    var strName = elA ? (elA.name ? elA.name : "" ) : "";
    	
	    var args = strHref + ";" + strTitle + ";" + strTarget + ";" + strName;

	    // show hyperlink dialog
	    // modified to show selection dialog from cmssupport site
	    //  returned string s/b = "HRef;Title;Target;Name"
	    //
	    var strPath = window.location.protocol;
    	
	    strPath += "//";
	    strPath += window.location.hostname;
	    strPath += POPUP_DIALOG_VPATH;	
		
	    var strDlgRet = window.showModalDialog(strPath, args, "dialogWidth:450px;dialogHeight:600px;scroll:yes;status:no;help:no" );
       						
	    if( typeof(strDlgRet) == "undefined" )
	    {
		    return;
    	}	
	    if(strDlgRet == "Cancel")
	    {
		    return;
		}
		
	    // before setting new value, remove all old hyperlinks within the range
	    rngSelection.execCommand("Unlink");

	    // create new <A> only if strDlgRet is not a blank
	    if ( strDlgRet != "")
	    {
		    // split returned string "HRef;Title;Target;Name"
		    var aAttributes = strDlgRet.split(";");
		    strHref = aAttributes[0];
		    strTitle = aAttributes[1];
		    strTarget = aAttributes[2];
		    strName = aAttributes[3];

		    // get IE to set the hyperlink through execCommand
		    if(rngSelection.execCommand("CreateLink", "", strHref))
		    {
			    // apply attributes for <a> tag
			    elA = getAncestorAnchorRAD( selSelection, rngSelection );
			    if (elA)
			    {
				    // element <a> found in ancestors chain,
				    // next step is to modify it
				    modifyAnchorRAD(elA, strTitle, strTarget, strName);
			    }
			    else
			    {
				    // element <a> not found in ancestors chain, try decendents
				    // of the parent element.

				     // From rng, look for all parent's decendents with <a> tags with 
				    // href attributes equal to strHref
				    colA = getParentElementRAD(selSelection, rngSelection).all.tags("A");
    				
				    for (var i = colA.length - 1; i >= 0; i--)
				    {
					    modifyAnchorRAD(colA[i], strTitle, strTarget, strName);
				    }
			    }

			    // for some unknown reason, need to refresh placeholder 
			    // for images to show up properly
			    if (selSelection.type == "Control")
			    {
				    rngSelection.innerHTML = rngSelection.innerHTML;
				    // previous line resulting in entire content 
				    // selected, need to compensate by emptying selection
				    selSelection.empty();
			    }			    
	        }  
		}
		else
		{
			alert("Failed to insert hyperlink!");
		}
	}
	else
	{
	    window.alert("There is no selected text or object for hyperlinking!");
	    return;
	}	
}

/************************************************
*
* Modifed for use with Telerik(r) RAD Editor
* (MSCMS Version)
*
* 2004-07-16
* 
************************************************/
function getElementRAD(sTag,start) 
{
  while ((start!=null) && (start.tagName!=sTag)) start = start.parentElement;
  return start;
}

/************************************************
*
* Modifed for use with Telerik(r) RAD Editor
* (MSCMS Version)
*
* 2004-07-16
* 
************************************************/
function getAncestorAnchorRAD( sel, rng )
{
	var elA;
	if (sel.type == "Control")
	{
		elA = getElementRAD("A",rng.commonParentElement());
	}
	else 
	{
		elA = getElementRAD("A",rng.parentElement());
	}
	
	return elA;
}

/************************************************
*
* Modifed for use with Telerik(r) RAD Editor
* (MSCMS Version)
*
* 2004-07-16
* 
************************************************/
function getParentElementRAD( sel, rng )
{
	var elParent;
	if (sel.type == "Control")
	{
		elParent = rng.commonParentElement();
	}
	else 
	{
		elParent = rng.parentElement();
	}
	
	return elParent;
}

/************************************************
*
* Modifed for use with Telerik(r) RAD Editor
* (MSCMS Version)
*
* 2004-07-16
* 
************************************************/
function fixTrailingSpaceRAD(rng)
{
	var strText = rng.text;
	if (strText.length > 0)
	{
		if ( strText.charAt(strText.length-1) == " " )
		{
			rng.moveEnd("character", -1);
		}
	}
	return rng;
}

/************************************************
*
* Modifed for use with Telerik(r) RAD Editor
* (MSCMS Version)
*
* 2004-07-16
* 
************************************************/
function modifyAnchorRAD(elA, strTitle, strTarget, strName)
{
	// apply attributes for the <a> tag
	if (strTitle == "")
		elA.removeAttribute("title");
	else
		elA.title = strTitle;

	if (strTarget == "")
		elA.removeAttribute("target");
	else
		elA.target = strTarget;

	if (strName == "")
	{
		elA.removeAttribute("name");
		elA.removeAttribute("id");
	}
	else
	{
		elA.name = strName;
		elA.id = strName;
	}
	
	// lastly, remove HREF attribute if it is empty
	if ( elA.href == "" || elA.href == "http://" )
	{
		elA.removeAttribute("href");	
	}
}
