
function Form() {} // empty constructor


  // a little helper function to let 'Place A Hold' widgets call Place_A_Hold.html
Form.placeAHold = function( pObj )
  {
  var title = '', author = '', isbn = '';
  if ( pObj.title && pObj.title.value )
    title  = encodeURIComponent( pObj.title.value );
  if ( pObj.author && pObj.author.value )
    author = encodeURIComponent( pObj.author.value );
  if ( pObj.isbn && pObj.isbn.value )
    isbn = encodeURIComponent( pObj.isbn.value );
  var s = "../Books/Place_A_Hold.html?title=" + title + "&author=" + author + "&isbn=" + isbn;
  window.location.href = s;
  return false;
  }


Form.checkEntry = function( pObj, pLabel, pIsOptional )
  {
  return Form._doCheck( pObj, pLabel, pIsOptional, null )
  }


Form.checkSelection = function( pObj, pLabel, pIsOptional )
  {
  var ndx = pObj.options.selectedIndex; // save selection
  if ( ndx == 0 )
    return 'No selection made for "' + pLabel + '"';
  else
    return '';
  }


Form.checkZip = function( pObj, pLabel, pIsOptional )
  {
  function checkIt( pValue )
    {
    var valid = true;
    var regExp = /^(\d\d\d\d\d)[^\d]*(\d\d\d\d)$/; // nnnnn-nnnn
    var match = value.match( regExp );
    if ( match == null )
      {
      regExp = /^\d\d\d\d\d$/; // nnnnn
      valid = value.match( regExp ) != null;
      }
    return valid;
    }
  return Form._doCheck( pObj, pLabel, pIsOptional, checkIt )
  }


Form.checkEmail = function( pObj, pLabel, pIsOptional )
  {
  function checkIt( pValue )
    {
    var regExp = /^([^@\.]+)@([^@\.]+)((\.[^@\.]+)+)$/;
    return pValue.match( regExp ) != null;
    }
  return Form._doCheck( pObj, pLabel, pIsOptional, checkIt )
  }


Form.checkPhone = function( pObj, pLabel, pIsOptional )
  {
  function checkIt( pValue )
    {
    var valid = true;
    var regExp = /^[^\d]*(\d\d\d)[^\d]*(\d\d\d)[^\d]*(\d\d\d\d)$/;
    var match = pValue.match( regExp );
    if ( match == null )
      {
      regExp = /^(\d\d\d)[^\d]*(\d\d\d\d)$/;
      valid = pValue.match( regExp ) != null;
      }
    return valid;
    }
  return Form._doCheck( pObj, pLabel, pIsOptional, checkIt )
  }


  // a 'number' such as 0-201-59625-3
  // Note: the last digit is a base 11 check digit, where 10 == 'X'
Form.checkISBN = function( pObj, pLabel, pIsOptional )
  {
    // The check digit is the sum of the digit number times the digit, modulo 11,
    // with "10" represented by the character "X"
  function doISBNChecksum( pISBN )
    {
    if ( typeof pISBN == "number" )
      pISBN = '' + pISBN;
  
    if ( typeof pISBN != "string" )
      return null;
  
    if ( pISBN.length != 10 && pISBN.length != 13 )
      return null;
  
    if ( ! pISBN.match( /[\dX]$/ ) )
      return null;
  
    var sum = 0;
    for ( var i = 0; i < pISBN.length - 1; i++ )
      {
      var c = pISBN.charAt( i );
  
      if ( ! c.match( /^\d$/ ) )
        return null;
  
      sum += c * (i + 1);
      }
    sum = sum % 11;
    sum = '' + sum;
  
    if ( sum == '10' )
      sum = 'X';
  
    return sum;
    }
  function checkIt( pValue )
    {
    var valid = true;
    pValue = pValue.replace( /[-\s]+/g, '' );
    if ( pValue.length != 10 && pValue.length != 13 )
      {
      valid = false;
      }
    else
      {
      var c = doISBNChecksum( pValue );
      if ( c == null )
        valid = false;
      if ( c != pValue.charAt( pValue.length - 1 ) )
        valid = false;
      }
    return valid;
    }
  return Form._doCheck( pObj, pLabel, pIsOptional, checkIt )
  }


Form.checkLibraryCardNum = function( pObj, pLabel, pIsOptional )
  {
  function checkIt( pValue )
    {
    pValue = pValue.replace( /\s+/g, '' );
    return pValue.match( /^\d{14}$/ ) != null;
    }
  return Form._doCheck( pObj, pLabel, pIsOptional, checkIt )
  }


Form._doCheck = function( pObj, pLabel, pIsOptional, pFunc )
  {
  var msg = '';
  var value = Utils.trim( pObj.value );
  pObj.value = value;
  if ( value.length == 0 )
    {
    if ( ! pIsOptional )
      msg = 'The "' + pLabel + '" is missing.';
    }
  else if ( pFunc )
    {
    var valid = pFunc( value );
    if ( ! valid )
      msg = 'Not a valid "' + pLabel + '".';
    }
  return msg;
  }







