function IsNumeric(strString)
//  check for valid numeric strings	
// cribbed from http://www.pbdr.com/vbtips/asp/JavaNumberValid.htm
{
	var strValidChars = "0123456789.";
	var strChar;
	var blnResult = true;
	
	if (strString.length == 0) return false;
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
}

function cardval(s) 
{
// remove non-numerics
	var v = "0123456789";
	var w = "";
	for (i=0; i < s.length; i++) 
	{
		x = s.charAt(i);
		if (v.indexOf(x,0) != -1)
		w += x;
	}
	// validate number
	j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7) return false;
	k = Math.floor(j);
	m = Math.ceil(j) - k;
	c = 0;
	for (i=0; i<k; i++) 
	{
		a = w.charAt(i*2+m) * 2;
		c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
	return (c%10 == 0);
}

function validate1() 
{
   if (document.form1.FirstName.value == "") 
   {
       alert("Please enter First Name.");
	   document.form1.FirstName.focus();
	   return false;
   }

   if (document.form1.LastName.value == "")
    {
       alert("Please enter Last Name.");
	   document.form1.LastName.focus();
	   return false;
   }
   
   if (document.form1.StreetAddress.value == "")
    {
       alert("Please enter an address.");
	   document.form1.StreetAddress.focus();
	   return false;
   }

   if (document.form1.City.value == "")
   {
       alert("Please enter city.");
	   document.form1.City.focus();
	   return false;
   }

   var index = document.form1.State.selectedIndex;

   if (document.form1.state.options[index].value == "") 
   {
       alert("Please select State.");
	   document.form1.State.focus();
	   return false;
   }   

   if ((document.form1.ZipCode.value == "") || (document.form1.zipcode.value.length < 5)) 
   {
       alert("Please enter Zip code.");
	   document.form1.ZipCode.focus();
	   return false;
   }
   
   if (document.form1.phone_a.value == "" || document.form1.phone_a.value.length < 3) 
   {
       alert("Please enter phone number.");
	   document.form1.phone_a.focus();
	   return false;
   }
   if (document.form1.phone_b.value == "" || document.form1.phone_b.value.length < 3) 
   {
       alert("Please enter phone number.");
	   document.form1.phone_b.focus();
	   return false;
   }
   if (document.form1.phone_c.value == "" || document.form1.phone_c.value.length < 4) 
   {
       alert("Please enter phone number.");
	   document.form1.phone_c.focus();
	   return false;
   }
   
   //validate email
   if (document.form1.email.value == "") 
   {
       alert("Please enter email.");
	   document.form1.email.focus();
	   return false;
   } 
   else 
   {
      var str=document.form1.email.value;
	  var filter=/^(.*)@(.*)\.(.*)$/i;
	  if (!filter.test(str)) 
	  {
		alert("Invalid email address.");
		document.form1.email.focus();
		return false;
	  } 
	  else 
	  {
		  var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
		  if (str.match(illegalChars)) 
		  {
			  alert("The email address contains illegal characters.");
			  document.form1.email.focus();
			  return false;
		  }
	  }
   }
   
   if (document.form1.DollarAmount.value == "")
   {
	   alert("You must enter a  donation amount.");
   }
   
   if (!IsNumeric(document.form1.DollarAmount.value)) 
   {
       alert("You must enter a properly formated amount. Allowed characters are '0123456789.'.");
	   document.form1.DollarAmount.focus();
	   return false;
   }

	var index = document.form2.card_type.selectedIndex;

	if (document.form2.CardType.options[index].value == "") 
	{
		alert("Please select card type.");
		document.form2.CardType.focus();
		return false;
	}

	if (!cardval(document.form2.CardNumber.value))
    {
       alert("The card number you entered is invalid.");
	   document.form2.CardNumber.focus();
	   return false;
   }
   
   	var index = document.form2.card_month.selectedIndex;
	if (document.form2.card_month.options[index].value == "") 
	{
		alert("Please select expiration month.");
		document.form2.card_month.focus();
		return false;
	}
	
	var index = document.form2.card_year.selectedIndex;
	if (document.form2.card_year.options[index].value == "") 
	{
		alert("Please select expiration month.");
		document.form2.card_year.focus();
		return false;
	}

   return true;
}

