var brow = navigator.appName=="Microsoft Internet Explorer" ? "ie" : "net";

var winX=0;
var winY=0;

function ietruebody()
{
    return (document.documentElement && document.documentElement.scrollTop)? document.documentElement : document.body
}

/* funkcia na oznacovanie riadkov v tabulke */

function MarkTable(riadok,styl,typ) {
	stlpec=riadok.childNodes;
	for (i=0; i<riadok.childNodes.length; i++) {
		if (stlpec[i].tagName=="TD") {
			var actualStyle=stlpec[i].className;

			if (typ=="over") {
				if (actualStyle==styl+"_click") {
					newClass = actualStyle;
				}
				else {
					newClass = actualStyle+"_over";
				}
			}
			if (typ=="out") {
				if (actualStyle == styl+"_click") {
				    newClass = actualStyle;
				}
				else { newClass = actualStyle.substr(0,actualStyle.length-5); }
			}
			if (typ=="click") {
				if (actualStyle==styl+"_click") {
					newClass = styl+"_over";
				}
				else {
					newClass = styl+"_click";
				}
			}
			stlpec[i].className = newClass;
		}
	}
}

function ZmenStyl(objekt,styl,typ) {
	var actualStyle=objekt.className;
	if (typ=="over") {
		if (actualStyle==styl+"_click") {
			newClass = actualStyle;
		}
		else {
			newClass = actualStyle+"_over";
		}
	}
	if (typ=="out") {
		if (actualStyle == styl+"_click") {
		    newClass = actualStyle;
		}
		else { newClass = actualStyle.substr(0,actualStyle.length-5); }
	}
	objekt.className = newClass;
}


/* FORMULARE */

function SubmitFormular(name) {
	document.getElementById(name).submit();
}

function SubmitFormularGet(name,url) {
	var hodnota=document.getElementById(name).boxform.value;
	url+="&id="+hodnota;
	window.location.assign(url);
}

function Button(operacia,data) {
	switch (operacia) {
		case "cancel" : history.go(-1); break;
		case "cancel2" : window.location.assign(data); break;
	}
}

function isChecked(isitchecked){
	if (isitchecked == true){
		document.adminForm.boxform.value++;
	}
	else {
		document.adminForm.boxform.value--;
	}
}

function isChecked2(isitchecked,hodnota){
	if (isitchecked == true){
		document.adminForm.boxform.value=hodnota;
	}
	else {
		document.adminForm.boxform.value="0";
	}
}

function CheckForm(formular) {
	for (i=0; i<=(document.getElementById(formular).length)-1; i++) {
		if (document.getElementById(formular)[i].type=="radio") {
			if (document.getElementById(formular)[i].checked) {
				
			}
		}
	}
}

function presun_polozku(typ,z_id,do_id,del) {
	//chybove hlasky
	var error1="Vyberte položku, ktorú chcete pesunúť"; //nebola vybrata ziadna polozka
	var error2="Položku nie je možne posunúť"; //zoznam ma iba jednu polozku
	var error3="Položku nie je možné posunúť na vybrané miesto"; //polozku nie je mozne presunut na vybrane miesto
	
	
	if (typ=="presun") {
		//odkial polozka pochadza
		var odkial=document.getElementById(z_id);
		var odkial_pol_id=odkial.selectedIndex;
		//kam polozka ide
		var kam=document.getElementById(do_id);

		//ak bola niektora polozka oznacena
		if (odkial_pol_id!=-1) {
			//vlastnosti polozky
			var polozka=odkial[odkial_pol_id];
			//nova option
			opt=document.createElement("option");
			opt.value=polozka.value;
			opt.text=polozka.text;
			//vlozenie novej option
			if (brow=="net") { kam.appendChild(opt); }
			else { kam.add(opt); }
			//vymazanie polozky v starom zozname
			//ak del==1
			if (del) {
				odkial.removeChild(polozka);
			}
		}
		else {
			alert(error1);
		}
	}
	if (typ=="posun") {
		//odkial polozka pochadza
		var odkial=document.getElementById(z_id);
		var odkial_pol_id=odkial.selectedIndex;
		
		kam=do_id=="+"?1:-1;
		//ak bola niektora polozka oznacena
		if (odkial_pol_id!=-1) {
			//celkova velkost zoznamu
			velkost_zoznamu=odkial.length;
			if (velkost_zoznamu>1) {
				//nova pozicia polozky
				nova_pozicia=odkial_pol_id+kam;
				//ak je mozne polozku presunut na vybrane miesto
				if (nova_pozicia>-1 && nova_pozicia<(velkost_zoznamu)) {
					//ziskanie vlastnosti vybranej polozky
					prva_polozka_value=odkial[odkial_pol_id].value;
					prva_polozka_text=odkial[odkial_pol_id].text;
					//ziskanie vlastnosti polozky kam sa vybrana presuva
					druha_polozka_value=odkial[nova_pozicia].value;
					druha_polozka_text=odkial[nova_pozicia].text;
					//nastavenie vlastnosti vybranej polozky
					odkial[nova_pozicia].value=prva_polozka_value;
					odkial[nova_pozicia].text=prva_polozka_text;
					//nastavenie vlastnosti polozky kam sa vybrana presunva
					odkial[odkial_pol_id].value=druha_polozka_value;
					odkial[odkial_pol_id].text=druha_polozka_text;
					odkial.selectedIndex=nova_pozicia;
				}
				else { alert(error3); }
			}
			else { alert(error2); }
		}
		else { alert(error1); }
	}
}

function SelectAll(zoznam_id) {
	zoznam=document.getElementById(zoznam_id);
	if (zoznam.multiple==false) { zoznam.multiple=true; }
	if (zoznam.length>0) {
		for (i=0; i<=(zoznam.length)-1; i++) {
			zoznam[i].selected=true;
		}
	}
}

/*function HeadMenu(t) {
	t_li=t.className;
	t_li_w=t.offsetWidth;
	od=t_li.length>5?t_li.length-5:0;
	if (t_li=="") { t.className="over"; }
	if (t_li=="prvy") { t.className="prvy_over"; }
	if (t_li.substr(od,5)=="over") { t.className=""; }
	if (t_li.substr(od,5)=="_over") { t.className="prvy"; }
	p=t.parentNode;
	p=p.getElementsByTagName("LI");
	for (i=0; i<p.length; i++) {
		dlzka_c=p[i].className.length>4?p[i].className.length-4:0;
		if (p[i].innerHTML==t.innerHTML && i>0) {
			if (p[i].className.substr(dlzka_c,4)=="over") { p[i-1].className="over2"; }
			else { p[i-1].className=""; }
		}
		if (p[i].innerHTML==t.innerHTML && i==0) {
			parent_ul=t.parentNode;
			if (parent_ul.className.substr(parent_ul.className.length-4,4)=="over") {
				parent_ul.className=parent_ul.className.substr(0,parent_ul.className.length-5);
			}
			else {
				parent_ul.className=parent_ul.className+"_over";
			}
			//alert(parent_ul.className);
			if (p[i].className.substr(dlzka_c,4)=="over") { parent_ul.style.background='url("'+server+'/img/backg/menu_e_hlinik_over.gif") top right no-repeat'; }
			else { parent_ul.style.background='url("'+server+'/img/backg/menu_e.gif") top right no-repeat'; }
		}
	}
}*/
function HeadMenu(t) {
	t_li=t.className;
	pot=t.getElementsByTagName("a");
	if (t_li=="over") { t.className=""; pot[0].className=""; }
	else { t.className="over"; pot[0].className="over"; }
}

function TopMenu(t) {
	div01=t.childNodes;
	div02=div01[0].childNodes;
	
	div2=div02[0];
	div1=div01[0];
	
	od1=div1.className.length>5?div1.className.length-5:0;
	od2=div2.className.length>5?div2.className.length-5:0;
	
	if (div1.className.substr(od1,5)=="_over") { div1.className=div1.className.substr(0,od1); }
	else { div1.className=div1.className+"_over"; }
	if (div2.className.substr(od1,5)=="_over") { div2.className=div2.className.substr(0,od1); }
	else { div2.className=div2.className+"_over"; }
}

function LMenu(t) {
	if (t.className!="plocha") {
		od=t.className.length>5?t.className.length-5:0;
		if (t.className.substr(od,5)=="_over") { t.className=t.className.substr(0,od); }
		else if (t.className.substr(od,5)!="_over") { t.className=t.className+"_over"; }
	}
}

function QBVymaz(id_p) {
	ok=confirm(lang_qb_vymaz);
	if (ok) {
		location.href=server+"/qb_forum.php?f="+id_p+"&w="+location.href;
	}
}

function F_re(nazov,text) {
	document.getElementById('nazov').value="Re: "+nazov;
	document.getElementById('text').value="[c]"+text+"[/c]";
}

function getPS(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function ZobrazSpravu(styl,nadpis,text) {
	pole=getPS();
	SchovajObject(0);
	
	if (!document.getElementById("dialog")) {
		el=Builder.node('div',{id:'dialog'});
		kam=document.getElementsByTagName("body")[0];
		$(kam).appendChild(el);
	}if (!document.getElementById("send")) {
		el=Builder.node('div',{id:'send'});
		kam=document.getElementsByTagName("body")[0];
		$(kam).appendChild(el);
	}
	div=document.getElementById("dialog")
	div.innerHTML="";
	div.style.top=0;
	div.style.left=0;
	div.style.width=pole[0]+"px";
	div.style.height=pole[1]+"px";
	div.style.zIndex=100;
	div.style.background="#000000";
	div.style.display="block";
	
	new Effect.Opacity('dialog', { duration: 0.3, from: 0.0, to: 0.8 });
	
	newDiv=document.getElementById("send");
	newDiv.style.display="block";
	newDiv.innerHTML=text;
	//newDiv.setAttribute("id","dialog2");
	newDiv.setAttribute("class",styl);
	newDiv.setAttribute("className",styl);
	//newDiv.innerHTML="<h1>"+nadpis+"</h1>"+text;
	newDiv.style.zIndex=101;
	vyska=newDiv.offsetHeight
	sirka=newDiv.offsetWidth
	t=(pole[3]/2)-(vyska/2);
	l=(pole[2]/2)-(sirka/2);
	newDiv.style.top=t+"px";
	newDiv.style.left=l+"px";
	new Effect.Opacity('send', { duration: 0.5, from: 0.0, to: 1.0 });
		
}

function SchovajSpravu() {
	document.getElementById("dialog").style.display="none";
	document.getElementById("send").style.display="none";
	SchovajObject(1);
}

function SchovajObject(stav) {
	objekty=document.body.getElementsByTagName("object")
	if (objekty.length) {
		stav=stav==1?"visible":"hidden";
		for (i=0; i<objekty.length; i++) {
			objekty[i].style.visibility=stav;
		}
	}
}

function SkopirovatUdaje(typ) {
	if (typ=="f" || typ=="d") {
		pole=new Array("adresa","psc","mesto","stat");
		
		if ($("firma").value=="") {
			nazov=($("titul_pred").value!=""?$("titul_pred").value+" ":"")+$("meno").value+" "+$("priezvisko").value+($("titul_za").value!=""?", "+$("titul_za").value:"");
		}
		else {
			nazov=$("firma").value;
		}
		
		for (i=0; i<pole.length; i++) {
			$(typ+"_"+pole[i]).value=$(pole[i]).value;
		}
		$(typ+"_nazov").value=nazov;
	}
}

function ZobrazNezobraz(t) {
    pot=t.parentNode;
    tabulka=pot.select("div");
    
    if (tabulka[0].getStyle("display")=="none") {
        new Effect.SlideDown(tabulka[0])
    }
    else {
        new Effect.SlideUp(tabulka[0])
    }
}
