/* Last Modified:  7/17/2000 5:30pm - mft - added numeric and int validation including negatives */
/* Last Modified:  3/20/2000 4:30pm - mft - added "Date and Time" validation */
/* Last Modified:  3/ 7/2000 4:30pm - mft - added selectbox, checkbox and option validation */
/* Last Modified:  2/21/2000 3:00pm - mft - added datetime validation */
/* Last Modified: 10/27/1999 9:30am - mft */
/* Last Modified: 12/13/2001 1:45pm - mal - added validation to check for inserted spaces without text* may not work for Netscape & IE 3.0 */ 
/* ============================================================= */

//Purpose: Main validation function for forms
//Input: robjForm: form that needs validation
//Output: false if not a valid form, true if it is a valid form

function doValidation(robjForm) {
	var blnGoodField, strType, blnRequired, intLoopValidate;

	for (intLoopValidate = 0; intLoopValidate < robjForm.length; intLoopValidate++) {
		blnGoodField = true;
		//alert("The name is: " + robjForm[intLoopValidate].name + "\nThe value is: " + robjForm[intLoopValidate].value);

		var strValidCharacters = ' ';
		var strTempField='';
		varField = robjForm[intLoopValidate].value
		varFieldname = robjForm[intLoopValidate].name
		if(varField != null){
			for (var i=0; i<varField.length; i++){
				if (strValidCharacters.indexOf(varField.substring(i,i+1)) == 0){
					strTempField = strTempField + varField.substring(i,i+1).replace(' ','');
				}else{
					strTempField = robjForm[intLoopValidate].value
				}
			}
		}
		if ((robjForm[intLoopValidate].type == "button") ||
			(robjForm[intLoopValidate].type == "submit"))
			{ continue; }

		strType = robjForm[intLoopValidate].name.substring(robjForm[intLoopValidate].name.length-4,robjForm[intLoopValidate].name.length);

		if (robjForm[intLoopValidate].name.substring(robjForm[intLoopValidate].name.length-6,robjForm[intLoopValidate].name.length-4) == "_R") {
			blnRequired = true;
		} else {
			blnRequired = false;
		}

		// PHONE NUMBER
		if (strType == "_phn") {
			if ((blnRequired) && (strTempField == "")) {
				blnGoodField = isPhoneNumber(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isPhoneNumber(robjForm[intLoopValidate]);
				}
			}
		}

		// DATE
		if (strType == "_dat") {
			if ((blnRequired) && (strTempField == "")) {
				blnGoodField = isValidDate(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidDate(robjForm[intLoopValidate]);
				}
			}
		}

		// TIME
		if (strType == "_tim") {
			if ((blnRequired) && (strTempField == "")) {
				blnGoodField = isValidTime(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidTime(robjForm[intLoopValidate]);
				}
			}
		}

		// DATE or TIME or BOTH
		if (strType == "_dtm") {
			if ((blnRequired) && (strTempField == "")) {
				blnGoodField = isValidDateTime(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidDateTime(robjForm[intLoopValidate]);
				}
			}
		}

		// DATE and TIME
		if (strType == "_dtr") {
			if ((blnRequired) && (strTempField == "")) {
				blnGoodField = isValidDateAndTime(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidDateAndTime(robjForm[intLoopValidate]);
				}
			}
		}

		// NUMBER
		if (strType == "_num") {
			if ((blnRequired) && (strTempField == "")) {
				if (robjForm[intLoopValidate].value == "") {
					blnGoodField = false;
				} else {
					blnGoodField = isNumber(robjForm[intLoopValidate].value);
				}
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isNumber(robjForm[intLoopValidate].value);
				}
			}
			if (!blnGoodField) {
				alert("Not a valid number");
				robjForm[intLoopValidate].focus();
			}
		}

		// INTEGER
		if (strType == "_int") {
			if ((blnRequired) && (strTempField == "")) {
				if (robjForm[intLoopValidate].value == "") {
					blnGoodField = false;
				} else {
					blnGoodField = isInteger(robjForm[intLoopValidate].value);
				}
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isInteger(robjForm[intLoopValidate].value);
				}
			}
			if (!blnGoodField) {
				alert("Not a valid integer");
				robjForm[intLoopValidate].focus();
			}
		}

		// ANY NUMBER (positive or negative)
		if (strType == "_anm") {
			if ((blnRequired) && (strTempField == "")) {
				if (robjForm[intLoopValidate].value == "") {
					blnGoodField = false;
				} else {
					blnGoodField = isAnyNumber(robjForm[intLoopValidate].value);
				}
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isAnyNumber(robjForm[intLoopValidate].value);
				}
			}
			if (!blnGoodField) {
				alert("Not a valid number");
				robjForm[intLoopValidate].focus();
			}
		}

		// ANY INTEGER (positive or negative)
		if (strType == "_ain") {
			if ((blnRequired) && (strTempField == "")) {
				if (robjForm[intLoopValidate].value == "") {
					blnGoodField = false;
				} else {
					blnGoodField = isAnyInteger(robjForm[intLoopValidate].value);
				}
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isAnyInteger(robjForm[intLoopValidate].value);
				}
			}
			if (!blnGoodField) {
				alert("Not a valid integer");
				robjForm[intLoopValidate].focus();
			}
		}

		// CURRENCY
		if (strType == '_cur') {
			if ((blnRequired) && (strTempField == "")) {
				if (robjForm[intLoopValidate].value == '') {
					blnGoodField = false;
				} else {
					blnGoodField = ForceCurrency(robjForm[intLoopValidate]);
				}
			} else {
				if (robjForm[intLoopValidate].value != '') {
					blnGoodField = ForceCurrency(robjForm[intLoopValidate]);
				}
			}
			if (!blnGoodField) {
				alert('Not a valid currency entry');
				robjForm[intLoopValidate].focus();
			}
		}

		// EMAIL ADDRESS
		if (strType == "_eml") {
			if ((blnRequired) && (strTempField == "")) {
				blnGoodField = isValidEmail(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidEmail(robjForm[intLoopValidate]);
				}
			}
		}

		//SOCIAL SECURITY NUMBER
		if (strType == "_ssn") {
			if ((blnRequired) && (strTempField == "")) {
				blnGoodField = isSSNumber(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isSSNumber(robjForm[intLoopValidate]);
				}
			}
		}

		// MULTIPLE EMAIL ADDRESS
		if (strType == "_mem") {
			if ((blnRequired) && (strTempField == "")) {
				blnGoodField = isValidEmailList(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidEmailList(robjForm[intLoopValidate]);
				}
			}
		}

		// URL (WEB ADDRESS)
		if (strType == "_url") {
			if ((blnRequired) && (strTempField == "")) {
				blnGoodField = isValidURL(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidURL(robjForm[intLoopValidate]);
				}
			}
		}

		// TEXT STRING
		if (strType == "_txt") {
			//if ((blnRequired) && (robjForm[intLoopValidate].value == "")) {
			if ((blnRequired) && (strTempField == "")) {
				alert("Please enter the required information");
				robjForm[intLoopValidate].focus();
				blnGoodField = false;
			}
		}

		// ZIPCODE (5 OR 9 DIGITS)
		if (strType == "_zip") {
			if ((blnRequired) && (strTempField == "")) {
				blnGoodField = isValidZip(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidZip(robjForm[intLoopValidate]);
				}
			}
		}

		// 5 DIGIT ZIPCODE
		if (strType == "_zp5") {
			if ((blnRequired) && (strTempField == "")) {
				blnGoodField = isValidZip5(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidZip5(robjForm[intLoopValidate]);
				}
			}
		}

		// 4 DIGIT ZIPCODE
		if (strType == "_zp4") {
			if ((blnRequired) && (strTempField == "")) {
				blnGoodField = isValidZip4(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidZip4(robjForm[intLoopValidate]);
				}
			}
		}

		// SINGLE SELECTION BOX
		if (strType == "_sel") {
			if ((blnRequired) && (strTempField == "")) {
				if (robjForm[intLoopValidate].selectedIndex == 0) {
					alert("Please select a value");
					robjForm[intLoopValidate].focus();
					blnGoodField = false;
				}
			}
		}

		// SINGLE SELECT LIST BOX
		if (strType == "_ssl") {
			if ((blnRequired) && (strTempField == "")) {
				if (robjForm[intLoopValidate].selectedIndex == -1) {
					alert("Please select a value from the list");
					robjForm[intLoopValidate].focus();
					blnGoodField = false;
				}
			}
		}

		// MULTIPLE SELECT LIST BOX
		if (strType == "_msl") {
			if ((blnRequired) && (strTempField == "")) {
				if (robjForm[intLoopValidate].selectedIndex == -1) {
					alert("Please select at least one value from the list");
					robjForm[intLoopValidate].focus();
					blnGoodField = false;
				}
			}
		}

		// OPTION BUTTON
		if (strType == "_opt") {
			//if ((blnRequired) && (strTempField == "")) {
			if (blnRequired) {
				var blnAnyOptionsSelected = false;
				for (var intOpts = 0; intOpts < robjForm(robjForm[intLoopValidate].name).length; intOpts++) {
					if (robjForm(robjForm[intLoopValidate].name)[intOpts].checked == true) {
						blnAnyOptionsSelected = true;
					}
				}
				if (blnAnyOptionsSelected == false) {
					alert("Please select one of these options");
					robjForm(robjForm[intLoopValidate].name)[1].focus();
					blnGoodField = false;
				}
			}
		}

		// CHECK BOX
		if (strType == "_chk") {
			if ((blnRequired) && (strTempField == "")) {
				var blnAnyCheckedOff = false;
				var intNumberOfCheckBoxes = 0;
				for (var intElems = 0; intElems < robjForm.length; intElems++) {
					if (similarCheckBoxNames(robjForm.elements[intElems].name,robjForm[intLoopValidate].name)) {
						intNumberOfCheckBoxes++;
						if (robjForm.elements[intElems].checked == true) {
							blnAnyCheckedOff = true;
						}
					}
				}
				if (blnAnyCheckedOff == false) {
					if (intNumberOfCheckBoxes == 1) {
						alert("Please check this box to continue");
					} else {
						alert("Please check at least one of these boxes to continue");
					}
					robjForm[intLoopValidate].focus();
					blnGoodField = false;
				}
			}
		}

		if (!blnGoodField) {
			return false;
		}
	}
	return true;
}

function addRow(id){
  var value = 0;
  divTable.style.display = '';
  var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
  var lastRow = tbody.lastChild;
  var elementid = parseInt(parseInt(lastRow.id) + 1);
  var row = document.createElement("TR")
  row.id = elementid;
  var td1 = document.createElement("TD")
  td1.align = "center";
  var txt = document.createElement("<input type='button' name='cmddelete' id='cmddelete' value='remove' onclick=deleteRow('myTable','" + elementid + "')>")
  td1.appendChild (txt);
  var td2 = document.createElement("TD")
  td2.align = "left";
  var txt = document.createElement("<input type='text' name='txtTeamEmail_R_eml' id='txtTeamEmail'+value+'_R_eml' size='30' maxlength='100'>")
  td2.appendChild(txt);
  var td3 = document.createElement("TD")
  td3.align = "center";
  var txt = document.createTextNode("False")
  td3.appendChild(txt);
  row.appendChild(td1);
  row.appendChild(td2);
  row.appendChild(td3);
  tbody.appendChild(row);
}

function deleteRow(id,rowid){
  var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
  var row = document.getElementById(rowid);
  tbody.removeChild(row);
  var lastRow = tbody.lastChild;
  
  if (checkId() == 0) {
	divTable.style.display = 'none';
  } else {
	divTable.style.display = '';
  }
}

function checkId() {
  var value=0;
  var element;
  for (var i = 0; i < document.frmeBrigade.length; i++) {
	element = document.frmeBrigade.elements[i].name;
	if (element.substring(0,12) == 'txtTeamEmail') {
		value++;
  	}
  }
  return(value);
}

function doOpen(url) {
	window.open(url,'eBrigade','resizable=no,scrollbars=yes,toolbars=no,height=500,left=0,menubar=no,top=0,width=400',true);
}

function similarCheckBoxNames(strName1, strName2) {
	if (strName1.substring(strName1.length-6,strName1.length) != '_R_chk') return false;
	if (strName2.substring(strName2.length-6,strName2.length) != '_R_chk') return false;
	var intPos1 = 0;
	var intPos2 = 0;
	intPos1 = strName1.indexOf('_');
	intPos2 = strName1.indexOf('_');
	//alert(strName1+'\n'+intPos1);
	//alert(strName2+'\n'+intPos2);
	if (strName1.substring(0,intPos1+1) == strName2.substring(0,intPos2+1)) {
		//alert('They are in the same checkbox typegroup thingies...');
		return true;
	} else {
		return false;
	}
}

/* ============================================================= */

