
/* Generic type check functions */
/* author Bernhard Sporkmann */
/* version $Revision: 1.1 $ */
/* Copyright: Siteforce AG - 2001 */


 function convertHTMLEntities ( text )
 {
 	text = text.replace ( /&uuml;/, "ü" );
 	text = text.replace ( /&Uuml;/, "Ü" );
 	text = text.replace ( /&auml;/, "ä" );
 	text = text.replace ( /&Auml;/, "Ä" );
 	text = text.replace ( /&ouml;/, "ö" );
 	text = text.replace ( /&Ouml;/, "Ö" );
 	text = text.replace ( /&szlig;/, "ß" );
 	return text;
 }

/** 
 * Checks whether value is not equal to "" 
 *
 *param field The name of an input field as a string.
 *param label A description of the field that is used in an error message.
 *
 *return "false" if the check fails - otherwise "true"
 */
 function emptyCheck(field, label) {
    var value = eval(field).value;
    if ( value == null )
    {
	  value = eval(field).options[eval(field).options.selectedIndex].value;
    }
   
    if ( value == "" || value == null ) {
       alert(convertHTMLEntities ( label ));
       eval(field).focus();
       return false;
    }
    return true;
 }
 
 /** 
 * Checks whether value is not equal to "" 
 *
 *param field The name of an input field as a string.
 *param label A description of the field that is used in an error message.
 *
 *return "false" if the check fails - otherwise "true"
 * DOES NOT create an alert box in case of empty field
 */
 function emptyCheckSilent(field, label) {
    var value = eval(field).value;
    if ( value == "" ) {
       return false;
    }
    return true;
 }


/**
 * Checks whether value consists only of digits.
 *
 *param field    The name of an input field as a string.
 *param label    A description of the field that is used in an error message.
 *param required If this parameter is set to "true" the function
 *               "emptycheck()" is called first.
 *return "false" if the check fails - otherwise "true"
 */
 function unsignedIntCheck(field, label, required) {
   if ( required == true )
      if ( ! emptyCheck(field, label) )
         return false;
     var value = eval(field).value;
     for( i = 0; i < value.length; ++i)
       if( value.charAt(i) != " " && (value.charAt(i) < "0" || value.charAt(i) > "9") ) {
        alert(convertHTMLEntities ( label ));
        if (eval(field) )
			eval(field).focus();
    
	    return false;
      }
    return true;
  }

 /* 
  * Checks whether value consists only of digits and an optional leading "-". 
  */
 function intCheck(field, label, required) {
   if ( required == true )
      if ( ! emptyCheck(field, label) )
         return false;
     var value = eval(field).value;
     for( i = 0; i < value.length; ++i)
       if( (value.charAt(i) < "0" || value.charAt(i) > "9" ) && (i > 0 || value.charAt(i) != "-") && value.charAt(i) != " ") {
        alert(convertHTMLEntities ( label ));
        eval(field).focus();
        return false;
      }
    return true;
 }

/** 
 *  Calls "intCheck" and checks whether lower <= value <= upper .
 *
 *param upper Upper bound for value.
 *param lower Lower bound for value.
 */
 function intRangeCheck(field, label, upper, lower, required) {
   if ( ! intCheck(field, label, required) )
     return false;
   var value = eval(field).value;
   if ( value != "" && ( value > upper || value < lower ) ) {
     alert(convertHTMLEntities ( label ));
     eval(field).focus();
     return false;
   }
   return true;
 }

 function yearCheck(field, label) {
   var value = eval(field).value;
   if ( value > 0 && value < 1000) {
     alert(convertHTMLEntities ( label ));
     eval(field).focus();
     return false;
   }
   return true;
 }


 function unsignedFloatCheck(field, label, required) {
   if ( required == true )
      if ( ! emptyCheck(field, label) )
         return false;
     var value = eval(field).value;
     value = value.replace(/,/,".");
     eval(field).value = value;
     for( i = 0; i < value.length; ++i)
       if( (value.charAt(i) < "0" || value.charAt(i) > "9" ) 
          && value.charAt(i) != "." && value.charAt(i) != " ") {
        alert(convertHTMLEntities ( label ));
        eval(field).focus();
        return false;
      }
    return true;
 }

 function floatCheck(field, label, required) {
   if ( required == true )
      if ( ! emptyCheck(field, label) )
         return false;
     var value = eval(field).value;
     value = value.replace(/,/,".");
     eval(field).value = value;
     for( i = 0; i < value.length; ++i)
       if( (value.charAt(i) < "0" || value.charAt(i) > "9" ) 
          && (i > 0 || value.charAt(i) != "-") && value.charAt(i) != "." && value.charAt(i) != " " ) {
        alert(convertHTMLEntities ( label ));
        eval(field).focus();
        return false;
      }
    return true;
 }

/**
 * Checks whether value is a valid date.
 * It is assumed that the input is taken from three input fields.
 * These fields' names are the logical name indexed with "_0", "_1", "_2",
 * respectively. 
 *
 *param field    The logical name of a virtual date input field as a string.
 *param label    A description of the date that is used in an error message.
 *param required If this parameter is set to "true" the function
 *               "emptycheck()" is called first.
 *return "false" if the check fails - otherwise "true"
 */
 function dateCheck(field, label, required) {
     var dayField = field+"_dd";
     var monthField = field+"_mm";
     var yearField = field+"_yyyy";
     if ( required == true && ( ! emptyCheck(dayField, label) &&
	                          emptyCheck(monthField, label) &&
	                          emptyCheck(yearField, label) ) )
                   return false;
     if (!yearCheck(yearField, label))
       	return false;
     var upper = 31;
     var month = parseInt(eval(monthField).value);
     if ( month == "4" || month == "6" || month == "9" || month == "11" )
        upper = 30;
     else if ( month == "2" )
        upper = 29;
     return intRangeCheck(dayField,label,upper,1)
          && intRangeCheck(monthField,label,12,1)
          && unsignedIntCheck(yearField,label);
 }


 /* Checks whether value - if not empty - contains a "@" and a "." after it. */
 function emailCheck(field, label, required) {
   if ( required == true )
      if ( ! emptyCheck(field, label) )
         return false;
   var value = eval(field).value;
   if ( value == "" )
      return true;
   var pos1 = value.indexOf('@');
   var pos2 = value.lastIndexOf('.');
   if (  pos1 == -1 || pos1 > pos2 ) {
      alert(convertHTMLEntities ( label ));
      eval(field).focus();
      return false;
   }
   
   return true;
 }

 /* Checks wether value is a valid GERMAN zip 5 digits, only numbers, leading 0 possible */
 function zipCheck(field, label, required) {
 	
	// se if field is required or not
	if ( required == true ) {
		if ( !emptyCheck(field, label) ) return false;
	}
	
	if ( emptyCheckSilent(field, label) ) {
		if (! intRangeCheck(field, label, 99999, 0, required) || ! unsignedIntCheck(field, label, required) || ! stringLengthCheck(field, label, 5) ) {
			alert(convertHTMLEntities ( label ));
			eval(field).focus();
			return false;
		}
		return true;
	}
	
	return true;
}
	
		
		
 /* Checks wether the string stored in field has the specific lenfth of myLength */ 
 function stringLengthCheck(field, label, myLength) {
    var value = eval(field).value;
    if ( value.length != myLength ) {
       return false;
    }
    return true;
 }



