// ********************************************************************
// Copyright (C) 2001 Microsoft Corporation. All rights reserved.
//
//	IMPORTANT.  Please read the legal.txt file, located in the 
//	"<CMS Install Directory>\Server\httpexec\WBC\Internals" 
//	directory, governing the use of this internal file."
// ********************************************************************

// ********************************************************************
// File:	/NR/System/WBC/Internals/PageTemplate/PlaceholderSupport/
//			DhtmlPlaceholder/HLink.asp
// Author:	Microsoft Corporation.
// Purpose:	JS to support Hyperlink dialog for IE4/5 running under Windows
// ********************************************************************


// This is to read the arguments passed by the caller when calling window.showModalDialog()
var args = window.dialogArguments;

						
// split args string "HRef;Title;Target;Name"
var aAttributes = args.split(";");
var g_OrigHRef = aAttributes[0];
var g_OrigTitle = aAttributes[1];
var g_OrigTarget = aAttributes[2];
var g_OrigName = aAttributes[3];

// ---- [Private] Sets the Target field to be Readonly or writable.
function setTargetReadOnly(bReadOnly)
{
	if (bReadOnly)
	{
		document.HLink_Form.Target.style.backgroundColor = "#c0c0c0";
		document.HLink_Form.Target.title = "";
		document.HLink_Form.Target.readOnly = true;
	}
	else
	{
		document.HLink_Form.Target.style.backgroundColor = "#ffffff";
		document.HLink_Form.Target.title = IDS_HLINK_CUSTOMWIN_HELPTEXT;
		document.HLink_Form.Target.readOnly = false;
	}
}

// ---- [Public] The event handler for the body onload event
function onBodyLoad()
{
	// init dialog values with passed in values
	document.HLink_Form.HRef.value = g_OrigHRef;
	document.HLink_Form.Title.value = g_OrigTitle;
	// Target field information
	setTargetReadOnly(false);
	if (g_OrigTarget == "")
	{
		setTargetReadOnly(true);
	}
	else
	{
		var i, max = document.HLink_Form.TargetSel.options.length;
		// default to last one: Custom
		document.HLink_Form.TargetSel.selectedIndex = max - 1;
		for (var i=1; i<max-1; i++)
		{
			if (document.HLink_Form.TargetSel.options[i].value == g_OrigTarget)
			{
				document.HLink_Form.TargetSel.selectedIndex = i;
				//  target is 1 of those standard values, so make Target field readonly
				setTargetReadOnly(true);
				break;
			}
		}
		document.HLink_Form.Target.value = g_OrigTarget;
	}
	
	document.HLink_Form.Name.value = g_OrigName;
}


// ---- [Public] This function retrieves the HLink field in the dialog
function WBC_GetHLink()
{
	return document.HLink_Form.HRef.value;
}

// ---- [Public] This function sets the HLink field in the dialog
function WBC_SetHLink(strURL)
{
	document.HLink_Form.HRef.value = strURL;
}

// ---- [Public] The event handler for the Browse button
function onClickBrowse()
{
	// Call InternalLinks.asp with "wbc_caller=IEModal" query string to indicate that
	// we called it with IE's showModalDialog() call instead of JavaScript window.open()
	// Also, need to set NRMODE to Unpublished in order for the tree to display offline objects
	var strURL = document.HLink_Form.HRef.value;
	var strDlgRet = window.showModalDialog(IDS_INTLINKS_PATH + "?NRMODE=Unpublished&wbc_caller=IEModal", 
												strURL, IDS_IEWIN_INTLINKS_FEATURES);
	if (strDlgRet != "Cancel")
	{
		document.HLink_Form.HRef.value = strDlgRet;
	}
}

// ---- [Public] The event handler for the Target droplist
function onClickTargetSel()
{
	var pTargetSel = document.HLink_Form.TargetSel;
	var strTarget = pTargetSel.options[pTargetSel.selectedIndex].value;
	document.HLink_Form.Target.value = strTarget;
	// reset readOnly attribute of Target field
	setTargetReadOnly(strTarget != "Custom");
}

// ---- [Public] The event handler for the OK button
function onClickOkay()
{
	var strHref = document.HLink_Form.HRef.value;
	var strName = document.HLink_Form.Name.value;
	var bHrefBlank = (strHref == "" || strHref == "http://");
	// if both Hyperlink Address field and Name field are blank, warn user
	if ( bHrefBlank && (strName == "") )
	{
		if ( !confirm(IDS_HLINK_WARN_EMPTY) )
			return;
	}
	// check if user entered a relative URL in the Hyperlink Address field
	if ( !bHrefBlank && (strHref.charAt(0) != '#') && (strHref.charAt(0) != '/') && 
				(strHref.substr(0,2) != '\\\\') && (strHref.indexOf(':') <= 0) )
	{
		// if it's not one of the 4 allowed cases, then it's likely to be a 
		// relative URL, which we do not support right now
		if ( !confirm(IDS_HLINK_WARN_RELLINK) )
			return;
	}

	// return string in format of "Href;Title;Target"
	var strRet = "";
	// only construct strRet if not both of Href and Name are blank
	if ( !bHrefBlank || (strName != "") )
	{
		strRet = strHref + ";";
		strRet = strRet + document.HLink_Form.Title.value + ";";
		strRet = strRet + document.HLink_Form.Target.value + ";";
		strRet = strRet + strName;
	}

	window.returnValue = strRet;
	window.close();
}

// ---- [Public] The event handler for the Cancel button
function onClickCancel()
{
	// retain special well-known value "Cancel"
	window.returnValue = "Cancel";
	window.close();
}