//==========================================================================
// fnSpawnWindow(): Spawns a new window with no toolbars, and no menubars.  
//		The Url opened, whether or not there are scrollbars, the width and
//		height are defined by the parameters.
//
//		Input: string, integer (0 or 1), integer, integer
//		Output: window object
//==========================================================================
function fnSpawnWindow(strUrl,intScroll,intWidth,intHeight) {
	var strWinFeatures = 'toolbars=0,menubar=0,scrollbars=' + intScroll + ',width=' + intWidth + ',height=' + intHeight;
	var objWindow	= window.open(strUrl, "subWindow", strWinFeatures);
}


//function changeWindowSize(siteName) {
//	var isNav = ((navigator.appName.indexOf("Netscape") != -1) && (navigator.appVersion.charAt(0) > 3));
//	var isIE  = (navigator.appName.indexOf("Microsoft") != -1);
//	if ( siteName == 'ublius' ) {
//		if (isNav){
//			resizeTo(770,590);
//		}
//		if (isIE){
//			resizeTo(770,715);
//		}
//	}
//	else if ( siteName == 'ospublius' ) {
//		if (isNav){
//			resizeTo(770,505);
//		}
//		if (isIE){
//			resizeTo(770,630);
//		}
//	}
//}


//====================================================
// fnLTrim() : trim all spaces from left side of string
//   Input   : string
//   Returns : string
//====================================================
function fnLTrim( myString ) {
	var string = new String(myString);
	var copy = false;
	var trim = '';

	// Trim whitespace from front of string
	for ( var i = 0 ; i < string.length ; i++ ) {
		ch = string.charAt(i);

		if ( ch != ' ' ) {
			copy = true;
		}

		if ( copy ) {
			trim = trim + ch;
		}
	}

	return trim;
}


//====================================================
// fnRTrim() : trim all spaces from right side of string
//   Input   : string
//   Returns : string
//====================================================
function fnRTrim( myString ) {
	var string = new String(myString);
	var copy = false;
	var trim = '';

	// Trim whitespace from end of string
	for ( var i = string.length-1 ; i >= 0 ; i-- ) {
		ch = string.charAt(i);

		if ( ch != ' ' ) {
			copy = true;
		}

		if ( copy ) {
			trim = ch + trim;
		}
	}

	return trim;
}


//====================================================
// trim() : trim all spaces from ends of string
//   Input   : string
//   Returns : string
//====================================================
function fnTrim( string ) {
	return fnRTrim(fnLTrim(string));
}


//==========================================================================
// fnValidateAll(): Validates the elements in the form submitted have all 
//		been filled in.  
//
//		Input: form
//		Output: boolean (if false, an alert as well)
//==========================================================================
function fnValidateAll(objForm) {
	
	for (i = 0; i < objForm.elements.length; i++) {
		if ( objForm.elements[i].type == 'text' || objForm.elements[i].type == 'password' ) {
			if (objForm.elements[i].value == '') {
				alert("You must fill in all fields.");
				objForm.elements[i].focus();
				return false;
			}
		}
		else if (objForm.elements[i].type == 'select-one' || objForm.elements[i].type == 'select-multiple' ) {
			if (objForm.elements[i].value == '') {
				alert("You must fill in all fields.");
				objForm.elements[i].focus();
				return false;
			}
		}
		else if (objForm.elements[i].type == 'checkbox') {
			if ( !fnValidateRadio(objForm.elements[i],objForm.elements[i].length) ) {
				return false;
			}
		}
		else if (objForm.elements[i].type == 'radio') {
			if ( !fnValidateRadio(objForm) ) {
				return false;
			}
		}
		else {
			if (objForm.elements[i].value == '') {
				alert("You must fill in all fields.");
				objForm.elements[i].focus();
				return false;
			}
		}
	}
	return true;
}


//==========================================================================
// fnValidateTxtFld(): Validates a page with only text elements in the form 
//		has each field filled in.  
//
//		Input: form
//		Output: boolean (if false, an alert as well)
//==========================================================================
function fnValidateTxtFld(objForm) {
	for (i = 0; i < objForm.elements.length; i++) {
		if (objForm.elements[i].value == '' && objForm.elements[i].name != 'StreetType') {
			alert("You must fill in all fields.");
			objForm.elements[i].focus();
			return false;
		}	
	}
	return true;
}


//==========================================================
// fnValidateCheckbox() : check that a checkbox is selected
//
//   Input		: element object
//   Returns	: boolean
//==========================================================
function fnValidateCheckbox(elem,len) {
	for ( var i = 0; i < len; i++ ) {
		if ( elem[i].checked ) { 	
			return true;
		}
	}
	
	if ( elem.checked ) {
		return true;
	}
	
	alert("Please select one of the checkboxes.");
	return false;
}


//==========================================================
// fnValidateRadio() : check that radio is selected
//
//   Input		: element object
//   Returns	: boolean
//==========================================================
function fnValidateRadio(objForm) {
	for ( var i = 0; i < objForm.ContactOption.length; i++ ) {
		if ( objForm.ContactOption[i].checked ) {
			objForm.RadioArrayCheckedVal.value = i
			return true;
		}
	}
	
/*	if ( elem.checked ) {
		return true;
	}*/
	
	alert("Please select one of the radio buttons.");
	return false;
}


//==========================================================================
// fnValidateNum(): Validates that a number has been filled in. 
//
//		Input: form element object
//		Output: Boolean
//==========================================================================
function fnValidateNum( objElement ) {
	var ch;
	var blnNum = true;
	
	// check string for digits if it has chars
	if ( objElement.value.length > 0 ) {
		
		for ( var i = 0 ; i < objElement.value.length ; i += 1 ) {
			ch = objElement.value.charAt(i);
		
			if ( ch < '0' || ch > '9' ) {
				blnNum = false;
			}
		}
		if ( !blnNum ) {
			fnNotifyInvalid(objElement,'Please fill in a valid numerical value.');
			return false;
		}
	}
	return true;
}


//==========================================================================
// fnValidateBirthYear(): Validate birth year entered. 
//
//		Input: form element object
//		Output: Boolean
//==========================================================================
function fnValidateBirthYear( objForm ) {
	if ( objForm.BirthYear.value.length != 4 && objForm.BirthYear.value.length != 2 ) {
		fnNotifyInvalid(objForm.BirthYear,'Please fill in a valid birth year.');
		return false;
	}
	
	if ( !fnValidateNum(objForm.BirthYear) ) {
		return false;
	}
	
	if ( objForm.BirthYear.value.length == 2 ) {
		objForm.BirthYear.value = '19' + objForm.BirthYear.value;
	}
	return true;
}


//==========================================================================
// fnSubmitForm(): Validates a page with only text elements in the form 
//		has each field filled in.  
//
//		Input: form
//		Output: boolean (if false, an alert as well)
//==========================================================================
function fnSubmitForm(objForm,strField) {
	
	if ( strField == 'name' || strField == 'addr' ) {
		if ( fnValidateTxtFld(objForm) ) {
			objForm.submit();
		}
	}
	else if ( strField == 'birthyear' ) {
		if ( fnValidateBirthYear(objForm) ) {
			objForm.submit();
		}
	}
}


//==========================================================================
// fnNewSearch(): Changes the form action and submits the form so the user 
//		can begin a new search.
//
//		Input: form
//		Output: none
//==========================================================================
function fnNewSearch(objForm) {
	objForm.action = 'enter_criteria.asp?SearchPage=ent_name';
	objForm.submit();
}


//==========================================================================
// fnNotifyInvalid(): Displays error message. 
//
//		Input: form element object, string
//		Output: none
//==========================================================================
function fnNotifyInvalid( objElement, strMessage ) {
	alert(strMessage);
	objElement.focus();
	objElement.select();
}


var isNav = ((navigator.appName.indexOf("Netscape") != -1) && (navigator.appVersion.charAt(0) > 3));
var isIE  = (navigator.appName.indexOf("Microsoft") != -1);
var NotInChr13 = true;


function CheckForEnterKey(evtObj){
 if (isNav){
  var fldType = evtObj.target.type;
  var asciiVal  = evtObj.which;
  }
 if (isIE){
  var evtObj = window.event;
  var fldType = evtObj.srcElement.type;
  var asciiVal  = evtObj.keyCode;
 }
 if ((asciiVal == 13) || (asciiVal == 3)){
  NotInChr13 = false;
  if (isIE){
   if ('undefined' != '' + fldType){
     fnSubmitForm(document.forms[0],'name');
   }
  }else{
   if (fnValidateTxtFld(document.forms[0])){
    document.forms[0].submit();
   }
  }
  NotInChr13 = true;
 }
}
if (isNav) document.captureEvents(Event.KEYPRESS);
document.onkeypress=CheckForEnterKey;