/*global $, $F, Ajax, call_accepted, call_could_not_connect, call_declined, call_no_answer, request_id, return_data, validate_data */
function submit_placecall()
{
    if ($('btnSubmit').hasClassName('inactive'))
    {
        return;
    }

    //reset error
    $('country_err').hide();
    $('fullname_err1').hide();
    $('fullname_err2').hide();
    $('fullname_err3').hide();
    $('phone_err1').hide();
    $('phone_err2').hide();
    $('phone_err3').hide();

    if ($F('country').split('*')[0] == '??')
    {
        $('country_err').show();
        return;
    }

    if ($F('fullname').length === 0)
    {
        $('fullname_err1').show();
        $('fullname').select();
        return;
    }
    if ($F('fullname').length > 50)
    {
        $('fullname_err2').show();
        $('fullname').select();
        return;
    }

    if (!validate_data("req", $('phone'), ""))
    {
        $('phone_err1').show();
        $('phone').select();
        return;
    }
    if (!validate_data("maxlen=20", $('phone'), ""))
    {
        $('phone_err2').show();
        $('phone').select();
        return;
    }
    if (!validate_data("phone", $('phone'), ""))
    {
        $('phone_err3').show();
        $('phone').select();
        return;
    }

    $('frmPlacecall').submit();
}


function on_country_changed()
{
    $('country_code').value = ($('country').value != '??*XX') ? '+' + $('country').value.split('*')[1] : '';
}


function pollCallmeStatus(pe)
{
    //use of global var: request_id
    var opt = {
        parameters: {'request_id': request_id},
        onSuccess: function (t) {
            return_data = ('(' + t.responseText + ')').evalJSON();
            if (return_data.result == 'ok')
            {
                if (return_data.statusMsg == "CallingMember")
                {
                    return;
                }

                switch (return_data.statusMsg)
                {
                case "Accepted":
                    $('call_status_msg').update(call_accepted);
                    break;
                case "Declined":
                    $('call_status_msg').update(call_declined);
                    break;
                case "NoAnswer":
                    $('call_status_warning').show();
                    $('call_status_msg').update(call_no_answer);
                    break;
                default:
                    $('call_status_warning').show();
                    $('call_status_msg').update(call_could_not_connect);
                }
            }
            else
            {
                $('call_status_warning').show();
                $('call_status_msg').update(call_could_not_connect);
            }
            pe.stop();
        },
        onFailure: function (t) {
            $('call_status_warning').show();
            $('call_status_msg').update(call_could_not_connect);
            pe.stop();
        }
    };

    var tmp = new Ajax.Request('/callme/pollstatus/', opt);
}