﻿// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

Type.registerNamespace('GSAjaxControls');

GSAjaxControls.DirtyPanelBehavior = function(element) {
    GSAjaxControls.DirtyPanelBehavior.initializeBase(this, [element]);
    
    // Properties
    this._Enabled = true;
    this._IsDirty = false;
    this._DirtyMessage = 'The changes you have made have not been saved.';
	this._ControlValues = new Array();
	this._SaveControlID = null;

	// Handlers
	this._unloadHandler = null;
	this._SaveControlHandler = null;
}

GSAjaxControls.DirtyPanelBehavior.prototype = {

    initialize : function() {
       GSAjaxControls.DirtyPanelBehavior.callBaseMethod(this, 'initialize');

		if (this._Enabled == true)
		{
			this._unloadHandler = Function.createDelegate(this, this._onUnload);
			this._SaveControlHandler = Function.createDelegate(this, this._onSaveControlClick);
			
			var SaveControl = $get(this._SaveControlID);
			
			if(navigator.appName.indexOf("Microsoft") > -1)
			{
				$addHandler(window, "beforeunload", this._unloadHandler);
				$addHandler(SaveControl, "click", this._SaveControlHandler);
			}
			else
			{
				// Doing it this way because the message
				// doesn't get displayed properly in FireFox the other way
				window.onbeforeunload = this._unloadHandler;
				SaveControl.onclick = this._SaveControlHandler;
			}
			
			
			
			this._GetControls();
		}
    },

    dispose : function() {

		if (this._Enabled == true)
		{
			var SaveControl = $get(this._SaveControlID);
			
			if(navigator.appName.indexOf("Microsoft") > -1)
			{
				$removeHandler(window, "beforeunload", this._unloadHandler);
				$removeHandler(SaveControl, "click", this._SaveControlHandler);
			}
			else
			{
				window.onbeforeunload = null;
				SaveControl.onclick = null
			}
			
			this._unloadHandler = null;
			this._SaveControlHandler = null;
		}

        GSAjaxControls.DirtyPanelBehavior.callBaseMethod(this, 'dispose');
    },

	_onSaveControlClick : function(e) {
		this.dispose();
	},
	
	_onUnload : function(e) {
		this._IsDirty = this._CheckDirty();
		if (this._IsDirty)
		{
			if (this._DirtyMessage == null)
			{
				this._DirtyMessage = 'The changes you have made have not been saved.';
			}

			if(navigator.appName.indexOf("Microsoft") > -1)
			{
				event.returnValue = this._DirtyMessage;
			}
			else
			{
				e.returnValue = this._DirtyMessage;
				e.preventDefault();
			}
		}
	},
	
	_CheckDirty : function() {
	
		for (i=0; i < this._ControlValues.length; i++)
		{
			var theItem = this._ControlValues[i];
			var theControl = $get(theItem.id);
			if (theControl.type == 'text' || theControl.type == 'textarea' || theControl.type == 'password')
			{
				if (theControl.value != theItem.value)
				{
					return true;
				}
			}
			else if(theControl.type == 'select-one')
			{
				if(theControl.selectedIndex != theItem.value)
				{
					return true;
				}		
			}
			else if(theControl.type == 'checkbox' || theControl.type == 'radio')
			{
				if(theControl.checked != theItem.value)
				{
					return true;
				}
			}
		}
		return false;
	},
	
	_GetControls : function() {
		var element = this.get_element();
		var ctrls = element.getElementsByTagName('input');

		for(i = 0; i < ctrls.length; i++)
		{
			var ctrl = new Object();
			ctrl.id = ctrls[i].id;
			
			if (ctrls[i].type == 'text' || ctrls[i].type == 'password')
			{
				ctrl.value = ctrls[i].value;
			}
			else if(ctrls[i].type == 'checkbox' || ctrls[i].type == 'radio')
			{
				ctrl.value = ctrls[i].checked;
			}
			Array.add(this._ControlValues, ctrl);
		}
		
		ctrls = element.getElementsByTagName('textarea')
		
		for(i = 0; i < ctrls.length; i++)
		{
			var ctrl = new Object();
			ctrl.id = ctrls[i].id;
			ctrl.value = ctrls[i].value;
			Array.add(this._ControlValues, ctrl);
		}
		
		ctrls = element.getElementsByTagName('select')
		
		for(i = 0; i < ctrls.length; i++)
		{
			var ctrl = new Object();
			ctrl.id = ctrls[i].id;
			ctrl.value = ctrls[i].selectedIndex;
			Array.add(this._ControlValues, ctrl);
		}
	},
	
	
    get_Enabled : function() {
        return this._Enabled;
    },

    set_Enabled : function(value) {
        this._Enabled = value;
    },
    
    get_IsDirty : function() {
        return this._IsDirty;
    },

    set_IsDirty : function(value) {
        this._IsDirty = value;
    },
    
    get_DirtyMessage : function() {
		return this._DirtyMessage;
    },
    
    set_DirtyMessage : function(value) {
		this._DirtyMessage = value;
    },
    
    get_SaveControlID : function() {
		return this._SaveControlID;
	},
	
	set_SaveControlID : function(value) {
		this._SaveControlID = value;
	}
}

GSAjaxControls.DirtyPanelBehavior.registerClass('GSAjaxControls.DirtyPanelBehavior', Sys.UI.Behavior);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();