// Copyright 1999-2009 Tyco Electronics Corporation

dojo.provide("mpl.MplPartManage");

dojo.require("te.MplSaveNoteDlg");
dojo.require("te.MplRemoveNoteDlg");
dojo.require("te.MplRemovePartDlg");
dojo.require("dojo.string");

dojo.declare("mpl.MplPartManage", null,
{
	constructor:
		function(/*int*/partIdx, /*String*/requestPartNbr, /*String*/partKeyId, /*String(optional)*/partDesc) {
			this.partIndex = partIdx;
			this.requestPartNbr = requestPartNbr;
			this.partKeyId = partKeyId;
			this.partDesc = partDesc;
			
			this.initialize();
		},
	partIndex: null,
	requestPartNbr: null,
	partKeyId: null,
	partDesc: null,
	noteNode: null,
	partRowNode: null,
	noteNotExistsActionsNode: null,
	noteExistsActionsNode: null,
	actionsOverlayNode: null,
	addNoteDialog: null,
	editNoteDialog: null,
	removeNoteDialog: null,
	removePartDialog: null,
	evt_addNoteDialog_endEditNote_1: null,
	evt_editNoteDialog_endEditNote_1: null,
	evt_removeNoteDialog_endRemoveNote_1: null,
	evt_removePartDialog_endRemove_1: null,
	
	
	initialize: function() {
	
		this.noteNode = dojo.byId("TE_mpl_partNoteTxt_" + this.partIndex);
		this.noteNotExistsActionsNode = dojo.byId("TE_mpl_partNoteNotExists_actions_" + this.partIndex);
		this.noteExistsActionsNode = dojo.byId("TE_mpl_partNoteExists_actions_" + this.partIndex);
		this.actionsOverlayNode = dojo.byId("TE_mpl_actionsOverlay_manage");
		
		/* Add/Edit List Notes */
		var addNoteLink = dojo.byId("TE_mpl_addPartNoteLink_" + this.partIndex);
		var editNoteLink = dojo.byId("TE_mpl_editPartNoteLink_" + this.partIndex);

		var noteTxt = null;
		if (this.noteNode.firstChild) {
			noteTxt = this.noteNode.firstChild.nodeValue;
			noteTxt = dojo.string.trim(noteTxt);
			if (noteTxt.length == 0) {
				noteTxt = null;
			}
		}
		
		this.addNoteDialog = new mpl.MplSaveNoteDlg(null, addNoteLink, TE_mplMessages.mplAddNoteMessage, this.partKeyId, noteTxt,
									dojo.hitch(this, this.showDialogOverlay), dojo.hitch(this, this.hideDialogOverlay), dojo.hitch(this, this.hideError));	
		this.editNoteDialog = new mpl.MplSaveNoteDlg(null, editNoteLink, TE_mplMessages.mplEditNoteMessage, this.partKeyId, noteTxt,
									dojo.hitch(this, this.showDialogOverlay), dojo.hitch(this, this.hideDialogOverlay), dojo.hitch(this, this.hideError));	
									
		this.evt_addNoteDialog_endEditNote_1 = dojo.connect(this.addNoteDialog, "endEditNote", this, "completeSaveNote");
		this.evt_editNoteDialog_endEditNote_1 = dojo.connect(this.editNoteDialog, "endEditNote", this, "completeSaveNote");
		/* Remove Note */
		var removeNoteLink = dojo.byId("TE_mpl_removePartNoteLink_" + this.partIndex);
		this.removeNoteDialog = new mpl.MplRemoveNoteDlg(null, removeNoteLink, this.partKeyId,
									dojo.hitch(this, this.showDialogOverlay), dojo.hitch(this, this.hideDialogOverlay), dojo.hitch(this, this.hideError));	
		this.evt_removeNoteDialog_endRemoveNote_1 = dojo.connect(this.removeNoteDialog, "endRemoveNote", this, "completeRemoveNote");
		
		/* Remove Part */
		this.partRowNode = dojo.byId("TE_mpl_partRow_manage_" + this.partIndex);
		var removePartLink = dojo.byId("TE_mpl_removePartLink_manage_" + this.partIndex);
		this.removePartDialog = new mpl.MplRemovePartDlg(null, removePartLink, this.partKeyId, this.requestPartNbr, this.partDesc,
									dojo.hitch(this, this.showDialogOverlay), dojo.hitch(this, this.hideDialogOverlay), dojo.hitch(this, this.hideError));		
		this.evt_removePartDialog_endRemove_1 = dojo.connect(this.removePartDialog, "endRemove", this, "completeRemovePart");
	},
	
	completeSaveNote: function(status, newNote, errorMsg, errorCode) {
		if (status == "success") {
			if (newNote == ""){
				this.removeNoteFromDisplay();
			} else {
				this.noteNode.innerHTML = newNote;
				this.addNoteDialog.noteTxt = newNote;
				this.editNoteDialog.noteTxt = newNote;
				dojo.style(this.noteNotExistsActionsNode, "display", "none");
				dojo.style(this.noteExistsActionsNode, "display", "");
			}
		} else {
			this.showError(errorMsg, errorCode);
		}	
	},
	
	completeRemoveNote: function(status, errorMsg, errorCode) {
		if (status == "success") {
			this.removeNoteFromDisplay();
		} else {
			this.showError(errorMsg, errorCode);
		}
	},
	
	removeNoteFromDisplay: function () {
		if (this.noteNode.firstChild) {
			this.noteNode.firstChild.nodeValue = "";
		}
		this.addNoteDialog.noteTxt = "";
		this.editNoteDialog.noteTxt = "";
		
		dojo.style(this.noteNotExistsActionsNode, "display", "block"); /*TODO This should just display ""*/
		dojo.style(this.noteExistsActionsNode, "display", "none");	
	},
	
	completeRemovePart: function(errorMsg, errorCode) {
		if (errorCode) {
			this.showError(errorMsg, errorCode);
		} else {
			partCount--;
			saveStateInCookie();
			new dojo.NodeList(this.partRowNode).orphan();
		}
	},
	
	showError: function(errorMsg, errorCode) {
		var errorsNode = dojo.byId("TE_mpl_errors");
		errorsNode.innerHTML = errorMsg;
		dojo.style(dojo.byId("TE_mpl_errorBox"), "display", "");
	},
	
	showDialogOverlay: function() {
		this.actionsOverlayNode.style.height = document.body.offsetHeight + 'px';
		this.actionsOverlayNode.style.width = document.body.offsetWidth + 'px';
		
		this.actionsOverlayNode.style.display = 'block';
	},
	
	hideDialogOverlay: function() {
		dojo.style(this.actionsOverlayNode, "display", "none");	
	},
	
	hideError: function() {
		dojo.style(dojo.byId("TE_mpl_errorBox"), "display", "none");	
	},
	
	destroy: function() {
		dojo.disconnect(this.evt_addNoteDialog_endEditNote_1);	
		dojo.disconnect(this.evt_editNoteDialog_endEditNote_1);	
		dojo.disconnect(this.evt_removeNoteDialog_endRemoveNote_1);
		dojo.disconnect(this.evt_removePartDialog_endRemove_1);	
	}
	
});



