﻿// zde funkce prompt ziskani id radku ulozeneho var name buttonu
// start superloading script
// loading script

// MESSAGE BOXY

// constanty pro detail mode
var DETAIL_MODE_NEW = 'New';
var DETAIL_MODE_EDIT = 'Edit';

function AtonNothing() 
{
}

function AtonVarDebug(x, y, z) 
{
    ;
}


function AtonPrompt(message)
{
    return prompt(message,"");
}

function AtonAlert(message)
{
    alert(message);
}

function AtonConfirm(message)
{
    return confirm(message)   
}

//zjisti, zda promena existuje
function AtonIsVarDefined(name)
{
    return (typeof (window[name]) != 'undefined');
}

// loading funkce
var FStartLoading = false;

function AtonDisabledStartLoading()
{
    FStartLoading = true;
}

function AtonPageLoadingFinish()
{
    FStartLoading = false;
    var popupDivElement = document.getElementById("LoadingPanelPopupDiv");
    popupDivElement.style.display = "none";
    popupDivElement.style.left = '-500px';
    popupDivElement.style.top = '-500px';
    var popupDivElementAlpha = document.getElementById("LoadingPanelPopupDivAlpha");
    popupDivElementAlpha.style.display="none";
}


function StartLoading()
{    
    if (!FStartLoading)
    {
        var a = getPageSizeWithScroll();
        var height = a[1];
        var width = a[0];

        var s = getScrollXY();
        var w = getWindowSize();

        var popupDivElement = document.getElementById("LoadingPanelPopupDiv");
        popupDivElement.style.display = "block";
        popupDivElement.style.left = ((w[0] - popupDivElement.offsetWidth) / 2) + s[0] + 'px';
        popupDivElement.style.top = ((w[1] - popupDivElement.offsetHeight) / 2) + s[1] + 'px';
         
        var popupDivElementAlpha = document.getElementById("LoadingPanelPopupDivAlpha");
        popupDivElementAlpha.style.height = height + 'px';
        popupDivElementAlpha.style.width=width+'px';
        popupDivElementAlpha.style.display = "block";
       
    }
    FStartLoading = true;   
}

// spusteni scriptu po prodleve (v ms)
var AtonCallMethodParam1 = null;
var AtonCallMethodParam2 = null;
function AtonCallMethod(method,param1,param2,interval)
{
    var timeout = 300;
    if (interval) timeout = interval;

    if (typeof method == 'function') {

        if (param1 == null && param2 == null) {
            method();
        }
        
        if (param1 != null && param2 == null) {
            method(param1);
        }
        if (param1 != null && param2 != null) {
            method(param1, param2);
        }
        return;
    }

    var strMethod = method + "(";

    if (param1 != null) {
        if (typeof (param1) == 'object') {
            var tmp = 'var obj = new Object();';
            var i2 = 0;
            for (var i = 0; i < param1.length; i++) {
                if (typeof (param1[i]) == 'string') {
                    tmp += "obj[" + i2 + "] = '" + param1[i] + "';";
                    i2++;
                }
                if (typeof (param1[i]) == 'number') {
                    tmp += "obj[" + i2 + "] = " + param1[i] + ";";
                    i2++;
                }
            }
            strMethod = tmp + strMethod + 'obj';
        } else strMethod += param1;
    }
   
    if (param2 != null) {
        if (typeof (param2) == 'object') {                 
            var tmp = 'var obj2 = new Object();';
            var i2 = 0;
            for (var i = 0; i < param2.length; i++) {
                if (typeof (param2[i]) == 'string') {
                    tmp += "obj2[" + i2 + "] = '" + param2[i] + "';";
                    i2++;
                }
                if (typeof (param2[i]) == 'number') {
                    tmp += "obj2[" + i2 + "] = " + param2[i] + ";";
                    i2++;
                }
            }
            strMethod = tmp + strMethod + 'obj2';
        } else strMethod += ',' + param2;
    }
    strMethod += ")";
    setTimeout(strMethod,timeout);
}

function AtonGetParameter(data,name)
{
    var data = data.split('_');
    for (var i = 0;i < data.length;i+=2)
    {
        if (data[i] == name) return data[i+1];    
    }
    return null;
}

// pomocna funkce pro zavreni okna
function AtonClosePopupWindow(popupVarName)
{
    var imgWin = eval(popupVarName).GetWindow(0);
    eval(popupVarName).HideWindow(imgWin);
}


// Funkce pro praci s Cookie
function AtonGetCookie(sName)
{
  if (document.cookie.length == 0) return null;  
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }
  return null;
}

function AtonDelCookie(sName)
{
  document.cookie = sName + "=; expires=Fri, 21 Dec 1976 04:31:24 GMT;";
}

function AtonSetCookie(sName, sValue)
{
  document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 2099 23:59:59 GMT;";
}

/*
function AtonDeleteAllCookies()
{
    var cookies = document.cookie.split("; ");
    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i]; var eqPos = cookie.indexOf("=");
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        if ((name != 'loginUser') && (name != 'loginPass')) {
            document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
        }
    }
}
*/       

//-----------

//validace emailu
function AtonCheckEmail(email)
{
 var filter=/^.+@.+\..{2,3}$/;
 if (filter.test(email)) return true
 return false
}

//string Trim()
function AtonStringTrim(txt) {
    if (txt == null) return '';
    return txt.replace(/^\s*|\s*$/g, "");
}

function AtonIsNumeric(sText)
{
   var ValidChars = "0123456789.,";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

// spusteni scriptu po nahrani stranky
function AtonAddFuncOnLoad(func)
{
    if (window.addEventListener)
    window.addEventListener("load", func, false)
    else if (window.attachEvent)
    window.attachEvent("onload", func)
}

// spusteni scriptu po opusteni stranky
function AtonAddFuncOnUnLoad(func)
{
    if (window.addEventListener)
    window.addEventListener("unload", func, false)
    else if (window.attachEvent)
    window.attachEvent("onunload", func)
} 


// odstrani elementy IFRAME

function AtonDeleteIframe()
{
    return;

    var pole = document.getElementsByTagName("iframe");
  
//    while (var cx = 0; cx < pole.length)
//	    pole[0].parentNode.removeChild(pole[0]);
  
}

// tisk obsahu vybraneho elementu <DIV>
function AtonClickToPrintDiv(id,name,width)
{
    var print_setting="toolbar=yes,location=no,directories=yes,menubar=yes,";
    print_setting+="scrollbars=yes,left=100, top=25";
    docPrint=window.open("","Print",print_setting);
    docPrint.document.open();
    docPrint.document.write('<html><head><title>'+name+'</title>');
    docPrint.document.write('</head><body onLoad="self.print()">');
    docPrint.document.write('<table width="'+width+'" border=0><tr><td width=100%>');
    docPrint.document.write(document.getElementById(id).innerHTML);
    docPrint.document.write('</td></tr></table></body></html>');
    docPrint.document.close();
    docPrint.focus();
}


function AtonExecScript(s)
{
    eval(s);
}

//odeslani formulare do noveho okna
function AtonSubmitFormNewWindow() {
    document.forms[0].target = '_blank';
    AtonCallMethod('AtonExecScript', "document.forms[0].target=''", null, 1);
    
}

function AtonDateAdd(timeU,byMany,dateObj) {
var millisecond=1;
var second=millisecond*1000;
var minute=second*60;
var hour=minute*60;
var day=hour*24;
var year=day*365;

var newDate;
var dVal=dateObj.valueOf();
switch(timeU) {
case "ms": newDate=new Date(dVal+millisecond*byMany); break;
case "s": newDate=new Date(dVal+second*byMany); break;
case "mi": newDate=new Date(dVal+minute*byMany); break;
case "h": newDate=new Date(dVal+hour*byMany); break;
case "d": newDate=new Date(dVal+day*byMany); break;
case "y": newDate=new Date(dVal+year*byMany); break;
}
return newDate;
}


var ClientCallInputValue = {
    DataParam: function(name, value, type) {
        if (!type) type = typeof value       
        eval("var result = {" + name + ": { name: name, type: type, value: value} };");
        return result;
    },

    DataParam2: function(name, value, type, name2, value2, type2) {
        if (!type) type = typeof value        
        eval("var result = {" + name + ": { name: name, type: type, value: value} };");
        ClientCallInputValue.AddParam(result, name2, value2, type2);
        return result;
    },

    DataParam3: function(name, value, type, name2, value2, type2, name3, value3, type3) {
        if (!type) type = typeof value
        eval("var result = {" + name + ": { name: name, type: type, value: value} };");
        ClientCallInputValue.AddParam2(result, name2, value2, type2, name3, value3, type3);
        return result;
    },
   
    AddParam: function(dataParams, name, value, type) {
        if (!type) type = typeof value;
        dataParams[name] = { name: name, type: type, value: value };
    },

    AddParam2: function(dataParams, name, value, type, name2, value2, type2) {
        if (!type) type = typeof value;
        if (!type2) type2 = typeof value2
        dataParams[name] = { name: name, type: type, value: value };
        dataParams[name2] = { name: name2, type: type2, value: value2 };
    },

    AddParam3: function(dataParams, name, value, type, name2, value2, type2, name3, value3, type3) {
        if (!type) type = typeof value;
        if (!type2) type2 = typeof value2
        if (!type3) type3 = typeof value3
        dataParams[name] = { name: name, type: type, value: value };
        dataParams[name2] = { name: name2, type: type2, value: value2 };
        dataParams[name3] = { name: name3, type: type3, value: value3 };
    },

    Serialize: function(data) {
        var result = "";
        for (key in data) {
            var o = data[key];
            var type = o.type;
            if (!type) type = typeof o.value;
            var value = o.value;
            if (value == null) value = '';
            else {
                if ((type == 'date') && (typeof o.value == 'object')) value = value.getFullYear() + '.' + (1 + value.getMonth()) + '.' + value.getDate();
            }
            result += o.name + ":" + type + ":" + value + "<?%$\n\r>";
        }
        return Base64.encode(result);
    }

}


var ClientCallOutputValue = {
    Deserialize: function(data) {
        var result = {};
        var values = Base64.decode(data).split("<?%$\n\r>");
        for (var i = 0; i < values.length; i++) {
            var value = values[i].split(":", 3);
            result[value[0]] = { name: value[0], type: value[1], value: value[2] };
        }

        return result;
    }
}


// BASE 64

/**
*
*  Base64 encode / decode
*
**/

var Base64 = {

    // private property
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode: function(input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode: function(input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }
}

function isIE() {
    var ua = navigator.userAgent.toLowerCase();
    return ua.indexOf("msie ") > -1;
};

function isIE7() {
    var ua = navigator.userAgent.toLowerCase();
    return ua.indexOf("msie 7.") > -1;
};

function isIE8() {
    var ua = navigator.userAgent.toLowerCase();
    return ua.indexOf("msie 8.") > -1;
};

function isChrome() {
    var ua = navigator.userAgent.toLowerCase();
    return ua.indexOf("webkit") > -1;
};

function isFirefox() {
    var ua = navigator.userAgent.toLowerCase();
    return ua.indexOf("firefox") > -1;
};

function isOpera() {
    var ua = navigator.userAgent.toLowerCase();
    return ua.indexOf("opera/") > -1;
};


function getWindowSize()
{
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return [myWidth,myHeight];
}

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}

function getPageSizeWithScroll() {
    if (window.innerHeight && window.scrollMaxY) {// Firefox
        yWithScroll = window.innerHeight + window.scrollMaxY;
        xWithScroll = window.innerWidth + window.scrollMaxX;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        yWithScroll = document.body.scrollHeight;
        xWithScroll = document.body.scrollWidth;
    } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
        yWithScroll = document.body.offsetHeight;
        xWithScroll = document.body.offsetWidth;
    }
    arrayPageSizeWithScroll = new Array(xWithScroll, yWithScroll);
    //alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
    return arrayPageSizeWithScroll;
}
