// Global flags
gSubmitFlg = false;
gAddFlg = false;

function OnUnloadHandler()
{
	// Form will unload when user navigates away or when it is redrawn because
	// the user has elected to add or delete a field to/from the form. The user
	// should be prompted to save changes before navigating away from the current
	// form. No prompt is necessary if the form is being redrawn automatically due 
	// to added fields, unless the user adds fields to the form and then attempts to
	// navigate away without saving the changes.
	
	var rc = false;
	
	//alert("gSubmitFlg:" + gSubmitFlg + " gChangeFlg:" + document.getElementById('gChangeFlg').value + " gAddFlg:" + gAddFlg);
	
	if((gSubmitFlg == false) && (document.getElementById("gChangeFlg").value == 1) && (gAddFlg == false))
	{
		rc = true;
	}
	
	if(rc == true)
	{
		return 'Your changes will be lost unless you click "Save Changes" before continuing.';
	}
	else
	{
		// Return NOTHING if there is no reason to post a confirmation dialog.  If ANYTHING at all is
		// returned, as in the case above when rc == true, that string will be inserted between the
		// top and bottom lines of the standard navigation/exit confirmation dialog that is used 
		// by each browser. No dialog box will be posted if the event handler returns {};
	} 
}

function ValField(fieldName)
{
	document.getElementById("gChangeFlg").value = 1;
}

function SaveHandler(formId)
{
	gSubmitFlg = true;
	document.getElementById("gChangeFlg").value = 0;
	
	var changedForm = document.getElementById(formId);
	
	// Add hidden field to signal notify backend to modify fields.
	var input;
	input = document.createElement('input');
	input.type = 'hidden';
	input.id = "saveData";
	input.value = 0;
	input.name = "saveData";		
	changedForm.appendChild(input);
	
	//document.forms.educationForm.submit();
	changedForm.submit();
	return true;
}

function ResetHandler(formId)
{
	if(confirm("Settings will be restored to the last saved state. Continue?"))
	{
		gSubmitFlg = true;
		document.getElementById("gChangeFlg").value = 0;

		var changedForm = document.getElementById(formId);
	
		// Add hidden field to signal notify backend to modify fields.
		var input;
		input = document.createElement('input');
		input.type = 'hidden';
		input.id = "resetData";
		input.value = 0;
		input.name = "resetData";		
		changedForm.appendChild(input);
	
		changedForm.submit();
	}
	return true;
}

function AddField(formId,hiddenField)
{
	gAddFlg = true;
	document.getElementById("gChangeFlg").value = 1;
	
	var changedForm = document.getElementById(formId);
	
	// Add hidden field to signal notify backend to modify fields.
	var input;
	input = document.createElement('input');
	input.type = 'hidden';
	input.id = hiddenField;
	input.value = 0;
	input.name = hiddenField;		
	changedForm.appendChild(input);
	
	var input;
	input = document.createElement('input');
	input.type = 'hidden';
	input.id = "changeField";
	input.value = 0;
	input.name = "changeField";		
	changedForm.appendChild(input);
	
	changedForm.submit();
	return true;
}

function RemField(formId,hiddenField,textField)
{
	var context = document.getElementById(textField).value;
	
	if(confirm("Do you want to remove:" + context + "?"))
	{
		AddField(formId,hiddenField);
	}
	return true;
}