// function returns true if a string contains only whitespace characters.

function isBlank( strValue ) {
  for ( var i = 0; i < strValue.length; i++ ) {
    var strChar = strValue.charAt( i );
    if ( ( strChar != ' ' ) && ( strChar != '\n' ) && ( strChar != '\t' ) ) {
      return false;
    }
  }
  return true;
}

// function returns true if a string is a validly formatted email address
// email requirements:
// - exactly one "@" sign, and not the first character
// - at least one "." after the "@"
// - the "." is not the first character after the "@"
// - the "." is not the last character
// - no 2 "." in a row

function isEmail( objEmail ) {
  var intAtPos;
  var intDotPos1;
  var intDotPos2;

  intAtPos = objEmail.value.indexOf( "@", 1 );
  if ( intAtPos != -1 ) {
    if ( objEmail.value.indexOf( "@", intAtPos + 1 ) != -1 ) {
      return false;
    }

    intDotPos1 = intAtPos;
    while ( ( intDotPos2 = objEmail.value.indexOf( ".", intDotPos1 + 1 ) ) != -1 ) {
      if ( intDotPos2 - intDotPos1 == 1 ) {
        return false;
      }

      intDotPos1 = intDotPos2;
    }

    if ( intDotPos1 < ( intAtPos + 2 ) ) {
      return false;
    }

    if ( intDotPos1 == objEmail.value.length - 1 ) {
      return false;
    }
  }
  else {
    return false;
  }
  return true;
}

function isNumeric( strValue ) {
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

  return objRegExp.test( strValue );
}

function validateDate( strYear, strMonth, strDay ) {
  intYear  = parseInt( strYear );

  if ( strMonth.charAt(0) != "0" ) {
    intMonth = parseInt( strMonth );
  }
  else {
    intMonth = parseInt( strMonth.charAt(1) );
  }

  if ( strDay.charAt(0) != "0" ) {
    intDay = parseInt( strDay );
  }
  else {
    intDay = parseInt( strDay.charAt(1) );
  }

  var arrLookup = { '01' : 31, '03' : 31, '04' : 30, '05' : 31, '06' : 30, '07' : 31, '08' : 31, '09' : 30, '10' : 31, '11' : 30, '12' : 31 }
  var strReturn = "";

  if ( ( strYear.length  == 4 ) && ( strMonth.length == 2 ) && ( strDay.length   == 2 ) && ( ( intMonth < 13 ) && ( intMonth > 0 ) ) && ( ( intDay   < 32 ) && ( intDay   > 0 ) ) ) {
    if ( strMonth == "02" ) {
      if( ( intYear % 4 == 0 && intDay <= 29 ) || ( intYear % 4 != 0 && intDay <=28 ) ) {
        return strReturn;
      }
      else {
        if ( intYear % 4 == 0 && intDay >= 29 ) {
          strReturn = "- This year is a leap year and therefore Febuary cannot have more than 29 days.";
        }

        if ( intYear % 4 != 0 && intDay >=28 ) {
          strReturn = "- This year is not a leap year and therefore Febuary cannot have more than 28 days.";
        }
      }
    }
    else {
      if ( strDay > arrLookup[strMonth] ) {
        strReturn = "- The number for day of the month was too high and thus invalid.";
      }
    }
  }
  else {
    strReturn = "- Date is invalid.";
  }

  return strReturn;
}

// function performs form verfication. invoked from the onSubmit() event handler.
// handler return whatever value this function returns.
// form wil not submit as long as function returns false.

function verifyForm( objForm ) {
  var strMsg           = "";
  var strEmptyFields   = "";
  var strFaultyEmail   = "";
  var strMinLength     = "";
  var strMinValue      = "";
  var strMaxValue      = "";
  var strNumericFields = "";
  var strFaultyDate    = "";

  for ( var i = 0; i < objForm.length; i++ ) {
    var objCurElement = objForm.elements[i];
    if ( ( objCurElement.type == "text" ) || ( objCurElement.type == "textarea" ) || ( objCurElement.type == "password" ) ) {
      if ( objCurElement.required ) {
        if ( ( objCurElement.value == null ) || ( objCurElement.value == "" ) || isBlank( objCurElement.value ) ) {
          strEmptyFields += "\n          " + objCurElement.label;
          objCurElement.className = "error";
        }
        else {
          objCurElement.className = "";
        }
      }

      if ( objCurElement.min_length ) {
        if ( objCurElement.value.length < objCurElement.min_length ) {
          strMinLength += "\n          " + objCurElement.label + " [Minimal length: " + objCurElement.min_length + "]";
          objCurElement.className = "error";
        }
        else {
          objCurElement.className = "";
        }
      }

      if ( objCurElement.required || ( !objCurElement.required && !( ( objCurElement.value == null ) || ( objCurElement.value == "" ) || isBlank( objCurElement.value ) ) ) ) {
        if ( objCurElement.numeric ) {
          if ( !isNumeric( objCurElement.value ) ) {
            strNumericFields += "\n          " + objCurElement.label;
            objCurElement.className = "error";
          }
          else {
            objCurElement.className = "";
          }
        }
      }

      if ( objCurElement.email ) {
        if ( !isEmail( objCurElement ) ) {
          strFaultyEmail += "\n          " + objCurElement.label;
          objCurElement.className = "error";
        }
        else {
          objCurElement.className = "";
        }
      }

      if ( objCurElement.min_value ) {
        if ( parseInt( objCurElement.value ) < parseInt( objCurElement.min_value ) ) {
          strMinValue += "\n          " + objCurElement.label + " [Minimum value: " + objCurElement.min_value + "]";
          objCurElement.className = "error";
        }
        else {
          objCurElement.className = "";
        }
      }

      if ( objCurElement.max_value ) {
        if ( parseInt( objCurElement.value ) > parseInt( objCurElement.max_value ) ) {
          strMaxValue += "\n          " + objCurElement.label + " [Maximum value: " + objCurElement.max_value + "]";
          objCurElement.className = "error";
        }
        else {
          objCurElement.className = "";
        }
      }
    }

    if ( objCurElement.type == "select-one" ) {
      if ( objCurElement.required ) {
        if ( objCurElement.value == "placeholder" ) {
          strEmptyFields += "\n          " + objCurElement.label;
          objCurElement.className = "error";
        }
        else {
          objCurElement.className = "";
        }
      }
    }

    if ( objCurElement.type == "select-multiple" ) {
      strEmptyFields += "\n !!!" + objCurElement.label + " is a SELECT-MULTIPLE tag";
    }
  }

  if ( objForm.check_date ) {
    if ( !( objForm.date_day.value == null && objForm.date_day.value == "" && isNumeric( objForm.date_day.value ) ) && !( objForm.date_month.value == null && objForm.date_month.value == "" && isNumeric( objForm.date_month.value ) ) && !( objForm.date_year.value == null && objForm.date_year.value == "" && isNumeric( objForm.date_year.value ) ) ) {
      strFaultyDate = validateDate( objForm.date_year.value, objForm.date_month.value, objForm.date_day.value );
    }
  }

  if ( !strEmptyFields && !strFaultyEmail && !strMinLength && !strNumericFields && !strFaultyDate && !strMinValue && !strMaxValue ) {
    return true;
  }

  strMsg  = "The form was not submitted because of the following error(s).\n";
  strMsg += "____________________________________________________________\n\n";

  if ( strEmptyFields ) {
    strMsg += "\n- The following required field(s) are empty:" + strEmptyFields + "\n";
  }

  if ( strMinLength ) {
    strMsg += "\n- The following field(s) do now have the required length:" + strMinLength + "\n";
  }

  if ( strNumericFields ) {
    strMsg += "\n- The following field(s) do not have the required numeric value:" + strNumericFields + "\n";
  }

  if ( strFaultyEmail ) {
    strMsg += "\n- The following email field(s) are invalid or incomplete:" + strFaultyEmail + "\n";
  }

  if ( strMinValue ) {
    strMsg += "\n- The following field(s) do not have the required minimum value:" + strMinValue + "\n";
  }

  if ( strMaxValue ) {
    strMsg += "\n- The following field(s) do not have the required maximum value:" + strMaxValue + "\n";
  }

  if ( strFaultyDate ) {
    strMsg += "\n" + strFaultyDate + "\n";
  }

  alert( strMsg );
  return false;
}
