/* --------------------------------------------------------
	Initialization
-------------------------------------------------------- */

//window.onload = function () {
function Check(obj,CatSort,selectValue,oldCategory,nRootCategory) {
	var form = document.forms.count;
	var el = getE(obj);
	//el.onchange = function () {
	  //alert(getE(obj).options.selectedIndex);
		if (!this._value) this._value = "";
		if (this._value != this.value) {
			if (this._timer)
				window.clearTimeout(this._timer);	

			var el = this;
			this._timer = window.setTimeout(function () {
        if (nRootCategory == 375){
				  processKeysAlu(obj,CatSort,selectValue,oldCategory,nRootCategory)
				}else{
				  processKeys(obj,CatSort,selectValue,oldCategory,nRootCategory)
				}
      }, 500);
		}
		this._value = this.value;
	//}

}


function processKeys(el,link,selValue,oldValue,nRootCategory) {
	el._loading = true;

  if (el == "obdobi"){
      //if (getE('prumer').value == "0") {
       XmlHttp.get(comboPrumer, "/forms.php?zmena=obdobi&typ=prumer&selvalue="+getE('prumer').value+"&link=" +link+"&obdobi="+getE('obdobi').value+"&sirka="+getE('sirka').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory, el);
      // alert("/forms.php?zmena=obdobi&typ=prumer&selvalue="+getE('prumer').value+"&link=" +link+"&obdobi="+getE('obdobi').value+"&sirka="+getE('sirka').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory);
      //}
      //if (getE('profil').value == "0") {
       XmlHttp.get(comboProfil, "/forms.php?zmena=obdobi&typ=profil&selvalue="+getE('profil').value+"&link=" +link+"&prumer="+getE('prumer').value+"&sirka="+getE('sirka').value+"&obdobi="+getE('obdobi').value+"&rootcategory="+nRootCategory, el);
       //alert("/forms.php?zmena=obdobi&typ=profil&selvalue="+getE('profil').value+"&link=" +link+"&prumer="+getE('prumer').value+"&sirka="+getE('sirka').value+"&obdobi="+getE('obdobi').value+"&rootcategory="+nRootCategory);
      //}
      //if (getE('sirka').value == "0") {
       XmlHttp.get(comboSirka, "/forms.php?zmena=obdobi&typ=sirka&selvalue="+getE('sirka').value+"&link=" +link+"&prumer="+getE('prumer').value+"&obdobi="+getE('obdobi').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory, el);
       //alert("/forms.php?zmena=obdobi&typ=sirka&selvalue="+getE('sirka').value+"&link=" +link+"&prumer="+getE('prumer').value+"&obdobi="+getE('obdobi').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory);
      //}
  }
  if (el == "prumer"){
      //if (getE('obdobi').value == "0") {
       XmlHttp.get(comboObdobi, "/forms.php?zmena=prumer&typ=obdobi&selvalue="+getE('obdobi').value+"&link=" +link+"&prumer="+getE('prumer').value+"&sirka="+getE('sirka').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory, el);
       //alert("/forms.php?zmena=prumer&typ=obdobi&selvalue="+getE('obdobi').value+"&link=" +link+"&prumer="+getE('prumer').value+"&sirka="+getE('sirka').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory);
      //}
      //if (getE('profil').value == "0") {
        XmlHttp.get(comboProfil, "/forms.php?zmena=prumer&typ=profil&selvalue="+getE('profil').value+"&link=" +link+"&prumer="+getE('prumer').value+"&sirka="+getE('sirka').value+"&obdobi="+getE('obdobi').value+"&rootcategory="+nRootCategory, el);
       //alert("/forms.php?zmena=prumer&typ=profil&selvalue="+getE('profil').value+"&link=" +link+"&prumer="+getE('prumer').value+"&sirka="+getE('sirka').value+"&obdobi="+getE('obdobi').value+"&rootcategory="+nRootCategory);
      //}
      //if (getE('sirka').value == "0") {
       XmlHttp.get(comboSirka, "/forms.php?zmena=prumer&typ=sirka&selvalue="+getE('sirka').value+"&link=" +link+"&prumer="+getE('prumer').value+"&obdobi="+getE('obdobi').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory, el);
       //alert("/forms.php?zmena=prumer&typ=sirka&selvalue="+getE('sirka').value+"&link=" +link+"&prumer="+getE('prumer').value+"&obdobi="+getE('obdobi').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory);
      //}
  }
  if (el == "profil"){
      //if (getE('prumer').value == "0") {
       XmlHttp.get(comboPrumer, "/forms.php?zmena=profil&typ=prumer&selvalue="+getE('prumer').value+"&link=" +link+"&obdobi="+getE('obdobi').value+"&sirka="+getE('sirka').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory, el);
       //alert("/forms.php?zmena=profil&typ=prumer&selvalue="+getE('prumer').value+"&link=" +link+"&obdobi="+getE('obdobi').value+"&sirka="+getE('sirka').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory);
      //}
      //if (getE('obdobi').value == "0") {
       XmlHttp.get(comboObdobi, "/forms.php?zmena=profil&typ=obdobi&selvalue="+getE('obdobi').value+"&link=" +link+"&prumer="+getE('prumer').value+"&sirka="+getE('sirka').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory, el);
       //alert("/forms.php?zmena=profil&typ=obdobi&selvalue="+getE('obdobi').value+"&link=" +link+"&prumer="+getE('prumer').value+"&sirka="+getE('sirka').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory);
      //}
      //if (getE('sirka').value == "0") {
       XmlHttp.get(comboSirka, "/forms.php?zmena=profil&typ=sirka&selvalue="+getE('sirka').value+"&link=" +link+"&prumer="+getE('prumer').value+"&obdobi="+getE('obdobi').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory, el);
       //alert("/forms.php?zmena=profil&typ=sirka&selvalue="+getE('sirka').value+"&link=" +link+"&prumer="+getE('prumer').value+"&obdobi="+getE('obdobi').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory);
      //}
  }
  if (el == "sirka"){
      //if (getE('prumer').value == "0") {
       XmlHttp.get(comboPrumer, "/forms.php?zmena=sirka&typ=prumer&selvalue="+getE('prumer').value+"&link=" +link+"&obdobi="+getE('obdobi').value+"&sirka="+getE('sirka').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory, el);
       //alert("/forms.php?zmena=sirka&typ=prumer&selvalue="+getE('prumer').value+"&link=" +link+"&obdobi="+getE('obdobi').value+"&sirka="+getE('sirka').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory);
      //}
      //if (getE('profil').value == "0") {
       XmlHttp.get(comboProfil, "/forms.php?zmena=sirka&typ=profil&selvalue="+getE('profil').value+"&link=" +link+"&prumer="+getE('prumer').value+"&sirka="+getE('sirka').value+"&obdobi="+getE('obdobi').value+"&rootcategory="+nRootCategory, el);
       //alert("/forms.php?zmena=sirka&typ=profil&selvalue="+getE('profil').value+"&link=" +link+"&prumer="+getE('prumer').value+"&sirka="+getE('sirka').value+"&obdobi="+getE('obdobi').value+"&rootcategory="+nRootCategory);
      //}
      //if (getE('obdobi').value == "0") {
       XmlHttp.get(comboObdobi, "/forms.php?zmena=sirka&typ=obdobi&selvalue="+getE('obdobi').value+"&link=" +link+"&prumer="+getE('prumer').value+"&sirka="+getE('sirka').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory, el);
       //alert("/forms.php?zmena=sirka&typ=obdobi&selvalue="+getE('obdobi').value+"&link=" +link+"&prumer="+getE('prumer').value+"&sirka="+getE('sirka').value+"&profil="+getE('profil').value+"&rootcategory="+nRootCategory);
      //}
  }


}
function processKeysAlu(el,link,selValue,oldValue,nRootCategory) {
	el._loading = true;

  if (el == "prumer"){
       // alert("/forms.php?zmena=prumer&typ=obdobi&selvalue="+getE('obdobi').value+"&link=" +link+"&prumer="+getE('prumer').value+"&sirka="+getE('sirka').value+"&profil="+getE('profil').value);
      //if (getE('pcd').value == "0") {
       XmlHttp.get(comboPCD, "/forms.php?zmena=prumer&typ=pcd&selvalue="+getE('pcd').value+"&link=" +link+"&prumer="+getE('prumer').value+"&rootcategory="+nRootCategory, el);
      //alert("/forms.php?zmena=prumer&typ=pcd&selvalue="+getE('pcd').value+"&link=" +link+"&prumer="+getE('prumer').value+"&rootcategory="+nRootCategory);
      //}
  }
  if (el == "pcd"){
      //if (getE('prumer').value == "0") {
       XmlHttp.get(comboPrumer, "/forms.php?zmena=pcd&typ=prumer&selvalue="+getE('prumer').value+"&link=" +link+"&pcd="+getE('pcd').value+"&rootcategory="+nRootCategory, el);
       //alert("/forms.php?zmena=pcd&typ=prumer&selvalue="+getE('prumer').value+"&link=" +link+"&pcd="+getE('pcd').value+"&rootcategory="+nRootCategory);
      //}
  }


}

function setMode(e) {
	if (!e) e = window.event;
	var el = e.srcElement ? e.srcElement : e.target;
	el.onkeydown = function () {};

	var list = document.getElementById("suggest-list");
	var span = document.getElementById("counter");

	if (list)
		list.parentNode.removeChild(list);

	if (span)
		span.parentNode.removeChild(span);
}

/* --------------------------------------------------------
	Callbacks
-------------------------------------------------------- */

function comboPCD(res, el) {
	el._loading = false;
	if (res.status == 200) {
  	var names = res.responseXML.getElementsByTagName("n");
		var list = document.getElementById("pcd");
		var pcdOldValue = (getE('pcd').value);

		if (!list) {
			list = el.parentNode.appendChild(document.createElement("select"));
			list.id = "pcd";
		}
		
		var response = res.responseText;
		//eval(response);
		if (names.length > 0) {
  		getE('pcd').style.visibility = "visible";
      //getE('pcd').innerHTML = "";
			var name = null;
    	if (names.length > 0) {
         for(var i=(list.options.length-1);i>=0;i--){
            var o=list.options[i];
            list.options[i] = null;
         }
    	}
   		name = list.appendChild(document.createElement("option"));
   		name.value= 0;
   		name.text = "- - - - - - - - -";
			for (var i=0; i < names.length; i++) {
				name = list.appendChild(document.createElement("option"));
        //var sValueActual = names[i].firstChild.nodeValue.split('##');
        //alert(names[i].firstChild.nodeValue);
				name.value = names[i].firstChild.nodeValue;
				//name.text = sValueActual[1];
				name.text = names[i].firstChild.nodeValue;
//					names[i].replace(el.value, "<em>" + el.value + "</em>")
			}
		}
		//alert(pcdOldValue);
		getE('pcd').value = pcdOldValue;
		
//		else {
//		  alert('nic');
//			el.style.background = "pink";
//		}
	}
	else {
		return;
	}
}
function comboPrumer(res, el) {
	el._loading = false;
	if (res.status == 200) {
  	var names = res.responseXML.getElementsByTagName("n");
		var list = document.getElementById("prumer");
		var PrumerOldValue = (getE('prumer').value);

		if (!list) {
			list = el.parentNode.appendChild(document.createElement("select"));
			list.id = "prumer";
		}
		//alert(PrumerOldValue);
		var response = res.responseText;
		//eval(response);
		if (names.length > 0) {
  		getE('prumer').style.visibility = "visible";
      //getE('prumer').innerHTML = "";
			var name = null;
			//alert(names.length);
    	if (names.length > 0) {
    	//alert(list.options.length-1);
         for(var i=(list.options.length-1);i>=0;i--){
          
            var o=list.options[i];
            list.options[i] = null;
         }
    			//names[i].parentNode.removeChild(names[i])
    	}
   		name = list.appendChild(document.createElement("option"));
   		name.value= 0;
   		name.text = "- - - - - - - - -";
			for (var i=0; i < names.length; i++) {
				name = list.appendChild(document.createElement("option"));
        var text = names[i].firstChild.nodeValue;
        name.value = text;
				name.text  = text;
			}
		
		}
 		getE('prumer').value = PrumerOldValue;
		
//		else {
//		  alert('nic');
//			el.style.background = "pink";
//		}
	}
	else {
		return;
	}
}
function comboObdobi(res, el) {
	el._loading = false;
	if (res.status == 200) {
  	var names = res.responseXML.getElementsByTagName("n");
		var list = document.getElementById("obdobi");
		var pcdOldValue = (getE('obdobi').value);

		if (!list) {
			list = el.parentNode.appendChild(document.createElement("select"));
			list.id = "obdobi";
		}
		var response = res.responseText;
		//eval(response);
		if (names.length > 0) {
  		getE('obdobi').style.visibility = "visible";
      //getE('obdobi').innerHTML = "";
			var name = null;
    	if (names.length > 0) {
         for(var i=(list.options.length-1);i>=0;i--){
            var o=list.options[i];
            list.options[i] = null;
         }
    	}
   		name = list.appendChild(document.createElement("option"));
   		name.value= 0;
   		name.text = "- - - - - - - - -";
			for (var i=0; i < names.length; i++) {
				name = list.appendChild(document.createElement("option"));
        //var sValueActual = names[i].firstChild.nodeValue.split('##');
				name.value = names[i].firstChild.nodeValue;
				//name.text = sValueActual[1];
				name.text = names[i].firstChild.nodeValue;
//					names[i].replace(el.value, "<em>" + el.value + "</em>")
			}

		}
  	getE('obdobi').value = pcdOldValue;
//		else {
//		  alert('nic');
//			el.style.background = "pink";
//		}
	}
	else {
		return;
	}
}

function comboProfil(res, el) {
	el._loading = false;
	if (res.status == 200) {
  	var names = res.responseXML.getElementsByTagName("n");
		var list = document.getElementById("profil");
		var ProfilOldValue = (getE('profil').value);

		if (!list) {
			list = el.parentNode.appendChild(document.createElement("select"));
			list.id = "profil";
		}
		var response = res.responseText;
		//eval(response);
		if (names.length > 0) {
  		getE('profil').style.visibility = "visible";
      //getE('profil').innerHTML = "";
			var name = null;
    	if (names.length > 0) {
         for(var i=(list.options.length-1);i>=0;i--){
            var o=list.options[i];
            list.options[i] = null;
         }
    	}
   		name = list.appendChild(document.createElement("option"));
   		name.value= 0;
   		name.text = "- - - - - - - - -";
			for (var i=0; i < names.length; i++) {
				name = list.appendChild(document.createElement("option"));
        //var sValueActual = names[i].firstChild.nodeValue.split('##');
				//name.value = sValueActual[0];
				//name.text = sValueActual[1];
				name.value = names[i].firstChild.nodeValue;
				name.text = names[i].firstChild.nodeValue;
//					names[i].replace(el.value, "<em>" + el.value + "</em>")
			}
		}
		getE('profil').value = ProfilOldValue;
	}
	else {
		return;
	}
}

function comboSirka(res, el) {
	el._loading = false;
	if (res.status == 200) {
  	var names = res.responseXML.getElementsByTagName("n");
		var list = document.getElementById("sirka");
		var SirkaOldValue = getE('sirka').value;

		if (!list) {
			list = el.parentNode.appendChild(document.createElement("select"));
			list.id = "sirka";
		}
		var response = res.responseText;
		//eval(response);
		if (names.length > 0) {
  		getE('sirka').style.visibility = "visible";
      //getE('sirka').innerHTML = "";
			var name = null;
    	if (names.length > 0) {
         for(var i=(list.options.length-1);i>=0;i--){
            var o=list.options[i];
            list.options[i] = null;
         }
    	}
   		name = list.appendChild(document.createElement("option"));
   		name.value= 0;
   		name.text = "- - - - - - - - -";
			for (var i=0; i < names.length; i++) {
				name = list.appendChild(document.createElement("option"));
        //var sValueActual = names[i].firstChild.nodeValue.split('##');
				//name.value = sValueActual[0];
				//name.text = sValueActual[1];
				name.value = names[i].firstChild.nodeValue;
				name.text = names[i].firstChild.nodeValue;
//					names[i].replace(el.value, "<em>" + el.value + "</em>")
			}
//      alert(getE('sirka').value);
      if (SirkaOldValue > ''){
  		  getE('sirka').value = SirkaOldValue;
  		}
		}
		//alert(SirkaOldValue);
	}
	else {
		return;
	}
}


function count(res, el) {
	el.style.background = "";
	el._loading = false;

	if (res.status == 200) {
		var span = document.getElementById("counter");
		if (!span) {
			span = el.parentNode.appendChild(document.createElement("span"));
			span.id = "counter"
		}

		var count = parseInt(res.responseText);
		span.innerHTML = "(" + count + ")";
		
//		if (count == 0)
//			el.style.background = "pink";
	}
	else {
		return;
	}
}


function autoComplete(res, el) {
	el.style.background = "#FFD9A1";
	el._loading = false;
	if (res.status == 200) {
		var names = res.responseXML.getElementsByTagName("n");

  	var len = el.value.length;
		if (names.length > 0) {
			el.value = names[0].firstChild.nodeValue;
			selectText(el, len, el.value.length);
			
			if (names.length > 1) {
				var span = document.getElementById("counter");
				if (!span) {
					span = document.createElement("span");
					span.id = "counter";
					el.parentNode.appendChild(span);
				}
//				span.innerHTML = "1/" + names.length + " users.";
	
				el.onkeydown = function (e) {
					if (!this._current) this._current = 0; 
					if (!e) e = window.event;
					if (e.keyCode == 38 || e.keyCode == 40) {
						if (e.keyCode == 38 && this._current > 0)
							this._current--;
						else if (e.keyCode == 40 && this._current+1 < names.length)
							this._current++;
						this._value = names[this._current].firstChild.nodeValue;
						this.value = this._value;

						//span.innerHTML = (this._current+1) + "/" + names.length + " users.";
						selectText(this, len, this.value.length);
					}
				}
			}
		}
//		else {
//			el.style.background = "pink";
//		}
	}
	else {
		return;
	}
}


function suggest(res, el) {
//	el.style.background = "";
	el._loading = false;
	if (res.status == 200) {
  	var names = res.responseXML.getElementsByTagName("n");
//  	if (names.length > 0) {
//  		for (var i=(names.length-1); i>=0; i--)
//  			names[i].parentNode.removeChild(names[i])
//  	}
		var list = document.getElementById("suggest-list");
		if (!list) {
			list = el.parentNode.appendChild(document.createElement("ul"));
			list.id = "suggest-list";
		}
		var response = res.responseText;
		//eval(response);
		if (names.length > 0) {
  		getE('suggest-list').style.visibility = "visible";
      getE('suggest-list').innerHTML = "";
			var name = null;
//    	if (names.length > 0) {
//    		for (var i=(names.length-1); i>=0; i--)
//    			names[i].parentNode.removeChild(names[i])
//    	}
			for (var i=0; i < names.length; i++) {
				name = list.appendChild(document.createElement("li"));
				name.innerHTML = "<span onClick=\"Javascript:SetValue('" + names[i].firstChild.nodeValue + "')\";>" + names[i].firstChild.nodeValue + "</span>";
//					names[i].replace(el.value, "<em>" + el.value + "</em>")
			}
		}
//		else {
//		  alert('nic');
//			el.style.background = "pink";
//		}
	}
	else {
		return;
	}
}


function SetValue(sText){
  getE('words').value = sText ;
  getE('suggest-list').style.visibility = "hidden" ;
}
/* --------------------------------------------------------
	Santa's little helpers
-------------------------------------------------------- */

function selectText(el, start, end) {
	if (document.all) {
		var rng = el.createTextRange();
		rng.moveStart("character", start);
		rng.moveEnd("character", end);
		rng.select();
	}
	else {
		el.setSelectionRange(start, end);
	}
	el.focus();
}

function getCheckedElement(elements) {
	for (var i=0; i < elements.length ; i++) {
		if (elements[i].checked)
			return elements[i];
	}
}

function getFormElements(form) {
	var elements = form.elements;

	var arr = new Array();
	for (var i=0; i < elements.length; i++) {
		if (elements[i].name)
			arr.push(elements[i].name + "=" + elements[i].value);
	}

	return arr;
}
