
//usage
//<a href="#" onclick="return doCheck(event)" onfocus="doCheckF(event)"> <input type=checkbox> sth...</a>
function doCheck(evt){
	var src = evt.srcElement;
	if (src == null) src = evt.target;
	if (src.tagName == "INPUT") return true;
	var nodes = src.childNodes;
	for (var i=0; i<nodes.length; i++){
		var c = nodes[i];
		if (c.tagName == "INPUT" && c.getAttribute("type") != null){
			c.checked = !c.checked;
			c.focus();
		}
	}
	return false;
}
function doCheckF(evt){
	var src = evt.srcElement;
	if (src == null) src = evt.target;
	if (src.tagName == "INPUT") return true;
	var nodes = src.childNodes;
	for (var i=0; i<nodes.length; i++){
		var c = nodes[i];
		if (c.tagName == "INPUT" && c.getAttribute("type") != null){
			c.focus();
		}
	}
	return false;
}

function co_loadFormFocus(){
	if (document.forms == null) return;
	for(var i=0; i<document.forms.length; i++){
		var fi = Form.getElements(document.forms[i]);
		if (fi.type=="hidden") continue;
		for(var j=0; j<fi.length; j++){
			var elem = fi[j];
			Event.observe(elem,"focus",focus);
			Event.observe(elem,"blur", blur);
		}
	}

	function focus(evt){
		var elem = Event.element(evt);
		Element.addClassName(elem,"focused");
	}

	function blur(evt){
		var elem = Event.element(evt);
		Element.removeClassName(elem,"focused");
	}
}

function CheckForm(){

	if (!CheckForm.prototype.loaded){
		CheckForm.prototype.loaded = true;
		CheckForm.prototype.init = init;
	}
	
	function init(){
		if (document.forms == null) return;
		for(var i=0; i<document.forms.length; i++){
			if (document.forms[i].onsubmit == null){
				document.forms[i].onsubmit = check;
			}
		}
	}

	function check(evt){
	try{
		var fi = Form.getElements(this);
		for(var j=0; j<fi.length; j++){
			var elem = fi[j];
			if (elem.type=="hidden" || elem.type=="submit"|| elem.type=="image") continue;
			if(elem.value == "" || elem.value == null){
				if (elem.getAttribute("empty") != "ok"){
					alert(CheckForm.prototype.form_message.must_fill);
					elem.focus();
					return false;
				}
			}else{
				var regex = elem.getAttribute("regex");
				if(regex != null && regex != ""){
					var format = elem.getAttribute("format");
					var example = elem.getAttribute("example");
					var template = CheckForm.prototype.format[regex];
					if(template != null) {
						regex = template.regex;
						if(format == null) format = template.format;
						if(example == null) example = template.example;
					}
					var reg = new RegExp(regex);
					if (!reg.test(elem.value)){
						var message = CheckForm.prototype.form_message.wrong_format[0];
						if (format != null && format != "") message += CheckForm.prototype.form_message.wrong_format[1] +"    "+ format+"\n";
						if (example != null && example != "") message += CheckForm.prototype.form_message.wrong_format[2] +"    "+ example;
						elem.select();
						alert(message);
						elem.focus();
						elem.select();
						return false;
					}
				}
			}
		}
		return true;
	}catch (e){alert(e);return false;}
	}
}

var checkForm = new CheckForm();

CheckForm.prototype.form_message = {
	"must_fill": "Morate popuniti obavezna polja",
	"wrong_format": ["Vrijednost je krivo upisana!\n",
			"Traženi format je:\n",
			"Primjer pravilnog upisa je:\n"
		]
};

CheckForm.prototype.format = new Array();
CheckForm.prototype.format["num"] = {
	"regex": "^\\d+$",
	"format":"#",
	"example":"123"
};
CheckForm.prototype.format["float"] = { 
	"regex": "^((\\d+)|(\\d+\\.\\d+))$",
	"format":"#.#",
	"example":"11.45"
}
CheckForm.prototype.format["hr_date"] = { 
	"regex": "^(0?[1-9]|[12][0-9]|3[01])\\.(0?[1-9]|1[0-2])\\.[0-9]{4}[ ]*$",
	"format":"dd.mm.yyyy",
	"example":"1.1.2005"
};
CheckForm.prototype.format["hr_date_time"] = { 
	"regex": "^(0?[1-9]|[12][0-9]|3[01])\\.(0?[1-9]|1[0-2])\\.[0-9]{4} ([01]?[0-9]|2[0-3]):(0?[0-9]|[1-5][0-9])$",
	"format":"dd.mm.yyyy hh:mm",
	"example":"1.1.2005 11:33"
};
CheckForm.prototype.format["time_min"] = { 
	"regex": "^([01]?[0-9]|2[0-3]):(0?[0-9]|[1-5][0-9])*$",
	"format":"hh:mm",
	"example":"9:33"
};
function focusFirst(){
	try{
		var inp = document.getElementById("firstInput");
		if (inp != null) {
			inp.focus();
			if (inp.select) inp.select();
		}
	}catch(e){
	}
}

function bookmarksite(title, url){
if (document.all)
window.external.AddFavorite(url, title);
else if (window.sidebar)
window.sidebar.addPanel(title, url, "")
}

function doClock(){
	var div = document.getElementById("dateHolder");
	var date = new Date();
	div.innerHTML = " &nbsp; "
		+mask(date.getDate())
		+"."+mask((date.getMonth()+1))
		+"."+date.getFullYear()
		+" &nbsp; <b>"+mask(date.getHours())
		+":"+mask(date.getMinutes())
		+"</b>:"+mask(date.getSeconds());
	
	setTimeout(doClock, 1000);

	function mask(str){
		str = ""+str;
		if(str.length == 1) return "0"+str;
		return str;
	}
}
