var oLED = new Date()

function getTommorow(S) {
	oS = makeDate(S["day"], S["month"], S["year"]);
	oLED = new Date();
	oLED.setTime(oS.getTime() + 86400000);
	return oLED;
}

function makeDate(d,m,y) {
//	alert(d + "\n" + m + "\n" + y)
	oS = new Date();
	oS.setDate(d);
	oS.setMonth(m - 1);
	oS.setYear(y);
//	alert(oS.getTime())
	return oS;
}

function makeFDate(sDI) {
	oF = document.forms["fsearch"];
	return makeDate(1+oF.elements[sDI + "_Day"].selectedIndex, 1+oF.elements[sDI + "_Month"].selectedIndex, oF.elements[sDI + "_Year"].options[oF.elements[sDI + "_Year"].selectedIndex].value);
}

function setFDate(sDI, oD) {
	oF = document.forms["fsearch"];
	setFOption(oF, sDI+"_Day", oD.getDate());
	setFOption(oF, sDI+"_Month", oD.getMonth()+1);
	setFOption(oF, sDI+"_Year", oD.getFullYear());
}

function setFOption(oF,sDI,iV) {
	oE = oF.elements[sDI];
	for(i=0;i<oE.options.length;i++) {
		if (oE.options[i].value == iV) {
			oE.selectedIndex=i;
			return;
		}
	}
}

function fixDates(oE) {
	aN = oE.name.split("_");
	if (aN[0] == "start" || aN[0] == "end") {
		oST = makeFDate("start");
		oEN = makeFDate("end");

		if (oEN.getTime()<=oST.getTime()) {
			if (aN[0] == "start") {
				oEN = new Date();
				oEN.setTime(oST.getTime() + 86400000);
				setFDate("end", oEN);
			}
			if (aN[0] == "end") {
				setFDate("end", oLED);
				return;
			}
		}
		oLED = oEN;
	}
	if (aN[0]=="SGL" || aN[0]=="DBL") {
		oF = oE.form;
		iA = 1 * getOptionVal(oF, "SGL") + 2 * getOptionVal(oF, "DBL");
		setFOption(oF, "adults", iA);
	}
}

function drawOptions(sName, iS, iE, iV, bLZ) {
	HT = '';
	HT += '<select name="' + sName + '" style="width: ' + ((iE>99)?62:45) + 'px"  onChange="fixDates(this)">';
for (i=iS; i<iE+1; i++) {
	HT += '<option value="' + i + '"' + ((iV==i)?' selected="selected"':'') + '>' + ((i<10 && bLZ)?"0"+i:i) + '</option>';
}
HT += '</select>';

return HT;
}

function checkSearch(oF) {
	
	SGL = getOptionVal(oF,"SGL");
	DBL = getOptionVal(oF,"DBL");
	adults = getOptionVal(oF,"adults");
	if ((1*SGL + 2*DBL)!=adults) {
		alert("Number of adults, doesn't fit to the number of (" + (1*SGL+2*DBL) + ") bed(s)!");
		return false;
	}
	if ((Number(SGL)+Number(DBL))>3) {
		window.open("popup_more_rooms.php", "popup_more_rooms", "width=450,height=350,scrollbars=no,status=no");
		return false;
	}
	return true;
}

function getOptionVal(oF,sN) {
	oE = oF.elements[sN];
	return oE.options[oE.selectedIndex].value;
}
