<?php
  function createAccountForm()
  {
    global $connexion;
	global $txt;

    $html = '';

    $html .= '<ol class="breadcrumb">';
			$html .= '<li><a href="index.php">' . $txt['connexionBtn'] . '</a></li>';
      $html .= '<li class="active">' . $txt['accountCreation'] . '</li>';
		$html .= '</ol>';

		$html .= '<p>' . $txt['selectInstitution'] . '</p>';

    $html .= '<form class="form-horizontal" name="frmInstitution" id="frmInstitution" method="get" action="' . $_SERVER['PHP_SELF'] . '">';
      $html .=  '<input type="hidden" name="createAccount" value="yes">';
      $html .=  '<div class="form-group frmGroupCP">';
        $html .=  '<label class="control-label col-md-2" for="codePostal">' . $txt['postalCode'] . '</label>';
        $html .=  '<div class="col-md-2">';
          $html .=  '<input type="text" class="form-control" name="codePostal" id="codePostal" maxlength="6">';
        $html .=  '</div>';
        $html .=  '<div class="col-md-3">';
          $html .=  '<button type="submit" class="btn btn-primary" id="searchSchoolsButton">' . $txt['search'] . '</button>';
        $html .= '</div>';
      $html .=  '</div>';
    $html .= '</form>';

    $html .= '<p class="text-warning top-buffer-20">' . $txt['ifNoAreaCode'] . '</p>';

    if (isset($_GET['codePostal']))
    {
			$requete = 'SELECT * FROM t_liste_ecoles_08 WHERE REPLACE(CODE, " ", "") LIKE \'%' . escapeValue($_GET['codePostal']) . '%\' ORDER BY NOM ASC';
			if(!$resultat = $connexion->query($requete)){ die('There was an error running the query : School search'); }

			if ($resultat->num_rows == 0)
			{
				$html .= '<p class="text-danger"><span class="glyphicon glyphicon-exclamation-sign"></span><strong>' . $txt['noSchoolsFound'] . '</strong></p>';
			}
			else
			{
				$html .= '<p class="top-buffer-20"><strong>' . $txt['foundSchools'] . '</strong></p>';

				$html .= '<ul>';

					while ($ligne = $resultat->fetch_assoc())
					{
						$html .= '<li><a href="' . $_SERVER['PHP_SELF'] . '?createAccount&schoolId=' . $ligne['ID'] . '">' . $ligne['NOM'] . '</a></li>';
					}

				$html .= '</ul>';
			}
    }
    else if (isset($_GET['schoolId']))
		{
			$requeteVerif = "SELECT * FROM isoactif_t_usager_ecole WHERE u_e_id_fk = '" . escapeValue($_GET['schoolId']) . "'";
			if(!$resultatVerif = $connexion->query($requeteVerif)){ die('There was an error running the query : Get school info');}
			$nombrePourVerif = $resultatVerif->num_rows;

      // Si pas de compte pour cette école
			if ($nombrePourVerif == 0)
			{
				$requete = "SELECT * FROM t_liste_ecoles_08,t_arse WHERE a_id_fk = a_id AND ID = '" . escapeValue($_GET['schoolId']) . "'";
				if(!$resultat = $connexion->query($requete)){ die('There was an error running the query : Get all school info');}
				$ligne = $resultat->fetch_assoc();

				$id = $ligne['ID'];
				$ecole = $ligne['NOM'];
				$region = $ligne['a_nomComplet'];
				$add = $ligne['ADRESSE'];
				$vil = $ligne['VILLE'];
				$cp = $ligne['CODE'];
				$ind = $ligne['IND'];
				$tel = $ligne['TEL'];
				$secteur = $ligne['PRISEC'];
				$nbEtudiantsTotal = $ligne['NBETUD'];
				$id_commscol = $ligne['e_c_id_fk'];
				$id_arse = $ligne['a_id_fk'];

        $html .= '<hr></hr>';

			  $html .= '<form name="frmInscriptionISO" id="frmInscriptionISO" action="php/create-account.php" method="post">';

  				$html .= '<strong style="font-size:22px;">' . $ligne['NOM'] . '</strong>';

  				$html .= '<br style="clear:both;" />';
  				$html .= '<br style="clear:both;" />';

  				// ID de l'école (hidden)
  				$html .= '<input type="hidden" name="idEcole" value="' . $id . '">';

  			  // INSTANCE RÉGIONALE RSEQ
  				$html .= '<strong>' . $txt['RSEQregion'] . ' : </strong> ' . $ligne['a_nomComplet'];


          // NIVEAU / SECTEUR
          $html .= '<p class="top-buffer-20"><strong>' . $txt['sector'] . '</strong></p>';
          $html .= '<div class="form-group margin-top-0">';
            $html .= '<div class="radio">';
              $html .= '<label>';
                $html .= '<input type="radio" name="niveau" value="P"';
                  if ($secteur == 'P') {
                    $html .= ' checked'; }
                $html .= ' required>';
                $html .= $txt['elementarySector'];
              $html .= '</label>';
            $html .= '</div>';
            $html .= '<div class="radio">';
              $html .= '<label>';
                $html .= '<input type="radio" name="niveau" value="S"';
                  if ($secteur == 'S') {
                    $html .= ' checked'; }
                $html .= ' required>';
                $html .= $txt['highSchoolSector'];
              $html .= '</label>';
            $html .= '</div>';
            $html .= '<div class="radio">';
              $html .= '<label>';
                $html .= '<input type="radio" name="niveau" id="niveauPS" value="PS"';
                  if ($secteur == 'PS') {
                    $html .= ' checked'; }
                $html .= ' required>';
                $html .= $txt['bothSectors'];
              $html .= '</label>';
            $html .= '</div>';
          $html .= '</div>';


          // Niveau du formulaire d'auto-évaluation
          // Si école est Primaire ET secondaire
          $html .= '<div id="divChoixNiveauForm" style="display:none;">';
            $html .= '<p class="top-buffer-20"><strong>' . $txt['frmLevelChoice'] . '</strong></p>';
            $html .= '<div class="form-group margin-top-0">';
              $html .= '<div class="radio">';
                $html .= '<label>';
                  $html .= '<input type="radio" name="niveauForm" value="P"';
                    //if ($secteur == 'P') {
                      //$html .= ' checked'; }
                  $html .= '>';
                  $html .= $txt['elementarySector'];
                $html .= '</label>';
              $html .= '</div>';
              $html .= '<div class="radio">';
                $html .= '<label>';
                  $html .= '<input type="radio" name="niveauForm" value="S"';
                    //if ($secteur == 'S') {
                      //$html .= ' checked'; }
                  $html .= '>';
                  $html .= $txt['highSchoolSector'];
                $html .= '</label>';
              $html .= '</div>';
            $html .= '</div>';
          $html .= '</div>';

          // Nb tudiants garçons
          $html .= '<div class="form-group">';
            $html .= '<label for="nbEtudiantsGarcons">' . $txt['numStudentsBoys'] . '</label>';
            $html .= '<input class="form-control inputNumber" type="text" name="nbEtudiantsGarcons" style="max-width:100px;" required>';
          $html .= '</div>';

          // Nb tudiants filles
          $html .= '<div class="form-group">';
            $html .= '<label for="nbEtudiantsFilles">' . $txt['numStudentsGirls'] . '</label>';
            $html .= '<input class="form-control inputNumber" type="text" name="nbEtudiantsFilles" style="max-width:100px;" required>';
          $html .= '</div>';

          // Nb tudiants total
          $html .= '<div class="form-group">';
            $html .= '<label for="nbEtudiants">' . $txt['numStudentsTotal'] . '</label>';
            $html .= '<input class="form-control inputNumber" type="text" name="nbEtudiants" style="max-width:100px;" required>';
          $html .= '</div>';


          ##### INFORMATIONS PERSONNELLES ##########################################
  				$html .= '<p class="top-buffer-20"><strong style="font-size:18px;">' . $txt['personalInfo'] . '</strong></p>';

          // Prénom
          $html .= '<div class="form-group">';
            $html .= '<label for="prenom">' . $txt['firstName'] . '</label>';
            $html .= '<input class="form-control" type="text" name="prenom" style="max-width:400px;" required>';
          $html .= '</div>';

          // Nom
          $html .= '<div class="form-group">';
            $html .= '<label for="prenom">' . $txt['lastName'] . '</label>';
            $html .= '<input class="form-control" type="text" name="nom" style="max-width:400px;" required>';
          $html .= '</div>';

          // Fonction
          $html .= '<div class="form-group">';
            $html .= '<label for="fonction">' . $txt['function'] . '</label>';
            $html .= '<input class="form-control" type="text" name="fonction" style="max-width:400px;" required>';
          $html .= '</div>';

          // Courriel
          $html .= '<div class="form-group">';
            $html .= '<label for="courriel">' . $txt['email'] . '</label>';
            $html .= '<input class="form-control" type="email" name="courriel" id="courriel" style="max-width:400px;" required>';
          $html .= '</div>';

          // Courriel (Confirm)
          $html .= '<div class="form-group">';
            $html .= '<label for="courriel">' . $txt['emailConfirm'] . '</label>';
            $html .= '<input class="form-control" type="email" data-match="#courriel" data-match-error="' . $txt['doesNotMatch'] . '" name="courrielConfirm" style="max-width:400px;" required>';
            $html .= '<div class="help-block with-errors"></div>';
          $html .= '</div>';

          // Tél. travail
          $html .= '<div class="form-group">';
            $html .= '<label for="fonction">' . $txt['phoneWork'] . '</label>';
            $html .= '<input class="form-control" type="text" name="telBur" style="max-width:400px;" required>';
          $html .= '</div>';

          // Tél. résidence
          $html .= '<div class="form-group">';
            $html .= '<label for="fonction">' . $txt['phoneHome'] . '</label>';
            $html .= '<input class="form-control" type="text" name="telRes" style="max-width:400px;">';
          $html .= '</div>';

          // Tél. mobile
          $html .= '<div class="form-group">';
            $html .= '<label for="fonction">' . $txt['phoneMobile'] . '</label>';
            $html .= '<input class="form-control" type="text" name="telCell" style="max-width:400px;">';
          $html .= '</div>';

          $html .= '<hr></hr>';

					// Message - Conserver accès
          $html .= '<div class="alert alert-info top-buffer-20" role="alert">';
            $html .= $txt['saveAccessMsg'];
          $html .= '</div>';

          // Login
          $html .= '<div class="form-group" id="loginFormGroup">';
            $html .= '<label for="login">' . $txt['login'] . '</label>';
            $html .= '<input class="form-control" data-minlength="7" type="text" name="login" id="login" style="max-width:400px;" required>';
            $html .= '<div class="help-block">' . $txt['pwdMinLength'] . '</div>';
            $html .= '<span id="warningUsernameTaken" class="text-warning" style="display:none;"> <span class="glyphicon glyphicon-exclamation-sign"></span>' . $txt['loginTaken'] . '</span>';
          $html .= '</div>';

          // Pwd
          $html .= '<div class="form-group">';
            $html .= '<label for="fonction">' . $txt['password'] . '</label>';
            $html .= '<input class="form-control" data-minlength="7" type="password" name="password" id="password" style="max-width:400px;" required>';
            $html .= '<div class="help-block">' . $txt['pwdMinLength'] . '</div>';
          $html .= '</div>';

          // Pwd (Confirm)
          $html .= '<div class="form-group">';
            $html .= '<label for="fonction">' . $txt['confirmPassword'] . '</label>';
            $html .= '<input class="form-control" type="password" name="passwordConfirm" data-match="#password" data-match-error="' . $txt['doesNotMatch'] . '" style="max-width:400px;" required>';
            $html .= '<div class="help-block with-errors"></div>';
          $html .= '</div>';

					// CONFIRM AUTORISATION ET RESPONSABLE ISO
					$html .= '
          <div class="form-group">
            <div class="checkbox">
              <label>
                <input type="checkbox" name="autorisation" value="oui" required>' . $txt['dirAuthorisation'] . '
              </label>
            </div>
          </div>';

					$html .= '<div class="g-recaptcha top-buffer-30px" data-sitekey="6Leb9ToUAAAAACgitVOouG2npni5DdYWeE5aT7iZ" data-callback="enableBtn" data-expired-callback="disableBtn"></div>';

					// SUBMIT
          $html .= '<div class="text-center top-buffer-20"><button type="button" id="submitBtn" class="btn btn-primary" onclick="validateMyForm();" disabled>' . $txt['submit'] . '</button></div>';

				$html .= '</form>';
			}
			else
			{
				$html .= '<div class="alert alert-warning"><span class="glyphicon glyphicon-exclamation-sign"></span>' . $txt['accountAlreadyCreated'] . '</div>';
			}
		}

    //$html .= '<form role="form" name="form-create-account" id="form-create-account" method="post" action="php/create-account.php">';
    //$html .= '</form>';

    $html .= '';

    return $html;
  }
?>
