<!--

var formValidator = function(formName)
{
	this.formObject = document.forms[formName];
	this.formElem = new Array();
	this.msgStr = "";
	this.onBeforeSubmit = null;
	
	this.regxp = {
		int : /^(\-)?[0-9]{1,}$/i,
		float : /^(\-)?[0-9]{1,}(\.[0-9]{1,})?$/i,
		date : /^[0-9]{4}\-[01]{1}[0-9]{1}\-[0123]{1}[0-9]{1}$/i,
		time : /^[0-9]{2}\:[0-9]{2}\:[0-9]{2}$/i,
		date_time : /^[0-9]{4}\-[0-9]{2}\-[0-9]{2}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}$/i,
		post_code : /^[0-9]{2}\-[0-9]{3}$/i,
		phone : /^[0-9 \(\)\.\-wewn\/]{6}$/i,
		www : /^(http:\/\/)?www[0-9_a-z._-]+\.[a-z]{2,3}([\/0-9_a-z\.]{1,})?(\/?.*)?$/i,
		email : /^[0-9_a-z.-]+(@|\(at\))+(([0-9_a-z._-]+\.[a-z]{2,3})|(localhost))$/i,
		price : /^[0-9]{1,}([\.]{1})?([0-9]{2})?$/i,
		text : /^[^\|]{1,}$/i
	};	
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	this.add_int = function(fldName, require, min, max)	{
		this.addFormItem("int", fldName, require, min, max);
	}

	this.add_float = function(fldName, require, min, max)	{
		this.addFormItem("float", fldName, require, min, max);
	}

	this.add_date = function(fldName, require, min, max)	{
		this.addFormItem("date", fldName, require, min, max);
	}		
	
	this.add_text = function(fldName, require, min, max)	{
		this.addFormItem("text", fldName, require, min, max);
	}
	
	this.add_email = function(fldName, require)	{
		this.addFormItem("email", fldName, require);
	}

	this.add_select = function(fldName, require)	{
		this.addFormItem("select", fldName, require);
	}	
	
	this.del_item = function(fldName)
	{
		//this._getElemByName
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	this.addFormItem = function(type, fldName, require, min, max)
	{
		if(typeof this.formObject.elements[fldName] == "object")
		{
			this.formElem.push({type:type, fn:fldName, require:require});
		}
		else
			this.msg("Błąd\n\nBrak pola w formularzu ("+fldName+")");
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		
	this.trySubmit = function()
	{
		var ok = true;
		
		for(var a in this.formElem)
		{
			if(ok)
				ok = this.validateFld(this.formElem[a]);
			else
				break;
		}

		if(ok && typeof this.onBeforeSubmit == "function")
			ok = this.onBeforeSubmit.apply();
		
		if(ok)
			return true;
		else
		{
			this.msg(this.msgStr);
			return false;
		}
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	this.validateFld = function(obj)
	{
		this.msgStr = "Nie można wysłać formularza\n\n";
		
		var f = this.formObject.elements[obj.fn];
		
		switch(obj.type)
		{
			case "int":
			case "float":
			
				if(obj.require)
				{
					if(f.value.length > 0 && f.value.match(this.regxp[obj.type]))
						return true;
					else
					{
						this.msgStr += "Brakujący lub niepoprawny wpis w ("+this.getLabel(f)+")";
						return false;
					}
				}
				else
				{
					if(f.value.length == 0 || (f.value.length > 0 && f.value.match(this.regxp[obj.type])))
						return true;
					else
					{
						this.msgStr += "Niepoprawny wpis w ("+this.getLabel(f)+")";
						return false;
					}
				}
				
			break;
			
			case "text" :
			case "date" :
			case "time" :
			case "date_time" :
			case "post_code" :
			case "phone" :
			case "www" :
			case "email" :
			case "price" :
			
				if(obj.require)
				{
					if(f.value.length > 0 && f.value.match(this.regxp[obj.type]))
						return true;
					else
					{
						this.msgStr += "Brakujący lub niepoprawny wpis w ("+this.getLabel(f)+")";
						return false;
					}
				}
				else
				{
					if(f.value.length == 0 || (f.value.length > 0 && f.value.match(this.regxp[obj.type])))
						return true;
					else
					{
						this.msgStr += "Niepoprawny wpis w ("+this.getLabel(f)+")";
						return false;
					}
				}
			
			break;
			
			case "select":

				if(obj.require && f.options[f.selectedIndex].value.length == 0)
				{
					this.msgStr += "Nie wskazano wartości w ("+this.getLabel(f)+")";
					return false;
				}
			
			break;

			default:
				
				this.msgStr += "Nieznany typ ("+obj.type+") !!!";
				return false;
		}
		
		return true;
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	this._getElemByName = function(n)
	{
		for(var a in this.formElem)
		{
			if(this.formElem[a].fn == n)
				return a;
		}

		return false;
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	this.msg = function(str)
	{
		alert(str);
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	var t = this;
	
	this.formObject.onsubmit = function()
	{
		return t.trySubmit();
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	this.getLabel = function(fo)
	{
		return fo.title || '';
	}
}

//-->


