var _id;
var _noteType;
var _noteCategory;
var _agentId;
var _panelName;

var NoteXHttp;
var NoteX, NoteY;

function DisplayNote(noteTypeCode, noteCategoryCode, id, agentId, panelName, e) {
	var xmlFile = "/Resources/Notes.cfm?Code=" + noteTypeCode + "&CatCode=" + noteCategoryCode + "&id=" + id + "&agentid=" + agentId;
	
	_id = id;
	_noteType = noteTypeCode;
	_noteCategory = noteCategoryCode;
	_agentId = agentId;
	_panelName = panelName;
	
	//window.document.style.cursor = 'hourglass';
	
	if(ie)
	{
		NoteX = event.clientX
		NoteY = event.clientY
	}
	
	if(ns)
	{
		NoteX = e.pageX
		NoteY = e.pageY
	}
	

	
	if (window.XMLHttpRequest) { // Mozilla - based browser 
		NoteXHttp = new XMLHttpRequest(); 
		
		//hook the event handler
		NoteXHttp.onreadystatechange = LoadNote;
		
		//prepare the call, http method=GET, false=asynchronous call	
		NoteXHttp.open("GET", xmlFile, true);		
		
		//finally send the call
		NoteXHttp.send(null); 
		
	} else if (window.ActiveXObject) {  // to see if we are running in IE 
		
		NoteXHttp = new ActiveXObject("Msxml2.XMLHTTP");
		
		//hook the event handler
		NoteXHttp.onreadystatechange = LoadNote;
	
		//prepare the call, http method=GET, false=asynchronous call	
		NoteXHttp.open("GET", xmlFile, false);	
		
		//finally send the call			
		NoteXHttp.send();   
	}
}

function LoadNote() {		
	// This handler is called 4 times for each 
	// state change of xmlhttp
	// States are: 0 uninitialized
	//      1 loading
	//      2 loaded
	//      3 interactive
	//      4 complete
	if (NoteXHttp.readyState==4){
		var arrNodes=Array();

		//responseXML contains an XMLDOM object				
		if (window.XMLHttpRequest)
			arrNodes = xmlChildNodes(NoteXHttp.responseXML.documentElement, "note");
		else 
			arrNodes = NoteXHttp.responseXML.selectNodes("//note");

		
		var pnlContainer = document.getElementById(_panelName);			
			
		// Hide until finished
		pnlContainer.style.display = 'none';
			
		if(arrNodes.length >0) 
		{
						
			var attrib;
			var noteId;
			var authorId;
			var authorName;
			var createdDateTime;
			var invoiceno;
			var pmtrefid;
			var csinvpaymentid;
			var noteText;	
			
			var htmlNotes = '<table width="100%" cellpadding="1" cellspacing="1" border="0" bgcolor="#FFFFCD">';	
			htmlNotes += '<tr bgcolor="silver"><td class="small" valign="top" style="font-family:Arial, Helvetica, sans-serif; font-size: 14pt; font-weight: bold;"><b>' + arrNodes.length + ((arrNodes.length==1) ? ' Note':' Notes') + '</b></td>';
			htmlNotes += '<td class="small" valign="top" align="right"><b><a href="javascript:CloseNote();">Close</a></b></td></tr>';
			
				
			for(var i=0; i<arrNodes.length; i++) {
				attrib = xmlAttributes(arrNodes[i]);
				
				
				noteId = attrib['id'];
				authorId = attrib['authorid'];
				authorName = attrib['authorname'];
				createdDateTime = attrib['created'];
				invoiceno = ((typeof(attrib['invoiceno'])!= 'undefined')?attrib['invoiceno']:'');
				pmtrefid = ((typeof(attrib['pmtrefid'])!= 'undefined')?attrib['pmtrefid']:'');
				csinvpaymentid = ((typeof(attrib['csinvpaymentid'])!= 'undefined')?attrib['csinvpaymentid']:'');
				
				
				if(!document.all) 
					noteText = arrNodes[i].firstChild.nodeValue;
				else
					noteText = arrNodes[i].text;	
					
				
				htmlNotes += '<tr><td class="small" valign="top" rowspan="2">' + noteText.replace("'", "\'") + '</td><td class="small" align="right">' + authorName + '</td></tr><tr><td class="small" align="right">' + createdDateTime + '</td></tr>';
				htmlNotes += '<tr><td class="small" style="border-top: 1px solid gray;" colspan="2"><img src="/images/blank.gif" height="1" width="1" border=0></td></tr>';
								
			}	
				
			
			htmlNotes += '<tr><td class="small" align="center" colspan="2"><a href="##" onclick="javascript:OpenNoteForm(\'' + _noteType + '\', \'' + _noteCategory + '\', \'' + _id + '\');CloseNote();">Click Here to Add a New Note for this Item.</td></tr>';
			htmlNotes += '<' + '/table>';	
			

			var pnlText = document.createElement ("div");
			pnlText.innerHTML = htmlNotes;	
			
			/*
			var pnlCloser = document.createElement ("div");
			var pnlHeader = document.createElement ("div");
			
			pnlCloser.style.width = '80';
			pnlCloser.style.position = 'relative';
			pnlCloser.style.float = 'right';
			pnlCloser.style.marginRight = '10px';
			pnlCloser.style.backgroundColor = '#c0c0c0';
			
			pnlCloser.style.fontFamily = 'Arial, Helvetica, sans-serif';
			pnlCloser.style.fontWeight = 'bold';
			pnlCloser.style.fontSize = '10px';
			
			pnlHeader.style.width = '200';
			pnlHeader.style.fontFamily = 'Arial, Helvetica, sans-serif';
			pnlHeader.style.fontWeight = 'bold';
			pnlHeader.style.fontSize = '14px';
			pnlHeader.style.backgroundColor = '#c0c0c0';
			
			
			pnlCloser.innerHTML = '<a href="javascript:CloseNote();">Close</a>';
			
			pnlHeader.innerHTML = arrNodes.length + ((arrNodes.length==1) ? ' Note':' Notes');			
			*/
			
			pnlContainer.innerHTML = '';			
			
			// Add Header and Text to Panel
			//pnlContainer.appendChild (pnlCloser);
			//pnlContainer.appendChild (pnlHeader);
			pnlContainer.appendChild (pnlText);			
			
			/*htmlNotes += '<div id="NoteCloser" style="position:relative; margin-right:10px; float:right;background-color: ; font-family:Arial, Helvetica, sans-serif bold; font-size:10px;"><a href="##" onclick="javascript:CloseNote();">Close</a></div>';
			*/


			
			// Move panel to correct location
			if(ie) pnlContainer.style.top = NoteY + document.body.scrollTop;
			if(ns) pnlContainer.style.top = NoteY;			
			pnlContainer.style.left = NoteX + 15;			
			
			// UnHide until finished
			pnlContainer.style.display = 'block';			
					
		}
		else {
				window.status='No Notes found.';
		}
	}
} 


function CloseNote() {
	document.getElementById(_panelName).style.display='none';
}


function OpenNoteForm(typeCode, categoryCode, Id) {
	var noteURL = '/AddNote.cfm?Add=true';
	if(typeCode != '') noteURL += '&type=' + typeCode;
	if(categoryCode != '') noteURL += '&category=' + categoryCode;	
	
	switch(typeCode) {
	case 'INV': 
		noteURL += '&InvoiceNo=' + Id;break;
	case 'CNT': 
		noteURL += '&ContactId=' + Id;break;
	case 'AGT': 
		noteURL += '&AgentId=' + Id;break;
	case 'PMT': 
		noteURL += '&PaymentRef=' + Id;break;
	case 'CRD': 
		noteURL += '&CreditId=' + Id;break;
	case 'TR': 
		noteURL += '&TourId=' + Id;break;
	case 'PON': 
		noteURL += '&POId=' + Id;break;
	case 'ADJ': 
		noteURL += '&AdjustmentId=' + Id;break;
	}
	
	var win = window.open(noteURL, 'AddNote', 'width=250,height=350,status=0,statusbar=0,scrollbars=0,resizing=1,resizable=1');
}