/*
	M K C M S  Java Script
*/

// Dodaje sadrzaj
function update(data, name) {
	document.getElementById(name).innerHTML = data;		
}
// Vraca vrijednost polja
function getVal(id) {
	return document.getElementById(id).value;
}
// Otkriva element
function show(id) {
	document.getElementById(id).style.display = '';
}
	
// Skriva element
function hide(id) {
	document.getElementById(id).style.display = 'none';
}
// ShowHide
function showHide(id) {	
	el = document.getElementById(id);
	if (el.style.display == 'none')			
		el.style.display = '';			
	else			
		el.style.display = 'none';			
}
	
// Prikazuje loading div
function showLoading()
	{
		var crossobj=document.getElementById("loading");
		var ScrollTop = document.body.scrollTop;

		if (ScrollTop == 0)
			{			
				if (window.pageYOffset)
					{
						ScrollTop = window.pageYOffset;
					}
				else
					{
						ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
					}			
			}

		crossobj.style.top=ScrollTop+"px";
		crossobj.style.display = ''; 
	}
	
// Skriva loading div
function hideLoading() {
	document.getElementById("loading").style.display = 'none';
}
	
/* Vraca value selektovanog elementa unutar select menija */
function getSelected(ob)
	{
		for (i = 0; i < ob.options.length; i++)
			{
				if (ob.options[i].selected)
					{
						return ob.options[i].value;
						break;
					}
			}
	}

/* Vraca vrijednost selektovanog radio buttona */
function getSelectedRadio(ob)
	{
		for (i=0; i<ob.length; i++)
			{
				if (ob[i].checked)
					return ob[i].value;
			}
			
		return 0;
	}

/* Selektuje određeni radio button na osnovu vrijednosti */
function selectRadioVal(ob, sel)
	{
		for (i=0; i<ob.length; i++)
			{
				if (ob[i].value == sel)
					{
						ob[i].checked = true; 
						return 1;
					}
			}
			
		return 0;
	}
	
/* Provjerava da li postoji error */
function errorsExists(data)
	{
		if (data.errors)
			{
				alert(data.errors);
				return false;
			}
		else
			{
				return true;
			}
	}

/* Upisuje errors & show done u div */
function sdeToDiv(data)
	{
		var htmlData = '';
		hide('errorsDiv');
		hide('showDoneDiv');
		
		if (data.errors)
			{
				update(nl2br(data.errors), 'errorsText');
				
				if (data.errorLink)
					{
						document.getElementById('errorsLink').href = data.errorLink;
						show('errorsLinkSpan');
					}
					
				show('errorsDiv');
			}
			
		if (data.showDone)
			{
				update(nl2br(data.showDone), 'showDoneText');
				
				if (data.showDoneLink)
					{
						document.getElementById('showDoneLink').href = data.showDoneLink;
						show('showDoneLinkSpan');
					}
					
				show('showDoneDiv');
			}
		
		if (data.errors)
			return false;
		else
			return true;
	}

/* Prikazuje errors & show done u alert() */
function sdeToAlert(data) {
	var htmlData = '';		
	if (data.errors) {
		alert(data.errors);				
	}			
	if (data.showDone) {
		alert(data.showDone);				
	}			
}
	
/* Ako je string prazan vraca zamjenu */
function ifEmptyReturn(data, ret) {
	if (!data) {
		return ret;
	} else {
		return data;
	}
}
	
/* Mijenja class elementu */
function changeClass(id, name) {
	document.getElementById(id).className = name;
}
	
/* Kao php stripslashes() funkcija */
function stripslashes(str) 
	{
		str=str.replace(/\\'/g,'\'');
		str=str.replace(/\\"/g,'"');
		str=str.replace(/\\\\/g,'\\');
		str=str.replace(/\\0/g,'\0');
		return str;
	}

/* Kao php nl2br() funkcija */
function nl2br(str) 
	{
	if(typeof(str)=="string") 
		return str.replace(/(\r\n)|(\n\r)|\r|\n/g,"<br />");
	else 
		return str;
}

/* Kao php urlencode() funkcija */
function urlencode (str) 
	{
		return str.replace(/&/g, '%26');
	}
	
/* TO DO */
function addElement(inEl, elTag, inHtml, otherD) 
	{
		parentEl = document.getElementById(inEl);
		newEl = document.createElement(elTag);
		/*if (otherD.id)
			newEl.setAttribute('id', otherD.id);
		if (otherD.class)
			newEl.setAttribute('class', otherD.id);
		if (otherD.style)
			newEl.setAttribute('style', otherD.style);*/
		for (index in otherD)
			newEl.setAttribute(index, otherD[index]);
		newEl.innerHTML = inHtml;
		parentEl.appendChild(newEl);
	}
	
function removeElement(inId, remId) {
  var d = document.getElementById(inId);
  var olddiv = document.getElementById(remId);
  d.removeChild(olddiv);
}
function popUpBasic(url, w, h)
	{
		fineline = window.open(url,'fineline','toolbar=0,scrollbars=0,location=0,status=0,menubars=0,resizable=0,width='+w+',height='+h);
		width=(screen.width/2)-w/2;
		height=(screen.height/2)-h/2;
		fineline.window.moveTo(width,height)
	}