// ********************************************************************
// Copyright (C) 2002 Microsoft Corporation. All rights reserved.
//
// ********************************************************************

//	Open a new image gallery window
function WBC_launchImageGallery(strPostingQueryString, strPhName, strPhType, bAllowUpload) {
	var strURL;
	var strGalleryUrl = IDS_FRAMEWORK_NEW_VIRTUAL_PATH + "/Dialogs/ResourceBrowser/Authoring/ImageSelectBrowse.aspx?";
	strURL = strGalleryUrl + strPostingQueryString + "&" + IDS_QPARAM_PHNAME + "=" + escape(strPhName) + "&" + IDS_QPARAM_PHTYPE + "=" +  strPhType + "&" + IDS_QPARAM_DESKRES + "=" + bAllowUpload + "&" + IDS_QPARAM_REFRESHTREE + "=True";
	WBC_openWindowOnFocus(strURL, IDS_WIN_GALLERY, WBC_getPoppedUpWindowFeatures(IDS_WIN_GALLERY_FEATURES, window.top));
}

//	Open a new attachment gallery window
function WBC_launchAttachmentGallery(strPostingQueryString, strPhName, strPhType, bAllowUpload, bAttachIcon, bAllowVideo) {
	var strURL;
	var strGalleryUrl = IDS_FRAMEWORK_NEW_VIRTUAL_PATH + "/Dialogs/ResourceBrowser/Authoring/AttachmentSelectBrowse.aspx?";
	strURL = strGalleryUrl + strPostingQueryString + "&" + IDS_QPARAM_PHNAME + "=" + escape(strPhName) + "&" + IDS_QPARAM_PHTYPE + "=" +  strPhType + "&" + IDS_QPARAM_DESKRES + "=" + bAllowUpload + "&" + IDS_QPARAM_ATTACHICON + "=" + bAttachIcon + "&" + IDS_QPARAM_VIDEO + "=" + bAllowVideo + "&" + IDS_QPARAM_REFRESHTREE + "=True";
	WBC_openWindowOnFocus(strURL, IDS_WIN_GALLERY, WBC_getPoppedUpWindowFeatures(IDS_WIN_GALLERY_FEATURES, window.top));
}

//	Open a new video gallery window
function WBC_launchVideoGallery(strPostingQueryString, strPhName, strPhType, bAllowUpload, bImg) {
	var strURL;
	var strGalleryUrl = IDS_FRAMEWORK_VIRTUAL_PATH + "/Internals/WBCFuncPages/Cross-Browser/ResourceBrowser/Authoring/VideoBrowser/VideoSelectBrowse.asp?";
	strURL = strGalleryUrl + strPostingQueryString + "&" + IDS_QPARAM_PHNAME + "=" + escape(strPhName) + "&" + IDS_QPARAM_PHTYPE + "=" + strPhType + "&" + IDS_QPARAM_DESKRES + "=" + bAllowUpload + "&" + IDS_QPARAM_IMG + "=" + bImg + "&" + IDS_QPARAM_REFRESHTREE + "=True";
	WBC_openWindowOnFocus(strURL, IDS_WIN_GALLERY, WBC_getPoppedUpWindowFeatures(IDS_WIN_GALLERY_FEATURES, window.top));
}

//	Open a new image properties window
function WBC_launchImageProperties(strPostingQueryString, strPhName) {
	var noImageurl;
    noImageurl = IDS_FRAMEWORK_NEW_VIRTUAL_PATH + 
	"/Client/PlaceholderControlSupport/Images/InsertImageHere.gif";
	
	if ((__CMS_PostbackForm["NCPH_" + strPhName].value == "") ||
		(__CMS_PostbackForm["NCPH_" + strPhName].value == noImageurl)
		)
		{
		alert(IDS_NO_IMAGE_SELECTED);
	} else {
		var strURL = IDS_FRAMEWORK_NEW_VIRTUAL_PATH + "/Dialogs/ResourceBrowser/Authoring/ImagePropertiesOnlyBrowse.aspx?" + strPostingQueryString + "&" + IDS_QPARAM_PHNAME + "=" + escape(strPhName);
		WBC_openWindowOnFocus(strURL, IDS_WIN_INSERT_PROPS, IDS_WIN_IMAGEPROPS_FEATURES);
	}
}

//	Open a new attachment properties window
function WBC_launchAttachmentProperties(strPostingQueryString, strPhName, bAttachIcon) {
	if (__CMS_PostbackForm["NCPH_" + strPhName].value == "") {
		alert(IDS_NO_ATTACHMENT_SELECTED);
	} else {
		var strURL = IDS_FRAMEWORK_NEW_VIRTUAL_PATH + "/Dialogs/ResourceBrowser/Authoring/AttachmentPropertiesOnlyBrowse.aspx?" + strPostingQueryString +"&" + IDS_QPARAM_PHNAME + "=" + escape(strPhName) + "&" + IDS_QPARAM_ATTACHICON + "=" + bAttachIcon;
		WBC_openWindowOnFocus(strURL, IDS_WIN_INSERT_PROPS, IDS_WIN_ATTACHPROPS_FEATURES);
	}
}

//	Open a new table window
function WBC_launchTable(strPostingQueryString, strPhName, strPhType) {
	var strURL = IDS_FRAMEWORK_NEW_VIRTUAL_PATH + "/Dialogs/Table/table.aspx?" + strPostingQueryString + "&" + IDS_QPARAM_PHNAME + "=" + escape(strPhName) + "&" + IDS_QPARAM_PHTYPE + "=" + strPhType;
	WBC_openWindowOnFocus(strURL, IDS_WIN_TABLE, IDS_WIN_TABLE_FEATURES);
}

//	Adds an image to a Single Image placeholder
function WBC_setSingleImage(strPhName, strURL, strHRef, strAltText) {
	if (WBC_isNetscape() && WBC_getBrowserVer() < 4) {
		__CMS_PostbackForm["NCPH_" + strPhName].value = strURL;
		__CMS_PostbackForm["NCPHImgNS3_" + strPhName].value = strURL.substring(strURL.lastIndexOf("/")+1);	
	} else {
		var strImgName = "NCPHImg_" + strPhName;
		for (var i = 0; i < document.images.length; i++) {
			if (document.images[i].name == strImgName) {
				__CMS_PostbackForm["NCPH_" + strPhName].value = strURL;
				document.images[i].src = strURL;
				break;
			}
		}
	}
	WBC_setImageProp(strPhName, strHRef, strAltText);
}

//	This function removes an image from a Single Image placeholder
function WBC_removeSingleImage(strPhName) {
	if (WBC_isNetscape() && WBC_getBrowserVer() < 4) {
		__CMS_PostbackForm["NCPH_" + strPhName].value = "";
		__CMS_PostbackForm["NCPHImgNS3_" + strPhName].value = "No Image";	
	} else {
		var strImgName = "NCPHImg_" + strPhName;
		for (var i = 0; i < document.images.length; i++) {
			if (document.images[i].name == strImgName) {
				__CMS_PostbackForm["NCPH_" + strPhName].value = "";
				document.images[i].src = IDS_FRAMEWORK_NEW_VIRTUAL_PATH + "/Client/PlaceholderControlSupport/Images/InsertImageHere.gif";
				break;
			}
		}
	}
	__CMS_PostbackForm["NCPHHRef_" + strPhName].value = "";
	__CMS_PostbackForm["NCPHAltText_" + strPhName].value = "";
}

//	This function sets the image properties
function WBC_setImageProp(strPhName, strHRef, strAltText) {
	if (strHRef == IDS_HTTP_PROTOCOL) {
		__CMS_PostbackForm["NCPHHRef_" + strPhName].value = "";
	} else {
		__CMS_PostbackForm["NCPHHRef_" + strPhName].value = strHRef;
	}
	__CMS_PostbackForm["NCPHAltText_" + strPhName].value = strAltText;
}

//	Adds an image to a MultiPurpose placeholder
function WBC_setMultiPurposeImage(strPhName, strURL, strHRef, strAltText) {
	var strMultiPurposeTag = "<img src=\"" + strURL + "\" border=\"0\" alt=\"" + strAltText + "\">";
	if ((strHRef != "") && (strHRef != IDS_HTTP_PROTOCOL)) {
		strMultiPurposeTag = "<a href=\"" + strHRef + "\">" + strMultiPurposeTag + "</a>";
	}

	var pFormElement = __CMS_PostbackForm["NCPH_" + strPhName];
	pFormElement.value = pFormElement.value + strMultiPurposeTag;
}

//	Adds an attachment to a Single Attachment placeholder
function WBC_setSingleAttachment(strPhName, strURL, strDispText) {
	__CMS_PostbackForm["NCPH_" + strPhName].value = strURL;
	__CMS_PostbackForm["NCPHAttach_" + strPhName].value = strURL.substring(strURL.lastIndexOf("/")+1);

	WBC_setAttachmentProp(strPhName, strDispText);
}

//	This function removes an attachment from a Single Attachment placeholder
function WBC_removeSingleAttachment(strPhName, strNoAttachment) {
	__CMS_PostbackForm["NCPH_" + strPhName].value = "";
	__CMS_PostbackForm["NCPHAttach_" + strPhName].value = strNoAttachment;
	WBC_setAttachmentProp(strPhName, "");
}

//	This function sets the attachment properties
function WBC_setAttachmentProp(strPhName, strDispText) {
	if (strDispText == null) {
		strDispText = "";
	}
	__CMS_PostbackForm["NCPHDispText_" + strPhName].value = strDispText;
}

//	Adds an attachment to a MultiPurpose placeholder
function WBC_setMultiPurposeAttachment(strPhName, strURL, strDispText) {
	var pFormElement = __CMS_PostbackForm["NCPH_" + strPhName];

	// append anchor <a> tag to the end of the multipurpose placeholder content
	pFormElement.value = pFormElement.value + "<a href=\"" + strURL + "\">" + strDispText + "</a>";
}

//	Adds a video to a Single Attachment placeholder
function WBC_setSingleVideo(strPhName, strURL) {
	__CMS_PostbackForm["NCPHAttach_" + strPhName].value = strURL.substring(strURL.lastIndexOf("/")+1);
	__CMS_PostbackForm["NCPH_" + strPhName].value = strURL;
}

//	Adds a video to a MultiPurpose placeholder
function WBC_setMultiPurposeVideo(strPhName, strURL, bImg) {
	var pFormElement = __CMS_PostbackForm["NCPH_" + strPhName];
	
	// append <img> or <embed> tags to the end of multipurpose placeholder content
	if (bImg) {
		pFormElement.value = pFormElement.value + "<img dynsrc=\"" + strURL + "\">";
	} else {
		pFormElement.value = pFormElement.value + "<embed src=\"" + strURL + "\" loop=\"false\" autoplay=\"false\" controller=\"true\">";
	}
}

//	Add table to a MultiPurpose placeholder
function WBC_setTable(strPhName, strPhType, nColumns, nRows, nBorder) {

	var nCellspacing = 2;
	var nCellpadding = 1;
	
	var strLinebreak;
	var strUserAgent = navigator.userAgent.toLowerCase();
	if (strUserAgent.indexOf("mac") != -1) {
		strLinebreak = "\r";
	} else if (strUserAgent.indexOf("win") != -1) {
		strLinebreak = "\n";
	} else {
		strLinebreak = "\r\n";
	}
	
	var strTableTag = strLinebreak + "<table border=\"" + nBorder + "\" cellspacing=\"" + nCellspacing + "\" cellpadding=\"" + nCellpadding + "\">" + strLinebreak;
	var strCellText;

	for (var i = 0; i < nRows; i++) {
		strTableTag = strTableTag + "  <tr>" + strLinebreak;
		for (var j = 0; j < nColumns; j++) {
			strCellText = "[Col " + (j+1) + " Row " + (i+1) + "]";
			strTableTag = strTableTag + "    <td>" + strCellText + "</td>" + strLinebreak;
		}
		strTableTag = strTableTag + "  </tr>" + strLinebreak;
	}
	
	strTableTag = strTableTag + "</table>" + strLinebreak;
	
	if (strPhType == IDS_PHTYPE_THINEDITIE) {
		WBC_setThinEditIETable(strPhName, strTableTag);
	} else {
		var pFormElement = __CMS_PostbackForm["NCPH_" + strPhName];
		pFormElement.value = pFormElement.value + strTableTag;
	}
}

//	Validate content in all placeholders to see if they contain illegal tags
function WBC_iteratePlaceholders() {
	var arrayIllegalTags = new Array ("img", "embed");
	var strPhType;
	var strPhName;
	
	for (i = 1; i <= __CMS_PostbackForm.NCPHCount.value; i++) {
		strPhName = __CMS_PostbackForm["NCPHName_" + i].value;
		// writing IE rich placeholder content to hidden <input> element
		if ( __CMS_PostbackForm["NCPHRICH_" + strPhName] ) {
			__CMS_PostbackForm["NCPH_" + strPhName].value = __CMS_PostbackForm["NCPHRICH_" + strPhName].html;
		}
	}
	return true;
}

//	For compatibility issue.  Relase after 4.0 will no longer required this
function WBC_previewAuthPosting() {
	WBC_previewAuthPostingWithURL(__CMS_PostbackForm.NC_PreviewURL.value);
}


//	Set Form action Url for different posting action
function WBC_previewAuthPostingWithURL( strPreviewURL ) {
	
	if ( WBC_iteratePlaceholders() == true ) {
		__CMS_PostbackForm.action = strPreviewURL;
		__CMS_PostbackForm.target = IDS_WIN_PREVIEW;
		WBC_openWindowOnFocus("", IDS_WIN_PREVIEW, WBC_getPoppedUpWindowFeatures(IDS_WIN_PREVIEW_FEATURES, window.top))
		__CMS_PostbackForm.submit();
	}
}



//	Save the page / new page
function WBC_savePosting(strAction, bNewPage) 
{
	if ( WBC_iteratePlaceholders() == true ) 
	{
		// modify form action URL based on strAction
		if ((strAction == IDS_ACTION_SAVE) || (strAction == IDS_ACTION_SAVEANDEXIT))
		{	
			if (bNewPage == false)
			{
				if (strAction == IDS_ACTION_SAVE)
				{
					__CMS_PostbackForm.action = __CMS_PostbackForm.NC_SavePageURL.value;	
				}
				else
				{
					__CMS_PostbackForm.action = __CMS_PostbackForm.NC_SavePageAndExitURL.value;	
				}
			}
			else
			// must be a new page
			{	
				if (strAction == IDS_ACTION_SAVE)
				{
					__CMS_PostbackForm.action = __CMS_PostbackForm.NC_SaveNewPageAndContinue.value;
				}
				else
				{
					__CMS_PostbackForm.action = __CMS_PostbackForm.NC_SaveNewPageAndExit.value;
				}
			}	
		
		}
		__CMS_PostbackForm.target = "_self";
		__CMS_PostbackForm.submit();
	}
}

// Set New Page Info
// When create a new page, we need to put in the name and display name
function WBC_setNewPageInfo(strName, strDisplayName)
{
	__CMS_PostbackForm.WBC_fieldNewPageName.value = strName;
	__CMS_PostbackForm.WBC_fieldNewPageDisplayName.value = strDisplayName;
}


//	Set Posting name
function WBC_setPostingName(strValue) {
	__CMS_PostbackForm.NC_Name.value = strValue;
}

//	Set Posting Display name
function WBC_setPostingDisplayName(strValue) {
	__CMS_PostbackForm.NC_DisplayName.value = strValue;
}


//	Get the hyperlink HREF of a placeholder (Single Image)
function WBC_getHRef(strPhName) {
	var strHRef = __CMS_PostbackForm["NCPHHRef_" + strPhName].value;
	if (strHRef == "") {
		return IDS_HTTP_PROTOCOL;
	} else {
		return strHRef;
	}
}

//	Get the ALT text of a placeholder (Single Image)
function WBC_getAltText(strPhName) {
	return __CMS_PostbackForm["NCPHAltText_" + strPhName].value;
}

//	Get the display text of a placeholder (Single Attachment)
function WBC_getDispText(strPhName) {
	return __CMS_PostbackForm["NCPHDispText_" + strPhName].value;
}

//	Gives warning before leaving authoring page
function WBC_handleOnBeforeUnload()  {
	if (g_bWarnBeforeLeave) {
		event.returnValue = IDS_WARN_BEFORE_LEAVE;
	}
}

//	Turn off warning before leaving authoring form
function WBC_offWarningBeforeLeave() {
	g_bWarnBeforeLeave = false;
}

//	g_bWarnBeforeLeave is a global variable to indicate whether warning should be given before
//	leaving this page. It works only in IE because of the onbeforeunload event.
//	When authoring commands preview, save, submit or cancel is clicked, this variable will set
//	to false by calling WBC_offWarningBeforeLeave.
var g_bWarnBeforeLeave = true;

// check if it's IE
if (document.all) 
{
	window.onbeforeunload = WBC_handleOnBeforeUnload;
}