/* This file contains QPS' versions of functionality found in IIS_CMS */

/*----------------------------------------------------
Image Selection and Properties Logic
-----------------------------------------------------*/
//Global variables to hold DOM objects that represent the selected image and hyperlink 
//whose properties will be changed.
var selectedImage = null;
var selectedImageLink = null;

// get the TextAlign attribute of the selected image
function WBC_getTextAlign(strPhName) {
	return __CMS_PostbackForm["NCPHTextAlign_" + strPhName].value;
}

// get the Border attribute of the selected image
function WBC_getBorder(strPhName) {
	return __CMS_PostbackForm["NCPHBorder_" + strPhName].value;
}

//Overrides the CMS functionality
//Supports the additional properties made available in the
//custom edit image properties dialog (\QPSTemplates\Dialogs\EditImageProperties.aspx)
function WBC_setImageProp(strPhName, strHRef, strAltText, strTextAlign, strBorder) {   
	if (selectedImage!= null) {
		//if (strHRef != IDS_HTTP_PROTOCOL&&strHRef != "") {
		if (strHRef != IDS_HTTP_PROTOCOL) {			
			var parentNode = selectedImage.parentNode;
			if (parentNode.nodeType==1&&parentNode.tagName=="A") {
				parentNode.href = strHRef;
			} else {
				selectedImageLink.href = strHRef;				
				selectedImage.applyElement(selectedImageLink);
			}
		} 
		
		//window.alert("Alt: " + strAltText + " Align: " + strTextAlign + " Border: " + strBorder);
		selectedImage.alt = strAltText;		
		selectedImage.align = strTextAlign;
		selectedImage.border = strBorder;
	}
}

/*----------------------------------------------------
Object Attachment Logic
-----------------------------------------------------*/
//Global constant that defines the placeholder type that accepts object attachments.
var IDS_PHTYPE_THINEDITOBJECT = "ThinEditObject";

//Overrides the CMS function in \IIS_CMS\WebAuthor\Dialogs\ResourceBrowser\Authoring\ResourceBrowserClient.js
//Adds a call to a QPS function that handles object attachments.
function WBC_procsAttachment(strPhName, strPhType, strURL, bAttachIcon, strInputDispText, strInputEncodedDispText) {
	var strDispText = "";
	var strEncodedDispText = "";

	if 	((!bAttachIcon) && (strInputDispText == null)) {
		alert(L_InvalidDispText_ErrorMessage);
		return;
	}
	
	if ((bAttachIcon) && (strInputEncodedDispText == null)) {
		// encoded display name was not specified so use the regular display name
		strInputEncodedDispText = strInputDispText;
	}
	
	if (strInputDispText) {
        strDispText = WBC_encodeContent(strInputDispText);
    }
    
    if (strInputEncodedDispText) {
    	strEncodedDispText = WBC_encodeContent(strInputEncodedDispText);
    }

	if (strPhType == IDS_PHTYPE_SINGLEATTACHMENT) {
		var pFuncWin = WBC_getFuncWin ("WBC_setSingleAttachment", top.opener);
		if (pFuncWin == null) {
			alert("WBC_setSingleAttachment " + L_FunctionNotFound_ErrorMessage);
			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("WBC_setMultiPurposeAttachment " + L_FunctionNotFound_ErrorMessage);
			return;
		}
		if (bAttachIcon) {
			// WBC_getIconTag uses the URLEncodeUnicode display name for the label and the non 
			// URLEncodeUnicode display name for the alt text
			pFuncWin.WBC_setMultiPurposeAttachment(strPhName, strURL, WBC_getIconTag(strURL, strEncodedDispText, 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("WBC_setThinEditIEAttachment " + L_FunctionNotFound_ErrorMessage);
			return;
		}
		if (bAttachIcon) {
			// WBC_getIconTag uses the URLEncodeUnicode display name for the label and the non 
			// URLEncodeUnicode display name for the alt text
			pFuncWin.WBC_setThinEditIEAttachment(strPhName, strURL, WBC_getIconTag(strURL, strEncodedDispText, strDispText));
		} else {
			pFuncWin.WBC_setThinEditIEAttachment(strPhName, strURL, strDispText);
		}
		top.close();
	}	
	QPS_procsObjectAttachment(strPhName, strPhType, strURL, bAttachIcon, strDispText, strEncodedDispText);	
}

//Provides the ability to select HTML objects from a resource gallery.
function QPS_procsObjectAttachment(strPhName, strPhType, strURL, bAttachIcon, strDispText, strEncodedDispText) {
    if (strPhType == IDS_PHTYPE_THINEDITOBJECT) {
		var pFuncWin = WBC_getFuncWin (strPhName + "WBC_setThinEditIEObject", top.opener);
		if (pFuncWin == null) {
			alert(strPhName + "WBC_setThinEditIEObject "+L_FunctionNotFound_ErrorMessage);
			return;
		}
		//alert(strPhName + " " + strURL + " " + strURL + " " + strDispText + " " + WBC_getIconTag(strURL, strEncodedDispText, strDispText))
		if (bAttachIcon) {
			// WBC_getIconTag uses the URLEncodeUnicode display name for the label and the non 
			// URLEncodeUnicode display name for the alt text
			eval("pFuncWin." + strPhName + "WBC_setThinEditIEObject(strPhName, strURL, WBC_getIconTag(strURL, strEncodedDispText, strDispText))");
		} else {
			eval("pFuncWin." + strPhName + "WBC_setThinEditIEObject(strPhName, strURL, strDispText)");
		}
		top.close();
	}
}

/*----------------------------------------------------
Decline Reason Dialog Support
-----------------------------------------------------*/
//Global constant to define the name of the Decline Reason window
var IDS_WIN_DECLINEREASON_NAME = "WBC_winDeclineReason";

//Global constant that defines the features for the Decline Reason dialog window
var IDS_WIN_DECLINEREASON_FEATURES = "width=650,height=180,resizable,scrollbars,status=no";

//Opens the Decline Reason dialog window using standard MCMS function.
function CMS_openWindowDeclineReason(strDlgURL){ 
    WBC_openWindowOnFocus(strDlgURL, IDS_WIN_DECLINEREASON_NAME,IDS_WIN_DECLINEREASON_FEATURES);
}

/*----------------------------------------------------
Hyperlink Support
-----------------------------------------------------*/
var INTRANET_SERVER_PATH = "http://intranetdv.qldpol";

function onClickBrowse()
{
	// Call InternalLinks.aspx 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("/qpstemplates/CMS/webauthor/dialogs/InternalLinks/InternalLinks.aspx" + "?NRMODE=Unpublished&wbc_caller=IEModal", 
												strURL, IDS_IEWIN_INTLINKS_FEATURES);
	if (strDlgRet != "Cancel")
	{
		document.HLink_Form.HRef.value = strDlgRet;
	}
}

// ---- [Public] This function opens the InternalLinks dialog for EditImageProperties
function WBC_onBrowse()
{
	WBC_openWindowOnFocus("/qpstemplates/cms/WebAuthor/Dialogs/InternalLinks/InternalLinks.aspx" + "?NRMODE=Unpublished", "InternalLinks", IDS_WIN_INTLINKS_FEATURES);
}

function setTargetReadOnly(bReadOnly)
{
	//do nothing
}

function onBodyLoad()
{
	// init dialog values with passed in values
	document.HLink_Form.HRef.value = g_OrigHRef;
	document.HLink_Form.Title.value = g_OrigTitle;	
	document.HLink_Form.Name.value = g_OrigName;
}

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(L_HlinkWarnEmpty_Message) )
			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
		alert("Relative links are not supported by the CMS. Please use the 'Browse' button to select page to link to or specify a absolute link.");
		return;
	}

    //Convert any absolute URLs to intranetdv to a root relative version.
	if (strHref.toLowerCase().indexOf(INTRANET_SERVER_PATH)==0) {
		strHref = strHref.substring(INTRANET_SERVER_PATH.length);
	}
	
	// return string in format of "Href;Title;Target"
	var retValue = null
	// only construct strRet if not both of Href and Name are blank
	if ( !bHrefBlank || (strName != "") )
	{
		retValue = new Object();
		retValue.Href = strHref;
		retValue.Title = document.HLink_Form.Title.value;
		retValue.Target = "_self";
		retValue.Name = strName;
	}
	
	
    //window.alert(retValue.Href);
	window.returnValue = retValue;
	window.close();
}
/*----------------------------------------------------
Preview Customisation
-----------------------------------------------------*/
function CMS_preparePreviewPostback( strUrl )
{
	// cache original state of the Form
	g_strOriginalFormAction = __CMS_PostbackForm.action;
	g_strOriginalFormTarget = __CMS_PostbackForm.target;

	__CMS_PostbackForm.action = strUrl;
	__CMS_PostbackForm.target = "WBC_winPreview";
	
	__CMS_PostbackFormBeenReset = true;

	// open a blank window for the target Preview postback
	//WBC_openWindowOnFocus("about:blank", "WBC_winPreview", WBC_UseDefaultSizing(window.top));
}