
function get_templates_for_category() {
	var category_select = gereg('emda.job-role_cz.c2d.asprecruiter.data.OpenPosition.category');
	var postype_select = gereg('emda.job-role_cz.c2d.asprecruiter.data.OpenPosition.positionType');
	if(category_select == null && postype_select == null)
		return;
	
	var cat_id = get_selecto(category_select);
		
	get_json();
	var templates = json.OpenPositionCategoryServer.get_templates_for_category(cat_id);
	
	set_selecto(postype_select, templates, 0, 1, null, true);
	on_changeo(postype_select);
}

var template_bubbles;

function show_template_texts() {
	var postype_select = gereg('emda.job-role_cz.c2d.asprecruiter.data.OpenPosition.positionType');
	if(postype_select == null)
		return;
	
	var temp_id = get_selecto(postype_select);
	if(temp_id == null || temp_id == '') {
		cancel_template_texts();
		return;
	}

	get_json();
	var tx = json.OpenPositionCategoryServer.get_template_texts(temp_id);
	if(tx == null)
		return;
		
	template_bubbles = new Array();
	var any_shown = false;
	
	for(var prop in tx) {
		if(prop == 'javaClass')
			continue;
			
		var val = tx[prop];
		var div_id = 'div_' + prop;
		if(val == null || val == '') {
			hide(div_id);
			continue;
		}
		
		//check if there's such a form control
		var ctrl = gereg('emda.job-role_cz.c2d.asprecruiter.data.OpenPosition.' + prop);
		if(ctrl == null)
			continue;
		
		//create new div if we don't have one yet
		var div = ge(div_id);
		if(div == null) {
			div = document.createElement('div');
			div.id = div_id;
			div.className = 'area-bubble';
			div.style.width = ctrl.clientWidth + 'px';
			ctrl.parentNode.appendChild(div);
			
			var a = document.createElement('a');
			a.className = 'copy-text-arrow';
			a.title = 'Copy text';
			a.href = 'javascript:copy_template_text("' + div_id + '", "' + ctrl.id + '")';
			div.appendChild(a);
		}
	
		set_valueo(div, val, null);
		template_bubbles[template_bubbles.length] = div;
		showo(div);
		any_shown = true;
	}
	
	var canc_id = postype_select.id + "_cancel";
	if(any_shown) {
		var div = ge(canc_id);
		if(div == null) {
			var div = document.createElement('div');
			div.style.position = 'relative';
			div.id = canc_id;
			postype_select.parentNode.appendChild(div);
			
			var a = document.createElement('a');
			a.className = 'cancel-bubbles';
			a.href = 'javascript:cancel_template_texts()';
			a.title = 'Hide template texts';
			div.appendChild(a);
		}
		showo(div);
	}
	else {
		var cdiv = ge(canc_id);
		if(cdiv != null)
			hideo(cdiv);
	}
}

function copy_template_text(src_id, dest_id) {
	var src = ge(src_id);
	if(!src)
		return;
	var val = src.firstChild.nodeValue;
	set_value(dest_id, val, null);
}

function cancel_template_texts() {
	if(template_bubbles == null)
		return;
	for(var div in template_bubbles)
		hideo(template_bubbles[div]);
	var postype_select = gereg('emda.job-role_cz.c2d.asprecruiter.data.OpenPosition.positionType');
	var cdiv = ge(postype_select.id + "_cancel");
	hideo(cdiv);
}

function domRollover() {
	if (navigator.userAgent.match(/Opera (\S+)/)) {
		var operaVersion = parseInt(navigator.userAgent.match(/Opera (\S+)/)[1]);
	}
	if (!document.getElementById||operaVersion <7) return;
	var imgarr=document.getElementsByTagName('img');
	var imgPreload=new Array();
	var imgSrc=new Array();
	var imgClass=new Array();
	for (i=0;i<imgarr.length;i++){
		if (imgarr[i].className.indexOf('domroll')!=-1){
			imgSrc[i]=imgarr[i].getAttribute('src');
			imgClass[i]=imgarr[i].className;
			imgPreload[i]=new Image();
			if (imgClass[i].match(/domroll (\S+)/)) {
				imgPreload[i].src = imgClass[i].match(/domroll (\S+)/)[1]
			}
			imgarr[i].setAttribute('xsrc', imgSrc[i]);
			imgarr[i].onmouseover=function(){
				this.setAttribute('src',this.className.match(/domroll (\S+)/)[1])
			}
			imgarr[i].onmouseout=function(){
				this.setAttribute('src',this.getAttribute('xsrc'))
			}
		}
	}
}

function toggle_leftbar() {
	var d = ge('leftbar-div');
	if(d.className == 'leftbar-off')
		d.className = 'leftbar-on';
	else
		d.className = 'leftbar-off';
}

function placement_type(ctrl) {
	var mw = geregx('maxWeeks');
	if(mw == null)
		return;
	
	if(typeof(ctrl) == 'string') {
		var cn = document.getElementsByName(ctrl)
		ctrl = null;
		for(var i = 0; i < cn.length; i++) {
			var n = cn.item(i);
			if(n.checked) {
				ctrl = n;
				break;
			}
		}
	}
	
	if(ctrl && ctrl.value && ctrl.value == 2) {
		mw.style.visibility = 'visible';
		mw.disabled = false;
		mw.parentNode.previousSibling.firstChild.style.visibility = 'visible';
	}
	else {
		mw.style.visibility = 'hidden';
		mw.disabled = true;
		mw.parentNode.previousSibling.firstChild.style.visibility = 'hidden';
	}
}


