// JavaScript Document

function a(msg)
{
	 window.alert(msg);
}

function c(msg)
{
	return window.confirm(msg);
}

function r(n)
{
	return history.go(n);
}

function submitForm(valida)
{		
  if(valida==null) valida = false;	
		if(document.forms[0].conteudoRte1 && document.getElementById('rte1'))
		{
			if(document.getElementById('rte1').contentDocument)
				document.forms[0].conteudoRte1.value = document.getElementById('rte1').contentDocument.body.innerHTML;
			else
				document.forms[0].conteudoRte1.value = document.getElementById('rte1').contentWindow.document.body.innerHTML;
		}
		if(valida)
		{
			if(validaForm() && document.forms[0])
		 	document.forms[0].submit();
		}
		else if(document.forms[0])
		 document.forms[0].submit();
}

function addHTML(tag, nm, vlr)
{
	//inner = (!document.getElementById(nm))? true : false;
	if(!document.getElementById(nm))
	{
		switch(tag)
		{
			case "input":
				if(document.forms[0])
					document.forms[0].innerHTML += "<input type='hidden' name="+nm+" id="+nm+" value="+vlr+" />";				
			break;
		}
	}
	else
	{
		tags = document.getElementsByTagName(tag);
		for(i=0; i<tags.length; i++)
		{
			 if(tags[i].id==nm)
				{
					tags[i].value = vlr;					
					//a(tags[i].value);
					break;
				}
		}
	}
}

function validaForm()
{
	msg = '';	
	campos = new Array();	
	compara = new Array('compare');
	mail = new Array('email');
	arquivo = new Array('arquivo');
	data = new Array('data');
	preco = new Array('preco');
	numero = new Array('numero');
	 diasMes = new Array(0);
		diasMes[1] = 31;
		diasMes[2] = 29;
		diasMes[3] = 31;
		diasMes[4] = 30;
		diasMes[5] = 31;
		diasMes[6] = 30;
		diasMes[7] = 31;
		diasMes[8] = 31;
		diasMes[9] = 30;
		diasMes[10] = 31;
		diasMes[11] = 30;
		diasMes[12] = 31;
		dataAtual = new Date();
		anoAtual = dataAtual.getFullYear();
	count = 0;
	for(i=0; i<document.forms[0].elements.length; i++)
	{
	 if( document.forms[0].elements[i].className.substring(0,9) == 'requerido' && document.forms[0].elements[i].value == '' ) //campos obrigatorios
		{
			campos[count++] =  " " + ((document.forms[0].elements[i].alt!=null)? document.forms[0].elements[i].alt : document.forms[0].elements[i].title);				
		}
		cmp1 = (document.forms[0].elements[i].className.substring(0,9)=='requerido' && document.forms[0].elements[i].className.substring(10)!='');
		cmp2 = (document.forms[0].elements[i].className.substring(0,8)=='opcional' && document.forms[0].elements[i].className.substring(9)!='');
	 if(cmp1 || cmp2) 
		{
			comparacao = document.forms[0].elements[i].className.indexOf(compara[0]); //comparacao
			if( comparacao!=(-1) ) 
			{
				n = Number(document.forms[0].elements[i].className.substring(comparacao + compara[0].length));
				if( !compara[n] )
				{
					compara[n] = new Array();
					compara[n][0] = document.forms[0].elements[i].value;
					compara[n][1] = document.forms[0].elements[i].alt;
				}
				else
				{
					compara[n][2] = document.forms[0].elements[i].value;
					compara[n][3] = document.forms[0].elements[i].alt;
				}				
			}
			else if(document.forms[0].elements[i].className.indexOf('data')!=(-1) && document.forms[0].elements[i].value!='')
			{
				contData = document.forms[0].elements[i].value;
				dia = Number(contData.substring(0, 2));
				mes = Number(contData.substring(3, 5));
				ano = Number(contData.substring(6, 10));				
				if(dia<1 || dia>diasMes[mes])
				 data[data.length] = document.forms[0].elements[i].alt;
				else if(mes<1 || mes>12)
					data[data.length] = document.forms[0].elements[i].alt;
				else if(ano<anoAtual-1 || ano>anoAtual+2)
					data[data.length] = document.forms[0].elements[i].alt;
			}
			else if(document.forms[0].elements[i].className.indexOf('email')!=(-1))
			{
				er = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{1,3})+$/;							
				if(er.test(document.forms[0].elements[i].value)==false)
					mail[mail.length] = document.forms[0].elements[i].alt;				
			}
			else if(document.forms[0].elements[i].className.indexOf('arquivo')!=(-1) && document.forms[0].elements[i].value!='')
			{
					ponto = document.forms[0].elements[i].value.charAt(document.forms[0].elements[i].value.length - 4);
					ext1 = document.forms[0].elements[i].value.substring(document.forms[0].elements[i].value.length - 4);
					ext2 = document.forms[0].elements[i].value.substring(document.forms[0].elements[i].value.length - 5);
				 if(document.forms[0].elements[i].className.indexOf('-img')==(-1))
					{						
						if((ponto!='.' && ext2!='.html' && ext2!='.jpeg') || ext1=='.exe' || ext1=='.dll' || ext1=='.msi')
						{
							arquivo[arquivo.length] = document.forms[0].elements[i].alt;
						}								
					}
					else
					{
						if((ponto!='.' && ext2!='.jpeg') || (ext1!='.gif' && ext1!='.jpg' && ext1!='.swf'))
						{
							arquivo[arquivo.length] = document.forms[0].elements[i].alt;
						}						 
					}
			}
			else if(document.forms[0].elements[i].className.indexOf('preco')!=(-1) && document.forms[0].elements[i].value!='')
			{
				 //5.000,00 //er = /^([0-9]?[0-9]?[0-9].)?([0-9]?[0-9]?[0-9].)?[0-9][0-9][0-9]\,[0-9][0-9]/; //a(er.test('5.000.000,55'));
					vlr = document.forms[0].elements[i].value;									
					if(vlr.charAt(vlr.length - 3)=='.' || vlr.charAt(vlr.length - 3)==',')
						casaDecimal = true;
					else
					{
						if(vlr.charAt(vlr.length - 2)=='.' || vlr.charAt(vlr.length - 2)==',')
						{
							vlr = vlr+"0";
							casaDecimal = true;
						}
						else						
					 	casaDecimal = false;
					}
					vlr = vlr.replace(".", "");
					vlr = vlr.replace(",", "");
					if(!Number(vlr))
					{
						preco[preco.length] = document.forms[0].elements[i].alt;
					}
					else
					{
						if(casaDecimal)
							vlr = vlr.substring(0, vlr.length-2)+"."+vlr.substring(vlr.length-2);							
						else
							vlr = vlr+".00";								
						document.forms[0].elements[i].value = vlr;					
					}
			}
			else if(document.forms[0].elements[i].className.indexOf('numero')!=(-1) && document.forms[0].elements[i].value!='')
			{
				vlr = document.forms[0].elements[i].value;
				if(isNaN(Number(vlr)))
				{
					numero[numero.length] = document.forms[0].elements[i].alt;
				}
			}
		}
	}
	if(document.getElementById('rte1'))
	{ //validacao de preenchimento de campo rico de edicao
	 if(document.getElementById('rte1').contentDocument)
		 vlrRte1 = document.getElementById('rte1').contentDocument.body.innerHTML;
		else if(document.getElementById('rte1').contentWindow)
			vlrRte1 = document.getElementById('rte1').contentWindow.document.body.innerHTML;
		if((vlrRte1.indexOf('Digite aqui')!=(-1) && vlrRte1.indexOf('Digite aqui')<5) || vlrRte1.length < 14)
			campos[count++] =  "Corpo do texto";			
	}	
	if(count>0)
	{
		msg = '- Os seguintes campos devem ser preenchidos:' + campos + '.';
	}
	if(compara.length>1)
	{
		for(i=1; i<compara.length; i++)
		{
			if( compara[i][0]!=null && compara[i][2]!=null && compara[i][0]!=compara[i][2] )
			{
				msg += ((msg!='')? '\n' : '') + '- Os campos "'+compara[i][1]+'" e "'+compara[i][3]+'" possuem informações diferentes. Eles devem ser iguais.';
			}
		}
	}	
	if(mail.length>1)
	{
		for(i=1; i<compara.length; i++)
		{
			msg += ((msg!='')? '\n' : '') + '- O ' + mail[i] + ' tem que ser válido.';
		}
	}
	if(arquivo.length>1)
	{
		for(i=1; i<arquivo.length; i++)
		{
			msg += ((msg!='')? '\n' : '') + '- O ' + arquivo[i] + ' tem que ter uma extensão válida.';
		}			
	}
	if(data.length>1)
	{
		for(i=1; i<data.length; i++)
		{
			msg += ((msg!='')? '\n' : '') + '- O campo "' + data[i] + '" possui uma data inválida ou muito distante.';
		}			
	}
	if(preco.length>1)
	{
		for(i=1; i<preco.length; i++)
		{
			msg += ((msg!='')? '\n' : '') + '- O campo "' + preco[i] + '" possui um valor de preco inválido.';
		}			
	}
	if(numero.length>1)
	{
		for(i=1; i<numero.length; i++)
		{
			msg += ((msg!='')? '\n' : '') + '- O campo "' + numero[i] + '" possui um valor que deveria ser numérico.';
		}			
	}	
	if(msg!='')
	{
		a(msg);
		return false;
	}
	else
	{
		return true;
	}
}

function operacao(n)
{
	if(n==null) n=0;
	switch(n)
	{
		case 'formCriar':
			addHTML('input', 'op', 'formCriar');
			if(operacao.arguments.length>1)
					addHTML('input', 'tipoCriar', operacao.arguments[1]);
			submitForm();
		break;
		case 0:
			addHTML('input', 'op', '');
			submitForm();
		break;
		case 1:	
		 addHTML('input', 'op', 1);						
			submitForm(true);		
		break;
		case 2:
		 addHTML('input', 'op', 2);
			if(operacao.arguments.length>1)
					addHTML('input', 'id', operacao.arguments[1]);
			submitForm();
		break;
		case 3:
		 a('Apagando este item os relacionamentos entre seções e arquivos do site podem ser afetados e a ação não pode ser desfeita.');
		 if(c('Está certo que este item deve ser apagado?'))
			{
				addHTML('input', 'op', 3);		
				if(operacao.arguments.length>1)
					addHTML('input', 'id', operacao.arguments[1]);
		 	submitForm();
			}
		break;
		case 4:
		 addHTML('input', 'op', 4);
			if(operacao.arguments.length>1)
					addHTML('input', 'id', operacao.arguments[1]);
			submitForm(true);
		break;
	}
}

function aplicaOperacao(elm, param, label)
{
	elm = document.getElementById(elm);
	if(label!=null) elm.value = label;
	
	if(elm.attachEvent)
		elm.attachEvent("onclick", funcao);
	else
	 elm.addEventListener("click", funcao, false);	
		
	function funcao()
	{
		operacao(param);
	}
	
}

// formata cep
function formataCEP(campo,teclapres) {
  var tecla=teclapres.keyCode;
  vr=campo.value;
  vr=vr.replace(".","");
  vr=vr.replace("-","");
  vr=vr.replace("/","");
  tam=vr.length;
  if(tecla!= 9 && tecla!=8){
    if (tam>4)
      campo.value=vr.substr(0,5)+'-'+vr.substr(5,2);
  }
}

//formata data feito e recomendado por zigomarrr o craque da camisa dez
function formataData(Arg1,teclapres){
	var tecla = teclapres.keyCode;
	vr = Arg1.value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;
	if(tecla != 9 && tecla!= 8 ){
		if ( tam > 2 && tam < 5 )
			Arg1.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		if ( tam >= 5 && tam <= 10 )
			Arg1.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
	}
}

// imprime
function Imprimir(){
	if(window.print){
		window.print();
	}else{
	    var WebBrowser='<OBJECT ID="WebBrowser1" WIDTH=0 HEIGTH=0 CLASSID="CLSID:8856F961-340A-11D0-A96B00C04FD705A2"></OBJECT>';
	    document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
	    WebBrowser1.ExecWB(6, 2);
	    WebBrowser1.outerHTML= " ";
	}
}

function soDigitaLetras(e)
{	
 if(window.event)
	{
	 if(e.keyCode>=48 && e.keyCode<=57) {	e.keyCode = 0;	}
	}
	else
	{
		if(e.which>=48 && e.which<=57) {	e.preventDefault();	}
	}
}

function soDigitaNumeros(e, except)
{	
	if(window.event)
	{
		if(except==null)
		{
	  if(e.keyCode < 48 || e.keyCode > 57) { e.keyCode = 0; }
		}
		else if(except=='decimal')
		{
			if((e.keyCode < 48 || e.keyCode > 57)&& (e.keyCode!=110 && e.keyCode!=188 && e.keyCode!=190 && e.keyCode!=194)) { e.keyCode = 0; }
		}
	}
	else
	{
		if(except==null)
		{
			if((e.which < 48 || e.which > 57) && e.which != 8 && e.which != 0) { e.preventDefault();	}
		}
		else if(except=='decimal')
		{
		 if((e.which < 48 || e.which > 57) && (e.which != 8 && e.which != 0 && e.which != 44 && e.which != 46)) { e.preventDefault();	}
		}
	}
}

function passaProximo(elm, prox)
{
		if(elm.value.length==elm.maxLength)
			eval('document.forms[0].'+prox+'.focus()');
}

function formataTelefone(campo,teclapres, soNum) {
  var tecla=teclapres.keyCode;
  vr=campo.value;
  vr=vr.replace(".","");
  vr=vr.replace("-","");
  vr=vr.replace("/","");
  vr=vr.replace("(","");
  vr=vr.replace(")","");
  tam=vr.length;
  if(tecla!= 9 && tecla!=8 && tecla!=0){
    if (tam<=1 && (soNum==null || soNum==false))
      campo.value='('+vr.substr(0,vr.length);																																
    if (tam>1 && tam<=2 && (soNum==null || soNum==false))
      campo.value=campo.value+')'+vr.substr(3,vr.length);
    if (tam>5 && tam<=6 && (soNum==null || soNum==false))
     campo.value=campo.value+'-'+vr.substr(6,vr.length);						
				else if (tam>3 && tam<=4 && (soNum==true))		
					campo.value=campo.value+'-'+vr.substr(4,vr.length);						
  }
}

function setRteContent(ctd)
{
			function conteudo()		
			{
			 if(document.getElementById('rte1').contentDocument)
				 document.getElementById('rte1').contentDocument.body.innerHTML = ctd;
				else if(document.getElementById('rte1').contentWindow)
					document.getElementById('rte1').contentWindow.document.body.innerHTML = ctd;
			}
			setTimeout(conteudo, 1000);
}

function copiaValor(elm, alvo)
{
	if(!elm) elm = document.getElementById(elm);
	alvo = document.getElementById(alvo);
	if(elm && alvo)
	{
		alvo.value = elm.value;
	}
}

function checkItem(elm) //AQUIIIII
{
		if(!elm) elm = document.form[0].elm[0];
		elm.check();
		a(elm);
}

//FUNÇÕES DE CONTROLE VISUAL:

function selectVisualizacao(slt)
{
	if(document.getElementById('tipoItem')) 
	{
		if(document.getElementById('tipoItem').value==1 && document.attachEvent) //pala do RTE no IE
		{
		 document.getElementById('fldHTML').innerHTML = "";
		}
	}
	operacao('formCriar', slt.value);	
}

function display(elm, result)
{
	elm = document.getElementById(elm);	
	elm.style.display = result;
}

function altHTML(elm)
{
	elmt = document.getElementById(elm);
	if(elmt)
	{ 		 
		if(elmt.innerHTML!='')
		{			
			document.elm = elmt.innerHTML;
			elmt.innerHTML = "";
		}
		else
		{			
			elmt.innerHTML = document.elm;
		}
	}
}

function abreJanelaParam(end, params, nm, props)
{
	if(document.nm) document.nm.close();
	if(!document.nm) document.nm = '';
	if (!document.nm.closed && document.nm.location) {
		//document.popup.location.href = end+'?'+params;		
		document.nm.close();
	}
	if(params!='')
		document.nm = window.open(end+'?'+params, nm, props);
	else
		document.nm = window.open(end, nm, props);
	return false;		
}

/*
function abreJanelaParam(end, params, nm, props)
{
	if(!document.popupJImg) document.popupJImg = '';
	if (!document.popupJImg.closed && document.popupJImg.location) {
		//document.popup.location.href = end+'?'+params;		
		document.popupJImg.close();
	}
	document.popupJImg = window.open(end+'?'+params, nm, props);
	return false;		
}
*/

function retornaParamImg(imagePath)
{
	if(window.opener)
	{
		window.opener.rteCommand("rte1", 'InsertImage', imagePath);
		window.close();
	}
}

function trocaItemMenu(obj, n)
{
	iten = document.getElementById('sub'+n);
	//iten.innerHTML.replace("+ ", "- ");
	if(obj.className == "itemMenuPlus")
	{
		obj.className = "itemMenuMinus";
		iten.className = "subItensMinus";
	}
	else
	{
		obj.className = "itemMenuPlus";
		iten.className = "subItensPlus";
	}
}

//FUNÇÕES DE SEÇÕES ESPECÍFICAS

//CATEGORIAS, PEÇAS E CONTROLE DE IMAGENS
