/* Input: A character.
 *
 * Returns: True if the character can be recognized as a
 * numeral False if it is anything else.
 * Last (Recorded) Update: Keoki 7/7/2002
 */

function isNumeral(character){
	if('0' <= character && '9' >= character){
		return true;
	}
	else{
		return false;
	}
}

/* The argument emailObj must have
 * something, then the at symbol (@),
 * then something, then a period, and
 * then something.
 * Last (Recorded) Update: Keoki 3/11/2003
 */

function emailOptionalError(emailObj){
	if(emailObj.value == ''){
		return false;
	}
	else{
		return emailError(emailObj);
	}
}

function emailOptionalCheck(emailObj){
	if(emailObj.value != '') emailCheck(emailObj);
}

function emailError(emailObj){
	var emailStr = emailObj.value;
	var idxSpace = emailStr.indexOf(' ');
	var idxAt = emailStr.indexOf('@');
	var idxPeriod = emailStr.indexOf('.', idxAt);
	if(idxSpace > -1 || idxAt < 1 || idxPeriod < idxAt + 2 || idxPeriod == emailStr.length - 1) return true;
	return false;
}

function emailCheck(emailObj){
	var emailStr = emailObj.value;
	var idxSpace = emailStr.indexOf(' ');
	var idxAt = emailStr.indexOf('@');
	var idxPeriod = emailStr.indexOf('.', idxAt);
	if(idxSpace > -1 || idxAt < 1 || idxPeriod < idxAt + 2 || idxPeriod == emailStr.length - 1){
		var emailObjTxt = 'document.' + emailObj.form.name + '.' + emailObj.name;
		alert('Email addresses should have the followiong format - alias@something.net.');
		setTimeout(emailObjTxt + '.focus(); ' + emailObjTxt + '.select();', 100);
	}
}

function phoneOptionalError(phoneObj){
	if(phoneObj.value == ''){
		return false;
	}
	else{
		return phoneError(phoneObj);
	}
}

function phoneOptionalCheck(phoneObj){
	if(phoneObj.value != '') phoneCheck(phoneObj);
}

function phoneError(phoneObj){
	var phoneStr = phoneObj.value;
	var phoneJustNumbersStr = '';
	for(var i = 0; i < phoneStr.length; i++){
		if(isNumeral(phoneStr.charAt(i))) phoneJustNumbersStr += '' + phoneStr.charAt(i);
	}
	if(phoneJustNumbersStr.length == '11' && phoneJustNumbersStr.charAt(0) == '1') phoneJustNumbersStr = phoneJustNumbersStr.substring(1);
	if(phoneJustNumbersStr.length == '7'){
		phoneObj.value = '(808) ' + phoneJustNumbersStr.substring(0, 3) + '-' + phoneJustNumbersStr.substring(3);
	}
	else if(phoneJustNumbersStr.length == '10'){
		phoneObj.value = '(' + phoneJustNumbersStr.substring(0, 3) + ') ' + phoneJustNumbersStr.substring(3, 6) + '-' + phoneJustNumbersStr.substring(6);
	}
	else{
		return true;
	}
	return false;
}

function phoneCheck(phoneObj){
	var phoneStr = phoneObj.value;
	var phoneJustNumbersStr = '';
	for(var i = 0; i < phoneStr.length; i++){
		if(isNumeral(phoneStr.charAt(i))) phoneJustNumbersStr += '' + phoneStr.charAt(i);
	}
	if(phoneJustNumbersStr.length == '11' && phoneJustNumbersStr.charAt(0) == '1') phoneJustNumbersStr = phoneJustNumbersStr.substring(1);
	if(phoneJustNumbersStr.length == '7'){
		phoneObj.value = '(808) ' + phoneJustNumbersStr.substring(0, 3) + '-' + phoneJustNumbersStr.substring(3);
	}
	else if(phoneJustNumbersStr.length == '10'){
		phoneObj.value = '(' + phoneJustNumbersStr.substring(0, 3) + ') ' + phoneJustNumbersStr.substring(3, 6) + '-' + phoneJustNumbersStr.substring(6);
	}
	else{
		var phoneObjText = 'document.' + phoneObj.form.name + '.' + phoneObj.name;
		alert('Please use the followiong format - (808) 987-6543.');
		setTimeout(phoneObjText + '.focus(); ' + phoneObjText + '.select();', 100);
	}
}

function wordCountError(textObj, countObj, maxWordsInt){
	var textStr = textObj.value.replace(/^[^a-zA-Z0-9]+/, '') + ' ';
	textStr = textStr.replace(/[^a-zA-Z0-9]+/g, ' ');
	var wordArr = textStr.split(' ');
	var wordsInt = (textStr == ' ') ? 0 : wordArr.length - 1;
	countObj.value = wordsInt;
	if(wordsInt > maxWordsInt) return true;
	return false;
}

function wordCountCheck(textObj, countObj, maxWordsInt, e){
	if(window.Event){ // Navigator
		var keyPressed = e.which;
	}
	else{ // Internet Explorer
		var keyPressed = e.keyCode;
	}
	var textStr = textObj.value.replace(/^[^a-zA-Z0-9]+/, '') + ' ';
	textStr = textStr.replace(/[^a-zA-Z0-9]+/g, ' ');
	var wordArr = textStr.split(' ');
	var wordsInt = (textStr == ' ') ? 0 : wordArr.length - 1;
	countObj.value = wordsInt;
	if(maxWordsInt > 0 && wordsInt > maxWordsInt && keyPressed != 13 && keyPressed != 8 && keyPressed != 46 && keyPressed != 91 && keyPressed != 16){
		alert('Please enter ' + maxWordsInt + ' words or less.');
	}
}

/* Will advance a large drop down menu to
 * the option alphabetically nearest the
 * value typed into the search text box.
 * This version skips the first 2 options.
 * Last (Recorded) Update: Keoki 1/15/2003
 */

function dropdownAdvance(searchObj, dropdownObj){
	var searchString = searchObj.value.toLowerCase();
	var dropValue = '';
	var dropValuePrevious = '';
	for(var i = 1; i < dropdownObj.options.length; i++){
		dropValue = dropdownObj[i].text.toLowerCase();
		dropValue = dropValue.substring(0, searchString.length);
		if(searchString == dropValue){
			dropdownObj.selectedIndex = i;
			return;
		}
		else if(searchString < dropValue){
			if(i == 0){
				dropdownObj.selectedIndex = i;
				return;
			}
			else{
				dropValuePrevious = dropdownObj[i - 1].text.toLowerCase();
				for(var j = searchString.length - 1; j > 0; j--){
					searchString = searchString.substring(0, j);
					dropValue = dropValue.substring(0, j);
					dropValuePrevious = dropValuePrevious.substring(0, j);
					if(searchString == dropValue){
						dropdownObj.selectedIndex = i;
						return;
					}
					else if(searchString == dropValuePrevious){
						dropdownObj.selectedIndex = i - 1;
						return;
					}
				}
				dropdownObj.selectedIndex = i;
				return;
			}
		}
	}
}

function setDropdown(currentValue, defaultValue, dropdownObj){
	for(var i = 0; i < dropdownObj.options.length; i++){
		if(dropdownObj[i].value == currentValue){
			dropdownObj.selectedIndex = i;
			return;
		}
	}
	for(var i = 0; i < dropdownObj.options.length; i++){
		if(dropdownObj[i].value == defaultValue){
			dropdownObj.selectedIndex = i;
			return;
		}
	}
}

function setCheckboxList(currentValues, checkboxObj){
	var currentValuesP = '#' + currentValues + '#';
	if(currentValues && currentValuesP != '##' && checkboxObj){
		if(checkboxObj[0]){
			for(var i = 0; i < checkboxObj.length ; i++){
				if(currentValuesP.indexOf('#' + checkboxObj[i].value + '#') != -1){
					checkboxObj[i].checked = true;
				}
			}
		}
		else{
			if(currentValuesP.indexOf('#' + checkboxObj.value + '#') != -1){
				checkboxObj.checked = true;
			}
		}
	}
}

function clearCheckboxList(objCheckbox, strStartValue, strAllValues){
	if(objCheckbox){
		if(strStartValue){
			if(objCheckbox[0]){
				for(var i = 0; i < objCheckbox.length ; i++){
					if(objCheckbox[i].value.indexOf(strStartValue) == 0){
						objCheckbox[i].checked = false;
					}
				}
			}
			else{
				if(objCheckbox.value.indexOf(strStartValue) == 0){
					objCheckbox.checked = false;
				}
			}
		}
		else if(strAllValues){
			if(objCheckbox[0]){
				for(var i = 0; i < objCheckbox.length ; i++){
					if(strAllValues.indexOf('#' + objCheckbox[i].value + '#') >= 0){
						objCheckbox[i].checked = false;
					}
				}
			}
			else{
				if(strAllValues.indexOf('#' + objCheckbox.value + '#') >= 0){
					objCheckbox.checked = false;
				}
			}
		}
		else{
			if(objCheckbox[0]){
				for(var i = 0; i < objCheckbox.length ; i++){
					objCheckbox[i].checked = false;
				}
			}
			else{
				objCheckbox.checked = false;
			}
		}
	}
}

function noneCheckedSimple(objCheckbox){
	if(objCheckbox){
		if(objCheckbox[0]){
			for(var i = 0; i < objCheckbox.length ; i++){
				if(objCheckbox[i].checked) return false;
			}
		}
		else if(objCheckbox.checked){
			 return false;
		}
	}
	return true;
}

function noneChecked(objCheckbox, strAllValues){
	if(objCheckbox){
		if(objCheckbox[0]){
			for(var i = 0; i < objCheckbox.length ; i++){
				if(objCheckbox[i].checked && (!strAllValues || strAllValues.indexOf('#' + objCheckbox[i].value + '#') >= 0)) return false;
			}
		}
		else if(objCheckbox.checked && (!strAllValues || strAllValues.indexOf('#' + objCheckbox[i].value + '#') >= 0)){
			 return false;
		}
	}
	return true;
}

var detailWindow;

function popDetail(id){
	detailWindow = window.open('/eventDetail.atm?' + id, 'eventDetail', 'width=720,height=540');
}
