var httpObject1 = null;
var page=0;
var loc='';
var key='';
var version ='v3';

var url1 = 'http://qjobs.snurbs.com/'+version+'/get_jobs.php?p=';
var url2 = 'http://qjobs.snurbs.com/'+version+'/get_comps.php?p=0';
var url3 = 'http://qjobs.snurbs.com/'+version+'/get_jobs_by_id.php?ids=';
var url4 = 'http://qjobs.snurbs.com/'+version+'/add_job.php';
var url5 = 'http://qjobs.snurbs.com/'+version+'/get_jobs_comp.php?c=';
//-------------------------------------   Get the HTTP Object
function getHTTPObject()
{
 if (window.ActiveXObject) 
  return new ActiveXObject("Microsoft.XMLHTTP");
 else 
  if (window.XMLHttpRequest) 
   return new XMLHttpRequest({mozAnon: true, mozSystem: true});
  else 
  {
   alert("Your browser does not support AJAX.");
   return null;
  }
} 

//-----------------------------------------draw jobs
function drawJobs()
{
var txt = '';
var dodo = true;
var delimiter='#!#';
var types=['Temporary job','Internship','Contract','Part-time job','Full-time job'];

 if(httpObject1.readyState == 4)
 {
  var response = httpObject1.responseText;
  if(response == "no_results")
  {
   if(page==0)
   {
    show_msg("No jobs found");
	$('#s21').click();
	}
   else
    show_msg("No more jobs"); 
  return;
  }
  var lines=response.split(delimiter);
  page = page+parseInt(lines[0])-1;
  var list = document.getElementById("search_results");
  
  for(i=1;i<lines.length-1;i=i+11)
  {
   if(dodo)
   {
    txt +='<div id="" class="one">';	
	txt+='<div id="left">';
	txt+='<div id="job_logo"><img id="pnn" src="img/jopb.jpg"/></div>';
   }
   else
   {
    txt +='<div id="" class="two">';	
	txt+='<div id="left">';
	txt+='<div id="job_logo"><img id="pnn" src="img/jop.jpg"/></div>';
   }
   
	txt+='<div id="options">';
	txt+='<a href="#">';
    if(job_exist(lines[i+9])===false)
	 txt+='<div id="save_job" class="size3" onclick="save_job(this,'+lines[i+9]+');">Save</div>';
	else
	 txt+='<div id="saved_job" class="size3" onclick="clear_job(this,'+lines[i+9]+');">Saved</div>';	
	txt+='</a>';
	txt+='</div>';
	
	txt+='</div>';
	
   txt+='<a href="'+lines[i+6]+'" target="_blanc">';
   txt+='<div id="txt">';   
   txt +='<p id="title" class="size2">'+lines[i]+'</p>';
   txt +='<p id="location" class="size1">at <b>'+lines[i+7]+'</b> '+lines[i+2]+','+lines[i+1]+'</p>';
   txt +='<p id="descr" class="size0"></br><b>Description:</b> '+lines[i+5]+'</p>'; 
   var sal_val='';
   if(lines[i+4]=='?')
   sal_val = 'unknown';
   else
   sal_val = lines[i+4]+' k $/year';
   
   txt +='<p id="expr" class="size0"><b>Experience:</b> '+lines[i+3]+' years  <b>Salary:</b> '+sal_val+'</br><b>Type:</b> '+types[lines[i+8]]+' <b>Date:</b> '+lines[i+10]+'<p>';
   txt +='</div></a>';
   
   txt +='</div>';	
   dodo=!dodo;  
  }//for
			
  //add a load more bt
  txt+='<div id="load_more_cont"> <a  href="#" onclick="load_more_jobs();"><div id="load_more_bt" class="size6">Get more</div></a></div>';
  list.innerHTML+= txt;
  txt=null;
 }//http object state 4
}
//-----------------------------------------draw companies
function drawComps()
{
var txt = '';
var dodo = true;
var delimiter='#!#';

 if(httpObject1.readyState == 4)
 {
  var response = httpObject1.responseText;
  var lines=response.split(delimiter); 
  var list = document.getElementById("comp");
    
  for(i=0;i<lines.length-1;i=i+2)
  {	
   txt +='<a href="#" onclick="get_comp_jobs(\''+lines[i+1]+'\');">';  
   if(dodo)
   {
    txt +='<div id="" class="one">';
	txt+='<div id="comp_logo"><img id="pnn" src="img/jopb.jpg"/></div>';
   }
   else
   {
    txt +='<div id="" class="two">';	
    txt+='<div id="comp_logo"><img id="pnn" src="img/jop.jpg"/></div>';
   }
   txt+='<div id="txt">';   
   txt +='<p id="descr" class="size0"></br><span id="bluebold">'+lines[i+1]+'</span> '+lines[i]+' jobs</br></br>0 reviews</p>';  
   txt +='</div>';
   
   txt +='</div></a>';	
   dodo=!dodo;           		 
  }
			
  //add a load more bt
 // txt+='<div id="load_more_cont"> <a  href="#" onclick="load_more_comp();"><div id="load_more_bt" class="size6">Get more</div></a></div>';
  list.innerHTML= txt;	
 }
}
//--------------------------------------------------------------------get more jobs
function load_more_jobs()
{
$('#load_more_cont').remove();
page++;
get_jobs();
}
//--------------------------------------------------------------------get jobs
function get_jobs()
{
 httpObject1 = getHTTPObject();
//httpObject1.mozSystem = true;
 if (httpObject1 != null) 
 {
  link = url1+page;
  
  if(loc!='')
  link+='&l='+loc;
  
  if(key!='')
   link+='&k='+key;
   
  httpObject1.open("GET", link , true);
  httpObject1.onreadystatechange = drawJobs;
  httpObject1.send(null);
 }
 return true;
}
//--------------------------------------------------------------------get companies
function get_comps()
{
 httpObject1 = getHTTPObject();
 httpObject1.mozSystem = true;
 if (httpObject1 != null) 
 {
  link = url2;//+page
  httpObject1.open("GET", link , true);
  httpObject1.onreadystatechange = drawComps;
  httpObject1.send(null);
 }
 return true;
}
//--------------------------------------------------------------------get jobs by companies
function get_comp_jobs(comp)
{
 $('#search_results').animate({left: '0%'}, 400);
 $('#search').animate({left: '100%'}, 400);	
 $('#comp').animate({left: '200%'},400);
 $('#comp_jobs').animate({left: '300%'},400);
 $('#comp').empty();
 $('#s21').addClass('selected_div');
 $('#s22').removeClass('selected_div');
 $('#s23').removeClass('selected_div');
  
  
 httpObject1 = getHTTPObject();
//httpObject1.mozSystem = true;
 if (httpObject1 != null) 
 {
  link = url5+comp;   
  httpObject1.open("GET", link , true);
  httpObject1.onreadystatechange = drawJobs;
  httpObject1.send(null);
 }
 return true;
}
//--------------------------------------------------------------------
function show_add_job()
{
$("#add_container").load( "add.html");
}
//--------------------------------------------------------------------
function hide_add_job()
{
$("#add_container").html("");
}

//--------------------------------------------------------------------
function is_empty(id,msg)
{
 if(document.getElementById(id).value=="")
 {
  show_msg("Please enter a "+msg);
  return true;
 }  return false;
}

//--------------------------------------------------------------------
function add_job()
{
if(is_empty("jobtitle_in","job title"))return;
if(is_empty("company_in","company name"))return;
if(is_empty("city_in","city"))return;
if(is_empty("desc_in","short job description"))return;

var http = getHTTPObject();
http.mozSystem = true;

var jobtitle = document.getElementById("jobtitle_in").value;
var company = document.getElementById("company_in").value;
var city = document.getElementById("city_in").value;
var sal = document.getElementById("sal_in").value;
var country = document.getElementById("country_in").value;
var desc = document.getElementById("desc_in").value;
var exp = document.getElementById("exp_in").value;
var type = document.getElementById("type_in").value;
var link = document.getElementById("link_in").value;

var params = "jobtitle_in="+jobtitle+"&company_in="+company+"&city_in="+city+"&sal_in="+sal+"&country_in="+country+"&desc_in="+desc+"&exp_in="+exp+"&type_in="+type+"&link_in="+link+"&visible=1";
http.open("POST", url4, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

/*http.onreadystatechange = function() {//Call a function when the state changes.
	if(http.readyState == 4 && http.status == 200) {
		alert(http.responseText);
	}
}*/
http.send(params);
 
show_msg("Job added");
$("#add_container").html("");
}
//-----------------------------------------------------------------------
function show_msg(msg)
{
$('#msg_container').html('<div id="msg"><div id="msg_box"><p id="msg_text" class="font-size-0">'+msg+'</p><a id="msg_link" href="#" onclick="hide_msg();"><p id="msg_bt" class="font-size-0">Ok</p></a></div></div>');
}
//-----------------------------------------------------------------------
function hide_msg()
{
$("#msg_container").html('');
}

//--------------------------------------------------------------------
function about()
{
 show_msg("QuickJobs app v1.0.1</br>by Djeradi Djibril </br>All rights reserved 2014</br>Contact: sazori@live.fr");
}

//------------------------------------------------------------------ inputs onfocus
function focus1(e,value)
{
 if(e.value == value) 
 { 
  e.value = ''; 
 }
}

//------------------------------------------------------------------ job title input onblur
function blur1(e)
{
 if(e.value=='')
 {
  if(e.id=='key')
   e.value='Job Title or Keyword';
  else
   e.value='Location (Optional)';
 }
}

//------------------------------------------------------------------ job title input onblur
function search_jobs()
{
 $('#search_results').animate({left: '0%'}, 400);
 $('#search').animate({left: '100%'}, 400);	
 $('#comp').animate({left: '200%'},400);
 $('#comp_jobs').animate({left: '300%'},400);
  
 page=0;//reset page count
 key = $('#key').val();
 if(key=='Job Title or Keyword')
  key='';
 loc = $('#loc').val();
 if(loc=='Location (Optional)')
  loc='';
 get_jobs(key,loc);  
}