/**
* Редактирование контакта.
*
* @param    array   $frm    Данные контакта
*/
function EditContact($frm)
{
    session_start();
    $objResponse = new xajaxResponse();
    if (hasPermissions('ourcontacts')) {
        $error = 0;
        $name = trim(strip_tags(stripslashes($frm['fld_edit_name'])));
        $surname = trim(strip_tags(stripslashes($frm['fld_edit_surname'])));
        $company = trim(strip_tags(stripslashes($frm['fld_edit_company'])));
        $note = trim(strip_tags(stripslashes($frm['fld_edit_note'])));
        $group = intval($frm['fld_edit_group']);
        if ($name == '' || strlen($name) > 250) {
            $error = 1;
            $objResponse->script("alert('Имя не может быть пустым и должно быть менее 250 символов');");
        }
        if ($surname == '' || strlen($surname) > 250) {
            $error = 1;
            $objResponse->script("alert('Фамилия не может быть пустым и должно быть менее 250 символов');");
        }
        if ($group < 1) {
            $error = 1;
            $objResponse->script("alert('Вы не выбралю группу');");
        }
        $emails = array();
        $frm['fld_edit_email'] = trim(strip_tags(stripslashes($frm['fld_edit_email'])));
        if ($frm['fld_edit_email']) {
            array_push($emails, $frm['fld_edit_email']);
        }
        for ($i = 1; $i < 5; ++$i) {
            $frm['fld_edit_email_' . $i] = trim(strip_tags(stripslashes($frm['fld_edit_email_' . $i])));
            if ($frm['fld_edit_email_' . $i]) {
                array_push($emails, $frm['fld_edit_email_' . $i]);
            }
        }
        reset($emails);
        if (!$emails) {
            $error = 1;
            $objResponse->script("alert('Email не может быть пустым');");
        }
        foreach ($emails as $email) {
            if (!is_email($email)) {
                $error = 1;
                $objResponse->script("alert('Неправильно введен email');");
            }
        }
        $phones = array();
        $frm['fld_edit_phone'] = trim(strip_tags(stripslashes($frm['fld_edit_phone'])));
        if ($frm['fld_edit_phone']) {
            array_push($phones, $frm['fld_edit_phone']);
        }
        for ($i = 1; $i < 5; ++$i) {
            $frm['fld_edit_phone_' . $i] = trim(strip_tags(stripslashes($frm['fld_edit_phone_' . $i])));
            if ($frm['fld_edit_phone_' . $i]) {
                array_push($phones, $frm['fld_edit_phone_' . $i]);
            }
        }
        $skypes = array();
        $frm['fld_edit_skype'] = trim(strip_tags(stripslashes($frm['fld_edit_skype'])));
        if ($frm['fld_edit_skype']) {
            array_push($skypes, $frm['fld_edit_skype']);
        }
        for ($i = 1; $i < 5; ++$i) {
            $frm['fld_edit_skype_' . $i] = trim(strip_tags(stripslashes($frm['fld_edit_skype_' . $i])));
            if ($frm['fld_edit_skype_' . $i]) {
                array_push($skypes, $frm['fld_edit_skype_' . $i]);
            }
        }
        $icqs = array();
        $frm['fld_edit_icq'] = trim(strip_tags(stripslashes($frm['fld_edit_icq'])));
        if ($frm['fld_edit_icq']) {
            array_push($icqs, $frm['fld_edit_icq']);
        }
        for ($i = 1; $i < 5; ++$i) {
            $frm['fld_edit_icq_' . $i] = trim(strip_tags(stripslashes($frm['fld_edit_icq_' . $i])));
            if ($frm['fld_edit_icq_' . $i]) {
                array_push($icqs, $frm['fld_edit_icq_' . $i]);
            }
        }
        $others = array();
        $frm['fld_edit_other'] = trim(strip_tags(stripslashes($frm['fld_edit_other'])));
        if ($frm['fld_edit_other']) {
            array_push($others, $frm['fld_edit_other']);
        }
        for ($i = 1; $i < 5; ++$i) {
            $frm['fld_edit_other_' . $i] = trim(strip_tags(stripslashes($frm['fld_edit_other_' . $i])));
            if ($frm['fld_edit_other_' . $i]) {
                array_push($others, $frm['fld_edit_other_' . $i]);
            }
        }
        if (!$error) {
            $contact['id'] = $frm['fld_edit_id'];
            $contact['name'] = $name;
            $contact['surname'] = $surname;
            $contact['company'] = $company;
            $contact['group'] = $group;
            $contact['note'] = $note;
            $contact['emails'] = $emails;
            $contact['phones'] = $phones;
            $contact['skypes'] = $skypes;
            $contact['icqs'] = $icqs;
            $contact['others'] = $others;
            contacts::editContact($contact);
            $objResponse->script("alert('Контакт успешно сохранен'); window.location='/siteadmin/contacts';");
        }
    }
    return $objResponse;
}