/*
vim:cindent:enc=utf8:fenc=utf8
*/
/*
 * Podiže članak ili rubriku prema gore radi resortiranja
 * Da bi spremio sort, trebamo i SSC ili SSR
 */
function Gore(x) {
	x.parentNode.parentNode.insertBefore( x.parentNode, x.parentNode.previousSibling );
}
/*
 * Sprema Sort Članaka
 */
function SSC(id, x) {
	var pom = "", zarez = "";
	x = x.parentNode.childNodes[2].firstChild;
	for( var i=0; i<x.childNodes.length; i++ ) {
		pom += zarez + i + "," + x.childNodes[i].lastChild.innerHTML;
		zarez = ",";
	}
	if( http && !uPogonuSam ) {
		Info("Spremam poredak članaka");
		http.open("POST", "rc5/rc_akcije.php", true);
		http.onreadystatechange = Info2;
		uPogonuSam = true;
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		http.send( "action=r_cSS&id="+id+"&niz="+pom );
	}
}
/*
 * Sprema Sort Rubrika
 */
function SSR(id, x) {
	var pom = "", zarez = "";
	x = x.parentNode.childNodes[2]/*.firstChild*/;
	for( var i=0; i<x.childNodes.length; i++ ) {
		pom += zarez + i + "," + x.childNodes[i].lastChild.innerHTML;
		zarez = ",";
	}
	if( http && !uPogonuSam ) {
		Info("Spremam poredak rubrika");
		http.open("POST", "rc5/rc_akcije.php", true);
		http.onreadystatechange = Info2;
		uPogonuSam = true;
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		http.send("action=r_SS&id="+id+"&niz="+pom);
	}
}

/*
 * Docekuje rezultate klika na checkbox
 */
function UI_2(cb) {
	if( http.readyState == 4 ) {
		uPogonuSam = false;
		cb.parentNode.parentNode.parentNode.style.cursor = "auto";
		if( http.responseText.indexOf("OK") == -1 ) {
			cb.checked = ! cb.checked;
			Info( http.responseText );
		}
	}
}

/* 
 * Ukljuci/iskljuci dozvolu za rubriku
 */
function UI_RD(id, doz, cb) {
	if( http && !uPogonuSam ) {
		http.open("POST", "rc5/rc_akcije.php", true);
		http.onreadystatechange = function() { UI_2(cb); } // Trik kako u funkciju prebaciti argument
		uPogonuSam = true;
		cb.parentNode.parentNode.parentNode.style.cursor = "progress";
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		http.send("action=r_UI_RD&id="+id+"&doz="+doz);
	} else {
		cb.checked = ! cb.checked;
	}
}

/*
 * Ukljuci/iskljuci dovzolu citanja clanka
 */
function UI_CV(id, cb) {
	if( http && !uPogonuSam ) {
		http.open("POST", "rc5/rc_akcije.php", true);
		http.onreadystatechange = function() { UI_2(cb); } // Trik kako u funkciju prebaciti argument
		uPogonuSam = true;
		cb.parentNode.parentNode.parentNode.style.cursor = "progress";
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		http.send("action=c_UI_CV&id="+id);
	} else {
		cb.checked = ! cb.checked;
	}
}

/* 
 * Ukljuci/iskljuci primanje clanaka preko mail-liste (za korisnike)
 */
function PO_ML(ulime, id, cb) {
	if( puno( ulime ) ) {
		if( http && !uPogonuSam ) {
			http.open("POST", "rc5/rc_akcije.php", true);
			http.onreadystatechange = function() { UI_2(cb); } // Trik kako u funkciju prebaciti argument
			uPogonuSam = true;
			cb.parentNode.parentNode.parentNode.style.cursor = "progress";
			http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			http.send("action=r_PO_ML&id="+id+"&ulime="+ulime);
		} else {
			cb.checked = ! cb.checked;
		}
	} else {
		Info("<div>Nemate ovlasti!<br />&nbsp;</div><div style='font-size:small'>Da biste koristili ovu moguÄ‡nost, morate se prijaviti, a ako niste registrirani, morate se prethodno registrirati!</div>");
	}
}

function MLClanka(id, otac) {
	if( http && !uPogonuSam ) {
		Info("Ĺ aljem ÄŤlanak na mail listu");
		http.open("POST", "rc5/rc_akcije.php", true);
		http.onreadystatechange = Info3;
		uPogonuSam = true;
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		http.send("action=c_MLClanka&id="+id+"&otac="+otac);
	}
}

function BrisanjeClanka( id, otac, program ) {
   if( http && !uPogonuSam ) {
      Info("BriĹˇem ÄŤlanak #"+id+" iz rubrike #"+otac);
      http.open("POST", "rc5/rc_akcije.php", true);
      http.onreadystatechange = function() {
         if( http.readyState == 4 ) {
            uPogonuSam = false;
            var inf = document.getElementById("info");
            inf.style.display = "block";
            inf.lastChild.innerHTML += http.responseText;
            if( http.responseText.indexOf("GreĹˇka") == -1 ) {
               setTimeout( "ZatvoriInfo()", 2 );
               location = program+"?action=r_vidi&id="+otac;
            }
         }
      }
      uPogonuSam = true;
      http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
      http.send("action=c_brisi&id="+id+"&otac="+otac);
   }
}

function BrisanjeRubrike( id, otac, program ) {
   if( http && !uPogonuSam ) {
      Info( "Brišem rubriku #"+ id + " iz rubrike #" + otac );
      http.open( "POST", "rc5/rc_akcije.php", true );
      http.onreadystatechange = function() {
         if( http.readyState == 4 ) {
            uPogonuSam = false;
            var inf = document.getElementById("info");
            inf.style.display = "block";
            inf.lastChild.innerHTML += http.responseText;
            if( http.responseText.indexOf("GreĹˇka") == -1 ) {
               setTimeout( "ZatvoriInfo()", 2 );
               location = program+"?action=r_vidi&id="+otac;
            }
         }
      }
      uPogonuSam = true;
      http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
      http.send("action=r_brisi&id="+id+"&otac="+otac);
   }
}

/* NOVE FUNKCIJE */

/*
 * AdminPanel se pokreće klikom na gumbić koji otvara i učitava AdminPanel
 * 	preko AJAX-a. Već tamo je osigurano da se učitava AdminPanel za id
 * 	koji je id dotične rubrike, podrubrike ili ako se radi o članku, onda
 * 	je to id oca toga članka (dakle id rubrike toga članka).
 *
 * 	clanak označava radi li se o članku (1) ili ne (0)
 */
function AdminPanel(vrh, id, clanak) {
	if( http && !uPogonuSam ) {
		var bd = document.getElementsByTagName("BODY") [0] ;
		var aP = document.getElementById("adminPanel");
		var aS = aP.firstChild.firstChild; 							// Sličica za otvori/zatvori
		
		bd.style.cursor = "progress";
		aS.style.cursor = "progress";
		/* 
		 * Šaljemo na AJAX primljeni id rubrike 
		 */
		http.open("POST", "rc5/rc_akcije.php", true); 
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				uPogonuSam = false;
				/*
				 * Upiši u Admin Panel ono što si dobio od AJAX-a
				 */
				aP.lastChild.innerHTML = http.responseText;
				aS.src = 'stilovi/zatvori.gif';
				aS.title = aS.alt = 'Zatvori';
				bd.style.cursor = "auto";
				aS.style.cursor = "auto";
				aS.onclick = function() {
					aP.lastChild.innerHTML = "";
					aS.src = 'stilovi/otvori.gif';
					aS.title = aS.alt = 'Otvori';
					aS.onclick = function() {
						AdminPanel(vrh, id, clanak);
					}
				}
			}
		}
		uPogonuSam = true;
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		http.send("action=r_AdminPanel&vrh="+vrh+"&id="+id+"&clanak="+clanak);
	}
}

// Kreiranje Forme za promjenu dozvola chgDoz 
// ------------------------------------------
function KreirajFCD() {
	var f = document.createElement( "FORM" );
	f.id = "chgDoz";
	f.className = "neprint";
	var s = document.createElement( "FIELDSET" );
	var l = document.createElement( "LABEL" );
	l.style.fontWeight = "bold";
	l.appendChild( document.createTextNode( "PROMJENA DOZVOLA" ) );
	s.appendChild( l );
	var t = document.createElement( "TABLE" );

	// Rubrika ---
	var r = document.createElement( "TR" );
	var d = document.createElement( "TD" );
	d.className = "f";
	d.appendChild( document.createTextNode( "Rubrika: " ) );
	r.appendChild( d );
	d = document.createElement( "TD" );
	var i = document.createElement( "INPUT" );
	i.name = "rub";
	i.disabled = "true";
	d.appendChild( i );
	r.appendChild( d );
	t.appendChild( r );

	// Korisnik ---
	r = document.createElement( "TR" );
	d = document.createElement( "TD" );
	d.className = "f";
	d.appendChild( document.createTextNode( "Korisnik: " ) );
	r.appendChild( d );
	d = document.createElement( "TD" );
	i = document.createElement( "INPUT" );
	i.name = "ulime";
	d.appendChild( i );
	r.appendChild( d );
	t.appendChild( r );

	// Admin ---
	r = document.createElement( "TR" );
	d = document.createElement( "TD" );
	d.className = "f";
	d.appendChild( document.createTextNode( "Admin: " ) );
	r.appendChild( d );
	d = document.createElement( "TD" );
	i = document.createElement( "INPUT" );
	i.type = "checkbox";
	i.name = "a";
	d.appendChild( i );
	var ii = document.createElement( "I" );
	ii.appendChild( document.createTextNode( " Upravljanje dozvolama" ) );
	d.appendChild( ii );
	r.appendChild( d );
	t.appendChild( r );
	
	// Rubrike ---
	r = document.createElement( "TR" );
	d = document.createElement( "TD" );
	d.className = "f";
	d.appendChild( document.createTextNode( "Rubrike: " ) );
	r.appendChild( d );
	d = document.createElement( "TD" );
	i = document.createElement( "INPUT" );
	i.type = "checkbox";
	i.name = "r";
	d.appendChild( i );
	ii = document.createElement( "I" );
	ii.appendChild( document.createTextNode( " Upravljanje rubrikama" ) );
	d.appendChild( ii );
	r.appendChild( d );
	t.appendChild( r );
	
	// ÄŚlanci ---
	r = document.createElement( "TR" );
	d = document.createElement( "TD" );
	d.className = "f";
	d.appendChild( document.createTextNode( "ÄŚlanci: " ) );
	r.appendChild( d );
	d = document.createElement( "TD" );
	i = document.createElement( "INPUT" );
	i.type = "checkbox";
	i.name = "c";
	d.appendChild( i );
	ii = document.createElement( "I" );
	ii.appendChild( document.createTextNode( " Upravljanje ÄŤlancima" ) );
	d.appendChild( ii );
	r.appendChild( d );
	t.appendChild( r );


	// JoĹˇ gumbi
	r = document.createElement( "TR" );
	d = document.createElement( "TD" );
	d.colSpan = "2";
	d.style.textAlgin = "right";
	i = document.createElement( "INPUT" );
	i.type = "button";
	i.value = "Odustani";
	i.onclick = function() {
		document.getElementById( "adminPanel" ).removeChild( document.getElementById( "chgDoz" ) );
	}
	i.style.margin = "5px";
	d.appendChild( i );
	i = document.createElement( "INPUT" );
	i.type = "button";
	i.value = "Spremi";
	i.onclick = SpremiDozvole;
	i.style.margin = "5px";
	d.appendChild( i );
	r.appendChild( d );
	t.appendChild( r );

	s.appendChild( t );
	f.appendChild( s );
	return( f );
}

function ChgDozAdmina(ulime, rub, span) {
	var cD = document.getElementById( "chgDoz" );
	if( http && !uPogonuSam ) {
		http.open("POST", "rc5/rc_akcije.php", true); 
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				doz = http.responseText.split(",");
				if( cD  == null ) {
					cD = KreirajFCD();
				}
				var aP = document.getElementById( "adminPanel" );
				aP.insertBefore( cD, aP.firstChild );
				cD.rub.value = rub;
				cD.ulime.value = ulime;
				cD.ulime.disabled = true;
				cD.a.checked = doz[0]==1 ? true : false;
				cD.r.checked = doz[1]==1 ? true : false;
				cD.c.checked = doz[2]==1 ? true : false;
				cD.span = span;
				cD.staro = true;
				
				uPogonuSam = false;
			}
		}
		uPogonuSam = true;
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		http.send("action=r_UzmiDozvole&ulime="+ulime+"&id="+rub);
	}
}

function DodajAdmina(rub, span) {
	var cD = document.getElementById( "chgDoz" );
	if( cD  == null ) {
		cD = KreirajFCD();
	}
	var aP = document.getElementById( "adminPanel" );
	aP.insertBefore( cD, aP.firstChild );
	cD.rub.value = rub;
	cD.ulime.value = '';
	cD.ulime.disabled = false;
	cD.a.checked = false;
	cD.r.checked = false;
	cD.c.checked = true;
	cD.span = span;
	cD.staro = false;
}

/* 
 * Sprema dozvole za pojedinog admina
 * kod spremanja forme chgDoz
 */

function SpremiDozvole() {
	var cD = document.getElementById("chgDoz");
	rub = cD.rub.value;
	ulime = cD.ulime.value;
	a = cD.a.checked;
	r = cD.r.checked;
	c = cD.c.checked;
	span = cD.span;

	if( cD.staro ) { // VeÄ‡ postojeÄ‡i admin
		if( a || r || c ) {
	// S p r e m a n j e   a d m i n a
	//-------------------------------
			if( http && ! uPogonuSam ) {
				http.open("POST",  "rc5/rc_akcije.php", true); 
				http.onreadystatechange = function() {
					if( http.readyState == 4 ) {
						uPogonuSam = false;
						if( http.responseText == "OK" ) {
							document.getElementById( "adminPanel" ).removeChild( document.getElementById( "chgDoz" ) );
						} else {
							Info( "("+http.responseText+")" );
						}
					}
				}
				uPogonuSam = true;
				http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
				alert("SPREMI ADMINA\\n"+ulime+" "+rub+" "+a+" "+r+" "+c);
				http.send("action=r_SpremiAdmina&ulime="+ulime+"&id="+rub+"&a="+a+"&r="+r+"&c="+c);
			}
		} else { 
	// B r i s a n j e    a d m i n a
	// ---------------------------------
			if( confirm("Nisu dodijeljene nikakve dozvole.\n\nĹ˝elite li ukloniti admina iz rubrike?") ) {
				if( http && ! uPogonuSam ) {
					http.open("POST", "rc5/rc_akcije.php", true); 
					http.onreadystatechange = function() {
						if( http.readyState == 4 ) {
							uPogonuSam = false;
							if( http.responseText == "OK" ) {
								span.parentNode.removeChild( span );
								document.getElementById( "adminPanel" ).removeChild( document.getElementById( "chgDoz" ) );
							} else {
								Info( http.responseText );
							}
						}
					}
					uPogonuSam = true;
					http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
					http.send("action=r_ObrisiAdmina&ulime="+ulime+"&id="+rub);
				} else {
					alert( "Postupak uklanjanja admina nije uspio. PokuĹˇate opet!" );
				}
			}
		}

	} else {  // Novi admin
		if( puno( ulime ) ) {
			if( a || r || c ) {		// D o d a v a n j e    a d m i n a
				if( http && ! uPogonuSam ) {
					http.open("POST", "rc5/rc_akcije.php", true); 
					http.onreadystatechange = function() {
						if( http.readyState == 4 ) {
							uPogonuSam = false;
							if( http.responseText == "OK" ) {
								document.getElementById( "adminPanel" ).removeChild( document.getElementById( "chgDoz" ) );
								var nA = document.createElement("SPAN");
								nA.className = "link";
								nA.textContent = ulime;
								nA.onclick = function() {
									ChgDozAdmina( ulime, rub, this );
								}
								span.parentNode.insertBefore( nA, span );
								span.parentNode.insertBefore( document.createTextNode(" "), span );
							} else {
								Info( http.responseText );
							}
						}
					}
					uPogonuSam = true;
					http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
					alert("DODAJ ADMINA\\n"+ulime+" "+rub+" "+a+" "+r+" "+c);
					http.send("action=r_DodajAdmina&ulime="+ulime+"&id="+rub+"&a="+a+"&r="+r+"&c="+c);
				}
			} else {
				alert("Morate dodati bar jednu dozvolu ako Ĺľelite spremiti admina!");
			}
		} else {
			alert("Morate unijeti korisniÄŤko ime admina!");
		}
	}
}



function Ponisti() {
}

function r_premjesti(kaj, kam) {	// Premjestanje podrubrike u novu rubriku
	if( http && !uPogonuSam ) {
		
		http.open("POST", "rc5/rc_akcije.php", true); 
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				uPogonuSam = false;
				if( http.responseText != "OK" ) {
					Info(http.responseText);
				} else {
					location.replace("?action=r_vidi&id="+kaj);
				}
			}
		}
		uPogonuSam = true;
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		http.send("action=r_premjesti&id="+kaj+"&kam="+kam);
	}
}

function c_premjesti(kaj, kam) {
	if( http && !uPogonuSam ) {
		
		http.open("POST", "rc5/rc_akcije.php", true); 
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				uPogonuSam = false;
				if( http.responseText != "OK" ) {
					Info(http.responseText);
				} else {
					location.replace("?action=c_vidi&id="+kaj);
				}
			}
		}
		uPogonuSam = true;
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		http.send("action=c_premjesti&id="+kaj+"&kam="+kam);
	}
}

/*
 * Slanje clanka mailom
 */
function Posalji( kaj, dozvola ) {
	if( dozvola < 1 ) {
		alert( "SIGURONOSNA PROVJERA\\nDa biste mogli slati ÄŤlanke\\nmorate biti prijavljeni (i registrirani) na KNI." );
		alert( "Ako niste registrirani - idite na link [Registracija].\\nAko niste prijavljeni - idite na link [Prijava].");
	} else {
		var email = prompt("UpiĹˇite e-mail adresu primatelja!", "");
		if( email != null && jeMail( email ) ) {
			Info( "Saljem mail" );
			if( http && !uPogonuSam ) {
				
				http.open("POST", "rc5/rc_akcije.php", true); 
				http.onreadystatechange = function() {
					if( http.readyState == 4 ) {
						uPogonuSam = false;
						if( http.responseText != "OK" ) {
							Info(http.responseText);
						} else {
							Info( "ÄŚlanak je poslan!" );
						}
					}
				}
				uPogonuSam = true;
				http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
				http.send("action=c_Posalji&id="+kaj+"&email="+email);
			}
		} else {
			alert( "Niste unijeli ispravnu e-mail adresu!" );
		}
	}
}

/* PREMJESTANJE VISE CLANAKA */
function r_premjestiClanke( rub ) {
	var fC = document.getElementById( "fClanci" );
	fC.action = "rc5/rc_akcije.php?action=r_premjestiClanke&id="+rub;
	fC.submit();
}

// Kreiranje forme za editiranje ÄŤlanka ili rubrike
// ------------------------------------------------
function KreirajCF( ) {
	if( arguments.length > 0 ) {
		tekst = arguments[0];
	} else {
		tekst = "";
	}
	var f = document.createElement( "FORM" );
	f.name = "cForm";
	f.id = "cForm";
	f.className = "neprint";
	f.method = "post";
	f.style.borderType = "solid";
	f.style.borderColor = "gray";
	f.style.borderWidth = "1px";

	h = document.createElement( "H1" );
	h.id = "FCKnaslov";

	f.appendChild( h );

	h = document.createElement( "H3" );
	var b = document.createElement( "B" );
	b.appendChild( document.createTextNode( "Naslov: " ) );
	h.appendChild( b );
	var i = document.createElement( "INPUT" );
	i.name = "naslov";
	i.size = "50";
	i.value = "";
	h.appendChild( i );
	f.appendChild( h );

	/* OVDJE DOLAZI FCK */
	var d = document.createElement( "DIV" );
	d.name = "FCK";
	d.id = "FCK";
	var fck = new FCKeditor("FCK", "", "500");
	fck.Value = tekst;
	d.innerHTML = fck.CreateHtml();


	f.appendChild( d );
	/* KRAJ FCK */
	h = document.createElement( "H3" );
	b = document.createElement( "B" );
	b.appendChild( document.createTextNode( "Razdoblje: " ) );
	h.appendChild( b );
	i = document.createElement( "INPUT" );
	i.name = "razdoblje";
	i.size = "10";
	i.value = "";
	h.appendChild( i );
	f.appendChild( h );

	i = document.createElement( "INPUT" );
	i.type = "button";
	i.value = "???";
	i.onclick = function() {
		open( "kada.html", "kada", "width=480, height=550 noresize" );
	}
	i.style.margin = "5px";
	f.appendChild( i );

	i = document.createElement( "INPUT" );
	i.type = "reset";
	i.value = "Odustani";
	i.onclick = Odustani;
	i.style.margin = "5px";
	f.appendChild( i );

	i = document.createElement( "INPUT" );
	i.type = "submit";
	i.value = "Spremi";
	i.style.margin = "5px";
	f.appendChild( i );

	return f;
}
