<!--

function checkForm(fname, action)
{
	this.add = checkForm_add_field;
	this.change = checkForm_change_field;
	this.msg = checkForm_message;
	this.send = checkForm_send;
	this.validate = checkForm_validate_fld;
	this.validate_r = checkForm_validate_require;
	this.validate_b = checkForm_validate_between;
	this.validate_bval = checkForm_validate_between_val
	this.blink_fld = checkForm_blink_field;
	this.deleteAll =checkForm_clear_fields;
	this.add_list = checkForm_addFromList;
	//end functions

	this.regxpObj = new Array();
	this.regxpObj["all"] = /.*/i;
	this.regxpObj["int"] = /^[\-0-9]{1,}$/i;
	this.regxpObj["float"] = /^[\-0-9]{1,}(\.[0-9]{1,})?$/i;
	this.regxpObj["date"] = /^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/i;
	this.regxpObj["time"] = /^[0-9]{2}\-[0-9]{2}\-[0-9]?$/i;
	this.regxpObj["date_time"] = /^[0-9]{4}\-[0-9]{2}\-[0-9]{2}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}$/i;
	this.regxpObj["post_code"] = /^[0-9]{2}\-[0-9]{3}$/i;
	this.regxpObj["phone"] = /^[0-9 \(\)\.\-wewn\/]{6}$/i;
	this.regxpObj["www"] = /^(http:\/\/)?www[0-9_a-z._-]+\.[a-z]{2,3}([\/0-9_a-z\.]{1,})?(\/?.*)?$/i;
	this.regxpObj["email"] = /^[0-9_a-z.-]+(@|\(at\))+[0-9_a-z._-]+\.[a-z]{2,3}$/i;
	this.regxpObj["price"] = /^[0-9]{1,}(\.[0-9]{1,3})?$/i;
	this.regxpObj["max_9"] = /^[0-9]{1}$/i;
	this.regxpObj["text"] = /^[^\|]{1,}$/i;

	action = ''+action;
	this.fObj = document.forms[fname];
	this.oldAction = this.fObj.action;
	this.action = (action.length == 0 || action == "undefined")? "index.php" : action ;
	this.ok = true;
	this.formName = fname;
	this.box = new Array();
	this.is_prompt = false;

	if(typeof(checkFormCss) == "object")
	{
		this.defaultBorderStyle = checkFormCss[0];
		this.selectBorderStyle = checkFormCss[1];
	}
	else
	{
		this.defaultBorderStyle = "1px solid #999999";
		this.selectBorderStyle = "1px solid #000000";
	}		
	this.msg_function = (typeof(display_message) == "function")? display_message : false ;
	
	this.messages =
	{
		"no_fld" : "Brak pola formularza o podanej nazwie (%1) .",
		"no_type" : "Nieznany typ (%1) zmiennej w formularzu o nazwie (%2).",
		"no_fill" : "Pole (%1) nie ma wpisanej warto¶ci.",
		"no_option" : "Pole (%1) nie ma wybranej żadnej z opcji.",
		"no_between_fill" : "Wartość pola (%1) powinna zawierać się pomiędzy (%2) a (%3).",
		"no_between_option" : "Pole (%1) powinno mieć zaznaczone minimalne (%2) a maksymalnie (%3) opcje.",
		"bad_fill" : "Pole (%1) zostało błędnie wypełnione.",
		"send" : "Czy napewno wysłać formularz ?",
		"err" : "Formularz nie może zostać wysłany z powodu uprzednich błędów."
	};
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function checkForm_add_field(n, t, d, r, m)
{
	var i = this.box.length;
	var tmp;
	var tmp_reg;
	var j;

	if(this.ok)
	{
		var tmp = this.fObj.elements[n];

		if(tmp == null || ''+tmp == "undefined" || ''+tmp.length == 0)
		{
			this.msg("no_fld", n);
			this.ok = false;
		}
		else
		{
			for(j=0; j<i; j++)
			{
				if(''+this.box[j]["name"] == n)
				{
					i = j;
					break;
				}
			}

			this.box[i] = new Array();
			this.box[i]["name"] = n;
		}

		if(this.ok)
		{
			t = (t == "" || t == "all")? "all": t ;
			
			if(typeof(this.regxpObj[t]) == "undefined")
			{
				this.msg("no_type", t, n);
				this.ok = false;
			}
			else
				this.box[i]["reg"] = t ;
		}

		if(this.ok)
		{
			this.box[i]["require"] = (!isNaN(parseInt(r)) || r.length > 0)? 1 : false ;
						
			if(typeof(m) != "undefined")
			{
				m = m.split(",");
				this.box[i]["between"] = [(m[0].length > 0)? m[0] : false, (typeof(m[1]) != "undefined" && m[1].length > 0)? m[1] : false];
			}
			else
				this.box[i]["between"] = false;
		}

		if(this.ok)
		{
			tmp = ''+this.fObj.elements[n].type;
			tmp = tmp.toLowerCase();
			
			if(tmp == "text" || tmp == "textarea" || tmp == "password" || tmp == "file" || tmp == "hidden")
			{
				this.box[i]["type"] = tmp ;
				this.box[i]["length"] = 1;
			}
			else
			{
				if(isNaN(parseInt(this.fObj.elements[n].length)))
					this.box[i]["length"] = 1;
				else
					this.box[i]["length"] = this.fObj.elements[n].length;	
			
				this.box[i]["type"] = (this.box[i]["length"] > 1) ? ''+this.fObj.elements[n][0].type : ''+tmp ;
			
			}

			if(tmp.match(/select/) || this.box[i]["type"].match(/select/))
				this.box[i]["type"] = "select";		

			this.box[i]["display"] = (d.length == 0)? 0 : d ;
		}
	}
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function checkForm_change_field(n, f, v)
{
	var i;
	for(i=0; i<this.box.length; i++)
	{
		if(''+this.box[i]["name"] == n)
		{
			this.box[i][f] = v;
			break;
		}
	}	
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function checkForm_addFromList()
{
	var tmp_l = checkForm_addFromList.arguments;
	var k;

	for(k=0; k<tmp_l.length; k++)
		this.add(tmp_l[k], "", 0, 1);
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function checkForm_clear_fields()
{
	this.box = "";
	this.box = new Array();
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function checkForm_message()
{
	var farg = checkForm_message.arguments;
	var msgtext;
	
	if(typeof(farg[0]) == "object")
	{
		msgtext = farg[0][0];
		for(i=1; i<farg[0].length; i++)
			msgtext = msgtext.replace("%"+i, farg[0][i]);
	}
	else
		msgtext = farg[0];

	if(typeof(this.msg_function) == "function")
	{
		if(farg.length > 1 && farg[1].indexOf("javascript") != -1)
			this.msg_function(msgtext, farg[1]);
		else
			this.msg_function(msgtext);		
	}
	else
		alert(msgtext);
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function checkForm_send()
{
	var result = 1;
	var i = 0;
	var tmp;

	if(this.ok)
	{
		for(i=0; i< this.box.length; i++)
		{
			result = this.validate(i);

			if(result != 1)
			{
				tmp = this.box[i]["type"];

				if(tmp == "text" || tmp == "textarea" || tmp == "password")
					this.blink_fld(this.fObj.name, 1, this.box[i]["name"], this.defaultBorderStyle, this.selectBorderStyle, 0);
				else if((tmp == "radio" || tmp == "checkbox") && this.box[i]["length"] > 1)
					this.blink_fld(this.fObj.name, 2, this.box[i]["name"], "", "", 0);
			
				this.msg(result);
				break;
			}
		}

		if(result == 1)
		{
			if(this.is_prompt)
			{
				if(typeof(this.msg_function) == "function")
					this.msg("send", "javascript:document."+this.formName+".action='"+this.action+"'; document."+this.formName+".submit()");
				else
				{
					if(confirm(""+this.messages["send"]))
					{
						this.fObj.action = this.action;
						this.fObj.submit();
					}
				}
			}
			else
			{
				this.fObj.action = this.action;
				this.fObj.submit();
			}
		}
	}
	else
		this.msg("err");
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function checkForm_validate_fld(i)
{
	var _type = this.box[i]["type"];
	var _fname = this.box[i]["name"];
	var _dname = this.box[i]["display"];
	var _length = this.box[i]["length"];
	var _require = this.box[i]["require"];
	var _between = this.box[i]["between"];
	var _reg = this.box[i]["reg"];
	var _value;
	var vr, vb, ret = 1;

	vr = this.validate_r(this.fObj.elements[_fname], _type, _length);

	if(_require == 1 && !vr)
	{
		if(_type == "checkbox" || _type == "radio" || _type == "select")
			return [this.messages["no_option"], _dname];
		else
			return [this.messages["no_fill"], _dname];
	}

	if(_type == "text" || _type == "textarea" || _type == "password")
	{
		if(vr && !vr.match(this.regxpObj[_reg]))
			return [this.messages["bad_fill"], _dname];
	}	

	if(vr && (_between[0] || _between[1]) && !this.validate_b(vr, _between, _type, _reg, _length))
	{
		if(!_between[0]){_between[0] = "---";}
		if(!_between[1]){_between[1] = "---";}
	
		if(_type == "checkbox" || _type == "radio" || _type == "select")
			return [this.messages["no_between_option"], _dname, _between[0], _between[1]];
		else
			return [this.messages["no_between_fill"], _dname, _between[0], _between[1]];
	}

	return ret;
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function checkForm_validate_require(obj, ftype, flength)
{
	var i;
	var cnt = 0;

	switch (ftype)
	{
		case "checkbox":
		case "radio":

			if(flength == 1)
				return (obj.checked)? 1 : false ;
			else
			{
				for(i=0; i<flength; i++)
				{
					if(obj[i].checked)
						cnt++;
				}
				return (cnt == 0)? false : cnt;
			}

		break;

		case "select":
			return (obj.options[obj.selectedIndex].value.length > 0)? 1 : false ;
		break;
		
		default:
			return (obj.value.length > 0)? obj.value : false ;
		
	}
	return false;
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function checkForm_validate_between(uinput, between, ftype, reg, flength)
{
	if(!between[0] && !between[1])
		return true;

	switch (ftype)
	{
		case "checkbox":
		case "radio":
		case "select":		
			return this.validate_bval(uinput, between);
		break;
		
		case "text":
			if(reg == "int")
				return this.validate_bval(parseInt(uinput), between);
			else if(reg == "float")
				return this.validate_bval(parseFloat(uinput), between);
			else if(reg == "date")
			{
				var dt = uinput.split("-");
				dt = new Date(dt[0], dt[1], dt[2]);
				dt = dt.getTime();

				if(between[0])
				{
					var btw1 = between[0].split("-");
					btw1 = new Date(btw1[0], btw1[1], btw1[2]);
					btw1 = btw1.getTime();
				}
				else
					btw1 = false;
				
				if(between[1])
				{
					var btw2 = between[0].split("-");
					btw2 = new Date(btw2[0], btw2[1], btw2[2]);
					btw2 = btw2.getTime();
				}
				else
					btw2 = false;
				
				return this.validate_bval(dt, [btw1, btw2]);
			}
			else
				return this.validate_bval(uinput.length, between);
				
		break;
		
		default:
			return true;
	}
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function checkForm_validate_between_val(val, btw)
{
	var result = true;

	if(btw[0] && val < btw[0])
		result = false;

	if(result && btw[1] && val > btw[1])
		result = false;

	return result;
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function checkForm_blink_field(f, t, fl, bd, bs, i)
{
	var i;
	i = i+1;

	if(t == 1)
	{
		if(i < 9)
			setTimeout("checkForm_blink_field('"+f+"', "+t+", '"+fl+"', '"+bd+"', '"+bs+"', "+i+")", 200);

		if((document.all || (!document.all && !document.layers)) && navigator.userAgent.indexOf("Opera") == -1)
			document.forms[f].elements[fl].style.border = (i%2 == 1)? bd : bs ;
	}
	else if(t == 2)
	{
		if(i < 8)
			setTimeout("checkForm_blink_field('"+f+"', "+t+", '"+fl+"', '"+bd+"', '"+bs+"', "+i+")", 200);

		document.forms[f].elements[fl][0].checked = (i%2 == 1)? true : false ;
	}
}

//-->
