function daysInMonth(month,year) {
  var dd = new Date(year, month, 0);
  return dd.getDate();
}

function initDates(lang) {
  _initDates('_in', 0, lang);
  _initDates('_out', 1, lang);
}

function _initDates(suffix, dur, lang) {
  if (lang == 'fi') {
    var months = new Array('', 'Tammikuu', 'Helmikuu', 'Maaliskuu',
      'Huhtikuu', 'Toukokuu', 'Kesäkuu', 'Heinäkuu', 'Elokuu',
      'Syyskuu', 'Lokakuu', 'Marraskuu', 'Joulukuu');
  } else if (lang == 'en') {
    var months = new Array('', 'January', 'February', 'March', 'April',
      'May', 'June', 'July', 'August', 'September', 'October',
      'November', 'December');
  } else if (lang == 'sv') {
    var months = new Array('', 'Januari', 'Februari', 'Mars', 'April',
      'Maj', 'Juni', 'Juli', 'Augusti', 'September', 'Oktober',
      'November', 'December');
  } else if (lang == 'de') {
    var months = new Array('', 'Januar', 'Februar', 'März', 'April',
      'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober',
      'November', 'Dezember');
  }
  var curdate = new Date();
  var curyear = curdate.getFullYear();  
  var curmonth = curdate.getMonth() + 1;
  var curday = curdate.getDate();

  var monthsel = document.getElementById('m' + suffix);
  var i = 0;
  var j = 0;
  for (i = 0; i < 24; i++) {
    j = Math.floor((i + curmonth - 1) / 12);
    monthsel.options[i] = new Option(months[(curmonth + i) - (j * 12)] + ' ' + (curyear + j), (curmonth + i - (j * 12)) + '.' + (curyear + j));
  }
  monthsel.selectedIndex = 0;

  var daysel = document.getElementById('d' + suffix);
  days = daysInMonth(curmonth, curyear) + 1;
  for (i = 1; i < days; i++) {
    daysel.options[i - 1] = new Option(i, i);
  }
  if (dur == 0) {
    daysel.selectedIndex = curday - 1;
  } else {
    if (curday + dur < days) {
      daysel.selectedIndex = curday + dur - 1;
    } else {
      monthsel.selectedIndex++;
      monthChanged(suffix);
      daysel.selectedIndex = curday + dur - days;
    }
  }
}

function monthChanged(suffix) {
  var daysel = document.getElementById('d' + suffix);
  var monthsel = document.getElementById('m' + suffix);
  var m = monthsel[monthsel.selectedIndex].value.split('.');
  var days = daysInMonth(m[0], m[1]);
  var dlen = daysel.length;
  if (days < dlen) {
    selid = daysel.selectedIndex;
    while (days < daysel.length) {
      daysel.remove(daysel.length - 1);
    }
    if (selid > daysel.length - 1) {
      daysel.selectedIndex = daysel.length - 1;
    }
  } else if (days > dlen) {
    while (days > daysel.length) {
      dlen = daysel.length;
      daysel[dlen] = new Option(parseInt(daysel[dlen - 1].text) + 1, parseInt(daysel[dlen - 1].value) + 1);
    }
  }
}

function pad(int) {
  return (int < 10) ? '0' + int : int;
}

function checkXmasRange() {
  var daysel = document.getElementById('d_in');
  var monthsel = document.getElementById('m_in');
  var m = monthsel[monthsel.selectedIndex].value.split('.');
  var date = '' + m[1] + pad(m[0]) + pad(daysel.selectedIndex + 1);
  var xmas = (date > 20081106 && date < 20081221);
  var xmasinput = document.getElementById('xmas')
  oldxmas = xmasinput.value;
  var rtypesel = document.getElementById('rtype');
  if (oldxmas == 0 && xmas == true) {
    xmasinput.value = 1
    rtypesel.options[0].text = '2hh (158 \u20AC)';
    rtypesel.options[1].text = '1hh (123 \u20AC)';
  } else if (oldxmas == 1 && xmas == false) {
    xmasinput.value = 0
    rtypesel.options[0].text = '2hh (126 \u20AC)';
    rtypesel.options[1].text = '1hh (96 \u20AC)';
  }
}

function validateRes() {
  var i = 0;
  var monthin = document.getElementById('m_in');
  var minindex = monthin.selectedIndex;
  var dayin = document.getElementById('d_in');
  var curdate = new Date();
  var curday = curdate.getDate();
  if (minindex == 0 && dayin.selectedIndex < curday - 1) {
    dayin.style.backgroundColor="#fbb";
    monthin.style.backgroundColor="#fbb";
    i++;
  } else {
    dayin.style.backgroundColor="#fff";
    monthin.style.backgroundColor="#fff";
  }

  var monthout = document.getElementById('m_out');
  var dayout = document.getElementById('d_out');
  var moutindex = monthout.selectedIndex;
  if (moutindex < minindex || (moutindex == minindex && dayout.selectedIndex <= dayin.selectedIndex)) {
    dayout.style.backgroundColor="#fbb";
    monthout.style.backgroundColor="#fbb";
    i++;
  } else {
    dayout.style.backgroundColor="#fff";
    monthout.style.backgroundColor="#fff";
  }

  fields = new Array('name', 'addr', 'zip', 'phone');
  var j = 0;
  for (j = 0; j < fields.length; j++) {
    var input = document.getElementById(fields[j]);
    if (input.value == '') {
      input.style.backgroundColor="#fbb";
      i++;
    } else {
      input.style.backgroundColor="#fff";
    }
  }

  var input = document.getElementById('email');
  var at = input.value.indexOf('@', 1);
  var dot = input.value.lastIndexOf('.')
  if (at < 1 || at == input.value.length - 1 ||
      dot - at < 2 || dot == input.value.length - 1) {
    input.style.backgroundColor="#fbb";
    i++;
  } else {
    input.style.backgroundColor="#fff";
  }

  if (i == 0) {
    return true;
  } else {
    $('#error').show();
    return false;
  }
}

$(function() {
  $('#sendap').click(function() {
    var i = 0;
    var monthin = document.getElementById('m_in');
    var minindex = monthin.selectedIndex;
    var dayin = document.getElementById('d_in');
    var curdate = new Date();
    var curday = curdate.getDate();
    if (minindex == 0 && dayin.selectedIndex < curday - 1) {
      dayin.style.backgroundColor="#fbb";
      monthin.style.backgroundColor="#fbb";
      i++;
    } else {
      dayin.style.backgroundColor="#fff";
      monthin.style.backgroundColor="#fff";
    }

    fields = new Array('name', 'phone');
    var j = 0;
    for (j = 0; j < fields.length; j++) {
      var input = document.getElementById(fields[j]);
      if (input.value == '') {
        input.style.backgroundColor="#fbb";
        i++;
      } else {
        input.style.backgroundColor="#fff";
      }
    }

    var input = document.getElementById('email');
    var at = input.value.indexOf('@');
    var dot = input.value.lastIndexOf('.')
    if (at < 1 || at == input.value.length - 1 ||
        dot - at < 2 || dot == input.value.length - 1) {
      input.style.backgroundColor="#fbb";
      i++;
    } else {
      input.style.backgroundColor="#fff";
    }

    if (i > 0) {
      return false;
    }

    $('#apform').fadeOut(function () {
      $('#apsending').fadeIn(function () {
        $.post("reserveap.php", $('#apform').serialize(), function(data) {
          $('#apsent').html(data + '<p><br /><input type="button" value="Sulje" class="closeDOMWindow" /></p>');
          $('.closeDOMWindow').closeDOMWindow({eventType:'click'});
          $('#apsending').fadeOut(function() {
            $('#apsent').fadeIn('slow');
          });
        });
      });
    });
    return false;
  });
});

function APDateChanged() {
  var d = $('#d_in').val();
  var m = $('#m_in').val();
  if (m == '7.2010' && (d == '16' || d == '17')) {
    $('#rt_1').text('2hh (136 \u20AC)');
    $('#rt_2').text('1hh (116 \u20AC)');
    $('#apwarn').text('Twin City Festivals: Lisätietoa vahvistuksessa');
    $('#apwarn').css('visibility', 'visible');
  } else {
    $('#rt_1').text('2hh (130 \u20AC)');
    $('#rt_2').text('1hh (98 \u20AC)');
    $('#apwarn').css('visibility', 'hidden')
  }
  return false;
}
