<?php $katao_supplier = $form->getObject() ?>
<div class="content-add01">
	<div class="heading01"><h1><?php echo $katao_supplier->isNew() ? 'Ajout' : 'Modification' ?> d'un fournisseur</h1></div>
</div>

<form action="<?php echo url_for('kataoSupplier/update' . (!$katao_supplier->isNew() ? '?id=' . $katao_supplier->getId() : '')) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
  <table>
    <tfoot>
      <tr>
        <td colspan="2">
          <br /><span id="submit-tag" style="display:inline"><input type="submit" value="Sauver" /> ou </span><a href="<?php echo url_for('kataoSupplier/index') ?>">annuler</a><?php if (!$katao_supplier->isNew()): ?> ou <a href="<?php echo url_for('@katao_supplier_edit_presentation?id=' . $katao_supplier->getId()) ?>">modifier sa présentation</a><?php endif ?>
        </td>
      </tr>
    </tfoot>
    <tbody>
      <?php echo $form->renderGlobalErrors() ?>
      <tr valign="top">
	  	<td colspan="2" align="left"><u>Informations générales</u></td>
	  </tr>
	  <tr valign="top">
        <th align="left"><?php echo $form['siret_number']->renderLabel() ?></th>
        <td>
          <?php echo $form['siret_number'] ?><span id="ajax-loader" style="display:none"><?php echo image_tag('actions/ajax-loader.gif', array('width' => 16, 'height' => 16, 'alt' => 'Chargement', 'title' => 'Vérification du N° SIRET')) ?></span>
		  <?php echo $form['siret_number']->renderError() ?>
        </td>
      </tr>
      <tr valign="top">
        <th align="left"><?php echo $form['name']->renderLabel() ?></th>
        <td>
          <?php echo $form['name'] ?>
		  <?php echo $form['name']->renderError() ?>
        </td>
      </tr>
	  <tr valign="top">
        <th align="left"><?php echo $form['email']->renderLabel() ?></th>
        <td>
          <?php echo $form['email'] ?>
		  <?php echo $form['email']->renderError() ?>
        </td>
      </tr>
	  <tr valign="top">
	  	<td colspan="2" align="left"><br /><u>Coordonnées</u></td>
	  </tr>
	  <tr valign="top">
        <th align="left"><?php echo $form['address1']->renderLabel() ?></th>
        <td>
          <?php echo $form['address1'] ?>
		  <?php echo $form['address1']->renderError() ?>
        </td>
      </tr>
      <tr valign="top">
        <th align="left"><?php echo $form['address2']->renderLabel() ?></th>
        <td>
          <?php echo $form['address2'] ?>
		  <?php echo $form['address2']->renderError() ?>
        </td>
      </tr>
      <tr valign="top">
        <th align="left"><?php echo $form['zip']->renderLabel() ?> / <?php echo $form['city']->renderLabel() ?></th>
        <td>
          <?php echo $form['zip'] ?>
          <?php echo $form['city'] ?>
		  <?php echo $form['zip']->renderError() ?>
		  <?php echo $form['city']->renderError() ?>
        </td>
      </tr>
      <tr valign="top">
        <th align="left"><?php echo $form['phone']->renderLabel() ?></th>
        <td>
          <?php echo $form['phone'] ?>
		  <?php echo $form['phone']->renderError() ?>
        </td>
      </tr>
      <tr valign="top">
        <th align="left"><?php echo $form['fax']->renderLabel() ?></th>
        <td>
          <?php echo $form['fax'] ?>
		  <?php echo $form['fax']->renderError() ?>
        </td>
      </tr>
      <tr valign="top">
        <th align="left"><?php echo $form['website']->renderLabel() ?></th>
        <td>
          <?php echo $form['website'] ?>
		  <?php echo $form['website']->renderError() ?>
        </td>
      </tr>
	  <tr valign="top">
	  	<td colspan="2" align="left"><br /><u>Informations supplémentaires</u></td>
	  </tr>
	  <tr valign="top">
        <th align="left"><?php echo $form['member_id']->renderLabel() ?></th>
        <td>
          <?php echo $form['member_id'] ?>
		  <?php echo $form['member_id']->renderError() ?>
        </td>
      </tr>
  <?php if (!$sf_user->isJustReferer()): ?>
  	  <tr valign="top">
        <th align="left"><?php echo $form['referer_id']->renderLabel() ?></th>
        <td>
          <?php echo $form['referer_id'] ?>
		  <?php echo $form['referer_id']->renderError() ?>
        </td>
      </tr>
  <?php endif ?>
      <tr valign="top">
        <th align="left"><?php echo $form['order_min_amount']->renderLabel() ?></th>
        <td>
          <?php echo $form['order_min_amount'] ?> €
		  <?php echo $form['order_min_amount']->renderError() ?>

		<?php echo $form['id'] ?>

	  <?php if ($sf_user->isJustReferer()): ?>
	  	<?php echo input_hidden_tag('katao_supplier[referer_id][]', $sf_user->getInstance()->getKataoMemberId()) ?>
	  <?php endif ?>
        </td>
      </tr>
  <?php if (!$katao_supplier->isNew()): ?>
      <tr valign="top">
        <th align="left"><label>Code comptable (€)</label></th>
        <td>
          <?php echo $katao_supplier->getAccountingCode() ?>
        </td>
      </tr>
      <tr valign="top">
        <th align="left"><label>Code comptable (<?php echo wpConfig::getAdditionalCurrencyName() ?>)</label></th>
        <td>
          <?php echo $katao_supplier->getAccountingCodeSol() ?>
        </td>
      </tr>
  <?php endif ?>
  <?php if (!$sf_user->isJustDelegate() && !$sf_user->isJustReferer()): ?>
      <tr valign="top">
        <th align="left"><?php echo $form['is_global']->renderLabel() ?></th>
        <td>
          <?php echo $form['is_global'] ?>
		  <?php echo $form['is_global']->renderError() ?>
        </td>
      </tr>
  <?php else: ?>
  	  <tr valign="top">
        <th align="left">Type</th>
        <td>
          <?php echo $katao_supplier->getTypeStr() ?>
		  <?php echo input_hidden_tag('katao_supplier[is_global]', $form['is_global']->getValue()) ?>
	  <?php if ($sf_user->isJustDelegate() && $katao_supplier->isNew()): ?>
	  	  <?php echo input_hidden_tag('katao_supplier[katao_supplier_node_list][]', $sf_user->getInstance()->getKataoMember()->getKataoNodeId()) ?>
	  <?php else: ?>
		<?php foreach ($form['katao_supplier_node_list']->getValue() as $katao_node_id): ?>
		  <?php echo input_hidden_tag('katao_supplier[katao_supplier_node_list][]', $katao_node_id) ?>
		<?php endforeach ?>
	  <?php endif ?>
        </td>
      </tr>
  <?php endif ?>
  <?php if (!$sf_user->isJustDelegate() && !$sf_user->isJustReferer()): ?>
      <tr valign="top" id="katao_supplier_node_list_row"<?php echo $katao_supplier->getIsGlobal()?' style="display: none"':''?>>
        <th align="left"><?php echo $form['katao_supplier_node_list']->renderLabel() ?></th>
        <td>
          <?php echo $form['katao_supplier_node_list'] ?>
		  <?php echo $form['katao_supplier_node_list']->renderError() ?>
        </td>
      </tr>
  <?php endif ?>
      <tr valign="top">
        <th align="left"><?php echo $form['katao_supplier_product_category_list']->renderLabel() ?></th>
        <td>
          <?php echo $form['katao_supplier_product_category_list'] ?>
		  <?php echo $form['katao_supplier_product_category_list']->renderError() ?>
        </td>
      </tr>
	  <?php include_partial('katao/user', array('form' => $form, 'is_member' => false)) ?>
    </tbody>
  </table>
</form>

<script type="text/javascript">
/* <![CDATA[ */

function checkSiretNumberAvailability(){
	$('ajax-loader').show();
	new Ajax.Request('<?php echo url_for('kataoSupplier/checkSiretNumberAvailability') ?>', {
	 	method: 'get',
		parameters: {
			id: <?php echo (int)$katao_supplier->getId() ?>,
			siret_number: $F('katao_supplier_siret_number')
		},
		onSuccess: checkSiretNumberAvailabilityResponse
	});
}

function checkSiretNumberAvailabilityResponse(xhr){
	$('ajax-loader').hide();
	var response = xhr.responseText;
	if ('national' == response) {
		alert('Un fournisseur national existe déjà avec ce n° SIRET.');
		$('submit-tag').style.display = 'none';
	} else if ('local' == response) {
		if (confirm('Un fournisseur local existe déjà avec ce n° SIRET.\nVoulez-vous rattacher ce fournisseur à votre noeud ?')) {
			new Ajax.Request('<?php echo url_for('kataoSupplier/addLocalSupplierToNode') ?>', {
			 	method: 'get',
				parameters: {
					siret_number: $F('katao_supplier_siret_number')
				},
				onSuccess: addLocalSupplierToNodeResponse
			});
		} else {
			$('submit-tag').style.display = 'none';
		}
	} else {
		$('submit-tag').style.display = 'inline';
	}
}

function addLocalSupplierToNodeResponse(xhr){
	var response = xhr.responseText;
	window.location = '<?php echo url_for('kataoSupplier/index') ?>';
}

/* ]]> */
</script>