/* $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 clearText()
{
  if (arguments)
    for (var i = 0 ; i < arguments.length ; i++ )
	  document.forms['bookingForm'][arguments[i]].value = '';
}

function podliczHotel()
{
  var sumaHotel;
  with (document.forms['bookingForm'])
  {
    sumaHotel = (hotelSingle.selectedIndex + hotelDouble.selectedIndex + hotelTriple.selectedIndex + hotelSuperior.selectedIndex + hotelApartment.selectedIndex);
    numberOfHotelRooms.value=sumaHotel;
  }	
}
	
function testAddr(MailAddr)
{
  toTest = new String(MailAddr.value);
  if ( toTest.match("") )
  {
   reg = new RegExp("^[0-9a-zA-Z.]+\@[0-9a-zA-Z]+\.[0-9a-zA-Z.]+$");
//   reg = /^[0-9a-zA-Z.]+\@[0-9a-zA-Z]+\.[0-9a-zA-Z.]+$/;
   blad = ( reg.test(MailAddr.value)==false || MailAddr.value == "");
    if (blad)
    {
	  return false;
    }
    return true;
  }
  return true;
}
	
function checkIt(which)
{
  document.forms['bookingForm'].confirmation[arguments[0]].checked = true;
  document.forms['bookingForm'][arguments[1]].value = '';
  document.forms['bookingForm'][arguments[2]].value = '';
}

function checkMe(what)
{
  if (what.value !="")
    document.forms['bookingForm'].paymentWant.checked = true;
}

function ch(a,b)
{
  var c;
  c = Math.ceil(a/b);
  return(c);
}

function testDays(dayToTest, monthNo)
{
  daysByMonth = new Array(30, 27, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30);
  if ( dayToTest.selectedIndex > daysByMonth[monthNo.selectedIndex] )
    dayToTest.selectedIndex = daysByMonth[monthNo.selectedIndex];
  testPobyt();
}

function testPobyt()
{
with (document.forms['bookingForm'])
{
  var dayFrom, monthFrom, yearFrom, dayTo, monthTo, yearTo, howLong, daysLong;
  yearFrom = 2005 + stayFromYear.selectedIndex;
  monthFrom = stayFromMonth.selectedIndex;
  dayFrom = stayFromDay.selectedIndex;
  yearTo = 2005 + stayToYear.selectedIndex;
  monthTo = stayToMonth.selectedIndex;
  dayTo = stayToDay.selectedIndex;
  if ( yearFrom > yearTo )
  {
    stayToYear.selectedIndex = stayFromYear.selectedIndex;
	testPobyt();
  }
  else if ( yearFrom == yearTo && monthFrom > monthTo)
  {
    stayToMonth.selectedIndex = stayFromMonth.selectedIndex;
    testDays( stayToDay, stayToMonth );
//	testPobyt();
  }
  else if ( yearFrom == yearTo && monthFrom == monthTo && dayFrom > dayTo )
  {
    stayToDay.selectedIndex = stayFromDay.selectedIndex;
	testPobyt();
  }
  else
  {
    dateTo = new Date(yearTo, monthTo, dayTo);
    dateFrom = new Date(yearFrom, monthFrom, dayFrom);
    frs = 24 * 60 * 60 * 1000;
    howLong = dateTo - dateFrom;
    daysLong = ch( howLong, frs );
    howLong = howLong % frs;
    stayLength.value=daysLong;
  }
}
}

function testConfirm(whichText)
{
  with (document.forms['bookingForm'])
  {
    if ( ( confirmation[0].checked == true ) || ( confirmation[1].checked == true && whichText.value != "" ) || ( confirmation[2].checked == true && whichText.value != "" ))
    {
      alert(MSG_RECEIPT_CONFIRMATION);
      confirmation[0].focus();
      return false;
    }
  }
}

function testPayDoc()
{
  with (document.forms['bookingForm'])
  {
    if ( paymentWant.checked == true )
    {
	  if ( ( companyName.value != "" ) && ( streetAddress.value != "" ) && ( postalCode1.value != "" ) && ( postalCode2.value != "" ) && ( city.value != "" ) && ( paymentDoc.value != "" ) ) 
      {
	    if ( paymentFor[0].checked )
		{
		  if ( ( nip1.value != "" ) && ( nip2.value != "" ) && ( nip3.value != "" ) && ( nip4.value != "" ) )
            return true;
		}
        else
		{
		  if ( paymentFor[1].checked )
		    return true;
          else
		    return false;
		}
      }
	  else
	    return false;
    }
    else
	{
	  clearText( "companyName", "streetAddress", "postalCode1", "postalCode2", "city", "nip1", "nip2", "nip3", "nip4");
      return true;
	}
  }
}


function testForm(formObj)
{
  var payChecked = false, confChecked = false;
  with (document.forms['bookingForm'])
  {
    if ( name.value == "" )
	{
	  alert(MSG_NAME);
          name.focus();
	  return false;
	}
	if ( eMail.value != "")
	{
	  if ( !testAddr(eMail) )
	  {
	    alert(MSG_EMAIL);
            eMail.focus();
            return false;
	  }
	}
	if ( numberOfGuests.selectedIndex<1 )
	{
	  alert(MSG_NO_OF_GUESTS);
          numberOfGuests.focus();
	  return false;
	}
	if ( numberOfHotelRooms.value<1) // && hostel.selectedIndex<1 
	{
	  alert(MSG_ROOMS);
          hostel.focus();
	  return false;
	}
	if ( stayLength.value<1 )
	{
	  alert(MSG_STAY);
          stayFromDay.focus();
	  return false;
	}
	if ( arrivalTime.selectedIndex<1 )
	{
	  alert(MSG_ARRIVAL);
          arrivalTime.focus();
	  return false;
	}
	
	for ( var n = 0 ; n < payment.length ; n++ )
	{
	if ( payment[n].checked == true)
	  payChecked = true;    
    }
	
	if ( !payChecked )
	{
	  alert(MSG_PAYMENT);
          payment[0].focus();
	  return false;
    }
	
    for ( var i = 0 ; i < confirmation.length ; i++ )
	{
	if ( confirmation[i].checked == true)
	  confChecked = true;    
    }

    if ( !confChecked )
	{
	  alert(MSG_RECEIPT_CONFIRMATION);
          confirmation[0].focus();
	  return false;
	}
	
	if (document.getElementById('payment2').checked && cart_name.value=="") { message = MSG_CART_NAME; cart_name.focus(); return false;}
	if (document.getElementById('payment2').checked && cart_number.value=="") { message = MSG_CART_NUMBER; cart_number.focus(); return false;}
	if (document.getElementById('payment2').checked && cart_expiry_m.value=="") { message = MSG_CART_EXPIRY; cart_expiry_m.focus(); return false;}
	if (document.getElementById('payment2').checked && cart_expiry_y.value=="") { message = MSG_CART_EXPIRY; cart_expiry_y.focus(); return false;}
	
	if ( confirmation[0].checked == true && !testAddr(confirmByMail) )
    {
	  alert(MSG_RECEIPT_CONFIRMATION_EMAIL);
          confirmByMail.focus();
	  return false;
	}

	if( confirmation[1].checked == true && confirmByPhone.value == "" )
	{
	  alert(MSG_RECEIPT_CONFIRMATION_PHONE);
          confirmByPhone.focus();
	  return false;
	}
	
	if( confirmation[2].checked == true && confirmByFax.value == "" )
	{
	  alert(MSG_RECEIPT_CONFIRMATION_FAX);
          confirmByFax.focus();
	  return false;
	}

	if ( testPayDoc() == false )
	{
	  alert(MSG_RECEIPT);
          companyName.focus();
	  return false;
	}
	
	return true;
  }
}

