var editor = null;
var img_dir = "";
var writer_addattach_index=0;
var writer_addattach_counter=0;
var obj = new Object();
function initEditor() {
/*
<!-- BEGIN block_htmlarea -->
  editor = new HTMLArea("{htmlarea_name}");
  editor.generate();
<!-- END block_htmlarea -->
*/
}
function getElementWithId(objectId) {
		return document.getElementById(objectId);
}
function dfrlsm(path){
	var pozice, pole_nazvu;
	pozice=path;
	pole_nazvu=path.split('\\');
	if (pole_nazvu.length>0) {
		for (var m=0;m<pole_nazvu.length;m++){
			pozice=pole_nazvu[m];
		}
	}
	return pozice;
}
function getObjStyl(objectId) {     //dmfrsl=getObjStyl
	return getElementWithId(objectId).style;
}
function zom(objectId, html) {
	obj = getElementWithId(objectId);
	if(obj) {
		obj.innerHTML = html;
		return true;
	} else {
		return false;
	}
}
function zoz(objectId, html) {
	obj = getElementWithId(objectId);
	if(obj) {
		obj.innerHTML += html;
		return true;
	} else {
		return false;
	}
}
function zop(objectId, html) {
	obj = getElementWithId(objectId);
	if(obj) {
		obj.innerHTML = html+obj.innerHTML;
		return true;
	} else {
		return false;
	}
}
function dmsrlf(objectId){
	zmenZobr(objectId, 'block');
}

function zmenZobr(objectId, newDisplay) { //dmfslr - zmena zobrazeni
	var styleObject = getObjStyl(objectId);
	if(styleObject) {
		if(newDisplay=='change'){
			var oldDisplay=styleObject.display;
			if(oldDisplay=='block') newDisplay='none';
			else                    newDisplay='block';
		}
		styleObject.display = newDisplay;
		return true;
	} else {
		return false;
	}
}
function dmflsr(objectId, newWidth) {
	var styleObject = getObjStyl(objectId);
	if(styleObject) {
		styleObject.width = newWidth;
		return true;
	} else {
		return false;
	}
}
var nactenProdukt = 0;
function stahniData(jazyk,produkt) {
	
	if (nactenProdukt!=produkt){
	nactenProdukt = produkt;
	zom('product_detail','Loading ....');
	// odmazeme stary skript, pokud existoval
	var hlavicka = document.getElementsByTagName('head')[0];
	var dataLoader = document.getElementById('scriptLoader');
	if (dataLoader) hlavicka.removeChild(dataLoader);

	// vytvorime novy element script
	var script = document.createElement('script');
	script.id = 'scriptLoader';
	script.type = 'text/javascript';
	script.src = './code.58.'+jazyk+'?id='+produkt+'&r='+Math.random();
//	alert(script.src);
	// POZOR, DULEZITE!! Skript musime vlozit do stranky
	// pomoci DOM - appendChild()
	// Ihned po vlozeni prohlizec stahne skript a spusti jej
	var x = document.getElementsByTagName('head')[0];
//	alert(x.appendChild(script));
	x.appendChild(script);
	}
	return false;
}
function stahniDataNew(jazyk,produkt) {
	
	if (nactenProdukt!=produkt){
	nactenProdukt = produkt;
	zom('product_detail','Loading ....');
	// odmazeme stary skript, pokud existoval
	var hlavicka = document.getElementsByTagName('head')[0];
	var dataLoader = document.getElementById('scriptLoader');
	if (dataLoader) hlavicka.removeChild(dataLoader);

	// vytvorime novy element script
	var script = document.createElement('script');
	script.id = 'scriptLoader';
	script.type = 'text/javascript';
	script.src = './code.59.'+jazyk+'?id='+produkt+'&r='+Math.random();
//	alert(script.src);
	// POZOR, DULEZITE!! Skript musime vlozit do stranky
	// pomoci DOM - appendChild()
	// Ihned po vlozeni prohlizec stahne skript a spusti jej
	var x = document.getElementsByTagName('head')[0];
//	alert(x.appendChild(script));
	x.appendChild(script);
	}
	return false;
}

function addZarazeni(){
//    zom("zarazeni",'Categories:');//dmlfsr = zom
	var v="";
	var nazev="";
	writer_addattach_counter++;
	writer_addattach_index++;
	var obj=getElementWithId("zarazeni");
	var zarrow = document.createElement("span");
	zarrow.setAttribute("id", "zarazeni"+writer_addattach_index);
	zarrow.style.display='block';
	obj.appendChild(zarrow);

	obj = getElementWithId("zarazeni"+writer_addattach_index);
	var table = document.createElement("table");
	table.setAttribute("id", "table"+writer_addattach_index);
	table.setAttribute("border", "0");
	table.style.display='block';
	obj.appendChild(table);

	obj=getElementWithId("table"+writer_addattach_index);
	var tr = document.createElement("tr");
	tr.setAttribute("id", "tr"+writer_addattach_index);
	obj.appendChild(tr);

	obj=getElementWithId("tr"+writer_addattach_index);
	var td1 = document.createElement("td");
	td1.setAttribute("id", "td1_"+writer_addattach_index);
	obj.appendChild(td1);
	var td2 = document.createElement("td");
	td2.setAttribute("id", "td2_"+writer_addattach_index);
	obj.appendChild(td2);
	var td3 = document.createElement("td");
	td3.setAttribute("id", "td3_"+writer_addattach_index);
	obj.appendChild(td3);
	var td4 = document.createElement("td");
	td4.setAttribute("id", "td4_"+writer_addattach_index);
	obj.appendChild(td4);

	obj=getElementWithId("zarazeni"+writer_addattach_index);
	stahniData('csBranche.getJSelect(   bran_sel'+writer_addattach_index+',8,-1,td1_'+writer_addattach_index+')');
	stahniData('csZuordnung.getJSelect( zuor_sel'+writer_addattach_index+',8,-1,td2_'+writer_addattach_index+')');
	stahniData('csSubbranche.getJSelect(subb_sel'+writer_addattach_index+',8,-1,td3_'+writer_addattach_index+')');
	stahniData('csProdukt.getJSelect(   prod_sel'+writer_addattach_index+',8,-1,td4_'+writer_addattach_index+')');
   var pridej = document.createElement("input");
		pridej.setAttribute("id", "add"+writer_addattach_index);
		pridej.setAttribute('type','button');
		pridej.setAttribute('value','add');
		pridej.setAttribute('onClick','zmen('+writer_addattach_index+')');
	obj.appendChild(pridej);
//    var pridejB = document.createElement("input");
//        pridejB.setAttribute("id", "add"+writer_addattach_index);
//        pridejB.setAttribute('type','button');
//        pridejB.setAttribute('value','add Branch');
//        pridej.setAttribute('onClick','zmen(zarazeni'+writer_addattach_index+'.value,'+writer_addattach_index+')');
//        pridejB.setAttribute('onClick','pridejBranche();');
//    obj.appendChild(pridejB);
}
function show_props(obj, obj_name) {
	var result = "";
	for (var i in obj)
	result += obj_name + "." + i + " = " + obj[i] + "\n <br>";
	return result;
}
function odeber(cislo){//smrfdl = odeber
  try{
	var v="";
	zom("zarazeni"+cislo,v);
	writer_addattach_counter--;
/*    var obj=getElementWithId("zarazeni");
		var row = getElementWithId("zarazeni"+cislo);
		obj.removeChild(row);
		row = getElementWithId("bran_sel"+cislo);
		obj.removeChild(row);
		row = getElementWithId("zuor_sel"+cislo);
		obj.removeChild(row);
		row = getElementWithId("subb_sel"+cislo);
		obj.removeChild(row);
		row = getElementWithId("prod_sel"+cislo);
		obj.removeChild(row);
*/
	if(writer_addattach_counter == 0)
		zom("zarazeni",'');
  }catch(e){
	alert(e);
  }
}

function zmen(cislo){
	try{
		//var v='aaaaaa<img src="'+img_dir+'att_icon.gif" width="10" height="13" alt="">'+dfrlsm(nazev);
		if ((getElementWithId('bran_sel'+cislo).value!='')&&
			(getElementWithId('zuor_sel'+cislo).value!='')&&
			(getElementWithId('subb_sel'+cislo).value!='')&&
			(getElementWithId('prod_sel'+cislo).value!='')){
				stahniData('csBranche.getJmeno('   +getElementWithId('bran_sel'+cislo).value+',zarazeni'+cislo+')');
				stahniData('csZuordnung.getJmeno(' +getElementWithId('zuor_sel'+cislo).value+',zarazeni'+cislo+')');
				stahniData('csSubbranche.getJmeno('+getElementWithId('subb_sel'+cislo).value+',zarazeni'+cislo+')');
				stahniData('csProdukt.getJmeno('   +getElementWithId('prod_sel'+cislo).value+',zarazeni'+cislo+')');
				var v='a<img src="'+img_dir+'fatcat.png" width="10" height="13" alt="">';
				zmenZobr('bran_sel'+cislo, 'none');
				zmenZobr('zuor_sel'+cislo, 'none');
				zmenZobr('subb_sel'+cislo, 'none');
				zmenZobr('prod_sel'+cislo, 'none');
				zmenZobr('table'+cislo, 'none');
				zmenZobr('add'+cislo, 'none');
			var branId = getElementWithId('bran_sel'+cislo).value
			var zuorId = getElementWithId('zuor_sel'+cislo).value
			var subbId = getElementWithId('subb_sel'+cislo).value;
			var prodId = getElementWithId('prod_sel'+cislo).value;
			v="";
			zom("zarazeni"+cislo,v);
			var hid = document.createElement("input");
				hid.setAttribute("id", "bran"+cislo);
				hid.setAttribute('type','hidden');
				hid.setAttribute('value',branId);
				hid.setAttribute('name','bran[]');
			obj=getElementWithId("zarazeni"+cislo);
			obj.appendChild(hid);
			 hid = document.createElement("input");
				hid.setAttribute("id", "zuor"+cislo);
				hid.setAttribute('type','hidden');
				hid.setAttribute('value',zuorId);
				hid.setAttribute('name','zuor[]');
			obj.appendChild(hid);
			 hid = document.createElement("input");
				hid.setAttribute("id", "subb"+cislo);
				hid.setAttribute('type','hidden');
				hid.setAttribute('value',subbId);
				hid.setAttribute('name','subb[]');
			obj.appendChild(hid);
			 hid = document.createElement("input");
				hid.setAttribute("id", "prod"+cislo);
				hid.setAttribute('type','hidden');
				hid.setAttribute('value',prodId);
				hid.setAttribute('name','prod[]');
			obj.appendChild(hid);
			var smaz = document.createElement("input");
				smaz.setAttribute("id", "del"+cislo);
				smaz.setAttribute('type','button');
				smaz.setAttribute('value','delete');
				smaz.setAttribute('onClick','odeber('+cislo+')');
		//      smaz.setAttribute('onClick','document.writeln(show_props(bran_sel,"obj")+"<br>")');
		//    var obj=getElementWithId("zarazeni"+cislo);
			obj.appendChild(smaz); //- pridat zpatky a opravit odebrani
		}else{
			alert('You must select all of categories!!!')
		}
	 }catch(e){
		alert(e);
	}
}

function help(){};

function pridejBranche(kam){
	var text = prompt('Set name of new branch:');
	if( text!=null){
		stahniData('csBranche.setNew("'+text+'")');
		stahniData("csBranche.getSelOptions("+kam+",-1,true)");
	}
}
function pridejZuordnung(kam){
	var text = prompt('Set name of new association:');
	if( text!=null){
		stahniData('csZuordnung.setNew("'+text+'")');
		stahniData("csZuordnung.getSelOptions("+kam+",-1,true)");
	}
}
function pridejSubbranche(kam){
	var text = prompt('Set name of new subbranch:');
	if( text!=null){
		stahniData('csSubbranche.setNew("'+text+'")');
		stahniData("csSubbranche.getSelOptions("+kam+",-1,-1,true)");
	}
}
function pridejProdukt(kam){
	var text = prompt('Set name of new product:');
	if( text!=null){
		stahniData('csProdukt.setNew("'+text+'")');
		stahniData("csProdukt.getSelOptions("+kam+",-1,-1,-1,true)");
	}
}