// JavaScript Document

var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);

var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
var is_ie4up  = (is_ie && (is_major >= 4));
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);
var ns = (navigator.appName=="Netscape");
var opera = (agt.indexOf("opera") != -1);


function Open(filename, width, height, index)
{
	if (index == undefined) index="";
	var x = Math.round((screen.width - width) / 2);
	var y = Math.round((screen.height - height) / 2);
	var newWindow=null;
	var date = new Date;
	if (! opera)
	{
		newWindow = window.open(filename,"Okno"+index,"scrollbars=yes,resizable=yes,width="+width+",height="+height+",screenX="+x+",screenY="+y+",top="+y+",left="+x); 
	}
	else
	{
		newWindow = window.open(filename,"Okno"+index,"scrollbars=yes,resizable=yes,width="+width+",height="+height); 
	}
	if (newWindow)
	{
		/*if (newWindow.outerHeight!=undefined)
		{
			if ((newWidnow.outerWidth!=width) || (newWidnow.outerHeight!=height)) 
			
		}*/
		//newWidnow.resizeTo(parseInt(width), parseInt(height));
		newWindow.focus();
	}
}

function OpenHref(href)
{
	window.location.href=href;
}

function OnDelSubmit()
{
	return confirm("Smazat záznam?");
}

function SetID(id)
{
	document.Delete.DelID.value=id;
}

function SetIDAndSubmit(id)
{
	SetID(id);
	if (OnDelSubmit()) document.Delete.submit();
}


function DownloadUtil(filename)
{
	window.location.href='download/utils/'+filename+'.zip';
}

function ReloadParent()
{
	window.opener.location.reload();
}

function CheckTariff(tarif, name)
{
	if (tarif.value!="")
	{
		
		znaky=new RegExp(/^[0-9]{0,2}$/);
		if (! znaky.test(tarif.value))
		{
			delete znaky;
			alert("Pocet "+name+" musí být celé kladné císlo !");
			tarif.focus();
			return false;
		}
		delete znaky;
	}
	return true;
}

var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function CheckDate(date)
{
	if (date=="0.0.0000") return true;
	
	if (window.RegExp)
	{
		znaky=new RegExp(/^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}$/);
		if (! znaky.test(date))
		{
			delete znaky;
			return false;
		}
		delete znaky;
	}
	
	var i=date.search(/\./);
	var day = parseInt(date.substr(0, i), 10);
	date=date.substr(i+1, date.length);
	i=date.search(/\./);
	var month = parseInt(date.substr(0, i), 10);
	var year = parseInt(date.substr(i+1, date.length), 10);

	if (!day || !month || !year)
		return false;
	
	if ((day<1) || (month<1) || (year<1990) || (year>2190) || (month>12)) return false;

	if (year/4 == parseInt(year/4))	monthLength[1] = 29;
	else monthLength[1] = 28;

	if (day > monthLength[month-1]) return false;

	/*var now = new Date();
	now = now.getTime(); //NN3

	var dateToCheck = new Date();
	dateToCheck.setYear(year);
	dateToCheck.setMonth(month-1);
	dateToCheck.setDate(day);
	var checkDate = dateToCheck.getTime();

	/*var futureDate = (now < checkDate);
	var pastDate = (now > checkDate);*/

	return true;
}

function GetDate(date)
{
	if (date=="") return null;

	if (window.RegExp)
	{
		znaky=new RegExp(/^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}$/);
		if (! znaky.test(date))
		{
			delete znaky;
			return null;
		}
		delete znaky;
	}
	
	if (date=="0.0.0000") return null;

	var i=date.search(/\./);
	var day = parseInt(date.substr(0, i), 10);
	date=date.substr(i+1, date.length);
	i=date.search(/\./);
	var month = parseInt(date.substr(0, i), 10);
	var year = parseInt(date.substr(i+1, date.length), 10);

	if (!day || !month || !year) return null;
	
	if ((day<1) || (month<1) || (year<1990) || (year>2190) || (month>12)) return null;

	if (year/4 == parseInt(year/4))	monthLength[1] = 29;
	else monthLength[1] = 28;

	if (day > monthLength[month-1]) return null;

	return new Date(year, month-1, day);
}

function GetMonths(Date1)
{
	if (Date1) return Date1.getMonth()+(Date1.getFullYear()*12);
	else return null;
}

function MonthsBetween(Date1, Date2)					
{
	d1 = GetMonths(Date1);
	d2 = GetMonths(Date2);
	if ((d1!=null) && (d2!=null)) return d2 - d1;
	else return null;
}

function MonthsBetweenInput(Input1, Input2)
{
	return MonthsBetween(GetDate(Input1.value), GetDate(Input2.value));
}


function DateBetween(Date1,Date2,DateInput)
{
	var date = GetDate(Date1);
	if (! date) return false;
	running_from = date.getTime();

	var date = GetDate(Date2);
	if (! date) return false;
	running_to = date.getTime();

	var date = GetDate(DateInput);
	if (! date) return false;
	date_in = date.getTime();
		
	if (date_in < running_from) return false;
	if (date_in > running_to) return false;
		
	return true;
}


function GoBack()
{
	history.back();
}

function WindowPrint(e)
{
	/*last=e.style.visibility;
	e.style.visibility="hidden";*/
	window.print();
	/*e.style.visibility=last;*/
}

function GetRadioValue(e)
{
	for (var i=0; i<e.length; i++) if (e[i].checked) return e[i].value;
}

function urlencode(s)
{
	s=escape(s);
	s.replace("*", "%2A");
	s.replace("+", "%2B");
	s.replace("/", "%2F");
	s.replace("@", "%40");
	return s;
}

function GetFormValues(f)
{
	var res="";
	for (var i=0; i<f.elements.length; i++) 
	{
		if (f.elements[i].name == "") continue;
		var s="";
		var type=f.elements[i].type.toLowerCase();
		if (type=="radio") s=GetRadioValue(f.elements[i]);
		else if ((type=="checkbox") && (! f.elements[i].checked)) continue;	//Skip unchecked checkbox
		else s=f.elements[i].value;
		
		if (res=="") res=urlencode(f.elements[i].name);
		else res=res+"&"+urlencode(f.elements[i].name);
		res=res+"="+urlencode(s);
	}
	var s=f.action;
	if (s.indexOf("?")==-1) return s+"?"+res;
	else if (s.substr(-1, 1)=="?") return s+res;
	else if (s.substr(-1, 1)=="&") return s+res;
	else return s+"&"+res;
}

function SendFormToIFrame(id, f, allow)
{
	if (allow)
	{
		var s=GetFormValues(f);
		el=document.getElementById(id);
		if (el)
		{
			el.style.visibility="visible";
			el.src=s;
		}
	}
	return false;
}

function GetTime()
{
	var now = new Date();
	res=now.valueOf() 
	delete now;
	return res;
}

function empty(i)
{
	return (i == "") || (parseInt(i) == 0);
}

function ConfirmDelete(text, href, newWindow, width, height)
{
	if (confirm(text))	
	{
		if (newWindow) Open(href, width, height);
		else window.location.href=href;
	}
}

/* COOKIES */

var Cookies = {
	init: function () {
		var allCookies = document.cookie.split('; ');
		for (var i=0;i<allCookies.length;i++) {
			var cookiePair = allCookies[i].split('=');
			this[cookiePair[0]] = cookiePair[1];
		}
	},
	create: function (name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
		this[name] = value;
	},
	erase: function (name) {
		this.create(name,'',-1);
		this[name] = undefined;
	}
};
Cookies.init();

function RegisterMonthSelector(id)
{
	el=document.getElementById(id);
	if (el)
	{
		cook = Cookies["LastMonthSelected"];
		if (cook != undefined)
			el.value = Cookies["LastMonthSelected"];
		el.onchange=function ()
		{
			Cookies.create("LastMonthSelected", el.value, 90);
		}
	}

}