function login_crm (){
    var local_login = function (clientContext) {
        ecid = clientContext.getEcid();
        //alert('ecid: ' + ecid + '\n' + 'isAuth: ' + clientContext.isAuth());
        //winopen('ecid: ' + ecid + '\n' + 'isAuth: ' + clientContext.isAuth(), 'debug', 640, 480, 1);
        if(ecid && clientContext.isAuth() == true) {
            document.getElementById('message').style.display = '';
            document.getElementById('messageShirma').style.display = '';
            document.getElementById('content').style.display = 'none';
            document.getElementById('message1').innerHTML = '<b>Идет проверка статуса авторизации. Пожалуйста, подождите...</b>';
            tests_form_crm('/auth/login/','authorize_ecid', ecid);
        }
    }
    AgavaAPI.CRM.DoAuth(local_login);
    //exitFromAuth();
    //dialogShow();
    //return false;
}

function anketa_crm (){
    AgavaAPI.CRM.DoUpdateProfile();
    //DoRegistry();
    //return false;
}

function register_crm (){
    var registration_callback = function (status) {
        if (!status || status == null || status == '' || status == 0) {
            var autoadd_img = document.getElementById('autoadd');
            if (autoadd_img && /\/images\/null\.gif/.test(autoadd_img.src)) {
                document.getElementById('autoadd').src = 'http://' + (location.host == 'test1.testsbox.ru'?'127.0.0.1':'s.agava.ru') + '/cgi/actions/autoadd.cgi?' + document.getElementById('autoadd_qs').value;
            }
        }
    }
    AgavaAPI.CRM.DoRegistry(registration_callback);
    //exitFromAuth();
    //DoRegistry();
    //return false;
}

function logout_crm (){
    var local_logout = function () {
        document.getElementById('message').style.display = '';
        document.getElementById('messageShirma').style.display = '';
        document.getElementById('content').style.display = 'none';
        document.getElementById('message1').innerHTML = '<b>Идет проверка статуса авторизации. Пожалуйста, подождите...</b>';
        tests_form_crm('/auth/logout/', '', '');
    }
    AgavaAPI.CRM.DoLogout(local_logout);
    //exitFromAuth({callback:local_logout});
    //return false;
}

function tests_form_crm (action, cmd, ecid) {
    var f = document.getElementById("form_tests_by_ecid");
    f.action = action;
    f.cmd.value = cmd;
    f.ecid.value = ecid;
    f.submit();
}

function winopen (data,myname,w,h,scroll){
    var winl = (screen.width-w)/2;
    var wint = (screen.height-h)/2;
    var settings ='height='+h+',';
        settings +='width='+w+',';
        settings +='top='+wint+',';
        settings +='left='+winl+',';
        settings +='scrollbars='+scroll+',';
        settings +='resizable=yes';
        settings +='menubar=yes';
        settings +='alwaysRaised=no';
    debug_window=window.open('about:blank',myname,settings);
    debug_window.document.write(data);
    debug_window.window.focus();
}

function notify (p) {
    var script = document.createElement("script");
    script.src = '/notify?subject=' + p.subject?p.subject:'<No subject>' + '&body=' + p.body?p.body:'<No body>' + '&caller=script&priority=' + p.priority?p.priority:'normal';
    script.type = 'text/javascript';
    document.body.appendChild(script);
}
