// JavaScript Document
function CreateErroDialog()
{
		if( $("#erroDialog").attr("id") == undefined)
		{
			//Criando o div pra receber o DIALOG
			$(document.body).append($.createElement("div",{attr:{id:"erroDialog"}}));
			//Criando o div do conteudo do DIALOG
			$("#erroDialog").append($.createElement("div",{attr:{id:"erroDialogContent"}, css:{width:"90%", height:"90%", padding:"3px", background:"transparent", "text-align":"justify"} }));
			
			//Butões
			var butoes = {"Fechar": function(){$("#erroDialog").dialog("close");}/*, "Enviar Erro":function() {alert("Enviar erro para o suporte");}*/}			
			//Aplicando a formatação ao DIALOG
			$("#erroDialog").dialog({title:"ERRO - "+Application.TITULO, autoOpen:false, modal:true, resizable:false, draggable:true, bgiframe:true, width:"800px", height:"500", buttons:butoes });
		}
		
		window.DialogErro = {
			
			Show:function(conteudo){ShowDialogErro(conteudo);},
			Close:function(){CloseDialogErro();},
			isErro:function(date){isErro(date);},
			content:$("#erroDialogContent"),
			Erro:$("#erroDialogContent").html(),
			getAjaxErro:function(b){if(b){$("#erroDialog").ajaxError(function(event, request, settings){
					var content = "<h2>Settings</h2>";
					for(var i in settings){					
						content += "<h3>"+i+"</h3><p>"+settings[i]+"</p>"
					}
					content += "<h2>Request</h2>";
					content += "<h3>ReadyState</h3><p>"+request.readyState+"</p>";
					content += "<h3>Status</h3><p>"+request.Status+"</p>";
					content += "<h3>ResponseText</h3><p>"+request.responseText+"</p>";
					content += "<h3>ResponseXML</h3><p>"+request.responseXML+"</p>";
					/*for(var i in request){
						try{
						content += "<h3>"+i+"</h3><p>"+request[i]+"</p>"
						}catch(e){
							//Faz nada						
						}
					}*/
					
					ShowDialogErro(content);				
				;});}}
		
		}
		
		
}

function isErro(objResp)
{	

	
	if(objResp.ERRO != undefined){
		
		if(Application.DEBUGMODE){
			ShowDialogErro();
			DialogErroContent(objResp);
			retorno = true;	
		}else{			
			ShowDialogErro("<h3>Ocorreu um erro inesperado no sistema</h3><p>Uma mensagem contendo o erro ocorrido já foi enviada ao Suporte Técnico. <br/>Caso o problema persista entre em contato conosco (71) 2103-5200.</p>", {width:"380px", height:"200", resizable:false, buttons:{}});
			retorno = true;	
		}
		
		
	}else{
		retorno = false;	
	}
	
	return retorno;	
}

function ShowDialogErro(conteudo, params)
{		
		if(params == undefined ){
			$("#erroDialog").dialog("option" , "width" , "800px");
			$("#erroDialog").dialog("option" , "height" , "500");
		}else{
			$("#erroDialog").dialog("option" , "width" , params.width);
			$("#erroDialog").dialog("option" , "height" , params.height);	
			$("#erroDialog").dialog("option" , "resizable" , params.resizable);	
			$("#erroDialog").dialog("option" , "buttons" , params.buttons);	
		}
		
		if(conteudo != undefined){
			//Limpa o conteudo da janela de erro
			$("#erroDialogContent").html("");
			$("#erroDialogContent").html(conteudo);
		}
		
		$("#erroDialog").dialog("open");
		$("#erroDialog").dialog("moveToTop");
}

function CloseDialogErro()
{
	$("#erroDialog").dialog("close");
}

function DialogErroContent(objResp)
{
		$("#erroDialogContent").html("");
		for(var i in objResp)
		{	
			//$("#erroDialog").html(($("#erroDialog").html())+"<h3>"+i+":</h3>"+objResp[i]+"<br><br>");		
			$("#erroDialogContent").html(($("#erroDialogContent").html())+"<h3>"+i+":</h3>"+objResp[i]+"<br><br>");			
		}
	
}

$(document).ready(function(){
	CreateErroDialog();
	DialogErro.getAjaxErro(true);
});