<?php

global $user;

define('SATELLITE_OFF', FALSE);

if (isset($user->uid) && $user->uid == 103) {
  define('QLA', TRUE);
} else {
  define('QLA', FALSE);
}

if (isset($_POST['commune']) && preg_match('/[0-9AB]/', $_POST['commune'])) {
  $result = db_select('communes', 'c')->fields('c', array('commune'))->condition('c.code_insee', $_POST['commune'])->execute();

  if ($res = $result->fetchAssoc()) {
    $commune = $res['commune'];

    $_SESSION['address']['address'] = (empty($_POST['numaddress']) ? $_POST['address'] : $_POST['numaddress'].' '.$_POST['address']);
    $_SESSION['address']['codepostal'] = $_POST['codePostal'];
    $_SESSION['address']['commune'] = $commune;
  }
}


require_once ('soap/AsgardWSClient.php');
setlocale(LC_MONETARY, 'fr_FR.UTF-8');
//session_start();
?><script type="text/javascript">
//<!--
<?php if (SATELLITE_OFF) { ?>
  var Onglet_afficher = 2;
<?php } else { ?>
  var Onglet_afficher = 1;
<?php } ?>
function Affiche(Nom)
{
document.getElementById('affiche-contenu-'+Onglet_afficher).className = 'inactif onglet';
document.getElementById('affiche-contenu-'+Nom).className = 'affiche-contenu-1 onglet';
document.getElementById('contenu_'+Onglet_afficher).style.display = 'none';
document.getElementById('contenu_'+Nom).style.display = 'block';
Onglet_afficher = Nom;
}
//-->
</script>


<?php
if (isset($_POST)) {
try {

$asgard = new AsgardWSClient();
// on recupere toutes les offres
$offres_dispos = $asgard->GetOffresDisponiblesWeb($_POST['commune'], $_POST['latitude'], $_POST['longitude']);
// on recupere toutes les offres éligibles
$_SESSION['offres'] = $offres_dispos;
/*
echo '<pre>';
print_r($offres_dispos);
echo '</pre>';
*/
$type = $_POST['type'];
?><?php
}
catch (Exception $e) {
//echo "Exception reçue : $e->getMessage()";
                                                        ?>
<p><span style="margin: 15px; padding: 10px; font-size: 130%; font-weight: bold; color: red;">Une erreur est survenue pendant la récupération des offres. Veuillez réessayer plus tard.</span> <?php
}
}
else {
header("location:./index.html");
die();
}
?></p>


 <header class="small">
  <div class="content">
  <h1 class="big">
  <strong>Mon compte</strong></h1>
  </div>
  </header>

<div class="menu-tunnel">
  <div class="content">
    <ul>
      <li class="active"><span>1</span>Ma formule</li>
      <li><span>2</span>Mes options</li>
      <li><span>3</span>Mes coordonnées</li>
      <li><span>4</span>Validation</li>
    </ul>
  </div>
</div>

<div class="offres-title">
     <div class="content">
          CLIQUEZ SUR LES TECHNOLOGIES DISPONIBLES ET DÉCOUVREZ VOS OFFRES :
     </div>
</div>

<div class="cadre-offres">
     <div class="menu-offre-type">

<ul class="conteneur-onglets content">
<li class="inactif onglet" id="affiche-contenu-2" onclick="javascript:Affiche('2');">
RADIO </li>
<li class="inactif onglet" id="affiche-contenu-3" onclick="javascript:Affiche('3');">
FIBRE </li>
<?php if (!SATELLITE_OFF) { ?>
<li class="inactif onglet" id="affiche-contenu-1" onclick="javascript:Affiche('1');">
SATELLITE </li>
<?php } ?>
</ul>
</div>

<div class="list-offres">
<div class="content">
<h3><span>LES FORFAITS</span></h3>

<?php if (!SATELLITE_OFF) { ?>

<div class="contenu" id="contenu_1">
<div class="cadre-interne-offres">
<?php
// on verifie s'il y a une offre sattelite
$nboffre=0;
  foreach ($offres_dispos->SOffre as $key => $offre) {
    if($offre->categorie == "SATELLITE" && !$offre->satellite_local){
      $nboffre++;
    }
  }


if($nboffre>0){ /* desactivé le 24/08/2015 */
// récupère les infos de subvention
// on se connecte à la seconde base pour y faire une recherche
connectextra();
$departement = substr($_POST['commune'],0,2);
$codepostal = $_POST['codePostal'];
$requete = mysql_query("SELECT * FROM liste WHERE cp = $codepostal AND ( materiel != '' OR installation != ''  )");
$i=0;
while($row = mysql_fetch_assoc($requete))
              {
$departement = $row["departement"];
$cp = $row["cp"];
$materiel = $row["materiel"];
$installation = $row["installation"];
$modalites = $row["modalites"];
$i++;
}
if( $i > 0 ){
echo '<div class="banniere-promo-offre" style="background: url(sites/default/files/offre/bg-bonne-nouvelle-new.jpg) no-repeat; height: 127px; padding-top: 8px; padding-left:55px;   margin-bottom: 45px; width: 949px; box-sizing: border-box;"><a class="fancybox fancybox.iframe" href="./devspe/subvention.php?codepostal='.$codepostal.'">
<div class="bonne-nouv">
<strong>Bonne nouvelle !</strong><br/>
Vous avez droit à une subvention
</div>
<div class="prix-matos">';
if( $materiel != "") echo '<span style="color:#032B55; font-size:26px; font-weight:bold; font-style:italic;">'.htmlentities($materiel).'€</span><span style="color:#ffffff; font-size:26px; font-weight:300; font-style:italic;"> pour le matériel<br/>';
if( $installation != "") echo '<span style="color:#032B55; font-size:26px; font-weight:bold; font-style:italic;">'.htmlentities($installation).'€</span> <span style="color:#ffffff; font-size:26px; font-weight:300; font-style:italic;"> pour l&acute;installation</span>';
echo '</div>
</a>
</div>';
}else{
/*echo "pas de subvention";*/
}
}
?>

                        <?php if(FALSE && $nboffre>0) { /* desactivé le 24/08/2015 */ ?>
<div class="banniere-promo-offre banniere-promo-offre-top-sat"><img src="/sites/default/files/offre/bandeau.jpg" /></div>
                        <?php } ?>
<?php
//on trie le tableau par prix
/*function comparer_qla($a, $b) {
 return strcmp($a->montant_ttc, $b->montant_ttc);
}*/
    // on trie par prix
usort($offres_dispos->SOffre, 'comparerprix');
$offres_dispos->SOffre = array_reverse($offres_dispos->SOffre);
$nboffre=0;
$afficheroffre="";
foreach ($offres_dispos->SOffre as $key => $offre) {
         if($offre->categorie == "SATELLITE" && !$offre->satellite_local) {
$nboffre++;
                                        $afficheroffre="satellite";

                                        $quota_go="";
                                       $quota_illi="&nbsp;<br>";
                                       $donnee=explode(";",$offre->quota); //chaines séparées par un point virgule
 	$quota_go = $donnee[0];
if($donnee[1])$quota_illi = $donnee[1];

 /* Offre obsoletes*/ {?>
<div class="bloc-offre">
<div class="nom-de-l-offre"><span class="wrapper">
<?php
$offres_split = preg_split("/[\s]+/", $offre->nom_externe);

foreach ($offres_split as $i => $word) {
echo '<span class="word-'.$i.'">'.$word.'</span> ';
}
//echo explode($offre->nom_externe.' id:'.$offre->id_offre;
?></span></div>
<div class="details-offre">

   <strong>Abonnement Internet : </strong><span class="nbGo"><?php echo $quota_go; ?></span> <span class="nbGoBis">Go</span> de volume d’échange</br>
<?php
if (in_array($offre->id_offre, array(853, 854))) {
echo '+ Téléphonie illimitée vers les fixes <br/>
+ Les mobiles en France et 50 destinations<br/>';
} elseif ($offre->id_offre == 852) {
echo '+ Téléphonie illimitée vers les fixes en France et 50 destinations <br/>';
}
if ($offre->id_offre != 851) {
echo '+ Télévision HD (option) <br/>';
}
?>
+ Matériel de connexion inclus

                                <div class="offre-right">


<div class="price"><span class="prix-offre"><?php
$price = $type == 1 ? $offre->montant_ttc : $offre->montant_ht;
$price = money_format('%.2n', $price);

list($price_1, $price_2) = preg_split("/[\s,]+/", $price);

echo '<span class="a-partir-de">à partir de</span><span class="price-1">'.$price_1.'</span><span class="euro">&euro;</span><span class="price-2">'.$price_2.'</span><span class="mensuel-offre">/mois</span>';
/*if( $type == 1){ echo money_format('%.2n', $offre->montant_ttc); }else{ echo money_format('%.2n', $offre->montant_ht); } ?></span><span class="mensuel-offre">/mois</span>
*/
?>
</div>
<div class="right-right">
 <form action="./mes-options" method="post">
<input name="lat" type="hidden" value="<?php echo $_POST['latitude']; ?>" /> <input name="lng" type="hidden" value="<?php echo $_POST['longitude']; ?>" /> <input name="code_insee" type="hidden" value="<?php echo $_POST['commune']; ?>" /> <input name="id_offre" type="hidden" value="<?php echo $offre->id_offre; ?>" /> <input name="type" type="hidden" value="<?php echo $_POST['type']; ?>" /><button aria-disabled="false" class="bt-je-m-abonne" id="submitOffre" name="submitOffre" role="button" style="margin: auto;" type="submit"><span class="ui-button-text">Je m'abonne</span></button></form>
          <div class="en-savoir-plus-offre">


                                         <?php
                                         $params = array();
                                         $params['nom'] = $offre->nom_externe;
                                         $params['cout'] =  $type == 1 ?  money_format('%.2n', $offre->montant_ttc)."" : money_format('%.2n', $offre->montant_ht)." ht";
                                         $params['quota'] = $offre->quota;
                                         $params['description'] = nl2br($offre->description_web_complementaire);
                                         $query = drupal_http_build_query($params);

                        $popup_info = '<div style="display: none;"><div id="offre-more-info-'.$offre->id_offre.'" class="offre-more-info">';
                        $popup_info .=  "<h1>Détails de l'offre</h1>";
                        $popup_info .= "<strong>Nom : </strong>".$params['nom'].'<br/>';
						$popup_info .= "<strong>Tarif : </strong>".$params['cout'].'<br/>';


                        if ($params['quota'] != "")
                          $popup_info .= "<strong>Volume d'échange en Go : </strong>".$params['quota'].'<br/>';
                        $popup_info .= "<strong>Détails : </strong>".$params['description'];
                        $popup_info .= '</div></div>';

echo $popup_info;
                                         ?>
<a class="fancybox fancybox.inline" href="#offre-more-info-<?php echo $offre->id_offre; ?>">En savoir +</a></div>
 </div>
</div>
</div>
</div>
<?php } ?><?php } ?><?php } ?><?php // s'il y a des offre pour le sattelite alors on affiche les offres promo
    if( $nboffre > 0) {
    echo '

	<script>
		jQuery("body").addClass("avecoffressattelite");
	</script>

	<div style="width:100%; clear:both;"></div>
            <!--<div class="banniere-promo-offre"><img src="./sites/default/files/bandeau-offre-promo-2015.png" /></div>

            <div class="banniere-promo-offre"><img src="./sites/default/files/offre/promo2.gif" width="637" height="auto"></div>
            <div class="banniere-promo-offre"><img src="./sites/default/files/promo4.gif"width="637" height="auto"></div>
-->';

    }else{
           // message pad d'offres
           echo "<a href='https://www.alsatis.com/offres-satellite-de-notre-partenaire.php' id='btnoffresnordnet'>Découvrez les offres Internet par Satellite<br/>de notre partenaire<br/><img src='https://www.alsatis.com/sites/all/themes/alsatis_theme/images/logo-nordnet-bouton.png'/></a>
		   <!--<div class='pas-d-offre'>Pour plus d'informations contactez notre service client au : 09 70 247 247 (appel non surtaxé)</div>-->";
    }
?>
<div style="width:100%; clear:both;">
&nbsp;</div>
</div>
</div>
<?php } /* if (!SATELLITE_OFF) {*/ ?>

<div class="contenu" id="contenu_2">
<?php


    // trie par débit
usort($offres_dispos->SOffre, 'comparerdebitnew');
$nboffre=0;





/**
 * Offre isère 4M bientôt 20M
 */

if( substr($departement, 0, 2 ) == "38") {
  $show_isere_20m = FALSE;
  foreach ($offres_dispos->SOffre as $key => $offre) {
    if($offre->categorie == "RADIO" && !$offre->satellite_local){
      if ($offre->debit_down == 4000) {
        $show_isere_20m = TRUE;
      }
    }
  }
  if ($show_isere_20m) {
    echo '<div style="margin-bottom: 30px; text-align: center;" class="encart-offre-isere"><img src="./sites/default/files/offre/encart-isere-2.png"></div>';
  }
}



foreach ($offres_dispos->SOffre as $key => $offre) {?><?php if($offre->categorie == "RADIO" && !$offre->satellite_local){
  $nboffre++;
                  $afficheroffre="radio";
  if ($offre->debit_down < 1000){
  $debitDow = $offre->debit_down." Kb";
  }else{
  $debitDow = $offre->debit_down/1000;
  $debitDow = round($debitDow, 0);
  $debitDow = $debitDow." Méga";
  }

  if ($offre->debit_up < 1000){
  $debitUp = $offre->debit_up." Kb";
  }else{
  $debitUp = $offre->debit_up/1000;
  $debitUp = round($debitUp, 0);
  $debitUp = $debitUp." Méga";
  }

  ?>
<div class="bloc-offre">
<div class="nom-de-l-offre">
<span class="wrapper">
<?php
$offres_split = preg_split("/[\s]+/", $offre->nom_externe);
                                        $add = sizeof($offres_split) == 3 ? $add = -1 : $add = 0;
foreach ($offres_split as $i => $word) {
 echo '<span class="word-'.($i + $add).'">'.$word.'</span> ';
}
?></span>
</div>
       <div class="details-offre">

                                     <strong>Abonnement Internet : </strong></br>

                                     <?php
                                     echo '+ Débit download  '. $debitDow.'<br/>';
                                     echo '+ Débit  upload '.$debitUp.'<br/>';
                                     if ($offre->debit_down > 20000) {
                                       echo '+ Routeur wifi inclus<br/>';
                                     }
                                     ?>



                                     <div class="offre-right">


                                         <div class="price"><span class="prix-offre"><?php
                                           $price = $type == 1 ? $offre->montant_ttc : $offre->montant_ht;
                                           $price = money_format('%.2n', $price);
                                           list($price_1, $price_2) = preg_split("/[\s,]+/", $price);
                                           echo '<span class="a-partir-de">à partir de</span><span class="price-1">'.$price_1.'</span><span class="euro">&euro;</span><span class="price-2">'.$price_2.'</span><span class="mensuel-offre">/mois</span>';
                                           ?>
                                         </div>

                                        <div class="right-right">
         <form action="./mes-options" method="post">
<input name="lat" type="hidden" value="<?php echo $_POST['latitude']; ?>" /> <input name="lng" type="hidden" value="<?php echo $_POST['longitude']; ?>" /> <input name="code_insee" type="hidden" value="<?php echo $_POST['commune']; ?>" /> <input name="id_offre" type="hidden" value="<?php echo $offre->id_offre; ?>" /> <input name="type" type="hidden" value="<?php echo $_POST['type']; ?>" /><button aria-disabled="false" class="bt-je-m-abonne" id="submitOffre" name="submitOffre" role="button" style="margin: auto;" type="submit"><span class="ui-button-text">Je m'abonne</span></button></form>
                  <div class="en-savoir-plus-offre">
                                             <?php
                                             $params = array();
                                             $params['nom'] = $offre->nom_externe;
                                             $params['cout'] =  $type == 1 ?  money_format('%.2n', $offre->montant_ttc)."" : money_format('%.2n', $offre->montant_ht)." ht";
                                             $params['quota'] = $offre->quota;
                                             $params['description'] = nl2br($offre->description_web_complementaire);

                                             $popup_info = '<div style="display: none;"><div id="offre-more-info-'.$offre->id_offre.'" class="offre-more-info">';
                                             $popup_info .=  "<h1>Détails de l'offre</h1>";
                                             $popup_info .= "<strong>Nom : </strong>".$params['nom'].'<br/>';


													$popup_info .= "<strong>Tarif : </strong>".$params['cout'].'<br/>';

                                             if ($params['quota'] != "")
                                               $popup_info .= "<strong>Volume d'échange en Go : </strong>".$params['quota'].'<br/>';
                                             $popup_info .= "<strong>Détails : </strong>".$params['description'];
                                             $popup_info .= '</div></div>';

echo $popup_info;
                                         ?>
<a class="fancybox fancybox.inline" href="#offre-more-info-<?php echo $offre->id_offre; ?>">En savoir +</a></div>





        </div>

                                      </div>
                                   </div>
                                      </div>


<?php } ?><?php } ?>
<?php // s'il y a des offre pour le radio alosr on affiche les offres promo
    if( $nboffre > 0) {

    echo '

	<script>
		jQuery("body").addClass("avecoffresradio");
	</script>

	<div style="width:100%; clear:both;"></div>

<div class="banniere-promo-offre"><img src="./sites/default/files/offre/telephonie.jpg" ></div>';
/*
            <div class="banniere-promo-offre"><img src="./sites/default/files/offre/bandeau6.gif" width="637" height="auto"></div>
            <div class="banniere-promo-offre"><img src="./sites/default/files/offre/bandeau_tv_2.png" width="637" height="auto"></div>';
  */

    }else{
           // message pad d'offres
           echo "<div class='pas-d-offre'>Pour plus d'informations contactez notre service client au : 09 70 247 247 (appel non surtaxé)</div>";

    }
    ?>
<div style="width:100%; clear:both;">
&nbsp;</div>
</div>
<div class="contenu" id="contenu_3">
<?php

    // on trie par prix
usort($offres_dispos->SOffre, 'comparer');
$nboffre=0;
foreach ($offres_dispos->SOffre as $key => $offre) {?><?php if($offre->categorie == "FIBRE_OPTIQUE" && !$offre->satellite_local){
  $nboffre++;
                                        $afficheroffre="fibre";
  if ($offre->debit_down < 1000){
  $debitDow = $offre->debit_down." Kb";
  }else{
  $debitDow = $offre->debit_down/1000;
  $debitDow = round($debitDow, 0);
  $debitDow = $debitDow." Méga";
  }

  if ($offre->debit_up < 1000){
  $debitUp = $offre->debit_up." Kb";
  }else{
  $debitUp = $offre->debit_up/1000;
  $debitUp = round($debitUp, 0);
  $debitUp = $debitUp." Méga";
  }

  ?>
<div class="bloc-offre">
<div class="nom-de-l-offre">
<span class="wrapper">
<?php
                                        $offres_split = preg_split("/[\s]+/", $offre->nom_externe);
                                        $add = sizeof($offres_split) == 3 ? $add = -1 : $add = 0;
foreach ($offres_split as $i => $word) {
 echo '<span class="word-'.($i + $add).'">'.$word.'</span> ';
}

                                 ?></span>
                        </div>
<div class="details-offre">
                         <strong>Abonnement Internet : </strong></br>

<?php
echo '+ Débit download  '. $debitDow.'<br/>';
echo '+ Débit  upload '.$debitUp.'<br/>';
if ($offre->debit_down > 20000) {
  echo '+ Routeur wifi inclus<br/>';
}
?>
                                <div class="offre-right">


<div class="price"><span class="prix-offre"><?php
$price = $type == 1 ? $offre->montant_ttc : $offre->montant_ht;
$price = money_format('%.2n', $price);

list($price_1, $price_2) = preg_split("/[\s,]+/", $price);

echo '<span class="a-partir-de">à partir de</span><span class="price-1">'.$price_1.'</span><span class="euro">&euro;</span><span class="price-2">'.$price_2.'</span><span class="mensuel-offre">/mois</span>';
?>
</div>

<div class="right-right">
 <form action="./mes-options" method="post">
<input name="lat" type="hidden" value="<?php echo $_POST['latitude']; ?>" /> <input name="lng" type="hidden" value="<?php echo $_POST['longitude']; ?>" /> <input name="code_insee" type="hidden" value="<?php echo $_POST['commune']; ?>" /> <input name="id_offre" type="hidden" value="<?php echo $offre->id_offre; ?>" /> <input name="type" type="hidden" value="<?php echo $_POST['type']; ?>" /><button aria-disabled="false" class="bt-je-m-abonne" id="submitOffre" name="submitOffre" role="button" style="margin: auto;" type="submit"><span class="ui-button-text">Je m'abonne</span></button></form>
          <div class="en-savoir-plus-offre">


                                         <?php
                                         $params = array();
                                         $params['nom'] = $offre->nom_externe;
                                         $params['cout'] =  $type == 1 ?  money_format('%.2n', $offre->montant_ttc)."" : money_format('%.2n', $offre->montant_ht)." ht";
                                         $params['quota'] = $offre->quota;
                                         $params['description'] = nl2br($offre->description_web_complementaire);

                                             $popup_info = '<div style="display: none;"><div id="offre-more-info-'.$offre->id_offre.'" class="offre-more-info">';
                                             $popup_info .=  "<h1>Détails de l'offre</h1>";
                                             $popup_info .= "<strong>Nom : </strong>".$params['nom'].'<br/>';
												$popup_info .= "<strong>Tarif : </strong>".$params['cout'].'<br/>';
                                             if ($params['quota'] != "")
                                               $popup_info .= "<strong>Volume d'échange en Go : </strong>".$params['quota'].'<br/>';
                                             $popup_info .= "<strong>Détails : </strong>".$params['description'];
                                             $popup_info .= '</div></div>';

echo $popup_info;
                                         ?>
<a class="fancybox fancybox.inline" href="#offre-more-info-<?php echo $offre->id_offre; ?>">En savoir +</a></div>
 </div>
</div>
</div>



<?php } ?><?php } ?><?php // s'il y a des offre pour la fibre alosr on affiche les offres promo
    if( $nboffre > 0) {
    echo '

	<script>
		jQuery("body").addClass("avecoffresfibre");
	</script>


	<div style="width:100%; clear:both;"></div>

<div class="banniere-promo-offre"><img src="./sites/default/files/offre/telephonie.jpg" ></div>';
/*
            <div class="banniere-promo-offre"><img src="./sites/default/files/offre/promo-fibre1.gif" width="637" height="auto"></div>
            <div class="banniere-promo-offre"><img src="./sites/default/files/offre/promo-fibre2.gif" width="637" height="auto"></div>';
*/
    }else{
           // message pad d'offres
           echo "<div class='pas-d-offre'>Pour plus d'informations contactez notre service client au : 09 70 247 247 (appel non surtaxé)</div>";
    }
    ?>
<div style="width:100%; clear:both;">
&nbsp;</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
//<!--
Affiche(Onglet_afficher);
<?php
if ($afficheroffre=="fibre") {
 echo "Affiche('3')";
} elseif ($afficheroffre=="radio") {
 echo "Affiche('2')";
} elseif ($afficheroffre=="satellite") {
 echo "Affiche('1')";
} else {
  if (SATELLITE_OFF) {
    echo "Affiche('2')";
  } else {
    echo "Affiche('1')";
  }
}
?>
//-->
</script><br />
<br />
<?php
echo "<!-- commune : ".$_POST['commune']." latitude : ".$_POST['latitude']." longitude : ".$_POST['longitude']. " code postal : ".$codepostal."-->";
?>
