var now;
var startTime;
var finishTime;

/*
imageLoader = new Image();
imageLoader.src = "images/preloader.gif";
var transitionsDone = new Array();
var imagesLoaded = new Array();
var galerie = new Array();
var shiftPixels = -118;

var currentIndex;

function screenHeight() {
	var x,y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) // all but Explorer Mac
	{
		x = document.body.scrollWidth;
		y = document.body.scrollHeight;
	}
	else // Explorer Mac;
			 //would also work in Explorer 6 Strict, Mozilla and Safari
	{
		x = document.body.offsetWidth;
		y = document.body.offsetHeight;
	}
	return y;
};

function positionTop() {
	return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
};

function closeGallery() {
	if (remover = document.getElementById('gallery-screen')) remover.parentNode.removeChild(remover); 
};

function displayImage(pictureToLoad) {
	var path = "public/" + galerie[pictureToLoad]['imgID'] + "_x.jpg";
	imagesLoaded[pictureToLoad]=true;
	document.getElementById('pictureToLoad').src = path;
	caption = document.getElementById('gallery-caption');
	if (galerie[pictureToLoad]['caption'] != "") {
		caption.innerHTML = galerie[pictureToLoad]['caption'];
	} else {
		caption.innerHTML = "&nbsp;";
	}
};

function loadImage(pictureToLoad) {
	var path = "public/" + galerie[pictureToLoad]['imgID'] + "_x.jpg";
	if (el = document.getElementById('pictureToLoad')) {
		if (imagesLoaded[pictureToLoad] == undefined) {
		el.src = imageLoader.src;
		newPic = new Image();
		newPic.onload = function() { setTimeout("displayImage("+pictureToLoad+")",600); }
		newPic.src = path;
		} else { 
		el.src = path; 
		caption = document.getElementById('gallery-caption');
		if (galerie[pictureToLoad]['caption'] != "") {
			caption.innerHTML = galerie[pictureToLoad]['caption'];
		} else {
			caption.innerHTML = "&nbsp;";
		}
		}
	}
};

function Transition(curve, milliseconds, callback) {
    this.curve_ = curve;
    this.milliseconds_ = milliseconds;
    this.callback_ = callback;
    this.start_ = new Date().getTime();
    var me = this;
    this.runCallback_ = function() {
        me.run();
    };
}

Transition.prototype.run = function() {
    if (!this.hasNext()) return; 
    this.callback_(this.next());
    setTimeout(this.runCallback_, 10);
}

Transition.prototype.hasNext = function() {
    if (this.done_) return this.finished();
    var now = new Date().getTime();
    if ((now - this.start_) > this.milliseconds_) {
        this.done_ = true;
        this.oneLeft_ = true;
    }
    return true;
}

Transition.prototype.next = function() {
    this.oneLeft_ = false;
    var now = new Date().getTime();
    var percentage = Math.min(1, (now - this.start_) / this.milliseconds_);
    return this.curve_(percentage);
}

Transition.prototype.finished = function() {
	var elOffset = this._count-1;
	transitionsDone.push(elOffset);
	if (transitionsDone.length == 9) { 
		transitionsDone = []; 
		if (parEl = document.getElementById('thumbs-container')) {	
		totalEls = parEl.getElementsByTagName('a');
		i = 0;
		while (i < totalEls.length) {
			id = parseInt(totalEls[i].id.substring(5));
			switch (this._dir) {
			case "forward" : 	newId = id+(1*this._steps); 
								if (id == 8 && this._steps==2) { 
								newId = 1; 
								si=this._ni-3; 
								frag = Math.floor(si/total);
								if (Math.floor(si/total)==si/total) si=total;
								else si = si - (total*frag);								
								//if (si<=0) si = Math.abs(si+total);
								//if (Math.ceil(si/total)==si/total) si=total;								
								totalEls[i].childNodes[0].src = "public/" + galerie[si-1]['imgID'] + "_s.jpg";
								totalEls[i].childNodes[0].alt = galerie[si-1]['caption'];
								}
								if (id == 7 && this._steps==2) { 
								newId = 0; 
								si=this._ni-4; 
								//if (si<=0) si = Math.abs(si+total);
								//if (Math.ceil(si/total)==si/total) si=total;								
								frag = Math.floor(si/total);
								if (Math.floor(si/total)==si/total) si=total;
								else si = si - (total*frag);																
								totalEls[i].childNodes[0].src = "public/" + galerie[si-1]['imgID'] + "_s.jpg";
								totalEls[i].childNodes[0].alt = galerie[si-1]['caption'];
								}
								if (id == 8 && this._steps==1) { 
								newId = 0; 
								si=this._ni-4; 
								//if (si<=0) si = Math.abs(si+total);
								//if (Math.ceil(si/total)==si/total) si=total;
								frag = Math.floor(si/total);
								if (Math.floor(si/total)==si/total) si=total;
								else si = si - (total*frag);																
								totalEls[i].childNodes[0].src = "public/" + galerie[si-1]['imgID'] + "_s.jpg";
								totalEls[i].childNodes[0].alt = galerie[si-1]['caption'];
								}
								break;
			case "backward": 	newId = id-(1*this._steps); 
								if (id == 0 && this._steps==2) {
								newId = 7; 
								si=this._ni+3; 
								frag = Math.floor(si/total);
								if (Math.floor(si/total)==si/total) si=total;
								else si = si - (total*frag);								
								totalEls[i].childNodes[0].src = "public/" + galerie[si-1]['imgID'] + "_s.jpg";
								totalEls[i].childNodes[0].alt = galerie[si-1]['caption'];
								}
								if (id == 1 && this._steps==2) { 
								newId = 8; 
								si=this._ni+4; 
								frag = Math.floor(si/total);
								if (Math.floor(si/total)==si/total) si=total;
								else si = si - (total*frag);
								//if (si>total) si = si-total;
								//if (Math.ceil(si/total)==si/total) si=total;								
								//else si = total - Math.ceil(si/total);
								totalEls[i].childNodes[0].src = "public/" + galerie[si-1]['imgID'] + "_s.jpg";
								totalEls[i].childNodes[0].alt = galerie[si-1]['caption'];
								}
								if (id == 0 && this._steps==1) { 
								newId = 8; 
								si=this._ni+4;
								frag = Math.floor(si/total);
								if (Math.floor(si/total)==si/total) si=total;
								else si = si - (total*frag);
								totalEls[i].childNodes[0].src = "public/" + galerie[si-1]['imgID'] + "_s.jpg";
								totalEls[i].childNodes[0].alt = galerie[si-1]['caption'];
								}
								break;
			}
			totalEls[i].id = 'thumb' + newId;
			totalEls[i].style.left = '';
			if (newId != 0 && newId!=1 && newId!=7 && newId != 8 && newId != 4) totalEls[i].href = "javascript:galleryGo(" + (newId-4) +");";
			if (newId == 4) totalEls[i].className = 'thumb selected';
			i++;
		}
		}
	loadImage(this._ni-1);
	document.getElementById('galleryleft').href="javascript:galleryGo(-1);";
	document.getElementById('galleryright').href="javascript:galleryGo(1);";	
	}
	return this.oneLeft_;
}

function SineCurve(percentage) {
    return (1 - Math.cos(percentage * Math.PI)) / 2;
}

function LinearCurve(percentage) {
    return percentage;
}

function galleryGo(gg) {
	if (arr = document.getElementById('galleryleft')) { arr.href="javascript:void(0);"; }
	if (arr = document.getElementById('galleryright')) { arr.href="javascript:void(0);"; }
	var steps = Math.abs(gg);
	var shiftPosition = steps*shiftPixels;
	if (steps != gg) shiftPosition = -shiftPosition;
	var newIndex = currentIndex + gg;
	if (newIndex > total) 	newIndex = newIndex - total;
	if (newIndex == 0) 		newIndex = total;
	if (newIndex < 0) 		newIndex = (total+1) + gg;
	currentIndex = newIndex;
	for (i=-4; i <= 4; i++) {
	id = i + 4;
		if (el = document.getElementById('thumb'+id)) {
		el.className = 'thumb';
		el._trans = function(elIndex) {
		var transition = new Transition(SineCurve, 200, function(percentage) {
			if (Math.abs(shiftPosition) == shiftPosition) {
			xOrig = (-shiftPixels * elIndex) - (2/steps)*shiftPosition;
			document.getElementById('thumb'+elIndex).style.left = xOrig + Math.round(percentage * shiftPosition) + "px"; 
			} else {
			xOrig = (-shiftPixels * elIndex) + (2/steps)*shiftPosition;
			document.getElementById('thumb'+elIndex).style.left = xOrig - Math.round(percentage * -shiftPosition) + "px"; 
			}
			});
		transition._count = id;
		transition._ni = newIndex;
		transition._steps = steps;
		if (Math.abs(shiftPosition) == shiftPosition) transition._dir = "forward";
		else transition._dir = "backward"; 
		transition.run();
		}
		el.href="javascript:void(0);";
		el._trans(id);
		
		}
	}

}
*/
function XMLDoc() {
	var me = this;
	var req = null;
	
	if (window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch (e) {
			req = null;
		}
	}	else if (window.ActiveXObject) {
		try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
				try {
					req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					req = null;
				}
		}
	} else {
		alert("Pro správnou fuknci této stránky je vyžadován prohlížeč Internet Explorer 5 a novější, Firefox, Safari 1.2, nebo Opera 8 a novější.");
	}
	this.request = req;
	
	this.loadXMLDoc = function(url, loadHandler) {
		if (this.request) {
			this.request.open("GET", url, true);
			this.request.onreadystatechange = function () { loadHandler(me); };
			this.request.setRequestHeader("Content-Type", "text/xml");
			this.request.send("");
		}
	};
}

function initXML(REG) {
	var myRequest = new XMLDoc();
	myRequest.loadXMLDoc("xml_response.php?reg=" + REG, responseHandler);
};

function responseHandler(req) {
	req = req.request;
	if (req.readyState == 4 && req.status == 200) {
			var responseStatus = req.responseXML.getElementsByTagName("status")[0].firstChild.nodeValue;
			if (responseStatus == 'OK') {
				showQuestions();
			} else {
				alert("Formulář není možné zobrazit.\nKontaktujte prosím pořádající agenturu.");
			}
	}
};

function showIt(REG) {
	now = new Date();
	startTime = now.getTime();

	e = document.getElementById('start');
	e.disabled = true;
	
	initXML(REG);
};

/*
function galleryHandler(req) {
	req = req.request;
	if (req.readyState == 4 && req.status == 200) {
		var xmldoc = req.responseXML;
		var xmlitems = xmldoc.getElementsByTagName('photo');
		total = xmlitems.length;
		galerie = [];

		for (i=0; i<total; i++) {
			galerie[i] = new Array('imgID', 'caption');
			galerie[i]['imgID'] = xmlitems[i].getAttribute('id');
			galerie[i]['caption'] = xmlitems[i].getAttribute('caption');
		}
		
		galleryScreenNode = document.createElement('div');
		galleryScreenNode.id = "gallery-screen";
		galleryScreenNode.style.height = screenHeight() + 'px';		
		
		galleryNode = document.createElement('div');
		galleryNode.id = "gallery-main";
		xtop = positionTop();
		xtop += 40;
		galleryNode.style.top = xtop+"px";
		
		galleryImageDiv = document.createElement('div');
		galleryImageDiv.id = "image-main";
		
		galleryThumbs = document.createElement('div');
		galleryThumbs.id = "thumbs-container";
		
		galleryCaption = document.createElement('div');
		galleryCaption.id = "gallery-caption";
		galleryCaption.innerHTML = '&nbsp;';
			
		galleryNav = document.createElement('div');
		galleryNav.id = "gallery-nav";

		galleryNav.innerHTML = '<a href="javascript:closeGallery();" id="closegallery"><span>x</span></a><a href="javascript:galleryGo(-1);" id="galleryleft"><img src="images/arr_left.gif" alt="" /></a><a href="javascript:galleryGo(1);" id="galleryright"><img src="images/arr_right.gif" alt="" /></a>';

		imgToLoad = document.createElement('img');
		imgToLoad.id = "pictureToLoad";
		imgToLoad.width = 600;
		imgToLoad.height = 400;
			
		galleryImageDiv.appendChild(imgToLoad);
		galleryNode.appendChild(galleryImageDiv);
		galleryNode.appendChild(galleryThumbs);
		galleryNode.appendChild(galleryCaption);	
		galleryNode.appendChild(galleryNav);	
		galleryScreenNode.appendChild(galleryNode);	
		document.body.appendChild(galleryScreenNode);

		loadImage(currentIndex-1);
		
		parElement = document.getElementById('thumbs-container');
		
		for (i=0; i<9; i++) {
			thumb = document.createElement('a');
			
			if (i!=4) thumb.className = "thumb"; else thumb.className = "thumb selected";
			
			thumb.id = "thumb" + i;
			
			if (i!=0 && i!=1 && i!=4 && i!=7 && i!=8) thumb.href = "javascript:galleryGo("+(i-4)+")";
			
			tempIndex = (currentIndex-1)+i-4;
			
			frag = Math.floor(tempIndex/total);
			
			if (Math.floor(tempIndex/total)==tempIndex/total) tempIndex=total;
				else tempIndex = tempIndex - (total*frag);
			if (tempIndex<0) tempIndex = Math.abs(total+tempIndex);
			if (tempIndex>=total) tempIndex = tempIndex-total;
			if (tempIndex==total) tempIndex = 0;
			
			var path = "public/" + galerie[tempIndex]['imgID'] + "_s.jpg";
			thumbIMG = document.createElement('img');
			thumbIMG.src = path;
			thumbIMG.width = 100;
			thumbIMG.height = 66;
			thumbIMG.alt = galerie[tempIndex]['caption'];
			thumb.appendChild(thumbIMG);
			parElement.appendChild(thumb);
		}
	}
};

function openGallery(ID) {
	currentIndex = 1;
	var myRequest = new XMLDoc();
	myRequest.loadXMLDoc("xml_gallery.php?gal_id=" + ID, galleryHandler);
};
*/

function setDiff() {
	now = new Date();
	finishTime = now.getTime();

	var diff = finishTime - startTime;
	
	e = document.getElementById('finish');
	e.disabled = true;
	e = document.getElementById('diff');
	e.value = diff;
};

function checkReg() {
	var score = 0;
	
	f = document.getElementById('reg_name');
	m = document.getElementById('reg_name_check');
	
	if (f.value != '') {
		m.className = 'validated';
		score++;
	} else {
		m.className = '';
	}
	
	f = document.getElementById('reg_school');
	m = document.getElementById('reg_school_check');
	
	if (f.value != '') {
		m.className = 'validated';
		score++;
	} else {
		m.className = '';
	}
	
	f = document.getElementById('reg_c_name');
	m = document.getElementById('reg_c_name_check');
	
	if (f.value != '') {
		m.className = 'validated';
		score++;
	} else {
		m.className = '';
	}
	
	f = document.getElementById('reg_name_1');
	m = document.getElementById('reg_name_1_check');
	
	if (f.value != '') {
		m.className = 'validated';
		score++;
	} else {
		m.className = '';
	}
	
	f = document.getElementById('reg_name_2');
	m = document.getElementById('reg_name_2_check');
	
	if (f.value != '') {
		m.className = 'validated';
		score++;
	} else {
		m.className = '';
	}
	
	f = document.getElementById('reg_name_3');
	m = document.getElementById('reg_name_3_check');
	
	if (f.value != '') {
		m.className = 'validated';
		score++;
	} else {
		m.className = '';
	}
	
	f = document.getElementById('reg_name_4');
	m = document.getElementById('reg_name_4_check');
	
	if (f.value != '') {
		m.className = 'validated';
		score++;
	} else {
		m.className = '';
	}
	
	re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	f = document.getElementById('reg_c_mail');
	m = document.getElementById('reg_c_mail_check');
	
	if (f.value != '' && re.test(f.value)) {
		m.className = 'validated';
		score++;
	} else {
		m.className = '';
	}

	re = /^(\+420)? ?\d{3} ?\d{3} ?\d{3}$/;
	f = document.getElementById('reg_c_phone');
	m = document.getElementById('reg_c_phone_check');

	if (f.value != '' && re.test(f.value)) {
		m.className = 'validated';
		score++;
	} else {
		m.className = '';
	}

	f = document.getElementById('check');
	m = document.getElementById('reg_check');

	if (f.checked == true) {
		m.className = 'validated';
		score++;
	} else {
		m.className = '';
	}

	if (score == 10) {
		m = document.getElementById('reg_submit');
		m.disabled = false;
	} else {
		m = document.getElementById('reg_submit');
		m.disabled = true;
	}
};

function showQuestions() {
	var q = new Array();

	q.push(new Array('Kdy byla ukončena První světová válka?', '11. listopadu 1918', '11. října 1918', '11. prosince 1918'));
	q.push(new Array('Kdy se stal Adolf Hitler říšským kancléřem?', '30. června 1934', '30. dubna 1935', '30. ledna 1933'));
	q.push(new Array('Kde byla podepsána dohoda o odstoupení československého území (Sudet) Německu?', 'v Mnichově', 'v Norimberku', 'v Berlíně'));
	q.push(new Array('Jak se jmenoval hlavní představitel strany sudetských Němců (SpD)?', 'Karl H. Frank', 'Konrád Henlein', 'Heinrich Himmler'));
	q.push(new Array('Kdy byla provedena všeobecná mobilizace Československé armády?', '23. září 1936', '23. září 1937', '23. září 1938'));
	q.push(new Array('Jméno plánu pro přepadení Polska bylo:', '„Weiss“', '„Grün“', '„Gelb“'));
	q.push(new Array('Jak se jmenuje přístav v severní Africe, který v roce 1941 bránili spolu s britskými a australskými jednotkami Čechoslováci?', 'Casablanca', 'Oran', 'Tobruk'));
	q.push(new Array('Který německý generál měl přezdívku „Liška pouště“?', 'Erwin Eugen Rommel', 'Gerd von Rundstedt', 'Erich von Manstein'));
	q.push(new Array('Jak se jmenovalo město v SSSR, kde byl založen 1. československý samostatný polní prapor?', 'Buzuluk', 'Orenburg', 'Samara'));
	q.push(new Array('Jméno vesnice, kde svedli vojáci 1. československého praporu první bitvu?', 'Komsomolske', 'Butivka', 'Sokolovo'));
	q.push(new Array('Generál G. S. Patton zemřel na následky:', 'zranění při nehodě jeho štábního automobilu', 'zranění při letecké nehodě', 'zranění v boji'));
	q.push(new Array('Kdo velel Československé samostatné obrněné brigádě ve Velké Británii?', 'generál Liška', 'generál Vlk', 'generál Jelen'));
	q.push(new Array('Jak se jmenovala skupina, která byla shozena na naše území aby provedla atentát na zastupujícího říšského protektora?', 'Anthropoid', 'Zinc', 'Out Distance'));
	q.push(new Array('Jak se jmenovali muži, kteří vlastní atentát provedli?', 'nadporučík Oldřich Pechal a rotmistr Arnošt Mikš', 'rotmistr Jan Kubiš a rotmistr Jozef Gabčík', 'nadporučík Adolf Opálka a desátník Ivan Kolařík'));
	q.push(new Array('Při operaci „Husky“, vylodění na Sicilii, velel generál Patton:', '7. armádě', '5. armádě', '9. armádě'));
	q.push(new Array('6. června 1944 se Spojenci vylodili v:', 'v Normandii', 'v Řecku', 'v Norsku'));
	q.push(new Array('Jak se nazývala operace vylodění v jižní Francii?', 'Dragoon', 'Avalanche', 'Dynamo'));
	q.push(new Array('Kdy byla osvobozena Paříž?', '25. června 1944', '25. července 1944', '25. srpna 1944'));
	q.push(new Array('Kdo velel 12. skupině armád?', 'generál Orlando Ward', 'generál Omar N. Bradley', 'generál Hugh J. Gaffey'));
	q.push(new Array('Která divize se zmocnila neporušeného mostu přes řeku Rýn u Remagenu?', '2. obrněná divize', '5. obrněná divize', '9. obrněná divize'));
	q.push(new Array('Jak se jmenoval americký těžký tank, nasazený na evropském válčišti?', 'M24 Chaffee', 'M26 Pershing', 'M47 Patton'));
	q.push(new Array('Jaké je jméno přístavního města u kterého v letech 1944–45 bojovali příslušníci Československé samostatné obrněné brigády:', 'Dunkirk', 'Brest', 'Saint-Malo'));
	q.push(new Array('Jak se jmenoval generál, který v roce 1945 velel V. armádnímu sboru?', 'Leonard T. Gerow', 'Clarence R. Huebner', 'Charles P. Summerall'));
	q.push(new Array('Příslušníci které divize osvobodili Strakonice?', '4. obrněné divize', '2. pěší divize', '9. obrněné divize'));
	q.push(new Array('Které jednotky překročily jako první hranice ČSR?', '23. pluku od 2. pěší divize', '303. pluku od 97. pěší divize', '358. pluku od 90. pěší divize'));
	q.push(new Array('Kdy spáchal Adolf Hitler sebevraždu?', '29. dubna 1945', '30. dubna 1945', '1. května 1945'));
	q.push(new Array('Kdy a kde byla podepsána bezpodmínečná kapitulace německých ozbrojených sil?', '8. května v Remeši', '8. května v Paříži', '8. května ve Vidni'));
	q.push(new Array('Muzeum generála Pattona v Plzni bylo otevřeno:', 'v květnu 2004', 'v květnu 2005', 'v květnu 2006'));
	q.push(new Array('Kdy byla přehlídka americké armády v Plzni, které se účastnil prezident republiky Edvard Beneš?', '15. červen 1945', '15. srpen 1945', '15. září 1945'));
	q.push(new Array('Jak se jmenovala první atomová bomba?', 'Little Girl', 'Little Boy', 'Little Lady'));

	var html = new Array();
	
	html.push('<table border="0" cellpadding="0" cellspacing="0" style="table-layout: auto;">');
	
	for (var i = 0; i < q.length; i++) {
		html.push('<tr>');
		html.push('<td colspan="2">&nbsp;</td>');
		html.push('</tr>');
		html.push('<tr>');
		html.push('<td><strong>' + (i + 1) + '.</strong>&nbsp;</td>');
		html.push('<td><strong>' + q[i][0] + '</strong><br /><br /></td>');
		html.push('</tr>');
		html.push('<tr>');
		html.push('<td>&nbsp;</td>');
		html.push('<td>');
		html.push('<table border="0" cellpadding="2" cellspacing="0" style="table-layout: auto;">');
		html.push('<tr>');
		html.push('<td><input type="radio" name="q' + (i + 1) + '" value="1" /></td>');
		html.push('<td>' + q[i][1] + '</td>');
		html.push('</tr>');
		html.push('<tr>');
		html.push('<td><input type="radio" name="q' + (i + 1) + '" value="2" /></td>');
		html.push('<td>' + q[i][2] + '</td>');
		html.push('</tr>');
		html.push('<tr>');
		html.push('<td><input type="radio" name="q' + (i + 1) + '" value="3" /></td>');
		html.push('<td>' + q[i][3] + '</td>');
		html.push('</tr>');
		html.push('</table>');
		html.push('</td>');
		html.push('</tr>');
	}
	
	html.push('<tr>');
	html.push('<td colspan="2">&nbsp;</td>');
	html.push('</tr>');
	html.push('</table>');
	html.push('<input name="finish" type="submit" value="KONEC" id="finish" />');
	
	e = document.getElementById('questions');
	e.innerHTML = html.join('');
	
	html = null;
	q = null;
}

