var errorFieldColor = 'Red';
var nonerrorFieldColor = 'White';
var envFIRSTDAY = 'Friday';
var envLASTDAY = 'Sunday';

// Removes spaces from the front of the field
function trim( s ) {
	return s.replace(/^\s+|\s+$/, '');
}

// calculate the ASCII code of the given character
function CalcKeyCode( aChar ) {
	var character = aChar.substring(0,1);
	var code = aChar.charCodeAt(0);
	return code;
}

// Used to check menu selection is valid for the day of game
function checkDayTime( day, time ) {
	if( day.options[day.selectedIndex].text == envFIRSTDAY ) {
		if( time.options[time.selectedIndex].text.indexOf(envFIRSTDAY) > -1 ) {
			time.style.background = errorFieldColor;
			alert( "Start time is not valid for " + envFIRSTDAY );
		} else {
			time.style.background = nonerrorFieldColor;
		}
	} else if( day.options[day.selectedIndex].text == envLASTDAY ) {
		if( time.options[time.selectedIndex].text.indexOf(envLASTDAY) > -1 ) {
			time.style.background = errorFieldColor;
			alert( "Start time is not valid for " + envLASTDAY );
		} else {
			time.style.background = nonerrorFieldColor;
		}
	}
	return true;
}

// Used to check menu selection is "Other" with onChange event
// If so then activates otherTextBox; if not disables otherTextBox
function checkOther( menufield, otherTextBox ) {
	if( menufield.options[menufield.selectedIndex].text == 'Other' ) {
		otherTextBox.disabled = false;
	} else {
		otherTextBox.disabled = true;
		otherTextBox.value = "";
		otherTextBox.style.background = nonerrorFieldColor;
	}		
	return true;
}

// Used to check numeric field with onKeyUp event
function checkNumber( val ) {
	var strPass = val.value;
	var strLength = strPass.length;
	var lchar = val.value.charAt( (strLength) - 1 );
	var cCode = CalcKeyCode( lchar );

	// Check if the keyed in character is a number
	// do you want alphabetic UPPERCASE only ?
	// or lower case only just check their respective
	// codes and replace the 48 and 57

	if( cCode < 48 || cCode > 57 ) {
		var myNumber = val.value.substring(0, (strLength) - 1);
		val.value = myNumber;
	}
	return false;
}

// Checks a numeric field using onBlur
function checkNumeric( objName, minval, maxval, comma, period, hyphen ) {
	var numberfield = objName;

	if( chkNumeric( objName, minval, maxval, comma, period, hyphen ) == false ) {
		numberfield.select();
		numberfield.focus();
		numberfield.style.background = errorFieldColor;
		return false;
	} else {
		numberfield.style.background = nonerrorFieldColor;
		return true;
	}
}

// only allow 0-9 be entered, plus any values passed
// (can be in any order, and don't have to be comma, period, or hyphen)
// if all numbers allow commas, periods, hyphens or whatever,
// just hard code it here and take out the passed parameters
function chkNumeric(objName,minval,maxval,comma,period,hyphen) {
	var checkOK = "0123456789" + comma + period + hyphen;
	var checkStr = objName;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	
	for( i = 0; i < checkStr.value.length; i++ ) {
		ch = checkStr.value.charAt(i);
		for( j = 0; j < checkOK.length; j++ ) {
			if( ch == checkOK.charAt(j) ) {
				break;
			}
		}

		if( j == checkOK.length ) {
			allValid = false;
			break;
		}

		if( ch != "," ) {
			allNum += ch;
		}
	}

	if( !allValid ) {	
		alertsay = "Please enter only these values \""
		alertsay = alertsay + checkOK + "\" in the \"" + checkStr.name + "\" field."
		alert(alertsay);
		return false;
	}

	// set the minimum and maximum
	var chkVal = allNum;
	var prsVal = parseInt(allNum);
	if( chkVal != "" && !(prsVal >= minval && prsVal <= maxval) ) {
		alertsay = "Please enter a value greater than or "
		alertsay = alertsay + "equal to \"" + minval + "\" and less than or "
		alertsay = alertsay + "equal to \"" + maxval + "\" in the \"" + checkStr.name + "\" field."
		alert(alertsay);
		return false;
	}
}

// Checks Email addresses
// input data must contain at least an @ sign and a dot (.). Also, the @ must not be
// the first character of the email address, and the last dot must at least be one
// character after the @ sign.
function validateEmail( fld ) {
	var tfld = trim( fld.value );                        // value of field with whitespace trimmed off
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

	if( fld.value == "" ) {
		fld.style.background = errorFieldColor;
		return false;
	} else if( !emailFilter.test( tfld ) ) {              // test email for illegal characters
		fld.style.background = errorFieldColor;
		return false;
	} else if( fld.value.match( illegalChars ) ) {
		fld.style.background = errorFieldColor;
		return false;
	} else {
		fld.style.background = nonerrorFieldColor;
	}
	return true;
}

// Checking Start Day/Time to make sure Time is valid for the Day selected
function validateDayTime( day, time ) {
	time.style.background = nonerrorFieldColor;
	if( day.options[day.selectedIndex].text == envFIRSTDAY ) {
		if( time.options[time.selectedIndex].text.indexOf(envFIRSTDAY) > -1 ) {
			time.style.background = errorFieldColor;
			return false;
		}
	} else if( day.options[day.selectedIndex].text == envLASTDAY ) {
		if( time.options[time.selectedIndex].text.indexOf(envLASTDAY) > -1 ) {
			time.style.background = errorFieldColor;
			return false;
		}
	}
	return true;
}

// Checks to see if the provided field is all numeric
function validateNumeric( fld ) {
	var charpos = fld.value.search( "[^0-9]" );

	if( fld.value.length > 0 &&  charpos >= 0 ) {
		fld.style.background = errorFieldColor;
		return false;	// contains non-numeric characters
	} else {
		fld.style.background = nonerrorFieldColor;
	}
	return true;		// all digits
}
	
// Checks to see if the provided field has data
function validateEmpty( fld ) {
	var tfld = trim( fld.value );                        // value of field with whitespace trimmed off

	if( tfld == 0 ) {
		fld.style.background = errorFieldColor; 
		return false;	// field is empty
	} else {
		fld.style.background = nonerrorFieldColor;
	}
	return true;		// field contains characters
}

// Checks to see if the provided field does not have more than maxlimit
function validateLimit( fld, maxlimit ) {
	if( fld.value.length > maxlimit ) {
		fld.style.background = errorFieldColor; 
		return false;	// field is exceeds maxlimit
	} else {
		fld.style.background = nonerrorFieldColor;
	}
	return true;
}

// Checks to see if the provided menu selection is still set to 'Required'
function validatePulldownRequired( fld ) {
	if( fld.options[fld.selectedIndex].text == 'Required' ) {
		fld.style.background = errorFieldColor; 
		return false;
	} else {
		fld.style.background = nonerrorFieldColor;
	}
	return true;
}

// Checks to see if the provided menu selection is set to 'Other'
// if so then fldOther cannot be empty
function validatePulldownOther( fld, fldOther ) {
	if( fld.options[fld.selectedIndex].text == 'Other' ) {
		return validateEmpty( fldOther );
	}
	return true;
}

// Put the current date into the objName field
function stampForm( objName ) {
	objName.value = new Date();
}

// Put the current date into the objName field
function stampFormMDY( objName ) {
	var d = new Date();
	var m = d.getMonth() + 1;
	objName.value = m + "/" + d.getDate() + "/" + d.getFullYear();
}

// Limits the input of the supplied textarea
function textCounter( field, countfield, maxlimit ) {
	if( field.value.length > maxlimit ) { 		// if too long...trim it!
		field.value = field.value.substring( 0, maxlimit );
	} else {									// otherwise, update 'characters left' counter
		countfield.value = maxlimit - field.value.length;
	}
}

// Limits the input of the supplied textarea
function textCounterWithPopup( field, countfield, maxlimit, message ) {
	if( field.value.length > maxlimit ) { 		// if too long...trim it!
		alert( message );
		field.value = field.value.substring( 0, maxlimit );
	} else {									// otherwise, update 'characters left' counter
		countfield.value = maxlimit - field.value.length;
	}
}

