function tch(sel)
{
	window.location="./?tt=" + sel.options[sel.options.selectedIndex].value;
}

var name;

function cal(d,f,g)
{
	name = window.open("inc/search/cal.php?d=" + d, "Calendar", "toolbar=no,top=230,left=640,menubar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,width=159,height=130,alwaysRaised=1");
	name.location = "inc/search/cal.php?d=" + d + "&f=" + f + "&g=" + g;
	name.focus();
}

function getYear(date)
{
	return Number(date.substring(6, 10));
}

function getMonth(date)
{
	return Number(date.substring(3, 5));
}

function getDay(date)
{
	return Number(date.substring(0, 2));
}

function isValidDate(startDate, endDate)
{
	startDate 	= startDate.value;
	endDate		= endDate.value;
	var msg, start, end;
	var now = new Date();
	var valid = function(a, b, msg) {
		//	alert(a + " > " + b + "\n" + (a.getTime() > b.getTime()))
		if(a.getTime() > b.getTime()) {
			alert(msg);
			return false;
		}
		return true;
	}

	var month 	= getMonth(startDate)-1;
	var year 	= getYear(startDate);
	var sDay 	= getDay(startDate);

	if ( startDate.length != 10 || startDate == "дд-мм-гггг" || isNaN(month) || isNaN(year) || isNaN(sDay) )
	{
		alert("Началната дата трябва да е в формат дд-мм-гггг!\n\nПример: 25-03-2006.");
		return false;
	}

	if(sDay > Date.DAYSINMONTH[month])
	{
		alert("Месец " + Date.MONTHS[month] + " има само " + Date.DAYSINMONTH[month] + " дена.");
		startDate.value = 'дд-мм-гггг';
		return false;
	}

	msg = "Крайната дата трябва да е по-голяма от началната!";
	start = getDate(year, month, sDay);
	if(!valid(now, start, "Началната дата трябва да е по голяма от днешна дата!")) return false;

	var month 	= getMonth(endDate)-1;
	var year 	= getYear(endDate);
	var eDay 	= getDay(endDate);
	if ( endDate.length != 10 || endDate == "дд-мм-гггг" || isNaN(month) || isNaN(year) || isNaN(eDay) )
	{
		alert("Крайната дата трябва да е в формат дд-мм-гггг!\n\nПример: 29-03-2006.");
		return false;
	}
	if(eDay > Date.DAYSINMONTH[month])
	{
		alert("There are only " + Date.DAYSINMONTH[month] + " days in " + Date.MONTHS[month]);
		endDay.value = 1;
		return false;
	}
	end = getDate(year, month, eDay);
//	alert(start.getTime()+" " +end.getTime());
	if ( start.getTime() == end.getTime() )
	{
		alert(msg);
		return false;
	}
	return valid(start, end, msg);
}

function getDate(year, month, day) {
	var d = new Date();
	d.setDate(1);

	if(typeof(year) != "undefined" && !isNaN(year)) 	d.setYear(year);
	else 												d.setYear(2000);
	if(typeof(month) != "undefined" && !isNaN(month)) 	d.setMonth(month);
	else												d.setMonth(0);
	if(typeof(day) != "undefined" && !isNaN(day)) 		d.setDate(day);
	else												d.setDate(1);
	return d;
}

if(!Date.MONTHS) {
Date.MONTHS = ['Януари', 'Февруари', 'Март', 'Април', 'Маи', 'Юни', 'Юли', 'Август', 'Септември', 'Октомври', 'Ноември', 'Декември'];
}
Date.DAYSINMONTH = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];


function subsh(fm)
{
	if ( (fm.date1.value == "дд-мм-гггг" && fm.date2.value == "дд-мм-гггг") || (fm.date1.value.length < 1 && fm.date2.value.length < 1) )	{
		fm.date1.value = '';
		fm.date2.value = '';
		fm.submit();
	}
	else if (  isValidDate(fm.date1, fm.date2) )	fm.submit();
}

function subcp(fm)
{
	if (  isValidDate(fm.date3, fm.date4) )	fm.submit();
}

function subrsearch(fm)
{
	if ( fm.date5.value == "дд-мм-гггг" || fm.date5.value.length < 1 )	fm.date5.value = '';
	if ( fm.date6.value == "дд-мм-гггг" || fm.date6.value.length < 1 )	fm.date6.value = '';
	fm.submit();
}

function subpsearch(fm)
{
	if ( fm.date7.value == "дд-мм-гггг" || fm.date7.value.length < 1 )	fm.date7.value = '';
	if ( fm.date8.value == "дд-мм-гггг" || fm.date8.value.length < 1 )	fm.date8.value = '';
	fm.submit();
}

