//============================================
// Easy 2.0                 
// Copyright     : .exe Evolution
// Wersja        : 2.0     
// Uwagi         : Brak 
//============================================

function selClick (idObj) {
	if (getObj(idObj).style.display == 'block') {
		getObj(idObj).style.display = 'none';
		document.alt = '';
	} 
	else {
		getObj(idObj).style.display = 'block';
		var t = setTimeout ("document.alt = '" + idObj + "';", 100);
	}
	return false;	
}

function selOut (idObj) {
	if (getObj(idObj + '_select_list').alt != 1) 
		getObj(idObj + '_select_list').style.display = 'none';
}

function prepareOptions (opt) {
	var prep = new Array();
	for (var id in opt) {
		if (catParents[opt[id]] != 0 && typeof catParents[opt[id]] != 'undefined') {
			var flag = true;
			for (var x in prep) {
				if (prep[x] == catParents[opt[id]])
					flag = false;
			}
			if (flag == true)
				prep[prep.length] = catParents[opt[id]];
			prep[prep.length] = opt[id];
		}
		else {
			var flag = true;
			for (var x in prep) {
				if (prep[x] == opt[id])
					flag = false;
			}
			if (flag == true)
				prep[prep.length] = opt[id];
		}
	}
	return prep;
}

function prepareManOptions (idObj) {
	var prep = new Array();
	for (var x in fstOpt [idObj]) {
		prep[prep.length] = fstOpt[idObj][x];
	}
	for (var id in catChilds[idObj]) {
		for (var x in fstOpt[catChilds[idObj][id]]) {
			var flag = true;
			for (var y in prep) {
				if (prep[y] == fstOpt[catChilds[idObj][id]][x])
					flag = false;
			}
			if (flag == true)
				prep[prep.length] = fstOpt[catChilds[idObj][id]][x];
		}
	}
	return prep;	
}

function changeOptions(obje) {
	obj = getObj (obje);
	//alert(obje + ' - ' + obj.value);
	isOpera91 = false;
	if (navigator.appName.indexOf('Opera') != -1 && parseFloat(navigator.appVersion) >= 9.1)
		isOpera91 = true;
	var tmp_div = document.createElement('div');
	tmp_div.style.display = 'none';

	if (obje == 'manu') {
		var catVal = 0;
		var tmpCatVal = getObj('categ').value;
		for (var i = getObj('categ').options.length; i >= 0; i--)
			getObj('categ').options[i] = null;
		getObj('categ').options[getObj('categ').options.length] = new Option (' kraj oraz region ', 0);
		if (obj.value == 0) {
			if (typeof allO == 'undefined') 
				var allO = prepareOptions(allCat);
			for (var id in allO) {
				if (tmpCatVal == allO[id])
					catVal = allO[id];
				var len = getObj('categ').options.length;
				tmp_div.innerHTML = allOpt[allO[id]];
				getObj('categ').options[len] = new Option (tmp_div.innerHTML, allO[id]);
				if (!isOpera91)
					getObj('categ').options[len].innerHTML = '<option class="inputfield" style="border:0;" value="'+ allO[id] +'" >'+ allOpt[allO[id]] +'</option>';		
			}
		}
		else {
			var secOptPrep = prepareOptions(secOpt[obj.value]);
			for (var id in secOptPrep) {
				if (tmpCatVal == secOptPrep[id])
					catVal = secOptPrep[id];
				var len = getObj('categ').options.length;
				tmp_div.innerHTML = allOpt[secOptPrep[id]];
				getObj('categ').options[len] = new Option (tmp_div.innerHTML, secOptPrep[id]);
				if (!isOpera91)
					getObj('categ').options[len].innerHTML = '<option class="inputfield" style="border: 0;" value="'+ secOptPrep[id] +'" >'+ allOpt[secOptPrep[id]] +'</option>';
			}
		}
		getObj('categ').value = catVal;
		//alert(catVal);
		activeDivSelector ('categ', 1, 1);
	}
	
	if (obje == 'categ') {
		var manVal = 0;
		var tmpManVal = getObj('manu').value
		for (var i = getObj('manu').options.length; i >= 0; i--)
			getObj('manu').options[i] = null;
		getObj('manu').options[getObj('manu').options.length] = new Option (' producent ', 0);
		if (obj.value == 0) {
			for (var id in allMan) {
				if (tmpManVal == allMan[id])
					manVal = allMan[id];
				var len = getObj('manu').options.length;
				//tmp_div.innerHTML = allManOpt[allMan[id]];
				getObj('manu').options[len] = new Option (allManOpt[allMan[id]], allMan[id]);
				if (!isOpera91)
					getObj('manu').options[len].innerHTML = '<option class="inputfield" style="border:0;" value="'+ allMan[id] +'" >'+ allManOpt[allMan[id]] +'</option>';		
			}
		}
		else {
			var secOptPrep = prepareManOptions(obj.value);
			for (var id in secOptPrep) {
				if (tmpManVal == secOptPrep[id])
					manVal = secOptPrep[id];
				if (typeof secOptPrep[id] == 'undefined')
					continue;
				var len = getObj('manu').options.length;
				getObj('manu').options[len] = new Option (allManOpt[secOptPrep[id]], secOptPrep[id]);
				if (!isOpera91)
					getObj('manu').options[len].innerHTML = '<option class="inputfield" style="border: 0;" value="'+ secOptPrep[id] +'" >'+ allManOpt[secOptPrep[id]] +'</option>';
			}
		}			
		getObj('manu').value = manVal;
		//alert(manVal);
		activeDivSelector ('manu', 1, 1);
	}
}

document.onclick = function () { if (document.alt != '' && document.alt != null) { getObj(document.alt).style.display = 'none'; document.alt = ''; } }

function activeDivSelector(idObj, on_change, update) {
	if(obj = getObj(idObj)) {
		counter = obj.options.length;
		if (update == null) {
			divObj = document.createElement('DIV');
			divObj.id = idObj + '_selector';
			divObj.className = 'div_selector';
		}
		else
			divObj = getObj (idObj + '_selector');
		divObj.innerHTML = '<a class="dsLink" href="javascript:;" onclick="selClick(\'' + idObj + '_select_list\');"><div class="div_selector_seltxt"></div></a>';
		//divObj.onmouseover = function () { document.alt = ''; }
		//divObj.onmouseout = function () { document.alt = idObj; }

		if (update == null) {
			selectObj = document.createElement('DIV');
			selectObj.id = idObj + '_select_list';
		}
		else {
			selectObj = getObj(idObj + '_select_list');
			selectObj.innerHTML = '';
		}
		selectObj.className = 'div_selector_list';
		//selectObj.onmouseover = function () { document.alt = ''; }
		//selectObj.onmouseout = function () { document.alt = idObj; }
			
		if(counter > 11) {
			selectObj.style.height = '200px';
			selectObj.style.overflow = 'auto';
		}
		else {
			selectObj.style.height = '';
			selectObj.style.overflow = '';
		}
		for(i = 0; i<counter; i++) {
			if(obj.options[i].selected) {
				divObj.getElementsByTagName('DIV')[0].innerHTML = obj.options[i].innerHTML;
			}
			selectObj.innerHTML = selectObj.innerHTML + '<a class="dslLink" id="'+ idObj +'_'+ obj.options[i].value +'" onmousedown="getObj(\'' + idObj + '\').value=\'' + obj.options[i].value + '\'; ' + (on_change != null ? 'changeOptions(\''+ idObj +'\');' : ' ') + ' getObj(\'' + idObj + '_selector\').getElementsByTagName(\'DIV\')[0].innerHTML = this.innerHTML; " href="javascript:;"> ' + obj.options[i].innerHTML + ' </a>';
		}
		
		if (update == null) {
			obj.offsetParent.appendChild(divObj);
			obj.offsetParent.appendChild(selectObj);
			obj.style.display = 'none';
		}

			
	}
}
