var display_search = false;

function do_page_change( e )
{
   var targ = get_event_target( e );
   var page = targ.options[targ.options.selectedIndex].value;

   set_search_cookie( page );

   if ( display_search == true )
   {
     document.location.reload(1);
   }
   else 
   {

     if(typeof(page_data) != 'undefined' && page_data.query_string)
     {
         document.location.href = 'http://' + document.location.host + '/search/results.cgi?' + page_data.query_string + '&page=' + page;
         return true;
     }

     outList = return_clean_search( {'results_type':1, 'page':1 } );
     outList[outList.length] = 'page=' + String( page );
     outList[outList.length] = 'vac_jump=' + (job_page?'1':'0');

     document.location.href = 'http://' + document.location.host + '/search/results.cgi?' + outList.join('&') ;

   }
}

function do_sort_change( e )
{
   var targ = get_event_target( e );
   var sort = targ.options[targ.options.selectedIndex].value;

   set_search_cookie( 1, { 'order_by':sort } );

   if ( display_search == true )
   {
     document.location.reload(1);
   }
   else 
   {


   outList = return_clean_search( {'results_type':1, 'page':1, 'order_by':1 } );

   outList[outList.length] = 'page=1';
   outList[outList.length] = 'order_by=' + sort;
   outList[outList.length] = 'vac_jump=' + (job_page?'1':'0');


   document.location.href = 'http://' + document.location.host + '/search/results.cgi?' + outList.join('&') ;

   }
}

function set_search_cookie( page_num, extra, clear_cookie )
{
    if ( ! extra ) { extra = {} }
    if ( ! clear_cookie ) { clear_cookie = false }

    var rem = { 'results_type':1, 'page':1, 'vac_jump':1 };

    for ( var x in extra ) { rem[x] = 1 }

    var outList = return_clean_search( rem );
    
    if ( clear_cookie ) { outList = new Array (); }

    outList[outList.length] = 'page=' + String( page_num );

    for ( var x in extra )
    {
      outList[outList.length] = x + '=' + extra[x];
    }

    var cookie_string = escape(outList.join("&"));

    document.cookie = "search=" + cookie_string +"; path=/";

    return true;
}

function return_clean_search( not_keep )
{
    var hrefList = read_search_cookie();
    
    var search = null;
    
    if ( hrefList.match( /&amp;/ ) )
        {
            var search = hrefList.split("&amp;");       
        }
    else
        {
            var search = hrefList.split("&");
        }
    
    var outList = new Array();
    
    for ( var x = 0; x < search.length; x++ )
        {
            var s = search[x].split("=");
            
            if ( s[0] && ! not_keep[s[0]] ) { outList[outList.length] = s.join("=") };
        }
    
    return outList;
}


function page_range_string( page, page_size, max )
{
   var start = ( ( page - 1 ) * page_size ) + 1;
   var end = page_size * page > max?max:page_size * page;


   if ( start != end )
   {
     return commas(String(start)) + '-' + commas(String(end));
   }
   else
   {
     return commas(String(start));
   }
}

function make_sort_dropdown(target)
{
   var select = document.createElement('SELECT');
   target.appendChild(select); 
   addEvent( select, 'change', do_sort_change);

   var sort_options = { 'date_posted':'most recent', 'job_title':'title a-z', '_job_title':'title z-a',
                    'location':'location a-z', '_location':'location z-a',
                    'company':'company a-z', '_company':'company z-a' };

   var sort_order = [ 'date_posted', 'job_title', '_job_title','location','_location','company', '_company'];

   var order = get_order_by();

   var idx = 0;

   for ( var i = 0; i < sort_order.length; i++ )
   {
      select.options[select.options.length] = new Option ( sort_options[sort_order[i]], sort_order[i], 0, 0);

      if ( sort_order[i] == order ) { idx = i }

   }
   select.selectedIndex = idx;

   return;
}

function get_order_by()
{

   outList = return_clean_search({});

   var order = 'date_posted';

   for ( var x = 0; x< outList.length; x++ )
   {
      var s = outList[x].split('=');
      if ( s[0].match(/^order_by/) ) { order = s[1] }
   }

   return order;
}

function make_page_dropdown( data_list, target )
{
   if ( data_list.count == 0 ) { target.appendChild(document.createTextNode('0')); return; }

   var select = document.createElement('SELECT');
   target.appendChild(select);
   addEvent( select, 'change', do_page_change);

   var max_page = parseInt(data_list.count/data_list.page_size) + ( ( data_list.count % data_list.page_size == 0 ) ?0:1 )

   var idx = 0;

   for ( var i = 1; i <= max_page; i++ )
   {
        select.options[select.options.length] = new Option ( page_range_string( i, data_list.page_size, data_list.count ) , i,0,0 );
       if ( i == data_list.page ) { idx = i - 1 } 
   }
  
   select.selectedIndex = idx;

   return;
}

