function _ax_useActiveX()
{
    if(typeof(ax_useActiveX)!='undefined') return ax_useActiveX;
    return isIE; 
}
function _ax()
{
    var ax = GetGlobalManager().GetValue('ax');
    if(ax==null) throw new Error("ax not detected");
    return ax;
}
function _ax_detect()
{
    try {
        return _ax().Hello(42) == 'Hello42';
    }
    catch(e) {
        return false;
    }
}
function _ax_version() {
    try {
        return _ax().GetVersion();
    }
    catch(e) {
        return "not installed";
    }
}
function _ax_subv() {
    try { return parseInt((""+_ax().GetVersion()).split('.')[1]); }
    catch(e) { return 0 };
}
function _ax_throw(e,name)
{
    var p = "" + name+ " error: ";
    if(typeof(e.getMessage)!="undefined" && e.getMessage) 
    {
        var x=e;
        while(x!=null && x.getMessage()==null) x=x.getCause();
        if(x!=null) throw new Error(p+x.getMessage());
    }
    if(typeof(e.message)!="undefined" && e.message) throw new Error (p+e.message);
    throw new Error(p+"unknown error "+e.toString());
}
//******
function ax_file_saveTempB(name, body)
{
    try {
        return _ax().file_saveTempB(name, body);
    }
    catch(e) { _ax_throw(e, "ax_file_saveTemp"); }
}
function ax_file_saveTempS(name, body)
{
    try {
        return _ax().file_saveTempS(name, body);
    }
    catch(e) { _ax_throw(e, "ax_file_saveTemp"); }
}                
function ax_file_loadB(name)
{
    try {
        return _ax().file_loadB(name);
    }
    catch(e) { _ax_throw(e, "ax_file_load"); }
}
function ax_file_loadS(name) {
    try {
        return _ax().file_loadS(name);
    }
    catch (e) { _ax_throw(e, "ax_file_load"); }
}
function ax_file_loadMultipart(boundary,name,signature)
{
    try {
        return _ax().file_loadMultipart(boundary,name,signature);
    }
    catch(e) { _ax_throw(e,"ax_file_loadMultipart"); }
}
function ax_file_loadMultipart2(boundary,name,signature,tstamp) {
    try {
        return _ax().file_loadMultipart2(boundary,name,signature,tstamp);
    }
    catch(e) { _ax_throw(e,"ax_file_loadMultipart2"); }
}
function ax_file_deleteTemp(name)
{
    try {
        _ax().file_deleteTemp(name);
    }
    catch(e) { _ax_throw(e, "ax_file_deleteTemp"); }
}
function ax_file_getSize(name)
{
    try {
        return _ax().file_getSize(name);
    }
    catch(e) { _ax_throw(e, "ax_file_getSize"); }
}
function ax_file_getDate(name)
{
    try {
        return _ax().file_getDate(name);
    }
    catch(e) { _ax_throw(e, "ax_file_getDate"); }
}
function ax_file_saveFromASPXWS(aspx_ws,success,failure,fnameparse,stateChange,param1) {
    if(isIE)
        _xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    else
        _xmlhttp=new XMLHttpRequest();
    _xmlhttp.open('GET',aspx_ws,true);
    if(isFF) _xmlhttp.overrideMimeType('text/plain; charset=x-user-defined');
    _xmlhttp.onreadystatechange=function() {
        if(stateChange&&stateChange(_xmlhttp.readyState,param1)==true) return;
        if(_xmlhttp.readyState==4) {
            if(_xmlhttp.status==502) return failure(0,null,param1);
            if(_xmlhttp.status!=201) {
                var s=_xmlhttp.responseText;
                if(s.toUpperCase().indexOf("HTML")<0) s="Error 21000118: "+s;
                return failure(-1,s,param1);
            }
            try {
                var len=_xmlhttp.getResponseHeader("Content-Length");
                mime=_xmlhttp.getResponseHeader("Content-Type").split(';')[0];
                cdisp=_xmlhttp.getResponseHeader("content-disposition");
                fname=cdisp.substr(cdisp.indexOf('filename=')+9);
                if(fname=='') return failure(1,"Error 21000119: "+mime+'\n'+cdisp,param1);
                if(fnameparse) fname=fnameparse(fname,mime,len,param1);
                if(fname!=null&&fname!='') {
                    if(_ax_useActiveX()) {
                        if(_xmlhttp.getResponseHeader("Content-Transfer-Encoding")=="base64") return failure(-1,"Error 21000123",param1);
                        if(_xmlhttp.responseBody==null) return failure(-1,"Error 21000121",param1);
                        _fname=ax_file_saveTempB(fname,_xmlhttp.responseBody);
                    } else {
                        if(_xmlhttp.responseText==null) return failure(-1,"Error 21000122",param1);
                        if(_xmlhttp.getResponseHeader("Content-Transfer-Encoding")=="base64")
                            _fname=ax_file_saveTempB(fname,_xmlhttp.responseText);
                        else
                            _fname=ax_file_saveTempS(fname,_xmlhttp.responseText);
                    }
                }
                success(_fname,param1);
            }
            catch(e) {
                if(e.message) return failure(-1,"Error 21000117: "+e.message,param1);
                return failure(-1,"Error 21000120: "+e,param1);
            }
        }
    }
    _xmlhttp.send(null);
}
function ax_file_uploadToASPXWS(aspx_ws,fname,signature,tstamp) {
    //HACK: AX verze i pro IE7
    if(isIE)
        _xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    else
        _xmlhttp=new XMLHttpRequest();

    _xmlhttp.open("POST",aspx_ws,false);
    if(_ax_subv()>=32) {
        if(fname==null) fname="";
        if(signature==null) signature="";
        if(tstamp==null) tstamp="";
        var boundary="AXUPLOAD{A3EE83BD-69DE-4431-9D9D-80A8C1A79CE8}";
        _xmlhttp.setRequestHeader("Content-Type","multipart/form-data; boundary="+boundary);
        var content=ax_file_loadMultipart2(boundary,fname,signature,tstamp);
        if(_ax_useActiveX()) _xmlhttp.send(content);else _xmlhttp.send(""+content);
    } else if(_ax_subv()>=31) {
        if(fname==null) fname="";
        if(signature==null) signature="";
        var boundary="AXUPLOAD{A3EE83BC-69DE-4431-9D9D-80A8C1A79CE8}";
        _xmlhttp.setRequestHeader("Content-Type","multipart/form-data; boundary="+boundary);
        var content=ax_file_loadMultipart(boundary,fname,signature);
        if(_ax_useActiveX()) _xmlhttp.send(content);else _xmlhttp.send(""+content);
    } else {
        if(signature) {
            var sig=new String(signature).replace(/\s/g,'')
            _xmlhttp.setRequestHeader("Signature",sig);
        }
        if(fname) {
            _xmlhttp.setRequestHeader("Content-Type","application/octet-stream");
            //_xmlhttp.setRequestHeader("Content-Length", ax_file_getSize(fname));
            if(_ax_useActiveX()) {
                _xmlhttp.send(ax_file_loadB(fname));
            }
            else {
                _xmlhttp.setRequestHeader("Content-Transfer-Encoding","base64");
                _xmlhttp.setRequestHeader("X-Content-Transfer-Encoding","base64");
                _xmlhttp.send(ax_file_loadS(fname));
            }
        }
        else
            _xmlhttp.send(null);
    }
    return _xmlhttp.status;
}
function ax_file_moveToSubfolder(name,folder)
{
    try {
        _ax().file_moveToSubfolder(name,folder);
    }
    catch(e) { _ax_throw(e, "ax_file_moveToSubfolder"); }
}
function ax_file_openDialog(folder,ext,title)
{
    try {
        return _ax().file_openDialog(folder,ext,title);
    }
    catch(e) { _ax_throw(e, "ax_file_openDialog"); }
}
function ax_file_saveDialog(folder,file,ext,title)
{
    try {
        return _ax().file_saveDialog(folder,file,ext,title);
    }
    catch(e) { _ax_throw(e, "ax_file_saveDialog"); }
}
function ax_file_browseFolderDialog(folder,title)
{
    try {
        return _ax().file_browseFolderDialog(folder,title);
    }
    catch(e) { _ax_throw(e, "file_browseFolderDialog"); }
}
function ax_file_copyAs(folder,file,ext,title)
{
    try {
        return _ax().file_copyWithDialog(folder,file,ext,title);
    }
    catch(e) { _ax_throw(e, "ax_file_copyAs"); }
}
function ax_file_enumFiles(path,pattern)
{
    try {
        return _ax().file_enumFiles(path,pattern);
    }
    catch(e) { _ax_throw(e, "ax_file_enumFiles"); }
}
//******
function ax_shell_open(fname)
{
    try {
        return _ax().shell_open(fname);
    }
    catch(e) {
        if(e.message && e.message.indexOf("Aplikace pro zobraz")>=0)
        {
            return 1; //vse se "povedlo"
        }
        _ax_throw(e, "ax_shell_open");
    }
}
function ax_shell_print(fname)
{
    try {
        _ax().shell_print(fname);
    }
    catch(e) { _ax_throw(e, "ax_shell_print"); }
}
function ax_shell_getHome()
{
    try {
        return _ax().shell_getHome();
    }
    catch(e) { _ax_throw(e, "ax_shell_getHome"); }
}
function ax_shell_detect(fname)
{
    try {
        return _ax().shell_detect(fname);
    }
    catch(e) { _ax_throw(e, "ax_shell_detect"); }
}//******
function ax_crypto_sign(fname)
{
    try {
        var ret= _ax().crypto_sign(fname);
        return ret;
    }
    catch(e) { _ax_throw(e, "ax_crypto_sign"); }
}
function ax_crypto_signString(text)
{
    text = text.replace(/\r/g,"");
    text = text.replace(/\n/g,"\r\n");
    try {
        return _ax().crypto_signString(text);
    }
    catch(e) { _ax_throw(e, "ax_crypto_signString"); }
}
//function ax_crypto_verify(fname, signature)
//{
//    try {
//        return _ax().crypto_verify(fname, signature);
//    }
//    catch(e) { _ax_throw(e, "ax_crypto_verify"); }
//}
