/** guifi_node_validate(): Confirm that an edited guifi item has fields properly filled in.
 */
function guifi_node_validate($node, $form)
{
    guifi_validate_nick($node->nick);
    // not at root zone
    if ($node->zone_id == 0 or $node->zone_id == guifi_zone_root()) {
        form_set_error('zone_id', t('Can\'t be assigned to root zone, please assign the node to an appropiate zone.'));
    } else {
        $nz = 0;
        guifi_zone_childs_tree_depth($node->zone_id, 3, $nz);
        if ($nz > 2) {
            form_set_error('zone_id', t('Can\'t be assigned to parent zone, please assign the node to an final zone.'));
        }
    }
    if ($node->elevation == 0) {
        $node->elevation = NULL;
    }
    if ($node->elevation < -1 && $node->elevation != NULL) {
        form_set_error('elevation', t('Elevation must be above the floor! :)'));
    }
    if ($node->elevation > 261 && $node->elevation != NULL) {
        form_set_error('elevation', t('Do you mean that you are flying over the earth??? :)'));
    }
    /*
     * Validate maintainer(s)
     */
    guifi_maintainers_validate($node);
    /*
     * Validate funder(s)
     */
    guifi_funders_validate($node);
}
function guifi_device_form_validate($form, &$form_state)
{
    global $user;
    guifi_log(GUIFILOG_TRACE, 'function guifi_device_form_validate()', $form_state['values']);
    guifi_log(GUIFILOG_TRACE, 'function guifi_device_form_validate(vlans)', $form_state['values'][vlans]);
    guifi_log(GUIFILOG_TRACE, 'function guifi_device_form_validate(aggregations)', $form_state['values'][aggregations]);
    guifi_log(GUIFILOG_TRACE, 'function guifi_device_form_validate(ipv4)', $form_state['values'][ipv4]);
    // nick
    if (isset($form['main']['nick'])) {
        guifi_validate_nick($form_state['values']['nick']);
        $query = db_query("\n      SELECT nick\n      FROM {guifi_devices}\n      WHERE lcase(nick)=lcase('%s')\n       AND id <> %d", strtolower($form_state['values']['nick']), $form_state['values']['id']);
        while (db_fetch_object($query)) {
            form_set_error('nick', t('Nick already in use.'));
        }
    }
    // ssid
    if (empty($form_state['values']['ssid'])) {
        $form_state['values']['ssid'] = $form_state['values']['nick'];
    }
    // Validate ip address(es)
    // Finding duplicates
    $ips = array();
    if (!empty($form_state['values']['ipv4'])) {
        // first checking local IPs
        foreach ($form_state['values']['ipv4'] as $keyS => $valueS) {
            if (empty($valueS[ipv4])) {
                continue;
            }
            // duplicate ip?
            if (in_array($valueS[ipv4], $ips)) {
                form_set_error("ipv4][{$keyS}][ipv4", t('address %addr is duplicated', array('%addr' => $valueS[ipv4])));
            }
            $ips[] = $valueS[ipv4];
            $valueSIPCalc = _ipcalc($valueS[ipv4], $valueS[netmask]);
            // Now looking into remote IPs
            foreach ($valueS[subnet] as $keyI => $valueI) {
                if (empty($valueI[ipv4])) {
                    continue;
                }
                guifi_log(GUIFILOG_TRACE, 'function guifi_device_form_validate(ipv4s)', $valueS[ipv4] . ' / ' . $valueI[ipv4] . ' ' . $valueI[did]);
                // duplicate ip?
                if (in_array($valueI[ipv4], $ips)) {
                    if ($valueI['new']) {
                        $field = "ipv4][{$keyS}][subnet][{$keyI}][ipv4txt";
                    } else {
                        $field = "ipv4][{$keyS}][subnet][{$keyI}][ipv4";
                    }
                    form_set_error($field, t('address %addr is duplicated', array('%addr' => $valueI[ipv4])));
                }
                $ips[] = $valueI[ipv4];
                // same subnet as related IP?
                $valueIIPCalc = _ipcalc($valueI[ipv4], $valueS[netmask]);
                if ($valueSIPCalc[netid] != $valueIIPCalc[netid] or $valueSIPCalc[maskbits] != $valueIIPCalc[maskbits]) {
                    form_set_error("ipv4][{$keyS}][subnet][{$keyI}][ipv4", t('address %addr1 not at same subnet as %addr2', array('%addr1' => $valueI[ipv4], '%addr2' => $valueS[ipv4])));
                }
                // remote id should be populated
                if (empty($valueI[did])) {
                    form_set_error("ipv4][{$keyS}][subnet][{$keyI}][did", t('Remote device for address %addr1 not specified', array('%addr1' => $valueI[ipv4])));
                }
            }
            // for remote IPs
        }
        // for local IPs
        /*   if (db_affected_rows(db_query("
              SELECT i.id
              FROM {guifi_interfaces} i,{guifi_ipv4} a
              WHERE i.id=a.interface_id AND a.ipv4='%s'
                AND i.device_id != %d",
              $form_state['values']['ipv4'],
              $form_state['values']['id']))) {
              $message = t('IP %ipv4 already taken in the database. Choose another or leave the address blank.',
                array('%ipv4' => $form_state['values']['ipv4']));
              form_set_error('ipv4',$message); 
            } */
    }
    // Validating vlans & aggregations
    foreach (array('vlans', 'aggregations') as $vtype) {
        foreach ($form_state['values'][$vtype] as $kvlan => $vlan) {
            // interface_type (name) should have a value
            if (empty($vlan['interface_type']) and !form_get_errors()) {
                $vlan['interface_type'] = substr($vtype, 0, 4) . $kvlan;
                form_set_value(array('#parents' => array($vtype, $kvlan, 'interface_type')), $vlan['interface_type'], $form_state);
                $form_state['rebuild'] = TRUE;
                drupal_set_message(t('Setting interface name to %name', array('%name' => $vlan['interface_type'])), 'warning');
            }
            // parent should exists
            if (empty($vlan['related_interfaces'])) {
                form_set_error("{$vtype}][{$kvlan}][related_interfaces", t('%name should have related interface(s)', array('%name' => $vlan[interface_type])));
            }
        }
    }
    // foreach vlans, aggregations
    // No duplicate names on interface names
    $ifs = guifi_get_currentInterfaces($form_state['values']);
    $iChecked = array();
    foreach ($ifs as $k => $iname) {
        if (in_array($iname, $iChecked)) {
            guifi_log(GUIFILOG_TRACE, 'function guifi_device_form_validate()', $iname);
            foreach (array('interfaces', 'vlans', 'aggregations', 'tunnels') as $iClass) {
                if (isset($form_state[values][$iClass][$k])) {
                    $f = $iClass . "][{$k}][interface_type";
                }
                form_set_error($f, t('Interface name %name duplicated', array('%name' => $iname)));
            }
        }
        $iChecked[] = $iname;
    }
    if (count($form_state['values']['radios'])) {
        foreach ($form_state['values']['radios'] as $k => $v) {
            guifi_radios_validate($k, $v, $form_state['values']);
        }
    }
    guifi_maintainers_validate(array2object($form_state['values']));
    guifi_funders_validate(array2object($form_state['values']));
}
function guifi_zone_validate($node)
{
    // if node master is root, check that there is not another zone as root
    if ($node->master == 0) {
        $qry = db_query('SELECT id, title, nick
            FROM {guifi_zone}
            WHERE master = 0');
        while ($rootZone = db_fetch_object($qry)) {
            if ($node->nid != $rootZone->id) {
                form_set_error('master', t('The root zone is already set to "%s". Only one root zone can be present at the database. Delete/change the actual root zone before assigning a new one or choose another parent.', array('%s' => $rootZone->title)));
            }
        }
    }
    // check if master zone is a child
    $childs = guifi_zone_childs($node->nid);
    foreach ($childs as $key => $child) {
        if ($child == $node->master) {
            $childname = db_fetch_object(db_query('SELECT title
            FROM {guifi_zone}
            WHERE id = %s', $child));
            form_set_error('master', t("You can't use a child zone <strong>%child</strong> from %zone as master!!!", array('%child' => $childname->title, '%zone' => $node->title)));
        }
    }
    // check that master is not being assigned to itself
    if (!empty($node->nid)) {
        if ($node->master == $node->nid) {
            form_set_error('master', t("Master zone can't be set to itself"));
        }
    }
    // check that zone area is consistent
    if ($node->minx > $node->maxx) {
        form_set_error('minx', t("Longitude: Min should be less than Max") . ' ' . $node->minx . '/' . $node->maxx);
    }
    if ($node->miny > $node->maxy) {
        form_set_error('miny', t("Latitude: Min should be less than Max"), ' ' . $node->miny . '/' . $node->maxy);
    }
    /*
     * Validate maintainer(s)
     */
    guifi_maintainers_validate($node);
}