//*****************************************************************************************
//
//  These scripts are all concerned with form fields
//
//*****************************************************************************************


//  Add field(s). 
//		Clones block-level, uniquely-identified element (ie <div id="xxx">); 
//		which may contain any w3c compliant code.
//		If field(s) with id are present as immediate children, they are uniquely renamed
//		by appending "_x" - where x is a number > 0.
var insertCounter = 0;

function addFields(whichFields) {
	insertCounter++;
	var newFields = document.getElementById(whichFields).cloneNode(true);
	newFields.id = '';
	newFields.style.display = 'block';
	var newField = newFields.childNodes;
	for (var i=0;i<newField.length;i++) {
		var theName = newField[i].name
		if (theName)
			newField[i].name = theName + "_" + insertCounter;
	}
	var insertHere = document.getElementById('insert_' + whichFields);
	insertHere.parentNode.insertBefore(newFields,insertHere);
}



//  Checks that a form input field has been entered
//
function validate_required(field,alerttxt) {
	with (field) {
		if (value==null||value=="") {
			alert(alerttxt);
  			return false;
		}
		else {
			return true;
		}
	}
}


//  Checks that a form select field has at least one item selected
//
function validate_selected(field,alerttxt) {
	with (field) {
		if (selectedIndex == -1) {
			alert(alerttxt);
  			return false;
		}
		else {
			return true;
		}
	}
}

//  Checks that at least one of many same-named checkboxes has been checked
//
function validate_checked(field,alerttxt) {

	var checkSelected = false;

	for (i = 0;  i < field.length;  i++) {
		if (field[i].checked) {
			return true;
		}
	}
	
	alert(alerttxt);
	return false;
}


//  Clears all named checkboxes
//
function reset_checked(field) {

	for (i = 0;  i < field.length;  i++) {
		field[i].checked = false;
	}
}


//	Checks that a string matches one of the values in a comma-delimited list
//	Case-insensitive (by rendering both to upper)
function validate_oneOfNoCase(field,testList,alerttxt) {

	var arrList = testList.split(",");
	
	for	(i = 0; i < arrList.length; i++) {
		if (field.value.toUpperCase() == arrList[i].toUpperCase()) {
			return true;
		}
	}
	alert(alerttxt);
	return false;
}
	
//	Checks that a string matches exactly one of the values in a comma-delimited list
//	
function validate_oneOf(field,testList,alerttxt) {

	var arrList = testList.split(",");
	for	(i = 0; i < arrList.length; i++) {

		if (field.value == arrList[i]) {
			return true;
		}
	}
	alert(alerttxt);
	return false;
}
	

//  Get data from named (id=) block-level element
//
function getData(fldName) {
	var x = document.getElementById(fldName);
	return x.innerHTML;
}


//  Request confirmation (eg. of "Delete" button)
// Nannette Thacker http://www.shiningstar.net
//
function confirmSubmit(msg) {
var agree=confirm(msg);
if (agree)
	return true;
else
	return false;
}



