/*
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) {
	document.getElementsByTagName("BODY")[0].style.cursor = "progress";
	var pom = "", zarez = "";
	x = x.parentNode.parentNode.lastChild;
	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", "rc/rc_akcije.php", true);
		http.onreadystatechange = OkZatvaraInfo;
		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) {
	document.getElementsByTagName("BODY")[0].style.cursor = "progress";
	var pom = "", zarez = "";
	x = x.parentNode.childNodes[1];

	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", "rc/rc_akcije.php", true);
		http.onreadystatechange = OkZatvaraInfo;
		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", "rc/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", "rc/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", "rc/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", "rc/rc_akcije.php", true);
		http.onreadystatechange = BezGreskeZatvaraInfo;
		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( ! confirm( "Doista želite obrisati članak #" + id ) ) return;
   if( http && !uPogonuSam ) {
      Info("Brišem članak #"+id+" iz rubrike #"+otac);
      http.open("POST", "rc/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 );
   }
}

function BrisanjeRubrike( id, otac, program ) {
	if( ! confirm( "Doista želite obrisati rubriku #" + id ) ) return;
   if( http && !uPogonuSam ) {
      Info("Brišem podrubriku #"+id+" iz rubrike #"+otac);
      http.open("POST", "rc/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 );
   }
}

/* 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 aP = document.getElementById("adminPanel");
		var aS = aP.firstChild.firstChild; 							// Sličica za otvori/zatvori
		var bd = document.getElementsByTagName("BODY") [0] ;
		bd.style.cursor = "progress";
		aS.style.cursor = "progress";
		/* 
		 * Šaljemo na AJAX primljeni id rubrike 
		 */
		http.open("POST", "rc/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);
	}
}

function ChgDozAdmina(ulime, rub, span) {
	if( http && !uPogonuSam ) {
		http.open("POST", "rc/rc_akcije.php", true); 
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				doz = http.responseText.split(",");
				var cD = document.getElementById("chgDoz");
				cD.style.display = "block";
				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");
	cD.style.display = "block";
	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",  "rc/rc_akcije.php", true); 
				http.onreadystatechange = function() {
					if( http.readyState == 4 ) {
						uPogonuSam = false;
						if( http.responseText == "OK" ) {
							cD.style.display = "none";
						} 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", "rc/rc_akcije.php", true); 
					http.onreadystatechange = function() {
						if( http.readyState == 4 ) {
							uPogonuSam = false;
							if( http.responseText == "OK" ) {
								cD.style.display = "none";
								span.parentNode.removeChild( span );
							} 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", "rc/rc_akcije.php", true); 
					http.onreadystatechange = function() {
						if( http.readyState == 4 ) {
							uPogonuSam = false;
							if( http.responseText == "OK" ) {
								cD.style.display = "none";
								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", "rc/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", "rc/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( "Šaljem mail" );
			if( http && !uPogonuSam ) {
				
				http.open("POST", "rc/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 = "rc/rc_akcije.php?action=r_premjestiClanke&id="+rub;
	fC.submit();
}

function PripremaForme( kopirajNT ) {
	var fid = document.getElementById( "FID" );
	if( fid !== null ) {
		alert( "Ne možemo krenuti na novo uređivanje dok trenutno nije dovršeno!" );
		return false; // Ne može mijenjati kad se već mijenja!!!
	}
	var nas = document.getElementById( "naslov" );

	var txt = document.getElementById( "tekst" );
	var raz = document.getElementById( "razdoblje" );
	var sBasePath = "http://www.katolici.org/fckeditor2/";
	var f = document.createElement( "FORM" );
	f.method = "post";
	f.id = "FID";

	var h2 = document.createElement( "H2" );
	h2.name = "h2";
	f.appendChild( h2 );

	var l1 = document.createElement( "LABEL" );
	l1.appendChild( document.createTextNode( "Naslov: " ) );
	var i1 = document.createElement( "INPUT" );
	i1.name = "naslov";
	l1.appendChild( i1 );
	f.appendChild( l1 );

	var l2 = document.createElement( "LABEL" );
	l2.appendChild( document.createTextNode( "Tekst: " ) );
	var i2 = document.createElement( "TEXTAREA" );
	i2.name="tekst";
	l2.appendChild( i2 );
	f.appendChild( l2 );

	var l3 = document.createElement( "LABEL" );
	l3.appendChild( document.createTextNode( "Razdoblje:"  ) );
	var i3 = document.createElement( "INPUT" );
	i3.name = "razdoblje";
	i3.value = raz.innerHTML;
	l3.appendChild( i3 );
	f.appendChild( l3 );

	var i = document.createElement( "INPUT" );
	i.type = "submit";
	i.value = "Spremi";
	f.appendChild( i );

	i = document.createElement( "BUTTON" );
	i.appendChild( document.createTextNode( "Odustani" ) );
	i.onclick = function() {	
		this.parentNode.parentNode.removeChild( this.parentNode );
		nas.style.display = "block";
		txt.style.display = "block";
	}
	f.appendChild( i );

	if( kopirajNT ) {
		i1.value = nas.innerHTML;
		i2.appendChild( document.createTextNode( txt.innerHTML ) );
		i3.value = raz.innerHTML;
	}
	var gS = document.getElementById( "glavniStupac" );
	gS.insertBefore( f, gS.firstChild );
	var oFCKeditor = new FCKeditor( "tekst" ) ;
	oFCKeditor.BasePath	= sBasePath ;
	oFCKeditor.ToolbarSet = "Ivica";
	oFCKeditor.Config["StylesXmlPath"] = sBasePath + "knistyles.xml";
	oFCKeditor.Config["EditorAreaCSS"] = "http://www.katolici.org/stilovi/fck.css";
	oFCKeditor.ReplaceTextarea() ;

	return f;
}

function SakrijNT( f ) {
	var gs  = document.getElementById( "glavniStupac" );
	var nas = document.getElementById( "naslov" );
	nas.style.display = "none";
	document.getElementById( "tekst" ).style.display = "none";
}

/* IZMJENA ČLANKA */
function IzmjenaClanka( id ) {
	var f = PripremaForme( true );
	if( f === false ) return;
	SakrijNT( f );
	f.action = "http://www.katolici.org/rc/rc_akcije.php?action=c_SpremiClanak&id=" + id;
	f.firstChild.innerHTML = "Uređivanje članka";
}


/* DODAVANJE ČLANKA */
function DodavanjeClanka( otac ) {
	var f = PripremaForme();
	SakrijNT( f );
	f.action = "http://www.katolici.org/rc/rc_akcije.php?action=r_SpremiNoviClanak&otac=" + otac;
	f.firstChild.innerHTML = "Dodavanje članka";
}

/* IZMJENA RUBRIKE */
function IzmjenaRubrike( id ) {
	var f = PripremaForme( true );
	SakrijNT( f );
	f.action = "http://www.katolici.org/rc/rc_akcije.php?action=r_SpremiRubriku&id=" + id;
	f.firstChild.innerHTML = "Uređivanje rubrike";
}

/* DODAVANJE RUBRIKE */
function DodavanjeRubrike( id ) {
	var f = PripremaForme( );
	SakrijNT( f );
	f.action = "http://www.katolici.org/rc/rc_akcije.php?action=r_SpremiNovuRubriku&id=" + id;
	f.firstChild.innerHTML = "Uređivanje rubrike";
}

/* KOMENTIRANJE ČLANKA */
function Komentiraj( id, dozvola ) {
	if( dozvola < 2 ) {
		alert( "Vi na žalost nemate pravo kometirati članke.\nMorate se prijaviti i popuniti potrebne podatke." );
	}
	var f = PripremaForme( );
	var gs  = document.getElementById( "glavniStupac" );
	var nas = document.getElementById( "naslov" );
	f.naslov.style.display = "none";
	if( nas.style.display == "none" ) {
		alert( "Ne možemo krenuti na novo uređivanje dok trenutno nije dovršeno!" );
		return; // Ne može mijenjati kad se već mijenja!!!
	}
	f.action = "http://www.katolici.org/rc/rc_akcije.php?action=c_Komentiraj&id=" + id;
	f.firstChild.innerHTML = "Komentiranje članka";
}
