function showMore(url, width, height, scroll, link, name) {
  var winName = (name ? name : "PopUpWin");
  var leftScreen=50;
  var topScreen=10;
  winStats='toolbar=no,location=no,directories=no,menubar=no,';
  if(scroll) winStats+='scrollbars=yes,resizable=yes,'
    else winStats+='scrollbars=no,resizable=no,';
  winStats+='width='+width+',height='+height;
  winStats+=',left='+leftScreen+',top='+topScreen;
  PopUpWin=window.open(url, winName, winStats, link);
  PopUpWin.focus();
} 

function showPopupTest(width, height, scroll, link, name) {
  var winName = (name ? name : "PopUpWin");
  var leftScreen=50;
  var topScreen=10;
  winStats='toolbar=no,location=no,directories=no,menubar=no,';
  if(scroll) winStats+='scrollbars=yes,resizable=yes,'
    else winStats+='scrollbars=no,resizable=no,';
  winStats+='width='+width+',height='+height;
  winStats+=',left='+leftScreen+',top='+topScreen;
  PopUpWin=window.open(winName, winStats, link);
  PopUpWin.focus();
} 


// odoslanie Allianz Index formulara

function Odoslat() 
{
  frm = document.forms['in_date'];                      
  frm.submit();              
}

function OdoslatFondy() 
{
  frm = document.forms['fondy'];                      
  frm.submit();              
}

function submit_form(form) 
{
  frm = document.forms[form];                      
  frm.submit();              
}



function ResetFormNapisteNam()
{
	  resetField(document.getElementById( 'napiste_nam_kraj'));
	  resetField(document.getElementById( 'napiste_nam_nazov_spr'));
	  resetField(document.getElementById( 'napiste_nam_vasa_spr'));
	  resetField(document.getElementById( 'napiste_nam_priezvisko'));
	  resetField(document.getElementById( 'napiste_nam_meno'));
	  resetField(document.getElementById( 'napiste_nam_email'));
	  resetField(document.getElementById( 'napiste_nam_vasa_spr'));
	  resetField(document.getElementById( 'napiste_nam_ulica'));
	  resetField(document.getElementById( 'napiste_nam_vasa_spr'));
	  resetField(document.getElementById( 'napiste_nam_cislo'));
	  resetField(document.getElementById( 'napiste_nam_psc'));
	  resetField(document.getElementById( 'napiste_nam_mesto'));
	  resetField(document.getElementById( 'napiste_nam_telefon'));
	  resetField(document.getElementById( 'napiste_nam_fax'));
	  resetField(document.getElementById( 'napiste_nam_vasa_spr'));
	  
	  resetField(document.getElementById( 'napiste_nam_klient_ano'));
          resetField(document.getElementById( 'napiste_nam_klient_nie'));
	/*if (document.getElementById("errorMessage"))
            document.getElementById("errorMessage").style.display = "none";
            document.location="#form";*/
            //showYellowFields(true);
}

function resetForm(frm) {
  frm = document.forms[frm];
  
  for (i=0;i<frm.elements.length; i++) {
    if (frm.elements[i].type == 'text') frm.elements[i].value = '';
    if (frm.elements[i].type == 'checkbox') frm.elements[i].checked=false;
    if (frm.elements[i].type == 'radio') frm.elements[i].checked=false;
    if (frm.elements[i].type == 'textarea') frm.elements[i].value= '';
    if (frm.elements[i].nodeName == 'SELECT') frm.elements[i].options.selectedIndex=0;
  }
}
 
 

function resetField(element)
{
        if (element) {
	  if ( ("INPUT" == element.nodeName) && ("text" == element.getAttribute('type')))
              element.value = "";
          else if ( ("INPUT" == element.nodeName) && ("radio" == element.getAttribute('type')))
	      element.checked = false; 
          else if ( ("SELECT" == element.nodeName))
                          element.options.selectedIndex=0;
          else if ( ("TEXTAREA" == element.nodeName))
              element.value = "";
          else if ( element.length)
              for ( i = 0; i < element.length; i++)
                  element[i].checked = false;
        }
}

function resetCheckboxes(element) 
{
        elms = document.forms['napiste_nam'].elements;
        for (i=0; i<elms.length; i++) {
          if (elms[i].id) 
            if (elms[i].type == "checkbox")
              if (elms[i].id.slice(0,element.length) == element) 
                elms[i].checked = false;
        }
}

//--- field hints

function formHints(frm, activeColor, hintColor) {
	this.frm = frm;
	this.activeColor = activeColor;
	this.hintColor = hintColor;
	this.add = formHints_add;	
	this.init = formHints_show;      
	if (this.frm.onsubmit) {
		frmonsub = this.frm.onsubmit;
		this.frm.onsubmit = function() {frmonsub(); formHints_clear(frm);}
	} else {                
		this.frm.onsubmit = function() {formHints_clear(frm);}
	}
}       

function formHints_add(el, msg, activeColor, hintColor) {
	hcol = hintColor?hintColor:this.hintColor;
	acol = activeColor?activeColor:this.activeColor;
	if (typeof(el)!='object') {               
		var elm = this.frm.elements[el]?this.frm.elements[el]:document.getElementById(el);
		if (elm) {
			elm.onfocus = function() { formHints_clearHint(elm, msg, acol); }
			elm.onblur = function() { formHints_showHint(elm, msg, hcol); }
		}
	} else {
		var elm = el;
		elm.onfocus = function() { formHints_clearHint(elm, msg, acol); }
		elm.onblur = function() { formHints_showHint(elm, msg, hcol); }
	}                  
	formHints_showHint(elm, msg, hcol);
}

function formHints_showHint(h_el, h_val, h_col) {
	if (h_el.value == '') {
		h_el.value = h_val;     
		h_el.style.color = h_col;
	}
}

function formHints_clearHint(h_el, h_val, h_col) {
	if (h_el.value == h_val) {
		h_el.value = '';      
		h_el.style.color = h_col;
	}
}  

function formHints_show(frm) {
	for (i=0; i<frm.elements.length; i++) {
		if (frm.elements[i].onblur) {
			frm.elements[i].onblur();
		}
	}
}

function formHints_clear(frm) {
	for (i=0; i<frm.elements.length; i++) {
		if (frm.elements[i].onfocus) {      
			frm.elements[i].onfocus();
		}
	}
}

//---------------------------------------
// resizes to height+1, or firefox will enable scrollbars after first page reload
function resizeWindowToFitScalar( w, h)
{
        var viewportwidth;
        var viewportheight; 

        // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

        if (typeof window.innerWidth != 'undefined')
        {
                viewportwidth = window.innerWidth;
                viewportheight = window.innerHeight-1;
        }

        // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

        else if (typeof document.documentElement != 'undefined'
                && typeof document.documentElement.clientWidth !=
                'undefined' && document.documentElement.clientWidth != 0)
        {
                viewportwidth = document.documentElement.clientWidth;
                viewportheight = document.documentElement.clientHeight-1;
        }

        // older versions of IE

        else
        {
                viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
                viewportheight = document.getElementsByTagName('body')[0].clientHeight-1;
        }
	var dx = Math.max( 0, w-viewportwidth);
	var dy = Math.max( 0, h-viewportheight);
        window.resizeBy( dx, dy);
}

function resizeWindowToFit( elementName)
{
	var pc = document.getElementById(elementName);
	//resizeWindowToFitScalar( pc.offsetWidth+112, pc.offsetHeight+74);
	resizeWindowToFitScalar( pc.offsetWidth+60, pc.offsetHeight+30);
	//setTimeout( "resizeWindowToFitScalar( "+pc.offsetWidth+", "+pc.offsetHeight+")", 500);
}



// ====================================
// SEARCH HIGHLIGHTING & COOKIE HISTORY
// ====================================

function highlightMatchTraverse( node, matchpattern)
{
	for ( var i = 0; i < node.childNodes.length; i++)
	{
		highlightMatchTraverse( node.childNodes[i], matchpattern);
		if ( (3 == node.childNodes[i].nodeType)
			&& (("DIV" == node.nodeName) ||
				("P" == node.nodeName) ||
				("A" == node.nodeName) ||
				("TD" == node.nodeName) ||
				("SPAN" == node.nodeName) ||
				("H1" == node.nodeName) ||
				("H2" == node.nodeName) ||
				("H3" == node.nodeName)
			))
		{
			if ( matchpattern.test( node.childNodes[i].nodeValue))
			{
				var tarspan = document.createElement( "span");
				var str = node.childNodes[i].nodeValue;
				matchpattern.lastIndex = 0;
				var matched = new String( matchpattern.exec( str));
				while ("null" != matched)
				{
					var leftContext = str.substr( 0, matchpattern.lastIndex-matched.length);
					if ( 0 < leftContext.length)
					{
						tarspan.appendChild( document.createTextNode( leftContext));
					}
					var span = document.createElement( "span");
					tarspan.appendChild( span);
					span.appendChild( document.createTextNode( matched));
					span.setAttribute( "className", "searchlit");
					span.setAttribute( "class", "searchlit");
					str = str.substr( matchpattern.lastIndex);
					matchpattern.lastIndex = 0;
					matched = new String( matchpattern.exec( str));
				}
				if ( 0 < str.length)
				{
					tarspan.appendChild( document.createTextNode( str));
				}
				node.replaceChild( tarspan, node.childNodes[i]);
			}
		}
	}
}

function highlightShow()
{
	if ( window.highlightSetting)
	{
		var count = 1;
		var words = window.highlightWords;
	}
	else
	{
		var count = getCookie( "highlightCount");
		var pom = getCookie( "highlightWord").split(',');
		var words = new Array( pom.length);
		for ( var i = 0; i < pom.length; i++)
		{
			words[i] = decodeURIComponent( pom[i]);
		}
	}
	if ( words && count && (count < 3))
	{
		setCookie( "highlightCount", count+1);
		for ( var i = 0; i < words.length; i++)
		{
			highlightMatch( words[i]);
		}
	}
}

function highlightSet( words)
{
	var pom = new Array( words.length);
	for ( var i = 0; i < words.length; i++)
	{
		words[i] = words[i].replace( /"/g, '');
		pom[i] = encodeURIComponent( words[i]);
	}
	setCookie( "highlightCount", 1);
	setCookie( "highlightWord", pom.join(','));;
	window.highlightSetting = 1;
	window.highlightWords = words;
}

function highlightUnset()
{
	setCookie( "highlightWord", "");
	setCookie( "highlightCount", "");
}

function highlightMatch( word)
{
	var rules = document.createTextNode( '.searchlit { background-color: #f0701a; color: white; }');	
	var styleElement = document.createElement('style');
	styleElement.type = 'text/css';
	if ( styleElement.styleSheet) 
	{
		styleElement.styleSheet.cssText = rules.nodeValue;
	}
	else
	{
		styleElement.appendChild( rules);
	}
	window.highlightRules = rules;
	window.highlightStyleElement = styleElement;
	document.getElementsByTagName('head')[0].appendChild( window.highlightStyleElement);	
	
	var pattern = new RegExp( word.replace(/([\[\\\^\$\.\|\?\*\+\(\)])/,'\\$1'), "gi");
	highlightMatchTraverse( document.body, pattern);
	
	var ele = document.getElementById( "highlightMatchButton");
	if ( ele)
	{
		ele.style.display = "block";
		ele.innerHTML = '<a class="button" href="#" onclick="highlightMatchDisable(); return false;" title="Zrušiť zvýraznenie">Zrušiť zvýraznenie</a><a class="sipka" href="javascript: highlightMatchDisable();" title="">&nbsp;</a>';
	}
}

function highlightMatchDisable()
{
	highlightUnset();
	var ele = document.getElementById( "highlightMatchButton");
	if ( ele)
	{
		ele.style.display = "none";
	}
	if ( window.highlightStyleElement)
	{
		window.highlightRules.nodeValue = '.searchlit { background-color: inherit; color: inherit; }';
		if ( window.highlightStyleElement.styleSheet)
		{
			window.highlightStyleElement.styleSheet.cssText = window.highlightRules.nodeValue;
		}
	}
}

function searchSuggestionsAddCandidate( word)
{
	var words_cookie = getCookie( "searchSuggestionsCandidates");
	var words;
	if ( words_cookie)
	{
		words = words_cookie.split(",");
	}
	else
	{
		words = new Array();
	}
	var found = false;
	var myword = encodeURIComponent( word);
	for ( var i = 0; i < words.length; i++)
	{
		if ( words[i] == myword)
		{
			found = true;
		}
	}
	if ( false == found)
	{
		words.push( myword);
	}
	setCookie( "searchSuggestionsCandidates", words.join( ","));
}

function searchSuggestionsInit_unique(a)
{
	var r = new Array();
	o:for(var i = 0, n = a.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==a[i]) continue o;
		}
		r[r.length] = a[i];
	}
	return r;
}

function searchSuggestionsInit( list)
{
	var words_cookie = getCookie( "searchSuggestionsCandidates");
	var words;
	if ( words_cookie)
	{
		words = words_cookie.split(",");
		for ( var i = 0; i < words.length; i++)
		{
			words[i] = decodeURIComponent( words[i]);
		}
	}
	else
	{
		words = new Array();
	}
	var mergedwords = searchSuggestionsInit_unique( list.concat( words));
	window.searchSuggest = new Suggest.Local("searchInput", "suggestPlaceholder", mergedwords);
	if ( document.getElementById( "suggestPlaceholder02"))
	{
		window.searchSuggest = new Suggest.Local("hladaj_query", "suggestPlaceholder02", mergedwords);
	}
}
