function putPoint( value ) {
 var c = 0,
     r = "";

 for( var x = value.length - 1; x >= 0 ; x-- ) {
  c++;
  r = value.charAt(x) + r;
  if( c % 3 == 0 && x > 0 ) {
   r = "."+ r;
  }
 }
 return r;
}

var div_cont = 2;

function ShowDiv(){
	document.getElementById('corpo').innerHTML += "<div id=\"form"+div_cont+"><table><tr><td><b>Artigo: CAPA<b></td><tr><td align=\"left\"><b>Titulo do Artigo:</b></td></tr><tr><td><input type=\"text\" class=\"txt\" name=\"titulo\" size=\"75\"></td></tr><tr><td align=\"left\"><b>Resumo do Conteúdo:</b></td></tr><tr><td><input type=\"text\" class=\"txt\" name=\"resumo\" size=\"75\" maxlength=\"60\">*Máximo de 60 caracteres</td></tr><tr><td align=\"left\"><b>Artigo:</b></td></tr><tr><td><textarea cols=110 rows=15 name=\"txtarea\" ></textarea></td></tr></table></div>"
	div_cont++;
	}

var cont_element = 1;

function criaElem(pai,newfilho,newtype){
	var Pai = document.getElementById(pai);
	var fiho = document.createElement(newfilho);
	if(newtype || typeof new type == 'undefined'){
		filho.type = newtype;
	}
	filho.name = 'elem'+cont_element;
	cont_element++;
	Pai.appendChild(filho);
}

function valEmail( email, flag ) {

  var expReg = /^ *.+@.+(\..+)+ *$/i;
  
  var result = expReg.test( email.value );

  if ( ( email.value && result ) || ( email.value && flag ) ) {

    expReg = /^ *[_a-z0-9]+((\.|\-)?[_a-z0-9]+)*@[_a-z0-9]+(\-?[_a-z0-9]+)*(\.[_a-z0-9]+(\-?[_a-z0-9]+)?)* *$/i;
  
    result = expReg.test( email.value );  
    
    if ( !result ) {
    
      email.style.borderColor = email.style.color = '#9A1010';
      email.style.backgroundColor = '#FFE5E5';
    
    } else {
    
      email.style.backgroundColor = email.style.borderColor = email.style.color = '';    
    
    }
    
  } else {
  
    email.style.backgroundColor = email.style.borderColor = email.style.color = '';
  
  }

}

function verData() {

 var select = document.composeform.type;
 
 var data = document.composeform.data;
 
 if ( select.value == 'noticia' ) {
 
   var erro;
 
   if ( data.value ) {      
   
      var expReg = /^\d{2}\/\d{2}\/\d{4}$/;
 
      var result = expReg.test( data.value );
      
      if ( !result ) {
      
        erro = 'Formato de data inv&#65533;ido';
      
      }
    
    } else {
    
      erro = 'Uma p&#65533;ina do tipo noticia precisa ter uma data';     
    
    }
    
    if ( erro ) {
 
      alert( erro );
      
      data.select();
   
      return false;      
 
    }
    
 } 
 
 return true;
 
}

function hideData() {
 
  var divData = document.getElementById( 'data' );
  
  var select = document.composeform.type;
  
  if ( isNaN( select.value ) ) {
  
    divData.style.display = '';
  
  } else {
  
    divData.style.display = 'none';
  
  }

}

function selCat( obj, pag ) {
	var val = document.getElementById( obj ).value; 
	//var cat = document.
	if ( val ) {
		document.location.href = '/cgi-bin/'+pag+'?id='+val;
	}
}

// Em desuso
function ___setArray( id, arrName ) {

 var obj = document.getElementById( id ); 
 
 if ( arrName ) {
 
 var actArray = eval( arrName );
 
 if ( actArray.length > 0 ) {
 
  for ( var i = 1; i <= actArray.length; i ++ ) {
  
   obj.options[i] = eval( actArray[i-1] );
   
  }
  
  obj.options.length = actArray.length + 1;
  
 } 
 
 } else {
 
  obj.options.length = 1;
 
 }
 
}

function setArray ( elId     // id do elemento
                  , opts ) { // array de opções
 var el = document.getElementById( elId ); // array

 el.options.length = 1; // Limpa a combo.

 if ( el != null ) { // Se elemento existir
  if ( opts != null       // Se array existir
   && opts.length > 0 ) { // E possuir itens
   
   // Percorre o array.
   for ( var pos = 0, last = opts.length; pos < last; pos ++ ) {
    var optVals = opts[pos]                                  // Item do array
      , optEl   = new Option( optVals.text, optVals.value ); // e elemento option.

    el.options.add( optEl ); // Adiciona a opção ao combo.
   }
  }
 } else {                        // Se não
  throw "elemento inexistente."; // gera excessão.
 }
}

// 3 fun&#65533;es para limitar o nmero de caracteres digitados em um textarea e ao mesmo tempo, tem um contador de caracteres visual
var ns6 = document.getElementById && !document.all

// 1
function RestringeInput( maxlength, e, placeholder ) {
 if ( window.event && event.srcElement.value.length >= maxlength ) {
  return false;
 } else if ( e.target && e.target == eval( placeholder ) && e.target.value.length >= maxlength ) {
  var PressTecla = /[a-zA-Z0-9\.\,\/]/; //detecta caracteres alfanum&#65533;icos
  if ( PressTecla.test ( String.fromCharCode ( e.which ) ) ) {
   e.stopPropagation();
  }
 }
}

// 2
function ContaLimite( maxlength, e, placeholder ) {
 var theform = eval( placeholder );
 var lengthleft = maxlength-theform.value.length;
 var placeholderobj = document.all ? document.all[placeholder] : document.getElementById( placeholder );
 if ( window.event || e.target && e.target == eval( placeholder ) ) {
  if ( lengthleft < 0 )
   theform.value = theform.value.substring( 0, maxlength );
   placeholderobj.innerHTML = lengthleft;
 }
}

// 3
function MostraLimite( theform, thelimit ) {
 var limit_text = '<span id="'+theform.toString()+'" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px; color:#555555">'+thelimit+'</span><span style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px; color:#555555"> caracteres restantes.</span>';
 if ( document.all || ns6 )
 document.write( limit_text );
 if ( document.all ) {
  eval( theform ).onkeypress = function() { return RestringeInput( thelimit, event, theform ) }
  eval( theform ).onkeyup = function() { ContaLimite( thelimit, event, theform ) }
 } else if ( ns6 ) {
  document.body.addEventListener( 'keypress', function( event ) { RestringeInput( thelimit, event, theform ) }, true );
  document.body.addEventListener( 'keyup', function( event ) { ContaLimite( thelimit, event, theform ) }, true );
 }
}

function showStateDescription ( StateId ) {
 document.getElementById( StateId ).style.display = "block";
}

function hideStateDescription ( StateId ) {
 document.getElementById( StateId ).style.display = "none";
}

function getXMLHTTPRequest () {
 var req;

 if ( window.XMLHttpRequest ) {
  req = new XMLHttpRequest;

  if ( req.overrideMimeType )
   req.overrideMimeType( "text/xml" );
 } else if ( window.ActiveXObject ) {
  try {
   req = new ActiveXObject( "Msxml2.XMLHTTP" );
  } catch (e) {
   try {
    req = new ActiveXObject( "Microsoft.XMLHTTP" );
   } catch (e) {
    alert( "Seu browser não dá suporte a esta operação." );
    return;
   }
  }
 }

 return req;
}
function abreconteudo(link,div){
	var resposta = getXMLHttpRequest();
	if(resposta){
	 	resposta.onreadystatechange = function() {
			if (resposta.readyState == 1){
				document.getElementById(div).innerHTML = "<div style=\"position:relative;top:10px;left:300px;\"><img src=\"img_lista/loading.gif\" /></div>";
			}
			if (resposta.readyState == 4) {
	 			if (resposta.status == 200) {
					document.getElementById(div).innerHTML = resposta.responseText;
				}else{
					alert("Houve um problema para conseguir os dados:\n"+ resposta.statusText);
				}
			}
		}
	}
	resposta.open("GET",link,true);
	resposta.send(null);
}

function monta_imgs_plantio(combo){
	var container = document.getElementById('img_plantio_jpg');
	var ind = combo.selectedIndex;
	var opt = combo.options.item(ind);
	var existe = document.getElementById('img'+opt.value);
	var cont = setCont();
	//alert(existe);
	//if(existe == null){
		var conteudo = '<div id="img'+opt.value+'" style="background-image:url(/img_plantio/'+opt.text+');background-repeat:no-repeat;border:2px solid lightgreen; width:118px; height:135px;float:left;margin:2px;">';
		//conteudo += '<img src="border="0" style="clear:left"/>';	
		conteudo += '<img src="/img_plantio/delete.png" onclick="javascript:remove_img('+opt.value+')" style="margin: 96% 0 14% 82%;"/><br />';
		conteudo += '<input type="hidden" name="img_plantio'+cont+'" value="'+opt.value+'" />';
		conteudo += '</div>';
		container.innerHTML += conteudo;
		cont++;
		document.getElementById('total_imgs_plantio').value = cont;
		opt.selected = '';
		combo.blur();
	/*
	}else{
		remove_img(opt.value);
	}
	*/
}
function setCont(){
	return parseInt(document.getElementById('total_imgs_plantio').value);
}

function remove_img(id_img){
	var div = document.getElementById('img'+id_img);
	var cont = setCont();
	div.innerHTML = '';
	div.style.display = 'none';
	div.removeAttribute('id');
	cont--;
	if(cont < 0 ){
		cont = 0;
	}
	document.getElementById('total_imgs_plantio').value = cont;
}

function buscaCliente(v,x){
      
      if( v.value != '' && v.value != null ) {
          var url = '/cgi-bin/z.cgi?cgc='+v.value;
          
          $(x).disabled = 1;
          $(x).options[0].innerHTML="Carregando..."; 
          new Ajax.Request(url, {
            method: 'get',
            onSuccess: function(transport) {
                $(x).options[transport.responseText].selected = 1;
                $(x).disabled = 0;
                $(x).options[0].innerHTML="Selecione um...";
            }
         });
      }
  }