function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

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_showHideLayers() { //v6.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; }
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function realizaBusca(){
	if ($("#desc_busca").val().length > 0 )
		location.href = "produto.php?seach="+$("#desc_busca").val();
	else 
		alert('Campo descrição da busca esta em branco!');
}
	
function buscaMarca(){
	if ($("#cod_marca").val())
		location.href = "produto.php?marca="+$("#cod_marca").val();
}

function vai(url){
	location.href = url;
}

function janelaCenter(url, nome, parms, width, height) {
 	var str = "height=" + height + ",innerHeight=" + height;
	str += ",width=" + width + ",innerWidth=" + width;
	if (parms != "") {
		str += ", " + parms;
	}
	if (window.screen) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;

	} else {
		var xc = 0, xy = 0;
	}
	str += ",left=" + xc + ",screenX=" + xc;
	str += ",top=" + yc + ",screenY=" + yc;
	hwnd = window.open(url, nome, str);
	if(hwnd.window.focus){hwnd.window.focus();}
//	if(text != "") hwnd.document.write(text);
//	hwnd.document.close();
	return hwnd;
}

function valida_camposArray(campo,nome){
  var d = document;
  for (var i = 0;i<campo.length; i++){
    var vlrCampo = d.getElementById(campo[i]).value; 
    if (vlrCampo == ""  || vlrCampo == 0){
		alert ("Campo "+nome[i]+" esta em branco!");
		d.getElementById(campo[i]).focus();
		return false;
    }
  }
  return true;
}

function valida_estado(estado){
	var estados = new Array('AC','AL','AP','AM','BA','DF','CE','ES','GO','MA','MT','MS','MG','PB','PR','PA','PE','PI','RJ','RN','RS','RO','RR','SC','SP','SE','TO');
	for (var i = 0; i < estados.length; i++){
		if (estados[i] == estado.toUpperCase()){
			return true;
		}
	}
	return false;
}

function validaCPF(cpf){
	erro = new String;
	
	cpf = cpf.replace('.', '');
	cpf = cpf.replace('.', '');
	cpf = cpf.replace('-', '');

	if (cpf.length == 11){    
		var nonNumbers = /\D/;
		if (cpf == "00000000000" || 
				cpf == "11111111111" || 
				cpf == "22222222222" || 
				cpf == "33333333333" || 
				cpf == "44444444444" || 
				cpf == "55555555555" || 
				cpf == "66666666666" || 
				cpf == "77777777777" || 
				cpf == "88888888888" || 
				cpf == "99999999999") {
				
				return false;
		}
	
		var a = [];
		var b = new Number;
		var c = 11;
	
		for (i=0; i<11; i++){
				a[i] = cpf.charAt(i);
				if (i < 9) b += (a[i] * --c);
		}
	
		if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
		b = 0;
		c = 11;
	
		for (y=0; y<10; y++) b += (a[y] * c--); 
	
		if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
	
		if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])) {
			return false;
		}
	} else 
		return false;
	
    return true;    
}

function validar_email(email){
   if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) || !email) 
     return false;
   else 
     return true;
}


function validar_data(campo){
	var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
	if ((campo.match(expReg)) && (campo!='')){
		var dia = campo.substring(0,2);
		var mes = campo.substring(3,5);
		var ano = campo.substring(6,10);

		if((mes==4 || mes==6 || mes==9 || mes==11) && dia > 30){
			//alert("Dia incorreto !!! O mês especificado contém no máximo 30 dias.");
			return false;
		} else{
			if(ano%4!=0 && mes==2 && dia>28){
	//			alert("Data incorreta!! O mês especificado contém no máximo 28 dias.");
				return false;
			} else{
				if(ano%4==0 && mes==2 && dia>29){
	//				alert("Data incorreta!! O mês especificado contém no máximo 29 dias.");
					return false;
				} else{	
	//				alert ("Data correta!");
					return true;
				}
			}
		}
	} else {
	//	alert(msgErro);
	//	campo.focus();
		return false;
	}
}

function $_get( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


// Codificar Strings

function url_encode(str) {  
        var hex_chars = "0123456789ABCDEF";  
        var noEncode = /^([a-zA-Z0-9\_\-\.])$/;  
        var n, strCode, hex1, hex2, strEncode = "";  

        for(n = 0; n < str.length; n++) {  
            if (noEncode.test(str.charAt(n))) {  
                strEncode += str.charAt(n);  
            } else {  
                strCode = str.charCodeAt(n);  
                hex1 = hex_chars.charAt(Math.floor(strCode / 16));  
                hex2 = hex_chars.charAt(strCode % 16);  
                strEncode += "%" + (hex1 + hex2);  
            }  
        }  
	return strEncode;  
}  

// Descodificar String 

function url_decode(str) {  
	var n, strCode, strDecode = "";  

	for (n = 0; n < str.length; n++) {  
		if (str.charAt(n) == "%") {  
			strCode = str.charAt(n + 1) + str.charAt(n + 2);  
			strDecode += String.fromCharCode(parseInt(strCode, 16));  
			n += 2;  
		} else {  
			strDecode += str.charAt(n);  
		}  
	}  

	return strDecode;  
} 

function number_format (number, decimals, dec_point, thousands_sep){
	var exponent = "";
	var numberstr = (number ? number.toString () : 0);
	var eindex = numberstr.indexOf ("e");

	if (eindex > -1)
	{
		exponent = numberstr.substring (eindex);
		number = parseFloat (numberstr.substring (0, eindex));
	}
	
	if (decimals != null)
	{
		var temp = Math.pow (10, decimals);
		number = Math.round (number * temp) / temp;
	}

	var sign = number < 0 ? "-" : "";
	var integer = (number > 0 ? 
	  Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
	
	var fractional = number.toString ().substring (integer.length + sign.length);
	dec_point = dec_point != null ? dec_point : ".";
	fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
			   (dec_point + fractional.substring (1)) : "";
	if (decimals != null && decimals > 0)
	{
		for (i = fractional.length - 1, z = decimals; i < z; ++i)
		  fractional += "0";
	}
	
	thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
				  thousands_sep : null;
	if (thousands_sep != null && thousands_sep != "")
	{
		for (i = integer.length - 3; i > 0; i -= 3)
		  integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
	}
	
	return sign + integer + fractional + exponent;
}

function trocaponto(valor){
	return (valor ? valor.replace(".", "").replace(",",".") : 0);
}

function validaCNPJ(cnpj){
	var cnpj = cnpj.replace(".","").replace(".","").replace("-","").replace("/","");
	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
	digitos_iguais = 1;
	
	if (cnpj.length < 14 && cnpj.length < 15)
		return false;
		
	for (i = 0; i < cnpj.length - 1; i++)
		if (cnpj.charAt(i) != cnpj.charAt(i + 1)){
			  digitos_iguais = 0;
			  break;
		}
		if (!digitos_iguais){
			tamanho = cnpj.length - 2
			numeros = cnpj.substring(0,tamanho);
			digitos = cnpj.substring(tamanho);
			soma = 0;
			pos = tamanho - 7;
			for (i = tamanho; i >= 1; i--){
				  soma += numeros.charAt(tamanho - i) * pos--;
				  if (pos < 2)
						pos = 9;
			}
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
			if (resultado != digitos.charAt(0))
				  return false;
			tamanho = tamanho + 1;
			numeros = cnpj.substring(0,tamanho);
			soma = 0;
			pos = tamanho - 7;
			for (i = tamanho; i >= 1; i--){
				soma += numeros.charAt(tamanho - i) * pos--;
				if (pos < 2)
					pos = 9;
			}
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
			if (resultado != digitos.charAt(1))
				return false;
		
		return true;
	} else
		return false;
} 
