<?php
// process.php

$errors         = array();      // array to hold validation errors
$data           = array();      // array to pass back data

function setheaders() {
    $fp = fopen('registro.csv', 'w');
    $cabezal = array('Nombre', 'Apellido','TipoDoc','Documento',
                    'Direccion','Pais','Ciudad', 'Telefono', 'Email',
                    'Profesión','Financiación','Detalle Financiación');
    fputcsv($fp,$cabezal);
    fclose($fp);
}
function registrar($fila){
    $fp = fopen('registro.csv', 'w');
    fputcsv($fp,$fila);
    fclose($fp);
}

// validate the variables ======================================================
    // if any of these variables don't exist, add an error to our $errors array
    $data['nombre'] = $_POST['nombre'];
    $data['apellido'] = $_POST['apellido'];
    $data['tipodoc'] = $_POST['doctype'];
    $data['nrodoc'] = $_POST['docnro'];    
    $data['direccion'] = $_POST['dir'];
    $data['pais'] = $_POST['pais'];
    $data['ciudad'] = $_POST['ciudad'];
    $data['telefono'] = $_POST['tel'];
    $data['email'] = $_POST['email'];
    $data['profesion'] = $_POST['profesion'];
    $data['financiacion'] = $_POST['financiacion'];
    $data['detallefinan'] = $_POST['detallefinan'];
    
    if (empty($_POST['nombre']))
        $errors['nombre'] = 'Nombre is required.';

    if (empty($_POST['apellido']))
        $errors['nombre'] = 'Apellido is required.'; 

    if (empty($_POST['email']))
        $errors['email'] = 'Email is required.';

    if (empty($_POST['mensage']))
        $errors['mensage'] = 'Mensaje is required.';

    if( !file_exists("registro.csv")){
        setheaders();        
    }


    //$fp = fopen("registro.csv", "w");


// return a response ===========================================================

    // if there are any errors in our errors array, return a success boolean of false
    if ( ! empty($errors)) {

        // if there are items in our errors array, return those errors
        $data['success'] = false;
        $data['errors']  = $errors;
    } else {
        $fila = array();


        // if there are no errors process our form, then return a message

        // DO ALL YOUR FORM PROCESSING HERE
        // THIS CAN BE WHATEVER YOU WANT TO DO (LOGIN, SAVE, UPDATE, WHATEVER)

        // show a message of success and provide a true success variable
       


        $data['success'] = true;
        $data['message'] = 'Registro exitoso';
        
    }

    // return all our data to an AJAX call
    echo json_encode($data);

?>