﻿/****************************************************************************************
	Description:   Exweb Check Unsaved Form Data JS
	Created:       September 2007
	Author:        Mike Hearfield
	Copyright:     Vertex Financial Services 2006, All Rights Reserved.
	
	This library will locate all of the input, select and textarea elements
	inside the "divMain" div and adds an onchange event handler that monitors 
	whether or not the page's data has changed from the load state and warns 
	the user that unsaved data will be lost if they navigate away. If the user
	presses the cancel button in the popup, they will not leave the page.
 ***************************************************************************************/
 
	//====================================================================
	var containsEditedFields = false;
	var checkingPaused = false; // added to allow pausing of edit checking
	var customMessage = '';
	var pauseCheckingTimer;
	//====================================================================

	//====================================================================
	function unsavedFormDataCheckInit(){
		var parentNode = document.getElementById('divMain');
		if(!parentNode){
			parentNode = document.getElementById('divContent');
		}
		if(parentNode){
			var arr_fields = getMultipleElementsByTagName([ 'INPUT', 'SELECT', 'TEXTAREA' ], parentNode);
			for (var i = 0, j = arr_fields.length; i < j; i++) {	
			    
			    if ( arr_fields[i].type == 'checkbox')
			    {
			        addEvent(arr_fields[i], 'click', formChangeListener, false);				
			    }
			    else {
			        addEvent(arr_fields[i], 'change', formChangeListener, false);				
			    }
				
			}
		}
	}
	addLoadEvent(unsavedFormDataCheckInit);
	//====================================================================

	//====================================================================
	function formChangeListener(e) {
		containsEditedFields = true;
	}
	//====================================================================

	//====================================================================
	function addEvent(obj, evType, fn, useCapture) {
		if (obj.addEventListener) {
			obj.addEventListener(evType, fn, useCapture);
			return true;
		} else if (obj.attachEvent) {
			var r = obj.attachEvent('on' + evType, fn);
			return r;
		} else {
			obj['on' + evType] = fn;
		}
	}
	//====================================================================

	//====================================================================
	function getMultipleElementsByTagName(tagNames, parentNode) {
		var out = new Array();
		for( var i = 0, j = tagNames.length; i < j; i++ ) {
			elementsFound =	parentNode.getElementsByTagName(tagNames[i]);
			for (var k = 0, l = elementsFound.length; k < l; k++)
				out.push( elementsFound.item(k) );
		}
		return out;
	}
	//====================================================================

	//===================================================================
	/* Set the message that is displayed onBeforeUnload.
	This message is set on a click event so that the onBeforeUnload call 
	immediately following the event uses it. */
	function SetMessage(strMessage)
	{	
		customMessage  = strMessage;
	}
	//====================================================================


	//====================================================================
	/* It may be necessary to temporarily pause the error checking, for 
	example when doing something in a popup that causes the current page to 
	reload but not actually change. */
	
	function pauseChecking(){
		if(containsEditedFields){
			// turn this off, remember the fact!
			containsEditedFields = false;
			checkingPaused = true;
			// set a timer to turn it back on
			pauseCheckingTimer = setTimeout("reenableChecking()", 1000);
			return;
		}
	}	
	
	function reenableChecking(){
		// turn it back on.
		containsEditedFields = true;
	}
	//====================================================================


	//====================================================================
	function leavePage(){
		var message;
		if(containsEditedFields){
			//alert(containsEditedFields);
			if (customMessage != '')
			{
				message = customMessage;
				customMessage = '';
			}
			else
			{
				message = 'You have unsaved data.';
            }
            pauseChecking();
		return message;
		}
	}

	window.onbeforeunload = leavePage;
	//===================================================================
