// Test is the given string is made of all whitespace (includes empty string)
function isWhitespace(string)
{
	var ch;
	var isWhitespace = true;
	
	// Beware of undefined input.
	if ( !string ) { return true; }

	for (i = 0; i < string.length; i++) {
		ch = string.charAt(i);
		if ( ch != ' ' ) {
			isWhitespace = false;
			break;
		}
	}
	
	return isWhitespace;
}

// Trim off whitespace from the left side of the given string
function leftTrim(string)
{
	var ch;
	
	// Beware of empty string
	if ( isWhitespace(string) ) { return ''; }
	
	// Start scanning from the left end of the string
	for (i = 0; i < string.length; i++) {
		ch = string.charAt(i);
		if ( ch == ' ' ) {
			// We have encountered nothing but whitespace.  Keep going.
			continue;
		} else {
			// We encountered the first non-whitespace character.
			// Return the rest of the string.
			return string.substring(i);
		}
	}
	
}

// Trim off whitespace from the right side of the given string
function rightTrim(string)
{
	var ch;

	// Beware of empty string
	if ( isWhitespace(string) ) { return ''; }
	
	// Javascript's for...loop can only increment.
	// However we need to scan the string backwards.
	var index = string.length - 1;
	for (i = 0; i < string.length; i++) {
		ch = string.charAt(index);
		if ( ch == ' ' ) {
			// We have encountered nothing but whitespace.  Keep going.
			index--;
			continue;
		} else {
			// We encountered the first non-whitespace character.
			// Return the rest of the string.
			return string.substring(0, index+1);
		}
	}
}

// Trim off whitespace from both sides of the given string
function trim(string)
{
	return rightTrim(leftTrim(string));
}


function existEmbeddedWhitespace(string)
{
	var trimmedString = trim(string);
	
	if ( trimmedString.indexOf(' ') == -1 ) {
		return false;
	} else {
		return true;
	}
}


function isNumber(string)
{
	// Beware of empty string
	if ( isWhitespace(string) ) { return false; }

	if ( isNaN(trim(string)) ) {
		return false;
	}
	
	return true;
}


function isInteger(string)
{
	if ( isNaN(string) ) { return false; }
	
	var intString = parseInt(string);
	
	if ( intString == string ) {
		return true;
	}
	else {
		return false;;
	}
}


// Determine if all characters in 'string1' is found within 'string2'
function isAllCharsInStringValid(string1, string2)
{
	// Watch out for undefined or empty inputs
	if ( !string1 || string1 == '' ) { return false; }
	if ( !string2 || string2 == '' ) { return false; }
	
	var ch;
	
	// Loop through 'string1' and inspect each character
	for (i = 0; i < string1.length; i++) {
		ch = string1.charAt(i);
		// If a character is not found in 'string2'.  Return false.
		// Else continue to work on the next character.
		if ( string2.indexOf(ch) == -1 ) {
			return false;
		}
	}
	
	// Scanned through 'string1' and all characters are OK.
	return true;
}


// Determine the number of times 'ch' appears in 'string'
function countCharInString(ch, string)
{
	var count = 0;		// Initialize the count variable

	// Watch out for undefined or empty inputs
	if ( !ch || ch == '' ) { return count; }
	if ( !string || string == '' ) { return count; }
	
	for (i = 0; i < string.length; i++) {
		if ( ch == string.charAt(i) ) { 
			// Increment the count every time there is a match
			count++; 
		}
	}
	
	return count;
}


function isEmail(string)
{
	var validChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWKYZ.-_!1234567890';
	var trimmedString;
	
	// Reject empty string or string with all whitespace
	if ( isWhitespace(string) ) { 
		// alert('All whitespace');  
		return false; 
	}
	
	// Trim the input string.  The trimmed input string is the copy we can start working with.
	trimmedString = trim(string);
	
	// There must be exactly one '@' character in the string.  No more and no less.
	if ( countCharInString('@', trimmedString) != 1 ) { 
		// alert('No @ or too many @ in email');  
		return false; 
	}
	
	// The '@' sign may not be at either end of the string
	var position = trimmedString.indexOf('@');
	if ( position == 0 || position == trimmedString.length - 1 ) { 
		// alert('@ at either end of usename');  
		return false; 
	}

	// Break the email into its username and domain components
	var tempArray;
	tempArray = trimmedString.split('@');
	var username = tempArray[0];  // alert('Username - (' + username + ')');
	var domain = tempArray[1];  // alert('Domain - (' + domain + ')');

	// Check that the username part of the email address is made up of valid characters
	if ( !isAllCharsInStringValid(username, validChars) ) { 
		// alert('Invalid character in username');  
		return false; 
	}

	// Check that the domain part of the email address is made up of valid characters
	if ( !isAllCharsInStringValid(domain, validChars) ) { 
		// alert('Invalid character in domain');  
		return false; 
	}

	// There must be at least one '.' in the domain
	if ( countCharInString('.', domain) < 1 ) { 
		// alert('No . in domain');  
		return false; 
	}
	
	// No '.' may be at the left or right end of domain
	position = domain.indexOf('.');
	if ( position == 0 || position == domain.length - 1 ) { 
		// alert('. at either end of domain');  
		return false; 
	}
	
	// Everything checked out.  Return true.
	return true;	
}


// Check if the given string constitutes a valid date
function isDate(string) 
{
	// Reject empty string or string with all whitespace
	if ( isWhitespace(string) ) { return false; }
	
	var millisec = Date.parse(trim(string));
	
	// alert(millisec + ' msec');
	
	if ( isNaN(millisec) ) {
		return false;
	}
	else {
		return true;
	}
}