function _ax()
{
    var ax = GetGlobalManager().GetValue('ax');
    //if(ax==null) window.alert("ax null");
    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_file_saveTempB(name, body)
{
    try {
        return _ax().file_saveTempB(name, body);
    }
    catch(e) {
        window.alert("ax_file_saveTemp error: " + e.message);
        throw e;
    }
}
function ax_file_saveTempS(name, body)
{
    try {
        return _ax().file_saveTempS(name, body);
    }
    catch(e) {
        window.alert("ax_file_saveTemp error: " + e.message);
        throw e;
    }
}                
function ax_file_loadB(name)
{
    try {
        return _ax().file_loadB(name);
    }
    catch(e) {
        window.alert("ax_file_load error: " + e.message);
        throw e;
    }
}                
function ax_file_deleteTemp(name)
{
    try {
        _ax().file_deleteTemp(name);
    }
    catch(e) {
        window.alert("ax_file_deleteTemp error: " + e.message);
        throw e;
    }
}
function ax_file_getSize(name)
{
    try {
        return _ax().file_getSize(name);
    }
    catch(e) {
        window.alert("ax_file_getSize error: " + e.message);
        throw  e;
    }
}
function ax_file_getDate(name)
{
    try {
        return _ax().file_getDate(name);
    }
    catch(e) {
        window.alert("ax_file_getDate error: " + e.message);
        throw  e;
    }
}
function ax_file_saveFromASPXWS(aspx_ws,success,failure,fnameparse,stateChange,param1)
{
    _xmlhttp = new XMLHttpRequest();
    _xmlhttp.open('GET', aspx_ws, true);
    if(Sys.Browser.agent==Sys.Browser.Firefox) 
        _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)
	        {
	            return failure(-1,_xmlhttp.responseText,param1);
	        }
		    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,mime+'/'+cdisp,param1);
		    }			
		    if(fnameparse) fname=fnameparse(fname,mime,len,param1);
		    if(fname!='')
		    {
                if(Sys.Browser.agent==Sys.Browser.InternetExplorer) 
                    _fname = ax_file_saveTempB(fname, _xmlhttp.responseBody);
                else
                    _fname = ax_file_saveTempS(fname, _xmlhttp.responseText);
            }
            success(_fname,param1);
	    }
    }
    _xmlhttp.send(null);
}
function ax_file_uploadToASPXWS(aspx_ws,fname,signature)
{
    //HACK: AX verze i pro IE7
    if(Sys.Browser.agent==Sys.Browser.InternetExplorer) 
	    _xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    else
        _xmlhttp = new XMLHttpRequest();
        
    _xmlhttp.open("POST",aspx_ws,false);
    if(signature)
        _xmlhttp.setRequestHeader("Signature",signature.replace(/\s/g,''));
    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(Sys.Browser.agent==Sys.Browser.InternetExplorer) 
        {
            _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) {
        window.alert("ax_file_moveToSubfolder error: " + e.message);
        throw  e;
    }
}
function ax_file_openDialog(folder,ext,title)
{
    try {
        return _ax().file_openDialog(folder,ext,title);
    }
    catch(e) {
        window.alert("ax_file_openDialog error: " + e.message);
        throw  e;
    }
}
function ax_file_saveDialog(folder,file,ext,title)
{
    try {
        return _ax().file_saveDialog(folder,file,ext,title);
    }
    catch(e) {
        window.alert("ax_file_saveDialog error: " + e.message);
        throw  e;
    }
}
function ax_file_copyAs(folder,file,ext,title)
{
    try {
        return _ax().file_copyWithDialog(folder,file,ext,title);
    }
    catch(e) {
        window.alert("ax_file_copyAs error: " + e.message);
        throw  e;
    }
}
//******
function ax_shell_open(fname)
{
    try {
        return _ax().shell_open(fname);
    }
    catch(e) {
        if(e.message.indexOf("Aplikace pro zobraz")>=0)
        {
            return 1; //vse se "povedlo"
        }
        window.alert("ax_shell_open error: " + e.message);
        throw  e;
    }
}
function ax_shell_print(fname)
{
    try {
        _ax().shell_print(fname);
    }
    catch(e) {
        window.alert("ax_shell_print error: " + e.message);
        throw  e;
    }
}
//******
function ax_crypto_sign(fname)
{
    try {
        return _ax().crypto_sign(fname);
    }
    catch(e) {
        window.alert("ax_crypto_sign error: " + e.message);
        throw  e;
    }
}
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) {
        window.alert("ax_crypto_signString error: " + e.message);
        throw  e;
    }
}
//function ax_crypto_verify(fname, signature)
//{
//    try {
//        return _ax().crypto_verify(fname, signature);
//    }
//    catch(e) {
//        window.alert("ax_crypto_verify error: " + e.message);
//        throw  e;
//    }
//}
