

var timed=0;
var live=0;
$(document).ready(function(){
  	$(".searchFilter li a").bind("click",function() {
		$(this).toggleClass("iconHit");


		var obj = $(this).attr('rel');

		/*var str1="document.form2."+obj+".checked=";

		eval("document.form2."+obj+".checked")?eval(str1+"false"):eval(str1+"true");*/

		$("#form2 #"+obj).attr("checked",$(this).hasClass("iconHit"));
	

		advanced();
		return false;
	});

 });

$(document).ready(function(){
	/* comment this section out to simulate JS off mode */

	$('#jsOn').show();
	$('#jsOff').hide();
	$('#advSearchHolder').hide();
	$('#search').keyup(fireup); 


});



function show_advanced(page_number,sr){
	if(sr==undefined) sr=0;
	
	
	$('#liveSearchHolder').hide();
	$('#advSearchHolder').show();

	/* create a list of job types for the form */
	var listHTML;
	listHTML = "<label for='jobtype'>Job Type</label><br />";
   	listHTML +="<select name='jobtype[]' size='4' multiple='multiple' class='listField' id='jobtype' onchange='setliveicons()'>";

     	listHTML +="<option value='3'";
	if(document.form2.icon_3.checked) listHTML += " selected";
	listHTML +=">Freelance</option>";
     	listHTML +="<option value='2'";
	if(document.form2.icon_2.checked) listHTML += " selected";
	listHTML +=">Permanent (PT)</option>";
     	listHTML +="<option value='1'";
	if(document.form2.icon_1.checked) listHTML += " selected";
	listHTML +=">Permanent (FT)</option>";

   	listHTML +="</select>";
	$('.advSearchType').html(listHTML);

	
	
	live=1;
	advanced(page_number);
}
function show_simple(page_number,sr){
		if(sr==undefined) sr=0;
		

		$('#liveSearchHolder').show();
		$('#advSearchHolder').hide();



/*		sel = document.form2.jobtype;

		/* 'document.form2.jobtype' is the drop down list on form2 - not the icons*/
/*		for(f=0; f<sel.length; f++){
			g=sel.options[f].value;
			if(sel.options[f].selected){
				$('#icon_'+g+' a').addClass("iconHit");
				eval("document.form2.icon_"+g+".checked=true");
			}else{
				$('#icon_'+g+' a').removeClass("iconHit");
				eval("document.form2.icon_"+g+".checked=false");
			}
		
		}
*/
		seticons;
	
	live=0;

	advanced(page_number,sr);
 
}


function fireup(){
	clearTimeout(timed);
	
	timed=setTimeout("advanced(0)",500);
}

function advanced(page_number,sr){
		if(sr==undefined) sr = 0;

		live==0?document.form2.keywords.value=document.form2.search.value:document.form2.search.value=document.form2.keywords.value;

		$('#loading').show();
		var formdata = $('#form2').serialize();	
		formdata += "&live="+live+"&sr="+sr;

		$.post('includes/get_jobs_ajax.inc.php?start_record='+page_number,formdata,searchResults);
		return false;
	}

function searchResults(data, status){

		var idx = data.indexOf(':');
		current_page = data.substring(0,idx);
		if(current_page=='undefined') current_page=0;
		++idx;

		data = data.substring(idx,data.length);
		var idx = data.indexOf(':');
		rec_cnt = data.substring(0,idx);
		$('.searchHeadLeftStatus').html('There are currently <strong>'+rec_cnt+'</strong> jobs available');	/* write the record count */
		
		var newHTML = data.substring(idx+1);

		$('.jobsHolder').html(newHTML);

		var func = "show_simple";
		live==1?func="show_advanced":func="show_simple";


		/* build a string showing the page numbers*/
		j=current_page;
		++j;
		
		var tp= Math.ceil(rec_cnt/9); /* total number of pages */

		var s_pn=current_page-4;
		if(s_pn<0) s_pn=0;

		var f_pn=s_pn+8;
		if(f_pn>(tp-1)) f_pn=(tp-1);

		if(f_pn==(tp-1)) s_pn=tp-9;
		if(s_pn<0) s_pn=0;




		newHTML="";
		if(current_page<(tp-1))newHTML="<a href='javascript:"+func+"("+j+",1)' class='next'><span class='hidden'>Previous</span></a>";
		newHTML+="<ul>";
		
		for(f=s_pn;f<=f_pn;f++){
		j = f+1;
		newHTML+="<li";

		if(f==current_page) newHTML+=" class='current'";
		newHTML+=">";
		if(f!=current_page){
			newHTML+="<a href='javascript:"+func+"("+f+",1)'>"+j+"</a>";
			}else{
			newHTML+=j;
			}
		newHTML+="</li>";
		}

		

		j=current_page;
		--j
		if(j<0) j=0;
		newHTML+="</ul>";
		if(current_page>0) newHTML+="<a href='javascript:"+func+"("+j+",1)' class='prev'><span class='hidden'>Previous</span></a>";
	
		$('.searchFilterRight').html(newHTML);
		$('#loading').hide();

	}
	
function setlocations(id){
	sel = document.form2.joblocation;

	for(f=0; f<sel.length; f++){
		 if(sel.options[f].value==id) sel.options[f].selected=true;
	}

}

function settitles(id){

	sel = document.form2.jobtitle;

	for(f=0; f<sel.length; f++){
		 if(sel.options[f].value==id) sel.options[f].selected=true;
		
	}

}
function settypes(id){

	sel = document.form2.jobtype;

	for(f=0; f<sel.length; f++){
		 if(sel.options[f].value==id) sel.options[f].selected=true;
		
	}

}
function seticons(){
	
	for(f=0;f<20;f++){
		/*ic = "document.form2.icon_"+f+".checked=true";*/
		
		if($("#form2 #icon_"+f).attr("checked")) {
			$('#icon_'+f+' a').addClass("iconHit");
		}else{
			$('#icon_'+f+' a').removeClass("iconHit");
		}
	}
}



function setliveicons(){
		         
          		sel = document.form2.jobtype;
 	
		        		/* 'document.form2.jobtype' is the drop down list on form2 - not the icons*/
		        for(f=0; f<sel.length; f++){
		        	g=sel.options[f].value;

 	        			if(sel.options[f].selected){
		        				$('#icon_'+g+' a').addClass("iconHit");
		        				/*$('#form2 #icon_'+g).attr("checked","checked");*/
							 $("input[name='icon_"+g+"']").attr("checked",true);
							
		        		}else{
		        				$('#icon_'+g+' a').removeClass("iconHit");
		        				/*eval("document.form2.icon_"+g+".checked=false");*/
							$("input[name='icon_"+g+"']").attr("checked",false);
		        		}
		        		
			}         		
}

function setDefaultText(defualttext){
	
	sel = document.form2.search;
	if(sel.value=="") sel.value=defualttext; 
}
function clearDefaultText(searchstring){
	sel = document.form2.search;
	string = "/"+sel.value+"/";
	if(string.search(searchstring)!=-1) sel.value="";
	$('#search').focus(); 

}
function passboxes(){
	//if (set==undefined) set=0;
	if($('#reg').attr("checked")){//){
		$('#passbox').show();
		$('#reg').attr("checked",true);
	}else{
	
		$('#passbox').hide();
		$('#reg').attr("checked",false);
	}
}	