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)\]/,'@'));
		}
	}
}
window.addEvent('domready', rewriteEmails);

window.addEvent('domready', function(){
	if ($('imageGallery')){
		var totIncrement = 0;
		var increment = 115;
		var maxRightIncrement = increment*(-4);
	
		var sliderFx = new Fx.Tween('slider', {
			duration: 700,
			transition: Fx.Transitions.Back.easeInOut,
			wait: true
		});
	
		$('scrollLeft').addEvents({
			'click' : function(event){
				if (totIncrement < 0){
					totIncrement = totIncrement + increment;
					//fx.stop();
					sliderFx.start('margin-left',totIncrement);
				}
			}
		});
	
		$('scrollRight').addEvents({
			'click' : function(event){
				if(totIncrement > maxRightIncrement){
					totIncrement = totIncrement - increment;
					//fx.stop();
					sliderFx.start('margin-left', totIncrement);
				}
			}
		});
	}
});

switch_sa_form = function() {
	var elem = $('shippingAddressContainer');
	if ($('shippingAddress').checked)
		elem.setStyle('display', 'block');
	else
		elem.setStyle('display', 'none');
}

switch_cd_form = function() {
	var elem = $('companyDataContainer');
	if ($('companyData').checked)
		elem.setStyle('display', 'block');
	else
		elem.setStyle('display', 'none');
}

changeCaptcha = function() {
	ajaxDisplay('captcha_ajax.php', 'captchaLoader', 'captchaPlace',
			'../images/captcha_loading.gif');
}

ajaxDisplay = function(ajaxUrl, loaderId, resultId, loaderImg) {
	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(loaderId, resultId, loaderImg);
	};
	httpRequest.send(null);
}

processRequest = function(loaderId, resultId, loaderImg) {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			$(resultId).setStyle('display','block');
			$(loaderId).setStyle('display','none');
			$(resultId).set('html',httpRequest.responseText);
			imgLoader = new Image();// preload image
			imgLoader.src = loaderImg;
		} else {
			alert("Loading error " + httpRequest.status + " : "
					+ httpRequest.statusText);
		}
	} else {
		$(resultId).setStyle('display','none');
		$(loaderId).setStyle('display','block');
	}
}

check_contact_form = function(inputs, mainErrorMsg) {
	var result = validate(inputs, mainErrorMsg);
	if (result)
		$('contactForm').submit();
	else
		return false;
}

/*validates form*/
/*input array format (keys):*/
/* 0...type of validation*/
/* 1...id of input element*/
/* 2...id of second input element (for password match)*/
/* 3...id of checkbox element. if it is checked, do the validation*/
/* 4...bad format message*/
/* 5...empty field message*/
/* 6...field importance (mandatory field if 1, voluntary if 0)*/
function validate(inputs,mainErrorMsg){
	var errorMsg = "";
	var focusInput = true;
	var doValidation = true;
	
	for(mKey = 0; mKey < inputs.length; mKey++){
		doValidation = true;
		/*if the field is conditional on checked checkbox with id = inputs[mKey][3]*/
		if (inputs[mKey][3] != ""){
			checkbox = document.getElementById(inputs[mKey][3]);
			doValidation = checkbox.checked;
		}
		/*if the element is TinyMCE editor*/
		if (inputs[mKey][0] == 12){
			elem = tinymce.EditorManager.get(inputs[mKey][1]);
			elem_value = elem.getContent();
		}
		else {
			elem = document.getElementById(inputs[mKey][1]);
			elem_value = elem.value;
		}
		/*if the field is conditional and must be in special format*/
		if (elem_value == ""){
			if ((inputs[mKey][3] == "") || ((inputs[mKey][3] != "") && (checkbox.checked)))
				if (inputs[mKey][6] > 0){
					errorMsg += "\n - " + inputs[mKey][5];
					doValidation = false;
				}
				else	
					doValidation = false;
		}
		/*if validate*/
		if (doValidation){
			switch(inputs[mKey][0]){
				/*string*/
				case 1:
					if (elem_value == "")
						errorMsg += "\n - " + inputs[mKey][4];
				break;
				/*int*/
				case 2:
					var regex = /^-?\d+$/;
					if (!regex.test(elem_value))
						errorMsg += "\n - " + inputs[mKey][4];
				break;
				/*positive int*/
				case 3:
					var regex = /^\d+$/;
					if (!regex.test(elem_value))
						errorMsg += "\n - " + inputs[mKey][4];
				break;
				/*float*/
				case 4:
					var regex = /(^-?\d\d*(\.|\,)\d*$)|(^-?\d\d*$)/;
					if (!regex.test(elem_value))
						errorMsg += "\n - " + inputs[mKey][4];
				break;
				/*positive float*/
				case 5:
					var regex = /(^\d\d*(\.|\,)\d*$)|(^\d\d*$)/;
					if (!regex.test(elem_value))
						errorMsg += "\n - " + inputs[mKey][4];
				break;
				/*email*/
				case 6:
					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(elem_value))
						errorMsg += "\n - " + inputs[mKey][4];
				break;
				/*url*/
				case 7:
					var regex = /^(http|https|ftp):\/\/([a-zA-Z0-9\.\-]+(:[a-zA-Z0-9\.&%\$\-]+)*@)?((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.[a-zA-Z]{2,4})(\:[0-9]+)?(\/[^\/][a-zA-Z0-9\.\,\?\/\'\\\+&%\$#\=~_\-@]*)*$/;
					if (!regex.test(elem_value))
						errorMsg += "\n - " + inputs[mKey][4];
				break;
				/*string with minimal length*/
				case 8:
					if ((elem_value == "") || (elem_value.length < inputs[mKey][2]))
						errorMsg += "\n - " + inputs[mKey][4];
				break;
				/*match of two fields, e.g. passwords*/
				case 9:
					var regex = /^[a-zA-Z0-9]+$/;
					elem2 = document.getElementById(inputs[mKey][1] + '_repeat');
					if ((elem2.value != elem_value) || !regex.test(elem_value) || (elem_value.length < inputs[mKey][2]))
						errorMsg += "\n - " + inputs[mKey][4];
				break;
				/*us date*/
				case 10:
					var regex = /^\d{4}-\d{1,2}-\d{1,2}$/;
					if (!regex.test(elem_value))
						errorMsg += "\n - " + inputs[mKey][4];
				break;
				/*smart url*/
				case 11:
					var regex = /^[a-zA-Z0-9]+((-{1}[a-zA-Z0-9]+)*)$/;
					if (!regex.test(elem_value))
						errorMsg += "\n - " + inputs[mKey][4];
				break;
				/*login name A-Z a-z 0-9*/
				case 13:
					var regex = /^[a-zA-Z0-9]+$/;
					if (!regex.test(elem_value) || (elem_value.length <= inputs[mKey][2]))
						errorMsg += "\n - " + inputs[mKey][4];
				break;
			}
		}
		if (focusInput && (errorMsg != "")){
			elem.focus();
			focusInput = false;
		}
	}
	
	if (errorMsg != "") {
		errorMsg = mainErrorMsg + errorMsg;
		alert(errorMsg);
		return false;
	}
	return true;
}
