/* javascript functions */

//get available height and width of users screen and save it to cookie height and width
function getScreen() {
	//get the available height and width of the screen
	var height=screen.availHeight;
	var width=screen.availWidth;
	//minus head and padding ( menu and padding )
	height-=(270+305);
	width-=300;
	
	//try to find cookie height and width
	var cheight=getCookie('height');
	var cwidth=getCookie('width');
	
	//try to get cookie called height and width, if not found, create it
	if (cheight==0 || cheight!=height) saveCookie('height',height,14)
	if (cwidth==0 || cwidth!=width) saveCookie('width',width,14)
}

//switching cookie
function getMode() {
	//try to get current switch cookie
	var mode=getCookie('mode');
	
	//if there is no such a cookie
	if (mode==0) saveCookie('mode','off',1);
}

//get the name of browser and save it to cookie
function browser() {
	var browser=window.navigator.appName;
	
	if (getCookie('browser')==0) saveCookie('browser',browser,1);
}

// change image onmouse
function chImage(id,pic) {
	var item=document.getElementById(id);
	
	item.src='grafics/'+pic+'.png';
	//item.style.backgroundImage='url("grafics/'+pic+'.png")';
	item.style.padding='';
}

//homepage menu
function chMenu(id,pic,text) {
	//get the picture element
	var item=document.getElementById(id);
	//change the picture
	item.src='grafics/'+pic+'.png';
	
	//get the text element
	var textitem=document.getElementById('menutext');
	//change the text
	textitem.innerHTML=text;
}

//logout function
function logout() {
	alert("Uživatel byl odhlášen ze systému.");
	window.location.href="?pg=news";
}

//control number
function numctrl(text) {
	var nums=new Array('0','1','2','3','4','5','6','7','8','9','.',',',' ');
	var ok2=true;
	for (i=0;i<text.length;i++)
	{
		var ok=false;
		for (j=0; j<=nums.length;j++)
		{
			if (text[i]==nums[j]) ok=true;
		}
		if (ok==false) ok2=false;
	}
	return ok2;
}

//control e-mail
function mailctrl(mail) {
	var a=mail.indexOf('@');
	var dot=mail.indexOf('.');
	
	var ok=true;
	if ((a==-1) || (a==0) || (a==mail.length-1) || (dot==-1) || (dot==0) || (dot==mail.length-1) || (mail.lastIndexOf('.')<a) || mail[a+1]=='.' || mail[a-1]=='.') ok=false;
	else ok=true;		

	return ok;
}

//resize galery window according to a picture
function resize(news)
{
	var img=document.getElementById('mainpict');
	var width=img.width+80;
	if (width<880) width=880;
	
	if (news) var height=img.height+150;
	else var height=img.height+300;
	window.resizeTo(width,height);
}



/* control forms functions */



//rememer user if wanted in login form
function ctrllogin(rem,nick) {
	var rem=document.getElementById(rem).checked;
	var nick=document.getElementById(nick).value;
	
	var user=getCookie('user');
	
	//save new cookie if doesn't exist or enlarge the expiracy of the cookie
	if (rem) saveCookie('nick',nick,14);
	else saveCookie('nick','',0);
}

//if the input was changed, checks the id'_chck' checkbox
function changed(id) {
	id=id+'_chck';
	var chbox=document.getElementById(id);
	
	if (!chbox.checked) chbox.checked=true;
}

//make element with idvis visible and idhide hidden
function chText(idvis,idhide) {
	var vis=document.getElementById(idvis);
	var hid=document.getElementById(idhide);
	
	vis.style.display='block';
	hid.style.display='none';
}

//redirect to url
function redir(url) {
	window.location.href=url;
}

//get the position of mouse
//according to axle return X or Y positon
function mousePos(axle,e) {
	var mouseX, mouseY, d;
	
	//fucking IE mouse event 
	if (!e) {
		var e=window.event;
	} 
	
	//normal browsers
	if (e.pageX || e.pageY) {
		mouseX=e.pageX;
		mouseY=e.pageY;
		
	}
	//fucking IE
	else if (e.clientX || e.clientY) { 
		d=document.documentElement?document.documentElement:document.body; 
		mouseX=e.clientX+d.scrollLeft; 
		mouseY=e.clientY+d.scrollTop; 
	}
	
	if (axle=='x') return mouseX;
	else return mouseY;
}

//show div, which moves with mouse
function mouseDiv(id,show) {
	//doladit tuto funkci tak, aby se div v IE zobrazoval spravne i pri scrollovane strance a byl u mysky i v Mozille
	
	var div=document.getElementById(id);
	var reser=document.getElementById('resercontainer');
	
	if (show) {
		if (div.style.display=='none' || div.style.display=='') {
			div.style.display='block';
			reser.style.paddingBottom='0';
		}
		
	}
	else {
		if (div.style.display!='none') {
			div.style.display='none';
			reser.style.paddingBottom='50px';
		}
	}
	
	div.style.top=mousePos('y',event)+15;
	div.style.left=mousePos('x',event)+15;
}

//add the number of the table to the reservation form
function reserve(num) {
	var input=document.getElementById('reserved');
	var tables=document.getElementById('tables');
	
	var i,isthere=false;
	var inputarr=input.value.split(',');
	
	for (i=0;i<inputarr.length;i++) {
		if (inputarr[i]==num) isthere=true;
	}
	
	if (isthere) alert('Stůl číslo '+num+' byl již rezervován.');
	else {
			input.value+=num+',';
			tables.innerHTML+="<SPAN onclick=\"delreserve('"+num+"')\" class=\"reserspan\">"+num+"</SPAN> ";
	}
}

//delete reservation
function delreserve(num) {
	var input=document.getElementById('reserved');
	var tables=document.getElementById('tables');
	
	var invalue=input.value;
	var tablevalue=tables.innerHTML;
	
	var needle=num+',';
	var pos=invalue.indexOf(needle);
	invalue=invalue.substring(0,pos)+invalue.substring(pos+needle.length,invalue.length);
	
	while(tablevalue.indexOf(num)!=-1) {
		pos=tablevalue.indexOf(num);
		tablevalue=tablevalue.substring(0,pos)+tablevalue.substring(pos+num.length,tablevalue.length);
	}
	
	input.value=invalue;
	tables.innerHTML=tablevalue;
}

//check date and time in reservation form
function reserCheck() {
	var year=document.getElementById('year').value;
	var month=document.getElementById('month').value;
	var day=document.getElementById('day').value;
	
	var hour=document.getElementById('hour').value;
	var minute=document.getElementById('minute').value;
	var second='00';
	
	var date=new Date();
	
	date.setFullYear(year);
	date.setMonth(month-1);
	date.setDate(day);
	date.setHours(hour);
	date.setMinutes(minute);
	date.setSeconds(second);
	date.setMilliseconds(second);
	
	//alert(date.getTime());
	
	var time=date.getTime();
	time=time.toString();
	time=time.substring(0,time.length-3)
	
	window.location.href='?pg=reserve&time='+time;
}

//controling reservation form
function reservectrl() {
	var name=document.getElementById('name').value;
	var surname=document.getElementById('surname').value;
	var mail=document.getElementById('mail').value;
	var phone=document.getElementById('phone').value;
	var tables=document.getElementById('reserved').value;
	var spam=document.getElementById('spam').value;
	
	if (name=='' || surname=='') {
		alert('Vyplňte prosím kolonku Jméno a Příjmení.');
		return false;
	}
	else if (mail=='' || !mailctrl(mail)) {
		alert('Vyplňte prosím korektně kolonku E-mail.');
		return false;
	}
	else if (phone=='' || !numctrl(phone)) {
		alert('Vyplňte prosím korektně kolonku Telefon.');
		return false;
	}
	else if (tables=='') {
		alert('Rezervujte si prosím alespoň jeden stůl.');
		return false;
	}
	else if (spam=='') {
		alert('Opište prosím kontrolu proti spamu.');
		return false;
	}
}

//controling new news inserting
function newCtrl() {
	var head=document.getElementById('newhead').value;
	var text=document.getElementById('newtext').value;
	
	if (head=='') {
		alert("Vyplňte prosím kolonku Hlavička.");
		return false;
	}	
	if (text=='') {
		alert("Vyplňte prosím kolonku Text.");
		return false;
	}
}

//controling if the user really want to delete chosed news
function newsdelCtrl() {
	var ok=confirm("Chystáte se smazat vybrané novinky. Přejete si pokarčovat?");
	
	if (ok) {
		document.newsform.action='?pg=news&fce=delete';
		return true;
	}
	else {
		return false;	
	}
}

//controling update news form
function newsupdateCtrl(ids) {
	var ids=ids.split(",");
	var i;
	
	for (i=0;i<ids.length;i++) {
		var chck=document.getElementById(ids[i]+'_chck').checked;
		
		if (chck==true) {
			var head=document.getElementById(ids[i]+'_head').value;
			var text=document.getElementById(ids[i]+'_text').value;
			
			if (head=='') {
				alert ('U jedné nebo více novinek nebyla vyplněna kolonka Hlavička.');
				return false;
			}
			if (text=='') {
				alert ('U jedné nebo více novinek nebyla vyplněna kolonka Text.');
				return false;
			}
		}
	}
}

//controlig new partner inserting form
function partnerCtrl() {
	var name=document.getElementById('new_name').value;
	var url=document.getElementById('new_url').value;
	
	if (name=='') {
		alert ('Vyplňte prosím kolonku Text k zobrazení.');
		return false;
	}
	if (url=='' || url=='http://') {
		alert ('Vyplňte prosím kolonku Adresa odkazu.');
		return false;
	}
}

//controling if the user really want to delete chosed partners
function partnerdelCtrl() {
	var ok=confirm("Chystáte se smazat vybrané odkazy. Přejete si pokarčovat?");
	
	if (ok) {
		document.partnersform.action='?pg=partners&fce=delete';
		return true;
	}
	else {
		return false;
	}
}

//controling update partner form
function partnerupdateCtrl(ids) {
	var ids=ids.split(",");
	var i;
	
	for (i=0;i<ids.length;i++) {
		var chck=document.getElementById(ids[i]+'_chck').checked;
		
		if (chck==true) {
			var name=document.getElementById(ids[i]+'_name').value;
			var url=document.getElementById(ids[i]+'_url').value;
			
			if (name=='') {
				alert ('U jednoho nebo více odkazů nebyla vyplněna kolonka Text k zobrazení.');
				return false;
			}
			if (url=='' || url=='http://') {
				alert ('U jednoho nebo více odkazů nebyla vyplněna kolonka Adresa odkazu.');
				return false;
			}
		}
	}
}

//controling adress form
function adressCtrl() {
	var street=document.getElementById('street').value;
	var city=document.getElementById('city').value;
	var post=document.getElementById('post').value;
	
	if (street=='') {
		alert('Vyplňte prosím kolonku Ulice, č.p.');
		return false;
	}
	if (city=='') {
		alert('Vyplňte prosím kolonku Město.');
		return false;
	}
	if (post=='') {
		alert('Vyplňte prosím kolonku PSČ.');
		return false;
	}
}

//controling new employe form
function emplCtrl() {
	var name=document.getElementById('new_name').value;
	var surname=document.getElementById('new_surname').value;
	var mail=document.getElementById('new_mail').value;
	var phone=document.getElementById('new_phone').value;
	
	if (name=='') {
		alert ('Nebyla vyplněna kolonka Jméno.');
		return false;
	}
	if (surname=='') {
		alert ('Nebyla vyplněna kolonka Příjmení.');
		return false;
	}
	if (mail=='' && phone=='') {
		alert ('Nebyla vyplněna kolonka E-mail ani Telefon. Prosím vyplňte alespoň jednu z nich.');
		return false;
	}
	if (!numctrl(phone)) {
		alert ('Kolonka Telefon byla vyplněna chybně.');
		return false;
	}
	if (!mailctrl(mail)) {
		alert ('Kolonka E-mail byla vyplněna chybně.');
		return false;
	}
}

//controling if the user really want to delete chosed employe
function contdelCtrl() {
	var ok=confirm("Chystáte se smazat vybrané zaměstnance. Přejete si pokarčovat?");
	
	if (ok) {
		document.contform.action='?pg=cont&fce=delete';
		return true;
	}
	else {
		return false;
	}
}

//controling update employe form
function contupdateCtrl(ids) {
	var ids=ids.split(",");
	var i;
	
	for (i=0;i<ids.length;i++) {
		var chck=document.getElementById(ids[i]+'_chck').checked;
		
		if (chck==true) {
			var name=document.getElementById(ids[i]+'_name').value;
			var surname=document.getElementById(ids[i]+'_surname').value;
			var mail=document.getElementById(ids[i]+'_mail').value;
			var phone=document.getElementById(ids[i]+'_phone').value;
			
			if (name=='') {
				alert ('U jednoho nebo více zaměstnanců nebyla vyplněna kolonka Jméno.');
				return false;
			}
			if (surname=='') {
				alert ('U jednoho nebo více zaměstnanců nebyla vyplněna kolonka Příjmení.');
				return false;
			}
			if (mail=='' && phone=='') {
				alert ('U jednoho nebo více zaměstnanců nebyla vyplněna kolonka E-mail ani Telefon. Prosím vyplňte alespoň jednu z nich.');
				return false;
			}
			if (!numctrl(phone)) {
				alert ('Kolonka Telefon byla vyplněna chybně.');
				return false;
			}
			if (!mailctrl(mail)) {
				alert ('Kolonka E-mail byla vyplněna chybně.');
				return false;
			}
		}
	}
}

//controling the insert galery form
function insertgalCtrl() {
	var name=document.getElementById('new_name').value;
	
	if (name=='') {
		alert('Kolonka Název nebyla vyplněna.');
		return false;
	}
}

//controling the update galery name and text form
function updategalCtrl() {
	var name=document.getElementById('name').value;
	
	if (name=='') {
		alert('Kolonka Název nebyla vyplněna.');
		return false;
	}
}

//controling inserting offer form
function offerCtrl() {
	var name=document.getElementById('offer_name').value;
	var price=document.getElementById('offer_price').value;
	
	if (name=='') {
		alert("Kolonka Název položky nebyla vyplněna.");
		return false;
	}
	if (price=='') {
		alert("Kolonka Cena nebyla vyplněna.");
		return false;
	}
	if (!numctrl(price)) {
		alert("Kolonka Cena nebyla vyplněna korektně.");
		return false;
	}
}

//controling update offer form
function offerupdateCtrl(ids) {
	var ids=ids.split(",");
	var i;
	
	for (i=0;i<ids.length;i++) {
		var chck=document.getElementById(ids[i]+'_chck').checked;
		
		if (chck==true) {
			var name=document.getElementById(ids[i]+'_name').value;
			var price=document.getElementById(ids[i]+'_price').value;
			
			if (name=='') {
				alert ('U jedné nebo více položek nebyla vyplněna kolonka Název položky.');
				return false;
			}
			if (price=='') {
				alert ('U jedné nebo více novinek nebyla vyplněna kolonka Cena.');
				return false;
			}
			if (!numctrl(price)) {
				alert ('U jedné nebo více novinek nebyla vyplněna kolonka Cena korektně.');
				return false;
			}
		}
	}
}

//controling if the user really want to delete chosed offers
function offerdelCtrl(menu,id) {
	var ok=confirm("Chystáte se smazat vybrané položky. Přejete si pokračovat?");
	
	if (ok) {
		document.offerform.action='?pg=offer&menu='+menu+'&id='+id+'&fce=delete';
		return true;
	}
	else {
		return false;	
	}
}

//controling loging data update form
function logCtrl() {
	var nick=document.getElementById('e_nick').value;
	var pass=document.getElementById('e_pass').value;
	var pass2=document.getElementById('e_pass2').value;
	var oldpass=document.getElementById('oldpass').value;
	
	if (nick=='') {
		alert('Kolonka Nick nebyla vyplněna.');
		return false;
	}
	if (pass!='' && pass=='') {
		alert('Pokud chcete změnit heslo, musíte jej zadat také do kolonky Heslo kontrola.');
		return false;
	}
	if (oldpass=='') {
		alert('Kolonka Nynější heslo nebyla vyplněna.');
		return false;
	}
	
}

//controling anket insert form
function anketCtrl(opts) {
	var text=document.getElementById('text').value;
	
	if (text=='') {
		alert('Nebyla vyplněna kolonka Zadání ankety.');
		return false;
	}
	
	var i;
	
	for(i=1;i<=opts;i++) {
		var index=i+'_opt';
		var opt=document.getElementById(index).value;
		
		if (opt=='') {
			alert(i+'. možnost nebyla vyplněna.');
			return false;
		}
	}
}

//controling anket voting form
function voteCtrl(opts) {
	var opts=opts.split(",");
	var i,voted=false;
	for (i=0;i<opts.length;i++) {
		var rad=document.getElementById(opts[i]+'_rad').checked;
		if (rad) voted=true;
	}
	
	if (!voted) {
		alert('Zatrhněte prosím alespoň jednu možnost.');
		return false;
	}
}


