
function jobProfilSearch(){
	var actnv = 'jobProfilSearch';
	//draw progress img
	drawPageLoading('showSearch');
	elCount = document.getElementById('itemCount');
	if(elCount != null){
		//JQUERY BUG itemCount = document.getElementById('itemCount').value;
		
		var w = document.getElementById('itemCount').selectedIndex;
		var itemCount = document.getElementById('itemCount').options[w].text;
	}else{
		itemCount=0;
	}	

	setSearchTextValue();
	setArgKey();
	setUrgentJobs();

	$.ajax({
		data:	({	
					actn: actnv,
					shi: itemCount,							
					so: document.getElementById('so').value,
					s1: document.getElementById('s1').value,
					s2: document.getElementById('s2').value,
					s3: document.getElementById('s3').value,
					su: document.getElementById('s4').value,
					arkey: document.getElementById('s5').value,
					st: document.getElementById('s6').value,
					sp: document.getElementById('s7').value	,
					jobid: document.getElementById('dlid').value	
						
				}),	
		success:function(result){		
				setSearchContent(result,'');
				applyShadowbox();
				applyHighlighting();
				
				if($("#dlid").val() > 0){
					$('a.fancylink1').fancybox().trigger('click');		
					$("#dlid").val('');
					jobSearch();
				}
			}
	});	
}


// ajax script to get jobs
function jobSearch(){
	var actnv = 'jobSearch';
	//draw progress img
	drawPageLoading('showSearch');
	elCount = document.getElementById('itemCount');
	if(elCount != null){
		itemCount = document.getElementById('itemCount').value;
	}else{
		itemCount=0;
	}	
	
	setSearchTextValue();
	setArgKey();
	setUrgentJobs();

	$.ajax({
		data:	({	
					actn: actnv,
					shi: itemCount,							
					so: document.getElementById('so').value,
					s1: document.getElementById('s1').value,
					s2: document.getElementById('s2').value,
					s3: document.getElementById('s3').value,
					su: document.getElementById('s4').value,
					arkey: document.getElementById('s5').value,
					st: document.getElementById('s6').value,
					sp: document.getElementById('s7').value	,
					s8: document.getElementById('s8').value	,
					jobid: document.getElementById('dlid').value	
						
				}),	
		success:function(result){		
				setSearchContent(result,'');
				applyShadowbox();
				applyHighlighting();
				
				if($("#dlid").val() > 0){
					$('a.fancylink1').fancybox().trigger('click');		
					$("#dlid").val('');
					jobSearch();
				}
			}
	});						
}


function jobSearchCategory(){
	var actnv = 'jobSearch';

	//draw progress img
	drawPageLoading('showSearch');
	elCount = document.getElementById('itemCount');
	if(elCount != null){
		itemCount = document.getElementById('itemCount').value;
	}else{
		itemCount=0;
	}	

	$.ajax({
		data:	({	
					actn: actnv,
					shi: itemCount,							
					so: '',
					s1: '',
					s2: '',
					s3: document.getElementById('jobCat').value,
					su: '',
					arkey: '',
					st: '',
					sp: document.getElementById('s7').value	,
					s8: ''	,
					jobid: ''	
						
				}),	
		success:function(result){		
				setSearchContent(result,'');
				applyShadowbox();
				applyHighlighting();

			}
	});						
}




// load jobsearch only when 
function filterJobSearch(){
	if($("#flagfilterchanged").length >0 && $("#flagfilterchanged").val()==1){
		setPage('firstPage','');    	
		$("#flagfilterchanged").val(0);
		jobSearch();
		applyShadowbox();
	}
}

// getAndDraws unselected Filters in selectbox
function reloadDrawFilters(filter,val){
	var actnv = 'setReloadFilters';

	$.ajax({
//		url:	"../services/ajax_job_services.php",
		data:	({	
					actn: actnv,					
					val: document.getElementById(val).value,
					f: filter
					
				}),
		success:	function (result){
		
			//remove all existings filtersbefore fill
			childs = $("#"+filter+'_selectbox').children();		
			for(i=0; i < childs.length; i++){
				if(childs[i].tagName == 'dd'){
					$("#"+childs[i].id).remove();
				}
			}
			$("#"+filter+'_selectbox').html(result);
		}
 });
	applyShadowbox();
}



function removeDrawFilter(id,cont, mark,flag_reload){
	$("#"+id).remove();

	// create name from parent
	arr = id.split('_');
	name='';
	for(i=0;i< (arr.length-1);i++){
		if(name != ''){
			name = name +'_';
		}
		name = name + arr[i];
	}		

	// remove filter value
	removeFilterValue(cont,arr[(arr.length-1)]);
	
	// if children exists when not add default
	res = $("#"+name).children();	
	// add default when nothing set
	if(res.length == 0){	
		addDrawFilter(null,name,cont, mark, mark);
	}
	
	// reload filter select box
	reloadDrawFilters(name,cont);
	
	// search jobs
	setPage('prevPage','');
	jobSearch();
	applyShadowbox();
}
/*
//add filter value
function addFilterValue(name,value){

	if($("#"+name).val() == '' || $("#"+name).val() == undefined) {
		$("#"+name).val(value);
		return true;
	}

	val = $("#"+name).val();
	
	arr = val.split(',');	

	arr = addUniQueValToArr(arr,value);
	$("#"+name).val(arr.toString());
	applyShadowbox();
}
*/

// remove filter value
function removeFilterValue(name,value){

	if($("#"+name).val() == '')return true;

	val = $("#"+name).val();
	arr = val.split(',');
	arr = removeValueFromArr(arr, value);
	$("#"+name).val(arr.toString());
	applyShadowbox();
}


// sets found jobs, pages etc
function setShowJobMatches(name){
	var el = document.getElementById(name+'showJobMatches1');
	if(el == null)return;

	var mval = parseInt(document.getElementById(name+'maxPages').value);
	var ival = parseInt(document.getElementById(name+'itemCount').value);
	var pval = parseInt(document.getElementById('s7').value);
	var ifval = parseInt(document.getElementById(name+"foundJobMatches").innerHTML);

	if(ifval > 0){
		if(pval >1){
			var start = ((pval-1)*ival)+1;
		}else if(ival > 0 && ifval > 0){
			start = 1;
		}
		end = pval*ival;
		if(end > ifval) end = ifval;
		msg = start+'-'+end;
	}else{
		$("#"+name+"selpage").val("0");
		msg = "0";		
	}	

	$("#"+name+"showJobMatches1").html(msg);
//	el.innerHTML = start+'-'+end;		
	applyShadowbox();
}


// set page
function setPage(actn, addName){
	var pel = document.getElementById(addName+'selpage');
	if(pel == null) return;
	
	var pelv = document.getElementById(addName+'s7');
	if($("#"+addName+"maxPages").length > 0){
		var mval = document.getElementById(addName+"maxPages").value;
	}else{
		return ;
	}
	
	oldval = parseInt(pelv.value);
	
	// go to first page
	if(actn == 'firstPage'){
		pelv.value = 1;
		pel.value = pelv.value;
		return true;
	}

	// go to last page
	if(actn == 'lastPage'){			
		pelv.value = mval;
		pel.value= pelv.value;
		return true;
	}

	// go to next page
	if(actn == 'nextPage' && oldval < mval){
		pelv.value = (oldval +1);
		pel.value = pelv.value ;			
	}

	// go to prev page
	if(actn == 'prevPage' && oldval >1){
		pelv.value = (oldval -1);
		pel.value = pelv.value ;			
	}

	return true;
}

// argument for text search all words or one which has to match
function setArgKey(el){
	var el = document.getElementById("allkeywords");
    var val = document.getElementById("s5");
  
    if(val == null )return;
    
    var oldval = val.value;

	if(el.checked == true && el.value == 1){      	
		val.value='1';
	}else{
		val.value='0';
	}

}


// search for urgent jobs
function setUrgentJobs(){
	var el = document.getElementById("urgentjobs");
    var val = document.getElementById("s4");
    if(val == null )return;
    var oldval = val.value;
   
	if(el.checked == true){
    	
		val.value='on';
	}else{
		val.value='';
	}        	
}

// when page is loading start default search
function defaultPageSearch(){	 
	if($("#showSearch").html() == "" && $("#jid").val() == ''){				
		jobSearch();
	}
}


// display found jobs
function setSearchContent(content,name){
	//console.log(content);
    var arr = content.split('[|||||]');
  
    $("#"+name+"foundJobMatches").html(arr[0]);    
    document.getElementById(name+"maxPages").value = arr[1];

    $("#"+name+"foundPages1").html(arr[1]);   
    $("#"+name+"showSearch").html(arr[2]);

	setShowJobMatches(name);
}


// order jobs
function setOrder(el){
	setOrderDynamic(el, null);
}





