// ********************************************************************
// 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 a variable for form
var g_pFormViewRevisionByDate;
		
// initial the UI for PageRevisionPreviewByDateDlg.asp
function WBC_onLoadPreviewDlg() {
	// get the form object for this page
	g_pFormViewRevisionByDate = window.document.WBC_formViewRevisionByDate;
			
	// set the date is today.
	var pDate = new Date();
	var nFullYear = WBC_fixJavaScriptYear( pDate.getYear() );
	var bYearFound = false;
			
	//	Match Start Year with option list	
	for (var i=0; i<g_pFormViewRevisionByDate.WBC_selYear.options.length; i++) {
		if (g_pFormViewRevisionByDate.WBC_selYear.options[i].value == nFullYear) {
			g_pFormViewRevisionByDate.WBC_selYear.selectedIndex = i;
			bYearFound = true;
			break;
		}
	}

	//	If Start Year is not in option list, append it to the option list
	if (!bYearFound) {
		g_pFormViewRevisionByDate.WBC_selYear.options[g_pFormViewRevisionByDate.WBC_selYear.options.length] = new Option( nFullYear, nFullYear, false, true );
		g_pFormViewRevisionByDate.WBC_selYear.selectedIndex = g_pFormViewRevisionByDate.WBC_selYear.options.length - 1;
	}
			
	g_pFormViewRevisionByDate.WBC_selMonth.selectedIndex = pDate.getMonth();
	g_pFormViewRevisionByDate.WBC_selDay.selectedIndex = pDate.getDate() - 1;
	if (pDate.getHours() < 12) {
		g_pFormViewRevisionByDate.WBC_selHours.selectedIndex = pDate.getHours();
		g_pFormViewRevisionByDate.WBC_selAmPm.selectedIndex = 0;
	} else {
		g_pFormViewRevisionByDate.WBC_selHours.selectedIndex = pDate.getHours() - 12;
		g_pFormViewRevisionByDate.WBC_selAmPm.selectedIndex = 1;
	}
			
	g_pFormViewRevisionByDate.WBC_selMinutes.selectedIndex = pDate.getMinutes();
}

// Retrieve and verify Start Date from UI elements. Convert Date to GMT.
function WBC_verifyUISelectedDate() {
	var pDate;
			
	var nYear = g_pFormViewRevisionByDate.WBC_selYear.options[g_pFormViewRevisionByDate.WBC_selYear.selectedIndex].value;
	var nMonth = g_pFormViewRevisionByDate.WBC_selMonth.options[g_pFormViewRevisionByDate.WBC_selMonth.selectedIndex].value - 1;
	var nDay = g_pFormViewRevisionByDate.WBC_selDay.options[g_pFormViewRevisionByDate.WBC_selDay.selectedIndex].value;
	if (g_pFormViewRevisionByDate.WBC_selAmPm.selectedIndex == 0) {
		var nHours = g_pFormViewRevisionByDate.WBC_selHours.options[g_pFormViewRevisionByDate.WBC_selHours.selectedIndex].value;
		if (nHours == 12) {
			nHours = 0;
		}
	} else {
		var nHours = parseInt(g_pFormViewRevisionByDate.WBC_selHours.options[g_pFormViewRevisionByDate.WBC_selHours.selectedIndex].value) + 12;
		if (nHours == 24) {
			nHours = 12;
		}		
	}
	var nMinutes = g_pFormViewRevisionByDate.WBC_selMinutes.options[g_pFormViewRevisionByDate.WBC_selMinutes.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;
	}
}

// launch the preview of the Revision Page base on the selected date and time.
function WBC_onClickPreview( strPreviewURL ) {
	var pDate = WBC_verifyUISelectedDate();
	if (pDate == null) {
		return;
	}
	
	// set the hidden field value of the UTC time
	// Note: Have to pass the Year, Month, Day, Hour, Minutes in each hidden field
	// and then reconstruct in the ASP file in order to get the Revision Date. Some
	// Regional Setting doesn't will cause the CDate doesn't work. For example,
	// CDate( "Jan 1, 3000" ) will get error when the server regional = Romanian
	g_pFormViewRevisionByDate.WBC_inhidPreviewDateYear.value = pDate.getFullYear();
	g_pFormViewRevisionByDate.WBC_inhidPreviewDateMonth.value = pDate.getMonth() + 1;
	g_pFormViewRevisionByDate.WBC_inhidPreviewDateDay.value = pDate.getDate();
	g_pFormViewRevisionByDate.WBC_inhidPreviewDateHour.value = pDate.getHours();
	g_pFormViewRevisionByDate.WBC_inhidPreviewDateMinute.value = pDate.getMinutes();
	
	// form submit the preview URL
	WBC_openWindowOnFocus("about:blank", IDS_WIN_PREVIEW, WBC_getPoppedUpWindowFeatures(IDS_WIN_PREVIEW_FEATURES, window.opener.top));
	g_pFormViewRevisionByDate.target = IDS_WIN_PREVIEW;
	g_pFormViewRevisionByDate.action = strPreviewURL;
	g_pFormViewRevisionByDate.submit();
}

// launch the view revison by date dialog
function WBC_openViewRevisionByDateDlg() {

	if (!WBC_isOpenerWindowClosed()) {
		
		var pFuncWin = WBC_getFuncWin("WBC_refreshWindow", top.opener);
		if (pFuncWin != null)
		{
			pFuncWin.WBC_refreshWindow();
		}
	}
	window.close();
}
	