/* $Id:$ */
var CURRENT_DATE = new Date();
var CURRENT_YEAR = CURRENT_DATE.getFullYear();
var ONE_DAY_MILLIS = 24 * 60 * 60 * 1000;
var MIN_DATE = new Date(CURRENT_YEAR, 0, 1, 0, 0, 0, 0);
var MAX_DATE = new Date(CURRENT_YEAR+3, 11, 31, 0, 0, 0, 0);

var now, fromDate, toDate, newDate;

function initDates()
{
  now = new Date();
  now.setHours(0, 0, 0, 0);
  fromDate = readDate("stayFrom");
  toDate = readDate("stayTo");
  if (fromDate.getTime() == MIN_DATE.getTime() && toDate.getTime() == MIN_DATE.getTime())
  {
    fromDate = new Date(now.getTime() + ONE_DAY_MILLIS);
    toDate = new Date(fromDate.getTime());
    newDate = new Date(fromDate.getTime());
    adjustDate("stayFrom", fromDate);
    adjustDate("stayTo", toDate);
  }
  else adjustDateDifference("stayLength", fromDate, toDate);
  newDate = new Date(fromDate.getTime());
}

function readDate(fieldNamePrefix)
{
  return new Date(
    document.forms['bookingForm'][fieldNamePrefix + "Year"].selectedIndex + MIN_DATE.getFullYear(),
    document.forms['bookingForm'][fieldNamePrefix + "Month"].selectedIndex,
    document.forms['bookingForm'][fieldNamePrefix + "Day"].selectedIndex + 1,
    0, 0, 0, 0
  );
}

function adjustDate(fieldNamePrefix, date)
{
    document.forms['bookingForm'][fieldNamePrefix + "Day"].selectedIndex = date.getDate() - 1;
    document.forms['bookingForm'][fieldNamePrefix + "Month"].selectedIndex = date.getMonth();
    document.forms['bookingForm'][fieldNamePrefix + "Year"].selectedIndex = date.getFullYear() - MIN_DATE.getFullYear();
}

function adjustDateDifference(fieldName, fromDate, toDate)
{
  document.forms['bookingForm'][fieldName].value = Math.ceil((toDate - fromDate) / ONE_DAY_MILLIS);
}

function newDay(date, index)
{
  newDate.setTime(date.getTime());
  newDate.setDate(index + 1);
  if (testDate(date, newDate, false)) date.setTime(newDate.getTime());
}

function newMonth(date, index)
{
  newDate.setTime(date.getTime());
  newDate.setMonth(index);
  if (testDate(date, newDate, true)) date.setTime(newDate.getTime());
}

function newYear(date, index)
{
  newDate.setTime(date.getTime());
  newDate.setFullYear(index + MIN_DATE.getFullYear());
  if (testDate(date, newDate, true)) date.setTime(newDate.getTime());
}

function testDate(date, newDate, testDays)
{
  if (testDays && date.getDate() != newDate.getDate())
    return false;
  else if (!testDays && date.getMonth() != newDate.getMonth())
    return false;
  else return true;
}

function testDates(unchangedDate)
{
  if (fromDate > toDate)
  {
    if (unchangedDate == toDate)
      toDate.setTime(fromDate.getTime());
    else
      fromDate.setTime(toDate.getTime());
  }
  adjustDate("stayFrom", fromDate);
  adjustDate("stayTo", toDate);
  adjustDateDifference("stayLength", fromDate, toDate);
}

function calculateRooms()
{
  with (document.forms['bookingForm'])
    numberOfRooms.value = roomsSingle.selectedIndex + roomsDouble.selectedIndex;
}

function checkReceipt(field)
{
  if (field.value != "") document.forms['bookingForm'].receipt.checked = true;
}

function checkConfirmation(field)
{
  var index;
  if (field.name == "confirmationEMail")
    index = 0;
  else if (field.name == "confirmationPhone")
    index = 1;
  else index = 2;
  document.forms['bookingForm'].confirmation[index].checked = true;
}

var EMAIL_REGEXP = new RegExp("^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$");
function validateEMailAddress(address)
{
  return address != "" && (typeof(EMAIL_REGEXP.test) == "undefined" || EMAIL_REGEXP.test(address));
}

var NIP_REGEXP = new RegExp("^[0-9]{10}$");
function validateNIP(nip)
{
  return nip != "" && (typeof(NIP_REGEXP.test) == "undefined" || NIP_REGEXP.test(nip));
}

var POCODE_REGEXP = new RegExp("^[0-9]{5}");
function validatePostalCode(code)
{
  return code != "" && (typeof(POCODE_REGEXP.test) == "undefined" || POCODE_REGEXP.test(code));
}

function validateGroup(group)
{
  var valid = false;
  for (var i = 0; i < group.length && !valid; i++) valid |= group[i].checked;
  return valid;
}

function validate(formObj)
{
  var message = null, field = null;
  with (formObj)
  {
    if (name.value == "")
    {
      message = MSG_NAME;//"Prosimy poda� imi� i nazwisko zamawiaj�cego.";
      field = name;
    }

    else if (eMail.value != "" && !validateEMailAddress(eMail.value))
    {
      message = MSG_EMAIL;//"Prosimy poda� prawid�owy adres poczty elektronicznej zamawiaj�cego.";
      field = eMail;
    }
    else if (numberOfGuests.selectedIndex == 0)
    {
      message = MSG_NO_OF_GUESTS;//"Prosimy poda� liczb� os�b.";
      field = numberOfGuests;
    }
    else if (!validateGroup(apartments) && !validateGroup(halls) && numberOfRooms.value == 0)
    {
      message = MSG_WHICH_ROOMS;//"Prosimy poda�, jakie zarezerwowa� apartamenty, sale lub pokoje.";
      field = apartments[0];
    }
    else if (fromDate < now || stayLength.value < 1)
    {
      message = MSG_STAY;//"Prosimy poda� poprawny okres pobytu.";
      field = stayFromDay;
    }
    else if (arrivalTime.selectedIndex == 0)
    {
      message = MSG_ARRIVAL;//"Prosimy poda� planowan� godzin� przybycia.";
      field = arrivalTime;
    }
    else if (!validateGroup(payment))
    {
      message = MSG_PAYMENT;//"Prosimy poda� form� p�atno�ci.";
      field = payment[0];
    }
    else if (receipt.checked && !validateGroup(receiptType1))
    {
      message = MSG_RECEIPT_TYPE;//"Prosimy poda� rodzaj dokumentu sprzeda�y.";
      field = receiptType1[0];
    }
    else if (receipt.checked && !validateGroup(receiptType2))
    {
      message = MSG_RECEIPT_WHO;//"Prosimy poda�, dla kogo ma by� wystawiony dokument sprzeda�y.";
      field = receiptType2[0];
    }
    else if (receipt.checked && receiptName.value == "")
    {
      if (receiptType2[0].checked)
        message = MSG_RECEIPT_FIRM;//"Prosimy poda� nazw� firmy do dokumentu sprzeda�y.";
      else
        message = MSG_RECEIPT_NAME;//"Prosimy poda� imi� i nazwisko do dokumentu sprzeda�y.";
      field = receiptName;
    }
    else if (receipt.checked && receiptAddress.value == "")
    {
      message = MSG_RECEIPT_STREET;//"Prosimy poda� nazw� ulicy do dokumentu sprzeda�y.";
      field = receiptAddress;
    }
    else if (receipt.checked && !validatePostalCode(receiptPostalCode1.value + receiptPostalCode2.value))
    {
      message = MSG_RECEIPT_ZIP;//"Prosimy poda� poprawny kod pocztowy do dokumentu sprzeda�y.";
      field = receiptPostalCode2;
    }
    else if (receipt.checked && receiptCity.value == "")
    {
      message = MSG_RECEIPT_CITY;//"Prosimy poda� nazw� miejscowo�ci do dokumentu sprzeda�y.";
      field = receiptCity;
    }
    else if (receipt.checked &&
              receiptType1.selectedIndex == 0 &&
              !validateNIP(receiptNIP1.value + receiptNIP2.value + receiptNIP3.value + receiptNIP4.value))
    {
      message = MSG_RECEIPT_NIP;//"Prosimy poda� poprawny NIP do dokumentu sprzeda�y.";
      field = receiptNIP4;
    }
	else if (document.getElementById('payment2').checked && cart_name.value=="") { message = MSG_CART_NAME; field = cart_name;}
	else if (document.getElementById('payment2').checked && cart_number.value=="") { message = MSG_CART_NUMBER; field = cart_number;}
	else if (document.getElementById('payment2').checked && cart_expiry_m.value=="") { message = MSG_CART_EXPIRY; field = cart_expiry_m;}
	else if (document.getElementById('payment2').checked && cart_expiry_y.value=="") { message = MSG_CART_EXPIRY; field = cart_expiry_y;}
	
    else if (!validateGroup(confirmation))
    {
      message = MSG_RECEIPT_CONFIRMATION;//"Prosimy poda� spos�b potwierdzenia rezerwacji.";
      field = confirmation[0];
    }
    else if (confirmation[0].checked && !validateEMailAddress(confirmationEMail.value))
    {
      message = MSG_RECEIPT_CONFIRMATION_EMAIL;//"Prosimy poda� poprawny adres e-mail do potwierdzenia rezerwacji.";
      field = confirmationEMail;
    }
    else if (confirmation[1].checked && confirmationPhone.value == "")
    {
      message = MSG_RECEIPT_CONFIRMATION_PHONE;//"Prosimy poda� numer telefonu do potwierdzenia rezerwacji.";
      field = confirmationPhone;
    }
    else if (confirmation[2].checked && confirmationFax.value == "")
    {
      message = MSG_RECEIPT_CONFIRMATION_FAX;//"Prosimy poda� numer faksu do potwierdzenia rezerwacji.";
      field = confirmationFax;
    }
    else return true;

    alert(message);
    field.focus();
    return false;
  }
}

