//  ############################################
//  #                                          #
//  #   cleverspin javascript form functions   #
//  #                                          #
//  ############################################

createLoadListener(formtools_parseForms);

formtools_formElements = new Array();

function formtools_parseForms()
{
	for (thisForm=0; thisForm<document.forms.length; thisForm++)
	{
		for (thisElement=0; thisElement<document.forms[thisForm].elements.length; thisElement++)
		{
			thisName = document.forms[thisForm].elements[thisElement].name;
			thisID = document.forms[thisForm].elements[thisElement].id;
			if (((thisID == null) || (thisID == "")) && ((thisName != null) && (thisName != "")))
			{
				document.forms[thisForm].elements[thisElement].id = thisName;
				thisID = document.forms[thisForm].elements[thisElement].id;
			}
			thisTag = document.forms[thisForm].elements[thisElement].tagName.toUpperCase();
			thisType = document.forms[thisForm].elements[thisElement].type.toUpperCase();
			thisDefault = document.forms[thisForm].elements[thisElement].getAttribute('defaulttext');
			if (thisDefault == null)
			{
				thisDefault = "";
			}
			thisRequired = document.forms[thisForm].elements[thisElement].getAttribute('requiredtext');
			if ((thisRequired == null) || (thisRequired == "") || (thisRequired.toUpperCase() == "FALSE"))
			{
				thisRequired = false;
			} else {
				thisError = thisRequired;
				thisRequired = true;
			}
			thisTab = document.forms[thisForm].elements[thisElement].getAttribute('tabonenter');
			if ((thisTab == null) || (thisTab == ""))
			{
				thisTab = false;
			}
			if ((thisType == "TEXT") || (thisType == "PASSWORD") || (thisType == "SEARCH") || (thisType == "FILE") || (thisTag == "SELECT") || (thisTag == "TEXTAREA"))
			{
			if (thisRequired)
				{
					formtools_formElements[thisID] = { isRequired : true, defaultText : thisDefault, tabOnEnter : thisTab, errorText : thisError};
				} else {
					formtools_formElements[thisID] = { isRequired : false, defaultText : thisDefault, tabOnEnter : thisTab};
				}
			} else if ((thisType == "CHECKBOX") || (thisType == "RADIO")) {
				formtools_formElements[thisID] = { tabOnEnter : thisTab};
			}
		}
	}
	formtools_defaults.filledClass = "filledForm";
	formtools_defaults.emptyClass = "unfilledForm";
	formtools_defaults.register(formtools_formElements);
}

var formtools_defaults = {
	list : new Array,
	allfields : new Array,
	register : function(sheet) {
		formtools_defaults.list.push(sheet);
		formtools_defaults.setup();
	},
	setup : function() {
		for (h=0;sheet=formtools_defaults.list[h];h++) {
			for (fieldname in sheet) {
				this.allfields[fieldname] = new Object();
				this.allfields[fieldname].isRequired = this.list[h][fieldname].isRequired;
				this.allfields[fieldname].defaultText = this.list[h][fieldname].defaultText;
				this.allfields[fieldname].errorText = this.list[h][fieldname].errorText;
				this.allfields[fieldname].tagType = document.getElementById(fieldname).tagName.toUpperCase();
				if ((this.allfields[fieldname].tagType == 'INPUT') || (this.allfields[fieldname].tagType == "TEXTAREA"))
				{
					inputType = document.getElementById(fieldname).type.toUpperCase();
					if ((inputType == "TEXT") || (inputType == "SEARCH") || (inputType == "FILE") || (inputType == "PASSWORD") || (this.allfields[fieldname].tagType == "TEXTAREA"))
					{
						var currentValue = document.getElementById(fieldname).value;
						if (currentValue == "")
						{
							document.getElementById(fieldname).value = this.allfields[fieldname].defaultText;
							document.getElementById(fieldname).className = this.emptyClass;
						} else if (currentValue == this.allfields[fieldname].defaultText)
						{
							document.getElementById(fieldname).className = this.emptyClass;
						} else {
							document.getElementById(fieldname).className = this.filledClass;
						}
						document.getElementById(fieldname).onfocus = function() {
							var currentValue = document.getElementById(this.id).value;
							if (currentValue == formtools_defaults.allfields[this.id].defaultText)
							{
								document.getElementById(this.id).value = "";
							}
							document.getElementById(this.id).className = formtools_defaults.filledClass;
						}
						document.getElementById(fieldname).onblur = function () {
							var currentValue = document.getElementById(this.id).value;
							if ((currentValue == null) || (currentValue == ""))
							{
								document.getElementById(this.id).value = formtools_defaults.allfields[this.id].defaultText;
								document.getElementById(this.id).className = formtools_defaults.emptyClass;
							}
						}
					}
				} else if ((this.allfields[fieldname].tagType.toUpperCase() == 'SELECT') && (!document.getElementById(fieldname).onchange)) {
					document.getElementById(fieldname).onchange = function () {
						var currentID = document.getElementById(this.id).selectedIndex;
						var currentValue = document.getElementById(this.id).options[currentID].value;
						if (currentValue == "") document.getElementById(this.id).selectedIndex = 0;
					}
				}
				if ((this.list[h][fieldname].tabOnEnter) && (this.allfields[fieldname].tagType != "TEXTAREA"))
				{
					this.allfields[fieldname].tabToField = this.list[h][fieldname].tabOnEnter;
					document.getElementById(fieldname).onkeypress = function() {
						if (window.event && window.event.keyCode == 13)
						{
							var totalElements = document.getElementById(this.id).form.elements.length;
							var thisElement = -1;
							for (var i=0;i<totalElements;i++) {
								if (document.getElementById(this.id).form.elements[i].id == this.id) {
									thisElement = i;
								}
							}
							if (thisElement == (totalElements-1))
							{
								newIndex = 0;
							} else {
								newIndex = thisElement+1;
							}
							document.getElementById(this.id).form.elements[newIndex].focus();
							return false;
						} else return true;
					}
				}

			}
		}
	},
	emptyClass : "unfilledForm",
	filledClass : "filledForm",
	validate : function (event) {
		if (typeof event == "undefined") event = window.event;
		var whichFormObj = getEventTarget(event);
		var whichForm = whichFormObj.id;
		return formtools_defaults.submit(whichForm);
	},
	submit : function(whichForm) {
		var errors = "";
		var testValue = "";
		var whichIndex = 0;
		for (var which in formtools_defaults.allfields)
		{
			if ( typeof document.forms[whichForm].elements[which] != "undefined" )
			{
				if (( formtools_defaults.allfields[which].isRequired == true ) || ( formtools_defaults.allfields[which].isRequired == "true" ))
				{
					if (formtools_defaults.allfields[which].tagType.toUpperCase() == 'SELECT')
					{
						whichIndex = document.forms[whichForm].elements[which].selectedIndex;
						testValue = document.forms[whichForm].elements[which].options[whichIndex].value;
					} else {
						testValue = document.forms[whichForm].elements[which].value;
					}
					if ((testValue == formtools_defaults.allfields[which].defaultText) || (testValue == "") || (testValue == " ") || (testValue == null))
					{
						errors += "&nbsp;&nbsp;&nbsp;"+formtools_defaults.allfields[which].errorText+"<br />";
					}
				}
			}
		}
		if (errors == "")
		{
			for (var which in formtools_defaults.allfields)
			{
				if ( typeof document.forms[whichForm].elements[which] != "undefined" )
				{
					if (formtools_defaults.allfields[which].tagType.toUpperCase() != 'SELECT')
					{
						testValue = document.forms[whichForm].elements[which].value;
						if (testValue == formtools_defaults.allfields[which].defaultText)
						{
							document.forms[whichForm].elements[which].value = "";
						}
					}
				}
			}
			document.forms[whichForm].submit();
			//return true;
		} else {
			var errorText = "<div class='formAlertHeader'><div>Attention</div></div><div class='formAlertInner'><div><b>Please provide the following required information:</b><br /><br />";
			errorText += errors;
			errorText += "<br /><a href='javascript:destroyAlert();destroyAlertShader();' class='formAlertButton'><span>OK</span></a><div class='formAlertClear'></div></div></div>";
			createAlertShader();
			createAlert(errorText);
			return false;
		}
	},
	reset : function(whichForm) {
		for (var which in this.allfields)
		{
			if (this.allfields[which].tagType.toUpperCase() == 'SELECT')
			{
				document.forms[whichForm].elements[which].selectedIndex = 0;
			} else {
				document.forms[whichForm].elements[which].className = this.emptyClass;
				document.forms[whichForm].elements[which].value = this.allfields[which].defaultText;
			}
		}
	}
};



