function frmChecker(id)
{
	this.id = id;
	this.clientID = '';
	this.uniqueID = '';
	this.form = null;
	this.warnURL = '';
	this.warnWin = null;
	this.tags = new Array();
	this.vals = new Array();
	this.href = '';
	this.initialized = false;

	return this;
}

frmChecker.prototype.route = function()
{
	if(this.href.indexOf('javascript:') > -1)
	{
		eval(unescape(this.href.substring(this.href.indexOf('javascript:') + 11)));
	}else{
		location.href = this.href;
	}
}

frmChecker.prototype.checkForm = function()
{
	var pass = true;
	
	for(i = 0; i < this.tags.length; i++)
	{
		var tagID = '';
		if(this.tags[i].id)
			tagID = this.tags[i].id;
		else if(this.tags[i].name)
			tagID = this.tags[i].name;
		switch(this.tags[i].type)
		{
			case 'radio':
			case 'checkbox':
				if(this.vals[tagID] != null && this.tags[i].checked != this.vals[tagID])
					pass = false;
				break;
			case 'text':
			case 'textarea':
				if(this.vals[tagID] != null && this.tags[i].value != this.vals[tagID])
					pass = false;
				break;
			case 'select-one':
			case 'select-multiple':
				if(this.vals[tagID] != null && this.tags[i].selectedIndex != this.vals[tagID])
					pass = false;
				break;
		}
	}
	return pass;	
}

frmChecker.prototype.Initialize = function(clientID, uniqueID, form, warnURL)
{
	this.clientID = clientID;
	this.uniqueID = uniqueID;
	this.form = form;
	this.warnURL = warnURL;
	this.initialized = true;

	for(i = 0; i < this.form.length; i++)
	{
		var fieldID = '';
		var ind = -1;
		
		if(this.form[i].id)
		{
			fieldID = this.form[i].id;
			ind = fieldID.indexOf(this.clientID)
		}
		else if(this.form[i].name)
		{
			fieldID = this.form[i].name;
			ind = fieldID.indexOf(this.uniqueID)
		}
		
		if(ind == 0)
		{
			switch(this.form[i].type)
			{
				case 'radio':
				case 'checkbox':
					this.tags[this.tags.length] = this.form[i];
					this.vals[fieldID] = this.form[i].checked;
					break;
				case 'text':
					this.tags[this.tags.length] = this.form[i];
					this.vals[fieldID] = this.form[i].value;
					break;
				case 'textarea':
					this.tags[this.tags.length] = this.form[i];
					this.vals[fieldID] = this.form[i].value;
					break
				case 'select-one':
				case 'select-multiple':
					this.tags[this.tags.length] = this.form[i];
					this.vals[fieldID] = this.form[i].selectedIndex;
					break;
			}
		}
	}
	
	var save = this.form[this.clientID + '_frmSaveState']
	if(save == null)
		save = this.form[this.uniqueID + ':frmSaveState']

	if(save != null)
	{
		var items = save.value.split('||')
		for(i = 1; i < items.length; i++)
		{
			eval('var frmTemp = ' + items[i])
			this.vals[frmTemp.id] = frmTemp.value
			frmTemp = null;
		}
		save.value = ''
	}
	
	for(i = 0; i < document.links.length; i++)
	{
		document.links[i].onclick = displayWarning
	}
}

frmChecker.prototype.saveState = function()
{
	var save = this.form[this.clientID + '_frmSaveState']
	if(save == null)
		save = this.form[this.uniqueID + ':frmSaveState']
		
	if(save != null)
	{
		for(i = 0; i < this.tags.length; i++)
		{
			var tagID = '';
			if(this.tags[i].id)
				tagID = this.tags[i].id;
			else if(this.tags[i].name)
				tagID = this.tags[i].name;
				
			switch(this.tags[i].type)
			{
				case 'radio':
				case 'checkbox':
					if(this.vals[tagID] != null && this.tags[i].checked != this.vals[tagID])
						save.value += '||{id : \'' + tagID + '\', value: ' + this.vals[tagID] + '}'
					break;
				case 'text':
					if(this.vals[tagID] != null && this.tags[i].value != this.vals[tagID])
						save.value += '||{id : \'' + tagID + '\', value: \'' + this.vals[tagID].replace("'", "\'") + '\'}'
					break;
				case 'textarea':
						if(this.vals[tagID] != null && this.tags[i].value != this.vals[tagID])
							save.value += '||{id : \'' + tagID + '\', value: \'' + this.vals[tagID].replace("'", "\'") + '\'}'
						break;
				case 'select-one':
				case 'select-multiple':
					if(this.vals[tagID] != null && this.tags[i].selectedIndex != this.vals[tagID])
						save.value += '||{id : \'' + tagID + '\', value: ' + this.vals[tagID] + '}'
					break;
			}
		}
	}
}

frmChecker.prototype.warn = function(obj)
{
	if(this.form != null)
	{
		var objID = '';
		var ind = 0;
		if(obj.id)
		{
			objID = obj.id;
			ind = objID.indexOf(this.clientID)
		}
		else if(obj.name)
		{
			objID = obj.name;
			ind = objID.indexOf(this.uniqueID)
		}
			
		if(obj.href.indexOf('__doPostBack') == -1 || (obj.href.indexOf('__doPostBack') > -1 && ind == -1))
		{
			if(!this.checkForm())
			{
				this.href = obj.href;
				var width = 400;
				var height = 220;
				var top = 0;
				var left = 0;
				
				top = (screen.availHeight - height) / 2;
				left = (screen.availWidth - width) / 2;
				
				if(typeof(window) != 'undefined')
					this.warnWin = window.open(this.warnURL, 'warning', 'resizeable=yes, width=' + width + ',height=' + height + ',top=' + top + ',left=' + left);
				else
					this.warnWin = open(this.warnURL, 'warning', 'resizeable=yes, width=400,height=220,top=300,left=450');

				return false;
			}
		}
	}
	return true;
}

var myFormChecker = new frmChecker('myFormChecker')

function displayWarning()
{
	return myFormChecker.warn(this);
}

onfocus = function()
{
	if(myFormChecker.initialized)
		if(myFormChecker.warnWin != null && !myFormChecker.warnWin.closed)
			myFormChecker.warnWin.focus();
}

function frmCancel()
{
	if(typeof(window.opener.myFormChecker) != 'undefined')
		window.opener.myFormChecker.route();
	self.close();
}

function openerFocus()
{
	if(window.opener)
		window.opener.focus();
}
