var blnCloseWindow = true;

function openError(page){
	var height = "233";
	var width = "414";
	var top = (screen.height-height)/2;
	var left = (screen.width-width)/2;
	var parms = "top="+ top +",left="+ left +",width="+ width +",height=" + height + ",toolbar=0,resizable=0,location=0,directories=0,status=0,menubar=0,scrollbars=0";
	var w = window.open("about:blank","wError",parms);
	
	w.location.replace("/Admin/Erro.aspx?page=" + page);
	w.focus();
}

function closeSystem(logado){
	if(confirm("Deseja realmente sair do sistema?")){
		blnCloseWindow = false;
		if(logado){
			top.location.replace("/Admin/LogOff.aspx");
		}else{
			window.opener = null;
			window.close();
		}
	}
}

function closeSystemOnUnload(){
	if(blnCloseWindow){
		var w = window.open("/Admin/LogOff.aspx", "logOff", "menu=no,toolbars=no,address=no,resizable=no")
		w.moveTo(1024,768);
		w.resizeTo(50,50);
	}
}

function resizeWindow(page,windowName){
	var w = window.open(page, windowName, "menu=no,toolbars=no,address=no,resizable=no")
	w.moveTo(0,0);
	w.resizeTo(screen.availWidth, screen.availHeight);	
	
	if(window.name == windowName){
		window.opener=null;
		window.close();
	}		
}

function resize(windowName){
	if(window.name != windowName){
		var strFile = window.location.toString(); 
		strFile = strFile.substring(strFile.lastIndexOf("/")+1,strFile.length);
		var w = window.open("/Admin/Index.aspx?Pagina=" + strFile, windowName, "menu=no,toolbars=no,address=no,resizable=no,status=yes");
		w.moveTo(0,0);
		w.resizeTo(screen.availWidth, screen.availHeight);	
		
		window.opener=null;
		window.close();			
	}
}

function help(system, code){
	alert("Sistema: " + system + "\nTela: " + code);
}

function dwsChat(hey, company){
	alert("Abre Suporte");
}

function IsEmpty(Texto){
	var strRetorno = "";

	// Retira espaços do início
	for (var i = 0; i < Texto.length; i++){
		if (Texto.substr(i, 1) != " "){
			strRetorno = Texto.substr(i);
			break;
		}
	}

	// Retira espaços do fim
	for (var i = strRetorno.length - 1; i >= 0; i--){
		if (strRetorno.substr(i, 1) != " "){
			strRetorno = strRetorno.substr(0, i+1);
			break;
		}
	}
	
	if(strRetorno == ""){
		return(true);
	}else{
		return(false);
	}
}

function styleField(field){
	if(field == "[object]"){
		field.className = "form-erro";
	}else{
		for(i = 0; i < document.all.tags("INPUT").length; i++){
			if(document.all.tags("INPUT")[i].type == "text"){
				document.all.tags("INPUT")[i].className = "form"
			}
		}
		
		for(i = 0; i < document.all.tags("SELECT").length; i++){
			if(document.all.tags("SELECT")[i].type == "select-one"){
				document.all.tags("SELECT")[i].className = "form"
			}
		}		
	}
}

function styleMenuAuxiliar(obj, e){
	if(obj != "[object]") return;
	
	if(e.type == "mouseover"){
		obj.className = "menu-auxiliar-over";
	}else if(e.type == "mouseout"){
		obj.className = "menu-auxiliar-out";								
	}
}

function UploadImage(botaoIncluir,botaoExcluir, imagem, imagemCampo, pasta){
	var btIncluir = document.getElementById(botaoIncluir);
	var btExcluir = document.getElementById(botaoExcluir);
	var img = document.getElementById(imagem);
	var imgCampo = document.getElementById(imagemCampo);
	var url = "/Admin/UploadImage.aspx?pasta=" + pasta;
	var retorno = showModalDialog("/Admin/Modal.htm", url,"help:no;status:no;scroll:no;edge:raised;dialogWidth:500px;edge:raised;dialogHeight:400px");

	//oculta botão
	btExcluir.style.display = "none";

	if(!IsEmpty(retorno)){
		//exibe botão
		btExcluir.style.display = "";
		
		//oculta botão
		btIncluir.style.display = "none";				
		
		//exibe imagem
		img.src = retorno;
		
		//armazena imagem
		imgCampo.value = retorno;
	}			
}				

function IsEmail(valor){
	if (valor.indexOf("@") < 0)
	{
		return false;
	}
		
	if (valor.indexOf("@.") >= 0)
	{
		return false;
	}


	if (valor.indexOf("@") == 0)
	{
		return false;
	}

	if (valor.indexOf("@") == valor.length-1)
	{
		return false;
	}

	if (valor == "")
	{
		return false;
	}

	return true;
}

function formataData(){
	if(event.keyCode < 48 || event.keyCode > 57){
		event.keyCode = 0;
	}
	
	if(event.srcElement.value.length == 2 || event.srcElement.value.length == 5){
		event.srcElement.value += "/";
	}
}

function formataHora(){
	if(event.keyCode < 48 || event.keyCode > 57){
		event.keyCode = 0;
	}
	
	if(event.srcElement.value.length > 8){
		event.keyCode = 0;
	}
	
	if(event.srcElement.value.length == 2){
		event.srcElement.value += ":";
	}
}

function formataCNPJ(){
	if (event.keyCode < 48 || event.keyCode > 57){
		event.keyCode = 0;
	}
	
	if (event.srcElement.value.length == 2 || event.srcElement.value.length == 6){
		event.srcElement.value += ".";
	}else if (event.srcElement.value.length == 10){
		event.srcElement.value += "/";
	}else if (event.srcElement.value.length == 15){
		event.srcElement.value += "-";
	}
}

function formataCPF(){
	if (event.keyCode < 48 || event.keyCode > 57){
		event.keyCode = 0;
	}
	
	if (event.srcElement.value.length == 3 || event.srcElement.value.length == 7){
		event.srcElement.value += ".";
	}

	if (event.srcElement.value.length == 11){
		event.srcElement.value += "-";
	}
}

function formataTelefone(){
	if (event.keyCode < 48 || event.keyCode > 57){
		event.keyCode = 0;
	}
	
	if (event.srcElement.value.length == 0)	{
		event.srcElement.value += "(";
	}else if (event.srcElement.value.length == 3){
		event.srcElement.value += ") ";
	}else if (event.srcElement.value.length == 9){
		event.srcElement.value += "-";
	}
}

function formataCEP(){
	if (event.keyCode < 48 || event.keyCode > 57){
		event.keyCode = 0;
	}
	
	if (event.srcElement.value.length == 5){
		event.srcElement.value += "-";
	}
}

function IsCNPJ(campo)
{
	sValor1 = campo.value;
	sValor1 = sValor1.toString();
	sValor = '';
	xpqp = sValor1.length
	for (iputo = 0; iputo < xpqp; iputo++)
	{
		sAux = sValor1.charAt(iputo);
		if (sAux < '0' || sAux > '9')
		{
			if (sAux != '.' && sAux != '-' && sAux != '/')
			{
				return(false);
			}
		}
		else
			sValor = sValor + sAux
	}
	if (sValor != '')
	{
		if (isNaN(sValor) || sValor.length != 14)
		{
			return(false);
		}
		soma = (sValor.substr(0,1) * 5) + (sValor.substr(1,1) * 4) + (sValor.substr(2,1) * 3) + (sValor.substr(3,1) * 2) + (sValor.substr(4,1) * 9) + (sValor.substr(5,1) * 8) + (sValor.substr(6,1) * 7) + (sValor.substr(7,1) * 6) + (sValor.substr(8,1) * 5) + (sValor.substr(9,1) * 4) + (sValor.substr(10,1) * 3) + (sValor.substr(11,1) * 2)
		dv1 = (soma % 11);
		if (dv1 == 0 || dv1 == 1) 
			dv1 = 0;
		else
			dv1 = 11 - dv1;
		soma = (sValor.substr(0,1) * 6) + (sValor.substr(1,1) * 5) + (sValor.substr(2,1) * 4) + (sValor.substr(3,1) * 3) + (sValor.substr(4,1) * 2) + (sValor.substr(5,1) * 9) + (sValor.substr(6,1) * 8) + (sValor.substr(7,1) * 7) + (sValor.substr(8,1) * 6) + (sValor.substr(9,1) * 5) + (sValor.substr(10,1) * 4) + (sValor.substr(11,1) * 3)  + (dv1 * 2)
		dv2 = (soma % 11);
		if (dv2 == 0 || dv2 == 1) 
			dv2 = 0;
		else
			dv2 = 11 - dv2;
		if (sValor.substr(12,1) != dv1 || sValor.substr(13,1) != dv2)
		{
			return(false);
		}
	}
	return(true);
}

function quebrarLinha(texto){
	return(texto.replace("[quebra]","\n"));
}

function IsDate(Data){
	var strData = Data;
	
	if (strData.length != 10)
		return(false);
		
	var Dia = strData.substr(0, 2);
	var Mes = strData.substr(3, 2);
	var Ano = strData.substr(6, 4);
	
	if (isNaN(Dia) || isNaN(Mes) || isNaN(Ano))
		return(false);
		
	if (Dia < 1 || Dia > 31)
		return(false);
	
	if (Mes < 1 || Mes > 12)
		return(false);
		
	if (parseInt(Mes) == 2)	{
		if (Ano % 4 == 0){
			if (Dia > 29)
				return(false);
		}else{
			if (Dia > 28)
				return(false);
		}
	}else if (parseInt(Mes) == 4 || parseInt(Mes) == 6 || parseInt(Mes) == 9 || parseInt(Mes) == 11){
		if (Dia > 30)
			return(false);
	}
	
	if(parseInt(Ano) < 1900){
		return(false);
	}
	
	return(true);
}

function IsHora(valor){
	var hora = valor.substr(0, 2);
	var minuto = valor.substr(3, 2);

	if(IsEmpty(valor))
		return(false);

	if(valor.length < 5)
		return(false);
	
	if(parseInt(hora) > 23)
		return(false);
				
	if(parseInt(minuto) > 59)
		return(false);
	
	return(true);
}

function IsCPF(campo){
	sValor1 = campo.value;
	sValor1 = sValor1.toString();
	sValor = '';
	xpqp = sValor1.length
	for (iputo = 0; iputo < xpqp; iputo++){
		sAux = sValor1.charAt(iputo);
		if (sAux < '0' || sAux > '9'){
			if (sAux != '.' && sAux != '-' && sAux != '/'){
				return false;
			}
		}else{
			sAux = sAux.toString();
			sValor = sValor + '' + sAux;
		}
	}
	if (sValor != ''){
		if (isNaN(sValor) || sValor.length != 11){
			return false;
		}
		soma = (sValor.substr(0,1) * 10) + (sValor.substr(1,1) * 9) + (sValor.substr(2,1) * 8) + (sValor.substr(3,1) * 7) + (sValor.substr(4,1) * 6) + (sValor.substr(5,1) * 5) + (sValor.substr(6,1) * 4) + (sValor.substr(7,1) * 3) + (sValor.substr(8,1) * 2);
		dv1 = (soma % 11);
		if (dv1 == 0 || dv1 == 1) 
			dv1 = 0;
		else
			dv1 = 11 - dv1;
		soma = (sValor.substr(0,1) * 11) + (sValor.substr(1,1) * 10) + (sValor.substr(2,1) * 9) + (sValor.substr(3,1) * 8) + (sValor.substr(4,1) * 7) + (sValor.substr(5,1) * 6) + (sValor.substr(6,1) * 5) + (sValor.substr(7,1) * 4) + (sValor.substr(8,1) * 3) + (dv1 * 2);
		dv2 = (soma % 11);
		if (dv2 == 0 || dv2 == 1) 
			dv2 = 0;
		else
			dv2 = 11 - dv2;
		if (sValor.substr(9,1) != dv1 || sValor.substr(10,1) != dv2){
			return false;
		}
	}
	return true;
}

function BloquearEventos(){
	var intCodigo = event.keyCode;

	//Tecla Ctrl + N
	if ((intCodigo == 78) && (event.ctrlKey)){
		event.cancelBubble = true;
		event.returnValue = false;
		event.keyCode = false; 
		return false;
	}
	
	//Tecla Ctrl + O
	if ((intCodigo == 79) && (event.ctrlKey)){
		event.cancelBubble = true;
		event.returnValue = false;
		event.keyCode = false; 
		return false;
	}
	
	//Tecla Ctrl + P
	if ((intCodigo == 80) && (event.ctrlKey)){
		event.cancelBubble = true;
		event.returnValue = false;
		event.keyCode = false; 
		return false;
	}
	
	//Tecla Ctrl + F5
	if ((intCodigo == 116) && (event.ctrlKey)){
		event.cancelBubble = true;
		event.returnValue = false;
		event.keyCode = false; 
		return false;
	}
	
	//Tecla F5
	if (intCodigo == 116){
		event.cancelBubble = true;
		event.returnValue = false;
		event.keyCode = false; 
		return false;
	}
	
	//Tecla F11
	if (intCodigo == 122){
		event.cancelBubble = true;
		event.returnValue = false;
		event.keyCode = false; 
		return false;
	}

	//Tecla F3
	if (intCodigo == 114){
		event.cancelBubble = true;
		event.returnValue = false;
		event.keyCode = false; 
		return false;
	}
	
	//Tecla F1
	if (intCodigo == 112){
		event.cancelBubble = true;
		event.returnValue = false;
		event.keyCode = false; 
		return false;
	}
	
	//Tecla Ctrl + A
	if ((intCodigo == 65) && (event.ctrlKey)){
		event.cancelBubble = true;
		event.returnValue = false;
		event.keyCode = false; 
		return false;
	}
	
	//Tecla Ctrl + E
	if ((intCodigo == 69) && (event.ctrlKey)){
		event.cancelBubble = true;
		event.returnValue = false;
		event.keyCode = false; 
		return false;
	}
	
	//Tecla Ctrl + F
	if ((intCodigo == 70) && (event.ctrlKey)){
		event.cancelBubble = true;
		event.returnValue = false;
		event.keyCode = false; 
		return false;
	}

	//Tecla Ctrl + H
	if ((intCodigo == 72) && (event.ctrlKey)){
		event.cancelBubble = true;
		event.returnValue = false;
		event.keyCode = false; 
		return false;
	}	
	
	//Tecla Ctrl + I
	if ((intCodigo == 73) && (event.ctrlKey)){
		event.cancelBubble = true;
		event.returnValue = false;
		event.keyCode = false; 
		return false;
	}		
}
document.onkeydown = BloquearEventos;