/*
** VAL.JS - JavaScript Form Element Validation Functions
** -------------------------------------------------------------
**
** Function Overview:
** 
** isEmail    Checks for the presence of "@" and "." characters.
** isFilled   Checks for null and for empty as values.
** isInt      Checks whether each character in the field is a 
**            number between 0 and 9.
** isPhone    Checks for valid US phone number configuration
**            123-465-7890
**
** 
*/
    
// Check for valid email address: look for [@] and [.]
function isEmail(elm) {
    if (elm.indexOf("@") != "-1" &&
        elm.indexOf(".") != "-1" &&
        elm != "" &&
        elm != null) {
        return true;
    }
    else {
        return false;
    }
}

// Check for null and for empty
function isFilled(elm) {
    if (elm == "" ||
        elm == null) {
        return false;
    }
    else {
        return true;
    }
}

// Check for positive integer
function isInt(elm) {
    var elmstr = elm.value + "";
    if (elmstr == "") {
        return false;
    }
    for (var i = 0; i < elmstr.length; i++) {
        if (elmstr.charAt(i) < "0" ||
            elmstr.charAt(i) > "9") {
            return false;
        }
    }
        return true;
}

// Check for valid US phone number
// Function currently does not work as expected
function isPhone(elm) {
    var elmstr = elm.value;
    if (elmstr.length != 12) {
        return false;
    }
    for (var i = 0; i < elmstr.length; i++) {
        if ((i < 3 && i > -1) ||
            (i > 3 && i < 7) ||
            (i > 7 && i < 12)) {
            if (elmstr.charAt(i) < "0" ||
                elmstr.charAt(i) > "9") return false;
            }
        else if (elmstr.charAt(i) != "-") return false;
    }
return true;
}

// Returns array containing index(es) of checked checkbox(es) 
// in checkbox set, or -1 if no checkboxes are checked

function getCheckedCheckboxes(checkboxSet)
  {
  var arr = new Array();
  for (var i=0,j=0; i<checkboxSet.length; i++)
    if (checkboxSet[i].checked)
      arr[j++] = i;
  if (arr.length > 0)
    return arr;
  else
    return -1;
  }

// Checks whether any boxes in checkbox set are selected

function isSelectedCheckboxes(checkboxSet)
  {
  var arr = new Array();
  for (var i=0,j=0; i<checkboxSet.length; i++)
    if (checkboxSet[i].checked)
      arr[j++] = i;
  if (arr.length > 0)
    return true;
  else
    return false;
  }

function getCheckedRadioButton(date) {
	for (var i=0; i<date.length; i++)
	if (date[i].checked)
		return i;
	return -1;
  }

function valForm(form) {

  // validate Name field
  // begin by stripping leading/trailing blanks
  //form.name.value = chopField(form.name.value);

if (!isFilled(form.first.value))
    {
    alert("Please enter your first name.");
    form.first.focus();
    return false;
    }

if (!isFilled(form.last.value))
    {
    alert("Please enter your last name.");
    form.last.focus();
    return false;
    }


if (!isFilled(form.company.value)) {
	alert("Please enter the name of your company");
	form.company.focus();
	return false;
}

if (!isFilled(form.address1.value)) {
	alert("Please enter your street address");
	form.address1.focus();
	return false;
}


if (!isFilled(form.city.value)) {
	alert("Please enter your city");
	form.city.focus();
	return false;
}


if (!isFilled(form.state.value)) {
	alert("Please enter your state");
	form.state.focus();
	return false;
}


if (!isFilled(form.zip.value)) {
	alert("Please enter your ZIP code");
	form.zip.focus();
	return false;
}

if (!isFilled(form.country.value)) {
	alert("Please select your country");
	form.country.focus();
	return false;
}

if (!isFilled(form.tel.value)) {
	alert("Please enter your telephone number");
	form.tel.focus();
	return false;
}

if (!isEmail(form.email.value)) {
	alert("Please enter a valid email address: xxx@xxx.xxx");
	form.email.focus();
	return false;
}


if (!isSelectedCheckboxes(form.replay)) {
	alert("Please select a seminar.");
	return false;
}

  // returning false causes the form data NOT to be submitted
  // comment out these two lines for real-world applications
   //alert("Congratulations: Your form has validated!");
   //return false;    // for demo only!

  // uncomment this line for real-world applications
   return true;  // form valid - submit to ACTION URL

}