/* UPVALID.JS - User-Profile Validation Library
** ============================================
**
** This library contains functions for validating
** the fields of the User Profile form (profile.htm).
**
** Note that these very primitive validation
** routines are designed solely to demonstrate
** JS SmartForms for the devHead ScriptHead column.
** Real-world validation would call for much more
** robust coding.
*/

function isTextBoxValid(str)  // is text-box str valid?
  {
  if (isBlank(str) || str.indexOf("|") != -1)  // nope - blank and/or |s
    return false;
  else                        // yep
    return true;
  }

function isTeacherIDBoxValid(str)  // Does TeacherID have 9 characters?
  {
  if (str.length != 9)  // nope - wrong str length
    return false;
  else                        // yep
    return true;
  }

function isListBoxValid(num)  // is list-box num valid?
  {
  if (num == 0)               // nope - blank (first) item selected
    return false;
  else                        // yep - nonblank item selected
  return true;
  }

function isZipValid(str)        // is zip str valid?
  {
  if (str.length != 5)          // nope - wrong str length
    return false;
  for (i=0; i<5; i++)
    {
    if (!isNum(str.charAt(i)))  // nope - # missing
      return false;
    }
  return true;                  // yep
  }

function isYearValid(str)        // is Year str valid?
  {
  if (str.length != 4)          // nope - wrong str length
    return false;
  for (i=0; i<4; i++)
    {
    if (!isNum(str.charAt(i)))  // nope - # missing
      return false;
    }
  return true;                  // yep
  }

function isEMailValid(str)                   // is email str valid?
  {
  if (isBlank(str) || str.indexOf("|") != -1)  // nope - blank and/or |s
    {
    alert("Enter your email address, please.\n(Do not use a | character.)")
    return false
    }
  var atsignPos = str.indexOf("@", 0)        // check for @
  if (atsignPos == -1)	                     // nope - @ missing
    {
    alert("Enter a valid email address with an @, please.")
    return false
    }
  if (str.indexOf(".", atsignPos) == -1)     // nope - . after @ missing
    {
    alert("Enter a valid email domain after the @, please.")
    return false
    }
  return true                                // yep
  }

function isPhoneValid(str)          // is phone str valid
  {
  if (str.length != 12)             // nope - wrong str length
    return false;
  for (i=0; i<12; i++)
    {
    if (i == 3 || i == 7)
      {
      if (str.charAt(i) != "-")     // nope - "-" missing
        return false;
      }
    else if (!isNum(str.charAt(i)))  // nope - # missing
      {
      return false;
      }
    }
  return true;                      // yep
  }

function isVisaValid(str)            // is VISA str valid?
  {
  if (str.length != 19)              // nope - wrong str length
    return false;
  for (i=0; i<19; i++)
    {
    if (i == 4 || i == 9 || i == 14)
      {
      if (str.charAt(i) != "-")      // nope - "-" missing
        return false;
      }
    else if (!isNum(str.charAt(i)))  // nope - # missing
      {
      return false;
      }
    }
  return true;                       // yep
  }

function isBlank(str)              // is str blank?
  {
  if (str.length == 0)             // yep - nothing entered
    return true
  for (i=0; i<=str.length-1; i++)  // yep - all spaces
    if (str.charAt(i) != " ")
      return false
  return true                      // nope
  }

function isNum(chr)            // is character a number?
  {
  if (chr < "0" || chr > "9")  // nope
    return false;
  else                         // yep
    return true;
  }
  function isNumeric(str)        // returns true if the string only contains characters 0-9
{
  for (i=0; i<=str.length-1; i++)
    {
    if (!isNum(str.charAt(i)))  // nope - # missing
      return false;
    }
  return true;                  // yep
  }
  
  function checkRadios(str) 
{
   var itemchecked = false;
   for(var j = 0 ; j < str.length ; ++j) 
   {
    if(str[j].checked) 
     itemchecked = true;
   }
   if(!itemchecked) 
  	return false;
else
  return true;
} 