function objExists(e)
{
    var exists = eval(e);
    if(exists)
    {
        return true;
    }
    else
    {
        return false;
    }        
}

// CHECKS THE FIELDS
function checkFields() {
missinginfo = "";
if (document.contact.firstname.value == "") {
missinginfo += "\n     -  Please enter your First Name";
}
if (document.contact.lastname.value == "") {
missinginfo += "\n     -  Please enter your Last Name";
}
if (document.contact.company.value == "") {
missinginfo += "\n     -  Please enter your Company Name";
}
if (objExists(document.contact.ddlMgmtLevel) == true) {
    if (document.contact.ddlMgmtLevel.options[document.contact.ddlMgmtLevel.selectedIndex].value == "") {
        missinginfo += "\n     -  Please enter your Management Level";
    }
}
if (objExists(document.contact.area) == true) {
    if (document.contact.area.options[document.contact.area.selectedIndex].value == "") {
        missinginfo += "\n     -  Please enter your Functional Area";
    }
}
if (document.contact.title.value == "") {
    missinginfo += "\n     -  Please enter your Title";
}
if (document.contact.email.value == "") {
missinginfo += "\n     -  Please enter your E-mail Address";
}
if (document.contact.confirm_email.value == "") {
missinginfo += "\n     -  Please enter your Confirm E-mail Address";
}
if (document.contact.confirm_email.value.toString() != document.contact.email.value.toString())
{
missinginfo += "\n     -  Your Confirm E-mail Address Does Not Match Your E-mail Address";
}
if (document.contact.phone.value == "") {
missinginfo += "\n     -  Please enter your Phone";
}
if(objExists(document.contact.cpStr) == true)
{
    if (document.contact.cpStr.value == "")
    {
        missinginfo += "\n     -  Please enter the Security Code";
    }
}
// WRITES THE ALERT CONTAINING THE MISSING INFORMATION
if (missinginfo != "") {
missinginfo = "_____________________________\n" +
"The following issues need to be addressed:\n" +
missinginfo + "\n_____________________________" +
"\nPlease enter and submit again!";
alert(missinginfo);
return false;
	} else {
	return true;
  }
}

//EMAIL VALIDATION
function VerifyEmailAddress()
{
  var Reason  = "E-mail Address entered incorrectly.  \n\nReason:"
  var checkStr = document.contact.email.value;
  var RC = true;
  var x = AtSignValid = DoublePeriod = PeriodValid = SpaceValid = RL = 0;

  for (i = 0;  i < checkStr.length;  i++)
  {
    if (checkStr.charAt(i) == '@')
      AtSignValid++;
    else if (checkStr.charAt(i) == '.')
    {
      if (x == (i-80))
        DoublePeriod++;
      else
      {
        x = i;
        PeriodValid++;
      }
    }
    else if (checkStr.charAt(i) == ' ')
      SpaceValid ++;
  }
  RL = Reason.length;
  if (AtSignValid != 1)
    Reason += "\nOnly one '@' allowed, " + AtSignValid + " found.";
  if (PeriodValid == 0)
    Reason += "\nAddress must contain at least one period.";
  if (SpaceValid > 0)
    Reason += "\nNo Spaces allowed. Address contains " + SpaceValid + " space";
  if (SpaceValid > 1)
    Reason += "s.";
  if (DoublePeriod > 0)
    Reason += "\nAddress contains multiple periods in a row.";
  if (checkStr.length > 120)
    Reason += "\nPlease limit the Email Address to 120 characters.";

  if (RL != Reason.length)
  {
    if (confirm(Reason))
    {
      document.contact.email.focus();
      RC = false;
    }
    else
      RC = true;
  }
  else
    RC = true;
  return(RC);
}

// CHECKS TO MAKE SURE THAT THE CHECKBOXES HAVE AT LEAST ONE BOX CHECKED
function validateCheckboxes(numBoxes, numToSelect, boxesName) {
        var numBoxesSelected = 0;
        var allValues = "";
        var valuesSelected = new Array;
        for (var i=1; i <= numBoxes; i++) {
                if (eval("document.contact.interest_area" + i + ".checked")) {
                    valuesSelected[numBoxesSelected] = eval("document.contact.interest_area" + i + ".value");
                    allValues += valuesSelected[numBoxesSelected] + "\t";
                    numBoxesSelected++;
                }
        }
        if (numBoxesSelected < numToSelect) {
            alert("Please select at least one program you are interested in.");
        return false;
        } else {
        return true;
        }
}

// MASTER VALIDATION ROUTINE

function isCorrect() {
	if (!checkFields())
	{
		return false;
	}
	else
	{
		if (!VerifyEmailAddress())
		{
		    return false;
		}
		else
		{
		    if(!validateCheckboxes(7,1,""))
		    {
		        return false;
		    }
		}
	}
return true;
}
