function tab_switch(tab_active, tab_count) {
  for (a=0;a<tab_count;a++) {
    tname1 = 'tab_list' + a;
    tname2 = 'tab_q' + a;
    if (document.getElementById(tname1).className == 'tab_active1') document.getElementById(tname1).className = 'tab_disabled1';
    if (document.getElementById(tname1).className == 'tab_active') document.getElementById(tname1).className = 'tab_disabled';
    document.getElementById(tname2).className = 'tab_qdisabled';
  }
  tname1 = 'tab_list' + tab_active;
  tname2 = 'tab_q' + tab_active;
  if (document.getElementById(tname1).className == 'tab_disabled1') document.getElementById(tname1).className = 'tab_active1'; 
  if (document.getElementById(tname1).className == 'tab_disabled') document.getElementById(tname1).className = 'tab_active';
  document.getElementById(tname2).className = 'tab_qactive';
}

function tab_switch2(tab_active, tab_count, start) {
  for (a=start;a<tab_count;a++) {
    tname1 = 'tab_list2' + a;
    tname2 = 'tab_w' + a;
    if (document.getElementById(tname1).className == 'tab_active1') document.getElementById(tname1).className = 'tab_disabled1';
    if (document.getElementById(tname1).className == 'tab_active') document.getElementById(tname1).className = 'tab_disabled';
    document.getElementById(tname2).className = 'tab_qdisabled';
  }
  tname1 = 'tab_list2' + tab_active;
  tname2 = 'tab_w' + tab_active;
  if (document.getElementById(tname1).className == 'tab_disabled1') document.getElementById(tname1).className = 'tab_active1'; 
  if (document.getElementById(tname1).className == 'tab_disabled') document.getElementById(tname1).className = 'tab_active';
  document.getElementById(tname2).className = 'tab_qactive';
}

function addDaysToDate(date, numOfDays)
{
	return new Date(date.getFullYear(), date.getMonth(), date.getDate()+numOfDays);
}

function parseDate(dateString)
{
	var str = new String(dateString);
	var array = str.split('-');
	if (array.length!=3)
		return null;
	var year = array[0];
	var month = array[1].charAt(0)=='0' ? parseInt(array[1].substr(1, 1)) : parseInt(array[1]);
	var day = array[2].charAt(0)=='0' ? parseInt(array[2].substr(1, 1)) : parseInt(array[2]);
	var date = new Date(year, month-1, day);
	if (isNaN(date))
		return null;
	date.setHours(0);
	date.setMinutes(0);
	date.setSeconds(0);
	date.setMilliseconds(0);
	return date;
}

function formatDate(date)
{
	return date.getFullYear() + "-" + formatDatePart(date.getMonth()+1) +
		"-" + formatDatePart(date.getDate());
}

function formatDatePart(value)
{
	var v = String(value);
	return v.length==1 ? "0"+v : v;
}

function switch_view(divname1, divname2) {
    document.getElementById(divname1).style.display = 'block';
    document.getElementById(divname2).style.display = 'none';
}

function onNumOfNightsChange(przyjazd, wyjazd, numOfNights)
{
	var arrivalDate = parseDate(document.getElementById(przyjazd).value);
	if (arrivalDate==null)
	{
		alert('Nieprawidłowy format Daty Przybycia.\n\nPrawidłowy format daty: YYYY-MM-DD.');
		return false;
	}
	numOfNights = parseInt(numOfNights);
	var newDate = new Date(arrivalDate.getFullYear(), arrivalDate.getMonth(), arrivalDate.getDate()+numOfNights);
	document.getElementById(wyjazd).value = formatDate(newDate);
}

function onArrivalDateChange(wyjz, nocy, arrivalDate, hotel)
{
	var arrivalDate = parseDate(arrivalDate);
	if (arrivalDate==null)
	{
		alert('Nieprawidłowy format Daty Przybycia.\n\nPrawidłowy format daty: YYYY-MM-DD.');
		return false;
	}
	
	var today = new Date();
	today.setHours(0);
	today.setMinutes(0);
	today.setSeconds(0);
	today.setMilliseconds(0);
	
	if (arrivalDate < today)
	{
		alert('Data Przybycia nie może być wcześniejsza od daty dzisiejszej.');
		return false;
	}

	if (hotel)
		document.getElementById(wyjz).value = formatDate(addDaysToDate(arrivalDate, parseInt(document.getElementById(nocy).value)));
}

function onDepartureDateChange(przyj, nocy, departureDate)
{
	var arrivalDate = parseDate(document.getElementById(przyj).value);
	if (arrivalDate==null)
	{
		alert('Nieprawidłowy format Daty Przybycia.\n\nPrawidłowy format daty: YYYY-MM-DD.');	
		return false;
	}
	var depDate = parseDate(departureDate);
	if (depDate==null)
	{
		alert('Nieprawidłowy format Daty Wyjazdu.\n\nPrawidłowy format daty: YYYY-MM-DD.');
		return false;
	}
	if (depDate <= arrivalDate)
	{
		alert('Data Wyjazdu musi być późniejsza od Daty Przybycia.');
		return false;	
	}
	if ((depDate-arrivalDate)/(1000*60*60*24) > 60)
	{
		alert('Data Wyjazdu nie może być późniejsza niż 60 dni od Daty Przybycia.');
		return false;
	}
	document.getElementById(nocy).value = Math.round((depDate.getTime() - arrivalDate.getTime())/(1000*60*60*24));
}

function onNumOfAdultsChange(beds, value)
{
	var count = document.getElementById(beds).options.length;
	var i;
	
	for (i=0; i<count; i++)
	{
		document.getElementById(beds).remove(0);
	}
	
	document.getElementById(beds).selectedIndex = 0;
	
	var start = value > 1 ? value-1 : 1;
	var stop = value > 4 ? 4 : value;
	
	for (i=start; i<=stop; i++)
	{
		var el = document.createElement('OPTION');
		el.text = String(i);
		el.value = i;
		document.getElementById(beds).add(el, document.all ? i : null);
	}
	
	document.getElementById(beds).value = stop;
}

function poszlo($id1, $id2) {
  document.getElementById($id1).style.display = "none";
  document.getElementById($id2).style.display = "block";
  return true;
}

function reprice() {
  it = document.getElementById('items').value;
  pr = 0;
  for(var a=1;a<it;a++) {
      ce = document.getElementById('c_' + a).value;
      cr = document.getElementById('r_' + a).value;
      pl = ce * cr;
      pr = pr + pl;
  }
  document.getElementById('r_koncowa').innerHTML = pr;
}

function otworz(adres, nazwa) {
	noweOkno = window.open(adres, nazwa, 'menubar=no, toolbar=no, location=no, scrollbars=yes, resizable=no, status=no, width=640, height=512')
}

