function openBookingCancellation() {
var url = 'https://www.revalhotels.com/clients/webbooking/?do=cancelBooking';
try {
var pwidth = 773;
var pheight = 600;
var winw = (screen.width - pwidth) / 2;
var winh = (screen.height - pheight) / 2;

var win = window.open(url, 'mwBookingWindow', "toolbar=no,directories=no,status=no,location=no,resizable=yes,scrollbars=yes,menubar=no,height="+pheight+",width="+pwidth+",top="+winh+",left="+winw+"");
win.focus();
} 
catch(e) {
alert(e.message);
}
return false;
}

function openBookingWindow() {
var url = 'https://www.revalhotels.com/clients/webbooking/?begin=1&mode=1&lang=eng';
//var url='http://www.revalhotels.com/index.aw?BOOKINGPRECHECK=1&begin=1&lang=eng';
if (GetElement('username') && GetElement('username').value!="")
{
return true;
}
else
{
try {
	url += '&areaCode=' + GetElement('areaCode').value;
	url += '&api_arrival=' + GetElement('date1').value;
	url += '&api_departure=' + GetElement('date2').value;
	url += '&api_departure_days=' + GetElement('t11b').value;
	url += '&api_nopersons=' + GetElement('t11').value;
	url += '&api_norooms=' + GetElement('rooms').value;
	//url += '&card_number=' + GetElement('firstcard').value;
	//url += '&birth_date=' + GetElement('firstcardDate').value;
	//url += '&promotionalcode=' + GetElement('promotionalcode').value;
	url += '&pcode=' + GetElement('pcode').value;
	url = url.replace (/ö/g, "%C3%B6");
	url = url.replace (/ü/g, "%C3%BC");
	url = url.replace (/õ/g, "%C3%B5");
	url = url.replace (/ä/g, "%C3%A4");
	url = url.replace (/Ö/g, "%C3%96");
	url = url.replace (/Ü/g, "%C3%9C");
	url = url.replace (/Õ/g, "%C3%95");
	url = url.replace (/Ä/g, "%C3%84");

	// j2rmigsed ei toimi
	//url = url.replace (/String.fromCharCode(382)/g, "%C5%BE");  // 382 väike katusega z
	//url = url.replace (/´/, "%C5%BD"); 
	//url = url.replace (/Ø/, "%C5%A1");
	//url = url.replace (/¦/, "%C5%A0");
		
var pwidth = 773;
var pheight = 600;
var winw = (screen.width - pwidth) / 2;
var winh = (screen.height - pheight) / 2;

var win = window.open(url, 'mwBookingWindow', "toolbar=no,directories=no,status=no,location=no,resizable=yes,scrollbars=yes,menubar=no,height="+pheight+",width="+pwidth+",top="+winh+",left="+winw+"");

win.focus();
} catch(e) {
alert(e.message);
}
return false;
}
}


// convert the modified Julian day number to Gregorian date
function calendarDay(julian)
{
        var jd0 = julian+2400000.5;
        var z = Math.floor(jd0);
        var f = jd0-z;
        var a = 0.0;
        var alp = 0.0;
        if (z<2299161) {
			a = z;
        } else {
                alp = Math.floor((z-1867216.25)/36524.25);
                a = z+1.0+alp-Math.floor(alp/4.0);
        }
        var b = a+1524;
        var c = Math.floor((b-122.1)/365.25);
        var d = Math.floor(365.25*c);
        var e = Math.floor((b-d)/30.6001);
        var day = Math.floor(b-d-Math.floor(30.6001*e)+f);
        var month = 0;
        if (e<13.5) {
                month = e-1;
        } else {
                month = e-13;
        }
        var year = 0;
        if (month>2.5) {
                year = c-4716;
        } else {
                year = c-4715;
        }
        temp = new Array(3);
        temp[0] = day;
        temp[1] = month;
        temp[2] = year;

        return temp;
}
// calculate the modified Julian day number from Gregorian date
function julianDay(year,month,day)
{
        var extra = 100.0*year+month-190002.5;
        var julian = 367.0*year;
        julian -= Math.floor(7.0*(year+Math.floor((month+9.0)/12.0))/4.0);
        julian += Math.floor(275.0*month/9.0);
        julian += day;
        julian -= 678985.5;
        julian -= 0.5*extra/Math.abs(extra);
        return julian;
}
function future(days, date)
{
		if (typeof(date)=="undefined")
		{
			var today = new Date();
			var sy = parseInt(today.getFullYear());
			var sm = parseInt(today.getMonth()+1);
			var sd = parseInt(today.getDate());
		} else {
			temp = date.split(".");
			var sy = parseInt(rnill(temp[2]));
			var sm = parseInt(rnill(temp[1]));
			var sd = parseInt(rnill(temp[0]));
		}

        ey = sy;
        em = sm;
        ed = sd;
        // need to check years,months,weeks and days are numbers
        var ad = days;

		// use julian day numbers to add weeks and days
        ej = julianDay(ey,em,ed);
		ej+= parseInt(ad);
        end = calendarDay(ej); // calanderDay(ej);
        return(end);
}

function rnill(s) {
	if (s.substring(0,1)=="0") {
		return (s.substring(1));
	} else {
		return (s);
	}

}


function anill(s) {
var st = s + "";
	if (st.length==1) {
		st = "0" + s + "";
	} 
	return st;
}


function setNow() {
	var today = new Date();
	now = anill(today.getDate())+"."+anill(today.getMonth()+1)+"."+anill(today.getFullYear());
	document.getElementById('date1').value = now;
	setEndField();
}

function setEndField() {
	var nooofdays = document.getElementById('t11b').value;
	var nextDay = future(nooofdays, document.getElementById('date1').value);
	nextDay = anill(nextDay[0])+"."+anill(nextDay[1])+"."+anill(nextDay[2]);
	document.getElementById('date2').value = nextDay;
}