rewriteEmails = function(){
	var linksref = $$('a');
	for(key in linksref){
		var link = linksref[key];
		if (link.href){
			var href = link.href;
			var title = link.get('title');
			var html = link.get('html');
			if (href)
				link.set('href',href.replace(/\[(zavináč|zavin%C3%A1%C4%8D|zavinac)\]/,'@'));
			if (title)
				link.set('title',title.replace(/\[(zavináč|zavin%C3%A1%C4%8D|zavinac)\]/,'@'));
			if (html)
				link.set('html',html.replace(/\[(zavináč|zavin%C3%A1%C4%8D|zavinac)\]/,'@'));
		}
	}
}

replaceMap = function(){
	if ($('googleMap'))
		$('googleMap').set('html','<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.cz/maps?f=q&amp;source=s_q&amp;hl=cs&amp;geocode=&amp;q=d%C5%99ev%C4%9Bn%C3%A1+9,+plze%C5%88&amp;sll=49.930008,15.369873&amp;sspn=3.826046,8.635254&amp;ie=UTF8&amp;hq=&amp;hnear=D%C5%99ev%C4%9Bn%C3%A1+9,+301+00+Plze%C5%88&amp;ll=49.753878,13.383751&amp;spn=0.007501,0.016866&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe>')
}

addFlash = function(type,room){
	$('flash').set('html','<iframe src="virtual_tour/' + type + '/' + room + '/index.html" width="500" height="250" frameborder="0" scrolling="no"></iframe>')
}

ajaxDisplay = function() {
	var ajaxUrl = 'send_email.php?daymenu_email=' + encodeURIComponent($('daymenu_email').value);
	if (ajaxUrl.indexOf("?") == -1)
		url = ajaxUrl + "?random=" + Math.random();
	else
		url = ajaxUrl + "&random=" + Math.random();
	if (window.ActiveXObject) {
		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		httpRequest = new XMLHttpRequest();
	}
	httpRequest.open("GET", url, true);
	httpRequest.onreadystatechange = function() {
		processRequest();
	};
	httpRequest.send(null);
}

processRequest = function() {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			$('ajaxContainer').set('html',httpRequest.responseText);
			$('daymenu_email').value = '';
		} else {
			alert("Loading error " + httpRequest.status + " : "
					+ httpRequest.statusText);
		}
	}
}

sendEmail = function(){
	var email = $('daymenu_email').value;
	if (email == ''){
		alert(lng['email_empty']);
		$('daymenu_email').focus();
	}
	else {
		var regex = /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;
		if (!regex.test(email)){
			alert(lng['email_bad_format']);
			$('daymenu_email').focus();
		}
		else {
			preloadImg = new Image();
			preloadImg.src = '../images/loading.gif';
			var mainDiv = new Element('div', {id: 'ajaxLoader'});
			var subDiv = new Element('div', {id: 'ajaxContainer', html: lng['sending'] + '...'});
			var loader = new Element('div', {id: 'loading'});
			mainDiv.inject(document.body,'top');
			subDiv.inject(mainDiv);
			loader.inject(subDiv,'bottom');
			var size = document.getSize();
			mainDiv.set('styles',{
				'display' : 'block',
				'left' : Math.round((size.x - 330)/2) + 'px',
				'top' : Math.round((size.y - 200)/2) + 'px'
			});
			ajaxDisplay('send_email');
		}
	}
	return false;
}

sendMessage = function(){
	var regex = /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;
	var msg = '';
	if ($('email').value == '')
		msg = msg + '- ' + lng['email_not_inserted'] + '\n';
	else
		if (!regex.test($('email').value))
			msg = msg + '- ' + lng['email_bad_format'] + '\n';
	if ($('text').value == '')
		msg = msg + '- ' + lng['text_not_inserted'] + '\n';
	if (msg == '')
		return true;
	else
		alert(lng['input_data_error'] + '\n' + msg);
	return false;
}

closeEmailWin = function(){
	$('ajaxLoader').dispose();
	return false;
}

window.addEvent('domready', rewriteEmails);
window.addEvent('domready', replaceMap);
window.addEvent('domready', function(){
	$('daymenuEmailSubmit').addEvent('click', sendEmail);
	if ($('messageSubmit')){
		$('messageSubmit').addEvent('click', sendMessage);
	}
});