//-----------------------------------------------------------------------
function supportsLocalStorage() {
  try {return 'localStorage' in window && window['localStorage'] !== null;} catch (e) {return false;}
}

//-----------------------------------------------------------------------
function job_exist(id)
{
if(localStorage.getItem("jobs.count") === null)
return false;

 if(localStorage["jobs.count"] == "0")
 return false;

 if(localStorage["jobs.id."+id.toString()] == id )
 return true;

return false;
}

//-----------------------------------------------------------------------
function save_job(e,id)
{var count = 0;
if(localStorage.getItem("jobs.count") === null)
count = 1;
else
count = parseInt(localStorage["jobs.count"]) +1;

localStorage["jobs.count"] = count;
localStorage["jobs.id."+(id).toString()] = id;

e.innerHTML="Saved";
e.style.backgroundColor = '#333';
 //$('#'+id).remove();
e.onclick = null;
}

//-----------------------------------------------------------------------
function load_jobs() {
 if (!supportsLocalStorage()) { return false; }	
 if((localStorage.getItem("jobs.count") == null)||(localStorage.getItem("jobs.count") == "0"))
 {
   $('#jobs').css('height','84%');
   $('#jobs').css('justify-content','center'); 
   $('#jobs').html('<div id="no_jobs"><p class="size9">No saved jobs</p></div>');
 return;
 }

 httpObject1 = getHTTPObject();
 var ids = '';
 count = parseInt(localStorage["jobs.count"]);
 var j=0;
 for (var i = 0; j< count;i++)
 {
 var key = localStorage.key(i);
  if( key.indexOf("jobs.id") > -1)
  {
   ids += localStorage[key]+","; 
   j++; 
  }   
 }

 ids = ids.substring(0, ids.length - 1);//take off the last coma
 
  httpObject1.mozSystem = true;
  if (httpObject1 != null) 
  {
   link = url3+ids;
   httpObject1.open("GET", link , true);
   httpObject1.onreadystatechange = drawSavedJobs;
   httpObject1.send(null);
  }//if

  if(count > 0)
  {
   $('#jobs').css('height','auto');
   $('#jobs').css('justify-content','unset'); 
   $('#jobs').empty();
  }
  else
  {
   $('#jobs').css('height','84%'); 
   $('#jobs').css('justify-content','center'); 
   $('#jobs').innerHTML='<div id="no_jobs"><p class="size9">No saved jobs</p></div>';
  }
}

//-----------------------------------------draw saved jobs
function drawSavedJobs()
{
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;
  var lines=response.split(delimiter); 
  var list = document.getElementById("jobs");
  

  for(i=0;i<lines.length-1;i=i+11)
  {	
   if(dodo)
   {
    txt +='<div id="'+lines[i+9]+'" class="one">';
	txt+='<div id="left">';
	txt+='<div id="job_logo"><img id="pnn" src="img/jopb.jpg"/></div>';
	}
   else
   {
   txt +='<div id="'+lines[i+9]+'" 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="#">';
   txt+='<div id="save_job" class="size3" onclick="clear_job(this,'+lines[i+9]+');">Clear</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>';  

   txt +='<p id="expr" class="size0"><b>Experience:</b> '+lines[i+3]+' years  <b>Salary:</b> '+lines[i+4]+'k $/year</br><b>Type:</b> '+types[lines[i+8]]+' <b>Date:</b> '+lines[i+10]+'<p>';
   txt +='</div></a>';
   
   txt +='</div>';	
   dodo=!dodo;           		 
  }

  list.innerHTML= txt;
  txt=null;
 }
}
//-----------------------------------------------------------------------
function clear_job(e,id)
{
if(job_exist(id)===false)
return;
var count  = parseInt(localStorage["jobs.count"]);
count--;

 localStorage.removeItem("jobs.id."+id.toString()); 
 localStorage["jobs.count"] = count;
 $('#'+id).remove();
 
 if(count == 0)
 {
   $('#jobs').css('height','84%');
   $('#jobs').css('justify-content','center'); 
   $('#jobs').html('<div id="no_jobs"><p class="size9">No saved jobs</p></div>');
 return;
 }
}
//-----------------------------------------------------------------------
function delete_jobs()
{
 localStorage.clear();
 show_msg("All saved jobs have </br>been cleared");
}