function startLoad() {
	document.getElementById('calendar-loading-indicator').style.display = 'block';
}
function stopLoad() {
	document.getElementById('calendar-loading-indicator').style.display = 'block';
}

CURRENTLY_LOADING = 0;
//AM ?adowanie opcji do selecta
function load_options(elementId){
  
  element = document.getElementById('elementId');
  var currently_loading = ++CURRENTLY_LOADING;
  
  function success(request){
    if(!(currently_loading == CURRENTLY_LOADING)) return;
    element.innerHTML = request.responseText;
    alert(request.responseText);
    stopLoad();
  }
  
  function failure(request){
    alert("Nie uda?o si? pod??czy? do serwera. Prosimy spr?bowa? p??niej");
    stopLoad();
  }
  switch (elementId) {
  	  case 'powiat':
  	  	  parentId = document.getElementById('woj').value;
  	  	  break;
  	  case 'osrodek':
  	  	  parentId = document.getElementById('powiat').value;
  	  	  break;
  	  case 'kurs':
  	  	  parentId = document.getElementById('osrodek').value;
  	  	  break;
  	  case 'trener':
  	  	  parentId = document.getElementById('kurs').value;
  	  	  break;
  } 
 
  startLoad();
  new Ajax.Request(webRoot + '/index.php?module=search&_action=load&type=' + elementId + '&parentId=' + parentId, {onSuccess: success, onFailure: failure, method: "post", parameters: params})
}




function getEntry(obj, entry) {
	return entry;
}

function onLoadPage() {
	if (document.getElementsByName('osrodek_nazwa').length > 0) {
		new Ajax.Autocompleter(document.getElementsByName('osrodek_nazwa')[0], 
			document.getElementById('autocomplete_div'), webRoot + '/index.php?module=search&_action=load&type=osrodek_nazwa', { callback: getEntry });
	}
	if (document.getElementsByName('kurs_nazwa').length > 0) {
		new Ajax.Autocompleter(document.getElementsByName('kurs_nazwa')[0], 
			'autocomplete_div', webRoot + '/index.php?module=search&_action=load&type=kurs_nazwa', { callback: getEntry });
	}
}
window.onload = onLoadPage;

function max_ilosc_kursow(projekt, nazwa_kursu) {
	switch (projekt) {
		case 21: return 3;
		case 23: return 1;
		case 24: return 1;
		case 42: return 5
	}
}
function DodajKurs(projekt, kurs, nazwa_kursu)
{		
	wk = getCookie('wyb_' + projekt + '[' + kurs + ']');
	if (wk == 1)
	{
		alert('Ten kurs znajduje si? ju? na Pa?stwa li?cie.');
		return;
	}

	ik = getCookie('ile_w_koszyku_' + projekt + '');
	if (ik == null) ik = 0;
	ile_w_koszyku = ik;
	if (ik >= max_ilosc_kursow(projekt))
	{
		alert('Osi?gni?to maksymaln? ilo?? zam?wionych kurs?w !');
		return;	
	}	
	if (ile_w_koszyku >= max_ilosc_kursow(projekt))
	{
		alert('Osi?gni?to maksymaln? ilo?? zam?wionych kurs?w !');
		return;
	}
	newi = 0;
	minik = getCookie('minik_' + projekt + '');
	minin = getCookie('minin_' + projekt + '');
	if (wyb != null)
	for (k=0; k < wyb.length; k++)
	{
		if (wyb[k] == '' + kurs + '')
		{
			alert('Ten kurs znajduje si? ju? na Pa?stwa li?cie.');
			return;
		}
		newi++;
	}
	if (wyb == null)
		var wyb = new Array();
	wyb[newi] = '' + kurs + '';
	minik+=','+wyb[newi];
	minin+='|'+nazwa_kursu;
	ile_w_koszyku++;
	document.cookie = 'minik_' + projekt + '='+minik+';path=/'
	document.cookie = 'minin_' + projekt + '='+minin+';path=/'
	document.cookie = 'ile_w_koszyku_' + projekt + '='+ile_w_koszyku+';path=/'
	document.cookie = 'wyb_' + projekt + '[' + kurs + ']=1;path=/'
	document.cookie = 'KOSZYK_' + projekt + '[' + kurs + ']=1;path=/'
	document.cookie = 'ADD_LULU' + projekt + '[' + kurs + ']=1;path=/'
	document.cookie = 'ADD_PAPU' + projekt + '[' + kurs + ']=1;path=/'
	alert('Dodano 1 pozycj? do Twojej listy kurs?w. \nAby dokona? zam?wienia - kliknij na link "MOJE KURSY".');
	cartUpdate();
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupCalendar.showAtElement(el, "Br");        // show the calendar

  return false;
}

// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked)
    // if we add this call we close the calendar on single-click.
    // just to exemplify both cases, we are using this only for the 1st
    // and the 3rd field, while 2nd and 4th will still require double-click.
    cal.callCloseHandler();
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar = null;
}
