<!--

/*

*/

var form = function(formName)
{
	this.formObject = document.forms[formName];
	this.formElem = new Array();
	this.msgStr = "";
	this.onBeforeSubmit = null;
	this.onSubmit = null;
	
	if(typeof this.formObject.onsubmit == "function")
		this.onSubmit = this.formObject.onsubmit;
	
	var that = this;
	this.formObject.onsubmit = function(){
		return that.trySubmit();
	}

	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.messages = {

		all : "Komunikat formularza",
		noSend : "Nie można wysłać formularza",

		badValidate : "Nieznany typ walidacji (%1) dla formularza.",
		badAdd : "Błąd - nie można dodać pola %1 (%2)",
		noField : "Błąd\n\nBrak pola w formularzu (%1)",
		badData1 : "Brakujący lub niepoprawny wpis w (%1)",
		badData2 : "Niepoprawny wpis w (%1)",
		countChars1 : "Ilość znaków w polu (%1) jest mniejsza niż %2",
		countChars2 : "Ilość znaków w polu (%1) jest większa niż %2",
		noSelect : "Nie wskazano wartości w (%1)",
		noRadio : "Nie zaznaczono żadnej opcji w (%1)",
		wrongType : "Nieznany typ (%1) !!!"
	};
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	this.auto = function()
	{
		var i, attr, fn1, fn2, type, req, ftype;
		
		for(i=0; i<this.formObject.elements.length; i++)
		{
			if(typeof this.formObject.elements[i]  == "object" && (attr = this.formObject.elements[i].getAttribute("rel")))
			{
				fn1 = attr.match(/(validate\:)([a-z_]{1,})/i);
				fn2 = attr.match(/(require\:)([1,0]{0,1})/i);

				req = (fn2 && fn2[2] && fn2[2].length>0)? parseInt(fn2[2]) : 0 ;
				type = (fn1 && fn1[2] && fn1[2].length > 0)? fn1[2] : (req? "text" : false) ;

				if(attr.length>0 && (req || type))
				{
					ftype = this.getType(this.formObject.elements[i]);

					if(this.regxp[type])
					{
						if(ftype == "select")
							this.addFormItem("select",  this.formObject.elements[i].name, req);
						else if(ftype == "radio")
							this.addFormItem("radio",  this.formObject.elements[i].name, req);
						else
							this.addFormItem(type,  this.formObject.elements[i].name, req);
					}
					else
						this.msg(this.msgP(this.messages.badValidate, fn1[2]));
				}
			}
		}
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	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_data = 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.add_radio = function(fldName, require)	{
		this.addFormItem("radio", fldName, require);
	}

	this.add_checkbox = function(fldName, require)	{
		this.addFormItem("checkbox", fldName, require);
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	this.add_fields = function(fieldsStr, require)
	{
		var n = fieldsStr.split(",");
		var type;
		
		for(var i=0; i<n.length; i++)
		{
			if(this.formObject[n[i]] != undefined && (type = this.getType(formObject[n[i]])))
			{
				switch(type.toLowerCase())
				{
					case "text":
					case "textarea":
						this.add_text(this.formObject[n[i]].name, require);
					break;
					
					case "select":
					case "select-one":
						this.add_select(this.formObject[n[i]].name, require);
					break;
						
					default :
						this.msg(this.msgP(this.messages.badAdd, this.formObject[n[i]].name, type));
				}
			}
		}
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	this.del_field = function(fldName)
	{
		var n = new Array();
		
		for(var i=0; i<this.formElem.length; i++)
		{
			if(this.formElem[i].fn != fldName)
				n.push(this.formElem[i]);
		}
		
		this.formElem = n;
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	this.addFormItem = function(type, fldName, require, min, max)
	{
		if(typeof this.formObject.elements[fldName] == "object")
		{
			this.formElem.push({type:type, fn:fldName, require:require||false, min:min||false, max:max||false});
		}
		else
			this.msg(this.msgP(this.messages.noField, 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.onSubmit == "function")
			ok = this.onSubmit.apply();
		
		if(ok && typeof this.onBeforeSubmit == "function")
			ok = this.onBeforeSubmit.apply();
		
		if(ok)
			return true;
		else
		{
			if(this.msgStr.length > 0)
				this.msg(this.msgStr);
	
			return false;
		}
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	this.validateFld = function(obj)
	{
		this.msgStr = this.messages.noSend+"\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 += this.msgP(this.messages.badData1, 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 += this.msgP(this.messages.badData2, 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]))
					{
						if(obj.min && f.value.length < obj.min)
						{
							this.msgStr += this.msgP(this.messages.countChars1, this.getLabel(f), obj.min);
							return false;
						}
						
						if(obj.max && f.value.length > obj.max)
						{
							this.msgStr += this.msgP(this.messages.countChars2, this.getLabel(f), obj.max);
							return false;
						}						
					
						return true;
					}
					else
					{
						this.msgStr += this.msgP(this.messages.badData1, 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 += this.msgP(this.messages.badData2, this.getLabel(f));
						return false;
					}
				}
			
			break;
			
			case "select":

				if(obj.require && f.options[f.selectedIndex].value.length == 0)
				{
					this.msgStr += this.msgP(this.messages.noSelect, this.getLabel(f));
					return false;
				}
			
			break;
			
			case "radio":
			case "checkbox":

				if(obj.require)
				{
					if(f.length)
					{
						for(var i=0; i<f.length; i++)
						{
							if(f[i].checked)
								return true;
						}
						
						this.msgStr +=  this.msgP(this.messages.noRadio, this.getLabel(f[0]));
						return false;
					}
					else
					{
						if(!f.checked)
						{
							this.msgStr +=  this.msgP(this.messages.noRadio, this.getLabel(f));
							return false;
						}
					}
				}
				
			break;

			default:
				
				this.msgStr +=  this.msgP(this.messages.wrongType, obj.type);
				return false;
		}
		
		return true;
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	this.msg = function(str)
	{
		if(typeof box == "object" && typeof box.show == "function")
			box.show("msg", this.messages.all, str.replace("\n", "<br>"));
		else
			alert(str);
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	this.msgP = function(str, a, b)
	{
		if(a != undefined)
			str = str.replace("%1", a);

		if(b != undefined)
			str = str.replace("%2", b);			
		
		return str;
	}	
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	var t = this;
	
	this.formObject.onsubmit = function()
	{
		return t.trySubmit();
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	this.getType = function(fo)
	{
		var t;
		if(fo && (t = fo.type))
		{
			t = t.toLowerCase();
		
			if(t == "select-one")
				t = "select";
		
			return t;
		}
		
		return false;
	}

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	this.getLabel = function(fo)
	{
		return fo.title || '';
	}
}

//-->


