function fncOpen(str, title, w, h)
{
	oOpen = window.open(str,'','scrollbars=yes,resizable=yes,width=' + w +',height=' + h +',status=no,location=no,toolbar=no');
}

function fncMensagem()
{
	var oAssunto = document.frmpost.txtAssunto;
	var oMensagem = document.frmpost.txtMensagem;
	
	if(oAssunto.value == "")
	{
		please("o assunto da mensagem!");
		oAssunto.focus();
		return false;
	}
	
	if(oMensagem.value == "")
	{
		please("a mensagem!");
		oAssunto.focus();
		return false;
	}
	
	document.frmpost.submit();
}

function fncCadPlano()
{
	var oPlano = document.frmplano.rdoPlano;
	var tam = oPlano.length;
	var post = false;
	
	for(i=0; i<tam;i++)
	{
		if(oPlano[i].checked == true)
		{
			post = true;	
			break;
		}
		
	}

	if(post){
		document.frmplano.submit();
	}else{
		please("o plano que gostaria de contratar!");
		return false;
	}
	
}

function fncAvancada()
{
	var oNome = document.frmavancada.txtNome;
	var oAutor = document.frmavancada.txtAutor;
	var oEditora = document.frmavancada.txtEditora;
	var oGenero = document.frmavancada.cmbGenero;
	
	if((oNome.value == "") && (oAutor.value == "") && (oEditora.value == "") && (oGenero.selectedIndex == 0))
	{
		please("algum campo da busca!");	
		return false;
	}
	
	 document.frmavancada.submit();
}

function fncEsqueceu()
{
	var oEmail = document.frmesqueceu.txtEmail;
	
	if(oEmail.value == "")
	{
		please('o e-mail corretamente!');
		oEmail.focus();
		return false;
	}
	else if(ValidateMail(oEmail.value) == false)
	{
		please('o e-mail corretamente!');
		oEmail.focus();
		return false;
	}
	
	document.frmesqueceu.submit();
}

function fncBusca()
{
	var oBusca = document.frmbusca.txtBusca;
	var oOpcao = document.frmbusca.cmbOpcao;
	
	if(oOpcao.selectedIndex == 0)
	{
		please("a opcao da busca!");
		oOpcao.focus();
		return false;
	}
	
	if(oBusca.value == "")
	{
		please("o que gostaria de buscar!");
		oBusca.focus();
		return false;
	}
	
	document.frmbusca.submit();
}

function confirmMessage(msg)
{
	var blnResposta=confirm(msg);
	
    return blnResposta;
}

function validaValue()
{
	var oStatus = document.frmlivro.cmbStatus;
	var oLinha = document.getElementById('1');
	var oObs = document.frmlivro.txtObservacao;
	
	if(oStatus.value == 2)
	{
		oLinha.style.display = 'block';	
		oObs.focus();
	}
}

function fncCadLivro()
{
	var oTitulo = document.frmlivro.txtTitulo;
	var oAutor = document.frmlivro.txtAutor;
	var oEditora = document.frmlivro.txtEditora;
	var oSinopse = document.frmlivro.txtSinopse;
	var oGenero = document.frmlivro.cmbGenero;
	var oStatus = document.frmlivro.cmbStatus;
	var oEdicao = document.frmlivro.txtEdicao;
	var oPagina = document.frmlivro.txtPagina;
	
	if(oTitulo.value == "")
	{
		please("o titulo do livro");	
		oTitulo.focus();
		return false;
	}
	
	
	if(oAutor.value=="")
	{
		please("o autor do livro");	
		oAutor.focus();
		return false;
	}

	if(oEditora.value=="")
	{
		please("a editora do livro");	
		oEditora.focus();
		return false;
	}

	if(oSinopse.value=="")
	{
		please("a sinopse do livro");	
		oSinopse.focus();
		return false;
	}

	if(oGenero.selectedIndex == 0)
	{
		please("o genero do livro");	
		oGenero.focus();
		return false;
	}

	if(oStatus.selectedIndex == 0)
	{
		please("o stauts do livro");	
		oStatus.focus();
		return false;
	}

	if(oEdicao.value=="")
	{
		please("o ano de edicao do livro");	
		oEdicao.focus();
		return false;
	}
	
	if(oPagina.value=="")
	{
		please("o numero de paginas do livro");	
		oPagina.focus();
		return false;
	}
		
	document.frmlivro.submit();
}

function fncLogin()
{
	var oLogin = document.frmlogin.txtLogin;
	var oSenha = document.frmlogin.txtSenha;
	
	if(oLogin.value=="")
	{
		please("o login corretamente!");
		oLogin.focus();
		return false;
	}
	
	if(oSenha.value=="")
	{
		please("a senha corretamente!");
		oSenha.focus();
		return false;
	}

//	document.frmlogin.location = 'login.php';
	document.frmlogin.submit();
}

function fncCadUser()
{
	var oNome = document.frmuser.txtNome;
	var oEmail = document.frmuser.txtEmail;
	var oEndereco = document.frmuser.txtEndereco;
	var oNumero = document.frmuser.txtNumero;
	var oBairro = document.frmuser.txtBairro;
	var oPrefCep = document.frmuser.txtPrefCep;
	var oSufCep = document.frmuser.txtSufCep;
	var oCidade = document.frmuser.txtCidade;
	var oEstado = document.frmuser.txtUf;
	var oDDD = document.frmuser.txtDDD;
	var oTelefone = document.frmuser.txtTelefone;
	var oCpf = document.frmuser.txtCpf;
	var oNascimento = document.frmuser.txtNascimento;
	var oLogin = document.frmuser.txtLogin;
	var oSenha = document.frmuser.txtSenha;
	var oConfSenha = document.frmuser.txtConfSenha;
	var oCadastro = document.frmuser.cadastro;
	var oAceite = document.frmuser.chkTermo;
	
	if(oNome.value == "")
	{
		please('o nome completo!');
		oNome.focus();
		return false;
	}
	
	if(oEmail.value == "")
	{
		please('o e-mail corretamente!');
		oEmail.focus();
		return false;
	}
	else if(ValidateMail(oEmail.value) == false)
	{
		please('o e-mail corretamente!');
		oEmail.focus();
		return false;
	}
	
	if(oEndereco.value == "")
	{
		please('o endereço corretamente!');
		oEndereco.focus();
		return false;
	}
	
	if(oNumero.value == "")
	{
		please('o numero corretamente!');
		oNumero.focus();
		return false;
	}
	
	if(oBairro.value == "")
	{
		please('o bairro corretamente!');
		oBairro.focus();
		return false;
	}
	
	if(oPrefCep.value == "")
	{
		please('o cep corretamente!');
		oPrefCep.focus();
		return false;
	}
	
	if(oSufCep.value == "")
	{
		please('o cep corretamente!');
		oSufCep.focus();
		return false;
	}

	if(oCidade.value == "")
	{
		please('a cidade corretamente!');
		oCidade.focus();
		return false;
	}
	
	if(oEstado.value == "")
	{
		please('o estado corretamente!');
		oEstado.focus();
		return false;
	}
	
	if(oDDD.value == "")
	{
		please('o ddd corretamente!');
		oDDD.focus();
		return false;
	}
	
	if(oTelefone.value == "")
	{
		please('o telefone corretamente!');
		oTelefone.focus();
		return false;
	}
	
	if(oCpf.value == "")
	{
		please('o cpf corretamente!');
		oCpf.focus();
		return false;
	}
	
	if(!isCPFCNPJ(oCpf.value,1))
	{
		please('o cpf corretamente!');
		oCpf.focus();
		return false;
	}
	
	if(oNascimento.value == "")
	{
		please('a data de nascimento corretamente!');
		oNascimento.focus();
		return false;
	}
	
	if(oLogin.value == "")
	{
		please('o login corretamente!');
		oLogin.focus();
		return false;
	}
	
	if(oSenha.value == "")
	{
		please('o senha corretamente!');
		oSenha.focus();
		return false;
	}
	
	if(oConfSenha.value == "")
	{
		please('confirmar a senha!');
		oConfSenha.focus();
		return false;
	}
	
	if(oSenha.value != oConfSenha.value)
	{
		please('a senha corretemante');
		oSenha.value = '';
		oConfSenha.value = '';
		oSenha.focus();
		return false;
	}
	
	if(oAceite.checked != true)
	{
		please("leia e aceite o termo de uso!");
		return false;
	}

	oCadastro.value = 1;
	document.frmuser.location = 'cadastrar.php';
	document.frmuser.submit();
}

function please(str)
{
	alert('Favor informar ' + str);
}

function isFieldBlank(theField)
{
	inStr = theField.value;
	inLen = inStr.length;
	
	for(var i = 0; i < inLen; i++)
	{
		var ch = inStr.substring(i, i+1)
		
		if (ch != " ")
		{
			return (false);   
		}
	}
	
	theField.Value = "";
	return (true);
}

// funcao de mascaras de campos (inicio)
function Mascara(formato, keypress, objeto)
{
	campo = eval (objeto);
// mascara de login
	if (formato == 'Acesso')
	{
		if(39==window.event.keyCode)
		{
			event.returnValue = false;
		}
		if(34==window.event.keyCode)
		{
			event.returnValue = false;
		}
		if(13==window.event.keyCode)
		{
			event.returnValue = false;
		}
	}
// mascara de caracter numerico
	if (formato == 'Numero')
	{
		caracteres = '01234567890';
	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < caracteres)
		{}
		else
			event.returnValue = false;
	}
// mascara de cpf
	if (formato == 'CPF')
	{
		caracteres = '01234567890';
		conjuntos = 4;
		conjunto1 = 3;
		conjunto2 = 4;
		conjunto3 = 4;
		conjunto4 = 3;
	
		if ((caracteres.search(String.fromCharCode (keypress))!=-1) && 
													campo.value.length < (conjunto1 + conjunto2 + conjunto3 + conjunto4))
		{
			if (campo.value.length == conjunto1)
			{
				campo.value = campo.value + ".";
			}
			if (campo.value.length == conjunto1 + conjunto2)
			{
				campo.value = campo.value + ".";
			}
			if (campo.value.length == conjunto1 + conjunto2 + conjunto3)
			{
				campo.value = campo.value + "-";
			}
		}
		else{ 
			event.returnValue = false;
		}	
	}
// mascara de data
	if (formato == 'Data')
	{
		caracteres = '01234567890';
		conjuntos = 3;
		conjunto1 = 2;
		conjunto2 = 3;
		conjunto3 = 5;
	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < 
		(conjunto1 + conjunto2 + conjunto3))
		{
			if (campo.value.length == conjunto1)
			{
				campo.value = campo.value + "/";
			}
			if (campo.value.length == conjunto1 + conjunto2)
			{
				campo.value = campo.value + "/";
			}
		}
		else 
			event.returnValue = false;
	}
	
// mascara de cep
	if (formato == 'CEP')
	{
		caracteres = '01234567890';
		conjuntos = 2;
		conjunto1 = 5;
		conjunto2 = 4;
	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < 
		(conjunto1 + conjunto2))
		{
			if (campo.value.length == conjunto1)
			{
				campo.value = campo.value + "-";
			}
		}
		else 
			event.returnValue = false;
	}
}
// funcao de mascaras de campos (fim)

// validando campo e-mail
function ValidateMail(sValue)
{
	var sPat = /^(([a-z]|[A-Z])([a-z]|[A-Z]|[0-9]|.|_)+)@(([a-z]|[A-Z])([a-z]|[A-Z]|[0-9]|_)+)\.(([a-z]|[A-Z]){1,3})(\.([a-z]|[A-Z]){1,3})?$/;
	return sPat.test(sValue);	
}

// validando campo data
function ValidateData(sValue)
{
	var sPat = /^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
	return sPat.test(sValue);
}

// Limitador de caracteres no campo
function BlocCaracteres(Param, Objeto)
{
	if(document.getElementById(Objeto).value.length >= Param)
	{
		if (window.event.keyCode == 8)
		{
			event.returnValue = true;	
		}
		else if (window.event.keyCode == 16)
		{
			event.returnValue = true;	
		}
		else if (window.event.keyCode == 17)
		{
			event.returnValue = true;	
		}
		else if (window.event.keyCode == 35)
		{
			event.returnValue = true;	
		}
		else if (window.event.keyCode == 36)
		{
			event.returnValue = true;	
		}
		else if (window.event.keyCode == 37)
		{
			event.returnValue = true;	
		}
		else if (window.event.keyCode == 38)
		{
			event.returnValue = true;	
		}
		else if (window.event.keyCode == 39)
		{
			event.returnValue = true;	
		}
		else if (window.event.keyCode == 40)
		{
			event.returnValue = true;	
		}
		else if (window.event.keyCode == 46)
		{
			event.returnValue = true;	
		}
		else
		{
			event.returnValue = false;	
		}
	}
}

function replaceSubstring(inputString, fromString, toString)
{
	// Goes through the inputString and replaces every occurrence of fromString with toString
	var temp = inputString;
	if (fromString == "")
	{
		return inputString;
	}
	
	if (toString.indexOf(fromString) == -1)
	{
		// If the string being replaced is not a part of the replacement string (normal situation)
		while (temp.indexOf(fromString) != -1)
		{
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	}
	else
	{
		// String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
		var midStrings = new Array("~", "`", "_", "^", "#");
		var midStringLen = 1;
		var midString = "";
		// Find a string that doesn't exist in the inputString to be used
		// as an "inbetween" string
		while (midString == "")
		{
			for (var i=0; i < midStrings.length; i++)
			{
				var tempMidString = "";
				for (var j=0; j < midStringLen; j++)
				{
					tempMidString += midStrings[i];
				}
				
				if (fromString.indexOf(tempMidString) == -1)
				{
					midString = tempMidString;
					i = midStrings.length + 1;
				}
			}
		}
		// Keep on going until we build an "inbetween" string that doesn't exist
		// Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
		while (temp.indexOf(fromString) != -1)
		{
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			temp = toTheLeft + midString + toTheRight;
		}
		// Next, replace the "inbetween" string with the "toString"
		while (temp.indexOf(midString) != -1)
		{
			var toTheLeft = temp.substring(0, temp.indexOf(midString));
			var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	}
	// Ends the check to see if the string being replaced is part of the replacement string or not
	return temp; // Send the updated string back to the user
}

function fncSetFocus(getCampo, setCampo, tam)
{
	if(parseInt(tam) == (getCampo.value.length))
	{
		setCampo.focus();
	}
}

function isCPFCNPJ(campo,pType)
{
	var campo_filtrado = "", valor_1 = " ", valor_2 = " ", ch = "";
	var valido = false;
	
	for (i = 0; i < campo.length; i++)
	{
		ch = campo.substring(i, i + 1);
		if (ch >= "0" && ch <= "9")
		{
			campo_filtrado = campo_filtrado.toString() + ch.toString()
			valor_1 = valor_2;
			valor_2 = ch;
		}
		if ((valor_1 != " ") && (!valido))
		{
			valido = !(valor_1 == valor_2);
		}
	}
	if (!valido)
	{
		campo_filtrado = "12345678912";
	}
	if (campo_filtrado.length < 11)
	{
		for (i = 1; i <= (11 - campo_filtrado.length); i++)
		{
			campo_filtrado = "0" + campo_filtrado;
		}
	}
	if(pType == 1 || pType == "1")
	{
		if ((campo_filtrado.substring(9,11) == fncValidaCPF(campo_filtrado.substring(0,9))) && (campo_filtrado.substring(11,12)==""))
		{
			return true;
		}
   }
   return false;
}

function fncValidaCPF(sCPF)
{
	var mControle = "";
	var mContIni = 2;
	var mContFim = 10;
	var mDigito = 0;
	
	for(j = 1 ; j <= 2 ; j++)
	{
		mSoma = 0;
		for (i = mContIni ; i <= mContFim ; i++)
		{
			mSoma = mSoma + (sCPF.substring((i-j-1),(i-j)) * (mContFim + 1 + j - i));
		}
		if (j == 2 )
		{
			mSoma = mSoma + ( 2 * mDigito );
		}
		mDigito = ( mSoma * 10 ) % 11;
		if (mDigito == 10)
		{
			mDigito = 0;
		}
		mControle1 = mControle;
		mControle = mDigito;
		mContIni = 3;
		mContFim = 11;
   }
   return ((mControle1 * 10) + mControle);
}

function fncMostra(campo)
{
	if(campo.style.display=="block" || campo.style.display=="")
	{
		campo.style.display="none";
	}
	else
	{
		campo.style.display="block";
	}
}

function fncEscondeMostra(esconde, mostra)
{
	esconde.style.display="none";
	mostra.style.display="block";
}

function goUrl(sUrl)
{
	document.location.href = sUrl;
}