
// ---- [Public] insert an Image through the placeholder DOM
function WBC_setThinEditIEImage(strPhName, strURL, strHRef, strAltText) {
	var strMultiPurposeTag;
	if ( strAltText != "" ) {
		strMultiPurposeTag = "<img src=\"" + strURL + "\" border=\"0\" alt=\"" + strAltText + "\">";
	} else {
		strMultiPurposeTag = "<img src=\"" + strURL + "\" border=\"0\">";
	}				
	if ((strHRef != "") && (strHRef != IDS_HTTP_PROTOCOL)) {
		strMultiPurposeTag = "<a href=\"" + strHRef + "\">" + strMultiPurposeTag + "</a>";
	}
	
	document.all["NCPHRICH_" + strPhName].insertHtml(strMultiPurposeTag);
}

// ---- [Public] insert an Attachment through the placeholder DOM
function WBC_setThinEditIEAttachment(strPhName, strURL, strDispText) {
	var strMyDispText = strDispText;
	if (strMyDispText == "") {
		strMyDispText = strURL;
	}
	document.all["NCPHRICH_" + strPhName].insertHtml("<a href=\"" + strURL + "\">" + strMyDispText + "</a>");
}

// ---- [Public] insert a Video through the placeholder DOM
function WBC_setThinEditIEVideo(strPhName, strURL, bImgTag) {
	if (bImgTag) {
		document.all["NCPHRICH_" + strPhName].insertHtml("<img dynsrc=\"" + strURL + "\">");
	} else {
		document.all["NCPHRICH_" + strPhName].insertHtml("<embed src=\"" + strURL + "\" loop=\"false\" autoplay=\"false\" controller=\"true\">");
	}
}

// ---- [Public] insert a Table into the placeholder DOM
function WBC_setThinEditIETable(strPhName, strTableTag) {
	document.all["NCPHRICH_" + strPhName].insertHtml(strTableTag);
}

// ---- [Public] Sets the foreground/background color.
function WBC_setColor( pDOM, strForeBack ) 
{
  var color = showModalDialog(IDS_FRAMEWORK_NEW_VIRTUAL_PATH + "/Dialogs/Color/ColorSelect.aspx", "", 
  				"dialogWidth:450px;dialogHeight:320px;status:no;help:no");

  pDOM.execCommand( strForeBack, "", color );

  pDOM.body.focus();
  
}

// ---- [Public] Cleans up HTML in rich placeholder
function WBC_cleanHtml( pDOM ) {
  var fonts = pDOM.body.all.tags("FONT");
  var curr;
  for (var i = fonts.length - 1; i >= 0; i--) {
    curr = fonts[i];
    if (curr.style.backgroundColor == "#ffffff") curr.outerHTML = curr.innerHTML;
  }
}

// ---- [Private] function to support WBC_createLink()
function getEl(sTag,start) 
{
  while ((start!=null) && (start.tagName!=sTag)) start = start.parentElement;
  return start;
}

// ---- [Private] function to support WBC_createLink()
function getAncestorA( sel, rng )
{
	var elA;
	if (sel.type == "Control")
	{
		elA = getEl("A",rng.commonParentElement());
	}
	else 
	{
		elA = getEl("A",rng.parentElement());
	}
	
	return elA;
}

// ---- [Private] function to get the appropriate parent element
function getParentEl( sel, rng )
{
	var elParent;
	if (sel.type == "Control")
	{
		elParent = rng.commonParentElement();
	}
	else 
	{
		elParent = rng.parentElement();
	}
	
	return elParent;
}

// ---- [Private] function to support WBC_createLink()
function fixTrailingSpace(rng)
{
	var strText = rng.text;
	if (strText.length > 0)
	{
		if ( strText.charAt(strText.length-1) == " " )
		{
			rng.moveEnd("character", -1);
		}
	}

	return rng;
}

// ---- [Private] function to support WBC_createLink()
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");	
	}
}

// ---- [Public] In response to user clicking on Hyperlink button
function WBC_createLink( 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
	// since the full path injected is for deditor.asp
	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
	
	var strPath = IDS_FRAMEWORK_NEW_VIRTUAL_PATH + "/Dialogs/HLink/Hlink.aspx";
	var strDlgRet = window.showModalDialog( strPath, args, 
						"dialogWidth:450px;dialogHeight:265px;status:no;help:no" );

	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!");
		}
	}
}