

function getObj(obj){
	if(document.getElementById(obj)){
		return document.getElementById(obj)
	}
}

function zamijeniCheckbox(frm){
	//ako ima checkbox-a, zamijeni ih s hidden inputima
	var frm = document.getElementById(frm);
	var chkbox = document.getElementsByTagName("input");
	var hid, newfld;
	for (var i=0;i<chkbox.length;i++){
		if(chkbox[i].getAttribute("type")=="checkbox"){
			//provjeri da li vec postoji hidden field
			if (chkbox[i].getAttribute("name").indexOf("_")<0) {
				newfld = document.createElement("input");
				newfld.setAttribute("name", chkbox[i].getAttribute("name"));
				newfld.setAttribute("type","hidden");
				newfld.id = chkbox[i].id;
				newfld.value = (chkbox[i].checked) ? 1 : 0;
				chkbox[i].setAttribute("name", "_" + chkbox[i].getAttribute("name"));
				chkbox[i].id = "_" + chkbox[i].id;
				//alert(newfld)
				frm.appendChild(newfld);
			}
			else { //ako postoji, preseli vrijednost
				hid = document.getElementById(chkbox[i].getAttribute("name").replace("_",""));
				if(hid){
					hid.value=(chkbox[i].checked) ? 1 : 0;
					//alert(hid.getAttribute("name") + " " + hid.value)
				}
			}
		}
	}
	return true;
	
}


function vratiVrijednosti(polje, vrijednost, id){
//polje u koje se vraćaju vrijednosti
//vrijednosti koje vraćamo (string)
//id za selected
    var sl = document.getElementById(polje);
    sl = (sl) ? sl : opener.document.getElementById(polje);
    if (sl) {
		sl.options.length = 0;
		var arr = vrijednost.split("-+-");
		for(var i=0;i<arr.length-1;i+=2){
			try{
			
				sl.options[sl.options.length] = new Option(arr[i+1], arr[i]);
				if (sl.options[sl.options.length-1].value==id) sl.options[sl.options.length-1].selected=true;
			}
			catch(e){
				alert("Greška kod dodavanja nove opcije???" + e.description)
			}
			
		}

	}
	else{
		alert("Nema elementa?" + polje);
	}
}

function vratiVrijednostiAjax(polje, vrijednost, id){
//polje u koje se vra†aju vrijednosti
//vrijednosti koje vra†amo (string)
//id za selected
    var sl = opener.document.getElementById(polje);
    if (sl){
		sl.value = id;
		sl = opener.document.getElementById(polje.replace("id",""));
		if (sl) {
			sl.value = vrijednost;
			sl.className = "greenInput";
		}
		sl = opener.document.getElementById("_" + polje.replace("id",""));
		if (sl) {
			sl.value = vrijednost;
			sl.className = "greenInput";
		}
    }
}



function showHide(obj, arr, hide){
	//onClick="showHide('divljina', arrPorijeklo);"
	var o = document.getElementById(obj);
	if(o){
	    if(hide){
	        o.style.visibility="hidden";
	        o.style.display="none";
		}
	    else{
		    if(o.style.visibility=="visible"){
	            o.style.visibility="hidden";
	        	o.style.display="none";
			}
			else{
	        	o.style.visibility="visible";
	        	o.style.display="block";
	        }
	        for(var i=0; i<arr.length; i++){
	            var o2 = document.getElementById(arr[i]);
				//alert(obj);
	            if (o2 && obj!=arr[i]){
	                //alert("Skrivam : " + obj);
		            o2.style.visibility="hidden";
		            o2.style.display="none";
	            }
	        }
        }
	}
}

function prozor(stranica, naziv, addUrl ){
	window.open(stranica + "?popup=true&_refreshField=" + naziv.toLowerCase() + "id&id=novi&" + addUrl, "", "height=440,width=800,status=yes,toolbar=yes,menubar=no,location=no,scrollbars=yes")
}
function prozorSized(stranica, naziv, addUrl, visina, sirina ){
	window.open(stranica + "?popup=true&_refreshField=" + naziv.toLowerCase() + "id&id=novi&" + addUrl, "", "height="+visina+",width="+sirina+",status=yes,toolbar=yes,menubar=no,location=no,scrollbars=yes")
}
function prozorPlusId(stranica, naziv, id){
	window.open(stranica + "?popup=true&_refreshField=" + naziv.toLowerCase() + "id&id=" + id, "", "height=440,width=800,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes")
}
function prozorDetail(stranica, naziv){
	if (getObj("_randomId").value != "")
	{
		stranica = stranica + "&_randomId=" + getObj("_randomId").value;
	}
	window.open(stranica, naziv, "height=600,width=1000,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=1")
}

var upisSlobodan=true;

function setMax(val,maxLength){
//texarea fields validation**************
    var reqLen=maxLength;
    $(val).addEvent("keyup",function(){
        if ($(val).value.length>=reqLen){
            var strLen = $(val).value;
            if ((strLen.length-reqLen)>0){
                strLen = strLen.slice(0,-(strLen.length-reqLen));
            }
            alert("Dosegnuli ste maximalan unos teksta.");
			$(val).value = strLen; 
        }
    });
}

var mustHave = [];
var addedEvent = false;
var nedostaje = false;

function provjeriUnos(el){
	if(mustHave.length>0){
		nedostaje = false;
		upisSlobodan = true;
		mustHave.each(function(el) {
			el = $(el);
			if (el){
				if (!addedEvent){
					
					el.addEvent("change", function(){
			        	provjeriUnos(el);
			        });
		        }
		        if(el.selectedIndex<0 || el.value == ""){
					upisSlobodan=false;
					nedostaje = true;
				}
				else {
					upisSlobodan = true;
					if(!nedostaje) nedostaje = false;
				}
				bojajPolje(el, upisSlobodan);
		    }
		})
		addedEvent = true;
	}
	else {
		if (el){
			if(el.selectedIndex<0 || el.value == ""){
				upisSlobodan=false;
			}
			else {
				upisSlobodan = true;			
			}
			el.addEvent("change", function(){
	        	if(el.value!="" || el.selectedIndex<0){
	                upisSlobodan=true;   
	        	}
	        	else{
	                upisSlobodan=false;   
	            }
	        	provjeriUnos(el);
	        });
			
			bojajPolje(el);	
		}
		
	}
	
	if(nedostaje) {
		upisSlobodan = false;
	}
	else {
		upisSlobodan = true;
	}
	return upisSlobodan
}

function bojajPolje(el, nedostaje){
	el = $(el);
	if(el){
        if(nedostaje){
				el.removeClass('upitnikTxt2');
				el.addClass('upitnikTxt');
			}else{
				el.removeClass('upitnikTxt');
				el.addClass('upitnikTxt2');
		}
	}
	else{
        alert("Nema elementa " + el.name + " " + el.id + el);
    }
}
/* remove white space from element */
function white_space(field)
{
	if (field.value==null){
     	field.innerHTML = (field.innerHTML).replace(/^\s*|\s*$/g,'');
     }else{
	 	field.value = (field.value).replace(/^\s*|\s*$/g,'');
	}
}
function generirajObrazac(id,vrstaid) {
	var w = 1024, h = 768;

	if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
	}
	
	var popW = 800, popH = 600;
	
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	window.open('obrasci/default.aspx?id=' + id + '&vrstaid=' + vrstaid, 'Upload', 'width=' + popW +',height=' + popH + ',top=' + topPos + ',left=' + leftPos);
}

var arrNumbersOnly = ["klasa"];

window.addEvent("domready",function() {
	provjeriUnos();
	$$(arrNumbersOnly).each(function(el) {
		if($(el)){
			$(el).addEvent("keyup",function(e) {
				$(el).value = $(el).value.replace(/[^0-9]/, "");
			})
		}
	})
	
	
})

