/*
	Tools.js by Mario Vargas
	Contains JavaScript functions for client-side form validation.
	Date: July, 2000
	Revision Date: January 31, 2006
	
	Copyright (C) 2001 The Findlay Publishing Company
*/
var NON_ALPHA_CHARS = "`~!@#$%^&*( )_+-={}[]|\":;'<>,./?\\";
var DIGITS = "0123456789";
var CAP_ALPHA_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var LOWCAP_ALPHA_CHARS = CAP_ALPHA_CHARS.toLowerCase();
var ALPHA_CHARS = CAP_ALPHA_CHARS + LOWCAP_ALPHA_CHARS;
var ALPHA_NUM_CHARS = ALPHA_CHARS + DIGITS;
var NON_DIGITS = ALPHA_CHARS + NON_ALPHA_CHARS;

function isSpace(character){var badChars=" \t\n\r";return(badChars.indexOf(character,0)>=0);}
function isStrSpace(str){for(i=0;i<str.length;i++ )if(!isSpace(str.charAt(i)))return false;return true;}

function supportsRegExp()
{
	// Are regular expressions supported?
	if( window.RegExp )
	{
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if( tempReg.test( tempStr ) )
		{
			return true;
		}
	}
	
	return false;
}


function ShowLetterCount( str )
{ // Shows a message box that displays the number of characters in a string.
  // Precondition: str is a String
  // Postcondition: A message box is displayed.

	var l;
	
	if( null == str || 0 == str.length )
		alert( "You have not yet entered anything." );
	else
		alert( "You have entered " + str.length + " characters." );
}

/**** E-mail verification function by webrefence.com
	http://webreference.com/js/tips/990928.html   *****/
function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

// This function is for backward-compatibility only.
// Most pages that use this module and that were made before February 2, 2001 will
//   use this function.
function verifyEmail( strEmailAddress )
{ // Determines whether a given e-mail address is valid.
  // Precondition: strEmailAddress is an e-mail address string.
  // Postcondition: Returns true if e-mail address meets certain criteria; otherwise it
  //   returns false.
  
	return isEmail( strEmailAddress );
}


function delNonAlphaNum( str ){str=""+str+"";newStr="";for(var i=0;i<str.length;i++)if(isAlphaNum(str.charAt(i)))newStr+=str.charAt(i);return newStr;}
function isAlphaNum(character){var badChars = "`~!@#$%^&*( )_+-={}[]|\":;'<>,./?\\";return (badChars.indexOf(character,0)<0);}

function hasSpace( str )
{ // Signals whether a given string has any space characters.
	return (str.indexOf( ' ' ) != -1);
}
function isDigit( character )
{ // Determines if a given character is a digit.

	return ( "0123456789".indexOf( character, 0 ) >= 0 );
} // end isDigit

function isNumeric( str )
{ // Determines if a given string is numeric

	str = "" + str + "";

	// if at least one character is not a digit, then return false
	for( var i = 0; i < str.length; i++ )
		if( !isDigit( str.charAt( i ) ) )
			return false;

	return true;  // string is numeric
} // end isNumeric

function areCheckBoxesEmpty( checkBox, numCheckBoxes )
{
	if( numCheckBoxes == 1 )
		return !(checkBox.checked);
	else
	{
		for( i = 0; i < numCheckBoxes; i++ )
			if( checkBox[ i ].checked )
				return false; // not empty
	}
	
	return true; // empty
} // end areCheckBoxesEmpty

var checkBoxesChecked = false;
function selectAllCheckBoxes( theForm, selAllCheckBoxName )
{ // Selects all checkboxes in given form.
  // Precondition: theForm is the form whose checkboxes will be checked.
  //   selAllCheckBoxName is the name of the checkbox that invoked this
  //   function.
  // Postcondition: All checkboxes are checked.
  
	checkBoxesChecked = !checkBoxesChecked;   // toggle checkboxes' state
	
	var elementI;
	for( i = 0; i < theForm.elements.length; i++ )
	{
		elementI = theForm.elements[ i ];
		if( elementI.type == "checkbox" && elementI.name != selAllCheckBoxName )
		{
			elementI.checked = checkBoxesChecked;
		}
	}
}

function changeFieldBGColor( formField, color )
{ // Changes the background color of a form field, given the field and a color.
  // Precondition: formField is the form field whose background color will be 
  //   changed. color is the background color of the object. 
  // Postcondition: The form field's background color is set to given color.
  //   Note that this only works on Internet Explorer 4.x and Netscape 6 and 
  //   later browsers.
  
	if( document.all || document.getElementById )
	{
		formField.style.backgroundColor = color;
	}
}

function validateNumberField( numberField, minNumber, maxNumber )
{ // Validates a number form field, given the form field, and the minimum and maximum
  //    values.
  // Precondition: numberField is an instance of the form field to validate.
  //   minNumber (optional) is the minimum number that may be entered in the field.
  //   maxNumber (optional) is the maximum number that may be entered in the field.
  // Postcondition: If the field's value is invalid, the function notifies the user
  //   about this and focus is set on this field. Returns true if no errors, otherwise,
  //   it returns false.
  
	var errorMsg = "";
	
	var theNumber = parseFloat( numberField.value );
	minNumber = (minNumber != 0 && minNumber)? minNumber : null;
	maxNumber = (maxNumber != 0 && maxNumber)? maxNumber : null;
	
	if( !( theNumber == 0 || theNumber ) )
		errorMsg = "Please enter a numeric value.";
	else if( theNumber < minNumber && minNumber != null )
		errorMsg = "This field does not accept values less than " + minNumber;
	else if( theNumber > maxNumber && maxNumber != null )
		errorMsg = "This field does not accept values greater than " + maxNumber;
	
	if( errorMsg )
	{
		alert( errorMsg );
		numberField.focus();
		
		return false;  // There were errors
	}
	else
	{
		return true;   // no errors
	}
}

function isStrAtLeast( minLength, str )
{ // Determines if a given string is at least a given minimum length.
  // Precondition: minLength is the minimum number of characters string str must have.
  //   str is the string whose length will be checked.
  // Postcondition: Returns true if str is at least minLength characters long. Otherwise,
  //   it returns false.
  
	return( str.length >= minLength );
}


/**************** String() **********************************/

// Add a function reverse as a method of the prototupe object of the 
//   String constructor.
String.prototype.reverse = reverseS;
function reverseS()
{ // Returns a string in reversed order
	var str = this.toString();
	var newStr = "";
		
	for( var i = str.length - 1; i >= 0; i-- )
		newStr += str.charAt( i );
		
	return newStr;
}

String.prototype.strip = stripS;
function stripS( charsToStrip )
{ // Filters all characters given in charsToStrip from current string.
  // Precondition: charsToStrip is a string containing all the characters to
  //   remove from the current string.
  // Postcondition: Returns the current string without these characters.

	var newString = "";
	
	// Include only those characters not in charsToStrip in the new string
	for( var i = 0; i < this.length; i++ )
		if( charsToStrip.indexOf( this.charAt( i ) ) == -1 )
			newString += this.charAt( i );
	
	return newString;	
}

// Add a function called trim as a method of the prototype 
// object of the String constructor.
String.prototype.trim = trimStr;
function trimStr()
{ // Removes beginning and ending space characters from given string.

	// Are regular expressions supported?
	var supported = false;
	if( window.RegExp )
	{
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if( tempReg.test( tempStr ) ) 
			supported = true;
	}

	if( supported )
	{
		return this.replace(/(^\s*)|(\s*$)/g, "");
	}
	else
	{
		// Implementation for older browsers that don't support regular expressions.
		
		if( 0 == this.length )
			return "";
		else if( !isSpace( this ) && this.length == 1 )  // only one nonwhite character?
			return new String( this );
		else
		{
			var a = 0,	// Index of first non-white space character from beginning of string
				b = 0;	// Index of first non-white space character from end of string
			
			// Search for first non-white space character, starting from beginning of string
			for( var i = 0; i < this.length; i++ )
			{
				if( !isSpace( this.charAt( i ) ) )
				{
					a = i;
					break;
				}
			}
			
			// Search for first non-white space character, starting from end of string
			for( var i = this.length - 1; i >= 0; i-- )
			{
				if( !isSpace( this.charAt( i ) ) )
				{
					b = i;
					break;
				}
			}

			//alert( "'" + this.substring( a, b + 1 ) + "'; [" + a + ", " + b + "]" );

			// String is blank (made of only white space characters)?
			if( a == b && 0 == b )
				return "";
			
			// Return substring between range [a, b]. 
			// We add 1 to b because substring() method does not include the character
			// at ending index.
			return this.substring( a, b + 1 );
		}
	}
}

String.prototype.count = countS;
function countS( subS, start, end )
{ // Returns the number of occurrences of given substring in a given string.
  // Precondition: subS is a string. subS is the substring to match in 
  //   current string instance.
  //   start, end: Indexes to start and end search.
  // Postcondition: The number of occurrences of subS in current string is returned.

	start = (start >= 0)? start : 0;
	end = (end > start)? end : this.length;

	var theStr = this.substring( start, end );
	var theStrLen = theStr.length;

	var count = 0;
	
	// count the number of occurrences of subS in theStr.
	var j = 0;  // keeps track of index where subS was last found
	for( var i = 0; i < theStrLen; i++ )
		if( (j = theStr.indexOf( subS, i )) != -1 )
		{
			count++;  // update tally
			i = j;  // start next search at j
		}

	return count;
}
