<div id="content" class="inscription">

    <?php
      if(isset($_POST['boton'])){

        session_start();
        if(strtoupper($_REQUEST["captcha"]) == $_SESSION["captcha"]){
          $_SESSION["captcha"] = md5(rand()*time());
          $text_captcha = 1;
        }else{
          $_SESSION["captcha"] = md5(rand()*time());
          $text_captcha = 0;
          $result = '<div class="result_fail">Wrong captcha! please, try again.</div>';
        }

        if($text_captcha == 1){
          if($_POST['nombre'] == ''){
            $error1 = '<span class="error">Insert your first name, please</span>';
          }else if($_POST['apellido'] == ''){
            $error2 = '<span class="error">Insert your last name, please</span>';
          }else if($_POST['email'] == '' or !preg_match("/^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/",$_POST['email'])){
            $error3 = '<span class="error">Invalid e-mail!</span>';
          }else if($_POST['affiliation'] == ''){
            $error4 = '<span class="error">Insert an affiliation, please</span>';
          }else if(empty($_POST['talk'])){
            $error5 = '<span class="error">Will you present a talk? yes or not?</span>';
          }else{      
            $dest = "fof@oac.uncor.edu"; //Email de destino
            $nombre = $_POST['nombre'];
            $apellido = $_POST['apellido'];
            $email = $_POST['email'];
            $affiliation = $_POST['affiliation'];
            $talk = $_POST['talk'];
            if($_POST['topic'] == 'Choose...' or $_POST['topic'] == 'Other'){
              $topic = $_POST['new_topic'];
            }else{
              $topic = $_POST['topic'];
            }
            $asunto = "Inscription $nombre $apellido";

            //Cabeceras del correo
            $header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
            $header1 = '=?UTF-8?B?'.base64_encode($nombre . $apellido).'?=';
            $header  = "From: " . $header1; //Quien envia?
            $headers = $header_ . $header;

            $cuerpo  = "Inscription:\n\n";
            $cuerpo .= "First name: $nombre\n\n";
            $cuerpo .= "Last name: $apellido\n\n";
            $cuerpo .= "E-mail: $email\n\n";
            $cuerpo .= "Affiliation: $affiliation\n\n";
            $cuerpo .= "Talk: $talk\n\n";
            $cuerpo .= "Topic: $topic\n\n";
            
            if(mail($dest,'=?UTF-8?B?'.base64_encode($asunto).'?=',$cuerpo,$headers)){

              $cuerpo  = "FoF Meeting 2014\n\n";
              $cuerpo .= "Your pre-inscription was processed succefully.\n\n";
              $cuerpo .= "Kind regards, LOC.-\n";
              $cuerpo .= "http://fof.oac.uncor.edu\n";

              $headers = 'From: FoF2014' . "\r\n" .
              'Reply-To: fof@oac.uncor.edu' . "\r\n" .
              'X-Mailer: PHP/' . phpversion();

              mail($email,"FoF 2014 Inscription",$cuerpo,$headers); 

              $result = '<div class="result_ok">Email sent successfully :)</div>'; 

              $file = "/var/www/webs/data.txt";
              $fp = fopen($file, 'a') or die("Couldn't open $file for writing!");
              $cuerpo = "$nombre $apellido $affiliation $email Talk: $talk\n";
              fwrite($fp, $cuerpo);
              fclose($fp);

              //reseteamos
              $_POST['nombre'] = '';
              $_POST['apellido'] = '';
              $_POST['email'] = '';
              $_POST['affiliation'] = '';
              $_POST['talk'] = '';
              $_POST['new_topic'] = '';

            }else{
              $result = '<div class="result_fail">Error while sending email :(</div>';
            }
          }
        }
      }
    ?>
    <form class='contacto' method='POST' action=''>
      <div>
        <label>First name:</label>
        <input type='text' class='nombre' name='nombre' value='<?php echo $_POST['nombre']; ?>'><?php echo $error1 ?>
      </div>
      <div>
        <label>Last name:</label>
        <input type='text' class='apellido' name='apellido' value='<?php echo $_POST['apellido']; ?>'><?php echo $error2 ?>
      </div>
      <div>
        <label>E-mail:</label>
        <input type='text' class='email' name='email' value='<?php echo $_POST['email']; ?>'><?php echo $error3 ?>
      </div>
      <div>
        <label>Affiliation:</label>
        <input type='text' class='affiliation' name='affiliation' value='<?php echo $_POST['affiliation']; ?>'><?php echo $error4 ?>
      </div>
      <br>
      <div>
        <label>Will you present a talk?:</label>
        yes: <input type="radio" class='talk' name="talk" value="yes">
        no:  <input type="radio" class='talk' name="talk" value="no"><?php echo $error5 ?>
      </div>
      <br>
      <div>
       Choose a topic:
       <SELECT name="topic">
         <OPTION VALUE="Choose...">Choose one...</OPTION>
         <OPTION VALUE="Galaxy groups and clusters">Galaxy groups and clusters</OPTION>
         <OPTION VALUE="Large scale structure">Large scale structure</OPTION>
         <OPTION VALUE="Radiogalaxies and AGN's">Radiogalaxies and AGN's</OPTION>
         <OPTION VALUE="History of Astronomy">History of Astronomy</OPTION>
         <OPTION VALUE="Galactic Astronomy">Galactic Astronomy</OPTION>
         <OPTION VALUE="Instrumentation">Instrumentation</OPTION>
         <OPTION VALUE="Solar Physics">Solar Physics</OPTION>
         <OPTION VALUE="Simulations">Simulations</OPTION>
         <OPTION VALUE="Exoplanets">Exoplanets</OPTION>
         <OPTION VALUE="Other">Other</OPTION>
       </SELECT>
      </div>
      <div style="font-size: 70%; padding: 0px 0px;">
        <?php $_POST['new_topic'] = 'if you have chosen "Other" write your topic here, please' ?>
        <input type='text' class='new_topic' name='new_topic' value='<?php echo $_POST['new_topic']; ?>'>
      </div>
      <br/>
      <div>
        <img src="captcha.php" /><br/>
        <input type="text" value="insert the captcha here, please" size="16" name="captcha"/>
      </div>
      <br>
      <div>
        <input type='submit' value='Send' class='boton' name='boton'>
      </div>
      <?php echo $result; ?>
    </form>
  </div>