/* Crea un parametro timestamp da accodare ad una url */
/* Utilizzato per ogni richiesta ajax e/o altre url (anche src di immagini) per evitare caching da parte dei browsers */
function urlTimestamp()
{
  var d = new Date();
  return 'ts=' + encodeURIComponent(d.getTime());
}

/* preso un parametro (myVar), controlla se e' vuoto o nullo;
   se trimIt e' true, effettua prima un trim */
function isBlank ( myVar, trimIt )
{
  if ( myVar == null )
    return true;

  if ( typeof(myVar) == 'object' )
    return false;

  if ( trimIt )
    myVar = trim(myVar);

  if ( myVar == '' )
    return true;

  return false;
}

/* preso un parametro (myVar), elimina gli spazi iniziali e finali */
function trim ( myVar )
{
  while (myVar.substring(0,1) == ' ')
  {
    myVar = myVar.substring(1, myVar.length);
  }
  while (myVar.substring(myVar.length-1, myVar.length) == ' ')
  {
    myVar = myVar.substring(0,myVar.length-1);
  }
  return myVar;
}

/* Encoding Email: inserisce nel testo un link di tipo mailto.
// Modificato rispetto al precedente per usare la classe e non l'id, e jquery.
// ------------------------------------------------------------------------------------------
// mailToClass : classe di un elemento span (o altro) che conterra' la mail.
// emUsername  : la parte prima della chiocciola della mail
// emDomain    : la parte dopo la chiocciola della mail
// emText      : il testo del link; se null viene usata la stessa mail come anchor text */

function jMailTo(mailToClass, emUsername, emDomain, emText)
{
  if ( emText == null )
    emText = emUsername + '@' + emDomain;

  $('.' + mailToClass).html('<a href="mailto:' + emUsername + '@' + emDomain + '">' + emText + '</a>');
}

/* Serve solo per la form di ricerca principale: prepara le url parlanti e 
// se i valori dei campi cosa e dove sono quelli di
// default (ovvero non sono stati settati dall'utente), i campi vengono resettati. Serve per 
// evitare di mandare richieste a Solr con i dati di default che in genere servono solo a spiegare
// brevemente cosa ci si aspetta in input in quel campo.
*/
//function doSearch( defaultCosa, defaultDove )
//{
//  var cosa = $('#inp_cosa').val();
//  var dove = $('#inp_dove').val();
//
//  if ( $('#inp_cosa').val() == defaultCosa )
//  {
//    $('#inp_cosa').val('');
//    cosa = '';
//  }
//
//  if ( $('#inp_dove').val() == defaultDove )
//  {
//    $('#inp_dove').val('');
//    dove = '';
//  }
//
//  var formAction  = $('#searchform #searchbase').attr('action');
//  var isLavoroorg = false;
//  if ( formAction == './vsearch' )
//      isLavoroorg = true;
//
//  /*
//  @TODO: riportata da lavoro.org, sara' da fare in futuro?
//  var firstChar   = '?';
//  */
//
//  /* @TODO: riportata da lavoro.org, sara' da fare in futuro
//  //Caso speciale: se c'e' un'offerta id, gli altri campi sono inutili (vedi ricerca nell'AH che non considera gli altri campi)
//  //Redirect alla ricerca
//  if ( ! isBlank($('#fld_query_filter_id').val(), true) )
//  {
//    location = contextPath + '/controller?action=search_basesearch&id=' + encodeURIComponent($('#fld_query_filter_id').val());
//    return;
//  }
//  */
//
//  //Se c'e' un termine di ricerca, viene costruita una url per l'url rewrite
//  if ( ! isBlank(cosa, true) || ! isBlank(dove, true) )
//  {
//    var querystringCosa = '';
//    if ( ! isBlank(cosa, true) )
//      querystringCosa = encodeURIComponent(trim(trim(trim(cosa).replace(/_/g, ' ')).replace(/ /g, '-')).replace(/-+/g, '-'));
//
//    if ( querystringCosa == '-' )
//      querystringCosa = '';
//
//    var querystringDove = '';
//    if ( ! isBlank(dove, true) )
//    {
//      if ( querystringCosa != '' )
//        querystringDove = '_';
//      querystringDove = querystringDove + encodeURIComponent(trim(dove).replace(/ /g,'-'), true);
//    }
//
//    var suffix = '-s';
//    if ( querystringCosa == '' && querystringDove != '' )
//      suffix = '-l';
//
//    if ( isLavoroorg )
//      suffix = suffix.toUpperCase()
//
//    location = globalContextPath + '/offerte_lavoro_' + querystringCosa + querystringDove + suffix + '.htm';
//    return;
//  }
//
//  /*
//  @TODO: riportata da lavoro.org, sara' da fare in futuro?
//  else
//  {
//    formAction += '?action=search_basesearch';
//    firstChar = '&';
//  }
//  */
//
//  /*
//  @TODO: riportata da lavoro.org, sara' da fare in futuro
//  var querystring = '';
//
//  $.each($('#inputbase :input'), function() {
//    if ( ! isBlank(this.value, true) && this.name != 'query_querystring_1' && this.name != 'action' && this.name != 'query_fieldname_1' && this.name != 'regione' )
//    {
//        if ( ( this.type == 'checkbox' || this.type == 'radio' ) && this.checked )
//        {
//          querystring += firstChar + this.name + '=' + encodeURIComponent(this.value);
//          firstChar = '&';
//        }
//        if ( this.type != 'checkbox' && this.type != 'radio' && this.type != 'submit' && this.type != 'reset' )
//        {
//          querystring += firstChar + this.name + '=' + encodeURIComponent(this.value);
//          firstChar = '&';
//        }
//    }
//  });
//  */
//
//  location = formAction;
//  return;
//}

function doSearch()
{
  var cosa = $('#inp_cosa').val();
  var dove = $('#inp_dove').val();

  var formAction  = $('#searchform #searchbase').attr('action');
  var isLavoroorg = false;
  if ( formAction == './vsearch' )
      isLavoroorg = true;

  //Se c'e' un termine di ricerca, viene costruita una url per l'url rewrite
  if ( ! isBlank(cosa, true) || ! isBlank(dove, true) )
  {
    var querystringCosa = '';
    if ( ! isBlank(cosa, true) )
      querystringCosa = encodeURIComponent(trim(trim(trim(cosa).replace(/_/g, ' ')).replace(/ /g, '-')).replace(/-+/g, '-'));

    if ( querystringCosa == '-' )
      querystringCosa = '';

    var querystringDove = '';
    if ( ! isBlank(dove, true) )
    {
      if ( querystringCosa != '' )
        querystringDove = '_';
      querystringDove = querystringDove + encodeURIComponent(trim(dove).replace(/ /g,'-'), true);
    }

    var suffix = '-s';
    if ( querystringCosa == '' && querystringDove != '' )
      suffix = '-l';

    if ( isLavoroorg )
      suffix = suffix.toUpperCase()

    location = globalContextPath + '/offerte_lavoro_' + querystringCosa + querystringDove + suffix + '.htm';
    return;
  }

  location = formAction;
  return;
}
