function showHide(objectName) {
	var estado = document.getElementById(objectName).style.display;
	var img = document.getElementById(objectName+'img');
	if( estado == 'block' ) {
		document.getElementById(objectName).style.display = 'none';	
		document.images[objectName+'img'].src = "images/seta.gif";
	}else{
		document.getElementById(objectName).style.display = 'block';
		document.images[objectName+'img'].src = "images/setaDown.gif";
	}
}

function checaBuscaProdutos(){
		if(document.buscaProdutos.campoBusca.value=='' || document.buscaProdutos.campoBusca.value.length < 2) {
			alert("A pesquisa deve conter pelo menos dois caracteres!");
			document.buscaProdutos.campoBusca.focus();
			return false;
		}else{
			document.buscaProdutos.submit()
		}
	return true;
}

function resizePopUP(object){

     var NS = (navigator.appName=="Netscape")?true:false; 
     var iWidth = (NS)?window.innerWidth:document.body.clientWidth; 
     var iHeight = (NS)?window.innerHeight:document.body.clientHeight; 
     iWidth = document.getElementById(object).offsetWidth - iWidth; 
     iHeight = document.getElementById(object).offsetHeight - iHeight; 

	if(document.all){
		extraHeight = 0;
		extraWidth = 0;
	}else{
		extraHeight = 50;
		extraWidth = 25;
	}
	window.resizeBy(iWidth, iHeight);
	window.moveTo(screen.width/2 - iWidth ,screen.height/2 - iHeight);
}

function showFoto(url){
	window.open(url,"FotoI","resizable=no, top=0, left=0, width=10, height=10, scrollbars=0, toolbar=0, location=0, menubar=0, status=no");
}

function imprimirProduto(url){
	window.open(url,"ProdutoI","resizable=no, top=30, left=100, width=650, height=570, scrollbars=yes, toolbar=0, location=0, menubar=0, status=no");
}

function formataValor(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.getElementById(campo).value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

	if (tam < tammax && tecla != 8){ 
		tam = vr.length + 1; 
	}

	if (tecla == 8 ){	
		tam = tam - 1; 
	}
	
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 2 ){ 
			document.getElementById(campo).value = vr; 
		}
		if ( (tam > 2) && (tam <= 5) ){
			document.getElementById(campo).value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); 
		}
		if ( (tam >= 6) && (tam <= 8) ){
			document.getElementById(campo).value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
		}
		if ( (tam >= 9) && (tam <= 11) ){
			document.getElementById(campo).value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
		}
		if ( (tam >= 12) && (tam <= 14) ){
			document.getElementById(campo).value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
		}
		if ( (tam >= 15) && (tam <= 17) ){
			document.getElementById(campo).value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
		}
	}

}

function apenasNumericos(caracter) {
	if(document.all) { // Internet Explorer
		var tecla = event.keyCode;
	}else {
		if(document.layers) { // Nestcape
		var tecla = caracter.which;
		}
	}
	if(tecla == null){
		tecla = event.keyCode;
	}
	if(tecla > 47 && tecla < 58) { // numeros de 0 a 9
		return true;
	}else {
		if (tecla != 8) { // backspace
			return false;
		}else {
			return true;
		}
	}
}

/**
 *
 * function mask(_mask, val)
 *
 * _mask = Mascara Exemplo: ##/##/#### ou ###.###.###-##
 * val   = Valor a ser formatado.
 *
 * Formata um valor  para a mascara definida.
 *
 * pedro.leao@ig.com.br 2003/08/16
 */
function mask(_mask, val) {
	var i, mki;
	var aux="";
	
	for(i=mki=0; i<val.length; i++, mki++) {
		if(_mask.charAt(mki)=='' || _mask.charAt(mki)=='#' || _mask.charAt(i)==val.charAt(i)) {
			aux+=val.charAt(i);
		} else {
			aux+=_mask.charAt(mki)+val.charAt(i);
			mki++;
		}
	}
	return aux;
}

/**
 * function maskEvent(field, _mask, event)
 *
 * field = Objeto que esta enviando o evendo onKeyPress()
 * _mask = Mascara Exemplo: ##/##/#### ou ###.###.###-##
 * event = Evento a ser observado.
 *
 * Formata um valor para a mascara definida conforma o valor vai sendo digitado.
 *
 */
function maskEvent(field, _mask, event) {
	var key ='';
	var aux='';
	var len=0;
	var i=0;
	var aux2='';
	var strCheck = '0123456789';
	var rcode = (window.Event) ? event.which : event.keyCode;
	
	if(rcode == 13) {
		//Enter
		return true;
	}
	
	//Get key value from key code
	key=String.fromCharCode(rcode);
	
	if(strCheck.indexOf(key)==-1) {
		//Not a valid key
		return false;
	}
	aux=field.value;
	aux2=_mask;
	if(aux.length < aux2.length) {
		//aux=field.value+key;
		//window.alert(aux);
		aux=mask(_mask,aux);
		//window.alert(aux);
		field.value=aux;
	}
	field.value=aux;
	return false;
}

function MM_findObj(n, d) { //v4.01
  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);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
