function testalert(theMessage) {
	//set testing using an if inside <script>
	if (testing) {
		alert(theMessage);
	} else {
		//warning(theMessage);
	}
}

function makeEnglishName(fieldName) {
	var fieldEnglish = fieldName.replace(/_/g, " ");
	fieldEnglish = '"' + fieldEnglish + '"';
	return fieldEnglish;
}

function getTrueFieldName(theForm, fieldName, fieldType) {
	var elem = theForm[fieldName];
	if (!elem) {
		//see if this is a PHP array
		arrayFieldName = fieldName + '[]';
		elem = theForm[arrayFieldName];
		if (!elem) {
			formMessages[formMessages.length] = "Form problem: " + fieldType + " field " + fieldName + " must exist.";
		}	
	}
	return elem;
}

function warning(message) {
	if (document.title == 'USD: MyPostings') {
		window.alert(message);
	}
}

function getSelectValue(theElement) {
	if (theElement.selectedIndex == -1) {
		//there is no selection; this is most likely a multiple select and nothing has been selected by default
		theString = "";
	} else {
		theString = theElement[theElement.selectedIndex];
		theString = theString.value;
	}
	return theString;
}

function getFieldValue(theElement) {
	var theString = "";


	if (theElement.length > 0) {
		var Counter = 0;

		if (theElement.type && theElement.type.substring(0,6) == "select") {
			testalert("A select: " + theElement.name);
			//selects
			theString = getSelectValue(theElement);
		//is this an Easy SQL list of selects? The first one will be a hidden input, so check the second
		} else if (theElement[1].type && theElement[1].type.substring(0, 6) == "select") {
			//warning("Looking at " + theElement[0].name + ":" + theElement[1].type + " - " + theElement[1].defaultChecked);
			Counter = 1;
			while (Counter < theElement.length) {
				if (theString = getSelectValue(theElement[Counter])) {
					break;
				}
				Counter++;
			}
		//trying to determine if this is a radio button or checkbox
		} else if (theElement.defaultChecked == true || theElement[0].defaultChecked == true || theElement[0].defaultChecked == false) {
			testalert("Checking a radio box or checkbox: " + theElement[0].name);
			//checkboxes and radio boxes
			while (Counter < theElement.length) {
				if (theElement[Counter].checked == true) {
					theString = theElement[Counter].value;
					//warning("Setting to " + theString + " at " + Counter);
				}
				Counter++;
			}
		} else {
			//multiple instances of same field name
			while (Counter < theElement.length) {
				if (theElement[Counter].value) {
					theString = theElement[Counter].value;
				}
				Counter++;
			}
		}
	} else {
		//single checkboxes don't come across as a list of items
		//radio buttons probably don't either but single radio buttons should be checkboxes
		if (theElement.type == "checkbox") {
			if (theElement.checked == true) {
				theString = theElement.value;
			}
		} else {
			theString = theElement.value;
		}
	}
	
	return theString;
}

function isempty(theElement) {
	theString = getFieldValue(theElement);

	if (theString == "") {
		return true;
	} else {
		return false;
	}
}

function isnumber(theString) {
	var theString = theString.toLowerCase();
	var isText = false;
	var j=0;
	while (j< theString.length && !isText) {
		var theChar = theString.charAt(j);
		if (theChar >= 'a' && theChar <= 'z') {
			isText = true;
		}
		j++;
	}
	return !isText;
}

var numbered = new Array();
var required = new Array();
var testing = false;

function verifyForm(theForm) {
	formMessages = new Array();

	//check to see that required fields are there
	if (required) {
		for (i=0; i< required.length; i++) {
			var fieldName = required[i];
			var elem = getTrueFieldName(theForm, fieldName, "required");
			
			if (elem) {
				if (isempty(elem)) {
					formMessages[formMessages.length] = mustBeAnswered(fieldName);
				}
			}
		}
	}
	
	//check to see that number fields are numbers
	if (numbered) {
		for (i=0; i< numbered.length; i++) {
			var fieldName = numbered[i];
			var elem = getTrueFieldName(theForm, fieldName, "numeric");
			if (elem) {
				if (!isempty(elem)) {
					if (!isnumber(elem.value)) {
						formMessages[formMessages.length] = mustBeNumber(elem.name);
					}
				}
			}
		}
	}

	if (formMessages.length > 0) {
		alert(formMessages.join("\n \n"));
		return false;
	} else {
		return true;
	}
}


Array.prototype.inArray = function(value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

function mustBeAnswered(fieldName) {
	return makeEnglishName(fieldName) + " must be answered";
}
function mustBeNumber(fieldName) {
	return makeEnglishName(fieldName) + " must be a number";
}	

//used for custom verification
function fieldValue(theForm, theField) {
	return getFieldValue(theForm[theField]);
}

function verifyFields(theForm, fieldNames) {
	var msgs = new Array();
	if (typeof(fieldNames) != 'object') {
		fieldNames = new Array(fieldNames)
	}
	
	for(counter=0;counter<fieldNames.length;counter++) {		fieldName = fieldNames[counter];

		if (elem = theForm[fieldName]) {
			if (isempty(elem)) {
				msgs[msgs.length] = mustBeAnswered(fieldName);
			} else if (numbered) {
				if (numbered.inArray(fieldName)) {
					if (!isnumber(elem.value)) {
						msgs[msgs.length] = mustBeNumber(fieldName);
					}	
				}
			}
		} else {
			alert("Error: there is no form element " + fieldName);
		}
	}
	
	return msgs.join("\n \n");
}

