var sel;    // variable for holding an HTML DOM select object
var opt;    // variable for holding an HTML DOM option object
var img;    // variable for holding an HTML DOM image object
var swap;   // variable for holding an HTML DOM image object
var newimg; // variable for holding an HTML DOM image object
var elem;   // variable for holding an HTML DOM element object
var redir;  // variable for holding an alternerate id

/*
** this function finds and returns an element by id or false if not 
** found.
*/
function find_id(id)
{
  if (('' + id) && (elem = document.getElementById(id)))
  {
    return elem;
  }
  return false;
}

/*
** this function hides an element by id.
*/
function hide_id(id)
{
  if (find_id(id))
  {
    elem.style.display    = 'none';
    elem.style.visibility = 'hidden';

    return elem;
  }
  return false;
}

/*
** this function shows an element by id (and by display style -- default
** is inline).
*/
function show_id(id, display)
{
  if (!display)
  {
    display = 'inline'; 
  }
  if (find_id(id))
  {
    elem.style.display    = display;
    elem.style.visibility = 'visible';

    return elem;
  }
  return false;
}

/*
** this function disables changes to a form element, and keeps its
** value from passing to the form handler.
*/
function disable(id)
{
  if (find_id(id))
  {
    elem.value	  = '';
    elem.readOnly = 'ReadOnly';
    elem.disabled = 'disabled';

    return elem;
  }
  return false;
}

/*
** function pending disables a button.
*/
function pending(id)
{
  if (find_id(id))
  {
    elem.readOnly = 'ReadOnly';
    elem.disabled = 'disabled';

    return elem;
  }
  return false;
}

/*
** this function enables changes to a form element, and makes sure its
** value will pass to the form handler.
*/
function enable(id)
{
  if (find_id(id))
  {
    elem.readOnly = '';
    elem.disabled = '';

    return elem;
  }
  return false;
}

/*
** this function puts a value in a form element as selected by id, just
** to make the interface a little simpler
*/
function input_value(id, value)
{
  if (find_id(id))
  {
    return elem.value = value;
  }
  return 'NOT FOUND';
}

/*
** Specific page helpers for select.
*/
function alt_options(list, item)
{
  len = list.length;
  i   = 0;

  for (i = 0; i < len; i++)
  {
    if (item == list[i][0])
    {
       return list[i];    
    }
  }
  return new Array();
}

/*
** fill_select (select, options, clear_one)
**
** required:  select	-> which select box to modify
**	      options	-> data to fill box with.
** optional:  clear_one	-> T/F default false, on true removes all old
**			    options for overwrite.  Otherwise leaves the
**			    first option. **** IMPORTANT ****
**			    if you want to completely erase the old
**			    version of the select you must pass a true
**			    value for this field!
*/
function fill_select(select, options, clear_one)
{
  clear_one	= ((clear_one) ? 0 : 1);
  len		= options.length;
  select.length = len + clear_one;

  for (i = 0; i < len; i++)
  {
    select.options[i + clear_one].value = options[i][0];
    select.options[i + clear_one].text  = options[i][1];
  }
}


/*
** this function stores a value in an input field and launches a form
*/
function confirmed_submit(id, input_id, form_id)
{
  input_value('' + input_id, id);
  if (find_id(form_id))
  {
    elem.submit();
  }
}


/*
** this function is for coloring a select to match the option, and 
** changing a display image.  Specifically designed for use in the
** publitapias site.
*/
function color_me(id, indir)
{
  if (find_id(id))
  {
    sel = elem;

    if (opt = sel.options[sel.selectedIndex])
    {
      redir = opt.id;

      if (!indir)
      {
	redir = opt.value;
      }
      if (!swap)
      { 
	swap = document.getElementById('flag_swap');
      }
      if (find_id('img_' + id))
      {
	img = elem;

	if (find_id('flag_id_' + redir))
	{
	  newimg      = elem;
	  swap.src    = img.src;
	  img.src     = newimg.src;
	  newimg.src  = swap.src;
	}
      }
      sel.style.backgroundColor = opt.style.backgroundColor;
      sel.style.color		= opt.style.color;
    }
  }
}

/*
** this function checks a select box's options for a match against value
** if found, resets the selectedIndex to the matching option, if not
** sets selectedIndex = -1.
*/
function get_option(sel_find, id_find, value)
{
  if ((!sel_find) && (!id_find))
  {
    return;
  }
  if (!sel_find)
  {
    sel = find_id('' + id_find);
  }
  else
  {
    sel = sel_find;
  }
  var opt = sel.options;
  var num = opt.size;
  var ind = 0;

  sel.selectedIndex = -1;

  while (ind < num)
  {
    if (value == opt[ind].value)
    {
      sel.selectedIndex = ind;
      break;
    }
    ind++;
  }
}

/*
** function deref
** This function  dereferences a hash by
** first array element (id)
** No previsions for multiple matching hash entries.
*/
function deref(source, id)
{
  for (i = 0; i < source.length; i++)
  {
    if (source[i][0] == id)
    {
      return source[i];
    }
  }
  return new Array();
}

/*
** Tools for Multi-lists
*/

/*
** change_lists()
** This is the action for onChange action
** of the list select box.
*/
function change_lists()
{
   document.getElementById('select_list').submit();
}

/*
** function fix_to(box)
** This function fix the current passed box
** droping out all the selected items
** (It's suppossed that this items where sent
** to another list.)
*/

function fix_to(box)
{
  for (ind = 0; ind < box.options.length; ind++)
  {
    option = box.options[ind];

    if (option.selected)
    {
      option2 = box.options[ind + 1]
      option  = option2;
      box.remove(ind);
      ind--;
    }
  }
}

/*
** function rebuild_txt()
** This function rebuilds the str_curr field
** with the values in the current select box
*/
function rebuild_txt() 
{
  box	    = find_id('edit_sel_current');
  txt_field = find_id('str_curr');
  txt	    = "";

  for (ind = 0; ind < box.length; ind++)
  {
    if (txt != '')
    {
      txt = txt + "|";
    }
    txt = txt + box.options[ind].value;
  }

  txt_field.value = txt;
}

/*
** is_ancestor -- needed to roll my own, since yui's dom didn't cover
**		  enough cases correctly. 
** @param   {HTMLElement} haystack The possible ancestor
** @param   {HTMLElement} needle The possible descendent
** @return  {Boolean}	  Whether or not the haystack antecedes needle
*/
function is_ancestor(haystack, needle)
{
  var parent; 

  if ((haystack) && (needle))
  {
    if (haystack == needle) // not sure if this test should be here ...
    {			    // but cuts down code elsewhere.
      return true;
    }
    for (parent = needle.parentNode; parent; parent = parent.parentNode)
    {
      if      (parent	      == haystack)  { return true;  }
      else if (parent.tagName == 'HTML')    { return false; }
    }
  }
  return false;
}

/*
** function move_elemnts(from, to)
** This function moves objects from "from"
** to "to"
*/
function move_elemnts(from, to)
{
  from = find_id('' + from);
  to   = find_id('' + to);

  for (ind = 0; ind < from.length; ind++)
  {
    option = from.options[ind];

    if (option.selected)
    {
      to.length			      = to.length + 1;
      to.options[to.length - 1].value = option.value;
      to.options[to.length - 1].text  = option.text;
    }
  }
  
  fix_to(from);
  rebuild_txt();
}

/*
** function is_element_in(elem, box, select)
** This function checks if the element "elem"
** existe in the select box "box".
** If so, "elem" will be marked as true
** (for future call of the function fix to)
** Otherwise, it does nothing 
*/

function is_element_in(elem, box, select)
{
  if (box.length > 0)
  {
    for (i = 0; i < box.length; i++)
    {
      option = box.options[i];
    
      if (option.value == elem.value)
      {
	elem.selected = select;
	return true;
      }
    }
  }

  return false;
}

/*
** function fix_possible()
** This function fixes the select box
** possible, right after filling the 
** select box current up with the
** values the list contains.
*/
function fix_possible()
{
  current  = find_id('edit_sel_current');
  possible = find_id('edit_sel_possible');

  for (ind = 0; ind < possible.length; ind++)
  {
    option = possible.options[ind];
    
    is_element_in(option, current, true);
  }

  fix_to(possible);
}

/*
** function replace_char()
** This functions replaces one character for another
** inside a string, and returns it back.
*/
function replace_char(curr_text, character, replace)
{
  var res	= "";
  var curr_char = '';
  for (i = 0; i < curr_text.length; i++)
  {
    curr_char = curr_text[i];
    
    if (curr_char == character)
    {
      curr_char = replace; 
    }
    res = res + curr_char; 
  }

  return res;
}

/*
** function change_class()
** either adds or tries to remove a class from an objects class list.
*/
function change_class(obj, cls, add)
{
  if (add)
  {
    obj.className += ' ' + cls;
  }
  else
  {
    obj.className = obj.className.replace(cls, '').replace(/ ?$/, '');
  }
}

var images	  = new Array();

function preload(name)
{
  path		      = 'images/banner/';
  temp_array	      = new Array();
  temp_array['a']     = new Image();
  temp_array['a'].src = path + name + '_a.jpg';
  temp_array['b']     = new Image();
  temp_array['b'].src = path + name + '_b.jpg';
  images[name]	      = temp_array; 
}

function toggle_image(elem, which)
{
  image		= images[elem][which];
  curr_img	= find_id(elem);
  curr_img.src	= image.src;
}

var months = new Array(	'January',    'February', 'March',    'April', 
			'May',	      'June',	  'July',     'August', 
			'September',  'October',  'November', 'December');

function fill_combos(month_box, day_box, year_box, date, min_date)
{
  month	      = 12;
  year	      = 10;
  days	      = 31;
  curr_year   = 0;
  curr_month  = 0;
  today	      = new Date();
  year_diff   =	1900;
  
  if (date.getYear() > year_diff)
  {
    year_diff = 0;
  }

  if ((3 == date.getMonth()) || (5  == date.getMonth()) || 
      (8 == date.getMonth()) || (10 == date.getMonth()))
  {
    days = 30;
  }

  if (1 == date.getMonth())
  {
    days  = 28;
    bis	  = (year_diff + date.getYear()) % 4;

    if (0 == bis)
    {
      days = 29;
    }
  }

  /*******YEARS******/
  for (i = 0; i < 10; i++)
  {
    year		      = min_date.getYear() + year_diff + i;
    year_box.options[i].value = year; 
    year_box.options[i].text  = year;

    if ((date.getYear() + year_diff) == (year))
    {
      curr_year			    = year;      
      year_box.options[i].selected  = 1;
    }
  }

  /*********MONTHS**********/
  j		    = 0;
  month_box.length  = 12;

  for (i = 0; i < 12; i++)
  {
    if ((curr_year > (year_diff + min_date.getYear())) ||
	(i >= min_date.getMonth()))
    {
      month_box.options[j].text  = months[i];
      m_value			 = i + 1;

      if (m_value < 10)
      {
	m_value = '0' + m_value;
      }
      
      month_box.options[j].value = m_value;

      if (date.getMonth() == i)
      {
	month_box.options[j].selected = 1;
	curr_month		      = i;
      }
      j = j + 1;
    }
  }
  month_box.length = j;

  /***********DAYS***********/
  day_box.length  = days;
  j		  = 0;

  for (i = 0; i < days; i++)
  {
    if ((curr_year  >  (year_diff + min_date.getYear())) ||
	(curr_month >  min_date.getMonth())         ||
	((i + 1)    >= min_date.getDate()))
    {
      d_value			= i + 1;
      day_box.options[j].text   = d_value;

      if (d_value < 10)
      {
	d_value	= '0' + d_value;
      }
      
      day_box.options[j].value  = d_value;
    
      if (date.getDate() == (i + 1))
      {
	day_box.options[j].selected = 1;
      }
      j = j + 1;
    }
  }
  day_box.length = j;
}

function fill_calendar_combos()
{
  date			= new Date();
  curr_date		= new Date();
  month_in_box		= document.getElementById('month_in');
  month_out_box		= document.getElementById('month_out');
  day_in_box		= document.getElementById('day_in');
  day_out_box		= document.getElementById('day_out');
  year_in_box		= document.getElementById('year_in');
  year_out_box		= document.getElementById('year_out');
  month_in_box.length	= 12;
  year_in_box.length	= 10;
  month_out_box.length	= 12;
  year_out_box.length	= 10;
  days			= 31;
  
  /***************IN BOXES*******************/

  fill_combos(month_in_box, day_in_box, year_in_box, date, curr_date);

  /***************OUT BOXES*******************/
  curr_date = date;
  date.setDate(date.getDate() + 1);
  fill_combos(month_out_box, day_out_box, year_out_box, date, curr_date);
  fill_season();
}

function change_date(which)
{
  min_date  = new Date();

  if ('out' == which)
  {
    which     = 'in';
    month_box = document.getElementById('month_'+ which);
    day_box   = document.getElementById('day_'	+ which);
    year_box  = document.getElementById('year_'	+ which);
    which     = 'out';
    min_date  = new Date(year_box.value, (month_box.value - 1), 
			  day_box.value);
    min_date.setDate(min_date.getDate() + 1);
  }

  month_box = document.getElementById('month_'	+ which);
  day_box   = document.getElementById('day_'	+ which);
  year_box  = document.getElementById('year_'	+ which);
  date	    = new Date(year_box.value, (month_box.value - 1), day_box.value);

  fill_combos(month_box, day_box, year_box, date, min_date);

  if ('in' == which)
  {
    min_date  =	date;
    date.setDate(date.getDate() + 1);
    which     =	'out';
    month_box = document.getElementById('month_'+ which);
    day_box   = document.getElementById('day_'	+ which);
    year_box  = document.getElementById('year_'	+ which);
    fill_combos(month_box, day_box, year_box, date, min_date);
  }
  fill_season();
}

function explode(div, what)
{
  word  = '';
  rid   = 0;
  ret   = new Array();
  
  for (ex = 0; ex < what.length; ex++)
  {
    if (div == what.charAt(ex))
    {
      ret.length  = rid + 1;
      ret[rid]    = word;
      rid         = rid + 1;
      word	  = '';
    }
    else
    {
      word  = word + what.charAt(ex);
    }
  }
  
  ret.length  =	rid + 1;
  ret[rid]    = word;
  
  return ret;
}

/*
** Function which_season()
** This function says what's the season with which
** prices must be calculated
*/
function which_season(this_date)
{
  year_diff = 1900;
  
  if (this_date.getYear() > year_diff)
  {
    year_diff = 0;
  }
    
  for (kl = 0; kl < Seasons.length; kl++)
  {
    begin_date    = explode('-', Seasons[kl][2]);
    end_date      = explode('-', Seasons[kl][3]);
    end_date[0]   = begin_date[0] = year_diff + this_date.getYear();
    this_begin    = new Date(begin_date[0], (begin_date[1] - 1), begin_date[2]);
    
    //if (3 == Seasons[kl][0])
    /*
    ** I had this hardcoded with season 3,
    ** but I improved it making the change of
    ** the year when the month of the begin date is
    ** bigger than the month of the end date.
    */

    if (begin_date[1] > end_date[1])
    {
      end_date[0] = year_diff  + (this_date.getYear()  + 1);
      
      if (0 ==	this_date.getMonth())						
      {
	this_begin.setYear(this_begin.getYear()	- 1);
	end_date[0] = year_diff  + this_date.getYear();
      }
    }
    
    this_end =  new Date(end_date[0], (end_date[1] - 1), end_date[2]);
    
    /*      alert(begin_date[1] + '-' + begin_date[2]	+ '-' + begin_date[0] + 
		  ' <= '	+ (this_date.getMonth() + 1)  + '-' +
		  this_date.getDate() + '-' + (this_date.getYear()  +
		  year_diff)   +
		  " <= "	+ end_date[1] + '-' + end_date[2]   + '-'     + 
		  end_date[0]);
    
		  alert(this_begin  + ' <= ' +	this_date + ' <= ' + this_end);*/

    if ((this_begin <= this_date) &&
	(this_date  <= this_end))
    {
      return Seasons[kl][0];
    }
  }
  
  return 0;
}

function fill_season()
{
  obj_season	    = document.getElementById('season');
  obj_period	    = document.getElementById('period');
  obj_month_in_box  = document.getElementById('month_in');
  obj_day_in_box    = document.getElementById('day_in');
  obj_year_in_box   = document.getElementById('year_in');
  season_date	    = new Date(obj_year_in_box.value,  
			     (obj_month_in_box.value - 1),obj_day_in_box.value);
  sea_id	= which_season(season_date);

  for (ss = 0; ss < Seasons.length; ss++)
  {
    if (sea_id == Seasons[ss][0])
    {
      obj_season.innerHTML  = Seasons[ss][1];
      s_date		    = explode('-', Seasons[ss][2]); 
      e_date		    = explode('-', Seasons[ss][3]); 

      obj_period.innerHTML  = 'From ' + months[(s_date[1] - 1)] + ' ' + s_date[2]
			    + ' to ' + months[(e_date[1] - 1)] + ' ' + e_date[2];
      ss = Seasons.length;
    }
  }
}

function check_dates()
{
  month_in_box	= document.getElementById('month_in');
  month_out_box	= document.getElementById('month_out');
  day_in_box	= document.getElementById('day_in');
  day_out_box	= document.getElementById('day_out');
  year_in_box	= document.getElementById('year_in');
  year_out_box	= document.getElementById('year_out');

  date_ini = new Date(year_in_box.value,  (month_in_box.value - 1),  
		      day_in_box.value);
  date_fin = new Date(year_out_box.value, (month_out_box.value - 1), 
		      day_out_box.value);

  if ((date_fin.getDate()   == date_ini.getDate()) && 
      (date_fin.getMonth()  == date_ini.getMonth()) && 
      (date_fin.getYear()   == date_ini.getYear()))
  {
    message = 'The search must be done with at least one day of difference.';
  }
  else if (date_fin < date_ini)
  {
    message = 'The end date cannot be lower that the init date.';
  }
  else if (which_season(date_ini) != which_season(date_fin))
  {
    message = 'The reservation must be done on the same season.';
  }
  else
  {
    message = '';
    form    = document.getElementById('search_form');
    form.submit();
  }

  if (message)
  {
    alert(message);
  }
}

function exec_reserve(p_id)
{
  document.getElementById('p_id').value = p_id; 
  document.getElementById('reserve_form').submit(); 
}

function toggle_continue()
{
  if (find_id('accept').checked)
  {
    show_id('continue');
  }
  else
  {
    hide_id('continue');
  }
}
