/*
** 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;
  }

// Returns index of checked radio button

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

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


// ************************* //
// FUNCTION TO VALIDATE FORM //
// ************************* //
function valForm(form) {

if (!isFilled(form.global.value))
    {
    alert("Please select the Global Region.");
    form.global.focus();
    return false;
    }
if (!isFilled(form.office.value))
    {
    alert("Please select your Local SIMULIA Office.");
    form.office.focus();
    return false;
    }
//alert(getCheckedValue(form.site));
if (getCheckedValue(form.site) == "sim")
    {
    form.num2.value = "";
    if (!isFilled(form.location.value))
        {
        alert("Please select your Preferred Location.");
        form.location.focus();
        return false;
        }
    } 
else
    {
    form.location.options[0].selected = true;
    if (!isInt(form.num2))
        {
        alert("Please enter a valid number of attendees.");
        form.num2.focus();
        form.num2.select();
        return false;
        }
    else
        {
        if (form.num2.value < "3")
           {
           alert("Minimum of 3 attendees required.");
           form.num2.focus();
           form.num2.select();
           return false;
           }
        }
    } 

if (form.courseOptions.value == " ")
    {
    alert("Please select a course.");
    form.courseOptions.focus();
    return false;
    }

  // validate Name field
if (!isFilled(form.custname.value))
    {
    alert("Please enter your name.");
    form.custname.focus();
    return false;
    }

//alert(form.phone.value);
if (!isFilled(form.phone.value))
    {
    alert("Please enter a valid phone number.");
    form.phone.focus();
    return false;
    }

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

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

//if (!isFilled(form.fax.value)) {
//	alert(form.office.value + "-");
//	form.fax.focus();
//	return false;
//}


   return true;  // form valid - submit to ACTION URL
}
