/* (c) 2008 Lukas Jelinek */


function limitSize(field, size) {
	if (field.value.length >= size) {
		field.value = field.value.substring(0, size - 1);
	}
}

function acceptOnlyNumbers(evnt) {
	var numbers='1234567890' + String.fromCharCode(8) + String.fromCharCode(0);  //cisla + aby fungovaly sipky aj. a backspace
	//alert (evnt.keyCode);
	var k = document.all ? parseInt (evnt.keyCode) : parseInt (evnt.which);
	return (numbers.indexOf(String.fromCharCode(k))!=-1);
}

/*
 * Vyhleda hodnotu v selectu
 */
function searchValue(obj_select, search_value) {
	var result = false;
	search_value_res = search_value.split("_", 1); //zde jde o to, sklada-li se hodnota ze dvou casti (kroku)

	for (var i = 0; i < obj_select.length; i++) {
		sel_value = obj_select.options[i].value;
		sel_value_res = sel_value.split("_", 1); //zde taky

		if (sel_value_res[0] == search_value_res[0]) {
			result = true;
			break;
		}
	}

	return result;
}

/*
 * Kopiruje vybranou volbu (option) do target selectu
 */
function copySelectedValue (source, target, value_prefix, text_prefix) {
	var optSource = source.options[source.selectedIndex];
	var result = false;

	if (optSource && !searchValue(target, value_prefix + optSource.value)) {
		var opt = new Option();
		opt.value = value_prefix + optSource.value;
		opt.text = text_prefix + optSource.text;
		try {
			target.add(opt, null);
		}
		catch(ex) {
			target.add(opt, target.selectedIndex);
		}
		result = true;
	}

	return result;
}

/*
 * Kopiruje option pod danym indexem do target selectu
 */
function copyValue (source, target, position) {
	var optSource = source.options[position];
	if (optSource && !searchValue(target, optSource.value)) {
			var opt = new Option();
			opt.value = optSource.value;
			opt.text = optSource.text;

			try {
				target.add(opt, null);
			}
			catch(ex) {
				target.add(opt, (target.length-1));
			}
	}
}

/*
 * vybranou option prenese do target selectboxu, v pripade skupiny okopiruje cely jeji obsah
 */
function selector(source_name, target_name) {
	var source = document.getElementById(source_name);
	var target = document.getElementById(target_name);

	var result = false;

	if (source.selectedIndex >= 0) {
		opt_value = source.options[source.selectedIndex].value.split("_", 1);


		if (opt_value=="head") {
			i = source.selectedIndex + 1;

			while (i < source.length && (source.options[i].value.split("_", 1) != "head")) {
				copyValue (source, target, i);
				//alert(i);
				i++;
			}
		}
		else {
			result = copySelectedValue(source, target, "", "");
		}
	}
	if (source.selectedIndex >= 0)
		source.options[source.selectedIndex].selected = false;
	if (target.selectedIndex >= 0)
		target.options[target.selectedIndex].selected = false;

	return result;
}

/*
 * vytvoreni noveho option z kombinace ze dvou vybranych voleb selectboxu
 */
function duoSelector(source1_name, source2_name, target_name) {
	var source1 = document.getElementById(source1_name);
	var source2 = document.getElementById(source2_name);
	var target = document.getElementById(target_name);
	var result = false;

	if(source1.selectedIndex >= 0 && source2.selectedIndex >= 0) {
		opt1_value = source1.options[source1.selectedIndex].value;
		opt1_text = source1.options[source1.selectedIndex].text;

		result = copySelectedValue(source2, target, opt1_value + "_", opt1_text + " - ");
	}

	if (source1.selectedIndex >= 0)
		source1.options[source1.selectedIndex].selected = false;
	if (source2.selectedIndex >= 0)
		source2.options[source2.selectedIndex].selected = false;
	if (target.selectedIndex >= 0)
		target.options[target.selectedIndex].selected = false;

	return result;
}

/*
 * odstrani zvolenou hodnotu ze selectboxu
 */
function deselector (select_name) {
	var obj_select = document.getElementById(select_name);

	if (obj_select.selectedIndex >= 0) {
		obj_select.remove(obj_select.selectedIndex);
	}

}

/*
 * nastavi checked danemu radioboxu. nenastavi, je-li ignore_value vybran.
 */
function setRadioCheckedValue(radio_name, new_value, ignore_value) {
	var radio = document.forms['form'].elements[radio_name];
	var result = false;

	var radio_length = radio.length;
	if(radio_length == undefined) {
		radio.checked = (radio.value == new_value.toString());
		return true; //!!! RETURN
	}

	if (ignore_value != "") {
		for(var i = 0; i < radio_length; i++) {
			//je-li checkla hodnota ignore_value, prerusi se cykl
			if (radio[i].value == ignore_value && radio[i].checked == true) {
				return false;
			}
		}
	}

	for(var i = 0; i < radio_length; i++) {
		radio[i].checked = false;
		if(radio[i].value == new_value.toString()) {
			radio[i].checked = true;
			result = true;
		}
	}

	return result;
}

/*
 * zjisti hodnotu vyberaneho radio.
 */
function getRadioCheckedValue(radio_name) {
	var radio = document.forms['form'].elements[radio_name];
	var result = false;

	var radio_length = radio.length;
	if(radio_length == undefined) {
		return false; //!!! RETURN
	}
	for(var i = 0; i < radio_length; i++) {
		if(radio[i].checked) {
			result = radio[i].value;
		}
	}
	return result;
}

/*
 * hodnoty vsech voleb selectboxu ulozi jako retezec hodnot oddelenych carkou do zvolene target.value
 */
function implodeSelectValues (select_name, dest_name) {
	var obj_select = document.getElementById(select_name);
	var obj_dest =  document.getElementById(dest_name);
	var arr = new Array();
	var str = "";

	for (i = 0; i < obj_select.length; i++) {
		arr[i] = obj_select[i].value;
	}
	str = arr.join(',');
	obj_dest.value = str;
}

/* funkce pro pridavani a odebirani sloupcu s praxi, predpokladaji existenci elementu (divu) experiences */
var global_experience_rows_count = 1;
function addNewExperienceRow(years, description) {
	global_experience_rows_count++;
	var current_experience_row = global_experience_rows_count;
	//rok
	place = document.getElementById('experiences');
	element = document.createElement("input");
	element = place.appendChild(element);
	element.name = "years_of_experience[" + current_experience_row + "]";
	element.id = "years_of_experience_" + current_experience_row;
	element.type = "text";
	element.value = years;
	element.size = 3;

	//popis
	element = document.createElement("input");
	element = place.appendChild(element);
	element.name = "years_of_experience_description[" + current_experience_row + "]";
	element.id = "years_of_experience_description_" + current_experience_row;
	element.type = "text";
	element.value = description;
	element.size = 80;

	//button
	//v tomto je tak neuveritelny bordel, proc proboha jeden browser vytvori submit a druhy button?
	element = document.createElement("button");
	element = place.appendChild(element);
	element.name = "years_of_experience_button[" + current_experience_row + "]";
	element.id = "years_of_experience_button_" + current_experience_row;
	text = document.createTextNode("Odebrat");
	element.setAttribute("type", "button")
	element.appendChild(text);
	element.onclick = function (){
			new Function(removeExperienceRow(current_experience_row));
			}
	element.value = "Odebrat";
	element.style.width = "100px";
	element.style.height = "19px";

	//br
	element=document.createElement('br');
	element = place.appendChild(element);
	element.id = "years_of_experience_br_" + current_experience_row;
}

function removeExperienceRow(row) {
	place = document.getElementById("years_of_experience_button_" + row);
	place.parentNode.removeChild(place);

	place = document.getElementById("years_of_experience_description_" + row);
	place.parentNode.removeChild(place);

	place = document.getElementById("years_of_experience_" + row);
	place.parentNode.removeChild(place);

	place = document.getElementById("years_of_experience_br_" + row);
	place.parentNode.removeChild(place);
}

function searchSelect(searched_text, select_name, next_result, key_pressed){
	var obj_select =  document.getElementById(select_name);
	var cnt;
	var regEx = new RegExp(searched_text, 'i');
	var found = false;
	//alert(key_pressed);
	if (key_pressed == 13)
		next_result = true;
	if (next_result == true)
		if (obj_select.selectedIndex >= 0 && obj_select.selectedIndex + 1 != obj_select.length)
			start_index = obj_select.selectedIndex + 1 ;
		else
			start_index = 0;

	else
		start_index = 0;

	for (cnt = start_index; cnt < obj_select.length; cnt++){
		if (obj_select[cnt].text.match(regEx)){
			obj_select.selectedIndex = cnt;
			found = true;
			break;
		}
	}
	return found;
}

