/*
  @Beschreibung: 
   Validierungsklasse für Formularfelder
   
  @Vorraussetzungen: 
   prototype.js (Prototype Framework 1.5+)
   
  @Usage:   
   
   Beispiel, Validierung eines Gästebuchformulares:
  ...
      function checkGbEntry(form){
   		
   		formID = form.id;
   		inputValidator = new ClassInputValidator("1px solid #bebebe", "1px solid #ff6600");
   		inputValidator.effectOn = false; //Style Änderung der HTML-Komponente deaktivieren
   		
   		var userMsgText = "";
   
   		if(!inputValidator.validate($('gbName')))
   			userMsgText = 'Bitte einen Namen angeben!';
   			
   		else if (!inputValidator.validate($('gbEmail'), 'email'))
   			userMsgText = 'Bitte eine gültige Emailadresse angeben!';
   			
   		else if (!inputValidator.validate($('gbMessage')))
   			userMsgText = 'Bitte einen Nachricht verfassen!';
   			
   		else {
   			if($('gbHomepage').value == "http://")
   				$('gbHomepage').value = "";	
   		}
   		if(userMsgText != ""){
   		   //Variante 1:
   		   alert(userMsgText);
   		   
   		   //Variante 2 (setzt eine HTML-Komponente mit id="userMsg" vorraus):
   			//Element.update('userMsg', userMsgText); 
   			return false;
   		}
   	   return true;
      }
  ...
*/

ClassInputValidator = function(p_defaultBorderStyle, p_dutyBorderStyle) {
	
	//Konstruktor
	this.defaultBorderStyle = p_defaultBorderStyle;
	this.dutyBorderStyle    = p_dutyBorderStyle;
	this.object             = undefined;
	this.validation         = 'text';
	this.effectsOn          = true;
}

// erbt von Object [optional]
ClassInputValidator.prototype = new Object ();

ClassInputValidator.prototype.setDefaultBorderStyle = function (p_borderStyle) {
	this.defaultBorderStyle = p_borderStyle;
}

ClassInputValidator.prototype.setDutyBorderStyle = function (p_borderStyle) {
	this.dutyBorderStyle = p_borderStyle;
}

ClassInputValidator.prototype.validate  = function(p_object, p_validation) {
	
	if((typeof p_object != "object") || (typeof p_object.val() != "string")) {
		alert('inputValidator: obejctType: ' + typeof p_object + ', obejctValueType:' + typeof p_object.val());
		return;
	} else {
		
		this.object = p_object;
		
		if(typeof p_validation != "undefined")
			this.validation = p_validation;
		else
			this.validation = 'text';
		
		//Validation
		switch (this.validation) {
		  case "text":
				if(this.object.val() == "") {
					if(this.effectsOn)
						this.object.css("border", this.dutyBorderStyle);
					this.object.focus();
					return false;
				}
				else {
					if(this.effectsOn)
						this.object.css("border", this.defaultBorderStyle);
				}
			break;
		  case "email":
				if(!this.emailCheck(this.object.val())) {
					if(this.effectsOn)
						this.object.css("border", this.dutyBorderStyle);
					this.object.focus();
					return false;
				}
				else {
					if(this.effectsOn)
						this.object.css("border", this.defaultBorderStyle);
				}
			break;
		  case "date":
				if(!this.object.val().match(/[0-3]\d\.[01]\d\.\d{4}/)) {
					if(this.effectsOn)
						this.object.css("border", this.dutyBorderStyle);
					this.object.focus();
					return false;
				}
				else {
					if(this.effectsOn)
						this.object.css("border", this.defaultBorderStyle);
				}
			break;
		  case "time":
				if(!this.object.val().match(/[0-2]\d\:[0-9]\d/)) {
					if(this.effectsOn)
						this.object.css("border", this.dutyBorderStyle);
					this.object.focus();
					return false;
				}
				else {
					if(this.effectsOn)
						this.object.css("border", this.defaultBorderStyle);
				}
			break;
			
		  default:
			alert("inputValidator: Validierung '" + this.validation +"' ist ungültig!");
			break;
		}
	}
	return true;
}

ClassInputValidator.prototype.emailCheck = function(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);
}

