jQuery(document).ready(function(){
	
	var botonCodesPulsado = false;
	
	$("#selectByCodesAccept").click(function(){
		
		botonCodesPulsado = true;
		//Añadimos la imagen de carga en el contenedor
    	$('#y42bSearchDivContent').html('<img src="'+$.rup.APP_STATICS+'/images/loading.gif"/>');
    	$('#y42bSearchDivContent').css("display","inline");

		searchByCodes();
		closeSearchByCodesDialog();
	});
	
	$("#selectByCodesCancel").click(function(){
		closeSearchByCodesDialog();
	});
	
	$("#selectByCodesClean").click(function(){
		$("#selectByCodesSelectedCodeXML").val("");
	});
	
});

function searchByCodes(){
	var doc = form2object("selectByCodesForm", ".", false);
	$.ajax({
		type : 'POST',
		url : "/y42bVistaWar/search",
		dataType : "json",
		data : $.toJSON(doc),
		contentType : 'application/json',
		error : function(jqXHR, textStatus, errorThrown) {
			botonCodesPulsado = false;
			$('#y42bSearchDivContent').css("display","none");
			
			$("#rupFeedback").rup_feedback("option", "delay", null);
			$("#rupFeedback").rup_feedback("set", textStatus + ": error", "error");
		},
		success : function(data, textStatus, jqXHR) {
			// parent page js
			loadGridResults(data.envioId, true);
			
			botonCodesPulsado = false;
			$('#y42bSearchDivContent').css("display","none");
		}
	});
}

function closeSearchByCodesDialog(){
	//parent jsp element
	$("#searchCodeSelectionDialog").dialog("close");
}
