


function PromptChangesLostSpecialCaseDispatch()
{
	//Call normal changes lost prompt first
	var bReturn = false;
	if (PromptChangesLost())  //Calls prompt normal save changes message if false then user cancelled so we skip warning about dispatch as not going to proceed
	{
		//Prompt about despatch
		if(document.getElementById('hidDespatchWarningsOnly').value == "Y")
		{
			bReturn = confirm("An attempt to dispatch this submission has generated warnings. Do you want to proceed with the dispatch anyway? Once dispatched subsequent editing will not be possible.");
		} 
		else 
		{
			bReturn = confirm("Once dispatched subsequent editing will not be possible. Are you sure you wish to dispatch this submission?");
		}
	
	}

	return bReturn;

}



function PromptChangesLost()
{
		
	var bReturn = true;
	var hiddenFlag = document.getElementById("hid_IsDirty");
	//See if this field exists
	if (hiddenFlag != null)  /*Only test for changes being lost if we find the hidden field*/
	{
		//Get the value in the hidden field
		var sFlag = hiddenFlag.value;
		if (sFlag != "")
		{
		
			// Clear the hiddenFlag field we only want the message once
			//
			// MODIFIED : Don't let them off the page with errors
			// hiddenFlag.value = "";
			
		
			//Give confirm that changes might be lost
			//
			// If this is a questionnaire page then we don't display the message
			// 
			if ( typeof prepareQuestionnaireForSubmission != 'undefined' )
			{
			
				// TARN do not like this feature so instead try to auto-save the page
				
				if ( prepareQuestionnaireForSubmission () == true ) 
				{
					// Submit this page and return true so original link will redirect the page.
					// We are assuming here that the submit won't fail.
					document.forms["Form1"].submit ();
					return true
			
				} else {
					return false;
				}
			} else {
			
					bReturn = confirm ('You have unsaved changes on this page, if you continue these changes will be lost.  Click OK to continue and lose your changes, or click Cancel to remain on this page where you may review and save your changes?');
			
			}
		}
	}
	
	return bReturn;

}

function SetIsDirtyStatus(Status)
{
	var hiddenFlag = document.getElementById("hid_IsDirty");
	
	//See if this field exists
	if (hiddenFlag != null)  /*Only test for changes being lost if we find the hidden field*/
	{

		if (Status)
		{
			hiddenFlag.value = "True";
		}
		else
		{
			hiddenFlag.value = "";
		}
		return true;
	}
	return true;
}


// This proc is intended to be used when going from a link embedded in the questionnaire
// to a sub-section. Here we want to ensure the original section is saved so we have to 
// perform a submit to save the section before instructing the page to go the sub-section.
// (using the NextSubmissionID/NextQuestionnaireID
function autoSaveAndGotoNewSection ( QuestionnaireSectionID, SubmissionSectionID, ParentSectionID )
{
	//Prevent multiple submissions. 
	//We need to check here that the page has not already been submitted because the page onSubmit event 
	//is not triggered when the page is submitted from javascript. 
	//The isSubmitted variable is declared in SubmitCheck.js
	if (isSubmitted == false)
	{
		if ( prepareQuestionnaireForSubmission () == true ) 
		{
			isSubmitted = true;

			// Set up the IDs of the page to render AFTER submitting this page
			document.forms["Form1"].elements ["hidNextQuestionnaireSectionID"].value = QuestionnaireSectionID;
			document.forms["Form1"].elements ["hidNextSubmissionSectionID"].value = SubmissionSectionID;
			document.forms["Form1"].elements ["hidNextParentSectionID"].value = ParentSectionID;
		
			document.forms["Form1"].submit ();
						
			return true;
		} 
		else
		{
			return false;
		}
	}
}

// This proc is called when a user presses a button to explicitly state that a particular
// assessment or intervention has not been performed 
function autoSaveNegativeQuestionaireSectionID (QuestionaireSectionListbox)
{
	var negativeQSid = -1;
	//Prevent multiple submissions. 
	//We need to check here that the page has not already been submitted because the page onSubmit event 
	//is not triggered when the page is submitted from javascript. 
	//The isSubmitted variable is declared in SubmitCheck.js
	if (isSubmitted == false)
	{
		if ( prepareQuestionnaireForSubmission () == true ) 
		{
			for (i = 0; i < QuestionaireSectionListbox.length; i++)
			{
				if (QuestionaireSectionListbox.options[i].selected == true)
				{
					negativeQSid = QuestionaireSectionListbox.options[i].value;
					break;
				}
			}	

			if (negativeQSid != -1)
			{
				isSubmitted = true;
				// Set up the ID of the Questionnaire Section the user is flagging as not performed
				document.forms["Form1"].elements ["hidNegativeQSID"].value = negativeQSid;	
				document.forms["Form1"].submit ();
			}			
			return true;
		} 
		else
		{
			return false;
		}
	}
}

// As above but no submit (for use with buttons)
function autoSaveAndGotoNewSectionNoSubmit ( QuestionnaireSectionID, SubmissionSectionID, ParentSectionID )
{

	if ( prepareQuestionnaireForSubmission () == true ) 
	{

		// Set up the IDs of the page to render AFTER submitting this page
		document.forms["Form1"].elements ["hidNextQuestionnaireSectionID"].value = QuestionnaireSectionID;
		document.forms["Form1"].elements ["hidNextSubmissionSectionID"].value = SubmissionSectionID;
		document.forms["Form1"].elements ["hidNextParentSectionID"].value = ParentSectionID;
		
		return true;
		
	} else {
	
		return false;
	}
		
}


// This proc is intended to be used when deleting a section and includes script
// to set the delete flag and script to navigate away from the deleted subsection
// No submit so use with buttons rather than links
function deleteAndGotoNewSection ( QuestionnaireSectionID, SubmissionSectionID, ParentSectionID )
{
	// Set up the IDs of the page to render AFTER submitting this page
	document.forms["Form1"].elements ["hidDeleteFlag"].value = "Y";
	
	// Set up the IDs of the page to render AFTER submitting this page
	document.forms["Form1"].elements ["hidNextQuestionnaireSectionID"].value = QuestionnaireSectionID;
	document.forms["Form1"].elements ["hidNextSubmissionSectionID"].value = SubmissionSectionID;
	document.forms["Form1"].elements ["hidNextParentSectionID"].value = ParentSectionID;
	
	
	// Don't submit - this script will be attached to button which will fire the submit itself
	return true;
}