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_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_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
	        { alert(1);
		        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 (isIE) {
                        if(_xmlhttp.responseBody==null) return failure(-1,"Error 21000121",param1);
                        
                        alert(fname);
                        alert(_xmlhttp.responseBody);
                        
                        _fname = ax_file_saveTempB(fname, _xmlhttp.responseBody);                        
                    }else{
                        if(_xmlhttp.responseText==null) return failure(-1,"Error 21000122",param1);
                        _fname = ax_file_saveTempS(fname, _xmlhttp.responseText);
                    }
                }
                success(_fname,param1);
            }
            catch(e)
            {
                alert(e.message);
                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)
{
    //HACK: AX verze i pro IE7
    if (isIE) 
	    _xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    else
        _xmlhttp = new XMLHttpRequest();
        
    _xmlhttp.open("POST",aspx_ws,false);
    if(signature)
    {
        var sig = new String(signature).replace(/\s/g,'')
        _xmlhttp.setRequestHeader("Signature",sig);
    }
    if(fname)
    {
        var size = ax_file_getSize(fname)
        content = ax_file_loadB(fname);
        _xmlhttp.setRequestHeader("Content-Type","application/octet-stream");
        //_xmlhttp.setRequestHeader("Content-Length", size);
        if (isIE) 
        {
            _xmlhttp.setRequestHeader("Content-Transfer-Encoding","binary");
            _xmlhttp.send(content);
        }
        else
        {
            _xmlhttp.setRequestHeader("Content-Transfer-Encoding","base64");
            _xmlhttp.send(btoa(content));
        }
    }
    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"); }
//}
