// ************************************************************************
// File:	/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/ResourceBrowser/Authoring/ResourceBrowserClient.js
// Author:	Microsoft Corporation.
// Purpose:	Provide client-side JavaScript Functions for use in the 
//			Resource Browser pages.
// Note:	The file that references ResourceBrowserClient.js must also
//			reference CommonClient.js
// ************************************************************************

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

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

// ---- [Public] This function opens the InternalLinks dialog
function WBC_onBrowse()
{
	WBC_openWindowOnFocus(IDS_INTLINKS_PATH + "?NRMODE=Unpublished", "InternalLinks", IDS_WIN_INTLINKS_FEATURES);
}

// ---- [Public] This function validates local attachment and its properties
function WBC_validateLocalAttachment(bAttachIcon) {
	if (!bAttachIcon) {
		if (WBC_verifyDispText(document.NC_LocalForm.NC_DispText.value) == null) {
			alert(IDS_INVALID_DISP_TEXT);
			return false;
		}
	}
	return WBC_validateLocalPath(IDS_RESTYPE_ATTACHMENT)
}




// ---- [Public] This function loads the image properties from the authoring page.
function WBC_loadImageProperties(strPhName) {
	var pFuncWin;
	if (document.NC_LocalForm.NC_HRef != null)
	{
		pFuncWin = WBC_getFuncWin("WBC_getHRef", top.opener);
		if (pFuncWin == null) {
			alert(IDS_FUNCTION_NOT_FOUND);
			return;
		}
		document.NC_LocalForm.NC_HRef.value = pFuncWin.WBC_getHRef(strPhName);
	}
	
	if (document.NC_LocalForm.NC_AltText != null)
	{
		pFuncWin = WBC_getFuncWin("WBC_getAltText", top.opener);
		if (pFuncWin == null) {
			alert(IDS_FUNCTION_NOT_FOUND);
			return;
		}
		document.NC_LocalForm.NC_AltText.value = pFuncWin.WBC_getAltText(strPhName);
	}
}


// ---- [Public] This function loads the attachment properties from the authoring page.
function WBC_loadAttachmentProperties(strPhName) {
	
	var pFuncWin = WBC_getFuncWin("WBC_getDispText", top.opener);
	if (pFuncWin == null) {
		alert(IDS_FUNCTION_NOT_FOUND);
		return;
	}
	document.NC_LocalForm.NC_DispText.value = pFuncWin.WBC_getDispText(strPhName);
}

// ---- [Public] This function calls the WBC_setSingleImage or WBC_setMultiPurposeImage 
//				function to add an image to a placeholder.
function WBC_procsImage(strPhName, strPhType, strURL) {
	var strHRef = "";
	var strAltText = "";
	// retrieve HREF and ALT values, if they exist on the form
	// they may not exist because the Image doesn't allow Hyperlinks thus
	// there is no input fields for entering those values
	if (document.NC_LocalForm != null)
	{
		if (document.NC_LocalForm.NC_HRef != null)
			strHRef = document.NC_LocalForm.NC_HRef.value;
		if (document.NC_LocalForm.NC_AltText != null)
			strAltText = WBC_encodeContent(document.NC_LocalForm.NC_AltText.value);
	}

	if (strPhType == IDS_PHTYPE_SINGLEIMAGE) {
		var pFuncWin = WBC_getFuncWin("WBC_setSingleImage", top.opener);
		if (pFuncWin == null) {
			alert(IDS_FUNCTION_NOT_FOUND);
		} else {
			pFuncWin.WBC_setSingleImage(strPhName, strURL, strHRef, strAltText);
			top.close();
		}
	}
	if (strPhType == IDS_PHTYPE_MULTIPURPOSE) {
		var pFuncWin = WBC_getFuncWin("WBC_setMultiPurposeImage", top.opener);
		if (pFuncWin == null) {
			alert(IDS_FUNCTION_NOT_FOUND);
		} else {
			pFuncWin.WBC_setMultiPurposeImage(strPhName, strURL, strHRef, strAltText);
			top.close();
		}
	}
	if (strPhType == IDS_PHTYPE_THINEDITIE) {
		var pFuncWin = WBC_getFuncWin("WBC_setThinEditIEImage", top.opener);
		if (pFuncWin == null) {
			alert(IDS_FUNCTION_NOT_FOUND);
		} else {
			pFuncWin.WBC_setThinEditIEImage(strPhName, strURL, strHRef, strAltText);
			top.close();
		}
	}	
}


// ---- [Public] This function calls the WBC_setSingleAttachment or 
//				WBC_setMultiPurposeAttachment function to add an attachment 
//				to a placeholder.
function WBC_procsAttachment(strPhName, strPhType, strURL, bAttachIcon) {
	var strDispText;
	strDispText = WBC_verifyDispText(document.NC_LocalForm.NC_DispText.value);
	if 	((!bAttachIcon) && (strDispText == null)) {
		alert(IDS_INVALID_DISP_TEXT);
		return;
	}
	if (strDispText == null) {
		strDispText = "";
	}

	if (strPhType == IDS_PHTYPE_SINGLEATTACHMENT) {
		var pFuncWin = WBC_getFuncWin ("WBC_setSingleAttachment", top.opener);
		if (pFuncWin == null) {
			alert(IDS_FUNCTION_NOT_FOUND);
			return;
		}
		pFuncWin.WBC_setSingleAttachment(strPhName, strURL, strDispText);
		top.close();
	}
	if (strPhType == IDS_PHTYPE_MULTIPURPOSE) {
		var pFuncWin = WBC_getFuncWin ("WBC_setMultiPurposeAttachment", top.opener);
		if (pFuncWin == null) {
			alert(IDS_FUNCTION_NOT_FOUND);
			return;
		}
		if (bAttachIcon) {
			pFuncWin.WBC_setMultiPurposeAttachment(strPhName, strURL, WBC_getIconTag(strURL, strDispText));
		} else {
			pFuncWin.WBC_setMultiPurposeAttachment(strPhName, strURL, strDispText);
		}
		top.close();
	}
	if (strPhType == IDS_PHTYPE_THINEDITIE) {
		var pFuncWin = WBC_getFuncWin ("WBC_setThinEditIEAttachment", top.opener);
		if (pFuncWin == null) {
			alert(IDS_FUNCTION_NOT_FOUND);
			return;
		}
		if (bAttachIcon) {
			pFuncWin.WBC_setThinEditIEAttachment(strPhName, strURL, WBC_getIconTag(strURL, strDispText));
		} else {
			pFuncWin.WBC_setThinEditIEAttachment(strPhName, strURL, strDispText);
		}
		top.close();
	}		
}


// ---- [Public] This function calls the WBC_setSingleVideo or WBC_setMultiPurposeVideo 
//				function to add a video to a placeholder.
function WBC_procsVideo(strPhName, strPhType, strURL, bImg) {
	if (strPhType == IDS_PHTYPE_SINGLEATTACHMENT) {
		var pFuncWin = WBC_getFuncWin ("WBC_setSingleVideo", top.opener);
		if (pFuncWin == null) {
			alert(IDS_FUNCTION_NOT_FOUND);
			return;
		}
		pFuncWin.WBC_setSingleVideo(strPhName, strURL);
		top.close();
	}
	if (strPhType == IDS_PHTYPE_MULTIPURPOSE) {
		var pFuncWin = WBC_getFuncWin ("WBC_setMultiPurposeVideo", top.opener);
		if (pFuncWin == null) {
			alert(IDS_FUNCTION_NOT_FOUND);
			return;
		}
		pFuncWin.WBC_setMultiPurposeVideo(strPhName, strURL, bImg);
		top.close();
	}
	if (strPhType == IDS_PHTYPE_THINEDITIE) {
		var pFuncWin = WBC_getFuncWin ("WBC_setThinEditIEVideo", top.opener);
		if (pFuncWin == null) {
			alert(IDS_FUNCTION_NOT_FOUND);
			return;
		}
		pFuncWin.WBC_setThinEditIEVideo(strPhName, strURL, bImg);
		top.close();
	}		
}


// ---- [Private] This function returns the image tag for an attachment icon
function WBC_getIconTag(strURL, strDispText) {
	var strIconURL;
	if (strURL.indexOf("?") != -1) {
		strIconURL = strURL + "&thumbnail=true";
	} else {
		strIconURL = strURL + "?thumbnail=true";
	}
	if (strDispText == "") {
		strIconURL = strIconURL + "&nolabel=true"
	} else {
		strIconURL = strIconURL + "&label=" + escape(strDispText);
	}
	return "<img alt=\"" + strDispText + "\" src=\"" + strIconURL + "\" border=\"0\">";
}


// ---- [Public] This function calls the WBC_setImageProp on the main authoring form
//					to set the image properties.
function WBC_setImageProp(strPhName) {
	var pFuncWin = WBC_getFuncWin("WBC_setImageProp", top.opener);
	if (pFuncWin == null) {
		alert(IDS_FUNCTION_NOT_FOUND);
		return;
	}
	// some fields may not exist due to placeholder config
	var strHRef = "";
	var strAltText = "";
	if (document.NC_LocalForm.NC_HRef != null)
		strHRef = document.NC_LocalForm.NC_HRef.value;
	if (document.NC_LocalForm.NC_AltText != null)
		strAltText = WBC_encodeContent(document.NC_LocalForm.NC_AltText.value);
	pFuncWin.WBC_setImageProp(strPhName, strHRef, strAltText);
	top.close();
}


// ---- [Public] This function calls the WBC_setAttachmentProp to set the attachment properties.
function WBC_setAttachmentProp(strPhName, bAttachIcon) {
	var strDispText = WBC_verifyDispText(document.NC_LocalForm.NC_DispText.value);
	if (!bAttachIcon && (strDispText == null)) {
		alert(IDS_INVALID_DISP_TEXT);
		return;
	}
	var pFuncWin = WBC_getFuncWin("WBC_setAttachmentProp", top.opener);
	if (pFuncWin == null) {
		alert(IDS_FUNCTION_NOT_FOUND);
		return;
	}
	pFuncWin.WBC_setAttachmentProp(strPhName, strDispText);
	top.close();
}


// ---- [Private] This function verifies the display text property of an attachment
function WBC_verifyDispText(strDispText) {
	if (strDispText.split(" ").length == strDispText.length + 1) {
		return null;
	} else {
		return strDispText;
	}
}


// ---- [Private] This function encodes double quote character to &quot;
function WBC_encodeContent(strContent) {
	return WBC_replaceCharInStr(strContent, "\"", "&quot;", "global");
}


// ---- [Private] This function preview the local resource item
function WBC_onPreview(strResType)
{
	WBC_previewLocalResource(strResType);
	return false;
}