function DateFromChange(SelDay,SelMonthYear,hidden)
{
	var seld2 = document.getElementById(SelDay);
	var selmy2 = document.getElementById(SelMonthYear);
	var today = new Date();
	
	
	var d2;
	try
	{
		d2 = ParseDate(selmy2.options[selmy2.selectedIndex].value + seld2.options[seld2.selectedIndex].value);
	}
	catch(err)
	{
		d2 = new Date();
	}
	if(d2< today)
	{
		d2=today;

		seld2.selectedIndex = today.getDate()-1;
	}
	document.getElementById(hidden).value = DateToString(d2);
	
}

function ValidDates()
{
	var dFrom = document.getElementById('dateFrom');
	var dTo = document.getElementById('dateTo');
	
	var d1;
	try
	{
		d1 = ParseDate(dFrom.value);
	}
	catch(err)
	{
		d1 = new Date();
		dFrom.value = DateToString(d1);
	}
	var d2;
	try
	{
		d2 = ParseDate(dTo.value);
	}
	catch(err)
	{
		d2 = AddDays(d1, 1);
		dTo.value = DateToString(d2);
	}
	
	var days = TimeSpanDays(d1,d2);
	if( days<=0 )
	{
		days = 1;
		d2 = AddDays(d1, days);
		dTo.value = DateToString(d2);
	}
	document.getElementById('nights').value = days;	
}

function ParseDate(str)
{
	re = /(\d{4})\-(\d{1,2})\-(\d{1,2})/
    var arr = re.exec( str );
    return new Date( parseInt(arr[1]), parseInt(arr[2], 10) - 1, parseInt(arr[3], 10) );
}

function TimeSpanDays(d1,d2)
{
	return (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24);
}

function AddDays(date, days)
{
	return new Date( date.getTime() + days * 24 * 60 * 60 * 1000);
}

function DateToString(date)
{
	return date.getFullYear() +'-'+ (date.getMonth()<9 ? '0':'') + (date.getMonth()+1) +'-'+ date.getDate();
}

function calendar(selDate,hiddenName,funName)
{
	pop = window.open('calendarPopup.aspx?selDate=' + selDate + '&hidName=' + hiddenName + '&funName='+funName, 'Calendar', 'height=208, width=220');
	if(pop!=null) pop.focus();
	if(!pop.opener)pop.opener = self;
}

