
<tr id="<?=$job['Job']['id']?>">

<script type="text/javascript">

    $(function() {

	/** This could be cleaner. However, this will work with multiple rows being edited */
	getSelectIDs();
	$('#<?=$job['Job']['id']?>_date_order_received_display').datepicker({
	    showButtonPanel: true,
	    dateFormat: 'd M yy',
	    altFormat: 'yy-mm-dd',
	    altField: '#<?=$job['Job']['id']?>_date_order_received'

	});


	$('#<?=$job['Job']['id']?>_date_scheduled_ex_works_display').datepicker({
	    showButtonPanel: true,
	    dateFormat: 'd M yy',
	    altFormat: 'yy-mm-dd',
	    altField: '#<?=$job['Job']['id']?>_date_scheduled_ex_works'

	});

	$('#<?=$job['Job']['id']?>_date_order_sent_to_customer_display').datepicker({
	    showButtonPanel: true,
	    dateFormat: 'd M yy',
	    altFormat: 'yy-mm-dd',
	    altField: '#<?=$job['Job']['id']?>_date_order_sent_to_customer'

	});


	$("#<?=$job['Job']['id']?>_sale_category").change(function() {
	    getSelectIDs();
	});



	$("#<?=$job['Job']['id']?>_currency_id").change(function() {
	    getSelectIDs();
	});



	function getSelectIDs() {
	    var catID = getSaleCategoryID();
	    var currencyID = getCurrencyID();
	    whichInputsReadOnly(catID, currencyID);
	}



	function whichInputsReadOnly(catID, currencyID) {
	    $(".highlighted").removeClass("highlighted");

	    if(catID < 2) { //Aust Sale.
		if(currencyID == 2) { //Aust Sale. AUD
		    $(".aust_sale_aud").attr("readonly",false);
		    $(".aust_sale_aud").addClass("highlighted");

		    $(".aust_sale_fc").attr("readonly",true);
		    $('.export_sale_fc').attr("readonly",true);
		    $('.export_sale_aud').attr("readonly",true);
		}
		else { //Aust Sale, Foreign Currency.
		    $(".aust_sale_fc").attr("readonly",false);
		    $(".aust_sale_fc").addClass("highlighted");

		    $(".aust_sale_aud").attr("readonly",true);
		    $('.export_sale_fc').attr("readonly",true);
		    $('.export_sale_aud').attr("readonly",true);
		}

	    }
	    else {
		//Handle Commissions.
		if(catID == 2 || catID == 5) { //Commission. Australian Supplier.
		    $(".aust_sale_aud").attr("readonly",true);
		    $(".aust_sale_fc").attr("readonly",true);
		    $('.export_sale_fc').attr("readonly",true);
		    $('.export_sale_aud').attr("readonly",true);
		    $('.commissions_any_currency').addClass("highlighted");
		}
		else { 
		    //Export Sale
		    if(currencyID == 2) {//Export Sale. AUD
			$('.export_sale_aud').attr("readonly",false);
			$(".export_sale_aud").addClass("highlighted");
			$(".aust_sale_aud").addClass("highlighted");

			$(".aust_sale_fc").attr("readonly",true);
			$('.export_sale_fc').attr("readonly",true);

		    }
		    else { //Export Sale. Foreign Currency.
			$('.export_sale_fc').attr("readonly",false);
			$(".export_sale_fc").addClass("highlighted");

			$(".aust_sale_fc").attr("readonly",true);
			$(".aust_sale_aud").attr("readonly",true);
			$('.export_sale_aud').attr("readonly",true);

		    }
		}
	    }



	}

	function getSaleCategoryID() {
	    var id;
	    $("#<?=$job['Job']['id']?>_sale_category :selected").each(function(i, selected) {
		id = this.value;
	    });
	    return id;
	}

	function getCurrencyID() {
	    var id;
	    $("#<?=$job['Job']['id']?>_currency_id :selected").each(function(i, selected) {
		id = this.value;
	    });
	    return id;
	}


    });

</script>


<?php
//format the mySQL date in the nice readable format.
/*if($job['date_order_received'] != '0000-00-00') {
    $order_received_time = strtotime($job['Job']['date_order_received']);
    $order_received_readable = date('j M Y', $order_received_time );
}
else {
    $order_received_readable = "";
}

if($job['date_order_sent_to_customer'] != '0000-00-00') {
    $order_sent_to_customer_time = strtotime($job['Job']['date_order_sent_to_customer']);
    $order_sent_readable = date('j M Y', $order_sent_to_customer_time);
}
else {
    $order_sent_readable = "";
}*/

?>

<?=$form->input('id', array('type'=>'hidden', 'value'=>$job['Job']['id'])); ?>
<td><button class="saveButton">Save</button> <button class="cancelButton">Cancel</button></td>
<td><?=$form->input('date_order_received_display', array('value'=>$this->element('isEmptyDate', array('date'=>$job['Job']['date_order_received'])), 'label'=>false, 'id'=>$job['Job']['id'].'_date_order_received_display'));?>
    <?=$form->input('date_order_received', array('type'=>'hidden', 'id'=>$job['Job']['id'].'_date_order_received', 'value'=>$job['Job']['date_order_received']));?>
</td>
<td><?=$form->input('all_paid', array('type'=>'select','options'=>array($yesNo), 'label'=>false, 'selected'=>$job['Job']['all_paid'], 'class'=>'yesNo'));?></td>
<td><?=$form->input('all_sent', array('type'=>'select','options'=>array($yesNo), 'label'=>false, 'selected'=>$job['Job']['all_sent'], 'class'=>'yesNo'));?></td>
<td><?=$form->input('job_status', array('type'=>'select', 'options'=>$job_status_options, 'value'=>$job['Job']['job_status'], 'label'=>false, 'class'=>''));?></td>
<td>
    <?=$form->input('sale_category', array('id'=>$job['Job']['id'].'_sale_category',  'options'=>$sale_category_options, 'type'=>'select', 'label'=>false, 'value'=>$job['Job']['sale_category'], 'class'=>'sale_category'));?>
</td>
<td>
    <?=$form->input('job_type', array('options'=>$job_type_options, 'label' => false, 'selected'=>$job['Job']['job_type']));?></td>
<td><?=$form->input('shipment_category', array('options'=>$shipment_category_options, 
	    'type'=>'select','label'=>false,'value'=>$job['Job']['shipment_category']));?>
</td>
<td><?=$html->link($job['Job']['title'], array('controller'=>'jobs', 'action'=>'view', $job['Job']['id']));?></td>
<td><?=$html->link($job['Enquiry']['title'], array('controller'=>'enquiries','action'=>'view', $job['Enquiry']['id']));?></td>
<td><?=$html->link($principleList[$job['Enquiry']['principle_id']], '/principles/view/'.$job['Enquiry']['principle_id']);?></td>
<td><?
    $poString = '';
    foreach($job['PurchaseOrder'] as $po) {
	$poString .= ' '.$html->link($po['title'], array('controller'=>'purchase_orders', 'action'=>'view', $po['id']));
    }
    echo $poString;
    ?>
</td>
<td class="nowrap"><?
    foreach($job['PurchaseOrder'] as $po) {
	echo $this->element('isEmptyDate', array('date'=>$po['issue_date']));
	echo "<br>";
    }
    ?>
</td>

<td><?=$html->link($job['Customer']['name'], array('controller'=>'customers','action'=>'view', $job['Customer']['id']));?></td>
<td>
    <?=$form->input('customer_order_number', array('value'=>$job['Job']['customer_order_number'], 'label'=>false));?>
</td>
<td>    <?
    foreach($job['PurchaseOrder'] as $po) {
	echo $po['principle_reference'];
	echo "<br>";
    }
    ?>
</td>

<td>
    <?=$form->input('date_scheduled_ex_works_display', array('value'=>$this->element('isEmptyDate', array('date'=>$job['Job']['date_scheduled_ex_works'])), 'label'=>false, 'id'=>$job['Job']['id'].'_date_scheduled_ex_works_display'));?>
    <?=$form->input('date_scheduled_ex_works', array('type'=>'hidden', 'id'=>$job['Job']['id'].'_date_scheduled_ex_works', 'value'=>$job['Job']['date_scheduled_ex_works'])); ?>
</td>
<td>
    <?=$form->input('date_order_sent_to_customer_display', array('value'=>$this->element('isEmptyDate', array('date'=>$job['Job']['date_order_sent_to_customer'])), 'label'=>false, 'id'=>$job['Job']['id'].'_date_order_sent_to_customer_display'));?>
    <?=$form->input('date_order_sent_to_customer', array('type'=>'hidden', 'id'=>$job['Job']['id'].'_date_order_sent_to_customer', 'value'=>$job['Job']['date_order_sent_to_customer'])); ?>
</td>
<td> <?=$form->input('domestic_freight_paid_by', array('type'=>'select','options'=>$freight_paid_by_options, 'value'=>$job['Job']['domestic_freight_paid_by'], 'label'=>false));?>
</td>
<td>
    <?=$form->input('currency_id', array('id'=>$job['Job']['id']."_currency_id",'value'=>$job['Job']['currency_id'], 'type'=>'select', 'options'=>$currencies, 'label'=>false ));?>
</td>
<td>
    <?=$form->input('company_gross_sales_aud', array('id'=>$job['Job']['id']."_company_gross_sales_aud", 'value'=>$job['Job']['company_gross_sales_aud'], 'label'=>false,'class'=>'aust_sale_aud'));?>

</td>
<td>
    <?=$form->input('net_sales_aud', array('id'=>$job['Job']['id']."_net_sales_aud",'value'=>$job['Job']['net_sales_aud'], 'label'=>false,'class'=>'aust_sale_aud'));?>
</td>
<td> <?=$form->input('gross_profit_aud', array('id'=>$job['Job']['id']."_gross_profit_aud",'value'=>$job['Job']['gross_profit_aud'], 'label'=>false,'class'=>'aust_sale_aud' ));?>
</td>
<td>
    <?=$form->input('net_export_sales_aud', array('id'=>$job['Job']['id']."_net_export_sales_aud",'value'=>$job['Job']['net_export_sales_aud'], 'label'=>false,'class'=>'export_sale_aud' ));?>
</td>
<td>
    <?=$form->input('gross_profit_exports_aud', array('id'=>$job['Job']['id']."_gross_profit_exports_aud",'value'=>$job['Job']['gross_profit_exports_aud'], 'label'=>false,'class'=>'export_sale_aud'));?>
</td>
<td>
    <?=$form->input('gp_percent', array('id'=>$job['Job']['id']."_gp_percent",'value'=>$job['Job']['gp_percent'], 'label'=>false));?>
</td>

<td>
    <?=$form->input('ato_exchange_rate', array('id'=>$job['Job']['id']."_ato_exchange_rate",'value'=>$job['Job']['ato_exchange_rate'], 'label'=>false ));?>
</td>
<td> <?=$form->input('gross_australian_sales_foreign_currency', array('id'=>$job['Job']['id']."_gross_australian_sales_foreign_currency",'value'=>$job['Job']['gross_australian_sales_foreign_currency'], 'label'=>false, 'class'=>'aust_sale_fc'));?>
</td>
<td>
    <?=$form->input('net_australian_sales_foreign_currency', array('id'=>$job['Job']['id']."_net_australian_sales_foreign_currency",'value'=>$job['Job']['net_australian_sales_foreign_currency'], 'label'=>false, 'class'=>'aust_sale_fc'));?>
</td>
<td> <?=$form->input('gross_profit_value_australian_sales_foreign_currency', array('id'=>$job['Job']['id']."_gross_profit_value_australian_sales_foreign_currency",'value'=>$job['Job']['gross_profit_value_australian_sales_foreign_currency'], 'label'=>false, 'class'=>'aust_sale_fc'));?>
</td>
<td>
    <?=$form->input('gross_export_sales_foreign_currency', array('id'=>$job['Job']['id']."_gross_export_sales_foreign_currency",'value'=>$job['Job']['gross_export_sales_foreign_currency'], 'label'=>false, 'class'=>'export_sale_fc'));?>
</td>
<td> <?=$form->input('net_export_sales_foreign_currency', array('id'=>$job['Job']['id']."_net_export_sales_foreign_currency",'value'=>$job['Job']['net_export_sales_foreign_currency'], 'label'=>false,'class'=>'export_sale_fc'));?>
</td>
<td> <?=$form->input('gross_profit_value_after_discount_exports_foreign_currency', array('id'=>$job['Job']['id']."_gross_profit_value_after_discount_exports_foreign_currency",'value'=>$job['Job']['gross_profit_value_after_discount_exports_foreign_currency'], 'label'=>false, 'class'=>'export_sale_fc'));?>
</td>
<td>
    <?=$form->input('gross_commissions', array('id'=>$job['Job']['id']."_gross_commissions",'value'=>$job['Job']['gross_commissions'], 'label'=>false, 'class'=>'commissions_any_currency'));?>
</td>
<td> <?=$form->input('net_commissions', array('id'=>$job['Job']['id']."_net_commissions",'value'=>$job['Job']['net_commissions'], 'label'=>false,'class'=>'commissions_any_currency'));?>
</td>
<td><button class="saveButton">Save</button> <button class="cancelButton">Cancel</button></td>
</tr>
<?php //debug($job); ?>