/*
	The isEmpty and isWhitespace functions are from Netscape's JavaScript dev site, http://developer.netscape.com.
	Some functions from http://www.4guysfromrolla.com/webtech/091998-1.shtml, which said to take them for free.
	I've added more functions and customized things as I went along... jfd 11/21/2002.
	Jay Davis, AMAZING CONCEPTS SOFTWARE, www.acsoft.biz
*/
// whitespace characters

var whitespace = " \t\n\r";
var numbers = "0123456789";

/****************************************************************/
// Check whether string s is empty.
function isEmpty(s)
{ return ((s == null) || (s.length == 0)) }

/****************************************************************/
function isWhitespace (s)
{
	var i;

	// Is s empty?
	if (isEmpty(s)) return true;  

	// Search through string's characters one by one
	// until we find a non-whitespace character.
	// When we do, return false; if we don't, return true.
	for (i = 0; i < s.length; i++)
	{
			// Check that current character isn't whitespace.
			var c = s.charAt(i);

			if (whitespace.indexOf(c) == -1) return false;
	}

	// All characters are whitespace.
	return true;
}

/****************************************************************/
/* function isAllNumbers, by Jay F. Davis, 1/27/2005            */
function isAllNumbers (s)
{
	var i;

	// Is s empty?
	if (isEmpty(s)) return true; // empty string is okay

	// Search through string's characters one by one
	// until we find a non-number character.
	// When we do, return false; if we don't, return true.
	for (i = 0; i < s.length; i++)
	{
		// Check that current character isn't a number.
		var c = s.charAt(i);
		if (numbers.indexOf(c) == -1) return false;
	}

	// All characters are numbers.
	return true;
} 

/****************************************************************/
function isBlank(val, str) {
	var strInput = new String(val.value);

	if (isWhitespace(strInput) || isEmpty(strInput)) {
		alert(str);
		return true;
	} else {
		return false;
	}

}

/****************************************************************/
function isBlankStr(str) {

	if (isWhitespace(str) || isEmpty(str)) {
		return true;
	} else {
		return false;
	}

}

/****************************************************************/
function passwordIsMismatched(val1, val2, str) {

	if (val1.value == val2.value) {
		return false;
	}
	else {
		alert(str);
		//alert("str1: " + val1.value + " str2: " + val2.value);
		return true;
	}

}

/****************************************************************/
function forbiddenValue(val, forbidStr, str) {

	if (val.value == forbidStr) {
		alert(str);
		return true;
	}
	else {
		return false;
	}

}
/****************************************************************/
// Added by VB
function requiredValue(val, reqStr, str) {

	if (val.value != reqStr) {
		alert(str);
		return true;
	}
	else {
		return false;
	}

}

/****************************************************************/
function bothFilled(val1, val2, str) {
	var strInput1 = new String(val1.value);
	var strInput2 = new String(val2.value);
	
	if (isWhitespace(strInput1) || isWhitespace(strInput2)) {
		return false;
	}
	else {
		alert(str);
		return true;
	}

}


/****************************************************************/
function bothEmpty(val1, val2, str) {
	var strInput1 = new String(val1.value);
	var strInput2 = new String(val2.value);

	if (!isWhitespace(strInput1) || !isWhitespace(strInput2)) {
		return false;
	}
	else {
		alert(str);
		return true;
	}

}

/****************************************************************/
/*function oneMatchesAnotherIsBlank by Veronica Brown, September 29 2005
**if one field's value (val1) matches a string (match1), then val2 cannot be blank.
**Not case-sensitive.
*/
function oneMatchesAnotherIsBlank(val1, match1, val2, str) {
	var strInput1 = new String(val1.value);

  if (strInput1.toLowerCase( ) == match1.toLowerCase( )) {
  	if (isBlank(val2, str)) {
  		return true;
  	} 
  }
   return false;
}

/****************************************************************/
/*function isProperDateNumber by Veronica Brown, September 29 2005
**Returns T/F if date number is appropriate for the month.
*/
function isProperDateNumber(mmIndex, dd) {
	// validates that the month and date entered make a valid date.
	// doesn't check leap years.
	// mmIndex is a month from 1 to 12. 
	// dd is the numbered date of the month to be tested.
	var max = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
	if ((dd > max[mmIndex-1]) || (dd < 1)) {
		return false;
	} else {
		return true;
	}
}
/****************************************************************/
/*function invalidDate by Veronica Brown, September 29 2005 */
function invalidDate(mval,dval,yval,str) {
	var mv = new String(mval.value);	
	var dv = new String(dval.value);	
	var yv = new String(yval.value);
	if ((isWhitespace(mv)) || (isWhitespace(dv)) || (isWhitespace(yv))) {
		alert(str);
		return true;
	}
	if (!isProperDateNumber(mv, dv)) {
		alert(str);
		return true;
	}
	return false;
}

/****************************************************************/
function notEmail(val1, str) {
	var strInput = new String(val1.value);

	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	
	var len = strInput.length;
	var rt = strInput.substring(len-4).toLowerCase();
	
	if (!supported) {
		if ((rt.substring(0,1) == "." || rt.substring(1,1) == ".") && strInput.indexOf("@") != -1) {
			return false;
		}
		else {
			alert(str);
			return true;
		}
	}
	else {
		// use regexp
		var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
		var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
		if (!r1.test(strInput) && r2.test(strInput)) {
			return false;
		}
		else {
			alert(str);
			return true;
		}
	}
	
}

/****************************************************************/
function ValidateApp() {
	// Data validations for online application:
	var f=document.forms[0].elements;
	if (	isBlank(f["Name"],"Please enter your name.") ||
			isBlank(f["Organization"],"Please enter your organization.") ||
			isBlank(f["City"],"Please enter your city.") ||
			bothEmpty(f["Phone"],f["Email"],"Please provide a way for us to respond: either Phone or Email.") 
		)
	{ 
		return false; 
	}
	else {
		return true;
	}
}

