//------------------------------------------------------------------------------------------  
//
//  Javascript trim, ltrim, rtrim
//  http://www.webtoolkit.info/
//
//
// Without the second parameter, they will trim these characters:
// " " (ASCII 32 (0x20)), an ordinary space. 
// "\t" (ASCII 9 (0x09)), a tab. 
// "\n" (ASCII 10 (0x0A)), a new line (line feed). 
// "\r" (ASCII 13 (0x0D)), a carriage return. 
// "\0" (ASCII 0 (0x00)), the NUL-byte. 
// "\x0B" (ASCII 11 (0x0B)), a vertical tab. 
//------------------------------------------------------------------------------------------  
function trim(str, chars) 
{
  return ltrim(rtrim(str, chars), chars);
}
//------------------------------------------------------------------------------------------  
function ltrim(str, chars) 
{
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
//------------------------------------------------------------------------------------------  
function rtrim(str, chars) 
{
  chars = chars || "\\s";
  return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

//------------------------------------------------------------------------------------------  
//
// check for valid numeric strings	
// http://www.pbdr.com/vbtips/asp/JavaNumberValid.htm
//
// Aufruf z.B.:  if (IsNumeric(document.frmUser.afield.value) == false)
//------------------------------------------------------------------------------------------  
function IsNumeric(strString)
{
   //var strValidChars = "0123456789.-";
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

// http://www.sourcesnippets.com/javascript-string-pad.html
// This functions returns the input string padded on the left, the right, or both sides to the specified padding length. 
// If the optional argument "pad" is not supplied, the input is padded with spaces, otherwise it is padded with characters from "pad" up to the "len" length. 

//------------------------------------------------------------------------------------------  
var STR_PAD_LEFT  = 1;
var STR_PAD_RIGHT = 2;
var STR_PAD_BOTH  = 3;
//------------------------------------------------------------------------------------------   
function pad(str, len, pad, dir) 
{
 
	if (typeof(len) == "undefined") { var len = 0; }
	if (typeof(pad) == "undefined") { var pad = ' '; }
	if (typeof(dir) == "undefined") { var dir = STR_PAD_RIGHT; }
 
	if (len + 1 >= str.length) {
 
		switch (dir){
 
			case STR_PAD_LEFT:
				str = Array(len + 1 - str.length).join(pad) + str;
			break;
 
			case STR_PAD_BOTH:
				var right = Math.ceil((padlen = len - str.length) / 2);
				var left = padlen - right;
				str = Array(left+1).join(pad) + str + Array(right+1).join(pad);
			break;
 
			default:
				str = str + Array(len + 1 - str.length).join(pad);
			break;
 
		} // switch
 
	}
 
	return str;
 
}
//------------------------------------------------------------------------------------------  
// E-Mail auf richtige Schreibweise überprüfen
// http://www.drweb.de/javascript/email_check.shtml
//------------------------------------------------------------------------------------------  
function CheckEMail(s)
{
 var a = false;
 var res = false;
 if(typeof(RegExp) == 'function')
 {
  var b = new RegExp('abc');
  if(b.test('abc') == true){a = true;}
  }

 if(a == true)
 {
  reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
                   '(\\@)([a-zA-Z0-9\\-\\.]+)'+
                   '(\\.)([a-zA-Z]{2,4})$');
  res = (reg.test(s));
 }
 else
 {
  res = (s.search('@') >= 1 &&
         s.lastIndexOf('.') > s.search('@') &&
         s.lastIndexOf('.') >= s.length-5)
 }
 return(res);
}
//------------------------------------------------------------------------------------------  
// URL auf richtige Schreibweise prüfen
// http://snippets.dzone.com/posts/show/452#related
// usage: if (isUrl("http://www.page.com")) alert("is correct") 
// else alert("not correct");
//------------------------------------------------------------------------------------------  
function CheckUrl(s) 
{
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}

// Prüfen des Datums auf Format tt.mm.jjjj 
function CheckDate(datum,minimumjahr)
{
  l = datum.length;
  if(l!=10)
  {
    return('Das Datum muß 10-stellig sein!');
  }
  
  arrDate=datum.split(".");
  DateAnzahl=arrDate.length;
  if  (DateAnzahl!=3)
  {
    return('Das Datum hat das falsche Format!');
  }
  
  etag   = arrDate[0];
  emonat = arrDate[1]; 
  ejahr  = arrDate[2]; 
  
  // Prüfen des exakten Erscheinungstages
  l = etag.length;
  if(l!=2)
  {
    return('Der Tag muß 2-stellig sein!');
  }
      
  if (IsNumeric(etag) == false)
  {
    return('Der Tag muß numerisch sein!');
  }

  if ((etag < 1) || (etag > 31))
  {
    return('Der Tag muß im Bereich 01 ... 31 sein!');
  }
  
  // Prüfen des exakten Monats
  l = emonat.length;
  if(l!=2)
  {
    return('Der Monat muß 2-stellig sein!');
  }
  
  if (IsNumeric(emonat) == false)
    {
    return('Der Monat muß numerisch sein!');
  }
  
  if ((emonat < 1) || (emonat > 12))
  {
    return('Der Monat muß im Bereich 01 ... 12 sein!');
  }  
  
  // Prüfen des exakten Erscheinungsjahres
  l = ejahr.length;
  if(l!=4)
  {
    return('Das Jahr muß 4-stellig sein!');
  }
  
  if (IsNumeric(ejahr) == false)
    {
    return('Das Jahr muß numerisch sein!');
  }
  
  if (ejahr < minimumjahr)
  {
    return('Das Jahr muß größer oder gleich ' + minimumjahr + ' sein');
  }
  return(''); 
}    
//------------------------------------------------------------------------------------------  
function encode_utf8(s)
{
  return unescape(encodeURIComponent(s));
}
//------------------------------------------------------------------------------------------   
function decode_utf8(s)
{
  return decodeURIComponent(escape(s));
}
//------------------------------------------------------------------------------------------  
