/************************  ValidEmail  ************************/
// corrects common mistakes in an e-mail address
function MassageEmail(email) {
	// make all commas into periods
	var badChar = email.indexOf(",",0);
	while (badChar > -1) {
		email = email.substring(0,badChar) + "." + email.substring(badChar+1,email.length);
		badChar = email.indexOf(",",0);
	}
	// take all spaces out
	badChar = email.indexOf(" ",0);
	while (badChar > -1) {
		email = email.substring(0,badChar) + "" + email.substring(badChar+1,email.length);
		badChar = email.indexOf(" ",0);
	}
	return email;
}

// check for certain chars common to all email addresses
function ValidEmail(email) {
	if (email.length > 5) {
		if (email.indexOf("@",0) > -1) {
			if (email.indexOf(";",email.indexOf("@",0)+1) == -1) {
				if (email.indexOf("@",email.indexOf("@",0)+1) == -1) {
					if (email.indexOf(".",email.indexOf("@",0)) > -1) {
						return true;
					}
				}
			} else {
				return true;
			}
		}
	}
	return false;
}

/************************  Autotabbing  ************************/
function autoTab(input,len) {
	if(input.value.length >= len)
    	input.form[(getIndex(input)+1) % input.form.length].focus();
}

function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
		if (input.form[i] == input)
			index = i;
		else 
			i++;
	return index;
}

/************************  IsNumeric  ************************/
function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber = true;
	var Char;
	
	if (sText.length != 0) {
		for (i = 0; i < sText.length && IsNumber == true; i++) { 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) {
				IsNumber = false;
			}
		}
	}
	else {
		IsNumber = false;
	}
	
	return IsNumber;
}

/************************  Word Count  ************************/
function WordCount(this_field, e, maxchar, maxwords) {
	if (e.keyCode != 13) {
		var count = CountWords(this_field, false);
		if (count == maxwords+1)
			alert("You have reached " + maxwords + " words");
		else if (this_field.value.length == maxchar)
			alert("You have reached " + maxchar + "  characters allowed.");
		else if (count > maxwords+1)
			alert("You have more than " + maxwords + " words");
	}
}

function CountWords(this_field, show_word_count, show_char_count) {
	if (show_word_count == null)
		show_word_count = true;
	if (show_char_count == null)
		show_char_count = false;
	var char_count = this_field.value.length;
	var fullStr = this_field.value + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length -1;
	if (fullStr.length <2)
		word_count = 0;
	if (word_count == 1)
		wordOrWords = " word";
	else
		wordOrWords = " words";
	if (char_count == 1)
		charOrChars = " character";
	else
		charOrChars = " characters";
	if (show_word_count & show_char_count)
		alert("Word Count:\n" + "    " + word_count + wordOrWords + "\n" + "    " + char_count + charOrChars);
	else {
		if (show_word_count)
			alert("Word Count:  " + word_count + wordOrWords);
		else if (show_char_count)
			alert("Character Count:  " + char_count + charOrChars);
	}
	return word_count;
}

// Restrict a textarea to a maxlength
function MaxLength(textField,len) {
	len += 1;
	if (textField.value.length > len)
		textField.value = textField.value.substring(0,len);
}