function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function validaAddFab(theForm)
{
  if (isBlank(theForm.nome) )
  {
    alert("Preencha o campo Nome.");
	theForm.nome.focus();
    return false;
  }
  
  return true;
}


function validaEditarProd(theForm)
{
/* ***************************************************************************** */
 if (theForm.promocao_semana.value == 1 && theForm.qtdade_promocao.value > 2 )
  {
    alert("Você já possue 9 produtos como Promoção da Semana.");
    return false;
  }	
/* ***************************************************************************** */
  if (isBlank(theForm.categoria_id) )
  {
    alert("Você deve selecionar uma categoria na tabela da direita.");
    return false;
  }
  if (isBlank(theForm.nome) )
  {
    alert("Preencha o campo Nome.");
	theForm.nome.focus();
    return false;
  }
  if (isBlank(theForm.fabrica) )
  {
    alert("Preencha o campo Fábrica.");
	theForm.fabrica.focus();
    return false;
  }  
  if (!isBlank(theForm.preco_fabrica) && !isNumber(theForm.preco_fabrica) )
  {
	alert("O campo Preço de Fábrica está preenchido incorretamente.");
	theForm.preco_fabrica.focus();
	return false;
  }
  if (!isBlank(theForm.preco_custo) && !isNumber(theForm.preco_custo) )
  {
    alert("O campo Preço de Custo está preenchido incorretamente.");
	theForm.preco_custo.focus();
    return false;
  }
  if (!isBlank(theForm.preco_venda) && !isNumber(theForm.preco_venda) )
  {
    alert("O campo Preço de Venda está preenchido incorretamente.");
	theForm.preco_venda.focus();
    return false;
  }
  if (!isBlank(theForm.preco_promocional) && !isNumber(theForm.preco_promocional) )
  {
    alert("O campo Preço Promocional está preenchido incorretamente.");
	theForm.preco_promocional.focus();
    return false;
  }
  
  return true;
}

function validaAddProd(theForm)
{
	/* ******************************************************* */
  if (theForm.promocao_semana.value == 1 && theForm.qtdade_promocao.value > 2 )
  {
    alert("Você já possue 9 produtos como Promoção da Semana.");
    return false;
  }
	/* ******************************************************* */ 
  
  if (isBlank(theForm.categoria_id) )
  {
    alert("Você deve selecionar uma categoria na tabela da direita.");
    return false;
  }
  if (isBlank(theForm.nome) )
  {
    alert("Preencha o campo Nome.");
	theForm.nome.focus();
    return false;
  }
  if (isBlank(theForm.fabrica) )
  {
    alert("Preencha o campo Fábrica.");
	theForm.fabrica.focus();
    return false;
  }  
  if (!isBlank(theForm.preco_fabrica) && !isNumber(theForm.preco_fabrica) )
  {
	alert("O campo Preço de Fábrica está preenchido incorretamente.");
	theForm.preco_fabrica.focus();
	return false;
  }
  if (!isBlank(theForm.preco_custo) && !isNumber(theForm.preco_custo) )
  {
    alert("O campo Preço de Custo está preenchido incorretamente.");
	theForm.preco_custo.focus();
    return false;
  }
  if (!isBlank(theForm.preco_venda) && !isNumber(theForm.preco_venda) )
  {
    alert("O campo Preço de Venda está preenchido incorretamente.");
	theForm.preco_venda.focus();
    return false;
  }
  if (!isBlank(theForm.preco_promocional) && !isNumber(theForm.preco_promocional) )
  {
    alert("O campo Preço Promocional está preenchido incorretamente.");
	theForm.preco_promocional.focus();
    return false;
  }
  if (isBlank(theForm.foto1) )
  {
    alert("Você deve enviar pelo menos a foto de destaque.");
	theForm.foto1.focus();
    return false;
  }
  
  return true;
}

function attachmentFields()
{
    fields = new Array();
    for (var i = 0; i < document.form1.elements.length; i++) {
        if (document.form1.elements[i].name.substr(0, 10) == 'categoria_') {
            fields[fields.length] = document.form1.elements[i];
        }
    }
    return fields;
}


function attachmentChanged()
{

    var usedFields = 0;
    var fields = attachmentFields();
    if (fields.length == 50) {
        return;
    }

    for (var i = 0; i < fields.length; i++) {
        if (fields[i].value.length > 0) {
            usedFields++;
        }
    }

    if (usedFields == fields.length) {
        var lastRow = document.getElementById('attachment_row_' + usedFields);
        if (lastRow) {
            var newRow = document.createElement('TR');
            newRow.id = 'attachment_row_' + (usedFields + 1);
			newRow.className = 'tabela_linha';
			
			td = document.createElement('TD');
            newRow.appendChild(td);
            td.align = 'center';
            var texto = document.createElement('INPUT');
            texto.type = 'text';
            td.appendChild(texto);
            texto.name = 'categoria_' + (usedFields + 1);
            texto.onchange = function() { attachmentChanged(); };
            texto.size = 40;
			texto.className = 'campo';
			
			
			td = document.createElement('TD');
            newRow.appendChild(td);
            td.align = 'left';
            var venda = document.createElement('INPUT');
            venda.type = 'checkbox';
            td.appendChild(venda);
			venda.setAttribute('value', 'ok');
            venda.name = 'tipo_venda_' + (usedFields + 1);
			
			td = document.createElement('TD');
            newRow.appendChild(td);
            td.align = 'left';
            var loca = document.createElement('INPUT');
            loca.type = 'checkbox';
            td.appendChild(loca);
			loca.setAttribute('value', 'ok');
            loca.name = 'tipo_locacao_' + (usedFields + 1);
						
			//td.appendChild(document.createTextNode(' '));
								
            lastRow.parentNode.insertBefore(newRow, lastRow.nextSibling);
			
			if(document.form1.venda.value == 1 && document.form1.locacao.value == 0) {
				for (var i = 0; i < document.form1.elements.length; i++) {
					if (document.form1.elements[i].name.substr(0, 11) == 'tipo_venda_') {
						document.form1.elements[i].checked = true;
					}
					if (document.form1.elements[i].name.substr(0, 13) == 'tipo_locacao_') {
						document.form1.elements[i].checked = false;
					}
				}
				
			} else if(document.form1.venda.value == 0 && document.form1.locacao.value == 1) {
				for (var i = 0; i < document.form1.elements.length; i++) {
					if (document.form1.elements[i].name.substr(0, 11) == 'tipo_venda_') {
						document.form1.elements[i].checked = false;
					}
					if (document.form1.elements[i].name.substr(0, 13) == 'tipo_locacao_') {
						document.form1.elements[i].checked = true;
					}
				}
			}
			
        }
    }
}

function validaEdiRamSub(theForm)
{
	var venda_pai = document.form1.venda_pai.value;
	var locacao_pai = document.form1.locacao_pai.value;
	
	if(document.form1.venda.checked == false && document.form1.locacao.checked == false) {
		alert("Você deve preencher todos os campos.");
		if(venda_pai == 1)
			document.form1.venda.checked = true;
			else
				document.form1.venda.checked = false;
		if(locacao_pai == 1)
			document.form1.locacao.checked = true;
			else
				document.form1.locacao.checked = false;
		return false;
	} else if(venda_pai == 1 && locacao_pai == 0 && document.form1.venda.checked == false) {
		alert("Para mudar essa categoria de Venda para Locação\nvocê deve alterar a categoria pai para Locação.");
		document.form1.venda.checked = true;
		document.form1.locacao.checked = false;
		return false;
	} else if(venda_pai == 0 && locacao_pai == 1 && document.form1.locacao.checked == false) {
		alert("Para mudar essa categoria de Locação para Venda\nvocê deve alterar a categoria pai para Venda.");
		document.form1.venda.checked = false;
		document.form1.locacao.checked = true;
		return false;
	} 
	
	
  return true;
}


function validaEdiRamCat(theForm)
{
	var venda_valor = document.form1.venda.value;
	var locacao_valor = document.form1.locacao.value;
	var venda_pai = document.form1.venda_pai.value;
	var locacao_pai = document.form1.locacao_pai.value;
	
	if(document.form1.venda.checked == false && document.form1.locacao.checked == false) {
		alert("Você deve preencher todos os campos.");
		if(venda_pai == 1)
			document.form1.venda.checked = true;
			else
				document.form1.venda.checked = false;
		if(locacao_pai == 1)
			document.form1.locacao.checked = true;
			else
				document.form1.locacao.checked = false;
		return false;
	} else if(venda_pai == 1 && locacao_pai == 0 && document.form1.venda.checked == false) {
		alert("Para mudar essa categoria de Venda para Locação\nvocê deve alterar a categoria pai para Locação.");
		document.form1.venda.checked = true;
		document.form1.locacao.checked = false;
		return false;
	} else if(venda_pai == 0 && locacao_pai == 1 && document.form1.locacao.checked == false) {
		alert("Para mudar essa categoria de Locação para Venda\nvocê deve alterar a categoria pai para Venda.");
		document.form1.venda.checked = false;
		document.form1.locacao.checked = true;
		return false;
	} 
	
	venda = new Array();
	locacao = new Array();
	for (var i = 0; i < document.form1.elements.length; i++) {
		if (document.form1.elements[i].name.substr(0, 11) == 'tipo_venda_') {
			venda[venda.length] = document.form1.elements[i].checked;
		}
		if (document.form1.elements[i].name.substr(0, 13) == 'tipo_locacao_') {
			locacao[locacao.length] = document.form1.elements[i].checked;
		}
	}
	for (var cont = 0; cont < locacao.length - 1; cont++) {
		if(venda[cont] == false && locacao[cont] == false) {
			alert("Você deve preencher todos os campos.");
			return false;
		} else if(document.form1.venda.checked == true && document.form1.locacao.checked == false && locacao[cont] == true) {
			alert("Nenhuma ramificação desta categoria pode ser Locação.");
			return false;				
		} else if(document.form1.venda.checked == false && document.form1.locacao.checked == true && venda[cont] == true) {
			alert("Nenhuma ramificação desta categoria pode ser Venda.");
			return false;				
		}
	}
	
  return true;
}

function validaAddRamCat(theForm)
{
	var venda_valor = document.form1.venda.value;
	var locacao_valor = document.form1.locacao.value;
		venda = new Array();
		locacao = new Array();
		for (var i = 0; i < document.form1.elements.length; i++) {
			if (document.form1.elements[i].name.substr(0, 11) == 'tipo_venda_') {
				venda[venda.length] = document.form1.elements[i].checked;
			}
			if (document.form1.elements[i].name.substr(0, 13) == 'tipo_locacao_') {
				locacao[locacao.length] = document.form1.elements[i].checked;
			}
		}
		for (var cont = 0; cont < locacao.length - 1; cont++) {
			if(venda[cont] == false && locacao[cont] == false) {
				alert("Você deve preencher todos os campos.");
				return false;
			} else if(venda_valor == 1 && locacao_valor == 0 && locacao[cont] == true) {
				alert("Nenhuma ramificação desta categoria pode ser Locação");
				return false;				
			} else if(venda_valor == 0 && locacao_valor == 1 && venda[cont] == true) {
				alert("Nenhuma ramificação desta categoria pode ser Venda");
				return false;				
			}
		}
	
  return true;
}
// *********
function validaAdd(theForm)
{
  if (isBlank(theForm.categoria) )
  {
    alert("Preencha o campo Categoria.");
	theForm.categoria.focus();
    return false;
  }
  
  if (theForm.tipo_venda.checked == false && theForm.tipo_locacao.checked == false)
  {
    alert("Você deve selecionar o tipo da categoria.\nVenda ou Locação.");
	theForm.categoria.focus();
    return false;
  }
  return true;
}

// *********************************************************************************************

function ValidaEdit(theForm)
{
if (theForm.opcao5.value == "ok" )
{
	if (isBlank(theForm.p5) )
	  {
		alert("Preencha o campo \"P\" com a dimensão da logo miniatura.");
		theForm.p5.focus();
		return false;
	  }
	if (isBlank(theForm.g5) )
	  {
		alert("Preencha o campo \"G\" com a dimensão da logo grande.");
		theForm.g5.focus();
		return false;
	  }
}

  return true;
}


// *************************************************

function edit(id_foto, ident)
{	
	var end = "legenda_foto_" + ident;
	var legenda = document.getElementById(end).value;
	var div = "leg_" + ident;
	var obj = document.getElementById(div);
	obj.innerHTML = "";
	/*
	var text = document.createElement('INPUT');
	text.type = 'text';
	obj.appendChild(text);
	text.name = 'leg_' + ident;
	text.value = legenda;
	text.setAttribute("class", "campo");
	text.size = 20;
	text.focus();
	
	var br = document.createElement("br");
	obj.appendChild(br);
	*/
	var text = document.createElement('TEXTAREA');
		var vai = 'leg_' + ident;
 	    text.setAttribute('name', vai);
		text.setAttribute('value', legenda);
		text.setAttribute('overflow', 'hidden');
   	    text.setAttribute('class','campo');
 	    text.setAttribute('cols','16');
		text.setAttribute('rows','4');
		text.focus();
	obj.appendChild(text);
	
	var br = document.createElement("br");
	obj.appendChild(br);
	
	var ok = document.createElement("a");
	ok.setAttribute("href", "#");
	ok.appendChild(document.createTextNode('OK'));
	ok.onclick = function(){ atualiza(text.value, id_foto, ident);}
	obj.appendChild(ok);	
	/*
	var ok = document.createElement("input");
	ok.setAttribute("class", "botao");
    ok.className = 'botao';
	ok.setAttribute("name", "ok");
    ok.setAttribute("type", "button");
	ok.setAttribute("value", " OK ");
	ok.onclick = function(){ atualiza(text.value, id_foto, ident);}
	obj.appendChild(ok);
	*/	
	obj.appendChild(document.createTextNode('      '));
	
	var can = document.createElement("a");
	can.setAttribute("href", "#");
	can.appendChild(document.createTextNode('Cancelar'));
	can.onclick = function(){ limpa(legenda, id_foto, ident);}
	obj.appendChild(can);
	/*
	var can = document.createElement("input");
	can.className = 'botao';
	can.setAttribute("class", "botao");
    can.setAttribute("name", "cancelar");
    can.setAttribute("type", "button");
	can.setAttribute("value", " Cancelar ");
	can.onclick = function(){ limpa(legenda, id_foto, ident);}
	obj.appendChild(can);*/

}

function atualiza(nova, id_foto, ident) {
	var end = "legenda_foto_" + ident;
	document.getElementById(end).value = nova;
	var div = "leg_" + ident;
	var obj = document.getElementById(div);
	if(nova != "")
		obj.innerHTML = "<a href=\"#\" onclick=\"edit('" + id_foto + "', '" + ident + "')\">" + nova + "</a>";
	else
		obj.innerHTML = "<a href=\"#\" onclick=\"edit('" + id_foto + "', '" + ident + "')\">[sem legenda]</a>";
	
	
}

function limpa(legenda, id_foto,ident) {
	var div = "leg_" + ident;
	var obj = document.getElementById(div);
	if(legenda != "" && legenda != " ") 
		obj.innerHTML = "<a href=\"#\" onclick=\"edit('" + id_foto + "', '" + ident + "')\">" + legenda + "</a>";
	else
		obj.innerHTML = "<a href=\"#\" onclick=\"edit('" + id_foto + "', '" + ident + "')\">[sem legenda]</a>";
}


//Variáveis globais
var _loadTimer	= setInterval(loadAnima,18);
var _loadPos	= 0;
var _loadDir	= 2;
var _loadLen	= 0;

//Anima a barra de progresso
function loadAnima(){
	var elem = document.getElementById("barra_progresso");
	if(elem != null){
		if (_loadPos==0) _loadLen += _loadDir;
		if (_loadLen>32 || _loadPos>79) _loadPos += _loadDir;
		if (_loadPos>79) _loadLen -= _loadDir;
		if (_loadPos>79 && _loadLen==0) _loadPos=0;
		elem.style.left		= _loadPos;
		elem.style.width	= _loadLen;
	}
}
//Esconde o carregador
function loadEsconde(){
	this.clearInterval(_loadTimer);
	var objLoader = document.getElementById("carregador_pai");
	objLoader.style.visibility	="hidden";
}

function loadAnimaDeVez(){
	var obj = document.getElementById("carregador_pai");
	obj.style.visibility = "visible";
	var elem = document.getElementById("barra_progresso");
	if(elem != null){
		if (_loadPos==0) _loadLen += _loadDir;
		if (_loadLen>32 || _loadPos>79) _loadPos += _loadDir;
		if (_loadPos>79) _loadLen -= _loadDir;
		if (_loadPos>79 && _loadLen==0) _loadPos=0;
		elem.style.left		= _loadPos;
		elem.style.width	= _loadLen;
	}
}

function loadEscondeDeVez(){
	var objLoader = document.getElementById("carregador_pai");
	objLoader.style.visibility	="hidden";
}
// ***********************************************

function MudaCursor(para) {
	var geral = document.getElementById("geral");
	if(para == "carregando") {
	  geral.style.cursor = "wait";
	} else {
	geral.style.cursor = "default";
	}
  }

function MudaFonte(elem, acao) {
	var tamInic = 16;
	var tamMin = 12;
    var tamMax = 22;
	var elemento = document.getElementById(elem);
	if(elemento.style.fontSize == "")
		var tamFonte = tamInic;
	else
		var tamFonte = parseInt(elemento.style.fontSize);
	switch (acao)
    {
      case '+':
        if (tamFonte < tamMax)
          elemento.style.fontSize = (tamFonte + 2) + "px";
      break;
      case '-':
        if (tamFonte > tamMin)
          elemento.style.fontSize = (tamFonte - 2) + "px";
      break;
    }

}

function HabilitarP(campo, num) {
	if (campo.checked == true){
	document.getElementById("quantp_" + num).disabled = false;
	document.getElementById("quantp_" + num).value = 1;
	} else {
	document.getElementById("quantp_" + num).disabled = true;
	document.getElementById("quantp_" + num).value = "";
	}
}
function HabilitarM(campo, num) {
	if (campo.checked == true){
	document.getElementById("quantm_" + num).disabled = false;
	document.getElementById("quantm_" + num).value = 1;
	} else {
	document.getElementById("quantm_" + num).disabled = true;
	document.getElementById("quantm_" + num).value = "";
	}
}
function HabilitarG(campo, num) {
	if (campo.checked == true){
	document.getElementById("quantg_" + num).disabled = false;
	document.getElementById("quantg_" + num).value = 1;
	} else {
	document.getElementById("quantg_" + num).disabled = true;
	document.getElementById("quantg_" + num).value = "";
	}
}

function updateValida(theForm)
{
var variavel = theForm.cpf.value;
  if (theForm.oferta1.value == 0 )
  {
    alert("Seu pedido está vazio!!!");
    return false;
  }
  if (isBlank(theForm.nome) )
  {
    alert("Preencha o campo Nome.");
	theForm.nome.focus();
    return false;
  }
  if (isBlank(theForm.cpf) )
  {
    alert("Preencha corretamente o CPF.");
	theForm.cpf.focus();
    return false;
  }
  if(variavel.charAt(3) != "." || variavel.charAt(7) != "." || variavel.charAt(11) != "-" || variavel.length != 14) {
	alert("O campo CPF deve ser preenchido corretamente!!!");
  	theForm.cpf.focus();
  	return (false);
	}
	if (isBlank(theForm.endereco) )
  {
    alert("Preencha o campo Endereço.");
	theForm.endereco.focus();
    return false;
  }
  if (isBlank(theForm.bairro) )
  {
    alert("Preencha o campo Bairro.");
	theForm.bairro.focus();
    return false;
  }
  if (isBlank(theForm.cep) )
  {
    alert("Preencha o campo CEP.");
	theForm.cep.focus();
    return false;
  }
  if (isBlank(theForm.cidade) )
  {
    alert("Preencha o campo Cidade.");
	theForm.cidade.focus();
    return false;
  }
  if (isBlank(theForm.uf) )
  {
    alert("Preencha o campo UF.");
	theForm.uf.focus();
    return false;
  }
  if (isBlank(theForm.email))
  {
    alert("Preencha corretamento o campo E-mail.");
	theForm.email.focus();
    return false;
  }
  else if (!isMail(theForm.email))
  {
    alert("Formato incorreto de e-mail.");
	theForm.email.focus();
    return false;
  }
  if (isBlank(theForm.msg) )
  {
    alert("Preencha o campo Mensagem.");
	theForm.msg.focus();
    return false;
  }

  return true;
}


function NumeroTelCep(e)
{
navegador = /msie/i.test(navigator.userAgent);
if (navegador)
var tecla = event.keyCode;
else
var tecla = e.which;
//alert("Codigo da tecla: " + tecla);
if(tecla > 47 && tecla < 58 || tecla == 45 || tecla == 32) // numeros de 0 a 9
return true;
else
{
if (tecla != 8) // backspace
return false;
else
return true;
}
}


function Numero(e)
{
navegador = /msie/i.test(navigator.userAgent);
if (navegador)
var tecla = event.keyCode;
else
var tecla = e.which;
//alert("Codigo da tecla: " + tecla);
if(tecla > 47 && tecla < 58 || tecla == 46) // numeros de 0 a 9
return true;
else
{
if (tecla != 8) // backspace
return false;
else
return true;
}
}

function isMail(mailField){
  strMail = mailField.value;
  var re = new RegExp;
  re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  var arr = re.exec(strMail);
  if (arr == null)
    return(false);
  else
    return(true);
}

function minLen(txtField, minVal){
  strExp = txtField.value;
  l = strExp.length;
  if (l < minVal)
    return(true);
  else
    return(false);
}

function maxLen(txtField, maxVal){
  strExp = txtField.value;
  l = strExp.length;
  if (l > maxVal)
    return(true);
  else
    return(false);
}

function isBlank(txtField){
  if (txtField.value)
    return (false);
  else
    return(true);
}

function isSelectedZero(txtField){
  selected = txtField.selectedIndex;
  if (selected == 0)
    return(true);
  else
    return(false);
}

function isNumber(txtField){
  numExp = txtField.value;
  if (isNaN(numExp) || (numExp.length == 0))
    return (false);
  else
    return(true);
}
function indica(url) {
  window.open(url,'popupImageWindow','width=370, height=160, top=12, left=15, scrollbars=no, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no')
}

function isCPF(txtField){ 

  var i; 
  s = txtField.value;  
  var c = s.substr(0,9); 
  var dv = s.substr(9,2); 
  var d1 = 0; 
  
  for (i = 0; i < 9; i++){ 
    d1 += c.charAt(i)*(10-i); 
  } 
  
  if (d1 == 0) return false;   
  
  d1 = 11 - (d1 % 11); 
  
  if (d1 > 9) d1 = 0; 
  
  if (dv.charAt(0) != d1) return false; 
  
  d1 *= 2; 
  
  for (i = 0; i < 9; i++){ 
    d1 += c.charAt(i)*(11-i);   
  } 
  
  d1 = 11 - (d1 % 11); 
  
  if (d1 > 9) d1 = 0; 
  
  if (dv.charAt(1) != d1) return false; 
  
  return true; 
  
}
//FIM da fun o validar form
