/*******************************************
  DIV-be írás
*******************************************/
function writeToDiv(divId,data) {
	var obj = document.getElementById(divId);
	obj.innerHTML = data;
	return false;
}

/*******************************************
  DIV elrejtése
*******************************************/
function hideDiv(divid) {
	$(divid).style.display = "none";
}

/*******************************************
  DIV megjelenítése
*******************************************/
function showDiv(divId, mode) {
	var obj = $(divId);
	obj.style.display = mode;
}

/*******************************************
  AJAX hívás
*******************************************/
function load_ajax(div, page, params, showProgress) {
	try {
		$(div).innerHTML = $('ajax_indicator').innerHTML;
	} catch (e) {}
	
	url = 'load_ajax.php';
	
	params2 = '_page='+page+'&'+params;

	var myAjax = new Ajax.Updater(
		div,
		url, {
			method: 'post',
			parameters: params2,
			evalScripts: false,
			onFailure: ajaxError,
			onSuccess: ajaxSuccess
		}
	);
	function ajaxError(xhr) {
		alert('Az oldal átmenetileg nem érhető el!');
	}
	function ajaxSuccess(xhr) {
		$(div).style.display = "block";
		res = xhr.responseText;
		//ha a visszaadott érték eleje '<!--%reload%-->', akkor frissítjük az oldalt
		if ( res.substring(0,15)=="<!--%reload%-->" ) {
			document.location.reload();
		} else
		//ha a visszaadott érték eleje '<!--%location%-->', akkor az utánna lévő címre ugrunk
		if ( res.substring(0,17)=="<!--%location%-->" ) {
			loc = res.substring(21);
			loc = loc.substring(0, loc.indexOf('-->'));
			document.location.href = loc;
		} else
		//ha a visszaadott érték eleje '<!--%alert%-->', akkor az utánna lévő szöveget kiírjuk
		if ( res.substring(0,14)=="<!--%alert%-->" ) {
			loc = res.substring(18);
			loc = loc.substring(0, loc.indexOf('-->'));
			alert(loc);
		}
	}
	
	return false;
}


function send_form(senderForm, div, page, params, showProgress) {
	load_ajax(div, page, Form.serialize(senderForm) + "&" + params, showProgress);
	return false;
}

// másolás 2 select küzütt
function copySelect(from, to, index) {
  to.options[to.options.length] = new Option(from.options[index].text,from.options[index].value);
  from.options[index] = null;
}


function checkselect_item_html(name, item, checked, index) {
	html = "";
	if (checked == 1) {
		checked_html = " checked='checked'";
	} else {
		checked_html = "";
	}
	html += "  <div style='cursor:pointer;cursor:hand;' id='"+name+"_check_"+index+"' onclick=\"$('"+name+"').selectedIndex="+index+"; for(i=0;i<$('"+name+"').options.length;i++) $('"+name+"_check_'+i).style.background='#ffffff'; this.style.background='#316AC5'\" >";
	html += "    <input type='checkbox'"+checked_html+" onclick=\"opt=document.getElementById('"+name+"').options["+index+"]; opt.value=opt.value.substr(0,opt.value.length-1)+(1-opt.value.substr(opt.value.length-1,1));\" />"+item;
	html += "  </div>";
	return html;
}

function checkselect_add(name, item, value, checked) {
  sel = $(name);
  checkdiv = $(name+'_checks');
	index = sel.options.length;
	sel.options[index] = new Option(item, value+'|'+checked);
	checkdiv.innerHTML += checkselect_item_html(name, item, checked, index);
}

function checkselect_del(name) {
  sel = $(name);
  checkdiv = $(name+'_checks');
	item_count = sel.options.length;
	index = sel.selectedIndex;
	
	html = "";
	j = 0;
	for (i=0; i<item_count; i++) {
		if (i != index) {
			checked = sel.options[i].value.substr(sel.options[i].value.length-1,1);
			html += checkselect_item_html(name, sel.options[i].text, checked, j);
			j++;
		}
	}
	
	sel.options[index] = null;
	
	checkdiv.innerHTML = html;
}