var page_dirty = 0;
var radios = new Array ();

function preserveRadioContent (element) {
	if (typeof (radios[element.name]) == 'undefined') {
		radios[element.name] = new Object ();
		radios[element.name].original_value = element.value;
		radios[element.name].is_value_dirty = false;
	}
}

function preserveCheckboxContent (element) {
	if (typeof (element.original_value) == 'undefined') {
		element.original_value = element.checked;
		element.is_value_dirty = false;
	}
}

function preserveContent (element) {
	if (typeof (element.original_value) == 'undefined') {
		element.original_value = element.value;
		element.is_value_dirty = false;
	}
}

function checkContent (element) {
	if (!(element.original_value === element.value)) {
		if (!element.is_value_dirty) {
			element.is_value_dirty = true;
			incPageDirty ();
		}
	} else if (element.value === element.original_value) {
		if (element.is_value_dirty) {
			element.is_value_dirty = false;
			decPageDirty ();
		}
	}	
}

function checkCheckboxContent (element) {
	if (!(element.original_value === element.checked)) {
		if (!element.is_value_dirty) {
			element.is_value_dirty = true;
			incPageDirty ();
		}
	} else if (element.checked === element.original_value) {
		if (element.is_value_dirty) {
			element.is_value_dirty = false;
			decPageDirty ();
		}
	}
}

function checkRadioContent (element) {
	if (!(radios[element.name].original_value === element.value)) {
		if (!radios[element.name].is_value_dirty) {
			radios[element.name].is_value_dirty = true;
			incPageDirty ();
		}
	} else if (element.value === radios[element.name].original_value) {
		if (radios[element.name].is_value_dirty) {
			radios[element.name].is_value_dirty = false;
			decPageDirty ();
		}
	}
}

function incPageDirty () {
	if (!isPageDirty ())
		writeDirtyMessage ();
	page_dirty++;
}

function decPageDirty () {
	page_dirty--;
	if (page_dirty < 0)
		page_dirty = 0;
	if (!isPageDirty ())
		wipeDirtyMessage ();
}

function isPageDirty () {
	return page_dirty != 0;
}

function writeDirtyMessage () {
	var DirtyMessageArea = document.getElementById ('dirty_message_area');
	var box = document.createElement ('fieldset');
	var boxtitle = document.createElement ('legend');
	boxtitle.appendChild (document.createTextNode ('Warning'));
	box.appendChild (boxtitle);
	box.appendChild (document.createTextNode ('You have made changes on this page.  Please be sure to update them before you go to another page.'));
	box.setAttribute ('id', 'dirty_message_box');
	DirtyMessageArea.appendChild (box);
}

function wipeDirtyMessage () {
	var DirtyMessageArea = document.getElementById ('dirty_message_area');
	DirtyMessageArea.removeChild (document.getElementById ('dirty_message_box'));
}

function validateChanges (action) {
	if (isPageDirty ()) {
		custom_confirm ("You have made changes to this page which will not be saved.  Are you sure you want to continue?", "Yes", "No", action);
		return false;
	}
	return true;
}

function checkChangeFormSwitch (url) {
	var ut = new full_url_thing (url);
	var ret = validateChanges (ut);
	if (ret)
		ut.yesaction ();
	return ret;
}

function checkChangeDesignation (type_element) {
	var action = new submitAction ('switch_designation');
	action.type_element = type_element;
	action.noaction = function () {
		this.type_element.value = this.type_element.getAttribute ('original');
		this.box.clear ();
	}
	var ret = validateChanges (action);
	if (ret)
		action.yesaction ();
	return ret;
}

function checkChangePost (action_name) {
	var action = new submitAction (action_name);
	var ret = validateChanges (action);
	if (ret)
		action.yesaction ();
	return ret;
}

function submitAction (action_name) {
	this.action_name = action_name;
	this.setBox = function (box) {
		this.box = box;
	}
	this.yesaction = function () {
		if ('undefined' != typeof (this.box))
			this.box.clear ();
		var inp = document.createElement ('input');
		inp.type = 'hidden';
		inp.value = this.action_name;
		inp.name  = 'action';
		var frm = document.getElementById ('winappform');
		frm.appendChild (inp);
		frm.submit ();
	}
	this.noaction = function () {
		this.box.clear ();
	}
}

function full_url_thing (url) {
	this.url = url;
	this.setBox    = function (box) {
		this.box = box;
	}
	this.yesaction = function () {
		if ('undefined' != typeof (this.box))
			this.box.clear ();
		formSwitch (this.url);
	}
	this.noaction = function () {
		this.box.clear ();
	}
}

function linkValidate (url, message, yes_text, no_text) {
	custom_confirm (message, yes_text, no_text, new linkAction (url));
	return false;
}

function linkAction (url) {
	this.url = url;
	this.yesaction = function () {
		window.location  = (url);
		this.box.clear ();
	}
	this.noaction  = function () {
		this.box.clear ();
	}
	this.setBox    = function (box) {
		this.box = box;
	}
}

function actionValidate (action) {
	this.action = action;
	this.yesaction = function  () {
		window.location = ('?action=' + this.action);
	}
	this.noaction = function () {
	}
}
