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 months = new Array(	'January',    'February', 'March',    'April',
			'May',	      'June',	  'July',     'August',
			'September',  'October',  'November', 'December');

function fill_combos(fc_month_box, fc_day_box, fc_year_box, date, min_date)
{
  days	      = 31;
  curr_year   = 0;
  curr_month  = 0;
  today	      = new Date();

  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******/
  fc_year_box.length = 20;
  for (i = 0; i < 20; i++)
  {
    year_diff   =	1900;

    if (min_date.getYear() > year_diff)
    {
      year_diff = 0;
    }
    year_set		      = min_date.getYear() + year_diff + i;
    //fc_year_box.options[i].value = year_set;
    //fc_year_box.options[i].text  = year_set;

    if (date.getYear() > year_diff)
    {
      year_diff = 0;
    }

    if ((date.getYear() + year_diff) == (year_set))
    {
      curr_year			    = year_set;
      //fc_year_box.options[i].selected  = 1;
    }
  }

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

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

      if (m_val < 10)
      {
	m_val = '0' + m_val;
      }

      //fc_month_box.options[j].value = m_val;

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

  /***********DAYS***********/
  fc_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_val			= i + 1
      //fc_day_box.options[j].text   = d_val;

      if (d_val < 10)
      {
	d_val = '0' + d_val;
      }

      //fc_day_box.options[j].value  = d_val;

      if (date.getDate() == (i + 1))
      {
	//fc_day_box.options[j].selected = 1;
      }
      j = j + 1;
    }
  }
  fc_day_box.length = j;
}

function fill_calendar_combos(year_in,	month_in,   day_in,
			      year_out, month_out,  day_out)
{
  date			= new Date();
  curr_date		= new Date();

  if ((year_in) && (month_in) && (day_in))
  {
    date      = new Date(year_in, (month_in - 1), day_in);
    curr_date = new Date(year_in, (month_in - 1), day_in);
  }

  curr_date.setYear(curr_date.getYear() - 10);
  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);

  if ((year_out) && (month_out) && (day_out))
  {
    date = new Date(year_out, (month_out - 1), day_out);
  }

  fill_combos(month_out_box, day_out_box, year_out_box, date, curr_date);
}

function change_date(which)
{
  min_date  = new Date();
  min_date.setYear(min_date.getYear() - 10);

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

  cd_month_box = document.getElementById('month_'	+ which);
  cd_day_box   = document.getElementById('day_'	+ which);
  cd_year_box  = document.getElementById('year_'	+ which);
  date	    = new Date(cd_year_box.value, (cd_month_box.value - 1), cd_day_box.value);

  fill_combos(cd_month_box, cd_day_box, cd_year_box, date, min_date);

  if ('in' == which)
  {
    min_date  =	date;
    date.setDate(date.getDate() + 1);
    which     =	'out';
    cd_month_box = document.getElementById('month_'+ which);
    cd_day_box   = document.getElementById('day_'	+ which);
    cd_year_box  = document.getElementById('year_'	+ which);
    fill_combos(cd_month_box, cd_day_box, cd_year_box, date, min_date);
  }
}

function bring(pric, uni)
{
  val = 0;

  for (ml = 0; ml < pric.length; ml++)
  {
    val = pric[ml]['id_'+uni];

    if (val)
    {
      ml = pric.length;
    }
  }

  if (!val)
  {
    val = 0;
  }

  return val;
}

/*function bring_price(prices, unity, bp_season)
{
  value = bring(prices, unity + '_' + bp_season);

  if (value)
  {
    unity = 1;
  }
  else
  {
    value = bring(prices, '1_' + bp_season);
  }

  return (unity * value);
}
*/
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_new(month, day)
{
  //alert('Start day: '+month+'/'+day);
  for (kl = 0; kl < Seasons.length; kl++)
  {
    begin_date	  = explode('-', Seasons[kl][2]);
    end_date	    = explode('-', Seasons[kl][3]);

    startmonth    = (2 == Seasons[kl][0] && month != 12)
                  ? 0 : begin_date[1];

    if (startmonth < month && end_date[1] > month)
    {
      return Seasons[kl][0];
    }
    if (startmonth == month && begin_date[2] <= day)
    {
      return Seasons[kl][0];
    }
    if (end_date[1] == month && end_date[2] >= day)
    {
      return Seasons[kl][0];
    }

  }
  alert ("Season not found");
  return 0;
}
/* SAMPLE DATA: (use month: 1-12, day: 1-31)
var Seasons = [[1, 'Low Season', '0000-04-16', '0000-06-30'],
[2, 'High Season', '0000-12-15', '0000-04-15'],
[3, 'Regular Season', '0000-11-01', '0000-12-14'],
[5, 'High Season (01-07/31-08)', '0000-07-01', '0000-08-31'],
[4, 'Low Season (01-09/31-10)', '0000-09-01', '0000-10-31']];*/
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))
    {
      //document.getElementById('season').innerHTML = Seasons[kl][1];
      return Seasons[kl][0];
    }
  }

  return 0;
}

/*
** This function calculates the price for a specific product
** and the number of days for rent
*/
function calculate_price(product_prices, days, date_in, cp_season)
{
  //min_days = 1;
  //max_days = product_prices[1];
  //alert (max_days);
  cp_total    = 0;

/*  if (max_days)
  {
  }
  else
  {
    return cp_total;
  }*/
//TODO
//validacion fechas en caso de que sea mismo dia
/*  if ((min_days <= days) && (days <= max_days))
  {
    cp_total = bring_price(product_prices[0], days, cp_season)
  }
  else*/ if (days < 10)
  {
    //days   = days - max_days;
    //cp_total  = bring(product_prices[0], max_days, cp_season);
    oneday = bring(product_prices[0], '1_' + cp_season);
  }
  else
  {
    //days   = days - max_days;
    //cp_total  = bring_price(product_prices[0], 'max_days', cp_season);
    oneday = bring(product_prices[0], '10_' + cp_season);
  }
  cp_total  = cp_total + (days * oneday);

/*  if (days >= 30)
  {
    oneday   = bring_price(product_prices[0], '1', cp_season);
    amonth   = bring_price(product_prices[0], '30', cp_season);
    nmonths  = Math.floor(days / 30);
    days     = days - (30 * nmonths);
    cp_total    =	(nmonths * amonth) + (days * oneday);
  }*/

  return cp_total;
}

function calculate_days(num)
{
  var Fini = document.getElementById('m_3').value;
  var Ffin = document.getElementById('m_4').value;
  var regex = /(\d+)-(\d+)-(\d+)/;

  if (Fini == "")
  {
   alert ("Please choose Pick up date");
   exit();
  }

  if (Ffin == "")
  {
   alert ("Please choose Return date");
   exit();
  }

  month_ini = Fini.split("-");
  month_fin = Ffin.split("-");

  month_name = new Array ("Jan","Feb","Mar","Apr","May","Jun","jul","Aug",
                           "Sep","Oct","Nov","Dec");

  month_number = new Array ("01","02","03","04","05","06","07","08",
                            "09","10","11","12");

  for(var i=0; i<month_name.length; i++ )
  {
    if (month_name[i] == month_ini[1])
    {
     month_ini.splice(1,1,month_number[i]);
    }
    if (month_name[i] == month_fin[1])
    {
     month_fin.splice(1,1,month_number[i]);
    }
  }

  Fini = month_ini[0]+"-"+month_ini[1]+"-"+month_ini[2];
  Ffin = month_fin[0]+"-"+month_fin[1]+"-"+month_fin[2];

  day_in_box	= Fini.replace(regex,"$3");
  day_out_box	= Ffin.replace(regex,"$3");
  month_in_box	= Fini.replace(regex,"$2");
  month_out_box	= Ffin.replace(regex,"$2");
  year_in_box	= Fini.replace(regex,"$1");
  year_out_box	= Ffin.replace(regex,"$1");

  if ( (day_in_box > day_out_box) && (month_in_box >= month_out_box)
       && (year_in_box >=year_out_box) || ( year_in_box > year_out_box ) )
  {
    alert("Return Date cannot be before Pick up date");
    if (0 != num)
    {
     var m_3 = document.getElementById('m_3');
     m_3.value= "";
     var m_4 = document.getElementById('m_4');
     m_4.value= "";
    }
    exit();
  }

  if ( (day_in_box < day_out_box) && (month_in_box > month_out_box)
       && (year_in_box >=year_out_box) || ( year_in_box > year_out_box ) )
  {
    alert("Return Date cannot be before Pick up date");
    if (0 != num)
    {
     var m_3 = document.getElementById('m_3');
     m_3.value= "";
     var m_4 = document.getElementById('m_4');
     m_4.value= "";
    }
    exit();
  }

/*  if ( year_in_box > year_out_box )
  {
    alert("Pick up date must be smaller than Return Date");
    exit();
  }*/

  //cd_ndays		= document.getElementById('ndays');
  //cd_pay		= document.getElementById('pay');
  //cd_deposit	= document.getElementById('deposit');
  //subtot = document.getElementById('subtotal');
  tot		= document.getElementById('total');
  subtot = document.getElementById('subtot');
  //cd_pay_left	= document.getElementById('pay_left');
  p_id		= document.getElementById('prop').value;
  //p_v = p_id;
  //p_v	= document.getElementById('id_ve').value;

  if (p_id == 'Choose One' || p_id == 'Elige uno')
  {
    alert ("You have to select a vehicle type");
    exit();
  }

  date_in	= new Date(year_in_box, (month_in_box - 1),
			    day_in_box);
  date_out	= new Date(year_out_box, (month_out_box - 1),
			    day_out_box);
  days		= 0;
  //cd_season	= which_season(date_in);
  cd_season	= which_season_new(month_in_box, day_in_box);

  //alert(cd_season + ' 1');

  while ( (date_in.getYear()  != date_out.getYear())  ||
	  (date_in.getMonth() != date_out.getMonth()) ||
	  (date_in.getDate()  != date_out.getDate()))
  {
    date_in.setDate(date_in.getDate() + 1);
    days = days + 1;
    //alert (days);
  }

  hmdays	  = 'days';

  if (1 == days)
  {
    hmdays	  = 'day';
  }
  //cd_ndays.innerHTML = '' + days + ' ' + hmdays;

  // si el calculo de los dias es cero
  // se le da el valor de uno para hacer el calculo
  // con un dia

/*  if (0 < days)
  {
   days = days + 1;
  }*/

  if (0 == days)
  {
   days = 1;
  }

  //season_id();

  //alert (days);
  //tot_days.value   = days;


  /*Dia extra*/
  var hora_ini = document.getElementById('hour_5');
  var hora_fin = document.getElementById('hour_6');
  var ad_day   = document.getElementById('ad_day');
  ad_day.value = parseInt(hora_fin.value)-parseInt(hora_ini.value);
  if (ad_day.value >= 5)
  {
   days += 1;
  }

  var tot_day  = document.getElementById('tot_days');
  tot_day.value = days;

  deposit_value	   = bring(Prices[p_id][0], 'deposit_' + cd_season);
  pay_value	       = calculate_price(Prices[p_id], days, date_in, cd_season);
  total_pay	       = pay_value;// + deposit_value;
  //cd_deposit.innerHTML = '$ ' + deposit_value;
  //cd_pay.innerHTML     = '$ ' + pay_value;
  subtot.value	   = total_pay.toFixed(2);
  tot.value	       = total_pay.toFixed(2);
  //cd_pay_left.innerHTML  = '$ ' + (total_pay - deposit_value);
}

/*function fill_amounts(deposit_value, pay_value, left_value)
{
  fa_pay	    = document.getElementById('pay');
  fa_deposit   = document.getElementById('deposit');
  tot	    = document.getElementById('total');
  fa_pay_left  = document.getElementById('pay_left');
  total_pay = pay_value;// + deposit_value;

  fa_deposit.innerHTML   = '$ ' + deposit_value;
  fa_pay.innerHTML	      = '$ ' + pay_value;
  tot.innerHTML	      = '$ ' + total_pay;
  fa_pay_left.innerHTML  = '$ ' + left_value;
}*/

function calculo_tarifa_menu(num,cont)
{
  var Fini = document.getElementById('f_menu').value;
  var Ffin = document.getElementById('f_menu').value;
  var regex = /(\d+)-(\d+)-(\d+)/;

  month_ini = Fini.split("-");
  month_fin = Ffin.split("-");

  month_name = new Array ("Jan","Feb","Mar","Apr","May","Jun","jul","Aug",
                           "Sep","Oct","Nov","Dec");

  month_number = new Array ("01","02","03","04","05","06","07","08",
                            "09","10","11","12");

  for(var i=0; i<month_name.length; i++ )
  {
    if (month_name[i] == month_ini[1])
    {
     month_ini.splice(1,1,month_number[i]);
    }
    if (month_name[i] == month_fin[1])
    {
     month_fin.splice(1,1,month_number[i]);
    }
  }

  Fini = month_ini[0]+"-"+month_ini[1]+"-"+month_ini[2];
  Ffin = month_fin[0]+"-"+month_fin[1]+"-"+month_fin[2];

  day_in_box	= Fini.replace(regex,"$3");
  day_out_box	= Ffin.replace(regex,"$3");
  month_in_box	= Fini.replace(regex,"$2");
  month_out_box	= Ffin.replace(regex,"$2");
  year_in_box	= Fini.replace(regex,"$1");
  year_out_box	= Ffin.replace(regex,"$1");

  if (cont == 1)
  {
   tot		= document.getElementById('total_1');
  }
  if (cont == 2)
  {
   tot		= document.getElementById('total_2');
  }
  if (cont == 3)
  {
   tot		= document.getElementById('total_3');
  }
  if (cont == 4)
  {
   tot		= document.getElementById('total_4');
  }

  p_id		= num;

  date_in	= new Date(year_in_box, (month_in_box - 1),
			    day_in_box);
  date_out	= new Date(year_out_box, (month_out_box - 1),
			    day_out_box);
  days		= 0;
  //cd_season	= which_season(date_in);
  //alert(month_in_box);
  //alert(day_in_box);
  cd_season	= which_season_new(month_in_box, day_in_box);

  while ( (date_in.getYear()  != date_out.getYear())  ||
	  (date_in.getMonth() != date_out.getMonth()) ||
	  (date_in.getDate()  != date_out.getDate()))
  {
    date_in.setDate(date_in.getDate() + 1);
    days = days + 1;
  }

  hmdays	  = 'days';

  if (1 == days)
  {
    hmdays	  = 'day';
  }


  if (0 == days)
  {
   days = 1;
  }

  deposit_value	   = bring(Prices[p_id][0], 'deposit_' + cd_season);
  pay_value	       = calculate_price_menu(Prices[p_id], days, date_in, cd_season);
  total_pay	       = pay_value;
  tot.value	       = "$" + total_pay;


}

function calculate_price_menu(product_prices, days, date_in, cp_season)
{
  cp_total    = 0;

  oneday = bring(product_prices[0], '2_' + cp_season);

  cp_total  = cp_total + (days * oneday);

  return cp_total;
}

function season_id()
{
  var id_season = document.getElementById('id_season');
  var Fini = document.getElementById('m_3').value;
  var Ffin = document.getElementById('m_4').value;
  var regex = /(\d+)-(\d+)-(\d+)/;

  month_ini = Fini.split("-");
  month_fin = Ffin.split("-");

  month_name = new Array ("Jan","Feb","Mar","Apr","May","Jun","jul","Aug",
                           "Sep","Oct","Nov","Dec");

  month_number = new Array ("01","02","03","04","05","06","07","08",
                            "09","10","11","12");

  for(var i=0; i<month_name.length; i++ )
  {
    if (month_name[i] == month_ini[1])
    {
     month_ini.splice(1,1,month_number[i]);
    }
    if (month_name[i] == month_fin[1])
    {
     month_fin.splice(1,1,month_number[i]);
    }
  }

  Fini = month_ini[0]+"-"+month_ini[1]+"-"+month_ini[2];
  Ffin = month_fin[0]+"-"+month_fin[1]+"-"+month_fin[2];

  day_in_box	= Fini.replace(regex,"$3");
  day_out_box	= Ffin.replace(regex,"$3");
  month_in_box	= Fini.replace(regex,"$2");
  month_out_box	= Ffin.replace(regex,"$2");
  year_in_box	= Fini.replace(regex,"$1");
  year_out_box	= Ffin.replace(regex,"$1");

  date_in	= new Date(year_in_box, (month_in_box - 1),
			    day_in_box);
  //alert(date_in);
  date_out	= new Date(year_out_box, (month_out_box - 1),
			    day_out_box);
  days		= 0;
  //cd_id_season	= which_season(date_in);
  cd_id_season	= which_season_new(month_in_box, day_in_box);
  //alert (cd_id_season);
  id_season.value = cd_id_season;
  //alert(cd_id_season + 'mia');
/*


  while ( (date_in.getYear()  != date_out.getYear())  ||
	  (date_in.getMonth() != date_out.getMonth()) ||
	  (date_in.getDate()  != date_out.getDate()))
  {
    date_in.setDate(date_in.getDate() + 1);
    days = days + 1;
  }*/

  return cd_id_season;
}
