/**************************
Desmarca Checkbox e RadioButton
***************************/
function anulaCheck(campo) {
	if(typeof(campo) == 'object') {
		num = campo.length;	
		if(typeof(num) == 'undefined') {
			num = 1;
		}
		if (num == 1) {
			campo.checked = false;
		} else {
			for (i = 0; i < num; i++) {
				campo[i].checked = false;
			}
		}
	}
}

/**************************
Abre nova Janela. Recebe o endere?o da pagina, altura e largura
***************************/
function abrirJanela(end,w,h) {
	loc=end;
	prop='left=20,top=20,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=yes,resizable=1,width='+w+',height='+h+';'
	janela=window.open(loc,"win",prop).focus()
}

/**************************
S? deixa digitar n?mero em campo de texto. Inserir no onkeypress="return soNumero(event);"
***************************/
function soNumero(event) {
	var isNS4 = (navigator.appName=="Netscape")?1:0;
	var tecla = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if((tecla < 48 || tecla > 57) && (tecla != 8 && tecla != 9 && tecla != 13)) {
			return false;
	}
	return true;
}

/**************************
S? deixa digitar n?mero e virgula em campo de texto. Inserir no onkeypress="return soNumVir(event);" 
***************************/
function soNumVir(event) {
	var isNS4 = (navigator.appName=="Netscape")?1:0;
	var tecla = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

	if((tecla < 48 || tecla > 57) && (tecla != 8 && tecla != 9 && tecla != 13 && tecla != 44)) {
			return false;
	}
	return true;
}
/**************************
S? deixa digitar n?mero, virgula e ponto em campo de texto. Inserir no onkeypress="return soNumVirPonto(event);" 
***************************/
function soNumVirPonto(event) {
	var isNS4 = (navigator.appName=="Netscape")?1:0;
	var tecla = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

	if((tecla < 48 || tecla > 57) && (tecla != 8 && tecla != 9 && tecla != 13 && tecla != 44 && tecla != 46)) {
			return false;
	}
	return true;
}



/**************************

***************************/
function marcaCheckbox(campo,valor) {
	if(valor.length > 1) {
		for(var j=0;j<campo.length;++j) {
			var cont = false;
			for(var i=1;i<valor.length;++i) {
				if(campo[j].value == valor[i]) {
					cont = true;
					break;
				}
			}
			campo[j].checked=cont;
		}
	}
}


/**************************
Fun??o que insere ponto e virgula em valores num?ricos
***************************/
function PutCents (numero) {
				var i=0;
        var y=0;
        var numero2;
        numero=numero.toString();
        numero=numero.replace(/[.]/g,",");
        while ((numero.charAt(i) != ",") && (i < numero.length)) { i++; }
        if ((numero.length-i) > 2) { numero=numero.substr(0,i+3); }
        else
        {
                switch (numero.length - i)
                {
                        case 0: numero+=",00"; break;
                        case 1: numero+="00"; break;
                        case 2: numero+="0"; break;
                }
        }
        qtdinteiros=numero.length - 4;
        numero2 = numero.substr(numero.length - 3,3);
        for (var i=qtdinteiros; (i != -1); i--)
            {
            y++;
            if ((y == 3) && (i > 0)) { numero2 = "." + numero.charAt(i) + numero2;y=0;}
            else {numero2 = numero.charAt(i) + numero2;}
            }
        return (numero2);
}
/**************************
Verifica se ? n?mero
***************************/
function ChkNumber(campo) {
	var numero, digitos="0123456789,.", Ok=true, virgula=false;
    if ((campo.value == "") || (campo.value == "0") || (campo.value == "0,") || (campo.value == "0,0") || (campo.value == "0,00") || (campo.value == "00,00")) {
    	Ok=false;
	}else{
		numero=campo.value;
		numero=numero.toString();
		numero=numero.replace(/[,]/g,"");
		if (numero.length > 12) {
			Ok=false;
		}else{
			if ((digitos.indexOf(numero.charAt(0)) < 0) || (digitos.indexOf(numero.charAt(0)) > 9)) {
				Ok=false;
			}else{
				for (var i=1; ((i < numero.length) && (Ok)); i++) {
					if (digitos.indexOf(numero.charAt(i)) < 0) {
						Ok=false;
					}
					
					if (digitos.indexOf(numero.charAt(i)) == 10) {
						if (((numero.length-i) <= 3) && (!virgula)) {
							virgula=true;
						}else{
							Ok=false;
						}
					}
				}
			}
		}
        if (Ok) {
       		campo.value=PutCents(numero);
        }else{
			alert("Valor digitado não é válido: " + numero);
			campo.value="";
       	}
	}
  	return Ok;
}
/**************************
Inserir no onchange="return Chkvalor(this)"
***************************/
function Chkvalor(campo){	
	if(campo.value != "") {
		campo.value=campo.value.replace(/[.]/g,"");
		campo.value=campo.value.replace(/[,]/g,".");
		return ChkNumber(campo);
	}
}



/**************************
Cria imagem "validadora" do form.
para validar precisa verificar o valor do campo com uma variavel "d"
***************************/
function randomString() {
	var nums = "123456789";
	var lets = "abcdefghijklmnpqrstuvwxyz";
	var string_length = 5;
	var randomstring = '';
	var contr = Math.floor(Math.random() * 2);
		
	for (var i=0; i<string_length; i++) {
		if (contr==0) {
			rnum = Math.floor(Math.random() * nums.length);
			d += nums.substring(rnum,rnum+1).toUpperCase();
			randomstring = nums.substring(rnum,rnum+1);
			contr=1;
			document.write("<img src=/visual/images/"+randomstring+".gif border=0>");
		} else if (contr==1) {
			rnum = Math.floor(Math.random() * lets.length);
			d += lets.substring(rnum,rnum+1).toUpperCase();
			randomstring = lets.substring(rnum,rnum+1);
			contr=0;
			document.write("<img src=/visual/images/"+randomstring+".gif border=0>");
		}
	}
}


/**************************
Fazer autotab entre campos
***************************/
function autoTab(original,destination){
	if (original.getAttribute&&original.value.length==original.getAttribute("maxlength")){
		destination.focus()
	}
}

/**************************
Valida se a data está correta. Nao valida formatacao
***************************/
function validaData(data) {
	var reDate = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
	var dtArray = data.split("/");
	var dia = dtArray[0];
	var mes = dtArray[1];
	var ano = dtArray[2];
	if(!reDate.test(data)) {
		return false;
	} else if((mes == 02) && (dia == 29)) {
		// Verifica se ano ? bissexto
		if( !(ano % 4 == 0 && (ano % 100 != 0 && ano % 400 != 0)) ){
			// N?o ? bissexto
			return false;
		}
	}
	return true;
}
function validaData2(dia, mes, ano) {
	var exprDate = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
	var data = dia + "/" + mes + "/" + ano;
	if(!exprDate.test(data)) {
		return false;
	} else if((mes == 02) && (dia == 29)) {
		// Verifica se ano ? bissexto
		if( !(ano % 4 == 0 && (ano % 100 != 0 && ano % 400 != 0)) ){
			// N?o ? bissexto
			return false;
		}
	}
	return true;
}






/******* Antigos Scripts deixados por causa dos antigos projetos ******/
var x=0;
var y=0;

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function mousePos(i){
    x=window.event.x;
    y=window.event.y+document.body.scrollTop;
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) {
  	if ((obj=MM_findObj(args[i]))!=null) { 
  		v=args[i+2];
  		if (obj.style) { 
  			obj=obj.style;
  			v=(v=='show')?'visible':(v='hide')?'hidden':v; 
  		}
    obj.visibility=v;
    obj.left=x;
    obj.top=y;
   	}
  }
}
function ShowHide(parTab)
{
	eval("document.all." + parTab + ".style.display = 'inline';");
}
function Hide(parTab){
	eval("document.all." + parTab + ".style.display = 'none';");
}

function mostrarPlano(lay) {
	var plano="plano"+lay;
	mousePos(plano);
	//alert("X = "+x+"\nY= "+y);
	MM_showHideLayers(plano,'','show');
}       

function esconderPlano(lay) {
	var plano="plano"+lay;
	MM_showHideLayers(plano,'','hide');
}


//Script de LOADING PAGE
function loadImages() {
if (document.getElementById) {  // DOM3 = IE5, NS6
document.getElementById('hidepage').style.visibility = 'hidden';
}
else {
if (document.layers) {  // Netscape 4
document.hidepage.visibility = 'hidden';
}
else {  // IE 4
document.all.hidepage.style.visibility = 'hidden';
      }
   }
}
//FIM Script de LOADING PAGE





//--------------------------------------------------------------------
 // Variaveis e Expressoes regulares globais
 //--------------------------------------------------------------------
 var reInteger      = /^\d+$/;
 var reWhitespace   = /^\s+$/;
 var reAlphabetic   = /^[a-zA-Z]+$/;
 var reAlphanumeric = /^[a-zA-Z0-9]+$/;
 var reDigit        = /^\d/;
 d 				=  "";
 var v_message      =  "";
 var v_focus        =  "";

 //--------------------------------------------------------------------
 // Checa se uma string e vazia
 //--------------------------------------------------------------------
 function isEmpty(param)
 {
   return ((param == null) || (param.length == 0));
 }

 //--------------------------------------------------------------------
 // Checa se uma string e vazia ou contem apenas espacos
 //--------------------------------------------------------------------
 function isWhitespace(param)
 {
   return (isEmpty(param) || reWhitespace.test(param));
 }

//---------------------------------------------------------------------------
// checaAcentos: Verifica se tem caracteres especiais
//        false: tem acentos     true: nao tem acentos
//---------------------------------------------------------------------------
function checaAcentos(txtField)
{
  var checkOK = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ-.@_";
  var checkStr = txtField.toUpperCase();
  var allValid = true;
  for (i = 0;  i < txtField.length;  i++)
    {
      ch = checkStr.charAt(i);
      for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
      break;
      if (j == checkOK.length)
         {
           allValid = false;
           break;
         }
    }

  if (!allValid)
    {
      return (false);
    }
  return (true);
}

//-------------------------------------------------------------------
// Checa campo endereco de e-mail
//-------------------------------------------------------------------
function checaEmail(p_email)
{
  erroFlag = false;

  if (!isWhitespace(p_email.value))
     {
       if (p_email.value.indexOf("@") + "" != "-1" &&
           p_email.value.indexOf(".") + "" != "-1")
          {
            if (!checaAcentos(p_email.value) )
               {
                 v_message = v_message + "Informe o E-MAIL sem acentua??o!\n";
                 erroFlag  = true;
               }
          }
       else
          {
            v_message = v_message + "E-MAIL incorreto!\n";
            erroFlag = true;
          }
     }
  else
     {
       v_message = v_message + "Informe o E-MAIL!\n";
       erroFlag = true;
     }

  if (erroFlag == true)
     {
       if ( v_focus = "" )
          {
            p_email.focus();
            return false;
          }
       else
          { return false; }
     }

  return true;
}

function checkRadios() {
 var el = document.forms[0].elements;
 for(var i = 0 ; i < el.length ; ++i) {
  if(el[i].type == "radio") {
   var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
   var itemchecked = false;
   for(var j = 0 ; j < radiogroup.length ; ++j) {
    if(radiogroup[j].checked) {
	 itemchecked = true;
	 break;
	}
   }
   if(!itemchecked) {
    if(el[i].focus)
     el[i].focus();
	return false;
   }
  }
 }
 return true;
}

function Abrir(end,w,h) {
	loc=''+ end + '.html';
	prop='left=20,top=20,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=yes,resizable=1,width='+w+',height='+h+';'
	janela=window.open(loc,"win",prop).focus()
}
