<?php
include ("../master.php");

function select_pieces_contrat_type($type){
	
	$pieces_contrat_type=array();
	$result=mysql_query("SELECT p.id FROM contrats_type_pieces ctp, pieces p WHERE ctp.id_contrat_type='".$type."' AND p.id=ctp.id_piece");
	while ($row=mysql_fetch_array($result)){
		$pieces_contrat_type[]=$row["id"];
		}
		
		
	$return_me='<select onchange="parent.piece_add(this.options[this.selectedIndex].value, this.options[this.selectedIndex].text); this.options[0].selected=true;"><option value="">Ajouter une pièce à fournir';
	
	$result=mysql_query("SELECT id, nom FROM pieces");
	while ($row=mysql_fetch_array($result)){
		$return_me.='<option value="'.$row["id"].'" '.((in_array($row["id"], $pieces_contrat_type))?'style="color:red"':"").'>'.$row["nom"];
		}
	$return_me.='</select>';
	echo '<script>parent.document.getElementById(\'select_pieces_div\').innerHTML=\''.addslashes($return_me).'\';</script>';
	}

function select_devis_apporteurs($id_apporteur, $id_devis){
	
	$return_me='<select name="id_devis" '.$_GET["disabled"].'>';
	
	$result=mysql_query("SELECT id, id_axa, date_add FROM devis WHERE id_apporteur='".$id_apporteur."' AND id_axa!='0' AND id_axa!=''");
	while ($row=mysql_fetch_array($result)){
		$return_me.='<option value="'.$row["id"].'" '.(($id_devis==$row["id"])?"SELECTED":"").'>#'.$row["id_axa"].' du '.dateshort($row["date_add"]);
		}
	
	$return_me.='<option value=""'.(($id_devis=="0")?"SELECTED":"").'>Non listé</select>';
	echo '<script>parent.document.getElementById(\'id_devis_div\').innerHTML=\''.$return_me.'\';</script>';
	}


function validate_contrat($id_contrat){	
	$result=mysql_query("SELECT id_client FROM contrats WHERE id_axa='".sn($id_contrat)."'");
	if (mysql_num_rows($result)==0) $return_me.='Ce contrat n\'existe pas !';
	else $return_me.=
		'<a title="Voir ce contrat" href="contrat.php?req=mod&id='.$id_contrat.'"><img src="../images/admin/contrat.gif" border="0" align="absmiddle"></a>&nbsp;&nbsp;'.
		'<a title="Voir le client" href="client.php?req=mod&id="'.mysql_result($result, 0, "id_client").'"><img src="../images/admin/client.gif" border="0" align="absmiddle">'.client_id_axa(mysql_result($result, 0, "id_client"))."</a> ".
		client_nom(mysql_result($result, 0, "id_client")).'';
	echo '<script>parent.document.getElementById(\'id_contrat_div\').innerHTML=\''.addslashes($return_me).'\';</script>';
	}
	
	

if (!isset($_GET["req"])) $_GET["req"]="";
switch($_GET["req"]) {
	case "select_pieces_contrat_type":
		select_pieces_contrat_type($_GET["id"]);
		break;
		
	case "select_devis_apporteurs":
		select_devis_apporteurs($_GET["id"], $_GET["idd"]);
		break;
		
	case "validate_contrat":
		validate_contrat($_GET["id"]);
		break;
	
	default:
    	break;
	}
	
?>