// ********************************************************************
// 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 DATE_NEVER = new Date(3000,0,1,0,0,0,0);
var IDS_NEVER = "Never";
var IDS_EOP_SELECTION_TYPE = "selection";
var IDS_ELEMENT_TYPE_CHECKBOX = "checkbox";
var IDS_FRAMEWORK_VIRTUAL_PATH = "/NR/System/WBC";
var IDS_TRANSPARENT_GIF = IDS_FRAMEWORK_VIRTUAL_PATH + "/Internals/Shared/Transparent.gif";
var IDS_IMG_ICON_YES = "/NR/System/WBC/Internals/WBCFuncPages/CommonImages/Icons/iconYes.gif";

// 	Define the modify dialog size
var IDS_WIN_CHANNEL_MODIFYDIALOG_TEXT_FEATURES = "width=500,height=280,resizable";
var IDS_WIN_CHANNEL_MODIFYDIALOG_LIST_FEATURES = "width=450,height=270,resizable";
var IDS_WIN_CHANNEL_MODIFYDIALOG_NAME = "WBC_winChannelModifyDialog";
var IDS_WIN_POSTING_MODIFYDIALOG_TEXT_FEATURES = "width=500,height=540,resizable";
var IDS_WIN_POSTING_MODIFYDIALOG_LIST_FEATURES = "width=450,height=350,resizable";
var IDS_WIN_POSTING_MODIFYDIALOG_NAME = "WBC_winPostingModifyDialog";
var IDS_WIN_READ_EOP_VALUE_NAME = "WBC_winEopCurrentValueDialog";
var IDS_WIN_READ_EOP_VALUE_FEATURES = "width=500,height=280,resizable";

// 	Define the Modify dialog URL
var IDS_WIN_CHANNEL_MODIFYDIALOG_URL = "/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/PropertiesDialog/Channel/ChannelEopModifyShow.asp";
var IDS_WIN_POSTING_MODIFYDIALOG_URL = "/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/PropertiesDialog/Posting/PostingEopModifyShow.asp";
var IDS_WIN_READ_EOP_VALUE_URL = "/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/PropertiesDialog/Common/EopCurrentValueShow.asp";

// 	Define Programming variable
var IDS_EOP_PREFIX_NAME = "WBC_intxtEop_";

// Define constant varible for XML
var IDS_XML_CDATA_BEGIN = "<![CDATA[";
var IDS_XML_CDATA_END = "]]>";
var IDS_XML_PROPS = "PROPS";
var IDS_XML_EOPS = "EOPS";
var IDS_XML_EOP = "EOP";
var IDS_XML_EOPNAME = "EOPNAME";
var IDS_XML_EOPVALUE = "EOPVALUE";
var IDS_XML_EOPDEFAULT = "EOPDEFAULT";
var IDS_XML_NAME = "NAME";
var IDS_XML_DISPLAYNAME = "DISPLAYNAME";
var IDS_XML_STARTDATE = "STARTDATE";
var IDS_XML_EXPIRYDATE = "EXPIRYDATE";
var IDS_XML_YEAR = "YEAR";
var IDS_XML_MONTH = "MONTH";
var IDS_XML_DAY = "DAY";
var IDS_XML_HOUR = "HOUR";
var IDS_XML_MINUTE = "MINUTE";
var IDS_XML_SECOND = "SECOND";
var IDS_XML_ISIMPORTANT = "ISIMPORTANT";
var IDS_XML_ISROBOTFOLLOW = "ISROBOTFOLLOW";
var IDS_XML_ISROBOTINDEX = "ISROBOTINDEX";
var IDS_XML_ISHIDDENMODE = "ISHIDDENMODE";
var IDS_XML_DESCRIPTION = "DESCRIPTION";

// ---- define a set of constant for start and expiry radio button
var ID_RADIOBTN_STARTDATE_IMMEDIATELY = 0;		// index of array for pointing "Immediately" radio button
var ID_RADIOBTN_STARTDATE_INPUT = 1;			// index of array for pointing "Start Date" radio button
var ID_RADIOBTN_EXPIRYDATE_NEVER = 0;			// index of array for pointing "Never" radio button
var ID_RADIOBTN_EXPIRYDATE_INPUT = 1;			// index of array for pointing "Expiry Date" radio button
var ID_RADIOBTN_IMMEDIATELY_OR_NEVER = 0;
var ID_RADIOBTN_DATE_INPUT = 1;

// alert message
var IDS_UI_ERROR_MISSING_ELEMENT = "Error: the element is undefined.";

// define a set of query parameter.
var IDS_QPARAM_PAGE_CHARSET = "wbc_charset";

//	Define variables
var sRepeat = null;
var g_bCanSetSharedProperties;		// boolean retrieve from ROM API "CanSetSharedProperties" properties
var g_bCanSetLocalProperties;		// boolean retrieve from ROM API "CanSetLocalProperties" properties
var g_pFormProperties;
var g_pFormStandardTab;
var g_pFormCustomTab;
var g_pradioStartDate = new Array();
var g_pradioExpiryDate = new Array();
var g_arrayModifiedEopIndex = new Array();
var g_nSelectedEopIndex = -1;
var g_strModifiedPropsXML;
var g_strCharSet;


// Update the UI content and set the global parameter
function WBC_onLoadProperties( bCanSetLocalProperties ) {	
	
	// get right to change
	g_bCanSetLocalProperties = bCanSetLocalProperties;
	
	// get the Forms object
	g_pFormProperties = window.document.WBC_formProperties;
}


// Submit the properties to the server using form tags
function WBC_onSaveProperties() {
	// Verify the user can change the properties or not base on two variable
	// "g_bCanSetLocalProperties" is retrieved from ROM API "CanSetLocalProperties" properties
	// "g_bCanSetSharedProperties" is retrieved from ROM API "CanSetSharedProperties" properties
	if (!g_bCanSetSharedProperties && !g_bCanSetLocalProperties) {
		window.close();
		return;
	}
	
	//	Verify g_pFormProperties.WBC_intxtName.value to see if its empty, all blanks or contain illegal characters
	//	such as #, &, +, %, /, | and empty string. WBC_verifyInputName is in CommonClient.js
	if (g_bCanSetSharedProperties) {
		if (!WBC_verifyInputName(g_pFormProperties.WBC_intxtName)) 
		{
			alert(IDS_INVALID_NAME);
			return;
		}
	}
	
	//	Verify Start Date and Expiry Date
	if (g_bCanSetLocalProperties) {
	
		// Start Date
		var pStartDate = WBC_verifyUIStartDate();
		if (pStartDate == null) {
			return;
		}
		if (g_pradioStartDate[ ID_RADIOBTN_STARTDATE_IMMEDIATELY ].checked) {
			g_pradioStartDate[ ID_RADIOBTN_STARTDATE_IMMEDIATELY ].value = WBC_formatDateToString( pStartDate, false );	
		} else {
			g_pradioStartDate[ ID_RADIOBTN_STARTDATE_INPUT ].value = WBC_formatDateToString( pStartDate, false );
		}
		
		// Expiry Date
		var pExpiryDate = WBC_verifyUIExpiryDate();
		if (pExpiryDate == null) {
			return;
		}
		if (g_pradioExpiryDate[ ID_RADIOBTN_EXPIRYDATE_NEVER ].checked) {
			g_pradioExpiryDate[ ID_RADIOBTN_EXPIRYDATE_NEVER ].value = WBC_formatDateToString( DATE_NEVER, false );	
		} else {
			g_pradioExpiryDate[ ID_RADIOBTN_EXPIRYDATE_INPUT ].value = WBC_formatDateToString( pExpiryDate, false );
		}			
		
		// don't let the expiry date is less than start date
		if (pStartDate.getTime() > pExpiryDate.getTime()) {
			alert(IDS_EXPIRYDATE_LESS_THAN_STARTDATE);
			return;
		}
	}

	// Transfer hook properties to the main submit form
	var bTransferredAdditionalProperties = WBC_transferAdditionalPropsToSubmitForm();
	
	// The input value is okay, start to update the hidden value in submit form "WBC_formProperties"
	var bCreatedModifiedPropsToSubmitForm = WBC_createModifiedPropsToSubmitForm();
	
	if (bTransferredAdditionalProperties || bCreatedModifiedPropsToSubmitForm) {
		// Submit the form
		g_pFormProperties.submit();
		
	} else {
		window.close();
	}
}
 

// Update UI start date
function WBC_updateUIStartDate() 
{
	var pDate = new Date(g_pradioStartDate[ ID_RADIOBTN_STARTDATE_INPUT ].value);
	if (isNaN(pDate.getTime()) || (pDate.getTime() == 0)) {
		g_pradioStartDate[ ID_RADIOBTN_STARTDATE_IMMEDIATELY ].checked = true;
		pDate = new Date();
	} else {
		g_pradioStartDate[ ID_RADIOBTN_STARTDATE_INPUT ].checked = true;
		pDate = WBC_convertUTC2Local(pDate);
	}

	var nFullYear = WBC_fixJavaScriptYear(pDate.getYear());
	var bYearFound = false;
	
	//	Match Start Year with option list	
	for (var i=0; i<g_pFormStandardTab.WBC_selStartYear.options.length; i++) {
		if (g_pFormStandardTab.WBC_selStartYear.options[i].value == nFullYear) {
			g_pFormStandardTab.WBC_selStartYear.selectedIndex = i;
			bYearFound = true;
			break;
		}
	}

	//	If Start Year is not in option list, append it to the option list
	if (!bYearFound) {
		g_pFormStandardTab.WBC_selStartYear.options[g_pFormStandardTab.WBC_selStartYear.options.length] = new Option(nFullYear, nFullYear, false, true);
		g_pFormStandardTab.WBC_selStartYear.selectedIndex = g_pFormStandardTab.WBC_selStartYear.options.length - 1;
	}
	
	g_pFormStandardTab.WBC_selStartMonth.selectedIndex = pDate.getMonth();
	g_pFormStandardTab.WBC_selStartDay.selectedIndex = pDate.getDate() - 1;
	if (pDate.getHours() < 12) {
		g_pFormStandardTab.WBC_selStartHours.selectedIndex = pDate.getHours();
		g_pFormStandardTab.WBC_selStartAmPm.selectedIndex = 0;
	} else {
		g_pFormStandardTab.WBC_selStartHours.selectedIndex = pDate.getHours() - 12;
		g_pFormStandardTab.WBC_selStartAmPm.selectedIndex = 1;
	}
	
	g_pFormStandardTab.WBC_selStartMinutes.selectedIndex = pDate.getMinutes();
}


// Update UI expiry date
function WBC_updateUIExpiryDate() {
	
	var pDate = new Date(g_pradioExpiryDate[ID_RADIOBTN_EXPIRYDATE_INPUT].value);
	if (pDate.getTime() == DATE_NEVER.getTime()) {
		g_pradioExpiryDate[ ID_RADIOBTN_EXPIRYDATE_NEVER ].checked = true;
		pDate = new Date();
	} else {
		g_pradioExpiryDate[ ID_RADIOBTN_EXPIRYDATE_INPUT ].checked = true;
		pDate = WBC_convertUTC2Local(pDate);
	}
	var nFullYear = WBC_fixJavaScriptYear(pDate.getYear());
	var bYearFound = false;
	
	//	Match Start Year with option list	
	for (var i=0; i<g_pFormStandardTab.WBC_selExpiryYear.options.length; i++) {
		if (g_pFormStandardTab.WBC_selExpiryYear.options[i].value == nFullYear) {
			g_pFormStandardTab.WBC_selExpiryYear.selectedIndex = i;
			bYearFound = true;
			break;
		}
	}

	//	If Start Year is not in option list, append it to the option list
	if (!bYearFound) {
		g_pFormStandardTab.WBC_selExpiryYear.options[g_pFormStandardTab.WBC_selExpiryYear.options.length] = new Option(nFullYear, nFullYear, false, true);
		g_pFormStandardTab.WBC_selExpiryYear.selectedIndex = g_pFormStandardTab.WBC_selExpiryYear.options.length - 1;
	}
	
	g_pFormStandardTab.WBC_selExpiryMonth.selectedIndex = pDate.getMonth();
	g_pFormStandardTab.WBC_selExpiryDay.selectedIndex = pDate.getDate() - 1;
	if (pDate.getHours() < 12) {
		g_pFormStandardTab.WBC_selExpiryHours.selectedIndex = pDate.getHours();
		g_pFormStandardTab.WBC_selExpiryAmPm.selectedIndex = 0;
	} else {
		g_pFormStandardTab.WBC_selExpiryHours.selectedIndex = pDate.getHours() - 12;
		g_pFormStandardTab.WBC_selExpiryAmPm.selectedIndex = 1;
	}
	
	g_pFormStandardTab.WBC_selExpiryMinutes.selectedIndex = pDate.getMinutes();
}


//	--- [Private] Move up the content of the listview in Custom tab
function WBC_doListViewScrollerUp() {
	if (window.document.readyState == "loading") {
		alert("Please wait until the page is finished loading.");
	}
	
	// amount is number of pixels to scroll up
	var amount = 40;
	var pList = document.all['WBCCustomTabInnerList'];
		
	pList.style.pixelTop += amount;
	if (pList.style.pixelTop > 0) {
		pList.style.pixelTop = 0;
	}
	
	if (sRepeat == null) {
		sRepeat = setInterval("WBC_doListViewScrollerUp()",100);
	}
	 
	return false;
}

//	--- [Private] Move down the content of the listview in Custom tab
function WBC_doListViewScrollerDown() 
{
	if (window.document.readyState == "loading") {
		alert("Please wait until the page is finished loading.");
	}
	
	// amount is number of pixels to scroll down
	var amount = 40;
	var pList = document.all['WBCCustomTabInnerList'];
		
	pList.style.pixelTop -= amount;
	if (-pList.style.pixelTop >= pList.offsetHeight - pList.offsetParent.offsetHeight) {
		pList.style.pixelTop = -pList.offsetHeight + pList.offsetParent.offsetHeight;
	}
	
	if (sRepeat == null) {
		sRepeat = setInterval("WBC_doListViewScrollerDown()",100);
	}
	 
	return false;
}


//	Write Start Date when user does not have rights to schedule 
function WBC_writeStartDate( strDate ) 
{
	var pDate = new Date(strDate);
	pDate = WBC_convertUTC2Local(pDate);
	WBC_writeDate(pDate);
}


//	Write Expiry Date when user does not have rights to schedule 
function WBC_writeExpiryDate( strDate ) 
{
	var pDate = new Date(strDate); 
	if (pDate == DATE_NEVER) {
		document.write(IDS_NEVER);
	} else {
		pDate = WBC_convertUTC2Local(pDate);
		WBC_writeDate(pDate);
	}
}


// Retrieve and verify Start Date from UI elements. Convert Date to GMT.
function WBC_verifyUIStartDate() {
	var pDate;
	if (g_pradioStartDate[ID_RADIOBTN_STARTDATE_IMMEDIATELY].checked) {
		pDate = new Date();
		return WBC_convertLocal2UTC(pDate);
	} else {
		var nYear = g_pFormStandardTab.WBC_selStartYear.options[g_pFormStandardTab.WBC_selStartYear.selectedIndex].value;
		var nMonth = g_pFormStandardTab.WBC_selStartMonth.options[g_pFormStandardTab.WBC_selStartMonth.selectedIndex].value - 1;
		var nDay = g_pFormStandardTab.WBC_selStartDay.options[g_pFormStandardTab.WBC_selStartDay.selectedIndex].value;
		if (g_pFormStandardTab.WBC_selStartAmPm.selectedIndex == 0) {
			var nHours = g_pFormStandardTab.WBC_selStartHours.options[g_pFormStandardTab.WBC_selStartHours.selectedIndex].value;
			if (nHours == 12) {
				nHours = 0;
			}
		} else {
			var nHours = parseInt(g_pFormStandardTab.WBC_selStartHours.options[g_pFormStandardTab.WBC_selStartHours.selectedIndex].value) + 12;
			if (nHours == 24) {
				nHours = 12;
			}		
		}
		var nMinutes = g_pFormStandardTab.WBC_selStartMinutes.options[g_pFormStandardTab.WBC_selStartMinutes.selectedIndex].value;
		pDate = new Date(nYear, nMonth, nDay, nHours, nMinutes, 0, 0);
		if ((nYear == WBC_fixJavaScriptYear(pDate.getYear())) && (nMonth == pDate.getMonth()) && (nDay == pDate.getDate())) {
			return WBC_convertLocal2UTC(pDate);
		} else {
			alert(IDS_INVALID_START_DATE);
			return null;
		}
	}
}

// Retrieve and verify Expiry Date from UI elements. Convert Date to GMT.
function WBC_verifyUIExpiryDate() {
	if (g_pradioExpiryDate[ID_RADIOBTN_EXPIRYDATE_NEVER].checked) {
		return DATE_NEVER;
	} else {
		var nYear = g_pFormStandardTab.WBC_selExpiryYear.options[g_pFormStandardTab.WBC_selExpiryYear.selectedIndex].value;
		var nMonth = g_pFormStandardTab.WBC_selExpiryMonth.options[g_pFormStandardTab.WBC_selExpiryMonth.selectedIndex].value - 1;
		var nDay = g_pFormStandardTab.WBC_selExpiryDay.options[g_pFormStandardTab.WBC_selExpiryDay.selectedIndex].value;
		if (g_pFormStandardTab.WBC_selExpiryAmPm.selectedIndex == 0) {
			var nHours = g_pFormStandardTab.WBC_selExpiryHours.options[g_pFormStandardTab.WBC_selExpiryHours.selectedIndex].value;
			if (nHours == 12) {
				nHours = 0;
			}
		} else {
			var nHours = parseInt(g_pFormStandardTab.WBC_selExpiryHours.options[g_pFormStandardTab.WBC_selExpiryHours.selectedIndex].value) + 12;
			if (nHours == 24) {
				nHours = 12;
			}	
		}
		var nMinutes = g_pFormStandardTab.WBC_selExpiryMinutes.options[g_pFormStandardTab.WBC_selExpiryMinutes.selectedIndex].value;
		var pDate = new Date(nYear, nMonth, nDay, nHours, nMinutes, 0, 0);
		if ((nYear == WBC_fixJavaScriptYear(pDate.getYear())) && (nMonth == pDate.getMonth()) && (nDay == pDate.getDate())) {
			return WBC_convertLocal2UTC(pDate);
		} else {
			alert(IDS_INVALID_EXPIRY_DATE);
			return null;
		}
		alert("Year=" + pDate.getYear());
	}
}


// Open the Channel Modify Dialog
function WBC_onClickChannelModifyDialog( nEopIndex ) {
	var strModifyDlgURL;
	
	// store the current selected Channel EOP properties index number
	g_nSelectedEopIndex = nEopIndex;
	
	if (g_bCanSetSharedProperties) {
		strModifyDlgURL = IDS_WIN_CHANNEL_MODIFYDIALOG_URL + "?" + IDS_QPARAM_PAGE_CHARSET + "=" + g_strCharSet;
		
		// Open the new window for Modify Dialog
		if (WBC_getEopType(nEopIndex).toLowerCase() == IDS_EOP_SELECTION_TYPE) {
	
			// the current Channel EOP is a list type
			WBC_openWindowOnFocus(strModifyDlgURL, IDS_WIN_CHANNEL_MODIFYDIALOG_NAME, IDS_WIN_CHANNEL_MODIFYDIALOG_LIST_FEATURES);
		} else {
	
			// the current Channel EOP is a text type
			WBC_openWindowOnFocus(strModifyDlgURL, IDS_WIN_CHANNEL_MODIFYDIALOG_NAME, IDS_WIN_CHANNEL_MODIFYDIALOG_TEXT_FEATURES);
		}
	} else {
		strModifyDlgURL = IDS_WIN_READ_EOP_VALUE_URL + "?" + IDS_QPARAM_PAGE_CHARSET + "=" + g_strCharSet;
		WBC_openWindowOnFocus(strModifyDlgURL, IDS_WIN_READ_EOP_VALUE_NAME, IDS_WIN_READ_EOP_VALUE_FEATURES);
	}
}


// Open the Channel Modify Dialog
function WBC_onClickPostingModifyDialog( nEopIndex ) {
	var strModifyDlgURL;

	// store the current selected Channel EOP properties index number
	g_nSelectedEopIndex = nEopIndex;
	
	if (g_bCanSetSharedProperties) {
		strModifyDlgURL = IDS_WIN_POSTING_MODIFYDIALOG_URL + "?" + IDS_QPARAM_PAGE_CHARSET + "=" + g_strCharSet;
		
		// Open the new window for Modify Dialog
		if (WBC_getEopType(nEopIndex).toLowerCase() == IDS_EOP_SELECTION_TYPE) {

			// the current Channel EOP is a list type
			WBC_openWindowOnFocus(strModifyDlgURL, IDS_WIN_POSTING_MODIFYDIALOG_NAME, IDS_WIN_POSTING_MODIFYDIALOG_LIST_FEATURES);
		} else {
	
			// the current Channel EOP is a text type
			WBC_openWindowOnFocus(strModifyDlgURL, IDS_WIN_POSTING_MODIFYDIALOG_NAME, IDS_WIN_POSTING_MODIFYDIALOG_TEXT_FEATURES);
		}
	} else {
		strModifyDlgURL = IDS_WIN_READ_EOP_VALUE_URL + "?" + IDS_QPARAM_PAGE_CHARSET + "=" + g_strCharSet;
		WBC_openWindowOnFocus(strModifyDlgURL, IDS_WIN_READ_EOP_VALUE_NAME, IDS_WIN_READ_EOP_VALUE_FEATURES);	
	}
}


// Retrieve the selected EOP index number
function WBC_getSelectedEopIndex() {
	return g_nSelectedEopIndex;
}


// Retrieve the selected EOP name
function WBC_getEopName( nEopIndex ) {
	return g_pFormCustomTab.elements[IDS_EOP_PREFIX_NAME + nEopIndex + "_name"].value;
}


// Retrieve the selected EOP type
function WBC_getEopType( nEopIndex ) {
	return g_pFormCustomTab.elements[IDS_EOP_PREFIX_NAME + nEopIndex + "_type"].value;
}


// Retrieve the selected EOP value
function WBC_getEopValue( nEopIndex ) {
	return g_pFormCustomTab.elements[IDS_EOP_PREFIX_NAME + nEopIndex + "_value"].value;
}


// Retrieve the selected EOP list allowable value in a string as this format
// (listvalue1<_NCOMPASSEOPTAG>listvalue2<_NCOMPASSEOPTAG> .... listvalueN)
function WBC_getEopList( nEopIndex ) {
	return g_pFormProperties.elements[IDS_EOP_PREFIX_NAME + nEopIndex + "_listvalue"].value;
}


// Retrieve the selected Page EOP default value
function WBC_getEopDefaultValue( nEopIndex ) {
	return g_pFormCustomTab.elements[IDS_EOP_PREFIX_NAME + nEopIndex + "_defaultvalue"].value;
}


// Retrieve the Page/Channel name
function WBC_getPageName() {
	return g_pFormProperties.elements['WBC_intxtName'].value;
}

// Return True, if the Page EOP is default value. Otherwise; returns False
function WBC_isEopUsesDefaultValue( nEopIndex ) {
	return g_pFormCustomTab.elements[IDS_EOP_PREFIX_NAME + nEopIndex + "_isdefault"].value.toLowerCase();
}


// Return True, if the pObject.CanSetPropeties is true. Otherwise, returns False
function WBC_isCanSetProperties() {
	return g_bCanSetSharedProperties;
}


// Return True, if the pObject.CanSetSchedult is true. Otherwise, returns False
function WBC_isCanSetSchedule() {
	return g_bCanSetLocalProperties;
}


// Update the EOP value in Custom Tab after the user modifies the EOP value through
// the user select option in modify dialog.
function WBC_updateUIEopUsesSelectedValue( nEopIndex, strEopValue ) {
	var strEopId = IDS_EOP_PREFIX_NAME + nEopIndex;
	if (g_bCanSetSharedProperties) {
	
		// update the EOP value if the value is changed
		if (strEopValue != g_pFormCustomTab.elements[strEopId + "_value"].value) {
			g_pFormCustomTab.elements[strEopId + "_value"].value = strEopValue;
			g_pFormCustomTab.elements[strEopId + "_label"].value = strEopValue;
			
			// set the default value is false if the original value is true.
			// and update the UI default image
			if (g_pFormCustomTab.elements[strEopId + "_isdefault"]) {
				g_pFormCustomTab.elements[strEopId + "_isdefault"].value = false;
				g_pFormCustomTab.elements[strEopId+ "_image"].src = IDS_TRANSPARENT_GIF;
			}
			
			// add to the g_strModifiedPropsXML if the data is changed
			WBC_addToModifiedEopIndex(nEopIndex);
		} else {
		
			// The user select the user select option so set the default value is false.
			if (g_pFormCustomTab.elements[strEopId + "_isdefault"]) {
				if (g_pFormCustomTab.elements[strEopId + "_isdefault"].value.toLowerCase() == "true") {
					g_pFormCustomTab.elements[strEopId + "_isdefault"].value = false;
					g_pFormCustomTab.elements[strEopId+ "_image"].src = IDS_TRANSPARENT_GIF;
					g_pFormCustomTab.elements[strEopId + "_value"].value = g_pFormCustomTab.elements[strEopId + "_defaultvalue"].value;
					g_pFormCustomTab.elements[strEopId + "_label"].value = g_pFormCustomTab.elements[strEopId + "_defaultvalue"].value;
			
					// add to the g_strModifiedPropsXML if the data is changed
					WBC_addToModifiedEopIndex(nEopIndex);
				}
			}
		}
	}
}


// Update the EOP value in Custom Tab after the user modifies the EOP value through
// the user default option in modify dialog.
function WBC_updateUIEopUsesDefaultValue( nEopIndex ) {
	var strEopId = IDS_EOP_PREFIX_NAME + nEopIndex;
	if (g_bCanSetSharedProperties) {
		if (g_pFormCustomTab.elements[strEopId + "_isdefault"].value.toLowerCase() != "true") {
			g_pFormCustomTab.elements[strEopId + "_isdefault"].value = true;
			g_pFormCustomTab.elements[strEopId + "_image"].src = IDS_IMG_ICON_YES;
			g_pFormCustomTab.elements[strEopId + "_value"].value = g_pFormCustomTab.elements[strEopId + "_defaultvalue"].value;
			g_pFormCustomTab.elements[strEopId + "_label"].value = g_pFormCustomTab.elements[strEopId + "_defaultvalue"].value;
		
			// add to the g_strModifiedPropsXML if the data is changed
			WBC_addToModifiedEopIndex(nEopIndex);
		}
	}
}


// reset the display name as object name when the user click the reset button.
function WBC_resetDisplayName() {
	if (g_bCanSetLocalProperties) {
		if (g_pFormProperties.elements['GeneralPropertiesControl_WBC_intxtDisplayName'].value != g_pFormProperties.elements['GeneralPropertiesControl_WBC_intxtName'].value) {
			g_pFormProperties.elements['GeneralPropertiesControl_WBC_intxtDisplayName'].value = g_pFormProperties.elements['GeneralPropertiesControl_WBC_intxtName'].value;
		}
	}
}


// Transfer Update all properties values to the hidden field in Submit form 
function WBC_createModifiedPropsToSubmitForm() {

	// update the start date, expiry date, display name and native flags
	// based on ROM API "CanSetLocalProperties"
	if (g_bCanSetLocalProperties) {
		// add modify date to submit form if the date is changed
		WBC_handleInputDateField(g_pFormStandardTab, g_pradioStartDate, IDS_XML_STARTDATE);
		WBC_handleInputDateField(g_pFormStandardTab, g_pradioExpiryDate, IDS_XML_EXPIRYDATE);
	
		// add modify properties to submit form if the value is changed
		WBC_handleInputTextField(g_pFormProperties, "WBC_intxtDisplayName", IDS_XML_DISPLAYNAME);
		WBC_handleInputCheckBoxField(g_pFormStandardTab, "WBC_inchkIsImportant", IDS_XML_ISIMPORTANT);
		WBC_handleInputCheckBoxField(g_pFormStandardTab, "WBC_inchkIsRobotFollowable", IDS_XML_ISROBOTFOLLOW);
		WBC_handleInputCheckBoxField(g_pFormStandardTab, "WBC_inchkIsRobotIndexable", IDS_XML_ISROBOTINDEX);
		WBC_handleInputCheckBoxField(g_pFormStandardTab, "WBC_inchkIsHiddenModePublished", IDS_XML_ISHIDDENMODE);
	}
		
	// update the name, Description and EOP base on ROM API "CanSetSharedProperties" properties
	if (g_bCanSetSharedProperties) {
		WBC_handleInputTextField(g_pFormProperties, "WBC_intxtName", IDS_XML_NAME);
		WBC_handleInputTextField(g_pFormStandardTab, "WBC_txtareaDescription", IDS_XML_DESCRIPTION);
		WBC_handleInputEopsField(g_pFormCustomTab);
	}
	
	// if g_strModifiedPropsXML is empty, this means nothing change; therefore,
	// close the properties dialog without saving anything.
	if (g_strModifiedPropsXML == "") {
		return false;
	}
	
	// set the g_strModifiedPropsXML value to the submit form in order to submit the 
	// changed properties value to the server.
	g_pFormProperties.elements['WBC_inhidProperties'].value = WBC_wrapXMLTag(IDS_XML_PROPS, g_strModifiedPropsXML, WBC_IsWrapCData(IDS_XML_PROPS));
	
	return true;
}

// Update a start or expiry date value to the hidden field in Submit form
function WBC_handleInputDateField( pSrcForm, pDateArray, strXMLTagName ) {
	var pDate;
	if (pDateArray[ ID_RADIOBTN_IMMEDIATELY_OR_NEVER ].checked) {
		pDate = pDateArray[ ID_RADIOBTN_IMMEDIATELY_OR_NEVER ];
	} else {
		pDate = pDateArray[ ID_RADIOBTN_DATE_INPUT ];
	}
	
	// get the original date value and the new setting date
	var pOriginalDate = new Date(pSrcForm.elements[pDate.name + "_original"].value);
	var pNewDate = new Date(pDate.value);
	
	// add to the g_strModifiedPropsXML if the date is changed
	if (pOriginalDate.getTime() != pNewDate.getTime()) {
		WBC_addModifiedDateToXML(strXMLTagName, pNewDate);
	}
}


//	Construct XML for one EOP element
function WBC_addModifiedDateToXML( strXMLTagName, pDate ) {

	// construct each XML Date elements such as <YEAR>, <MONTH>, <DAY>, <HOUR>, <MINUTE> and <SECOND>
	var strXMLDateYear = WBC_wrapXMLTag(IDS_XML_YEAR, pDate.getFullYear(), WBC_IsWrapCData(IDS_XML_YEAR));
	var strXMLDateMonth = WBC_wrapXMLTag(IDS_XML_MONTH, pDate.getMonth() + 1, WBC_IsWrapCData(IDS_XML_MONTH));
	var strXMLDateDay = WBC_wrapXMLTag(IDS_XML_DAY, pDate.getDate(), WBC_IsWrapCData(IDS_XML_DAY));
	var strXMLDateHour = WBC_wrapXMLTag(IDS_XML_HOUR, pDate.getHours(), WBC_IsWrapCData(IDS_XML_HOUR));
	var strXMLDateMinute = WBC_wrapXMLTag(IDS_XML_MINUTE, pDate.getMinutes(), WBC_IsWrapCData(IDS_XML_MINUTE));
	var strXMLDateSecond = WBC_wrapXMLTag(IDS_XML_SECOND, pDate.getSeconds(), WBC_IsWrapCData(IDS_XML_SECOND));
	
	// construct the XML Date, For example
	// <STARTDATE>
	//		<YEAR>2001</YEAR>
	//		<MONTH>2</MONTH>
	//		<DAY>28</DAY>
	//		<HOUR>4</HOUR>
	//		<MINUTE>40</MINUTE>
	//		<SECOND>50</SECOND>
	// </STARTDATE>	
	var strDateXML = strXMLDateYear + strXMLDateMonth + strXMLDateDay + strXMLDateHour + strXMLDateMinute + strXMLDateSecond;
	
	// concat to the final XML string
	WBC_addModifiedPropsToXML(strXMLTagName, strDateXML);
}


// Update the possible changed value of any input box or text area to hidden field in 
// Submit form such as "Name", "Display Name" and "Description"
function WBC_handleInputTextField( pSrcForm, strPropsName, strXMLTag ) {

	if (pSrcForm.elements[strPropsName]) {
		// add to the g_strModifiedPropsXML if the data is changed
		if (pSrcForm.elements[strPropsName + "_original"].value != pSrcForm.elements[strPropsName].value) {
			// add Hidden Value
			WBC_addModifiedPropsToXML(strXMLTag, pSrcForm.elements[strPropsName].value);
		}
	} else {
		alert( IDS_UI_ERROR_MISSING_ELEMENT );
	}
}


// handle the possible changed value of any check box such as 
// "Important Page/Channel", "Web Robots Can Crawl Links", "Web Robots Can Index This Page"
// "Web Robots Can Index This Channel's Navigation" and "Hidden When Published"
function WBC_handleInputCheckBoxField( pSrcForm, strPropsName, strXMLTag ) {

	if (pSrcForm.elements[strPropsName]) {
		if (pSrcForm.elements[strPropsName].type == IDS_ELEMENT_TYPE_CHECKBOX) {

			// get the new value of the checkbox
			var strChecked = "false";
			if (pSrcForm.elements[strPropsName].checked) {
				strChecked = "true";
			}
			
			// add to the g_strModifiedPropsXML if the checkbox value is changed
			if (pSrcForm.elements[strPropsName + "_original"].value.toLowerCase() != strChecked) {	
				WBC_addModifiedPropsToXML(strXMLTag, pSrcForm.elements[strPropsName].checked);
			}
		} 
	}
}


//	Construct XML for one EOP element
function WBC_addEachModifiedEopToXML( strEopName, strEopValue, strEopDefault ) {

	// construct each XML EOP elements such as <EOPNAME>, <EOPVALUE> and <EOPDEFAULT>
	var strXMLEopName = WBC_wrapXMLTag(IDS_XML_EOPNAME, strEopName, WBC_IsWrapCData(IDS_XML_EOPNAME));
	var strXMLEopValue = WBC_wrapXMLTag(IDS_XML_EOPVALUE, strEopValue, WBC_IsWrapCData(IDS_XML_EOPVALUE));
	var strXMLEopDefault =  WBC_wrapXMLTag(IDS_XML_EOPDEFAULT, strEopDefault, WBC_IsWrapCData(IDS_XML_EOPDEFAULT));
	
	// construct the XML with node with CDATA, For example
	// <EOP>
	//		<EOPNAME><![CDATA[my eop name]]></EOPNAME>
	//		<EOPVALUE><![CDATA[my eop value]]></EOPVALUE>
	//		<EOPDEFAULT>false</EOPDEFAULT>
	// </EOP>	
	
	// return the Eop XML
	return WBC_wrapXMLTag(IDS_XML_EOP, strXMLEopName + strXMLEopValue + strXMLEopDefault, WBC_IsWrapCData(IDS_XML_EOP));
}


// Construct XML for one element which is other than EOP
function WBC_addModifiedPropsToXML(strXMLTagName, strPropsValue) {
	bCData = WBC_IsWrapCData(strXMLTagName);

	// construct the XML with node with CDATA (e.g., <NAME><![CDATA[my channel]]></NAME> )
	var strXML = WBC_wrapXMLTag(strXMLTagName, strPropsValue, bCData);
	
	// concat to the g_strModifiedPropsXML
	g_strModifiedPropsXML = g_strModifiedPropsXML + strXML;
}


// wrap the value with XML tag. If bCData = true, then wrap with CDATA; otherwise; don't wrap CDATA.
// By Wrapping CDATA with the value in XML, it instructs the XML DOM parser to ignore most markup characters.
// Therefore, the user can type in the text even though it is same as XML tag.
// For example, the user type "</EOP> this is my eop value <EOP>" into the EOP input text area. Since this value
// is wrapped by CDATA. After submit the form by XML and parsing the XML, any charater between the start of the CDATA section, 
// <![CDATA[ and the end of the section, ]]> is passed directly to server, without interpretation.
function WBC_wrapXMLTag(strXMLTagName, strXMLTagValue, bCData) {
	if (bCData) {
	
		// wrap with CDATA
		return "<" + strXMLTagName + ">" + IDS_XML_CDATA_BEGIN + strXMLTagValue + IDS_XML_CDATA_END + "</" + strXMLTagName + ">";
	} else {
	
		// don't wrap with CDATA
		return "<" + strXMLTagName + ">" + strXMLTagValue + "</" + strXMLTagName + ">";
	}
}


// Define a set of Tag which needs to wrap with CDATA.
// Note: Whenever a new XML tag must define here if it needs to wrap with CDATA.
// Right now, only the "Name", "Display Name", "Description", "Eop Name" and "Eop Value" wrap with CDATA
function WBC_IsWrapCData(strXMLTag) {
	if ((strXMLTag == IDS_XML_NAME) || (strXMLTag == IDS_XML_DISPLAYNAME) || (strXMLTag == IDS_XML_DESCRIPTION) || (strXMLTag == IDS_XML_EOPNAME) || (strXMLTag == IDS_XML_EOPVALUE)) {
		return true;
	} else {
		return false;
	}	
}


// add the EOP index into the array "g_arrayModifiedEopIndex" in order to 
// keep check which EOP has been changed. 
// Note: This array doesn't allow duplicate value.
function WBC_addToModifiedEopIndex( nEopIndex ) {
	if (!WBC_isExistInModifiedEopIndex(nEopIndex)) {
		g_arrayModifiedEopIndex[g_arrayModifiedEopIndex.length] = nEopIndex;
	}
}


// Check whether the EOP index is in the array "g_arrayModifiedEopIndex" or not.
// If exist in array, don't add to the array. Otherwise, add the index to the array.
function WBC_isExistInModifiedEopIndex( nEopIndex ) {
	for (var i=0; i<g_arrayModifiedEopIndex.length; i++) {
      if (g_arrayModifiedEopIndex[i] == nEopIndex) {
		return true;
      }
	}
	return false; 
}

// Update the possible changed value of Eop to hidden field in 
// Submit form such as "Name", "Display Name" and "Description"
function WBC_handleInputEopsField( pSrcForm ) {
	var strEopId;
	var strEopName;
	var strEopValue;
	var strEopDefault;
	var strModifiedEopXML ="";
	
	// Check the each number in the array "g_arrayModifiedEopIndex" in order
	// to know which EOP has been changed. For each modified EOP, retrieve its
	// name, value, default boolean from a set hidden field. For example, if 
	// the EOP index is 3, the assicated hidden field is constructed as following
	// when the HTML is loaded.
	//			- Eop name is in the "WBC_intxtEop_3_name" hidden field
	//			- Eop value is in the "WBC_intxtEop_3_value" hidden field
	//			- Eop default is in the "WBC_intxtEop_3_isdefault" hidden field
	
	// start to check each modified Eop
	for (var i = 0; i < g_arrayModifiedEopIndex.length; i++) {
		strEopId = IDS_EOP_PREFIX_NAME + g_arrayModifiedEopIndex[i];
	
		// get the EOP name from hidden field "WBC_intxtEop_i_name"
		strEopName = pSrcForm.elements[strEopId + "_name"].value;
		
		// get the EOP value from hidden field "WBC_intxtEop_i_value" "
		// and default boolean if exist from hidden field "WBC_intxtEop_i_isdefault"
		if (!pSrcForm.elements[strEopId + "_isdefault"]) {
		
			// the field "WBC_intxtEop_i_isdefault" doesn't exist. So, this is a channel properties. 
			// There is not default value for Channel. Just set this strEopDefault to false in channel case.
			strEopDefault = "false";
			strEopValue = pSrcForm.elements[strEopId + "_value"].value;
		} else {
		
			// the field "WBC_intxtEop_i_isdefault: does exist. This isn't a Channel and is a Page.
			strEopDefault = pSrcForm.elements[strEopId + "_isdefault"].value;
			if (strEopDefault == "true") {
				strEopValue = "";
			} else {
				strEopValue = pSrcForm.elements[strEopId + "_value"].value;
			}
		}
		
		// concat to the strModifiedPropsXML   
		strModifiedEopXML = strModifiedEopXML + WBC_addEachModifiedEopToXML(strEopName, strEopValue, strEopDefault);
	}
	
	if (strModifiedEopXML != "") {
		g_strModifiedPropsXML = g_strModifiedPropsXML + WBC_wrapXMLTag(IDS_XML_EOPS, strModifiedEopXML, WBC_IsWrapCData(IDS_XML_EOPS));
	}
}
 
// Transfer all additional properties values to the hidden field in Submit form.
// Additional properties are properties in additional forms. They are forms added to the
// preset forms.  
function WBC_transferAdditionalPropsToSubmitForm() {

	var arrayForms = new Array();
	var pTabLayer, pTabForm, pTabElement;
	var pAdditionalProperties = new Object();
	var i = 0;
	var j = 0;
	
	if (WBC_isIE()) {
		arrayForms = window.document.forms;
	} else {
		for (i = 0; i < window.document.layers.length; i++) {
			pTabLayer = window.document.layers[i];
			if (pTabLayer.document.forms.length > 0) {
				arrayForms[j] = pTabLayer.document.forms[0];
				j++;
			}
		}
	}
	
	// Loop through all the forms to retrieve additional forms.
	// alert(document.forms.length);
	for (i = 0; i < arrayForms.length; i++) {
		pTabForm = arrayForms[i];
		// Do not transfer properties if the form is one of the preset forms
		if (!WBC_isPresetForm(pTabForm.name)) {
			for (j = 0; j < pTabForm.elements.length; j++) {
				pTabElement = pTabForm.elements[j];
				WBC_setAdditionalFormElements(pAdditionalProperties, pTabElement);
			}
		}
	}
	
	return WBC_transferAdditionalFormElements(pAdditionalProperties, document.WBC_formProperties);
}

// Test if the form is one of the preset forms
function WBC_isPresetForm(strFormName) {
	var arraypresetForms = new Array("WBC_formProperties", "WBC_formStandardProps", "WBC_formCustomProps");
	for (var i = 0; i < arraypresetForms.length; i++) {
		if (arraypresetForms[i] == strFormName) {
			return true;
		}
	}
	return false;
}

// Set additional form elements value
function WBC_setAdditionalFormElements(pAdditionalProperties, pFromFormElement)  {

	var strToElementValue = "";
	var nSelectedIndex;
	var strFromElementValue;
	
	if (pFromFormElement.type == "select-one") {
		nSelectedIndex = pFromFormElement.selectedIndex;
		if (nSelectedIndex != -1) {
			strToElementValue = pFromFormElement.options[nSelectedIndex].value;
		}
	} else if (pFromFormElement.type == "select-multiple") {
		nSelectedIndex = pFromFormElement.selectedIndex;
		if (nSelectedIndex != -1) {
			for (var i = 0; i < pFromFormElement.length; i++) {
				if (pFromFormElement.options[i].selected) {
					strFromElementValue = pFromFormElement.options[i].value;
					if (strToElementValue == "") {
						strToElementValue = strFromElementValue;
					} else {
						strToElementValue = strToElementValue + ", " + strFromElementValue;
					}
				}
			}
		}
	} else if (pFromFormElement.type == "checkbox" || pFromFormElement.type == "radio") {
		if (pFromFormElement.checked) {
			if (pFromFormElement.value == "") {
				strToElementValue = "on";
			} else {
				strToElementValue = pFromFormElement.value;
			}
		}
	} else {
		strToElementValue = pFromFormElement.value;
	}
	
	var strElementName = pFromFormElement.name;
	var strAdditionalElementValue = pAdditionalProperties[strElementName];
	if (typeof(strAdditionalElementValue) == "undefined" || strAdditionalElementValue == "") {
		pAdditionalProperties[strElementName] = strToElementValue;
	} else {
		if (strToElementValue != "") {
			pAdditionalProperties[strElementName] = strAdditionalElementValue + ", " + strToElementValue;
		}
	}
}

// Transfer additional form elements value to submit form
function WBC_transferAdditionalFormElements(pAdditionalProperties, pFormProperties) {

	var btransferAdditionalFormElements = false;
	var strElementName;
	
	for (strElementName in pAdditionalProperties) {
		if (pFormProperties.elements[strElementName]) {
			pFormProperties.elements[strElementName].value = pAdditionalProperties[strElementName];
			btransferAdditionalFormElements = true;
		}
	}
	return btransferAdditionalFormElements;
}

