function setHeight(left,right) 
{
	if (document.getElementById(left)) 
	{
		var lh = document.getElementById(left).offsetHeight;
		var rh = document.getElementById(right).offsetHeight;
		var nh = Math.max(lh, rh); 
		document.getElementById(left).style.height = nh+"px";
		document.getElementById(right).style.height = nh+"px";
	}
}

function setHeight2(left,center,right) 
{
	function sortNum(a,b) {	return b-a } 
	
	if (document.getElementById(left))
	{
		var obj=new Array(3);
		var option=[left,center,right];
		for(var i=0; i<option.length; i++) 
		{
			document.getElementById(option[i]).style.height="auto";
			obj[i]=document.getElementById(option[i]).offsetHeight;
			nh=obj.sort(sortNum);
		} 
		nh1=nh.splice(1,2);
		for(var i=0; i<option.length; i++) document.getElementById(option[i]).style.height=nh+"px";
	}
}

function kolumny()
{
	if (document.getElementById("booking_form") == null) window.onload=function() { setHeight('menu_left','main'); }
}
	
function isDate(dateStr)
{
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat);

	if (matchArray == null)	return false;

	month = matchArray[3];
	day = matchArray[1];
	year = matchArray[5];

	if (month < 1 || month > 12) return false;

	if (day < 1 || day > 31) return false;

	if ((month==4 || month==6 || month==9 || month==11) && day==31) return false;

	if (month == 2) 
	{ 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		
		if (day > 29 || (day==29 && !isleap))
		{
			return false;
		}
	}
	return true;
}

function isEmail(email)
{
	validRegExp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	if (email.search(validRegExp) == -1)
	{
		return false;
	}
	return true;
}

function isTel(tel)
{
	validRegExp = new RegExp(/[^0-9\+\ ]/g);
	
	if (tel.search(validRegExp) == -1)
	{
		return true;
	}
	return false;
}

function isNIP(nip)
{
	var myRegExp = new RegExp(/[^0-9]/g);
	var czysty = nip.replace(myRegExp, "");
	if (czysty.length==10) return true;
	else return false;
}

function isCode(code)
{
	validRegExp = new RegExp(/[^0-9\-\ ]/g);
	
	if (code.search(validRegExp) == -1)
	{
		return true;
	}
	return false;
}

function dateCompare(date1,date2)
{
	d1 = new Date(date1.split('-')[2],date1.split('-')[1],date1.split('-')[0]);
	d2 = new Date(date2.split('-')[2],date2.split('-')[1],date2.split('-')[0]);

	if (d1.getTime()<=d2.getTime()) return true;
	else return false;
}

function koduj(string)
{
	return encodeURIComponent(string);
}

function ajax (url,id)
{
	var req;

	if(window.XMLHttpRequest) 
	{
		req = new XMLHttpRequest();
	}
	else try 
	{
		req = new ActiveXObject('Msxml2.XMLHTTP');
	} catch(e) 
	{
		try 
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) 
		{
			req = false;
		}
	}

	if(req)
	{
		req.open('POST', url, true);
		req.onreadystatechange = function()
		{
			if(req.readyState == 4) {
				if(req.status == 200) {
					document.getElementById(id).innerHTML = req.responseText;
					//document.getElementById(obiekt).value = "2"
				}
			} 
		}
		req.send('');
	}
	else return true;
	return false;
}

function szukaj(id)
{
	var zmienne = 'panstwo='+koduj(document.search.panstwo.value)+'&miasto='+koduj(document.search.miasto.value)+'&obiekt='+koduj(document.search.obiekt.value)+'&standard='+koduj(document.search.standard.value);
	ajax ('/components/com_plazuj/functions/szukaj.php?'+zmienne,'pola');
	
	if (id!='')
	{	
		if (id=='obiekt') document.getElementById('standard').value=0;
		if (id=='standard' && document.getElementById('obiekt').value!=0) document.getElementById('standard').value=0;
		
		document.getElementById(id).focus();
	}
}

function wyszukiwarka(panstwo,miasto,obiekt,standard)
{
	if (panstwo=="") panstwo=0;
	if (miasto=="") miasto=0;
	if (obiekt=="") obiekt=0;
	if (standard=="") standard=0;
	
	var req;
	
	document.getElementById('panstwo').value=panstwo;
	document.getElementById('miasto').value=miasto;
	document.getElementById('obiekt').value=obiekt;
	document.getElementById('standard').value=standard;
	
	var zmienne = 'panstwo='+koduj(document.search.panstwo.value)+'&miasto='+koduj(miasto)+'&obiekt='+koduj(document.search.obiekt.value)+'&standard='+koduj(document.search.standard.value);
	var url = '/components/com_plazuj/functions/szukaj.php?'+zmienne;
	var id ='pola';

	if(window.XMLHttpRequest) 
	{
		req = new XMLHttpRequest();
	}
	else try 
	{
		req = new ActiveXObject('Msxml2.XMLHTTP');
	} catch(e) 
	{
		try 
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) 
		{
			req = false;
		}
	}

	if(req)
	{
		req.open('POST', url, true);
		req.onreadystatechange = function()
		{
			if(req.readyState == 4) {
				if(req.status == 200) {
					document.getElementById(id).innerHTML = req.responseText;
					document.getElementById('panstwo').value=panstwo;
					document.getElementById('miasto').value=miasto;
					document.getElementById('obiekt').value=obiekt;
					document.getElementById('standard').value=standard;
				}
			} 
		}
		req.send('');
	}
	else return true;
	return false;
}

function wyniki()
{
	var zmienne = 'panstwo='+koduj(document.search.panstwo.value)+'&miasto='+koduj(document.search.miasto.value)+'&obiekt='+koduj(document.search.obiekt.value)+'&standard='+koduj(document.search.standard.value);
	
	var url = '/components/com_plazuj/functions/wyniki.php?'+zmienne;
	
	var req;

	if(window.XMLHttpRequest) 
	{
		req = new XMLHttpRequest();
	}
	else try 
	{
		req = new ActiveXObject('Msxml2.XMLHTTP');
	} catch(e) 
	{
		try 
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) 
		{
			req = false;
		}
	}

	if(req)
	{
		req.open('POST', url, true);
		req.onreadystatechange = function()
		{
			if(req.readyState == 4) {
				if(req.status == 200) {
					var link = req.responseText;
					window.location.replace(link);
				}
			} 
		}
		req.send('');
	}

}

function regulamin()
{
	window.open('/regulamin.html', 'win2', 'status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=640,height=480,directories=no,location=no');
}

function sortuj(id)
{		
	if (document.getElementById('sort_order').value=='none' && document.getElementById('sort_type').value==id) 
	{
		document.getElementById('sort_order').value='asc';
	}
	
	else if (document.getElementById('sort_order').value=='asc' && document.getElementById('sort_type').value==id) 
	{
	document.getElementById('sort_order').value='desc';
	}
	
	else if (document.getElementById('sort_order').value=='desc' && document.getElementById('sort_type').value==id)
	{
		document.getElementById('sort_order').value='asc';
	}
	
	if (document.getElementById('sort_type').value!=id) 
	{
		document.getElementById('sort_order').value='asc';
	}
	
	document.getElementById('sort_type').value=id;
	
	document.ordering.submit();
}

function slide(id)
{
	$(id).slideToggle("fast");
}

function zamknij()
{
	window.location.replace("/");
}

function zero(liczba) {
    return liczba=(liczba < 10)? "0"+liczba : liczba;
}

// JS Calendar
var calendar = null; // remember the calendar object so that we reuse
// it and avoid creating another

// This function gets called when an end-user clicks on some date
function selected(cal, date) {
	cal.sel.value = date; // just update the value of the input field
	
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = date.match(datePat);

	if (matchArray == null)	return false;

	var pobyt_do = new Date();
	
	pobyt_do.setYear(matchArray[5]);
	pobyt_do.setMonth(matchArray[3]-1);
	pobyt_do.setDate(matchArray[1]);
	
	pobyt_do.setDate(pobyt_do.getDate()+7);
	
	new_date = zero(pobyt_do.getDate())+'-'+zero(pobyt_do.getMonth()+1)+'-'+pobyt_do.getFullYear();
	 
	if ( cal.sel.name == "pobyt_od" ) 
	{
		if (document.getElementById('kalkulacja')) document.kalkulacja.pobyt_do.value = new_date;
		else document.booking.pobyt_do.value = new_date;
	}
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks the "Close" (X) button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
	cal.hide();			// hide the calendar

	// don't check mousedown on document anymore (used to be able to hide the
	// calendar when someone clicks outside it, see the showCalendar function).
	Calendar.removeEvent(document, "mousedown", checkCalendar);
}

// This gets called when the user presses a mouse button anywhere in the
// document, if the calendar is shown.  If the click was outside the open
// calendar this function closes it.
function checkCalendar(ev) {
	var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
	for (; el != null; el = el.parentNode)
	// FIXME: allow end-user to click some link without closing the
	// calendar.  Good to see real-time stylesheet change :)
	if (el == calendar.element || el.tagName == "A") break;
	if (el == null) {
		// calls closeHandler which should hide the calendar.
		calendar.callCloseHandler(); Calendar.stopEvent(ev);
	}
}

// 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) {
	var el = document.getElementById(id);
	if (calendar != null) {
		// we already have one created, so just update it.
		calendar.hide();		// hide the existing calendar
		calendar.parseDate(el.value); // set it to a new date
	} else {
		// first-time call, create the calendar
		var cal = new Calendar(true, null, selected, closeHandler);
		calendar = cal;		// remember the calendar in the global
		cal.setRange(1900, 2070);	// min/max year allowed
		calendar.create();		// create a popup calendar
		calendar.parseDate(el.value); // set it to a new date
	}
	calendar.sel = el;		// inform it about the input field in use
	calendar.showAtElement(el);	// show the calendar next to the input field

	// catch mousedown on the document
	Calendar.addEvent(document, "mousedown", checkCalendar);
	return false;
}

function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

//RTrim(string) : Returns a copy of a string without trailing spaces.
function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

// Trim(string) : Returns a copy of a string without leading or trailing spaces
function trim(str) {
   return rtrim(ltrim(str));
}

function getElementByName( f, name ) {
	if (f.elements) {
		for (i=0, n=f.elements.length; i < n; i++) {
			if (f.elements[i].name == name) {
				return f.elements[i];
			}
		}
	}
	return null;
}
