// Copyright 2001, 2002, 2003 Macromedia, Inc. All rights reserved.

var g_actionName = '';

function commandButtons(){
	return new Array(BTN_OK, "okClicked()", BTN_Cancel, "cancelClicked()", BTN_Help, "displayHelp()");
}

function receiveArguments(action)
{
	g_actionName = action;
}	

function initializeUI()
{
	var FSM = FileStateManager.getManager(dw.getDocumentDOM());
	var state = FSM.getState();
	var labelNode = findObject('infoLabel');
	if (g_actionName == 'submit' || g_actionName == 'publish')
	{
		if (state == 'new')
			labelNode.innerHTML = LABEL_NewPage;
		else
			labelNode.innerHTML = LABEL_Draft;
	}
	else if (g_actionName == 'accept')
	{
		labelNode.innerHTML = LABEL_Approve;
	}
	else if (g_actionName == 'return')
	{
		labelNode.innerHTML = LABEL_Return;
	}
}

function okClicked() 
{
	var FSM = FileStateManager.getManager(dw.getDocumentDOM());
	var url = FSM.getCurrentStateURL();
	var notesFile = MMNotes.open(url, true);
	if (notesFile)
	{
		// Increment the last comment number by one.
		var lastCommentNumStr = MMNotes.get(notesFile, "ccNumComments");
		var newCommentNum;
		if (lastCommentNumStr == '')
			newCommentNum = 1;
		else
			newCommentNum = parseInt(lastCommentNumStr) + 1;
		newCommentNumStr = String(newCommentNum);
		MMNotes.set(notesFile, "ccNumComments", newCommentNumStr);
		MMNotes.set(notesFile, "ccCommentEvent" + newCommentNumStr, g_actionName);
		MMNotes.set(notesFile, "ccCommentAuthor" + newCommentNumStr, FSM.debugOnlyGetContributorName());
		MMNotes.set(notesFile, "ccCommentAuthorEmail" + newCommentNumStr, FSM.debugOnlyGetContributorEmail());
		MMNotes.set(notesFile, "ccCommentAuthorIsReviewer" + newCommentNumStr, (FSM.debugOnlyGetReviewer() ? 'true' : 'false'));
		MMNotes.set(notesFile, "ccCommentDate" + newCommentNumStr, getSimpleDateAndTime());
		MMNotes.set(notesFile, "ccCommentContents" + newCommentNumStr, findObject('notesField').value);
		MMNotes.close(notesFile);
	}

	window.close();
}

function cancelClicked() {
	MM.returnCode = false;
	window.close();
}

function getSimpleDateAndTime() {
	// *** TBD: localize time format as well
	var today = new Date();
	var date = createDateFromMask(DATE_Mask);
	var hours = today.getHours();
	var ampm;
	if (hours < 12)
	{
		if (hours == 0)
			hours = 12;
		ampm = 'am';
	}
	else
	{
		if (hours > 12)
			hours -= 12;
		ampm = 'pm';
	}

	var minutes = today.getMinutes();
	var minStr;
	if (minutes < 10)
		minStr = "0" + String(minutes);
	else
		minStr = String(minutes);

	var time = String(hours) + ':' + minStr + ' ' + ampm;
	return (date + ' ' + time);
}

//Renders date (numbers only) as needed. Accepts the following tokens:
//M or MM, D or DD, YY or YYYY. MM means pad with zero. Tokens can be mixed with any punctuation.
//Examples: M/D/YY => 6/25/99  or  YYYY.MM.DD => 1999.06.25

//This is only temporarily in the localized code. Should be moved
//to Shared/MM/Scripts/CMN/dateID.js for the next release.

function createDateFromMask(dateStr) {
  var today = new Date();
  var theYear  = String(today.getFullYear());
  var theMonth = String(Number(today.getMonth())+1);
  var theDate  = String(today.getDate());


  //Replace Year
  dateStr = dateStr.replace(/YYYY/g,theYear);
  dateStr = dateStr.replace(/YY/g,theYear.substring(2));

  //Replace Month
  dateStr = dateStr.replace(/MM/g,((Number(theMonth)<10)?"0":"")+theMonth);
  dateStr = dateStr.replace(/M/g,theMonth);

  //Replace Date
  dateStr = dateStr.replace(/DD/g,((Number(theDate)<10)?"0":"")+theDate);
  dateStr = dateStr.replace(/D/g,theDate);

  return dateStr;
}
