function bannerClick(param) {
	if (trim(param) != "") {
		var banner = unescape(param);
		banner = banner.replace(/^http:\/\//,"/banners/"); 
		urchinTracker(banner);
		window.location.href = unescape(param);
	}
}

function rgb2str(rgb) {		
	var reg = /rgb\(([0-9]*), ([0-9]*), ([0-9]*)\)/;
	var r, g, b;
	
	if (reg.test(rgb)) {
		var arr = reg.exec(rgb);
	
		r = parseInt(arr[1], 10);
		g = parseInt(arr[2], 10);
		b = parseInt(arr[3], 10);
	} else {
		r = parseInt(rgb.substr(1, 2), 16);
		g = parseInt(rgb.substr(3, 2), 16);
		b = parseInt(rgb.substr(5, 2), 16);
	}	
	
	return ((r < 16) ? "0" : "") + r.toString(16) + ((g < 16) ? "0" : "") + g.toString(16) + ((b < 16) ? "0" : "") + b.toString(16);
}

function fixFlash() {
    // fix flash
    /*var objects = document.getElementsByTagName("object");

	for (var i=0; i<objects.length; i++) {
		 var str = objects[i].outerHTML;
		 str = str.replace(/<\/object>/gi,"<param name='movie' value='"+objects[i].getAttribute('data')+"' /><param name='wmode' value='transparent' /></object>");
		 objects[i].outerHTML = str;
	}	*/
}


// 10px ffffff, ff0000 : radius, background, gradientto
makeRoundedBox = function(id, cornerRadius, outsideColorStr, gradToColorStr, outsideBottomColorStr, setSize) {
	var boxel = document.getElementById(id);
	
	if (!boxel) return;
	
	var intHTML = boxel.innerHTML;
	var innerHeight = (parseInt(YAHOO.util.Dom.getStyle(boxel, 'height'), 10));
	var innerWidth = (parseInt(YAHOO.util.Dom.getStyle(boxel, 'width'), 10));
	boxel.innerHTML = "";
	
	var borderWidth = YAHOO.util.Dom.getStyle(boxel, 'borderTopWidth');
	
	if (isNaN(parseInt(borderWidth, 10))) {
		borderWidth = "0px";
	}
	
	var backgroundColor = YAHOO.util.Dom.getStyle(boxel, 'backgroundColor');
	var borderColor = YAHOO.util.Dom.getStyle(boxel, 'borderTopColor');
		
	borderColorStr = rgb2str(borderColor);
	backgroundColorStr = rgb2str(backgroundColor);
	
	// create top bar
	var topbar = document.createElement("div");
	boxel.appendChild(topbar)
	
	topbar.style.width = '100%';
	topbar.style.lineHeight = '0';
	topbar.style.position = 'relative';
	topbar.style.height = cornerRadius;
	
	// top middle
	var tm = document.createElement("div");
	topbar.appendChild(tm);

	tm.style.lineHeight = 0;
	tm.style.position = 'absolute';
	tm.style.overflow = 'hidden';
	//tm.setStyle('zIndex', 5);
	tm.style.width = '100%';
	tm.style.height = (parseInt(cornerRadius, 10) - parseInt(borderWidth, 10)) + "px";
		
	tm.style.background = backgroundColor;
	tm.style.borderTopColor = borderColor;
	tm.style.borderTopWidth = borderWidth;
	tm.style.borderTopStyle = 'solid';
	
	tm = null;
	
	// top left
	var tl = document.createElement("div");
	topbar.appendChild(tl);
	
	tl.style.width = cornerRadius;
	tl.style.height = cornerRadius;
	tl.style.position = 'absolute';
	//tl.style.zIndex', 10);
	tl.style.backgroundRepeat = 'no-repeat';

	tl.style.backgroundImage = "url('/images/corner.php?location=top-left&radius="+parseInt(cornerRadius, 10)+"&borderwidth="+parseInt(borderWidth, 10)+"&border="+borderColorStr+"&background="+backgroundColorStr+"&outside="+outsideColorStr+"')";
	
	tl = null;
	
	// top right
	var tr = (document.createElement("div"));
	topbar.appendChild(tr);
	
	tr.style.width = cornerRadius;
	tr.style.height = cornerRadius;
	tr.style.position = 'absolute';
	//tr.style.zIndex', 10);
	tr.style.right = 0;
	tr.style.backgroundRepeat = 'no-repeat';
	tr.style.backgroundImage = "url('/images/corner.php?location=top-right&radius="+parseInt(cornerRadius, 10)+"&borderwidth="+parseInt(borderWidth, 10)+"&border="+borderColorStr+"&background="+backgroundColorStr+"&outside="+outsideColorStr+"')";
	
	tr = null;
	topbar = null;
	
	// create middle bar with actual content
	var midbar = (document.createElement("div"));
	boxel.appendChild(midbar);
	
	midbar.style.borderLeftColor = borderColor;
	midbar.style.borderLeftWidth = borderWidth;
	midbar.style.borderLeftStyle = 'solid';
	midbar.style.borderRightColor = borderColor;
	midbar.style.borderRightWidth = borderWidth;
	midbar.style.borderRightStyle = 'solid';
	midbar.style.overflow = 'hidden';
	
	midbar.style.position = 'relative';
	
	// content inside middle bar
	var content = (document.createElement("div"));
	
	var paddingTop = (parseInt(YAHOO.util.Dom.getStyle(boxel, 'paddingTop'), 10) - parseInt(cornerRadius, 10) + parseInt(borderWidth, 10));
	if (paddingTop < 0) {
		paddingTop = 0;
	}
	paddingTop = paddingTop + "px";
	
	var paddingBottom = (parseInt(YAHOO.util.Dom.getStyle(boxel, 'paddingBottom'), 10) - parseInt(cornerRadius, 10) + parseInt(borderWidth, 10));
	
	if (paddingBottom < 0) {
		paddingBottom = 0;
	}
	
	paddingBottom = paddingBottom + "px";
	
	content.style.paddingTop = paddingTop;
	content.style.paddingLeft = parseInt(YAHOO.util.Dom.getStyle(boxel, 'paddingLeft'), 10) - parseInt(borderWidth, 10) + "px";
	content.style.paddingBottom = paddingBottom;		
	content.style.paddingRight = parseInt(YAHOO.util.Dom.getStyle(boxel, 'paddingRight'), 10) - parseInt(borderWidth, 10) + "px";
	content.style.position = 'relative';
	
	if (YAHOO.env.ua.ie) {
		content.style.height = '1%';
	}
		
	// clear source padding & border
	if (setSize) {
		boxel.style.height = (innerHeight + parseInt(paddingTop, 10)) + parseInt(paddingBottom, 10) + 2*parseInt(cornerRadius, 10) + 2*parseInt(borderWidth, 10) + "px";
		boxel.style.width = (innerWidth + parseInt(YAHOO.util.Dom.getStyle(boxel, 'paddingRight'), 10)) + parseInt(YAHOO.util.Dom.getStyle(boxel, 'paddingLeft'), 10) + 2*parseInt(borderWidth, 10) + "px";
	}
	
	boxel.style.padding = '0px';
	boxel.style.borderWidth = '0';
	boxel.style.position = 'relative';
	
	// put actual content into middle bar
	content.innerHTML = intHTML;
		
	// create gradient background if needed
	if (gradToColorStr != undefined && gradToColorStr != "") {
		if (!YAHOO.env.ua.ie) {
			// gradient bg for normal browsers		
			var bgimg = document.createElement("img");
			midbar.appendChild(bgimg);
			bgimg.src = "/images/gradient.php?from="+backgroundColorStr+"&to="+gradToColorStr;		
		
			bgimg.style.height = '100%';
			bgimg.style.left = '0px';
			bgimg.style.position = 'absolute';
			bgimg.style.top = '0px';
			bgimg.style.width = '100%';
			//bgimg.setStyle('zIndex', '0');
			bgimg = null;
		} else {
			// gradient bg for IE						
			content.style.height = '1%';
			content.style.position = 'static';
			content.style.filter = "progid:DXImageTransform.Microsoft.Gradient(startColorstr=#ff"+backgroundColorStr+", endColorstr=#ff"+gradToColorStr+")";
		}
	}
	
	midbar.appendChild(content);
	midbar = null;
	content = null;
	
	// create bottom bar	
	var botbar = document.createElement("div");			
	boxel.appendChild(botbar);
	boxel = null;
	botbar.style.width = '100%';
	botbar.style.lineHeight = '0';
	botbar.style.position = 'relative';
	botbar.style.height = cornerRadius;
		
	if (gradToColorStr != undefined && gradToColorStr != "") {
		backgroundColorStr = gradToColorStr;
		backgroundColor = "#" + backgroundColorStr;
	}
	
	// bottom middle bar
	var bm = document.createElement("div");
	botbar.appendChild(bm);

	bm.style.lineHeight = 0;
	bm.style.position = 'absolute';
	bm.style.overflow = 'hidden';
	//bm.setStyle('zIndex', 5);
	bm.style.width = '100%';
	bm.style.height = (parseInt(cornerRadius, 10) - parseInt(borderWidth, 10)) + "px";
	bm.style.background = backgroundColor;
	bm.style.borderBottomColor = borderColor;
	bm.style.borderBottomWidth = borderWidth;
	bm.style.borderBottomStyle = 'solid';
		
	// bottom left
	var bl = document.createElement("div");
	botbar.appendChild(bl);
	
	bl.style.width = cornerRadius;
	bl.style.height = cornerRadius;
	bl.style.position = 'absolute';
	//bl.setStyle('zIndex', 10);
	bl.style.backgroundRepeat = 'no-repeat';

	bl.style.backgroundImage = "url('/images/corner.php?location=bottom-left&radius="+parseInt(cornerRadius, 10)+"&borderwidth="+parseInt(borderWidth, 10)+"&border="+borderColorStr+"&background="+backgroundColorStr+"&outside="+((outsideBottomColorStr != undefined && outsideBottomColorStr != "")?outsideBottomColorStr:outsideColorStr)+"')";
	bl = null;
	// bottom right
	var br = document.createElement("div");
	botbar.appendChild(br);
	
	br.style.width = cornerRadius;
	br.style.height = cornerRadius;
	br.style.position = 'absolute';
	//br.setStyle('zIndex', 10);
	br.style.right = 0;
	br.style.backgroundRepeat = 'no-repeat';
	br.style.backgroundImage = "url('/images/corner.php?location=bottom-right&radius="+parseInt(cornerRadius, 10)+"&borderwidth="+parseInt(borderWidth, 10)+"&border="+borderColorStr+"&background="+backgroundColorStr+"&outside="+((outsideBottomColorStr != undefined && outsideBottomColorStr != "")?outsideBottomColorStr:outsideColorStr)+"')";
	br = null;
	botbar = null;
};

function addBgImage(node, image) {
	// put illustration images for boxes
	var newsbox = document.getElementById(node);
	
	if (!newsbox) return;
	
	var illustrateImg = document.createElement("img");
	illustrateImg.src = image;
	illustrateImg.style.position = 'absolute';
	illustrateImg.style.top = '0';
	illustrateImg.style.right = '8px';
	//illustrateImg.setStyle('zIndex', '0');
	
	// gradient
	var divels = newsbox.firstChild.nextSibling.getElementsByTagName("div");
	var midbox = divels[0];
	//var content = new $E(divels[0]);
	midbox.insertBefore(illustrateImg, divels[0].firstChild);
	
	//var midbox = new $E(newsbox.get('element').firstChild.nextSibling.firstChild);
	//midbox.parentNode.(illustrateImg);
	//newsbox.appendChild(illustrateImg);
	//newsbox.insertBefore(illustrateImg, newsbox.get('element').firstChild);
}

function menuMouseOver() {
	var itema = this.firstChild;
	while (itema.nodeType != 1) {
		itema = itema.nextSibling;
	}
	var parentnode = this;
		
	var submenuitems = parentnode.getElementsByTagName("div");
	if (submenuitems.length > 0) {
		var submenu = submenuitems[0];
		
		itema.style.backgroundImage = 'url(/images/menu-right-over.gif)';
		parentnode.style.backgroundImage = 'url(/images/menu-bullet-left-over.gif)';
		submenu.style.display = '';
	} else {
		itema.style.backgroundImage = 'url(/images/menu-right-round-over.gif)';
		parentnode.style.backgroundImage = 'url(/images/menu-bullet-left-round-over.gif)';
	}
		
	//parentnode.style.backgroundColor = '#8dd016';
	
	itema = null;
	parentnode = null;
}

function menuMouseOut(e) {
	var itema = this.firstChild;
	while (itema.nodeType != 1) {
		itema = itema.nextSibling;
	}
	var parentnode = this;
	
	var submenuitems = parentnode.getElementsByTagName("div");
	if (submenuitems.length > 0) {
		var submenu = submenuitems[0];
				
		submenu.style.display = 'none';
		
		itema.style.backgroundImage = 'url(/images/menu-right.gif)';
		itema.parentNode.style.backgroundImage = 'url(/images/menu-bullet-left.gif)';
		//itema.parentNode.style.backgroundColor = '#d6f1a5';
	} else {
		itema.style.backgroundImage = 'url(/images/menu-right.gif)';
		parentnode.style.backgroundImage = 'url(/images/menu-bullet-left.gif)';
		//parentnode.style.backgroundColor = '#d6f1a5';	
	}
}

function cumulativeOffset(element) {
    var valueT = 0, valueL = 0;
    do {
      valueT += element.offsetTop  || 0;
      valueL += element.offsetLeft || 0;
      element = element.offsetParent;
    } while (element);
    return [valueL, valueT];
}

function processMenu() {
	var content = document.getElementById('dummy');
	var menu = document.getElementById('menu');
	
	//content.appendChild(menu);
	
	/*menu.style.float = 'none';
	menu.style.position = 'absolute';
	menu.style.top = "0px";
	menu.style.right = "0px";
	menu.style.marginTop = "65px";
	menu.style.marginRight = "11px";
	*/
	var menuitems = menu.getElementsByTagName("li");
	for (i = 0; i < menuitems.length; i++) {
		var menutitem = menuitems[i];
		var submenuitems = menutitem.getElementsByTagName("div");
		
		var items = menutitem.getElementsByTagName("a");
		var itema = items[0];
		 
		menutitem.onmouseover = menuMouseOver;
		menutitem.onmouseout = menuMouseOut;
		
		if (submenuitems.length > 0) {
			// has children -> move outside
			var submenuitem = submenuitems[0];
			submenuitem.style.width =  menutitem.clientWidth + "px";
		}
	}
}

function isValidNumber(val) {
	if (val.match(/^[0-9]+$/)) {
		return true;
	} else {
		return false;		
	}
}

function validateAndErrorEmail(obj) {
	var email = obj.value;
	if (!isValidMail(email)) {
		YAHOO.util.Dom.addClass(obj, "error");
	} else {
		YAHOO.util.Dom.removeClass(obj, "error");
	}  
}

function validateAndErrorText(obj) {
	var email = obj.value;
	if (isWhitespace(email)) {
		YAHOO.util.Dom.addClass(obj, "error");
	} else {
		YAHOO.util.Dom.removeClass(obj, "error");
	}  
}

function isValidMail(mail) {
	if (mail.match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/)) {
		return true;
	} else {
		return false;
	}
}

function isValidFloat(val) {
	if (val.match(/^[0-9]+((.|,)[0-9]+){0,1}$/)) {
		return true;
	} else {
		return false;		
	}
}
function isWhitespace(val) {
	if (val.match(/^\s*$/)) {
		return true;
	} else {
		return false;		
	}
}

var Meeting = function() {		
	var connection = null;
	var image = null;
	var arr = {
				"name" : { 
					"required" : true,
					"type" : "text"
				},
				"contact" : { 
					"required" : true,
					"type" : "text"
				},
				"town" : { 
					"required" : true,
					"type" : "text"
				},
				"day" : { 
					"required" : false
				},
				"month" : { 
					"required" : false
				},
				"year" : { 
					"required" : false
				},
				"text" : { 
					"required" : false
				},
				"hour" : { 
					"required" : true,
					"type" : "numeric"
				},
				"minute" : { 
					"required" : true,
					"type" : "numeric"
				}
			};
	return {
		success: function(resp) {
			if(!connection || (resp.tId != connection.tId)) {					
				return;
			}
			
			image.src = "/images/send-button.gif";
			var resp = resp.responseText;
			
			if (resp.substring(0, 1) == "0") {
				alert(resp.substring(1));
			} else {
				document.getElementById('meetres').innerHTML = resp.substring(1); 
				var anim = new YAHOO.util.ColorAnim('meetres',{ 
				backgroundColor: 
					{from: '#f89910', to: '#ffffff'}
				}); 
				urchinTracker('/schuzka.html');
				anim.onComplete.subscribe(this.reset);
				anim.animate();
			}
		},
		failure: function(resp) {
			image.src = "/images/send-button.gif";
		},
		reset: function(val) {
			document.getElementById('meetres').style.background = '';
		},
		send: function(img) {
			image = document.getElementById('meetimg');
			
			if (connection) {
   				YAHOO.util.Connect.abort(connection);
			}
			for (var key in arr) {
				var obj = document.getElementById("meet" + key);
				YAHOO.util.Dom.removeClass(obj, 'error');
			}
			var str = "";
			var valid = true;
			for (var key in arr) {
				var obj = document.getElementById("meet" + key);
				var val;
				if (!obj.selectedIndex) {
					val = obj.value;
				} else {
					val = obj.options[obj.selectedIndex].value;
				}
				
				str += "&" + key + "=" + encodeURIComponent(val);
				if (arr[key]['required']) {
					if (isWhitespace(val)) {
						valid = false;
						YAHOO.util.Dom.addClass(obj, 'error');
					}
					if (arr[key]['type'] == "numeric" && !val.match(/^[0-9]{1,2}$/)) {
						valid = false;
						YAHOO.util.Dom.addClass(obj, 'error');
					}
				}
			}
			
			if (!valid) {
				alert("Vyplňte všechny vyznačené položky formuláře.");
			} else {
				image.src = "/images/send-button-wait.gif";
				connection = YAHOO.util.Connect.asyncRequest("POST", 
					"/", this, 
					"ajax=1" + str);
			}
						
			
		}
	}
};

function validateAndFill(data) {
	var valid = true;
	
	for (var val in data) {
		var obj = document.getElementById(val);
		
		data[val]["value"] = trim(obj.value);
		
		if (data[val]["type"] == "int") {
			if (isValidNumber(data[val]["value"])) {
				if (!data[val]["zero"] && data[val]["value"] == 0) {
					data[val]["valid"] = false;
					valid = false;
				} else {
					data[val]["valid"] = true;
				}
			} else if (data[val]["value"] == "" && !data[val]["required"]) {
				data[val]["valid"] = true;
			} else {
				data[val]["valid"] = false;
				valid = false;
			} 
		} else if (data[val]["type"] == "float") {
			data[val]["value"] = data[val]["value"].replace(/,/, ".");
			
			if (isValidFloat(data[val]["value"])) {
				if (!data[val]["zero"] && data[val]["value"] == 0) {
					data[val]["valid"] = false;
					valid = false;
				} else {
					if (data[val]["max"]) {
						if (data[val]["value"] <= data[val]["max"]) {
							data[val]["valid"] = true;
						} else {
							data[val]["valid"] = false;
							valid = false;
						}
					} else {
						data[val]["valid"] = true;
					}
				}
			} else if (data[val]["value"] == "" && !data[val]["required"]) {
				data[val]["valid"] = true;
			} else {
				data[val]["valid"] = false;
				valid = false;
			} 
		}
	}
	
	return new Array(valid, data); 
}

function trim(str) {
	return str.replace( /^\s+/g,'').replace(/\s+$/g,''); 
}

function mailSendAsk(img) {
	document.getElementById('resultsstart').style.display = 'none';
	var mail = document.getElementById('resultsmail');
	if (trim(mail.value) == "") {
		mail.value = getCookie('sendmail', '');
		validateAndErrorEmail(mail);
	}
	document.getElementById('resultssend').style.display = '';
	mail.focus();
}

function mailSendClose(img) {
	document.getElementById('resultsstart').style.display = '';
	document.getElementById('resultssend').style.display = 'none';
}

function meetSendAsk(img) {
	document.getElementById('resultsstart').style.display = 'none';
	var mail = document.getElementById('resultsmmail');
	if (trim(mail.value) == "") {
		mail.value = getCookie('sendmail', '');
		validateAndErrorEmail(mail);
	}
	document.getElementById('meetingsend').style.display = '';
}


function meetSendClose(img) {
	document.getElementById('resultsstart').style.display = '';
	document.getElementById('meetingsend').style.display = 'none';
}

function cookieTime(days){
  var now = new Date();
  var exp = new Date();
  var x = Date.parse(now) + days*24*60*60*1000;
  exp.setTime(x);
  str = exp.toUTCString();
  re = '/(\d\d)\s(\w\w\w)\s\d\d(\d\d))/';
  return str.replace(re,"$1-$2-$3");
}

function setCookie(id, value) {
  document.cookie = id+'='+value+';path=/;expires='+cookieTime(365);
}

function getCookie(id, defaultValue) {
  var re = new RegExp(id+'=(.*)');
  var value = re.exec(document.cookie);
  return (value) ? value[1].split(';')[0] : defaultValue;
}

function printPage() {
	if (window.print) {
		window.print();
	} else {
		alert('Nepodařilo se spustit tisk. Zvolte funkci tisku přímo z menu prohlížeče.');
	}
}

function sendResults(calcres, data) {
	var data = validateAndFill(data);
	if (!data[0]) {
		// invalid - find invalid items
		alert('Zadejte prosím parametry hypotečního úvěru.');
	} else {
		obj = document.getElementById('resultsmail');
		var email = obj.value;
		if (!isValidMail(email)) {
			alert('Zadejte prosím platný e-mail.'); 
		} else {
			// send
			setCookie('sendmail', email);
			calcres.send(data[1], email);
		}
	}
}	

function sendMeetResults(calcres, data) {
	var data = validateAndFill(data);
	if (!data[0]) {
		// invalid - find invalid items
		alert('Zadejte prosím parametry hypotečního úvěru.');
	} else {
		if (!isValidMail(document.getElementById('resultsmmail').value) || 
				isWhitespace(document.getElementById('resultsname').value) || 
				isWhitespace(document.getElementById('resultstown').value) ||
				isWhitespace(document.getElementById('resultsphone').value)) {
			alert('Zadejte prosím jméno, telefon, platný e-mail a město.'); 
		} else {
			// send
			setCookie('sendmail', document.getElementById('resultsmmail').value);
			calcres.send(data[1], document.getElementById('resultsmmail').value, document.getElementById('resultsname').value, document.getElementById('resultsphone').value, document.getElementById('resultstown').value);
		}
	}
}	


function showCalcInfo(id, which) {
	var display = document.getElementById('display');
	if (which == "out") {
		display.innerHTML = 'Vyberte si, která hypokalkulačka je pro Vás nejvhodnější...';		
	} else {
		switch (id) {
			case 1:	display.innerHTML = '<strong>Jednoduchá kalkulačka:</strong> Spočítá výše měsíčních splátek při určité výši úrokové sazby';
				break;
			case 2:	display.innerHTML = '<strong>Srovnávací kalkulačka:</strong> Máte více nabídek od bank? Spočítejte si, která nabídka je skutečně nejlevnější! Úroková sazba nemusí být vždy rozhodující.';
				break;
			case 3:	display.innerHTML = '<strong>Kalkulačka rok po roce:</strong> Znázorní průběh splácení Vaší hypotéky v čase. Spočítá výši dlužné částky po konci fixace.';
				break;
			case 4:	display.innerHTML = '<strong>Bonitní kalkulačka:</strong>  Orientačně spočítá výši hypotéky, na kterou s Vašimi příjmy dosáhnete.';
				break;
			case 5:	display.innerHTML = '<strong>Daňová kalkulačka:</strong> Zaměstnancům orientačně spočítá výši daňové úspory.';
				break;
			case 6:	display.innerHTML = '<strong>Hypožurnál:</strong> Zde najdete užitečné informace ze světa hypoték. Můžete využít i hypoteční poradny.';
				break;
			
		}
	}
}


