/*-------------------------------------------------
Gerenciamento de Conteúdo
Desenvolvido por Vivian Ferreira e Danilo Artimos

Data: Janeiro/2010
1a. Revisão
--------------------------------------------------*/

//************************* administração *****************************//
  
//index
function valida_entrada() {
	$usuario = document.frm_valida.AUSLOGIN.value;
	$senha   = document.frm_valida.AUSSENHA.value;
	if ($usuario == "") {
		alert("Informe seu Usuário!");
		document.frm_valida.AUSLOGIN.focus(); 
		return false;
	}

	if ($senha == "") {
		alert("Informe sua Senha!");
		document.frm_valida.AUSSENHA.focus();
		return false;
	}		  
}

//excluir registro
function Confirma(form) {
	if (confirm("Deseja realmente EXCLUIR este registro?")) {
		form.submit();
	} else {
		return false;
	}
}

/* ----- grp-produto.php ----- */

//valida inclusão de produto
function fnValidaIncProduto() {
	if ( document.frmIncProduto.PRONOME.value == "" ){
		alert("O NOME do PRODUTO não pode ser vazio!");
		document.frmIncProduto.PRONOME.focus();
		return false;
	}
	if ( document.frmIncProduto.PROTIPODEVENDA.value == 0 ){
		alert("Selecione o tipo de venda!");
		return false;
	}

	if ( document.frmIncProduto.PROTIPODEVENDA.value == 1 && document.frmIncProduto.PROPESO.value == "" ){
		alert("O PESO do PRODUTO não pode ser vazio!");
		document.frmIncProduto.PROPESO.focus();
		return false;
	}

	if ( document.frmIncProduto.PROTIPODEVENDA.value == 1 && document.frmIncProduto.PROPRECO.value == "" ){
		alert("O PREÇO do PRODUTO não pode ser vazio!");
		document.frmIncProduto.PROPRECO.focus();
		return false;
	}
	if ( document.frmIncProduto.PROTIPODEVENDA.value == 1 && document.frmIncProduto.PROPRECOFINAL.value == "" ){
		alert("O PREÇO FINAL do PRODUTO não pode ser vazio!");
		document.frmIncProduto.PROPRECOFINAL.focus();
		return false;
	}

	if (isNaN(frmIncProduto.PROESTOQUE.value)){
		alert("O ESTOQUE do PRODUTO deve ser apenas números!");
		document.frmIncProduto.PROESTOQUE.focus();
		return false;
	}

	if ( document.frmIncProduto.PROTIPODEVENDA.value == 1 && document.frmIncProduto.PROESTOQUE.value == "" ){
		alert("O ESTOQUE do PRODUTO não pode ser vazio!");
		document.frmIncProduto.PROESTOQUE.focus();
		return false;
	}
	for (i=0;i<document.frmIncProduto.PROPROMOCAO.length;i++){
		if (document.frmIncProduto.PROPROMOCAO[i].checked)
		break;
	}
	
	if (document.frmIncProduto.PROPROMOCAO[i].value == 1) {
		if (document.getElementById("PRODATAPROMOINI").value == "" ){
			alert("A DATA INICIAL da promoção não pode ser vazio!");
			document.getElementById('PRODATAPROMOINI').focus();
			return false;
		}
		if (document.getElementById("PRODATAPROMOFIM").value == "" ){
			alert("A DATA INICIAL da promoção não pode ser vazio!");
			document.getElementById('PRODATAPROMOINI').focus();
			return false;
		}
		var dataIni = document.getElementById("PRODATAPROMOINI").value;
		var dataFim = document.getElementById("PRODATAPROMOFIM").value;
	
		var nova_dataIni = parseInt(dataIni.split("/")[2].toString() + dataIni.split("/")[1].toString() + dataIni.split("/")[0].toString());
		var nova_dataFim = parseInt(dataFim.split("/")[2].toString() + dataFim.split("/")[1].toString() + dataFim.split("/")[0].toString()); 
		
		if ( nova_dataFim < nova_dataIni ) {
			alert("A DATA FINAL não pode menor que a DATA INICIAL!");
			return false;
		}

	}
	return true;
}

//atualiza o preço final do produto com desconto
function fnAtualizaIncProValor() {
	document.frmIncProduto.PROPRECOFINAL.value = document.frmIncProduto.PROPRECO.value - document.frmIncProduto.PRODESCONTO.value
}

//produto_movimento
function valida_estoque(){
	$txt_quantidade = document.frm_estoque.txt_quantidade.value;
	if (isNaN(frm_estoque.txt_quantidade.value)){
		alert ("O campo deve conter apenas numeros!");
		frm_estoque.txt_quantidade.focus();
		return false;
	}		
		
	if ($txt_quantidade == ""){
		alert("Coloque quantidade !");
		frm_estoque.txt_quantidade.focus();
		return false;
	}
	return true;
}

//newsletter_envio
function validaformEnviaNews(){
	if (document.formEnviaNews.NCOCODIGO.value == 0){
		alert("Por favor escolher uma Newsletter válida!")
		return false;
	}
	return true;
}

//usuario_altera_email
function validarEmail() {
	$email = document.frmAlteraEmail.txtEmail.value; 
	if ($email == "") {
		alert("Email não pode ser vazio!");
		frmAlteraEmail.txtEmail.focus(); 
		return false;
	}
	return true;
}

//************************* site *****************************//

/* ----- login.php ----- */
//valida login
function valLogin() {
	if ( document.frmLogon.txtEmail.value == "" ){
		alert("O campo E-MAIL não pode ser vazio!");
		document.frmLogon.txtEmail.focus();
		return false;
	}
	if (document.frmLogon.txtEmail.value.search("@") == -1 || document.frmLogon.txtEmail.value.search("[.*]") == -1 ){
		alert("Não é um endereço de E-MAIL válido.");
		document.frmLogon.txtEmail.focus();
		return false;
	}
	var i;
	for ( i=0;i<document.frmLogon.sAcao.length;i++ ){
		if ( document.frmLogon.sAcao[i].checked )
		break;
	}
	if ( document.frmLogon.sAcao[i].value == 'cadastrar' ) {
		if ( document.frmLogon.txtCEP.value == "" ){
			alert("O campo CEP não pode ser vazio!");
			document.frmLogon.txtCEP.focus();
			return false;
		}
	}
	if ( document.frmLogon.sAcao[i].value == 'logar') {
		if ( document.frmLogon.txtSenha.value == "" ){
			alert("O campo SENHA não pode ser vazio!");
			document.frmLogon.txtSenha.focus();
			return false;
		}
	}
	return true;
}

/* ----- meuCadastro.php ----- */
//função que copia endereço de cobrança para a entrega
function funCopiaEndereco() {
	var copiaEnd = document.frmCadastro.copiaEndereco.value;
	if ( copiaEnd == 1 ) {
		document.frmCadastro.USUENTCEP.value	= document.frmCadastro.USUCOBCEP.value;
		document.frmCadastro.USUENTRUA.value	= document.frmCadastro.USUCOBRUA.value;
		document.frmCadastro.USUENTNUMERO.value = document.frmCadastro.USUCOBNUMERO.value;
		document.frmCadastro.USUENTCOMPL.value	= document.frmCadastro.USUCOBCOMPL.value;
		document.frmCadastro.USUENTBAIRRO.value = document.frmCadastro.USUCOBBAIRRO.value;
		document.frmCadastro.USUENTCIDADE.value = document.frmCadastro.USUCOBCIDADE.value;
		document.frmCadastro.USUENTESTADO.value = document.frmCadastro.USUCOBESTADO.value;
		document.frmCadastro.USUENTPAIS.value	= document.frmCadastro.USUCOBPAIS.value;
	}
}

//valida Cadastro
function valCadastro() {
	if ( document.frmCadastro.USUNOME.value == "" ){
		alert("O campo NOME / RAZÃO SOCIAL não pode ser vazio!");
		document.frmCadastro.USUNOME.focus();
		return false;
	}
	if ( document.frmCadastro.USUSOBRENOME.value == "" ){
		alert("O campo SOBRENOME / NOME FANTASIA não pode ser vazio!");
		document.frmCadastro.USUSOBRENOME.focus();
		return false;
	}
	if ( document.frmCadastro.USUCPFCNPJ.value == "" ){
		alert("O campo CFP / CNPJ não pode ser vazio!");
		document.frmCadastro.USUCPFCNPJ.focus();
		return false;
	}
	if ( document.frmCadastro.txtSenha.value == "" ){
		alert("O campo SENHA não pode ser vazio!");
		document.frmCadastro.txtSenha.focus();
		return false;
	}
	if ( document.frmCadastro.txtSenhaRepi.value == "" ){
		alert("O campo REPITA A SENHA não pode ser vazio!");
		document.frmCadastro.txtSenhaRepi.focus();
		return false;
	}
	if ( document.frmCadastro.txtSenhaRepi.value != document.frmCadastro.txtSenha.value ){
		alert("As SENHAS digitadas NÃO coincide!");
		document.frmCadastro.txtSenhaRepi.focus();
		return false;
	}
	var i;
	tipoPessoa = false;
	for ( i=0;i<document.frmCadastro.USUTIPOPESSOA.length;i++ ){
       if ( document.frmCadastro.USUTIPOPESSOA[i].checked ) {
	   	  tipoPessoa = true;
          break;
	   }
    }

	if ( tipoPessoa == false ) {
		alert( "Por favor escolha PESSOA FÍSICA ou JURÍDICA!" );
		return false;
	}	
	if ( document.frmCadastro.USUTELEFONE.value == "" ){
		alert("O campo TELEFONE não pode ser vazio!");
		document.frmCadastro.USUTELEFONE.focus();
		return false;
	}
	//endereco cobrança
	if ( document.frmCadastro.USUCOBCEP.value == "" ){
		alert("O campo CEP não pode ser vazio!");
		document.frmCadastro.USUCOBCEP.focus();
		return false;
	}
	if ( document.frmCadastro.USUCOBRUA.value == "" ){
		alert("O campo ENDEREÇO não pode ser vazio!");
		document.frmCadastro.USUCOBRUA.focus();
		return false;
	}
	if ( document.frmCadastro.USUCOBNUMERO.value == "" ){
		alert("O campo NÚMERO não pode ser vazio!");
		document.frmCadastro.USUCOBNUMERO.focus();
		return false;
	}
	if ( document.frmCadastro.USUCOBBAIRRO.value == "" ){
		alert("O campo BAIRRO não pode ser vazio!");
		document.frmCadastro.USUCOBBAIRRO.focus();
		return false;
	}
	if ( document.frmCadastro.USUCOBCIDADE.value == "" ){
		alert("O campo CIDADE não pode ser vazio!");
		document.frmCadastro.USUCOBCIDADE.focus();
		return false;
	}
	if ( document.frmCadastro.USUCOBESTADO.value == 0 ){
		alert("O campo ESTADO não pode ser vazio!");
		document.frmCadastro.USUCOBCIDADE.focus();
		return false;
	}
	//endereço entrega
	if ( document.frmCadastro.USUENTCEP.value == "" ){
		alert("O campo CEP DE ENTREGA não pode ser vazio!");
		document.frmCadastro.USUENTCEP.focus();
		return false;
	}
	if ( document.frmCadastro.USUENTRUA.value == "" ){
		alert("O campo ENDEREÇO DE ENTREGA não pode ser vazio!");
		document.frmCadastro.USUENTRUA.focus();
		return false;
	}
	if ( document.frmCadastro.USUENTNUMERO.value == "" ){
		alert("O campo NÚMERO DE ENTREGA não pode ser vazio!");
		document.frmCadastro.USUENTNUMERO.focus();
		return false;
	}
	if ( document.frmCadastro.USUENTBAIRRO.value == "" ){
		alert("O campo BAIRRO DE ENTREGA não pode ser vazio!");
		document.frmCadastro.USUENTBAIRRO.focus();
		return false;
	}
	if ( document.frmCadastro.USUENTCIDADE.value == "" ){
		alert("O campo CIDADE DE ENTREGA não pode ser vazio!");
		document.frmCadastro.USUENTCIDADE.focus();
		return false;
	}
	if ( document.frmCadastro.USUENTESTADO.value == 0 ){
		alert("O campo ESTADO DE ENTREGA não pode ser vazio!");
		document.frmCadastro.USUENTESTADO.focus();
		return false;
	}
	return true;
}

/* ----- alterarEmail.php ----- */
function valAlteraEmail() {
	if ( document.frmEmail.txtEmailAtual.value == "" ){
		alert("O campo E-MAIL ATUAL não pode ser vazio!");
		document.frmEmail.txtEmailAtual.focus();
		return false;
	}
	if (document.frmEmail.txtEmailAtual.value.search("@") == -1 || document.frmEmail.txtEmailAtual.value.search("[.*]") == -1 ){
		alert("Não é um endereço de E-MAIL válido.");
		document.frmEmail.txtEmailAtual.focus();
		return false;
	}
	if ( document.frmEmail.txtEmailNovo.value == "" ){
		alert("O campo NOVO E-MAIL não pode ser vazio!");
		document.frmEmail.txtEmailNovo.focus();
		return false;
	}
	if (document.frmEmail.txtEmailNovo.value.search("@") == -1 || document.frmEmail.txtEmailNovo.value.search("[.*]") == -1 ){
		alert("Não é um endereço de E-MAIL válido.");
		document.frmEmail.txtEmailNovo.focus();
		return false;
	}
	if ( document.frmEmail.txtSenha.value == "" ){
		alert("O campo SENHA não pode ser vazio!");
		document.frmEmail.txtSenha.focus();
		return false;
	}
	return true;
}

/* ----- alterarSenha.php ----- */
//valida a alteração de senha
function valAlteraSenha() {
	if ( document.frmSenha.txtSenha.value == "" ){
		alert("O campo SENHA não pode ser vazio!");
		document.frmSenha.txtSenha.focus();
		return false;
	}
	if ( document.frmSenha.txtNovaSenha.value == "" ){
		alert("O campo REPITA A SENHA não pode ser vazio!");
		document.frmSenha.txtNovaSenha.focus();
		return false;
	}
	if ( document.frmSenha.txtConfNovaSenha.value != document.frmSenha.txtNovaSenha.value ){
		alert("As SENHAS digitadas NÃO coincide!");
		document.frmSenha.txtConfNovaSenha.focus();
		return false;
	}
	return true;
}

/* ----- meusPedidos.php ----- */
//valida o check do tipo do pedido
function valPedido() {
	var i;
	for ( i=0;i<document.frmPedido.rbnPedido.length;i++ ){
		if ( document.frmPedido.rbnPedido[i].checked )
		break;
	}
	if ( document.frmPedido.rbnPedido[i].value == 'numero' ) {
		if ( document.frmPedido.txtPedido.value == "" ){
			alert("O campo NÚMERO DO PEDIDO não pode ser vazio!");
			document.frmPedido.txtPedido.focus();
			return false;
		}
	}
	return true;
}

/* ----- sac.php ----- */
// valida SAC
function validaSac() {
	if (document.getElementById("txtFaleNome").value == "") {
		alert("O campo NOME não pode ser vazio!");
		document.getElementById("txtFaleNome").focus();
		return false;
	}
	if (document.getElementById("txtFaleEmail").value == "") {
		alert("O campo E-MAIL não pode ser vazio!");
		document.getElementById("txtFaleEmail").focus();
		return false;
	}
	if (document.getElementById("txtFaleEmail").value.search("@") == -1 || document.getElementById("txtFaleEmail").value.search("[.*]") == -1 ){
		alert("Não é um endereço de E-MAIL válido!");
		document.getElementById("txtFaleEmail").focus();
		return false;
	}
	if (document.getElementById("txtFaleTel").value == ""){
		alert("O campo TELEFONE não pode ser vazio!");
		document.getElementById("txtFaleTel").focus();
		return false;
	}
	if (document.getElementById("txtFaleAssunto").value == ""){
		alert("O campo ASSUNTO não pode ser vazio!");
		document.getElementById("txtFaleAssunto").focus();
		return false;
	}
	if (document.getElementById("txtFaleMensagem").value == ""){
		alert("O campo MENSAGEM não pode ser vazio!");
		document.getElementById("txtFaleMensagem").focus();
		return false;
	}
	return true;
}
/* ----- carrinho.php ----- */
// valida frete
function valFrete() {
	if (document.getElementById("codCorreio").value == 0) {
		alert("Selecione a FORMA DE ENVIO!");
		return false;
	}
	if (document.getElementById("cepIni").value == "") {
		alert("O campo CEP não pode ser vazio!");
		document.getElementById("cepIni").focus();
		return false;
	}
	if (document.getElementById("cepFim").value == "") {
		alert("O campo CEP não pode ser vazio!");
		document.getElementById("cepFim").focus();
		return false;
	}
	return true;
}


//************************* geral *****************************//

//adiciona mascara ao em campo numerico
//Exemplo onkeypress="return formatCampo(NomeFormulario, 'NomeCampo', 'TipoFormato', event)"
function formatCampo(objForm, strField, sMask, evtKeyPress) {
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

	if(document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode;
	} else if(document.layers) { // Nestcape
		nTecla = evtKeyPress.which;
	} else {
		nTecla = evtKeyPress.which;
		if (nTecla == 8) {
			return true;
		}
	}

	sValue = objForm[strField].value;
	// Limpa todos os caracteres de formatação que já estiverem no campo.
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( ":", "" );
	fldLen = sValue.length;
	mskLen = sMask.length;

	i = 0;
	nCount = 0;
	sCod = "";
	mskLen = fldLen;

	while (i <= mskLen) {
		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
		bolMask = bolMask || (sMask.charAt(i) == ":")

		if (bolMask) {
			sCod += sMask.charAt(i);
			mskLen++;
		} else {
			sCod += sValue.charAt(nCount);
			nCount++;
		}

		i++;
	}

	objForm[strField].value = sCod;
	if (nTecla != 8) { // backspace
		if (sMask.charAt(i-1) == "9") { // apenas números...
			return ((nTecla > 47) && (nTecla < 58)); // números de 0 a 9
		} else { // qualquer caracter...
			return true;
		}
	} else {
		return true;
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

/* Antigo */





// onKeyPress=formataData(this);

function formataData( input ) {

	if ( ( input.value.length == 2 ) || ( input.value.length == 5 ) ) {

	   input.value = input.value + "/";

	}

}

// onKeyPress=formataHora(this);

function formataHora( input ) {

	if ( input.value.length == 2 ) {

	   input.value = input.value + ":";

	}

}

// onKeyPress=formataCPF(this);

function formataCPF( input ) {

	if ( ( input.value.length == 3 ) || ( input.value.length == 7 ) ) {

	    input.value=input.value + "." ;

	} else {

		if ( input.value.length == 11 ) {

			input.value = input.value + "-";

		}

	}

}

// onKeyPress=formataIP(this);

function formataIP( input ) {

	if ( ( input.value.length == 3 ) || ( input.value.length == 7 ) || ( input.value.length == 9 ) ) {

	    input.value=input.value + "." ;

	}

}



// funções para exibir/ocultar ou alternar tabelas

function blendoff(idname){

  document.getElementById(idname).style.display = 'none';

}

function blendon(idname){

  document.getElementById(idname).style.display = 'block';

}

function swichtabs(wert){

  if (wert=='1'){  

    document.getElementById('tablink1').className='tab1 tabactive';

    document.getElementById('tablink2').className='tab2';     

  }else if (wert=='2'){

    document.getElementById('tablink1').className='tab1';

    document.getElementById('tablink2').className='tab2 tabactive';

  }

}

// função de esconder a data de nascimento

function showHide() {

	var obj  = document.getElementById('selEmpresa').value;

	var obj2 = document.getElementById('funDtNasc');



	if(obj != "01") {

		obj2.style.display = "none";

  	} else {

		obj2.style.display = "";

	}

}
