// ********************************************************************
// 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."
// ********************************************************************

//	Define constants
var IDS_EOP_TEXT_TYPE = "text";
var IDS_EOP_SELECTION_TYPE = "list";
var IDS_EOP_PREFIX_NAME = "WBC_intxtEop_";

var IDS_SELECT_TEXTAREA_COLS_IE = 35;
var IDS_SELECT_TEXTAREA_COLS_NS = 24;
var IDS_SELECT_TEXTAREA_ROWS_IE = 6;
var IDS_SELECT_TEXTAREA_ROWS_NS = 6;
var ID_MAX_NO_OF_EOP_CHARS = 2001;

// Error message when the user input is exceeded 2001 character for EOP
var IDS_ERROR_INVALID_NO_OF_CHARS = "Enter text cannot be exceeded 2,000 characters."
var IDS_ERROR_INVALID_CHARS = "Enter text cannot contain ']]>'."

//	Define variables
var g_pFormModifyDialog;
var g_bCanSetSharedProperties = false;

var g_nEopIndex;
var g_strEopType;
var g_pinradioEopValue = new Array();

// Initial Modify Dialog and set all the variables
function WBC_initialModifyDialog() {
	var nEopIndex;
	var pFuncWin;
	
	// set whether the user has right to change the EOP or not
	pFuncWin = WBC_getFuncWin("WBC_isCanSetProperties", top.opener);
	if (pFuncWin == null) {
		alert(IDS_FUNCTION_NOT_FOUND);
	} else {
		g_bCanSetSharedProperties = pFuncWin.WBC_isCanSetProperties();
	}
	
	// get the current selected Eop index
	pFuncWin = WBC_getFuncWin("WBC_getSelectedEopIndex", top.opener);
	if (pFuncWin == null) {
		alert(IDS_FUNCTION_NOT_FOUND);
	} else {
		g_nEopIndex = pFuncWin.WBC_getSelectedEopIndex();
	}
	
	// ge the current selected Eop type and return text or list
	pFuncWin = WBC_getFuncWin("WBC_getEopType", top.opener);
	if (pFuncWin == null) {
		alert(IDS_FUNCTION_NOT_FOUND);
	} else {
		g_strEopType = pFuncWin.WBC_getEopType(g_nEopIndex);
	}
}


// get the current selected Eop name
function WBC_displayEopName() {
	var pFuncWin = WBC_getFuncWin("WBC_getEopName", top.opener);
	if (pFuncWin == null) {
		alert(IDS_FUNCTION_NOT_FOUND);
		return;
	} else {
		document.write(pFuncWin.WBC_getEopName(g_nEopIndex));
	}
}


// write HTML for the current selected Eop Type
function WBC_displayEopType() {
	document.write(g_strEopType);
}


// setup information of the Channel modify dialog
function WBC_onLoadChannelModifyDialog() {
	
	// get the form of Modify Dialog
	g_pFormModifyDialog = document.WBC_formModifyDialog;
}


// setup information of the Posting modify dialog
function WBC_onLoadPostingModifyDialog() {
	
	// get the form of Modify Dialog
	g_pFormModifyDialog = document.WBC_formModifyDialog;
	
	// update the value of the radio button
	var bIsUsesDefaultValue;
	var pFuncWin = WBC_getFuncWin("WBC_isEopUsesDefaultValue", top.opener);
	if (pFuncWin == null) {
		alert(IDS_FUNCTION_NOT_FOUND);
		return;
	}
	
	// get radio button object
	WBC_getFormElement(g_pFormModifyDialog, "WBC_inradioEopValue", g_pinradioEopValue);
		
	if (pFuncWin.WBC_isEopUsesDefaultValue(g_nEopIndex).toLowerCase() == "true") {
		g_pinradioEopValue[0].checked = true;
	} else {
		g_pinradioEopValue[0].checked = false;
	}
	g_pinradioEopValue[1].checked = !g_pinradioEopValue[0].checked;	
}


// write HTML for the current selected Eop Value
function WBC_displayEopValue() {
	if (g_strEopType.toLowerCase() == IDS_EOP_TEXT_TYPE) {
		WBC_displayEopText();
	} else {
		WBC_displayEopList();
	}
}


// write HTML for the current selected Eop Value
function WBC_displayPageName() {
	var pFuncWin = WBC_getFuncWin("WBC_getPageName", top.opener);
	if (pFuncWin == null) {
		alert(IDS_FUNCTION_NOT_FOUND);
		return;
	}
	
	var strPageName =  pFuncWin.WBC_getPageName();
	document.write(strPageName);
}


// write HTML for the current selected Eop value where its type is text
function WBC_displayEopText() {
	var pFuncWin = WBC_getFuncWin("WBC_getEopValue", top.opener);
	if (pFuncWin == null) {
		alert(IDS_FUNCTION_NOT_FOUND);
		return;
	} 
	
	if (g_bCanSetSharedProperties) {
		document.write("<textarea name='WBC_txtareaEopValue' " + WBC_displayTextAreaSize() + "' wrap='soft'>");
	} else {
		document.write("<textarea name='WBC_txtareaEopValue' ReadOnly onfocus='this.blur();' " + WBC_displayTextAreaSize() + "' wrap='soft'>");
	}
	document.write(pFuncWin.WBC_getEopValue(g_nEopIndex));
	document.write("</textarea>");
}


// write HTML for the allowable Eop list
function WBC_displayEopList() {	
	// get the Eop value
	var pFuncWin = WBC_getFuncWin("WBC_getEopValue", top.opener);
	if (pFuncWin == null) {
		alert(IDS_FUNCTION_NOT_FOUND);
		return;
	}	
	var strEopValue =  pFuncWin.WBC_getEopValue(g_nEopIndex);
	
	if (g_bCanSetSharedProperties) {
		// get the Eop allowable list string
		pFuncWin = WBC_getFuncWin("WBC_getEopList", top.opener);
		if (pFuncWin == null) {
			alert(IDS_FUNCTION_NOT_FOUND);
			return;
		}
		
		// display as a dropdown list
		var strEopList = pFuncWin.WBC_getEopList(g_nEopIndex);
		
		// for Mac OS the carrage return is different than the PC OS
		var colEopList;
		var strUserAgent = navigator.userAgent.toLowerCase();
		if (strUserAgent.indexOf("mac") >= 0) {
			colEopList = strEopList.split(/\r/);
		} else {
			colEopList = strEopList.split(/\r\n/);
		}
		
		document.write("<select width='180' name='WBC_selEopValue'>");
		
		// Notes: Since we need to support empty string in the Eop value of the "Selection" type,
		// when constructing the string of the Eop list (i.e., strEopList), each Eop available value
		// should wrap with a single quote in order to support empty string. If the string list doesn't
		// contain the single quote, the array of "colEopList" won't contain a empty string value after using
		// the JScript function "split". Therefore, we have to get rid of the quote after retrieve the Eop value
		// for the string of Eop List.
		for (var i=1; i<=colEopList.length; i++) {
			var strColEopListValue = WBC_unWrapEopListValue( colEopList[i-1] );
			if (strEopValue == strColEopListValue) {
				document.write("<option value='" + strColEopListValue + "' selected>" + strColEopListValue);
			} else {
				document.write("<option value='" + strColEopListValue + "'>" + strColEopListValue);
			}
		}
		document.write("</select>");
	} else {
		document.write(strEopValue);
	}
}


// write HTML for the default value of the current selected Eop
function WBC_displayEopUsesDefaultValue() {
	var pFuncWin = WBC_getFuncWin("WBC_getEopDefaultValue", top.opener);
	if (pFuncWin == null) {
		alert(IDS_FUNCTION_NOT_FOUND);
		return;
	} 
	
	if (g_strEopType.toLowerCase() == IDS_EOP_TEXT_TYPE) {
		document.write("<textarea name='WBC_txtareaEopDefaultValue' ReadOnly onfocus='this.blur();' " + WBC_displayTextAreaSize() + "' wrap='soft'>");
		document.write(pFuncWin.WBC_getEopDefaultValue(g_nEopIndex));
		document.write("</textarea>");
	} else {
		document.write(pFuncWin.WBC_getEopDefaultValue(g_nEopIndex));
	}
}

// write HTML for the value of the current selected Eop
function WBC_displayEopSelectedValue() {
	if (g_strEopType.toLowerCase() == IDS_EOP_TEXT_TYPE) {
		WBC_displayEopSelectedText();
	} else {
		WBC_displayEopSelectedList();
	}
}


// write HTML for the value of the current selected Eop where its type is Text
function WBC_displayEopSelectedText() {
	// get the Eop value
	var pFuncWin = WBC_getFuncWin("WBC_getEopValue", top.opener);
	if (pFuncWin == null) {
		alert(IDS_FUNCTION_NOT_FOUND);
		return;
	} 
	
	if (g_bCanSetSharedProperties) {
		document.write("<textarea name='WBC_txtareaEopValue' onfocus='WBC_setStatus();' " + WBC_displayTextAreaSize() + "' wrap='soft'>");
	} else {
		document.write("<textarea name='WBC_txtareaEopValue' ReadOnly onfocus='this.blur();'" + WBC_displayTextAreaSize() + "' wrap='soft'>");
	}
	
	var pFuncWin = WBC_getFuncWin("WBC_isEopUsesDefaultValue", top.opener);
	if (pFuncWin == null) {
		alert(IDS_FUNCTION_NOT_FOUND);
		return;
	}
	if (pFuncWin.WBC_isEopUsesDefaultValue(g_nEopIndex).toLowerCase() == "false") {
		document.write(pFuncWin.WBC_getEopValue(g_nEopIndex));
	}
	document.write("</textarea>");
}


// write HTML for the value of the current selected Eop where its type is List
function WBC_displayEopSelectedList() {
	// get the Eop value
	var pFuncWin = WBC_getFuncWin("WBC_getEopValue", top.opener);
	if (pFuncWin == null) {
		alert(IDS_FUNCTION_NOT_FOUND);
		return;
	}
	var strEopValue =  pFuncWin.WBC_getEopValue(g_nEopIndex);
		
	if (g_bCanSetSharedProperties) {
		pFuncWin = WBC_getFuncWin("WBC_getEopList", top.opener);
		if (pFuncWin == null) {
			alert(IDS_FUNCTION_NOT_FOUND);
			return;
		}
		
		// display as a dropdown list
		var strEopList = pFuncWin.WBC_getEopList(g_nEopIndex);
		
		// for Mac OS the carrage return is different than the PC OS
		var colEopList;
		var strUserAgent = navigator.userAgent.toLowerCase();
		if (strUserAgent.indexOf("mac") >= 0) {
			colEopList = strEopList.split(/\r/);
		} else {
			colEopList = strEopList.split(/\r\n/);
		}

		document.write("<select width='180' name='WBC_selEopValue' onchange='WBC_setStatus();'>");
		
		// Notes: Since we need to support empty string in the Eop value of the "Selection" type,
		// when constructing the string of the Eop list (i.e., strEopList), each Eop available value
		// should wrap with a single quote in order to support empty string. If the string list doesn't
		// contain the single quote, the array of "colEopList" won't contain a empty string value after using
		// the JScript function "split". Therefore, we have to get rid of the quote after retrieve the Eop value
		// for the string of Eop List.
		for (var i=1; i<=colEopList.length; i++) {
		
			var strColEopListValue = WBC_unWrapEopListValue( colEopList[i-1] );
			if (strEopValue == strColEopListValue) {
				
				document.write("<option value='" + strColEopListValue + "' selected>" + strColEopListValue);
			} else {
				
				document.write("<option value='" + strColEopListValue + "'>" + strColEopListValue);
			}
		}
		document.write("</select>");
	} else {
		document.write(strEopValue);
	}
}

// unwrap the quote of the list value, it will get rid of the quote of the EOP value.
// For example, strEopListValue = 'yellow', return value = yellow. (i.e., without quote)
function WBC_unWrapEopListValue( strEopListValue ) {
	return strEopListValue.substring( 1, strEopListValue.length - 1 );
}

// save the change for the Channel modify dialog
function WBC_onSaveChannelModifyDialog() {
	var strEopValue;
	
	if (g_bCanSetSharedProperties) {
		// get the Eop updated value
		if (g_strEopType.toLowerCase() == IDS_EOP_TEXT_TYPE) {
			strEopValue = g_pFormModifyDialog.WBC_txtareaEopValue.value;		
		} else {
			var nOptIndex = g_pFormModifyDialog.WBC_selEopValue.selectedIndex;
			strEopValue = g_pFormModifyDialog.WBC_selEopValue.options[nOptIndex].value;
		}
		
		// check max no of character (max = 2001 characters)
		if (strEopValue.length > ID_MAX_NO_OF_EOP_CHARS) {
			alert( IDS_ERROR_INVALID_NO_OF_CHARS );
			return false;
		}
		
		// check the invalid text. For CDATA cannot allow "]]>"
		if (strEopValue.indexOf("]]>") > -1) {
			alert( IDS_ERROR_INVALID_CHARS );
			return false;
		}
		
		// update the properties dialog
		var pFuncWin = WBC_getFuncWin("WBC_updateUIEopUsesSelectedValue", top.opener);
		if (pFuncWin == null) {
			alert(IDS_FUNCTION_NOT_FOUND);
			return;
		} else {
			pFuncWin.WBC_updateUIEopUsesSelectedValue(g_nEopIndex, strEopValue)
		} 
		
	}
	window.close();
	return;
}


// save the change for the Posting modify dialog
function WBC_onSavePostingModifyDialog() {
	var pFuncWin;
	
	if (g_bCanSetSharedProperties) {
		
		if (g_pinradioEopValue[0].checked) {
			
			// get the default value of the current Eop if the user selected default
			pFuncWin = WBC_getFuncWin("WBC_updateUIEopUsesDefaultValue", top.opener);
			if (pFuncWin == null) {
				alert(IDS_FUNCTION_NOT_FOUND);
				return;
			} else {
				pFuncWin.WBC_updateUIEopUsesDefaultValue(g_nEopIndex)	
			}
		} else {
			
			// get the modified value of the current Eop if the user selected non-default
			var strEopValue;
			if (g_strEopType.toLowerCase() == IDS_EOP_TEXT_TYPE) {
				strEopValue = g_pFormModifyDialog.WBC_txtareaEopValue.value;		
			} else {
				var nOptIndex = g_pFormModifyDialog.WBC_selEopValue.selectedIndex;
				strEopValue = g_pFormModifyDialog.WBC_selEopValue.options[nOptIndex].value;
			}			
			
			// check max no of character (max = 2001 characters)
			if (strEopValue.length > ID_MAX_NO_OF_EOP_CHARS) {
				alert( IDS_ERROR_INVALID_NO_OF_CHARS );
				return false;
			}
			
			// check the invalid text. For CDATA cannot allow "]]>"
			if (strEopValue.indexOf("]]>") > -1) {
				alert( IDS_ERROR_INVALID_CHARS );
				return false;
			}
			
			pFuncWin = WBC_getFuncWin("WBC_updateUIEopUsesSelectedValue", top.opener);
			if (pFuncWin == null) {
				alert(IDS_FUNCTION_NOT_FOUND);
				return;
			} else {
				pFuncWin.WBC_updateUIEopUsesSelectedValue(g_nEopIndex, strEopValue)	
			}
			 
		}	
	}
	window.close();
	return;
}

// return the no of cols and rows for different browser
function WBC_displayTextAreaSize() {
	var strTextAreaSize
	
	if (WBC_isNetscape()) {
		strTextAreaSize = "cols='" + IDS_SELECT_TEXTAREA_COLS_NS + "' rows='" + IDS_SELECT_TEXTAREA_ROWS_NS;
	} else {
		strTextAreaSize = "cols='" + IDS_SELECT_TEXTAREA_COLS_IE + "' rows='" + IDS_SELECT_TEXTAREA_ROWS_IE;
	}
	return strTextAreaSize;
}


// disable or enable the text are depend on the user selection
function WBC_setStatus() {	
	if (!g_pinradioEopValue[1].checked) {
		if (g_strEopType.toLowerCase() == IDS_EOP_TEXT_TYPE) {
			g_pFormModifyDialog.WBC_txtareaEopValue.blur();
		} else {
			g_pinradioEopValue[1].checked = true;
		}
	}
}


// write HTML for the label
function WBC_displayEopLabel() {
	if (g_strEopType.toLowerCase() == IDS_EOP_TEXT_TYPE) {
		document.write("Enter Text:");
	} else {
		document.write("Select Value:");
	}
}




