function makeRequest(url,target) {
	var httpRequest;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
			// See note below about this line
		}
	}
	else if (window.ActiveXObject) { // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
					   try {
							httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
						   }
						 catch (e) {}
					  }
								   }

	if (!httpRequest) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}

	var cel = document.getElementById(target);
	cel.innerHTML = '<center><img src="/img/loading.gif"></center>';
	httpRequest.open('GET', url, true);
	var requestTimer = setTimeout(function() {
       httpRequest.abort();
	   alert('request timeout');
       // Handle timeout situation, e.g. Retry or inform user.
     }, 10000); // max 10 sec
	httpRequest.onreadystatechange = function()
		{
			var cel = document.getElementById(target);
			if (httpRequest.readyState == 4) {
				clearTimeout(requestTimer);
				if (httpRequest.status == 200) {

					// alert(httpRequest.responseText);
					req = httpRequest.responseText;
					cel.innerHTML = req;
				} else {
					alert('There was a problem with the request.');
				}
			}
		}

	httpRequest.send('');

}

function callprice(hotelid) {
	//összerakja a paraméterlistát és meghívja az ár kiszámításához a php scriptet
	var x = document.getElementById('order_'+hotelid);
	var callstr = new String;
	for (var i=0;i<x.length;i++)
	{
		if ((x.elements[i].value != 0) && (x.elements[i].name != 'hrtid') && (x.elements[i].name.search('maxpot_') == -1) && (x.elements[i].name.search('ipot_') == -1)  && (x.elements[i].name.search('agy_'))) {
			callstr=callstr + x.elements[i].name+'='+x.elements[i].value +'&';
		}
	}
//    alert('TESZT');
	makeRequest('/lib/calcprice.php?from=search&'+callstr,'ar_'+hotelid);

}

function callcsomag(hotelid) {
	//összerakja a paraméterlistát és meghívja az ár kiszámításához a php scriptet
	var x = document.getElementById('order_'+hotelid);
	var callstr = new String;
	for (var i=0;i<x.length;i++)
	{
		if ((x.elements[i].value != 0) && (x.elements[i].name != 'hrtid') && (x.elements[i].name.search('maxpot_') == -1) && (x.elements[i].name.search('ipot_') == -1)  && (x.elements[i].name.search('agy_'))) {
			callstr=callstr + x.elements[i].name+'='+x.elements[i].value +'&';
		}
	}
//    alert('TESZT');
	makeRequest('/lib/calcprice.php?from=search&csomag=ok&'+callstr,'packagesend_'+hotelid);

}


function vendeg(hotelid) {
	//kiszámolja a szobák kapacitása és a kiválasztott pótágyak hány vendégnek elegendőek
	var ossz = 0;
	var x = document.getElementById('order_'+hotelid);
//	var nr = x.elements['nr'].value;
	var hr = x.elements['hrtid'];
//	var mar = 0;

if (hr.length > 0) {
	var i = 0;
	for (i=0;i<hr.length;i=i+1){

		var hrtid = hr[i].value;
		var szoba = x.elements['szoba_'+hrtid].value;
		var agy = x.elements['agy_'+hrtid].value;
		a = parseInt(szoba,10) * parseInt(agy,10);
		ossz = ossz + parseInt(a,10);
		//maradék
//		mar = mar + parseInt(nr % a);

		if ((x.elements['pot_'+hrtid])&&(x.elements['pot_'+hrtid].length > 0)) {

			var pot = x.elements['pot_'+hrtid].value;
			ossz = ossz + parseInt(pot,10);
		}
		if ((x.elements['ipot_'+hrtid])&&(x.elements['ipot_'+hrtid].length > 0)) {
			var ipot = x.elements['ipot_'+hrtid].value;
			ossz = ossz + parseInt(ipot,10);
		}
	}
}
else {

		var hrtid = hr.value;
		var szoba = x.elements['szoba_'+hrtid].value;
		var agy = x.elements['agy_'+hrtid].value;
		a = parseInt(szoba,10) * parseInt(agy,10);
		ossz = ossz + parseInt(a,10);
		//maradék
//		mar = mar + parseInt(nr % a);

		if ((x.elements['pot_'+hrtid])&&(x.elements['pot_'+hrtid].length > 0)) {

			var pot = x.elements['pot_'+hrtid].value;
			ossz = ossz + parseInt(pot,10);
		}
		if ((x.elements['ipot_'+hrtid])&&(x.elements['ipot_'+hrtid].length > 0)) {
			var ipot = x.elements['ipot_'+hrtid].value;
			ossz = ossz + parseInt(ipot,10);
		}


}
	var resp = '';

//	if (ossz < nr) {
//		resp = '<span style="color: red;">' + ossz + ' / ' + nr +' (' + (ossz-nr) + ')</span>';
//	}
//	else {
//		if (ossz > nr) {
//			resp = '<span style="color: green;">' + ossz + ' / ' + nr + ' ('+ (ossz-nr) + ')</span>';
//		}
//		else {
//			resp = '<span style="color: green;">' + ossz + ' / '+nr+'</span>';
//		}
//	}

	resp = '<span style="color: green;">' + ossz + '</span><input type="hidden" name="nr" value="' + ossz + '">';
//	resp = '<span style="color: green;">' + ossz + '</span>';

	cel = document.getElementById('emberek_'+hotelid);
	cel.innerHTML = resp;

	if (ossz > 0) {
		callprice(hotelid);
	}
	else {
		cel = document.getElementById('ar_'+hotelid);
		cel.innerHTML = '';
	}

}
function chcsomag(hotelid) {
	var x = document.getElementById('order_'+hotelid);
	var vis = document.getElementById('kuld_'+hotelid).style;
	var vis2 = document.getElementById('packagesend_'+hotelid).style;
	var vis3 = document.getElementById('ar_'+hotelid).style;
    var vis4 = document.getElementById('potagykiir_'+hotelid).style;
	var csomagpipa = false;
    for (var i=0; i < x.length; i++) {
      fldObj = x.elements[i];
      if (fldObj.name.indexOf('csomag_'+hotelid) != -1)  {
        if(fldObj.checked) {
          csomagpipa= true;
        }
      }
    }


    if (csomagpipa){
        for (var i=0; i < x.length; i++) {
            fldObj = x.elements[i];
            if (fldObj.name.indexOf('szoba_') != -1)  {
                fldObj.options[0].selected=true;
                vendeg(hotelid);
            }
        }
        x.elements['potagy'].checked= false;
        callcsomag(hotelid);
        vis2.display = 'block';
		vis.display = 'none';
		vis3.display = 'none';
		vis4.display = 'none';
    }else{
        vis2.display = 'none';
//		vis.display = 'block';
		vis3.display = 'block';
		document.getElementById('packagesend_'+hotelid).innerHTML='';
    }


}
function SelectNone(hotelid){
	var x = document.getElementById('order_'+hotelid);
	x.selectedcsomag.value = '0';
    for (var i=0; i < x.length; i++) {
        fldObj = x.elements[i];
        if (fldObj.name.indexOf('csomag_'+hotelid) != -1)  {
            if(fldObj.checked) {
                fldObj.checked = false;
            }
        }
    }
}

function SingleSelect(hotelid,current){
	var x = document.getElementById('order_'+hotelid);
    var seged = current.checked;
	x.selectedcsomag.value = '0';
    for (var i=0; i < x.length; i++) {
        fldObj = x.elements[i];
        if (fldObj.name.indexOf('csomag_'+hotelid) != -1)  {
            if(fldObj.checked) {
                fldObj.checked = false;
            }
        }
    }
    if (seged==true){
        current.checked = true;
		x.selectedcsomag.value = current.value;
    }
}


function chszoba(hrtid,hotelid) {
	// szobák számának változását kezeli, pótágyak és ingyenpótágyak számát állítja

	var x = document.getElementById('order_'+hotelid);
//	var nr = x.elements['nr'].value;
	var szoba = x.elements['szoba_'+hrtid].value;
	var agy = x.elements['agy_'+hrtid].value;
	var mpot = x.elements['maxpot_'+hrtid].value;
//    alert(szoba + ' ' + agy + ' ' + mpot);
	// új pótágyak

	if (x.elements['pot_'+hrtid]) {
		pot = x.elements['pot_'+hrtid].length;
		maxpot = szoba * mpot;
		z=document.getElementById('pot_'+hrtid);
		i = 0;

		for (i=pot;i>=0;i=i-1) {
				z.remove(i);
		}

		for (i=0;i<=maxpot;i=i+1) {
			y=document.createElement('option');
			y.text = i;
			y.value = i;
			try {
				z.add(y,null); // standards compliant
			}
			catch(ex) {
				z.add(y); // IE only
			}
		}

	}

	if (x.elements['ipot_'+hrtid]) {

		ipot = x.elements['ipot_'+hrtid].length;
		z=document.getElementById('ipot_'+hrtid);
		i = 0;
		maxpot = szoba * mpot;

		for (i=ipot;i>=0;i=i-1) {
				z.remove(i);
		}

		for (i=0;i<=maxpot;i=i+1) {
			y=document.createElement('option');
			y.text = i;
			y.value = i;
			try {
				z.add(y,null); // standards compliant
			}
			catch(ex) {
				z.add(y); // IE only
			}
		}
	}

	vendeg(hotelid);

}

function chpot(hotelid) {
	//pótágypipa fuggvenyeben allitja, hogy kiirjuk -e a potagy szoveget
	var x = document.getElementById('order_'+hotelid);
    var potagypipa = x.elements['potagy'];
	var vis = document.getElementById('potagykiir_'+hotelid).style;
	var pipa = false;
    if(potagypipa.checked) {
        pipa= true;
    }

    if (pipa){
		vis.display = 'block';
    }else{
        vis.display = 'none';
    }
}

function chingyenpot(hrtid,hotelid) {
	//ingyen pótágyakat számának változását kezeli, pótágyak számát állítja

	var x = document.getElementById('order_'+hotelid);
	var pot = x.elements['pot_'+hrtid].value;
	var ingyenpot = x.elements['ipot_'+hrtid].value
	var maxpot = x.elements['pot_'+hrtid].length;

	// ahol van ingyen pótágy, ott mindig van pótágy is, nem kell ellenőrizni

	if ((pot+ingyenpot)>maxpot) {
		var uj = pot - ingyenpot;
		if (uj < 0) { uj = 0};
		x.elements['pot_'+hrtid].value = uj;
	}

	vendeg(hotelid);

}

function check(hotelid) {
	//ha van kiválasztva ár, megjeleníti a küldés gombot
//    alert('hotek');

	var x = document.getElementById('order_'+hotelid);
	var pr_vis = (x.elements['pricetype']||false);
	var price = getCheckedValue(x.elements['pricetype']);
	var vis = document.getElementById('kuld_'+hotelid).style;
	if (document.getElementById('off_'+hotelid)) {
		var vis2 = document.getElementById('off_'+hotelid).style;
	}
	if ((price > '')&&(pr_vis)) {
		vis.display = 'block';
		if (vis2) {
			vis2.display = 'block';
		}
	}
	else {
		vis.display = 'none';
		if (vis2) {
			vis2.display = 'none';
		}
	}
	chcsomag(hotelid);
}

function offer(hotelid) {

	var x = document.getElementById('order_'+hotelid);
	x.elements['ajanlat'].value = '1';
	x.submit();

}

function chform(form) {
	if (checkDate("erkezes",form) && checkDate("tavozas",form)) {

		if  (checkdiff(form)) {
			callsearch();
		}
		else {
			document.getElementById("srctarget").innerHTML = "Legalább egy éjszakányi dátumot válasszon!";
		}

	}
	else {
		document.getElementById("srctarget").innerHTML = "Téves időpont választás";
	}
}

function clres() {
		document.getElementById("srctarget").innerHTML = "";
}
