var whitespace = " \t\n\r";

function isEmpty(s){return ((s == null) || (s.length == 0))}
function isWhitespace (s)
{
	var i;
	if (isEmpty(s)) return true;
	for (i = 0; i < s.length; i++)
	{
	var c = s.charAt(i);
	if (whitespace.indexOf(c) == -1) return false;
	}
	return true;
}
function numberValid(checkStr)
{
	var checkOK = "0123456789";
	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)
	  {
	   //alert("Enter numbers 0 - 9");
	   //checkStr.focus();
	   //checkStr.select();
	   return false;
	  }
	}
	return true;
}
function zipValid(checkStr)
{
	var checkOK = "0123456789";
	if(checkStr.value.length!=5) return false;
	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)
	  {
	   //alert("Enter numbers 0 - 9");
	   //checkStr.focus();
	   //checkStr.select();
	   return false;
	  }
	}
	return true;
}
function areaCodeValid(checkStr)
{
	var checkOK = "0123456789";
	if(checkStr.value.length!=3) return false;
	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)
	  {
	   //alert("Enter numbers 0 - 9");
	   //checkStr.focus();
	   //checkStr.select();
	   return false;
	  }
	}
	return true;
}
function isEmail (s)
{
	s = s.value;
	if (isWhitespace(s)) return false;

	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
	var returnval=emailfilter.test(s)

	if (returnval==false){
		//alert("Please enter a valid email address.")
		return false;
	}
	else {
		//alert("OK")
		return true;	
	}

	/*
	var i = 2;
	var sLength = s.length;
	while ((i < sLength) && (s.charAt(i) != "@"))
	{ i++
	}
	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 3;
	while ((i < sLength) && (s.charAt(i) != "."))
	{ i++
	}
	if ((i >= sLength - 2) || (s.charAt(i) != ".")) return false;
	else return true;
	*/
}
function numberRealValid(checkStr)
{
	var checkOK = ".0123456789";
	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)
	  {
	   //alert("Enter numbers 0 - 9");
	   //checkStr.focus();
	   //checkStr.select();
	   return false;
	  }
	}
	return true;
}
 function emailValid(checkStr)
 {
    if(!isEmail(checkStr))
		{
		  // alert("Enter properly formatted email address!");
			return false;
		}
		else return true;
 }
 function phoneValid(checkStr)
 {
	var checkOK = "-0123456789 Ext.EXT()";
	ret = true;
	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)
	{
	  ret = false;
		break;
	}
	}
	if (ret == false)
	{
	 alert("Enter numbers 0 - 9 and -, space,(, ) or Ext.\nExample: (800)-555-4445 Ext. 4569");
	checkStr.focus();
	checkStr.select();
	return false;
	}
	return true;
 }
 function stringValid(checkStr) {
	var checkOK = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_/#.,'";

	if (isWhitespace(checkStr.value))
	{
	 // alert("Enter characters A-Z a-z 0-9 _ /#.,'");
	// checkStr.focus();
	// checkStr.select();
	return false;
	}
	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)
	{
	   // alert("Enter characters A-Z a-z 0-9 _ /#.,'");
	 // checkStr.focus();
	  //checkStr.select();
	  return false;
	}
	}
	return true;
 }
function alphaValid(checkStr) {
	var checkOK = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

	if (isWhitespace(checkStr.value))
	{
	 // alert("Enter characters A-Z a-z 0-9 _ /#.,'");
	// checkStr.focus();
	// checkStr.select();
	return false;
	}
	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)
	{
	   // alert("Enter characters A-Z a-z 0-9 _ /#.,'");
	 // checkStr.focus();
	  //checkStr.select();
	  return false;
	}
	}
	return true;
 }
function urlValid(checkStr) {
	var httpregex=/^(http:\/\/)[a-zA-Z0-9.\/]+(\/)$/
	var value=checkStr;
	var match=value.match(httpregex);
	if (match){
		return true;
	}else{
		return false;
	}
}

function purePhoneValid(checkStr)
{
	var checkOK = "0123456789";
	if(checkStr.value.length!=10) return false;
	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)
	  {
	   //alert("Enter numbers 0 - 9");
	   //checkStr.focus();
	   //checkStr.select();
	   return false;
	  }
	}
	return true;
}

function fullPhoneValid(checkStr) {
	var phone=checkStr.value;
	//10 digits
	var phoneRegExp = new RegExp(/(^\d{10}$)/);
	if(!phoneRegExp.test(phone)) {
		//alert('Exactly 10 digits');
		return false;
	}
	//10 concurrent digits
	var phoneRegExp = new RegExp(/(\d)\1{9,}/);
	if(phoneRegExp.test(phone)) {
		//alert('10 concurrent digits');
		return false;
	}
	//leading 1 or 0 check
	if(phone.substr(0,1)==0 || phone.substr(0,1)==1) {
		//alert('leading 1 or 0');
		return false;
	}
	return true;
}

function isNumeric(str){
  var re = /[\D]/g
  if (re.test(str)) return false;
  return true;
}

function isLength(str, len){
  return str.length == len;
}


function checkbox_checker(checkbox)	{
	// set var checkbox_choices to zero
	var checkbox_choices = 0;

	// Loop from zero to the one minus the number of checkbox button selections
	for (counter = 0; counter < checkbox.length; counter++)	{
		// If a checkbox has been selected it will return true
		// (If not it will return false)
		if (checkbox[counter].checked) checkbox_choices = checkbox_choices + 1;
	}

//	if (checkbox_choices > 3 ) {
//	// If there were more than three selections made display an alert box
//	msg="You're limited to only three selections.\n"
//	msg=msg + "You have made " + checkbox_choices + " selections.\n"
//	msg=msg + "Please remove " + (checkbox_choices-3) + " selection(s)."
//	alert(msg)
//	return (false);
//	}

	if (checkbox_choices < 1 ) {
		// If there were less then selections made display an alert box
//		alert("Please make three selections. \n" + checkbox_choices + " entered so far.")
		return (false);
	}

	// If three were selected then display an alert box stating input was OK
//	alert(" *** Valid input of three outfielders was entered. ***");
	return (true);
	}

function checkbox_array(checkbox)	{
	var checkbox_choices=new Array();
	var check = 0;
	
	// Loop from zero to the one minus the number of checkbox button selections
	for (counter = 0; counter < checkbox.length; counter++)	{
		// If a checkbox has been selected it will return true
		// (If not it will return false)
		if (checkbox[counter].checked) {
			checkbox_choices[check] = checkbox[counter].value;
			check = check + 1;
		}
	}

	return checkbox_choices;
	}

function move2next_3(field, nextFieldID, error){
	  if(field.value.length >= field.maxLength){
		if (!isLength(field.value, 3) || !isNumeric(field.value)){
		    alert('Enter valid '+error+' !');
		    field.focus();
		    field.select();
			return false;
		}
	    nextFieldID.focus();
	  }
}