<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Copyright (C) 2006  Sylvain FATOME

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; 
either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program
(in the directory docs); 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA  02110-1301, USA.

Contact : galawa@free.fr
****************************************************************************************/

include ("../inc/config.inc.php");
?>
<?php include ("../inc/fic_fpdf.inc.php") ?>
<?php include ("../inc/func_time.inc.php") ?>
<?php include ("../inc/func_date.inc.php") ?>
<?php include ("../inc/func_log.inc.php") ?>
<?php include ("../inc/txt.inc.php") ?>
<?PHP
	Archivelog(@$HTTP_SESSION_VARS["code_log"]);

	function Retourneville($insee)
	{
		$ville_nom = "";
		
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rnom = "SELECT villes_insee,villes_nom FROM " .TBVILLES. " WHERE villes_insee='".$insee."'";;
		$rsnom = mysql_query($rnom) or die(mysql_error());
		if (mysql_num_rows($rsnom) == 1) // Uniquement UN SEUL enregistrement !
		{
			if ($rowville = mysql_fetch_array($rsnom)) 
			{
				extract ($rowville);
			}
		}
	
		mysql_free_result($rsnom);
		mysql_close($conn2);  
	
		$ville = str_majuscules($villes_nom);
		
		// Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
		
		if (stristr($villes_nom,"LYON"))
			$ville="LYON";
		if (stristr($villes_nom,"MARSEILLE"))
			$ville="MARSEILLE";
		if (stristr($villes_nom,"PARIS"))
			$ville="PARIS";
			
		return $ville;
	}

	function Retournecp($insee)
	{
		$villes_cp = "";
		
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rcp = "SELECT villes_insee,villes_cp FROM " .TBVILLES. " WHERE villes_insee='".$insee."'";;
		$rscp = mysql_query($rcp) or die(mysql_error());
		if (mysql_num_rows($rscp) == 1) // Uniquement UN SEUL enregistrement !
		{
			if ($rowcp = mysql_fetch_array($rscp)) 
			{
				extract ($rowcp);
			}
		}
	
		mysql_free_result($rscp);
		mysql_close($conn2);  
	
		return $villes_cp;
	}

	function formatetxt($texte)
	{
		$saut = chr(13).chr(10);
		$txt = ereg_replace($saut,"\n",$texte);
		
		return $txt;
	}

	function str_majuscules($texte)
	{
		$accentue="ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËéèêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ";
		$nonaccentue="AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn";

		$txt = strtr($texte,$accentue,$nonaccentue); 
		$txt = strtoupper($txt);
	
		return $txt;
	}

	$coderech = strip_tags(trim($_GET['code']));	// Récupération code Contrat
		
	$pbm_cocl = FALSE;

	if ($coderech == "")
		$pbm_cocl = TRUE;

	// Niveau <=2 uniquement
	if (@$HTTP_SESSION_VARS["niv_log"] <= 2)
		$pbm_cocl = TRUE;


	if ($pbm_cocl == FALSE)
	{
		// Récupération des informations contrat client
		$row = lit_enr_cocl($coderech);
		if (is_array($row))
		{
			extract ($row);
			// Contrôle qu'une agence n'essaye pas de "hacker" un contrat
			if (((@$HTTP_SESSION_VARS["niv_log"] == 4) OR (@$HTTP_SESSION_VARS["niv_log"] == 3)) AND ($cocl_codeage <> @$HTTP_SESSION_VARS["code_age"]))
				$pbm_cocl = TRUE;

			if ($pbm_cocl == FALSE)
			{
				$cocl_datecreaf = formatedate($cocl_datecrea);
				$cocl_datesigf = formatedate($cocl_datesig);
				$cocl_datefinf = formatedate($cocl_datefin);
			
				$row = lit_enr_co($cocl_codeco);
				extract($row);
			
				require('../classes/fpdf/fpdf.php');
				
				function hex2dec($color = "#000000"){
					$tbl_color = array();
					$tbl_color['R']=hexdec(substr($color, 1, 2));
					$tbl_color['G']=hexdec(substr($color, 3, 2));
					$tbl_color['B']=hexdec(substr($color, 5, 2));
					return $tbl_color;
				}
				
				function px2mm($px){
					return $px*25.4/72;
				}
				
				function txtentities($html)
				{
					$trans = get_html_translation_table(HTML_ENTITIES);
					$trans = array_flip($trans);
					return strtr($html, $trans);
				}
			
				function ConvertSize($size=5,$maxsize=0)
				{
				// Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
				  //Identify size (remember: we are using 'mm' units here)
				  if ( stristr($size,'px') ) $size *= 0.2645; //pixels
				  elseif ( stristr($size,'cm') ) $size *= 10; //centimeters
				  elseif ( stristr($size,'mm') ) $size += 0; //millimeters
				  elseif ( stristr($size,'in') ) $size *= 25.4; //inches 
				  elseif ( stristr($size,'pc') ) $size *= 38.1/9; //PostScript picas 
				  elseif ( stristr($size,'pt') ) $size *= 25.4/72; //72dpi
				  elseif ( stristr($size,'%') )
				  {
					$size += 0; //make "90%" become simply "90" 
					$size *= $maxsize/100;
				  }
				  else $size *= 0.2645; //nothing == px
				  
				  return $size;
				}
		
				class PDF extends FPDF
				{
					var $angle=0;
					
					//En-tête
					function Header()
					{
						global
							$cocl_code,
							$cocl_codeage,
							$cocl_datecreaf,
							$cocl_datesigf,
							$cocl_datefinf,
							$chemin_logos,
							$pv_ent_age,
							$pv_ent_cocl;
				
			
						$pv_ent_age = 30; // Valeur de départ (mm) du pointeur pour l'affichage agence par rapport au bord haut de la feuille
										  // Cette valeur comprend la hauteur du logo (20 mm)
				
						$pv_ent_cocl = 20; // Valeur de départ (mm) du pointeur pour l'affichage de l'entête intervention par rapport au bord haut de la feuille
			
			
						// On sauve la couleur en cours et on se met en noir
						$ex_couleurtxt = $this->$TextColor;
						$this->SetTextColor(0);
			
						// Logo Agence
						if (agence <> "")
						{
							$urlimg = $chemin_logos."/log".strtolower($cocl_codeage).".png"; 
						}
						$this->Image($urlimg,10,10,55,15); // Image à 10mm du bord gauche, 10mm du bord haut, largeur maximum 55mm
			
						$row = lit_enr_age($cocl_codeage);
						extract ($row);
									
						// Entête Agence
						
							// N° de Siret
							$this->SetY($pv_ent_age);
							$this->SetFont('Arial','I',6);
							$this->Cell(95,3,$age_rcs,0,0,'L');
			
							// Coordonnées
							$ent_age = $age_adr1."\n";
							if ($age_adr2 <> "")
								$ent_age.= $age_adr2."\n";
							$ent_age.= Retournecp($age_ville)." ".Retourneville($age_ville)."\n";
							$ent_age.= "Tel : ";
							$ent_age.=$age_telf;
							if ($age_txttelf <> "")
								$ent_age.= " (".$age_txttelf.")\n";
							else
								$ent_age.= "\n";
							$ent_age.="Fax : ";
							$ent_age.= $age_fax;
							if ($age_txtfax <> "")
								$ent_age.= " (".$age_txtfax.")\n";
							else
								$ent_age.= "\n";
			
							$pv_ent_age = $pv_ent_age + 3;
							$this->SetY($pv_ent_age);
							$this->SetFont('Arial','',8);
							$this->MultiCell(95,3,$ent_age,0,'L');
			
							// Textes complémentaires
							$ent_age = "";
							if ($age_web <> "")
							{
								$ent_age = "Site Web : ";
								$ent_age.= $age_web." / ";
							}
							$ent_age.= "Email : ";
							$ent_age.= $age_email."\n";
							$ent_age.= "Horaires : ";
							$ent_age.= $age_heures."\n";
							$ent_age.= formatetxt($age_txtent);
							$this->SetFont('Arial','I',6); // Pas besoin de SetY car le MultiCell renvoie juste en dessous
							$this->MultiCell(95,3,$ent_age,0,'L');
							$pv_ent_age = $this->GetY(); // Récupération de la position du pointeur
			
						// Entête Contrat
						
						$this->SetY($pv_ent_cocl);
						$this->Cell(95);
						$this->SetFont('Arial','BU',14);
						$this->Cell(95,10,'CONTRAT',0,1,'C');
						$ent_cocl = "N°".$cocl_code."\n";
						$ent_cocl.= 	"Date création : ";
						$ent_cocl.= $cocl_datecreaf;
						$pv_ent_cocl = $pv_ent_cocl + 10;
						$this->SetY($pv_ent_cocl);
						$this->Cell(95);
						$this->SetFont('Arial','',10);
						$this->MultiCell(95,5,$ent_cocl,0,'C');
						
						if ($cocl_datefinf <> "")  // Affichage si contrat clot
						{
							$this->SetY($pv_ent_age - 20);
							$this->Cell(95);
							$this->SetTextColor(200);
							$this->SetFont('Arial','B',14);
							$this->Cell(95,20,'TERMINE',0,1,'C');
							$this->SetTextColor(0);
						}
			
			
						$this->SetDrawColor(125);
						$this->SetLineWidth(0.4);
						$this->Line(10,$pv_ent_age,200,$pv_ent_age);
						$this->SetDrawColor(0);
						$this->SetLineWidth(0.2);
			
						// "Duplicata" si signé (pas de réimpression d'original pour le client !)
						if ($cocl_datesigf <> "") 
							$this->fond( "DUPLICATA");
				
						$pv_ent_age++;
						$this->SetY($pv_ent_age); // Renvoie le pointeur SOUS l'entête agence
			
						// On restaure la couleur
						$this->SetTextColor($ex_couleurtxt);
					}
					
					//Pied de page
					function Footer()
					{
						// On sauve la couleur en cours en on se met en noir
						$ex_couleurtxt = $this->$TextColor;
						$this->SetTextColor(0);
			
						//Positionnement à 1,5 cm du bas
						$this->SetY(-15);
						//Police Arial italique 8
						$this->SetFont('Arial','I',8);
						//Numéro de page
						$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'R');
			
						// On restaure la couleur
						$this->SetTextColor($ex_couleurtxt);
					}
			
					var $B;
					var $I;
					var $U;
					var $HREF;
					var $fontList;
					var $issetfont;
					var $issetcolor;
					var $issetbgcolor;
					var $ex_fontsize;
					var $ex_lineheight;
					var $ex_x;
					var $ex_y;
					var $nb_ol;
					var $index_li;
					var $nb_ul;
					var $nb_li;
					var $nb_blockquote;
					var $tag_precedent;
					var $tab_rgb;
					var $pas_afficher;
					
					function PDF($orientation='P',$unit='mm',$format='A4')
					{
						//Appel au constructeur parent
						$this->FPDF($orientation,$unit,$format);
						//Initialisation
						$this->SetMargins(10,10,10,10);
						$this->SetFont('Arial','',10);
						$this->lineheight = 5;
						$this->SetFillColor(255,255,255);
						$this->B=0;
						$this->I=0;
						$this->U=0;
						$this->HREF='';
						$this->fontlist=array("arial","times","courier","helvetica","symbol");
						$this->issetfont=false;
						$this->issetcolor=false;
						$this->issetbgcolor=false;
						$this->ex_fontsize=10;
						$this->ex_lineheight=5;
						$this->ex_x=0;
						$this->ex_y=0;
						$this->nb_ol=0;
						$this->index_li=array();
						$this->nb_ul=0;
						$this->nb_li=0;
						$this->nb_bq=0;
						$this->tag_precedent='';
						$this->tab_rgb=array();
						$this->pas_afficher=FALSE;
						$this->largeur_page= $this->fw - $this->lMargin - $this->rMargin ;
					}
					
					function WriteHTML($html)
					{
						// Nettoyage des tags
						$str=array(
						'<br />' => '<br>',
						'<hr />' => '<hr>',
						'[r]' => '<red>',
						'[/r]' => '</red>',
						'[l]' => '<blue>',
						'[/l]' => '</blue>',
						'&#8220;' => '"',
						'&#8221;' => '"',
						'&#8222;' => '"',
						'&#8230;' => '...',
						'&#8217;' => '\''
						);
						foreach ($str as $_from => $_to) 
							$html = str_replace($_from,$_to,$html);
			
						//Suppression de tous les tags non supportés
						$html=strip_tags($html,"<a><br><font><blockquote><h1><h2><h3><h4><h5><h6><ul><li><b><i><u><strong><em><sub><sup><ol><div><span>");
						//Remplace les retours chariots, nouvelles lignes, et tabulations par des espaces
						$html=str_replace("\n",' ',$html);
						// $repTable = array("\t" => " ", "\n" => " ", "\r" => " ", "\0" => " ", "\x0B" => " "); 
						// $html = strtr($html, $repTable);
			
						//Remplace quelques caractères spéciaux
						$html = str_replace('&trade;','™',$html);
						$html = str_replace('&copy;','©',$html);
						$html = str_replace('&euro;','€',$html);
						
						$pattern = '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/';
			
						$a=preg_split($pattern,trim($html),-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
						
						foreach($a as $key=>$element) 
						{
							if (!preg_match($pattern, $element)) 
							{
								//Texte
								if ($this->pas_afficher == FALSE)
								{
									if ($this->issetbgcolor == true)
									{
										$this->SetFillColor($this->tab_rgb[0],$this->tab_rgb[1],$this->tab_rgb[2]);
									}
									if($this->HREF) // Est-ce un lien ?
									{
										$this->PutLink($this->HREF,$element);
									}
									else 
									{
										// Vérification si blockquote, OL, ou UL
										if (($this->nb_bq > 0) OR ($this->nb_ol > 0) OR ($this->nb_ul > 0))
										{
											if (trim($element) <> "")
											{
												// On sauve la marge de gauche
												$lMargin = $this->lMargin;
												// Blockquote : blancs simples
												if ($this->nb_bq > 0)
												{
													$this->SetLeftMargin($this->nb_bq*10+$lMargin);
													$this->Write($this->lineheight,trim(stripslashes(txtentities($element))));
												}
												// OL ou UL : blancs + numérotation (ou pas)
												if ($this->nb_ol > 0)
												{
													$this->SetLeftMargin($this->nb_ol*10+$lMargin);
													$this->Write($this->lineheight,($this->index_li[$this->nb_ol]).'. '.trim(stripslashes(txtentities($element))));
												}
												if ($this->nb_ul > 0)
												{
													$this->SetLeftMargin($this->nb_ul*10+$lMargin);
													// $this->Write($this->lineheight,'» '.trim(stripslashes(txtentities($element))));
													$this->Write($this->lineheight,chr(149).' '.trim(stripslashes(txtentities($element))));
												}
												// On restaure la marge de gauche
												$this->SetLeftMargin($lMargin);
												$this->tag_precedent='';
											}
										}
										else
										{
											if (trim($element) <> "")
											{
												if ($this->issetbgcolor == true)
												{
													$this->Cell($this->GetStringWidth($element)+2,$this->lineheight,stripslashes(txtentities($element)),0,0,'L',1);
												}
												else
												{
													$this->Write($this->lineheight,stripslashes(txtentities($element)));
													
													// Si il y a un blanc à la fin de la chaîne, Write ne l'imprime pas
													// (ce qui "colle" le texte au suivant) : il faut donc le gérer.
													if ($element{(strlen($element)-1)} == " ")
													{
														$this->Cell($this->GetStringWidth(' '));
													}
													
												}
												$this->tag_precedent='';
											}
										}
									}
									$this->SetFillColor(255);
								}
							}
							else 
							{
								//Tag
								$element = substr($element, 1, -1);
								if($element{0}=='/') 
								{
									$this->CloseTag(strtoupper(substr($element,1)));
								}
								else 
								{
									// Attributs exacts
									// Récupération des tags
									preg_match('/([a-zA-Z0-9]*)/', $element, $tag);
									$tag = strtoupper($tag[0]);
									// Récupération des atributs
									preg_match_all('/([^=\s]*)=["\']?([^"\']*)["\']?/', $element, $attr_array, PREG_PATTERN_ORDER);
									$attr = array(); // reset attribute array
									while(list($id,$name)=each($attr_array[1])) {
										$attr[strtoupper($name)] = $attr_array[2][$id];
									}
									$this->OpenTag($tag,$attr);
								}
							}
						}
						// echo "<br>";
					}
					
					function OpenTag($tag,$attr)
					{
						//Balise ouvrante
						switch($tag)
						{
							case 'BR':
								$this->Ln($this->lineheight);
								break;
							case 'STRONG':
							case 'B':
								$this->SetStyle('B',true);
								break;
							case 'I':
							case 'EM':
								$this->SetStyle('I',true);
								break;
							case 'U':
								$this->SetStyle('U',true);
								break;
							case 'SUB': 
								$this->ex_fontsize = $this->FontSizePt;
								$this->ex_lineheight = $this->lineheight;
								$this->ex_x = $this->GetX();
								$this->ex_y = $this->GetY();
								$this->SetFontSize($this->ex_fontsize-3);
								$this->SetXY($this->ex_x, (($this->ex_y+($this->ex_fontsize*0.3))-2));					  
								break;
							case 'SUP': 
								$this->ex_fontsize = $this->FontSizePt;
								$this->ex_lineheight = $this->lineheight;
								$this->ex_x = $this->GetX();
								$this->ex_y = $this->GetY();
								$this->SetFontSize($this->ex_fontsize-3);
								$this->SetXY($this->ex_x, (($this->ex_y-($this->ex_fontsize*0.3))+2));
								break;
							case 'OL': 
								$this->nb_ol++;
								break;
							case 'UL': 
								$this->nb_ul++;
								break;
							case 'LI':
								$this->ln($this->lineheight);
								$this->nb_li++;
								if ($this->nb_ol > 0)
									$this->index_li[$this->nb_ol] = ($this->index_li[$this->nb_ol])+1;
								break;
							case 'BLOCKQUOTE':
								if($this->tag_precedent <> 'BLOCKQUOTE')
									$this->ln($this->lineheight);
								$this->nb_bq++;
								break;
							case 'H1': 
							  $this->SetFontSize(2*$this->FontSizePt); 
							  $this->lineheight *= 2;
							  break;
							case 'H2': 
							  $this->SetFontSize(1.5*$this->FontSizePt); 
							  $this->lineheight *= 1.5;
							  break;
							case 'H3':
							  $this->SetFontSize(1.17*$this->FontSizePt);
							  $this->lineheight *= 1.17;
							  break;
							case 'H4':
							  $this->SetFontSize($this->FontSizePt); 
							  break;
							case 'H5': 
							  $this->SetFontSize(0.83*$this->FontSizePt); 
							  $this->lineheight *= 0.83;
							  break;
							case 'H6': 
							  $this->SetFontSize(0.67*$this->FontSizePt); 
							  $this->lineheight *= 0.67;
							  break;
							case 'FONT':
							case 'SPAN':
							case 'DIV' :
								if ($tag == 'DIV')
								{
									$this->Ln($this->lineheight); // Toujours un saut de ligne sur balise DIV
								}
								if (isset($attr['COLOR']) and $attr['COLOR']!='') 
								{
									$coul=hex2dec($attr['COLOR']);
									$this->mySetTextColor($coul['R'],$coul['G'],$coul['B']);
									$this->issetcolor=true;
								}
								if (isset($attr['FACE']) and in_array(strtolower($attr['FACE']), $this->fontlist))
								{
									$this->SetFont(strtolower($attr['FACE']));
									$this->issetfont=true;
								}
								if (isset($attr['SIZE']) and $attr['SIZE']!='')
								{
									switch ($attr['SIZE'])
									{
										// Rapport de 1.2 entre chiffre, partant du principe que '4' = taille courante
										// Cf http://www.w3.org/TR/REC-CSS2/fonts.html#value-def-absolute-size
										case 1:
											$this->SetFontSize(0.58*$this->FontSizePt); 
											// $this->lineheight *= 0.58;
											break;
										case 2:
											$this->SetFontSize(0.69*$this->FontSizePt); 
											// $this->lineheight *= 0.69;
											break;
										case 3:
											$this->SetFontSize(0.83*$this->FontSizePt); 
											// $this->lineheight *= 0.83;
											break;
										case 4:
											// $this->SetFontSize($this->FontSizePt); 
											break;
										case 5:
											$this->SetFontSize(1.2*$this->FontSizePt); 
											// $this->lineheight *= 1.2;
											break;
										case 6:
											$this->SetFontSize(1.44*$this->FontSizePt); 
											// $this->lineheight *= 1.44;
											break;
										case 7:
											$this->SetFontSize(1.73*$this->FontSizePt); 
											// $this->lineheight *= 1.73;
											break;
									}
									$this->issetfont=true;
								}
								if (isset($attr['STYLE']) and $attr['STYLE']!='')
								{
									$attr_style = explode(";",$attr['STYLE']);
									for ($i=0; $i<count($attr_style);$i++)
									{
										if (trim($attr_style[$i]) <> "")
										{
											$detail_attr_style = explode(":",$attr_style[$i]);
											switch (strtolower(trim($detail_attr_style[0])))
											{
												case 'background-color' :
													// Mode Hexadécimal ?
													$pos = strpos(trim($detail_attr_style[1]), "#");
													if ($pos === FALSE)
													{
														// Mode RGB ?
														$pos2 = strpos(trim($detail_attr_style[1]), "(");
														if ($pos2 === FALSE)
														{
															break; // Aucun des 2, on quitte
														}
														else
														{
															$str_couleur = substr(trim($detail_attr_style[1]),$pos2+1);
															$str_couleur = substr($str_couleur,0,strlen($str_couleur)-1);
															$this->tab_rgb = explode(",",$str_couleur);
															for ($k=0; $k<count($this->tab_rgb); $k++)
															{
																settype($this->$tab_rgb[$k],"int");
															}
															$this->issetbgcolor=true;
														}
													}
													else
													{
														$coul=hex2dec(trim($detail_attr_style[1]));
														$this->SetFillColor($coul['R'],$coul['G'],$coul['B']);
														$this->issetbgcolor=true;
													}
													break;
												case 'page-break-after' :
													if (trim(strtolower($detail_attr_style[1])) == "always")
													{
														$this->AddPage();
													}
													break;
												case 'display' :
													if (trim(strtolower($detail_attr_style[1])) == "none")
													{
														$this->pas_afficher = TRUE;
													}
													break;
											}
										}
									}
								}
								break;
							case 'A':
								$this->HREF=$attr['HREF'];
								break;
							case 'SDP': // Pour conserver compatibilité avec anciens formats < SMI 0.9.5
								$this->AddPage();
								break;
							// Tableaux plus tard
							case 'TABLE':
							case 'TR':
							case 'TH':
							case 'TD':
								break;
						}
						$this->tag_precedent=$tag;
					}
					
					function CloseTag($tag)
					{
						//Balise fermante
						switch ($tag)
						{
							case 'STRONG':
							case 'B':
								$this->SetStyle('B',false);
								break;
							case 'I':
							case 'EM':
								$this->SetStyle('I',false);
								break;
							case 'U':
								$this->SetStyle('U',false);
								break;
							case 'SUB' :
							case 'SUP' :
								$this->SetFontSize($this->ex_fontsize);
								$this->SetXY($this->ex_x, $this->ex_y);					  
								$this->lineheight = $this->ex_lineheight;
								break;
							case 'OL': 
								$this->nb_ol = $this->nb_ol-1;
								if ($this->nb_ol == 0)
								{
									$this->SetLeftMargin(10);
									$this->ln($this->lineheight);
									$this->ln($this->lineheight);
								}
								break;
							case 'UL': 
								$this->nb_ul = $this->nb_ul-1;
								if ($this->nb_ul == 0)
								{
									$this->SetLeftMargin(10);
									$this->ln($this->lineheight);
									$this->ln($this->lineheight);
								}
								break;
							case 'LI' :
								$this->nb_li = $this->nb_li-1;
								break;
							case 'BLOCKQUOTE':
								$this->nb_bq = $this->nb_bq-1;
								break;
							case 'H1' :
							case 'H2' :
							case 'H3' :
							case 'H4' :
							case 'H5' :
							case 'H6' :
								$this->Ln($this->lineheight);
								$this->lineheight = 5;
								$this->SetFont('Arial','',10);
								$this->SetFontSize(11);
								$this->SetStyle('U',false);
								$this->SetStyle('B',false);
								break;
							case 'FONT' :
							case 'SPAN' :
							case 'DIV' :
								if ($tag == 'SPAN')
								{
									$this->pas_afficher = FALSE;
								}
								if ($tag == 'DIV')
								{
									$this->Ln($this->lineheight); // Toujours un saut de ligne sur balise DIV
								}
								if ($this->issetcolor==true) 
								{
									$this->SetTextColor(0,0,0);
								}
								if ($this->issetfont) 
								{
									$this->SetFont('Arial','',10);
									$this->issetfont=false;
								}
								if ($this->issetbgcolor == true) 
								{
									$this->SetFillColor(255,255,255);
									$this->issetbgcolor=false;
								}
								break;
							case 'A' :
								$this->HREF='';
								break;
							// Tableaux plus tard
							case 'TABLE':
							case 'TD':
							case 'TH':
								break;
							case 'TR':
								break;
						}
					}
					
					function SetStyle($tag,$enable)
					{
						//Modifie le style et sélectionne la police correspondante
						$this->$tag+=($enable ? 1 : -1);
						$style='';
						foreach(array('B','I','U') as $s)
							if($this->$s>0)
								$style.=$s;
						$this->SetFont('',$style);
					}
					
					function mySetTextColor($r,$g=0,$b=0)
					{
						static $_r=0, $_g=0, $_b=0;
				
						if ($r==-1) 
							$this->SetTextColor($_r,$_g,$_b);
						else 
						{
							$this->SetTextColor($r,$g,$b);
							$_r=$r;
							$_g=$g;
							$_b=$b;
						}
					}
					
					function PutLink($URL,$txt)
					{
						//Place un hyperlien
						$this->SetTextColor(0,0,255);
						$this->SetStyle('U',true);
						$this->Write($this->lineheight,$txt,$URL);
						$this->SetStyle('U',false);
						$this->SetTextColor(0);
					}
			
					function Rotate($angle,$x=-1,$y=-1)
					{
						// Rotation d'un texte
						if($x==-1)
							$x=$this->x;
						if($y==-1)
							$y=$this->y;
						if($this->angle!=0)
							$this->_out('Q');
						$this->angle=$angle;
						if($angle!=0)
						{
							$angle*=M_PI/180;
							$c=cos($angle);
							$s=sin($angle);
							$cx=$x*$this->k;
							$cy=($this->h-$y)*$this->k;
							$this->_out(sprintf('q %.5f %.5f %.5f %.5f %.2f %.2f cm 1 0 0 1 %.2f %.2f cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy));
						}
					}
					
					function fond($texte)
					{
						// Permet de rajouter un fonds de page en sous-impression
						$this->SetFont('Arial','B',60);
						$this->SetTextColor(203,203,203);
						$this->Rotate(45,55,190);
						$this->Text(55,190,$texte);
						$this->Rotate(0);
						$this->SetTextColor(0,0,0);
					}
				}
			
				$pdf=new PDF();
				$pdf->FPDF('P','mm','A4');
				$pdf->SetDisplayMode('real');
				$pdf->AliasNbPages();
				$pdf->SetTitle('Contrat N°'.$cocl_code);
				$pdf->SetSubject('Contrat N°'.$cocl_code);
				$pdf->AddPage();
			
				$pdf->SetLineWidth(0.4);
			
				$txt_texte = formatetxt($co_cgv);
				$txt_cocl = parse_txt("age",$cocl_codeage,$txt_texte);
				$txt_cocl = parse_txt("cdg","",$txt_cocl);
				$txt_cocl = parse_txt("cli",$cocl_codecli,$txt_cocl);
				$txt_cocl = parse_txt("con",$cocl_code,$txt_cocl)."<BR>"; // BR important pour retour à la ligne final
			
				$pdf->SetFont('Arial','',10);
				$pdf->WriteHTML($txt_cocl);

				$pdf->Output('Contrat_'.$cocl_code,'I');

			}
		}
		else
			$pbm_cocl = TRUE;

	}
?>