$(document).ready(function(){
	$("#browser").treeview({
		toggle: function() {
			console.log("%s was toggled.", $(this).find(">span").text());
		}
	});
	
	/*$("#add").click(function() {
		var branches = $("<li><span class='folder'>New Sublist</span><ul>" + 
			"<li><span class='file'>Item1</span></li>" + 
			"<li><span class='file'>Item2</span></li></ul></li>").appendTo("#browser");
		$("#browser").treeview({
			add: branches
		});
	});*/
	
	$("#btFormRMA").css("cursor", "pointer")
	$("#btFormRMA").click(function(){
		
		if ($("#FormRMA").css("display") == "none"){ $("#FormRMA").slideDown("normal");  }
		else { $("#FormRMA").css("display","none"); }
	});	
	
	$("#btFormCadastro").css("cursor", "pointer")
	$("#btFormCadastro").click(function(){
		
		if ($("#FormCadastro").css("display") == "none") { $("#FormCadastro").slideDown("normal") }
		else {$("#FormCadastro").css("display","none");}
	});
	
	//$("#btDownloadMaterial2").css("cursor", "pointer")
	/*
	$("#btDownloadMaterial").click(function(){
		
		if ($("#DownloadMaterial").css("display") == "none") { $("#DownloadMaterial").slideDown("normal") }
		else {$("#DownloadMaterial").css("display","none");}
	});
	*/
	
	
	
	$("#btRmaAdicionarItem").click(function (){
											
		var rows = new Array();
		
		
		
		var Y = 0; 
		var X = 0; 
		var totY = 0;
		var totX = 0;
		Y = 0; 
	//    totY = $("#tableRMA tr").length
		totY = $("#tableRMA tr").length
		
		// CARREGA OS DADOS 
		$("#tableRMA tr").each(function (){
			X = 0; 
			totX = $(this).children().length	
			rows[Y] = new Array()
			$(this).children().each(function(){ 
				if(Y > 0 && Y < totY-1){
						//$(this).css("border","1px solid")
					if(X > 0){
						//alert(Y + "/" + X + "/" + $(this).children().attr("name"))
						rows[Y][X] = {
								name 	: $(this).children().attr("name"), 
								id 		: $(this).children().attr("id"), 
								value 	: $(this).children().val()
									 }
					}else if(X == 0){
						rows[Y][X] = {
								name 	: 'label', 
								id 		: '', 
								value 	: Y - 1
									 }
					}
				}
				X++;
			})
			Y++;
		})
		strAux2 = '<td>' + (Y-1) + '.</td>'
		strAux2 = strAux2 + '<td><input type="text" class="med"  id="RMA_ITENS[' + (Y-1) + '][PARTNUMBER]" name="RMA_ITENS[' + (Y-1) + '][PARTNUMBER]"  /></td>'
		strAux2 = strAux2 + '<td><input type="text" class="peq"  id="RMA_ITENS[' + (Y-1) + '][PARTNAME]" name="RMA_ITENS[' + (Y-1) + '][PARTNAME]"  /></td>'
		strAux2 = strAux2 + '<td><input type="text" class="peq"  id="RMA_ITENS[' + (Y-1) + '][SERIAL]" name="RMA_ITENS[' + (Y-1) + '][SERIAL]"  /></td>'
		strAux2 = strAux2 + '<td><textarea  id="RMA_ITENS[' + (Y-1) + '][PROBLEM_DESCRIPTION]" name="RMA_ITENS[' + (Y-1) + '][PROBLEM_DESCRIPTION]"  ></textarea></td>'
        
		
		                          
		       
		
	Y = 0;	
	strAUX = $("#tableRMA").html();
	$("#tableRMA tr").each(function (){
		if(Y > 0 && Y < totY-1){
			//alert($(this).html())
			$(this).remove()
			//$(this).parent().empty()
	  	}
		Y++;
	})
	Y = 0;	
	var trAux = null;
	var strAux = "";
			//$(rows).each(function($i){ 
			/*
			$("#tableRMA tr:first-child").after("<tr>" + strAux + "</tr>");
			strAux = strAux + ('<td><input type="text" class="med"  id="RMA_ITENS[' +  $i + '][ITEM]" name="RMA_ITENS[' +  $i + '][ITEM]" value="' + this.value + '"  /></td>\n')	
			strAux = strAux + ('<td><input type="text" class="med"  id="RMA_ITENS[' +  $i + '][ITEM]" name="RMA_ITENS[' +  $i + '][ITEM]" value="' + this.value + '"  /></td>\n')	
			*/
	
	$("#tableRMA tr:first-child").after("<tr>" + strAux2 + "</tr>");
	
	for($i = rows.length-1; $i > 0; $i--){
		if(!rows[$i-1][1].value){
			rows[$i-1][1].value = ""	
		}
		strAux = '<td>' + ($i-1) + '.</td>'
		//strAux = strAux + '<td><input type="text" class="med"  id="RMA_ITENS[' + ($i-1) + '][ITEM]" name="RMA_ITENS[' + ($i-1) + '][ITEM]" value="' + rows[$i-1][1].value + '"  /></td> '
		strAux = strAux + '<td><input type="text" class="med"  id="RMA_ITENS[' + ($i-1) + '][PARTNUMBER]" name="RMA_ITENS[' + ($i-1) + '][PARTNUMBER]" value="' + rows[$i-1][1].value + '" /></td>'
		strAux = strAux + '<td><input type="text" class="peq"  id="RMA_ITENS[' + ($i-1) + '][PARTNAME]" name="RMA_ITENS[' + ($i-1) + '][PARTNAME]" value="' + rows[$i-1][2].value + '" /></td>'
		strAux = strAux + '<td><input type="text" class="peq"  id="RMA_ITENS[' + ($i-1) + '][SERIAL]" name="RMA_ITENS[' + ($i-1) + '][SERIAL]" value="' + rows[$i-1][3].value + '" /></td>'
		strAux = strAux + '<td><textarea  id="RMA_ITENS[' + ($i-1) + '][PROBLEM_DESCRIPTION]" name="RMA_ITENS[' + ($i-1) + '][PROBLEM_DESCRIPTION]"  >' + rows[$i-1][4].value + '</textarea></td>'
		$("#tableRMA tr:first-child").after("<tr>" + strAux + "</tr>");
	}
	
	$("body").append($("#tableRMA").html());
	//alert($("#tableRMA").html())
	//alert(strAUX + "\n\n\n" + $("#tableRMA").html());						
		
		
		for(y=0; y <= rows.lenght; y++){
			//alert(rows[y]);
		}
											
											
	})
	
	$("#searchOk").click(function(){
		$.get("/services/boxdownloadsearch/search/" + $('#searchBox').val(), function(data){
	    //$.get("/avionics/services/boxdownloadsearch/search/" + $('#searchBox').val(), function(data){
			$("#browser").html(data);
			$("#browser").treeview({
				toggle: function() {
					console.log("%s was toggled.", $(this).find(">span").text());
				}
			});
		})
	})

	$("#searchBox").keyup(function(){
		$.get("/services/boxdownloadsearch/search/" + $('#searchBox').val(), function(data){
	    //$.get("/avionics/services/boxdownloadsearch/search/" + $('#searchBox').val(), function(data){
			$("#browser").html(data);
			$("#browser").treeview({
				toggle: function() {
					console.log("%s was toggled.", $(this).find(">span").text());
				}
			});
		})
	})	
	
	
	
	
	
	
	
	$("#searchClear").click(function(){ window.location.href = '/sac/arearestrita'; })
//	$("#searchClear").click(function(){ window.location.href = '/avionics/sac/arearestrita'; })
	
});
