﻿
/*****************************************/
/* pomocne funkce                        */
/*****************************************/

    function GetXmlString(xml, name)
    //------------------------------
    {
        var nodes = xml.getElementsByTagName(name);
        if (nodes.length > 0)
        {
            if (nodes[0].firstChild)
            {
                return nodes[0].firstChild.nodeValue;
            }
        }
        
        // pouze v pripade, ze element neexistuje nebo je prazdny
        return "";
    }



    function GetXmlNumber(xml, name)
    //------------------------------
    {
        return parseFloat(GetXmlString(xml, name));
    }



    function StrFormat()
    //==================
    // pomocna funkce pro formatovani stringu
    {
        if( arguments.length === 0 )
        {
            return null;
        }
        
        var str = arguments[0];

        for(var i=1;i<arguments.length;i++)
        {
            var re = new RegExp('\\{' + (i-1) + '\\}','gm');
            str = str.replace(re, arguments[i]);
        }
        return str;
    }
    
    
    
    function GetQueryParam( name )
    //============================
    // vrati parametr z query stringu pri volani stranky
    {  
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
        var regexS = "[\\?&]"+name+"=([^&#]*)";  
        var regex = new RegExp( regexS );  
        var results = regex.exec( window.location.href );  
        if( results === null )    
            return "";  
        else    
            return results[1];
    }    
    
    
    
    function GetParamString(Name)
    //==========================
    // vytahne hodnotu z hidden <div id="Name"></>
    // pokud nenajde, vrati prazdny string
    {
        var el = document.getElementById(Name);
        if (el !== null)
        {
            return el.innerHTML;
        }
        return "";
    }
    
    
    
    
    function GetParamInt(Name)
    //==========================
    // vytahne hodnotu z hidden <div id="Name"></>
    // pokud nenajde, vrati 0
    {
        var el = document.getElementById(Name);
        if (el !== null)
        {
            return parseInt(el.innerHTML, 10);
        }
        return 0;
    }


    function GetParamFloat(Name)
    //==========================
    // vytahne hodnotu z hidden <div id="Name"></>
    // pokud nenajde, vrati 0.0
    {
        var el = document.getElementById(Name);
        if (el !== null)
        {
            return parseFloat(el.innerHTML);
        }
        return 0;
    }    
      
    
    
    function NoFunction()
    //===================
    // alert pro no function
    {
        alert("Tato funkce nebyla zatím implementována...");
    }    
    


    function LeadingZero(N, Count)
    //============================
    {
        var s = N.toString();
        while (s.length < Count)
        {
            s = "0" + s;
        }
        return s;
    }



    function $(Name)
    //==============
    // nahrada za document.getElementById
    {
        return document.getElementById(Name);
    }    



    function Set$(Name, Value)
    //========================
    // nahrada za document.getElementById().innerHTML = 
    {
        var el = document.getElementById(Name);
        el.innerHTML = Value;
    } 


    
    
//////////////////////////////////////////
//
// cookies


    function CookieGet(Name, DefaultValue)
    //====================================
    {
        var results = document.cookie.match ( '(^|;) ?' + Name + '=([^;]*)(;|$)' );

        if ( results )
        {
            return ( unescape ( results[2] ) );
        }
        else
        {
            return DefaultValue;
        }
    }



    function CookieDelete(Name)
    //=========================
    {
        var cookie_date = new Date ( );  // current date & time
        cookie_date.setTime ( cookie_date.getTime() - 1 );
        document.cookie = Name += "=; path=/; expires=" + cookie_date.toGMTString();
    }


    function CookieSet(Name, Value, Days)
    //===================================
    {
        var cookie_string = Name + "=" + escape(Value );
        var expires = new Date();

        expires.setDate(expires.getDate() + Days);
        cookie_string += "; path=/; expires=" + expires.toGMTString();
        document.cookie = cookie_string;
    }
        
    
    
//////////////////////////////////////////
//
// AJAX    
//
// http://www.w3.org/TR/XMLHttpRequest/


/*
    var HttpReq = null;
    
    function Ajax(Url, CallbackFunction)
    //==================================
    // vraci XML
    {
        HttpReq = null;
        if (window.XMLHttpRequest)
        {
            // code for Firefox, Opera, IE7, etc.
            HttpReq = new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {   // code for IE6, IE5
            HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (HttpReq != null)
        {
            if (CallbackFunction === null)
            {
                HttpReq.onreadystatechange = AjaxNoResponseNeeded;
            }
            else
            {
                HttpReq.onreadystatechange = CallbackFunction;
            }
            HttpReq.open("GET", Url, true);
            HttpReq.send(null);
        }
        else
        {
            alert("Your browser does not support HttpReq.");
        }
    }



    function AjaxJson(Url, CallbackFunction)
    //======================================
    // vraci JSON, request je jinak naformatovany
    // standarni komunikace, pro jeden thread
    {
        HttpReq = null;
        if (window.XMLHttpRequest)
        {
            // code for Firefox, Opera, IE7, etc.
            HttpReq = new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {   // code for IE6, IE5
            HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (HttpReq != null)
        {
            if (CallbackFunction === null)
            {
                HttpReq.onreadystatechange = AjaxNoResponseNeeded;
            }
            else
            {
                HttpReq.onreadystatechange = CallbackFunction;
            }
            HttpReq.open("POST", Url, true);
            HttpReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            HttpReq.send(null);
        }
        else
        {
            alert("Your browser does not support HttpReq.");
        }
    }




    function CreateAjaxJson(Url, CallbackFunction)
    //============================================
    // vytvori HttpRequst pro JSON, request je jinak naformatovany
    // tato metoda se musi pouzit, pokud se JSON vola vicekrat za sebou (vice requestu na jedne strance)
    {
        var httpReq = null;
        if (window.XMLHttpRequest)
        {
            // code for Firefox, Opera, IE7, etc.
            httpReq = new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {   // code for IE6, IE5
            httpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (httpReq != null)
        {
            if (CallbackFunction === null)
            {
                httpReq.onreadystatechange = AjaxNoResponseNeeded;
            }
            else
            {
                httpReq.onreadystatechange = CallbackFunction;
            }
            httpReq.open("POST", Url, true);
            httpReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            httpReq.setRequestHeader("Content-Length", "2000");
            httpReq.send(null);
            return httpReq;
        }
        else
        {
            alert("Your browser does not support HttpReq.");
            return null;
        }
    }



    function AjaxNoResponseNeeded()
    //=============================
    {
        if (HttpReq.readyState==4)
        {
            // 4 = "loaded"
            if (HttpReq.status==200)
            {
                // 200 = "OK"
                // do nothing
            }
            else
            {
                alert("Problem retrieving data: " + HttpReq.statusText);
            }
        }
    }



    function AjaxShowAnswer()
    //=======================
    {
        if (HttpReq.readyState==4)
        {
            // 4 = "loaded"
            if (HttpReq.status==200)
            {
                // 200 = "OK"
                alert(HttpReq.responseText);
            }
            else
            {
                alert("Problem retrieving data: " + HttpReq.statusText + " Response: " + HttpReq.responseText);
            }
        }
    }  
 */   
    
    
    
    function AjaxPost(Url, CallbackFunction)
    //======================================
    // vraci XML
    {
        HttpReq = null;
        if (window.XMLHttpRequest)
        {
            // code for Firefox, Opera, IE7, etc.
            HttpReq = new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {   // code for IE6, IE5
            HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (HttpReq !== null)
        {
            if (CallbackFunction === null)
            {
                HttpReq.onreadystatechange = AjaxNoResponseNeeded;
            }
            else
            {
                HttpReq.onreadystatechange = CallbackFunction;
            }
            HttpReq.open("POST", Url, true);
            HttpReq.send(null);
        }
        else
        {
            alert("Your browser does not support HttpReq.");
        }
    }    
    
    
    
//////////////////////////////////// jx ////////////////////////////////////////
//V3.01.A - http://www.openjs.com/scripts/jx/
Ajax = {
	//Create a xmlHttpRequest object - this is the constructor. 
	GetHTTPObject : function() 
	{
		var http = false;
		//Use IE's ActiveX items to load the file.
		if(typeof ActiveXObject != 'undefined') {
			try {http = new ActiveXObject("Msxml2.XMLHTTP");}
			catch (e) {
				try {http = new ActiveXObject("Microsoft.XMLHTTP");}
				catch (E) {http = false;}
			}
		//If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.
		} else if (window.XMLHttpRequest) {
			try {http = new XMLHttpRequest();}
			catch (e) {http = false;}
		}
		return http;
	},
	
	// This function is called from the user's script. 
	//Arguments - 
	//	url	- The url of the serverside script that is to be called. Append all the arguments to 
	//			this url - eg. 'get_data.php?id=5&car=benz'
	//	callback - Function that must be called once the data is ready.
	//	format - The return type for this function. Could be 'xml','json' or 'text'. If it is json, 
	//			the string will be 'eval'ed before returning it. Default:'text'
	Load : function (url, callback, format, method) 
	{
		var http = this.Init(); //The XMLHttpRequest object is recreated at every call - to defeat Cache problem in IE
		if(!http||!url) return;
		if (http.overrideMimeType) http.overrideMimeType('text/xml');

		//Kill the Cache problem in IE.
		var now = "uid=" + new Date().getTime();
		url += (url.indexOf("?")+1) ? "&" : "?";
		url += now;

		if(!format) var format = "text"; //Default return type is 'text'
		format = format.toLowerCase();

		if(!method) var method = "get"; //Default method type is 'get'
		method = method.toLowerCase();

        if (method == "get")
        {
    		http.open("GET", url, true);
            http.setRequestHeader("Content-Type", "text/xml, application/xml; charset=utf-8");
        }
        else
        {
    		http.open("POST", url, true);
            http.setRequestHeader("Content-Type", "text/xml, application/x-www-form-urlencoded; charset=utf-8");
        }
        
		http.onreadystatechange = function () {//Call a function when the state changes.
			if (http.readyState == 4) {//Ready State will be 4 when the document is loaded.
				if(http.status == 200) 
				{
					//Give the data to the callback function.
					if (format == "text")
					{
    					if (callback) callback(http.responseText);
					}
					else if (format == "json")
					{
						//\n's in JSON string, when evaluated will create errors in IE
						var result = http.responseText;
						result = result.replace(/[\n\r]/g,"");
						result = eval('('+result+')'); 					
    					if (callback) callback(result);
    				}
					else
					{
    					if (callback) callback(http.responseXML);
					}
				} 
				else 
				{ 
				    //An error occured
				    alert(http.status + " " + http.responseText);
					//this.Error(http.status);
				}
			}
		}
		http.send(null);
	},
	
	Init : function() 
	{
	    return this.GetHTTPObject();
	},
	
	Error : function(Text)
	{
	    alert(Text);
	}
};    
    
    
    
//////////////////////////////////////////
//
// Text ticker    
//  

    var Ticker = null;

    var TickerClass = function(ElementName, Color, Style)
    //===================================================
    {

        this.Element = document.getElementById(ElementName);
        this.Text = this.Element.innerHTML;
        this.Len = this.Text.length;
        this.Pos = 0;
        this.Color = Color;
        this.TickEnable = (this.Text !== "");
        this.Style = Style;
    };
    
    
    TickerClass.prototype.Tick = function()
    //=====================================
    {
        if (!this.TickEnable)
        {
            return;
        }
  
        switch (this.Style)
        {
            case "":
            case "fade":
                this.TickFade();
                break;
            case "running":
                this.TickRunningLetter();
                break;
            default:
                this.TickFade();
                break;
        }
    };
    


    TickerClass.prototype.TickRunningLetter = function()
    //==================================================
    {
        if (this.Pos < this.Len)
        {
            // preskoci mezeru
            if (this.Text[this.Pos] == ' ')
            {
                this.Pos++;
            }
            // najde znak uprostred
            var s1 = this.Text.substr(0, this.Pos);
            var s2 = this.Text.substr(this.Pos+1);
            var t = this.Text.substr(this.Pos, 1);
            var s = StrFormat("{0}<font color='{1}'>{2}</font>{3}", s1, this.Color, t, s2);
            this.Element.innerHTML = s;
            this.Pos++;
        }
        else
        {
            this.Pos = 0;
        }
        
        // opetovne nastartovani
        setTimeout("Ticker.Tick()", 200);    
    };


    TickerClass.prototype.TickFade = function()
    //=========================================
    {
        if (this.Pos < this.Len)
        {
            // preskoci mezeru
            if (this.Text[this.Pos] == ' ')
            {
                this.Pos++;
            }
            var s = this.Text.substr(0, this.Pos+1);
            this.Element.innerHTML = s;
            this.Pos++;
        }
        else
        {
            this.Pos = 0;
        }
        
        // opetovne nastartovani
        setTimeout("Ticker.Tick()", 150);    

    };


    TickerClass.prototype.StartStop = function()    
    //==========================================
    {
        this.TickEnable = !this.TickEnable;
        this.Tick();
    };



    function StartTicker(Element, Color)
    //==================================
    {
        Ticker = new TickerClass(Element, Color);
        Ticker.Tick();
    }