
//var _ajaxConfig = {'_cfscriptLocation':'_code/_Note/NoteWrapper.cfc', '_jsscriptFolder':'_scripts/core/js'};

var _cfscriptLocation = '_code/_Note/NoteWrapper.cfc';
var displayPoint = '';
var processingPoint = '';
	
/* GETNOTES() */
function getNotes(record, system, listName, noteType)
{
	//alert("i am getting called from somewhere");
	//alert((new Error).stack);
	
	DWREngine._execute(_cfscriptLocation, null, 'readNotes', record, system, listName, noteType, showNotes);
}

/* ADDNOTE() */
function addNote(record, system, listName, noteType, theNote, el)
{
	if(theNote != '')
	{
		//var theField = document.getElementById(el);
		
		showWait();
		
		// clear the form field
		//theField.value = '';
	
		DWREngine._execute(_cfscriptLocation, null, 'createNote', record, system, listName, noteType, theNote, el, showNoteAdded);
	}
	else
		alert("Why are you trying to add a *blank* note?");
}

/* SHOWNOTEADDED() */
function showNoteAdded(r)
{
	if(r.status == 0)
	{
		alert(r.message);
	}
	else
	{
		// blank out the form field.
		if (document.getElementById(r.fieldname))
		{
			document.getElementById(r.fieldname).value = '';
		}

		getNotes(r.record, r.system, r.listname, r.notetype);
	
	}	
}

/* DELNOTE() */
function delNote(record)
{
	showWait();
	DWREngine._execute(_cfscriptLocation, null, 'deleteNote', record, showNoteDeleted);
}

/* SHOWNOTEDELETED */
function showNoteDeleted(r)
{
	if(r.status == 0)
	{
		alert(r.message);
	}
}

/* SHOWNOTES() */
function showNotes(r)
{
	//alert(getDisplayPoint());
	var theTable = getDisplayPoint();
	var dateFieldText = '';
	
	// Delete the table and redraw it each time
	while (theTable.childNodes.length > 0)
	{
		theTable.removeChild(theTable.firstChild);
	}

	// table deleted. redraw the table with new data.	
	for(var i = 0; i < r.getRowCount(); i++)
	{
		theTable.insertRow(i);
		theTable.rows[i].insertCell(0);
		if(r.note[i].length != 0)
		{
			theTable.rows[i].cells[0].innerHTML = r.note[i];
		}
		else
		{
			theTable.rows[i].cells[0].innerHTML = "&nbsp;";
		}
		
		theTable.rows[i].insertCell(1);
		theTable.rows[i].cells[1].innerHTML = r.createdby[i];
		
		
		dateFieldText = r.createddate[i] + " " + r.createdtime[i];
		theTable.rows[i].insertCell(2);
		theTable.rows[i].cells[2].innerHTML = dateFieldText;
			
		theTable.rows[i].insertCell(3);
		theTable.rows[i].cells[3].innerHTML = r.description[i];
		
		// security may be enabled and force the page not to show the delete button.
		theTable.rows[i].insertCell(4);
		theTable.rows[i].cells[4].innerHTML = r.jsdelbutton[i];
				
		if(i % 2 == 0)
		{
			theTable.rows[i].style.backgroundColor = "#ffffff";
		}
		else
		{
			theTable.rows[i].style.backgroundColor = "#dddddd";
		}
	}
	
	// write the table headers each time
	theTable.insertRow(0);
	theTable.rows[0].className = "RBH4";
	//theTable.rows[0].style.backgroundColor="#dfeaf4";
	theTable.rows[0].insertCell(0);
	theTable.rows[0].insertCell(1);
	theTable.rows[0].insertCell(2);
	theTable.rows[0].insertCell(3);
	theTable.rows[0].insertCell(4);
			
	theTable.rows[0].cells[0].innerHTML = 'Note';	
	theTable.rows[0].cells[1].innerHTML = 'User';
	theTable.rows[0].cells[2].innerHTML = 'Date';
	theTable.rows[0].cells[3].innerHTML = 'Type';
	theTable.rows[0].cells[4].innerHTML = 'Action';
	
	hideWait();
}

function setDisplayPoint(tableID)
{
	displayPoint = document.getElementById(tableID);
}

function getDisplayPoint()
{
	return displayPoint;
}

function showWait()
{
	//alert(getProcessingPoint());
	
	if(getProcessingPoint() != '')
	{
		var waitBox = getProcessingPoint();
		waitBox.style.pixelTop = (document.body.scrollTop + 150);
		waitBox.style.visibility = 'visible';
	}
}

function hideWait()
{
	if(getProcessingPoint() != '')
	{
		var waitBox = getProcessingPoint();
		waitBox.style.visibility = 'hidden';
	}
}

function setProcessingPoint(el)
{
	processingPoint = document.getElementById(el);
}

function getProcessingPoint()
{
	return processingPoint;
}