/*
** General JS functions available for all PalCare pages
*/

function makeDialog(title, destURL, modalBox, h, w, position, hideCloseBtn){
	if (position === undefined){ position = null; }
	if (hideCloseBtn === undefined){ hideCloseBtn = false; }
	// Make a dialog box with a close button containing the contents of 'url'
	var myOptions = {
		bgiframe: true,
		buttons: {
			Close: function() {
				$(this).dialog('close');
				$(this).remove();
			}
		}
	};
	
	if (modalBox != null){ myOptions.modal = modalBox; }
	if (h != null){ myOptions.height = h; }
	if (w != null){ myOptions.width = w; }
	if (position != null){ myOptions.position = position; }
	if (hideCloseBtn){myOptions.buttons = ''; }
	
	var html = '<div id="makeDialog" title="' + title + '"></div>';

	$(html).load(destURL).dialog(myOptions);
}


function palcareMessage(show, title, msg, msgStyle, spinner, modalBox, h, w){
	if (show === undefined){ show = true; }

	$('#palcareMessage').remove();	// remove any existing dialog.
	
	if (show){
		var defStyle = 'font-size: 12pt; font-weight: bold;';
		
		if (title === undefined){ title = 'PalCare - Loading'; }
		if (msgStyle === undefined || msgStyle == null){ msgStyle = defStyle }
		if (msg === undefined){ msg = 'Loading - Please Wait'; }
		if (spinner === undefined){ spinner = true; }
		if (modalBox === undefined){ modalBox = null; }
		if (h === undefined){ h = null; }
		if (w === undefined){ w = null; }
		
		msg = '<span style="' + msgStyle + '">' + msg + '</span>';
		
		var myOptions = {
			bgiframe: true,
			modal: true,
			buttons: {
				Close: function() {
					$(this).dialog('close');
					$(this).remove();
				}
			}
		};

		if (modalBox != null){ myOptions.modal = modalBox; }
		if (h != null){ myOptions.height = h; }
		if (w != null){ myOptions.width = w; }

		if (spinner){
			msg = '<img src="/shared/images/spinners/bw_24.gif" alt="' + title + '" style="margin-right: 10px;"/>' + msg;
		}
		var html = '<div id="palcareMessage" title="' + title + '">' + msg + '</div>';
		$(html).dialog(myOptions);
	}
}



/*
** Check for Mandatory Contacts - Quick & Dirty Version
*/
function checkMandatoryContact(destination, patid, onlyFirstMandatory, recommendedOnly){
	if (recommendedOnly === undefined) recommendedOnly = false;	// Mandatory by default.
	
	var incFile = 'act_includeTemplate.cfm';
	var QS = 	'includefile=/shared/patient/act_checkMandatoryContact.cfm' +
				'&patid=' + patid; 
	
	$.ajax({
		type:	"GET",
		dataType: "json",
		url:	incFile,
		data:	QS,
		cache:	false,
		success: function(retData){
			proceedToDest = true;
			if (retData.last == ''){
				// a contact has not been made for this patient in the session
				if (recommendedOnly){
					alert('It is recommended you enter a contact for this action.');
				} else {
					proceedToDest = false;
				}
			}
			
			if (proceedToDest){
				if (destination == 'addProgressNote'){
					windowPopup('addProgressNote.cfm',445,226);
				} else {
					window.location = destination;
				}
			} else {
				// Inform the user they need to enter a contact first
				palcareMessage(true, 'Contact Required', '<br />A contact must be entered before proceeding.<br />',null,false,false,null,400);
				
				// load the contact screen. Requires contact.js to be included already
				setTimeout("loadContactScreen(); palcareMessage(false);", 1500);
			}
		}
	});	
}


function addProgressNote(patid){
	// fetch the html to put into the dialog box.
	var ts = new Date().getTime();	// for no cache...
	destURL =  '/patient/addProgressNote' + '___' + patid + '___.cfm?standalone=0&patientidCheck=' + patid + '&_ts=' + ts;
	makeDialog('Add Progress Note', destURL, true, null, 700, 'top',true);
}

