var SF_def_from_date = '';
var SF_def_to_date = '';

function SF_validate_dates()
{
  // first-time call, create the calendar.
  if (typeof Calendar != 'function') return;
  var cal = new Calendar(true, null);
  // uncomment the following line to hide the week numbers
  // cal.weekNumbers = false;

  if (typeof showsTime == "string") {
    cal.showsTime = false;
    cal.time24 = (showsTime == "24");
  }
  cal.setRange(2000, 2038);        // min/max year allowed.
  cal.create();

  //debugvars
  var def_date = (SF_def_from_date ? SF_def_from_date : "10.11.2000"); //FIX
  var def_num_art = 300;

  var num_fmt = gettext('GLOB_LAST_%D_ARTICLES');

  /* validate from: */
  if (typeof document.form.SF_date_from != 'undefined') {
    var parsed_date_from = cal.parseDate(document.form.SF_date_from.value, "%x", def_date, def_num_art);
    if ( typeof parsed_date_from != "object" ) {
      document.form.SF_date_from.value = num_fmt.replace(new RegExp("%d|%D","g"), parsed_date_from);
      document.form.SF_date_from_timestamp.value = "-" + parsed_date_from;
      SF_set_date_label(true);
    } else {
      document.form.SF_date_from.value = parsed_date_from.print("%x");
      document.form.SF_date_from_timestamp.value = parsed_date_from.valueOf() / 1000;
      SF_set_date_label(false);
    }
  }

  /* validate to: */
  if (typeof document.form.SF_date_to != 'undefined') {
    var parsed_date_to = cal.parseDate( document.form.SF_date_to.value, "%x" );
    document.form.SF_date_to.value = parsed_date_to.print("%x");
    document.form.SF_date_to_timestamp.value = parsed_date_to.valueOf() / 1000;
  }
}

function SF_set_date_label(show_last_num) {
  var SF_obj = new makeObj_popup('SF_date_from_label');
  var label = '';
  if (show_last_num)
    label = gettext('SF_SS_WIDGET_SEARCH_FROM_DATE').replace(new RegExp("^ +"),"");
  else
    label = gettext('SF_WIDGET_SEARCH_FROM_DATE') + ':';
  if (label!='') label += '&#160;';
  SF_obj.writeIt(label);
}

function SF_update_regex(field, caller)
{
  var data = document.form.SF_regex.value;
  document.form.SF_regex.value = field + " " + data;
  caller.options[0].selected=true;
}

function SF_submit_form()
{
  document.form.action_performed.value = 'SF_search';

  if (document.form.LBuck_active_id)
    document.form.LBuck_active_id.value = '0';
  submit_form();
}

function SF_reset_search()
{
  document.form.SF_new_search.value = 'true';
  SF_defaults();
  if (document.form.SF_search_context && document.form.SF_search_context[0])
    document.form.SF_search_context[0].checked = true;
}

function SF_context_changed()
{
  if (document.form.SF_search_context && document.form.SF_search_context[1] && document.form.SF_search_context[1].checked) {
    SF_defaults();
    document.getElementById('RL_browse').style.cursor='auto';
  } else {
    SF_reset();
    document.getElementById('RL_browse').style.cursor='default';
  }
}

function SF_empty()
{
  document.form.SF_search.value = '';
  document.form.SF_word_rule.options.selectedIndex = 0;
  document.form.SF_regex.value = '';
}

function SF_defaults()
{
  if (typeof document.form.SF_search != 'undefined')
    document.form.SF_search.value = '';
  if (typeof document.form.SF_word_rule != 'undefined')
    document.form.SF_word_rule.options.selectedIndex = 0;
  if (typeof document.form.SF_search_scope != 'undefined')
    document.form.SF_search_scope.checked = false;
  if (typeof document.form.SF_regex != 'undefined')
    document.form.SF_regex.value = '';
  if (typeof document.form.SF_date_from != 'undefined' && SF_def_from_date)
    document.form.SF_date_from.value = SF_def_from_date;
  if (typeof document.form.SF_date_to != 'undefined' && SF_def_to_date)
    document.form.SF_date_to.value = SF_def_to_date;
}

function SF_reset() {
  //alert(SF_searchtext);
  document.form.SF_search.value = SF_searchtext;
  document.form.SF_regex.value = SF_regextext;
  for (i = 0; i < document.form.SF_word_rule.options.length; i++) {
    if (document.form.SF_word_rule.options[i].value == SF_word_rule_text) {
      document.form.SF_word_rule.options.selectedIndex = i;
    }
  }
}



//function SF_new_search() {
//  document.form.action_performed.value='SF_new_search';
//  submit_form();
//}

function SF_edit_search(index) {
  if (document.form.SF_search_context && document.form.SF_search_context[0] && document.form.SF_search_context[0].checked) {
    return;
  } else {
    document.form.SF_edit.value          = index;
    document.form.action_performed.value = 'SF_edit';
    submit_form();
  }
}


var nav = window.Event ? true : false;
if (nav) {
  window.captureEvents(Event.KEYDOWN);
  window.onkeydown = key_pressed_handler;
} else {
  document.onkeydown = key_pressed_handler;
}

function key_pressed_handler(e) {
  if (nav && e.which == 13) {
    if (e.target && e.target.name && e.target.name.indexOf('SF_')>=0) {
      SF_submit_form();
    }
  } else if (!nav && window.event.keyCode == 13) {
    if (event.srcElement && event.srcElement.name && event.srcElement.name.indexOf('SF_')>=0) {
      SF_submit_form();
    }
  }
}

