/**
 * 	Funciones para la creacion y manipulacion de hojas de operacion
 * 
 */
	/**
	 *  A�ade un renglon al formulario de OperationSheet
	 * 
	 */

	function addOperationExpense(){
		var operationExpense = new Object();
		var itemTemplate = new Template(
			'<tr id="partida#{numeroPartida}" class="tr odd" onclick="showArrow(#{numeroPartida}, #{numeroParte})">' + 
				'<td class="id">' +
					'<span class="ajaxBg" >' +
						'<span id="expValidImg#{numeroPartida}"></span>' +
						'<span id="expLoading#{numeroPartida}" style="display:none;" >#{loaderImg}</span>' +
					'</span>' +
					'<input type="text" class="csProvId" value="" id="id#{numeroPartida}" name="data[Part][#{numeroParte}][Expense][#{numeroPartida}][id]"/>' +
	                '<input type="hidden" value="" id="concepto#{numeroPartida}" name="data[Part][#{numeroParte}][Expense][#{numeroPartida}][concepto]"/>' +
	                '<input type="hidden" value="" id="cotPrecio#{numeroPartida}" name="data[Part][#{numeroParte}][Expense][#{numeroPartida}][precio]"/>' +
	                '<input type="hidden" value="" id="precio_min#{numeroPartida}" name="data[Part][#{numeroParte}][Expense][#{numeroPartida}][precio_min]"/>' +
	                '<input type="hidden" value="" id="cotMoneda#{numeroPartida}" name="data[Part][#{numeroParte}][Expense][#{numeroPartida}][moneda]"/>' +
	                '<input type="hidden" value="" id="expenseValid#{numeroPartida}" name="data[Part][#{numeroParte}][Expense][#{numeroPartida}][valid]"/>' +
	                '<input type="hidden" value="" id="parte#{numeroPartida}" name="data[Part][#{numeroParte}][Expense][#{numeroPartida}][parte]"/>' +
	                '<input type="hidden" value="#{numeroParte}" id="opExParte#{numeroPartida}" name="data[Part][#{numeroParte}][Expense][#{numeroPartida}][OperationExpense][parte]"/>' +
	                '<input type="hidden" value="" id="expense_id#{numeroPartida}" name="data[Part][#{numeroParte}][Expense][#{numeroPartida}][OperationExpense][expense_id]"/>' +
	                '<input type="hidden" value="" id="supValid#{numeroPartida}" name="data[Part][#{numeroParte}][Expense][#{numeroPartida}][OperationExpense][valid]"/>' +
	            '</td>' +
				'<td><span id="conceptoSpan#{numeroPartida}"></span></td>' +
				'<td>' + deleteCodeOpExp + '</td>' +
				'<td>' +
					'<table class="infoSupplier noNiko">' +
						'<tbody>' +
	                       '<tr class="odd">' +
	                            '<td class="ajaxBg">' +
	                                '<div id="supValidImg#{numeroPartida}">#{alphaImg}</div>' +
									'<div id="supLoading#{numeroPartida}" style="display:none;" >#{loaderImg}</div>' +
	                            '</td>' +
	                            '<td class="id"><input type="text" value="" id="supId#{numeroPartida}" name="data[Part][#{numeroParte}][Expense][#{numeroPartida}][OperationExpense][supplier_id]"/></td>' +
	                            '<td class="nombre"><input type="text" value="" id="supNombre#{numeroPartida}" readonly="1" name="data[Part][#{numeroParte}][Expense][#{numeroPartida}][OperationExpense][supNombre]"/></td>' +
	                            '<td class="precio"><input type="text" value="" id="supPrecio#{numeroPartida}" name="data[Part][#{numeroParte}][Expense][#{numeroPartida}][OperationExpense][precioSup]"/><span id="monedaSpan#{numeroPartida}"></span></td>' +
	                        '</tr>' + 
						'</tbody>' +
	                '</table>' +
				'</td>' +
				'<td class="venta"><input type="text" value="" id="venta#{numeroPartida}" name="data[Part][#{numeroParte}][Expense][#{numeroPartida}][OperationExpense][venta]"/><span id="monedaSpan1-#{numeroPartida}"></span></td>' +
				'<td class="obsevaciones"><input type="text" value="" id="observaciones#{numeroPartida}" name="data[Part][#{numeroParte}][Expense][#{numeroPartida}][OperationExpense][observaciones]"/>' +
	            '<!-- Ajax Zone -->' +
					'<script>' +
						'/* Observamos los campos de id de proveedor */' +
						'Event.observe(\'supId#{numeroPartida}\', \'change\', function(event){ ' +
							'new Ajax.Updater(\'supValidImg#{numeroPartida}\',\'#{baseDir}/admin/suppliers/validate?id=\' + $(\'supId#{numeroPartida}\').value +\'&partida=#{numeroPartida}\' , {onLoading:function(request){Element.hide(\'supValidImg#{numeroPartida}\'); Element.show(\'supLoading#{numeroPartida}\')}, onComplete:function(request){Element.hide(\'supLoading#{numeroPartida}\'); Effect.Appear(\'supValidImg#{numeroPartida}\')}, asynchronous:true, evalScripts:true, requestHeaders:[\'X-Update\', \'supValidImg#{numeroPartida}\']}) }, false);' +
						'Event.observe(\'id#{numeroPartida}\', \'change\', function(event){ ' +
							'new Ajax.Updater(\'expValidImg#{numeroPartida}\',\'#{baseDir}/admin/expenses/validate?id=\' + $(\'id#{numeroPartida}\').value +\'&partida=#{numeroPartida}&action=operationSheet\' , {onLoading:function(request){Element.hide(\'expValidImg#{numeroPartida}\'); Element.show(\'expLoading#{numeroPartida}\')}, onComplete:function(request){Element.hide(\'expLoading#{numeroPartida}\'); Effect.Appear(\'expValidImg#{numeroPartida}\')}, asynchronous:true, evalScripts:true, requestHeaders:[\'X-Update\', \'expValidImg#{numeroPartida}\']}) }, false);' +
						'$(\'supPrecio#{numeroPartida}\').value = formatoValido($(\'supPrecio#{numeroPartida}\').value);' +
						'Event.observe(\'supPrecio#{numeroPartida}\', \'change\', function(event){ $(\'supPrecio#{numeroPartida}\').value = formatoValido($(\'supPrecio#{numeroPartida}\').value); } );' +						
						'$(\'venta#{numeroPartida}\').value = formatoValido($(\'venta#{numeroPartida}\').value);' +
						'Event.observe(\'venta#{numeroPartida}\', \'change\', function(event){ $(\'venta#{numeroPartida}\').value = formatoValido($(\'venta#{numeroPartida}\').value); } );' +						
					'</script>' +
					'<!--  =Ajax Zone -->' +
				'</td>' +
			'</tr>'
		);
		operationExpense = { numeroPartida: numero, numeroParte:parteActiva, baseDir:base, loaderImg:loader, alphaImg:alpha};
		b = itemTemplate.evaluate(operationExpense);
		
		new Insertion.Bottom('tbOpExp' + parteActiva,  b); 
		numero++;
		focuss =  parteActiva;
		tableDecorator('tbOpExp');

	}
	
	function addOpExpenseFromSearch(id, concepto, supplier_id, moneda, cuenta, tipo, precio_min, precio, supNombre)
	{
		addOperationExpense();
		numeroAux = numero - 1;
		/*
		if($('supId' + numeroAux))
			$('supId'+ numeroAux).value = id;
		if($('supNombre' + numeroAux))
		   $('supNombre'+ numeroAux).value = supNombre;*/
		//addOpExpenseFromSearch
		addPartidaFromSearch(id, concepto, supplier_id, moneda, cuenta, tipo, precio_min, precio, false);
	}
	
	/**
	 *  Agrega la informacion del proveedor principal en la hoja de operaciones
	 * 
	 */

	function addSupplierInfo(id, nombre, rfc, telefono )
	{
		if( $('supplier_id') )
			$('supplier_id').value = id;
		if ( $('supIdN') )
			$('supIdN').value = id;
		if( $('supplier_nombre') )
			$('supplier_nombre').value = nombre;
		if( $('supplier_rfc') )
			$('supplier_rfc').value = rfc; 
		if( $('supplier_tel') )
			$('supplier_tel').value = telefono;
		
	}
	
	
	/** muestra u oculta los elementos **/
	function muestraOculta(elementId)
	{
		if($(elementId))
		{
			if($(elementId).checked)
			{
				if(elementId == "OperationSheetCheckEntradaSalida"){
					$$('div.opEntradaSalida').each( 
							function(item, index){ 
								item.removeClassName("hide") 
								item.show() ;
							}
					   );
					$('OperationSheetFsalida').disabled = 0;
					$('OperationSheetFentrada').disabled = 0;
				}else if(elementId == "OperationSheetCheckInicioVenc"){
					$$('div.opInicioVenc').each( 
							function(item, index){ 
								item.removeClassName("hide") 
								item.show(); 
							}
					   );
					$('OperationSheetFinicio').disabled = 0;
					$('OperationSheetFtermino').disabled = 0;
				}				
				
			}else{
				if(elementId == "OperationSheetCheckEntradaSalida"){
					$$('div.opEntradaSalida').each( 
							function(item, index){ 
								item.hide() 
							}
					   );
					$('OperationSheetFsalida').disabled = 1;
					$('OperationSheetFentrada').disabled = 1;
				}else if(elementId == "OperationSheetCheckInicioVenc"){
					$$('div.opInicioVenc').each( 
							function(item, index){ 
								item.hide() 
							}
					   );
					$('OperationSheetFinicio').disabled = 1;
					$('OperationSheetFtermino').disabled = 1;
				}
			}
		}
	}
	