﻿var xml;
function spocti()
{
	if (xml==null)
	{
		xml=document.getElementById("data").XMLDocument;
		if (xml==null)
		{
			xml=document.implementation.createDocument("","",null);
			xml.async=false;
			xml.load(document.getElementById("data").attributes["src"].value);
		}
	}

	var xpath_base="/data/parms/r[@p1='"+document.getElementById("s1").value+"' and @p2='"+document.getElementById("s2").value+"' and @p3='"+document.getElementById("s3").value+"' and @p4='"+document.getElementById("s4").value+"' and @p5='"+document.getElementById("s5").value+"' and @p6='"+document.getElementById("s6").value+"']/@v";
	for (var a=1;a<14;a++)
	{
		var v=SelectSingleNode(xml,xpath_base+a);
		if (v==null)
			v="";
		document.getElementById("v"+a).innerHTML=v;
	}
	
	var v13=parseFloat(SelectSingleNode(xml,xpath_base+"13"));
	document.getElementById("r1").innerHTML=parseFloat(SelectSingleNode(xml,xpath_base+"11"))-v13;
	document.getElementById("r2").innerHTML=parseFloat(SelectSingleNode(xml,xpath_base+"12"))-v13;
	
	var v4=SelectSingleNode(xml,xpath_base+"4");
	document.getElementById("c1").innerHTML=SelectSingleNode(xml,"/data/prices/r[@t='"+v4+"']/@c1");
	var c2=SelectSingleNode(xml,"/data/prices/r[@t='"+v4+"']/@c2");
	document.getElementById("c2").innerHTML=c2;

	var el_kalk_castka=document.getElementById("ctl19_cilovaCastka");
	if (el_kalk_castka)
		el_kalk_castka.value=c2;
}

function SelectSingleNode(xmlDoc,elementPath)
{
	if (window.ActiveXObject)
	{
		var v=xmlDoc.selectSingleNode(elementPath);
		return v==null ? v : v.value;
	}
	else
	{
		var xpe = new XPathEvaluator();
		var results = xpe.evaluate(elementPath,xmlDoc,xpe.createNSResolver( xmlDoc),XPathResult.STRING_TYPE, null);
		return results.stringValue;
	}
}

function dosad_s1()
{
	var kw=parseFloat(document.getElementById("s1_vypocet_p").value)*2.55*(document.getElementById("s1_vypocet_n").checked ? 0.02 : 0.03);
	kw=Math.ceil(kw);
	var ddl=document.getElementById("s1");
	var o_length=ddl.options.length;
	if (o_length==0)
		return;
	if (kw<=4)
		ddl.selectedIndex=0;
	else if (kw>22)
		alert("Ztrátový výkon "+kw+" KW nutno řešit individuálně");
	else
	{
		kw=kw/2-1;
		for (var a=1;a<o_length;a++)
			if ((kw>parseFloat(ddl.options[a-1].value))&&(kw<=parseFloat(ddl.options[a].value)))
				{
					ddl.selectedIndex=a;
					break;
				}
	}
	s1_change();
}

function s1_change()
{
	var ddl=document.getElementById("s1");
	document.getElementById("s1_vypocet").style.display=ddl.selectedIndex==ddl.options.length-1 ? "block" : "none";
}

