
function cancel()
{
	if(document.cancel)
	{
		if(document.cancel.db_exitsp.value == "" || document.cancel.db_exitsp.value == 1)
		{
			window.close();
			return;
		}
	
		if(document.cancel.db_isdynamic.value == 0)	// branch back to a .html URL
			document.location.href = document.cancel.db_exitaction.value;
		else
		{
			if(document.cancel.db_exitsp.value != 0)
			{	// "pop" this one + one off the stack
				eval("document.cancel.db_exitstack" + (document.cancel.db_exitsp.value - 1) + ".value=\"\";");
				eval("document.cancel.db_exitstack" + (document.cancel.db_exitsp.value - 2) + ".value=\"\";");
				eval("document.cancel.db_exitsp.value -= 2;");
			}
			document.cancel.submit();
		}
	}
	else
		window.close();
}

function refresh()
{
	if(document.refresh)
		document.refresh.submit();
}

function buildURI(form)
{
	var i;
	var uri;
	
	uri = form.action + "?";
	for(i = 0; i < form.length; i++)
	{
		if(i != 0)
			uri += "&";
		uri += form.elements[i].name + "=" + escape(unescape(form.elements[i].value));
	}

	return(uri);	
}

function mirrorCheck(item)
{
	var c;

	var box = eval("document.save.mirror" + item);
	var item = eval("document.save." + item);
	item.value = (box.checked == true) ? 1 : 0;
}

function dirty(id)
{
	var dflag = eval("document.save.dirty_" + id);
	dflag.value = 1;
	document.save.db_dirty.value = 1;
}

function mirrorAndDirtyCheck(item, id)
{
	mirrorCheck(item);
	dirty(id);
}

function toggle(checkbox, varname)
{
	var v = eval("document.save." + varname);
	
	v.value |= checkbox.value;
	if(! checkbox.checked)
	{
		v.value ^= checkbox.value;
	}
}

var popwin;

function popupwin(uri)
{
	popwin = window.open(uri, "Confirm", "width=475, height=250");

	if (window.screen)
	{
		var aw = screen.availWidth;
		var ah = screen.availHeight;
		popwin.moveTo((aw - 475) / 2, (ah - 250) / 2);
	}
	popwin.focus();
}

function add()
{
	fConfirmSave = 1;
	if(document.save.db_dirty.value == 1)
	{
		fConfirmSave = 1;
		popupwin('/sitemaint/confirmadd.html');
	}
	else
	{
		document.add.submit();
	}
}

function change(id)
{
	fConfirmSave = 1;
	document.change.db_recordid.value = id;
	if(document.save.db_dirty.value == 1)
	{
		popupwin('/sitemaint/confirmchange.html');
	}
	else
	{
		document.change.submit();
	}
}

function remove(id)
{
	fConfirmSave = 1;
	document.remove.db_recordid.value = id;
	if(document.save.db_dirty.value == 1)
	{
		fConfirmSave = 1;
		popupwin('/sitemaint/confirmremove.html');
	}
	else
	{
		document.remove.submit();
 	}

}

function save()
{
	fConfirmSave = 1;
	saveHook();	// actual function inserted during form generation
	document.save.submit();
}

function indicateSave()
{
	fConfirmSave = 1;
	return(1);
}

function exitTrap()
{
	if(document.save.db_dirty.value == 1 && fConfirmSave == 0)
	{
//		saveHook();	// actual function inserted during form generation
		document.save.db_template.value = "rsfailsafecompletion";
		var saveuri = escape(buildURI(document.save));
		popupwin('/dbrun?db_template=rsfailsafesave&db_detoururi=' + saveuri);
	}
}

function dirtyDoc()
{
	document.save.db_dirty.value = 1;
}

function confirmCancel()
{
	// called when user hits cancel on the "save changes" popup
	fConfirmSave = 0;
}

function confirmAdd(savefirst)
{
	popwin.close();
	if(savefirst)
	{
		document.save.db_detoururi.value = escape(buildURI(document.add));
		document.save.db_template.value = "rsdetourpage";
		document.save.submit();
	}
	else
		document.add.submit();
}

function confirmChange(savefirst)
{
	popwin.close();
	if(savefirst)
	{
		document.save.db_detoururi.value = escape(buildURI(document.change));
		document.save.db_template.value = "rsdetourpage";
		document.save.submit();
	}
	else
		document.change.submit();
}

function confirmRemove(savefirst)
{
	popwin.close();
	if(savefirst)
	{
		document.save.db_detoururi.value = escape(buildURI(document.remove));
		document.save.db_template.value = "rsdetourpage";
		document.save.submit();
	}
	else
		document.change.submit();
}

var lastspot = null;

function spotlight(e)
{
	var p = null;

	if(lastspot)
	{
		lastspot.className = "trn";
	}
		
	if(e.parentNode)
		p = e.parentNode;

	p.className = "trs";
	lastspot = p;
}	

function unspotlight()
{
	if(lastspot)
	{
		lastspot.className = "trn";
	}
	lastspot = null;
}

var _param = new Array;

function parseParameters(str)
{
	var args = str.split('&');
	for(var i = 0; i < args.length; i++)
	{
		var p = args[i].split('=');
		_param[p[0]] = p[1];
	}
}

function getParameter(str)
{
	return(_param[str]);
}

