// ------------------------------------------------------------------------  //
//  Project:   Hofraete CMS                                                  //
//  Author:    Roland Froehler <webmaster@hofraete.at>                       //
//                                                                           //
//  Copyright: 2009 Die Wirklichen Hofraete                                  //
//  -----------------------------------------------------------------------  //

// validation functions used to verify form entries

function isEmailAddr(email,fieldLabel)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
  result = true;
  }
  if (!result) {
  	alert('Das Feld "' + fieldLabel +'" enthält keine gültige Emailadresse.');
  }
  return result;
}

function test() {
	alert("la");
	return false;
}

function validRequired(formField,fieldLabel)
{
 var result = true;
  
  if (trim(formField.value) == "")
  {
    alert('Das Feld "' + fieldLabel +'" darf nicht leer sein.');
    formField.focus();
    result = false;
  }
  
  return result;
}


function validInt(formField, fieldLabel, required)
{
  var result = true;
  var s = "" + formField.value;

  if (required && !validRequired(formField,fieldLabel)) {
  	result = false;
  }
    
  if(!required && (formField.value=="")) {
  	return true;
  }

 
  if (result) {
    s = trim(s);
    for (i = 0; i < s.length; i++) {   
    	// Check that current character is a number
      var c = s.charAt(i);

      if (!(isDigit(c))) {
        result = false;
      }
    } // for
    if(!result) {
      alert('Bitte geben Sie eine positive ganze Zahl im Feld "' + fieldLabel +'" an.');
      formField.focus();    
    }   
  }
  return result;
}


function isDigit (c) {
	return ((c >= "0") && (c <= "9"));
}


function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   return retValue; // Return the trimmed string back to the user   
} // Ends the "trim" function


function validDate(numDay,numMonth,numYear,fieldLabel) {
    // February can't be greater than 29 (leap year calculation comes later)
    if ((numMonth == 2) && (numDay > 29)) { alertInvalidDate(fieldLabel);return false; }
    // check for months with only 30 days
    if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) { 
        if (numDay > 30) { alertInvalidDate(fieldLabel);return false; } 
    }

    // check for leap year if the month and day is Feb 29
    if ((numMonth == 2) && (numDay == 29)) {
        var div4 = numYear % 4;
        var div100 = numYear % 100;
        var div400 = numYear % 400;
        // if not divisible by 4, then not a leap year so Feb 29 is invalid
        if (div4 != 0) { alertInvalidDate(fieldLabel);return false; }
        // at this point, year is divisible by 4. So if year is divisible by
        // 100 and not 400, then it's not a leap year so Feb 29 is invalid
        if ((div100 == 0) && (div400 != 0)) { alertInvalidDate(fieldLabel);return false; }
    }
    // date is valid
    return true;
}

function alertInvalidDate(fieldLabel){
	alert('Das eingegebene Datum im Feld "' + fieldLabel + '" gibt es nicht!' );
}


