// função para facilitar a vida.. heeh
function gE(ID){
	return document.getElementById(ID);
}

//função para mostrar algum elemento com displau none
function mostraElemento(param){
	var p = gE(param);
	
	if (p.style.display == 'none'){
		p.style.display = 'block';	
	}else{
		p.style.display = 'none';		
	}
	
}
function checka(valor)
{
 var flagTodos = document.getElementById("flagtodos");
 flagTodos.value = valor;
}
/*
function validaUnidade()
{ 
var flagUnidade = false;
var unidade = document.getElementById("unidade");
var pte = document.getElementById("pte");
var cam = document.getElementById("cam");
var bsb = document.getElementById("bsb");
var cax = document.getElementById("cax");
var auxFlag = false;

for(i=0;i<document.formulario.elements.length;i++){
if(document.formulario.elements[i].type == "radio"){
if(document.formulario.elements[i].checked == true)
{ flagUnidade=true;auxFlag=true;}
else
{ flagUnidade = false;}
}
}
if(auxFlag == false )
{
  pte.style.color = 'red';
  cam.style.color = 'red';
  bsb.style.color = 'red'; 
  cax.style.color = 'red'; 
  alert("Selecione uma unidade antes");
  return(false);
}
else
{
  pte.style.color = '#666666';
  cam.style.color = '#666666';
  bsb.style.color = '#666666'; 
  cax.style.color = '#666666'; 
  return(true);
}
}
*/
//atribui os campos com value padrao para vazio
function ValidaTelefones(){

var flagResidencial = false;
var flagComercial = false;
var flagCelular = false;
var residencial = document.getElementById("inp_FoneResidencial");
var comercial = document.getElementById("inp_FoneComercial");
var celular = document.getElementById("inp_FoneCel");

var flagDDDres = false;
var flagDDDcom = false;
var flagDDDcel = false;
var dddres = document.getElementById("inp_dddResidencial");
var dddcom = document.getElementById("inp_dddComercial");
var dddcel = document.getElementById("inp_dddCel");


function valida_par_r()
{
	if(residencial.value == "Residencial:")
	{
		if (dddres.value == "DDD:")
		{
			 residencial.style.border = 'solid 1px #8F111F';
			 dddres.style.border=  'solid 1px #8F111F';
			 flagDDDres = false;
			 flagResidencial = false;
		}else
		{
			 residencial.style.border = 'solid 1px #8F111F';
			  flagDDDres = true;
			  dddres.style.border = 'solid 1px #ede0d1';
		}
		
		//residencial.focus();
	}
	else
	{ 
		if (dddres.value == "DDD:")
		{
			 dddres.style.border=  'solid 1px #8F111F';
			 flagDDDres = false;
		}else
		{
			 dddres.style.border = 'solid 1px #ede0d1';
			 flagDDDres = true;
		}
	  flagResidencial = true;
	  residencial.style.border = 'solid 1px #ede0d1';
	}
	
	if (flagResidencial == true && flagDDDres== true)
	{
		return(true)	;
	}else
	{
		return (false)	;
	}
	
}

function valida_par_com ()
{

	if(comercial.value == "Comercial:")
	{
		
		if (dddcom.value == "DDD:")
		{
			 comercial.style.border = 'solid 1px #8F111F';
			 dddcom.style.border=  'solid 1px #8F111F';
			 flagDDDcom = false;
			 flagComercial = false;
		}else
		{
			 dddcom.style.border = 'solid 1px #ede0d1';
			 comercial.style.border = 'solid 1px #8F111F';
			  flagDDDcom = true;
		}
	  
	  
	
	}
	else
	{ 
		if (dddcom.value == "DDD:")
		{
			 dddcom.style.border=  'solid 1px #8F111F';
			 flagComercial = true;
		}else
		{
			 dddcom.style.border = 'solid 1px #ede0d1';
			 flagDDDcom = true;
		}
	
		
		
	 comercial.style.border = 'solid 1px #ede0d1';
	  flagComercial = true;
	
	}
	
	if(flagComercial == true && flagDDDcom == true)
	{
		return(true);	
	}else{
		return(false);	
	}
	

}


function valida_par_cel ()
{

	if(celular.value == "Celular:")
	{
		
		if (dddcel.value == "DDD:")
		{
			 celular.style.border = 'solid 1px #8F111F';
			 dddcel.style.border=  'solid 1px #8F111F';
			 flagDDDcel = false;
			 flagCelular = false;
		}else
		{
			 dddcel.style.border = 'solid 1px #ede0d1';
			 celular.style.border = 'solid 1px #8F111F';
			  flagDDDcel = true;
		}
	  
	  
	
	}
	else
	{ 
		if (dddcel.value == "DDD:")
		{
			 dddcel.style.border=  'solid 1px #8F111F';
			 flagCelular = true;
		}else
		{
			 dddcel.style.border = 'solid 1px #ede0d1';
			 flagDDDcel = true;
		}
	
		
		
	   celular.style.border = 'solid 1px #ede0d1';
	  flagCelular = true;
	
	}
	
	if(flagCelular == true && flagDDDcel == true)
	{
		return(true);	
	}else{
		return(false);	
	}
	

}

confere = valida_par_r();

if (confere)
{
	confere2 = valida_par_com();
	
	if (confere2)
	{
			confere3 = valida_par_cel();	
	}else
	{
		
		confere3 = false;	
	}
}else 
{
	confere3 = false;
	
}

if(confere3)
{ 
//residencial.style.border = 'solid 1px #ede0d1';
// comercial.style.border = 'solid 1px #ede0d1';
// celular.style.border = 'solid 1px #ede0d1'; 
return(true);
}
else
{ return(false); }

}

var Validacao = {
	ValidaItens: function(){
		if (ValidaTelefones() == true){
			var args = Validacao.ValidaItens.arguments; // coloca os parametros em uma variavel no qual se tornará um Array
			if (args.length > 0){ // verifica se há parametros atribuidos a função
				for (var x = 0; x < args.length; x++){
					var vItem = gE(args[x]);
					if (vItem.value == "" || vItem.value == null){
						vItem.focus();
						vItem.style.border = 'solid 1px #8F111F';
						alert("Campo em branco");
						return(false);
					}else{
						vItem.style.border = '';
					}				
					//verifica se há algum campo de e-mail para chamar o validaEmail
					var vCampo = args[x].toLowerCase();		
					if (vCampo.indexOf('email') > 0){
						if (Validacao.ValidaEmail(vItem) == false)
							return(false);
					}
					//verifica se selecionou pelo menos uma unidade
					if(Validacao.ValidaUnidades()==false)
					{return(false);}					
				} // fim do for	
				return(true);
			}else{
				alert('Ocorreu um erro ao realizar operação');
				return(false);			
			}// else (args.length > 0){
		}else{
			alert('1 - Preencha pelo menos um telefone\n2 - Preencha sempre o DDD do telefone\n3 - Preencha sempre o telefone do DDD');
			return(false);
		}
	}, // fim do ValidaItens
	
	ValidaEmail: function(pCampo){
		var email = pCampo.value;
		var resp = email.search(/(\w[\w\.\+]+)@(.+)\.(\w+)$/)==0;
		
		if (resp == false){
			pCampo.focus();
			pCampo.style.border = 'solid 1px red';
			alert('E-mail inválido');
			return(false);
		}else{
			return(true);
		}
	},//fim do ValidaEmail
	ValidaUnidades: function()
	{
		var flagUnidade = false;
		var unidade = document.getElementById("unidade");
		var pte = document.getElementById("pte");
		var cam = document.getElementById("cam");
		var bsb = document.getElementById("bsb");
		var cax = document.getElementById("cax");
		var auxFlag = false;

		for(i=0;i<document.formulario.elements.length;i++){
		if(document.formulario.elements[i].type == "radio"){
		if(document.formulario.elements[i].checked == true)
		{ flagUnidade=true;auxFlag=true;}
		else
		{ flagUnidade = false;}
		}
		}
		if(auxFlag == false )
		{
		  pte.style.color = 'red';
		  cam.style.color = 'red';
		  bsb.style.color = 'red'; 
		  cax.style.color = 'red'; 
		  alert("Selecione uma unidade antes");
		  return(false);
		}
		else
		{
		  pte.style.color = '#666666';
		  cam.style.color = '#666666';
		  bsb.style.color = '#666666'; 
		  cax.style.color = '#666666'; 
		  return(true);
		}
	}//fim do ValidaUnidades
};

var Validacao2 = {
	ValidaItens2: function(){
		var args = Validacao2.ValidaItens2.arguments; // coloca os parametros em uma variavel no qual se tornará um Array
		if (args.length > 0){ // verifica se há parametros atribuidos a função
			for (var x = 0; x < args.length; x++){
				var vItem = gE(args[x]);
				if (vItem.value == "" || vItem.value == null){
					vItem.focus();
					vItem.style.border = 'solid 1px #8F111F';
					alert("Campo em branco");
					return(false);
				}else{
					vItem.style.border = '';
				}				
				//verifica se há algum campo de e-mail para chamar o validaEmail
				var vCampo = args[x].toLowerCase();		
				if (vCampo.indexOf('email') > 0){
					if (Validacao2.ValidaEmail2(vItem) == false)
						return(false);
				}				
			} // fim do for			
			return(true);
		}else{
			alert('Ocorreu um erro ao realizar operação');
			return(false);			
		}// else (args.length > 0){
	}, // fim do ValidaItens
	
	ValidaEmail2: function(pCampo){
		var email = pCampo.value;
		var resp = email.search(/(\w[\w\.\+]+)@(.+)\.(\w+)$/)==0;
		
		if (resp == false){
			pCampo.focus();
			pCampo.style.border = 'solid 1px red';
			alert('E-mail inválido');
			return(false);
		}else{
			return(true);
		}
	}
};
