//VERIFICA SE CAMPO É VAZIO
function isEmpty(str) 
{ 
	if (str==null) return true
	for (var intLoop = 0; intLoop < str.length; intLoop++)
		if (" " != str.charAt(intLoop))
			return false;            
	return true; 
}

function getIntegerDate(value)
{
			aux = value.split("/");
			return aux[2] + aux[1] + aux[0];
}

function comparaCampo(tipo,value1,name1,value2,name2)
{
	switch(tipo)
	{
		case "equals":
			if (value1 != value2)
			{
				return "Os campos " + name1 + " e " + name2 + " foram não conferem.\n";
			}
			else
			{
				return "";
			}
			break;
		case "greater":
			if (value1 <= value2)
			{
				return "O campo " + name1 + " é menor do que o campo " + name2 + "\n";
			}
			else
			{
				return "";
			}
			break;
	}
}

function validaCampo(tipo,campo,mensagem)
{
	var erro;
	var aux;
	erro = false;
	switch(tipo)
	{
		case "date":
			//QUEBRAR O CAMPO DATA
			aux = campo.split("/");
			dia = aux[0];
			mes = aux[1];
			ano = aux[2];
			if (!validaData(dia,mes,ano)) erro = true; 
			break;
		case "string":
			if (isEmpty(campo)) { erro = true; }
			break;
		case "email":
			if (!validaEmail(campo)) { erro = true; }
			break;
		case "cpf":
			if (!validaCPF(campo)) { erro = true; }
			break;
		case "phone":
			if (!validaTelefone(campo))	{ erro = true; }
			break;
		case "ddd":
			if (!validaDDD(campo))	{ erro = true; }
			break;
		case "cep":
			if (!validaCEP(campo))	{ erro = true; }
			break;
		case "radio":
			if (!validaRadio(campo)) { erro = true; }
			break;
		case "cep":
			if (!validaTelefoneCEP(campo))	{ erro = true; }
		case "float":
			if (!isFloat(campo)) { erro = true;	}
		case "combo":
			if (!validaCombo(campo)) { erro = true;	}


	}
	if (erro == true)
	{
		return "O campo " + mensagem + " é invalido.\n";
	}
	else
	{
		return "";
	}
}


function validaCombo(field)
{
	if (field.options[field.selectedIndex].value!='')
	{
		return true;
	}
	return false;
}
//VALIDA DATA	
function validaData(d,m,a)
 {
	 if  (((isEmpty(d)) || (isEmpty(m)) || (isEmpty(a))) && ((!isEmpty(d)) && (!isEmpty(m)) && (!isEmpty(a)))) return false; 
	 if ( (isNaN(d) && d != '') || (isNaN(m) && m != '') || (isNaN(a) && a != '') ) return false
	 if ( (m<1 || m >12) && (m != '') ) return false
	 if ( (d<1 || d >31) && (d != '') ) return false
	 if ( (a<1900 || a>2078) && (a !='') ) return false
	 if (d == 31)
	 if( (m == 2) || (m == 4) || (m == 6) || (m == 9) || (m == 11)) return false
	 if (m ==2)
	 if( (parseInt(a)%4 != 0 && d ==29) || (d == 30) )return false
	 return true
 }

//FUNCAO PARA VALIDAR EMAIL
function validaEmail(str) 
{ 
	if (str==null) { return false; }
	if (str=="") { return false ; }
	s = str.indexOf(" ")
	if (s != -1) { return false; } // não pode conter espaços
	s = str.indexOf("@")
	if (s == -1) { return false; }// tem que ter 1 arroba
	s = str.indexOf(".")
	if (s == -1) { return false; } // tem que ter pelo menos 1 ponto
	if (str.length < 7) { return false; }  // tem que ter pelo menos tamanho 5    
	return true; 
}

//FUNCAO PARA VALIDAR CPF
function validaCPF(valor) 
{
    if (isEmpty(valor))
      return true
	if ((isNaN(valor)) && (valor.length != 11))
      return false
	Mult1 = 10   
	Mult2 = 11
	dig1=0
	dig2=0
	valor= valor.toString()
	for(var i=0;i<=8;i++)
	{
	    ind=valor.charAt(i)
		dig1 += ((parseFloat(ind))* Mult1)
		Mult1--
	}
	for(var i=0;i<=9;i++)
	{
	    ind=valor.charAt(i)
		dig2 += ((parseFloat(ind))* Mult2)
		Mult2--
	}
	dig1 = (dig1 * 10) % 11   
	dig2 = (dig2 * 10) % 11   
	if (dig1 == 10)
      dig1 = 0
	if(dig2 == 10)
      dig2 = 0
	if (parseFloat(valor.charAt(9)) != dig1)
		return false   
	if (parseFloat(valor.charAt(10)) != dig2)
		return false   
	return true  
}

function validaTelefone(valor)
{
  if (valor == '')
  {
	  return false
  }
  else
	{ 
//	  var filter=/^([0-9]{0,4}-?[0-9]{3,8})*$/
	  var filter=/^([0-9]{5,8})*$/
      if ((filter.test(valor))&&(valor!=''))
		 {
			  return true;
		 }
		 else
		 {
			 return false;
		 }
	}

}

function validaDDD(valor)
{
	var filter=/^([0-9]{2,3})*$/;
    if ((filter.test(valor))&&(valor!=''))
	 {
		  return true;
	 }
	 else
     {
	     return false;
	 }

}

function validaCEP(valor)
{
	var filter=/^([0-9]{5}-?[0-9]{0,3})*$/;
    if ((filter.test(valor))&&(valor!=''))
	 {
		  return true;
	 }
	 else
     {
	     return false;
	 }
}


function validaRadio(radioObj)
{
	 alert('vou retornar o valor');
	 for(var i = 0; i < radioObj.length; i++){
     if(radioObj[i].checked)
     {
		 return true; 
     }
 }
 return false;
}

function retornaRadio(radioObj)
{
	alert('vou retornar o valor');
	 for(var i = 0; i < radioObj.length; i++)
     {
		alert('bbb');
		 if(radioObj[i].checked)
		 {
			 return radioObj[i].value;
		 }
	 }
	 return;
}

var janela_exemplo
function popeditar_exemplo()
{
    
	// 410 x 260
    if ( janela_exemplo == null || janela_exemplo.closed ) {
        var alturaJanela;
        var larguraJanela = 794;
        var scrollJanela = 'yes';
        if ( screen == null || screen.height == 0 )
            alturaJanela = 400;
        else {
            alturaJanela = screen.height - 102;
            if ( alturaJanela > 650 ) {
                scrollJanela = 'no';
                larguraJanela = larguraJanela - 16;
                alturaJanela = 665;
            }
        }
    	janela_exemplo = window.open('imgedit/edit_exemplo.asp','edit_exemplo','width=' + larguraJanela + ',height=' + alturaJanela + ',scrollbars=' + scrollJanela);
    }
    else
        window.alert('Já existe uma janela de edição aberta.');
}

function tpNav() {
  var tipo;
  if (document.all)         { tipo = "IE"; }
  else if (document.layers) { tipo = "NN"; }
  else                      { tipo = "MO"; }

  return tipo;
}

function layerMostra(id) {
  var tipo = tpNav();
  if (tipo=="IE") { document.all[id].style.display='block'; }
  else if (tipo=="NN") { document.layers[id].display='block'; }
  else if (tipo=="MO") 
    { document.getElementById(id).style.display='block'; }
}

function layerEsconde(id) {
  var tipo = tpNav();
  if (tipo=="IE") { document.all[id].style.display='none'; }
  else if (tipo=="NN") { document.layers[id].display='none'; }
  else if (tipo=="MO") 
    { document.getElementById(id).style.display='none'; }
}
