// JavaScript Document
var swOpenSubmenu=false;
$(document).ready(function(){
	$(".submenuCatalogo").hide();
	$("#devolucion").click(function(){
								openSubmenu();
	});
	
	$("#addProd_devolucion").click(function(){addRowDevolucion();});
	
	$("#motivo_item").keypress(function(){
								   if (this.keycode == 13) addRowDevolucion();
								   });
	
	$("#sendData").click(function(){sendDataDevolucion();});
	
	if (swOpenSubmenu) openSubmenu();
	
	$("#waiting").css("top","-"+$("#cabecera").height()+"px");
	$("#waiting").css("height",$(document).height()+"px");

});

function openSubmenu()
{
	if ($(".submenuCatalogo").hasClass("open"))
	{
		$(".submenuCatalogo").hide().removeClass("open");	
	}else{
		$(".submenuCatalogo").show().addClass("open");
	}	
}

var rowsDevolutionItems = new Array();
var countRows = 0;
function addRowDevolucion()
{
	/** inicializamos **/
	var tipo_item = $("#tipo_item").val();
	var producto = $("#producto_item").val();
	var cantidad = $("#cantidad_item").val();
	var factura = $("#nfactura_item").val();
	var motivo = $("#motivo_item").val();
	/**
	Validar
	**/
	if ((producto.length == "") && (cantidad == "") || (factura == ""))
	{
		alert(txt_aviso);
		return false;
	}
	var idRow = countRows++;
	prod = escape(producto);
	moti = escape(motivo);
	fact = escape(factura);
	rowsDevolutionItems.push({
			'id'	: idRow,
			'tipo_item' : tipo_item,
			'producto' : prod,
			'cantidad'	: cantidad,
			'nfactura' : fact,
			'motivo' : moti});
			
	$("#listado_items_rows").find('tbody')
    .append($('<tr id="row_'+idRow+'">')
        .append($('<td>').html(tipo_item))
		.append($('<td>').append($('<span>').addClass('textproducto').attr('title',producto).html(producto)))
		.append($('<td>').addClass('tdRight').html(cantidad))
		.append($('<td>').addClass('tdCenter').append($('<span>').addClass('textfactura').attr('title',factura).html(factura)))
		.append($('<td>').append($('<span>').addClass('textmotivo').attr('title',motivo).html(motivo)))
		.append($('<td>').html("<a href=\"javascript:;\" class=\"menuButton\" onclick=\"quitarRowDevolucion('"+idRow+"')\"><span>"+txt_quitar+"</span></a>"))
    );
//	$("#tipo_item").val();
	$("#producto_item").val("");
	$("#cantidad_item").val("");
	$("#nfactura_item").val("");
	$("#motivo_item").val("");
	$("#tipo_item").focus();
	return false;
}

function quitarRowDevolucion(id)
{
	var auxRows = new Array();
	/** Find Register **/
	for (var cc = 0; cc < rowsDevolutionItems.length; cc++)
	{
		if (rowsDevolutionItems[cc].id != id)
		{
			auxRows.push(rowsDevolutionItems[cc]);
		}else{
			//Delete row
			$("#row_"+id).remove();
		}
	}
	rowsDevolutionItems = auxRows;
	auxRows = null;
}

function cleanDevolucion()
{
	rowsDevolutionItems = new Array();
	var w = $("#listado_items_rows tr")
	w.each(function(e){
			$(this).remove();
	});
}

function sendDataDevolucion()
{
	contentButton = "<br><br><button type='button' class='button' onclick='closeOverlay()'>Cerrar</button>";
	$("#info").center();
	$("#waiting").show();
	post_data = "idx="+$("#idx").val();
	post_data += "&tipo="+$("#tipo").val();
	if (rowsDevolutionItems.length <= 0) {$("#info").html(txt_aviso_no_items+contentButton); return;}
	for (var cc = 0; cc < rowsDevolutionItems.length; cc++)
	{
		post_data += "&plataforma["+cc+"]="+rowsDevolutionItems[cc].tipo_item;
		post_data += "&producto["+cc+"]="+rowsDevolutionItems[cc].producto;
		post_data += "&cantidad["+cc+"]="+rowsDevolutionItems[cc].cantidad;
		post_data += "&nfactura["+cc+"]="+rowsDevolutionItems[cc].nfactura;
		post_data += "&motivo["+cc+"]="+rowsDevolutionItems[cc].motivo;
	}
	$.post('addDevolucion', post_data, function(data) {
 		 var x = data;
		 $("#info").css({"width":"250px","height":"100px"});
		$("#info").center();
		cleanDevolucion();
		$("#info").html(x.message+contentButton);
	},'json');
}

jQuery.fn.center = function () {
	this.css("position","absolute");
	this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	return this;
}

function closeOverlay(){
	$("#waiting").hide();	
	$("#info").html("Send data...");
	$("#info").css({"width":"140px","height":"80px"});
	$("#info").center();
}

function fireCleanPedido()
{
	if (confirm(txt_aviso_limpiar_devolucion))
	{
		cleanDevolucion();	
	}
}



