
function Validation(input, type, error_text){
    this.input = input;
    switch(type){
        case "required":{
	    if(error_text)
		this.errorText = error_text;
	    else
		this.errorText = "Missing required field.";
        this.validate = function(){
             if(this.input.value == ""){
                return false;
                }
             else
                return true;
            }
            break;
        }
        case "alphanumeric":{
	    if(error_text)
		this.errorText = error_text;
	    else
		this.errorText = "Only letters or numbers permitted.";
        this.validate = function(){
            var charpos = this.input.value.search("[^A-Za-z0-9]"); 
            if(this.input.value.length > 0 && charpos >= 0) {
                return false;
                }
            else 
                return true;
            }
           break; 
        }
        case "email":{
	    if(error_text)
		this.errorText = error_text;
	    else
		this.errorText = "Invalid email address.";
        this.validate = function(){
            email = this.input.value;
            if(email.length <= 0)
	        {
	        return false;
	        }
            var splitted = email.match("^(.+)@(.+)$");
            if(splitted == null) return false;
            if(splitted[1] != null )
            {
            var regexp_user=/^\"?[\w-_\.]*\"?$/;
            if(splitted[1].match(regexp_user) == null) return false;
            }
            if(splitted[2] != null)
            {
            var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
            if(splitted[2].match(regexp_domain) == null) 
            {
	            var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
                if(splitted[2].match(regexp_ip) == null) return false;
            }// if
            return true;
            }
            return false;
        }
        break; 
        }
    }
}


function Validator(form, output){
    this.form=form;
	if(!this.form)
	{
	  alert("BUG: couldnt get Form object ");
		return;
	}
	if(this.form.onsubmit)
	{
	 this.form.old_onsubmit = this.form.onsubmit;
	 this.form.onsubmit=null;
	}
	else
	{
	 this.form.old_onsubmit = null;
	}
    
   this.output = output;
   if(!this.output)
   {
	  alert("BUG: couldnt get output object ");
		return;
	}
   
    
    this.validations = new Array();
    
    this.addValidation = function(input, type, error_text){
      var input = input
        if(!input)
	     {
	     alert("BUG: couldnt get input object "+inputName);
         return;
        } 
      this.validations.push(new Validation(input, type, error_text));
    }
    
    this.validate = function(){
      for(var i = 0; i < this.validations.length; i++) {
        var val = this.validations[i];
        if(!val.validate()){
            /*Output the error to the html*/ 
            while(this.output.hasChildNodes())
                this.output.removeChild(this.output.firstChild);
            newNode = document.createTextNode(val.errorText);  
            this.output.appendChild(newNode);
            return false;    
         } 
        }
       return true; 
    }
    
    /*remember 'this' refers to dynamic context NOT lexical*/
    this.form.validator = this;
    this.form.onsubmit = function(){
        return this.validator.validate();
    }
    
}

