function checkFields(formObj)
{
  with(document.webform){
    if (fname.value == ""){
      alert("Please provide your first name.");
      fname.focus();
    }
    else if (lname.value == ""){
      alert("Please provide your last name.");
      lname.focus();
    }
    else if (!(isEmail(email.value))) {
      alert("Please provide a valid email address.");
      email.focus();
    }
    else if (streetAddr1.value == ""){
      alert("Please provide your street address.");
      streetAddr1.focus();
    }
    else if (city.value == ""){
      alert("Please provide your city.");
      city.focus();
    }
    else if ((location[0].checked == true) && (state.value == "")){
      alert("Please provide your state.");
      state.focus();
    }
    else if (zip.value == ""){
      alert("Please provide your zip code.");
      zip.focus();
    }
    else if (!isPhoneNumber(phone.value, location[0].checked)){
      alert("Please provide valid phone number.");
      phone.focus();
    }
    else if (!(parseInt(bYear.value) >= 0) || parseInt(bYear.value) < 1900 || parseInt(bYear.value) > 2000){
      alert("Please provide a valid birth date.");
      bMonth.focus();
    }
    else if ((payment[0].checked == false) && (payment[1].checked == false) && (payment[2].checked == false)){
      alert("Please choose your payment preference.");
      payment[0].focus();
    }
    else if ((location[1].checked == true) && (countryOther.value == "") ){
      alert("For foreign membership, please specify your country.");
      countryOther.focus();
    }
    else if (oath.checked == false){
      alert("You must agree to the KCBS oath to join!");
      oath.focus();
    }
    else{
	  emailPreference.value = 'n';
	  opt_out.value = 'n';
      if(emailPreference.checked)
         emailPreference.value = 'y';

      if(opt_out.checked)
         opt_out.value = 'y';
      nobots.value = "human";
	  document.webform.phone.value = formatPhone(document.webform.phone.value);
	  document.webform.altphone.value = formatPhone(document.webform.altphone.value);
      document.webform.submit();
    }
  }
  return false;
}

function isPhoneNumber(str, domestic)
{
	var digits = "0123456789";
	// non-digit characters which are allowed in phone numbers
	var phoneNumberDelimiters = "()- ";
	if(!domestic)
		phoneNumberDelimiters = phoneNumberDelimiters + "+";
	// U.S. phone numbers have 10 digits.
	// They are formatted as 123 456 7890 or (123) 456-7890.
	var minDigitsInPhoneNumber  = 10;
	
	var valid = 1;
	var good_chars = digits + phoneNumberDelimiters;
	var i = 0;
	if (str =="") {
		// Return false if number is empty
		return false;
	}
	
	for (i =0; i <= str.length -1; i++) {
		if (good_chars.indexOf(str.charAt(i)) == -1) {
			return false;
		} 
	}
	var strPhone = stripCharsInBag(str, phoneNumberDelimiters);
	return (isInteger(strPhone) && strPhone.length >= minDigitsInPhoneNumber);
}

function formatPhone(phonenum) {
	var phonenum = phonenum.replace(/[^0-9]/g, '');
	var p1  = phonenum.substring(0,3);
	var p2  = phonenum.substring(3,6);
	var p3  = phonenum.substring(6,10);
	var res = p1 + '-' + p2 + '-' + p3;
	return res;
}

// Removes all characters which appear in string bag from string s.
function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function isEmail(str) {
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
		}
	if (!supported)
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
		var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
		var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}

function setCountry(num)
{
	var countryRow = document.getElementById("country");
	if(num)
		countryRow.style.display = '';
	else
		countryRow.style.display = 'none';  
}
function toggleFamMemVis(id)
{
	var famRows = document.getElementById("famMem");
	if (id == 2 || id == 4 || id == 6)
		famRows.style.display = '';
	else
		famRows.style.display = 'none'; 
} 