// JavaScript Document
var ultimo = "";

function verFoto(foto, w, h, op)
{	w = 500; h = 375;

	var pic = window.open('','','width=' + w + ',height=' + h);
	var fotoStr = '';	

	if (foto.src.indexOf("aspx") > 0)
	{	var inicio = foto.src.indexOf("=") + 1;
		var fim = foto.src.indexOf("&");
		foto = foto.src.substr(inicio, fim - inicio)
		fotoStr = '<img src="/thumb.aspx?foto=' + foto + '&w=' + w + '&h=' + h + '&op=' + op + '" border="0" alt="Fechar Janela">';
	}
	else
	{	fotoStr = '<img src="' + foto.src.replace('mini/','') + '" border="0" alt="Fechar Janela">';	}
	
	pic.document.write('<html><body onblur="self.close();" topmargin="0" leftmargin="0" oncontextmenu="return false" ondragstart="return false">');
	pic.document.write('<a href="javascript:window.close()">');
	pic.document.write(fotoStr);
	pic.document.write('</a></body></html>');
}

/*****************************************************************************************************
*****								   EDIÇÃO E INCLUSÃO DE TEXTOS								 *****
*****************************************************************************************************/
function salvarTexto(obj)
{	obj.txttexto.value = document.getElementById('field_body').contentWindow.document.body.innerHTML;
	return true;
}

function editarTexto()
{	var rte = document.getElementById('field_body');
	var conteudo = document.getElementById('txttexto').value;
	
	rte.contentWindow.document.body.innerHTML = conteudo
}


/*****************************************************************************************************
*****										   DATA E HORA										 *****
*****************************************************************************************************/
function data()
{	var mdata, mdiasemana, diasemana, ano, mmes, mes, dia;
	
	mdata = new Date();
	mdiasemana = mdata.getDay();
	mmes = mdata.getMonth();
	dia = mdata.getDate();
	ano = mdata.getFullYear();
	
	// Escolhendo dia semana
	switch (mdiasemana)
	{	case 0 :	diasemana = "Domingo";	break;
		case 1 :	diasemana = "Segunda";	break;
		case 2 :	diasemana = "Terça";	break;
		case 3 :	diasemana = "Quarta";	break;
		case 4 :	diasemana = "Quinta";	break;
		case 5 :	diasemana = "Sexta";	break;
		case 6 :	diasemana = "Sábado";	break;
	}

	// Escolhendo mês
	switch (mmes)
	{	case 0 :	mes = "Janeiro";	break;
		case 1 :	mes = "Fevereiro";	break;
		case 2 :	mes = "Março";		break;
		case 3 :	mes = "Abril";		break;
		case 4 :	mes = "Maio";		break;
		case 5 :	mes = "Junho";		break;
		case 6 :	mes = "Julho";		break;
		case 7 :	mes = "Agosto";		break;
		case 8 :	mes = "Setembro";	break;
		case 9 :	mes = "Outubro";	break;
		case 10 :	mes = "Novembro";	break;
		case 11 :	mes = "Dezembro";	break;
	}
	
	// Arrumando dia
	if (dia < 10)
		dia = "0" + dia;
	
	document.write(diasemana + ", " + dia + " de " + mes + " de " + ano);
}


/*****************************************************************************************************
*****										   FUNÇÕES AJAX										 *****
*****************************************************************************************************/
function favoritos(elem, foto, qtdade)
{	var caminho = elem.src.substring(0,elem.src.lastIndexOf("/") + 1);
	var inicio = elem.src.lastIndexOf("/") + 1;
	var fim = elem.src.length;

	//Zerando favoritos
	for (x=0; x < qtdade; x++)
	{	try
		{ document.getElementById("datalistfotos__ctl" + x + "_fav").src = caminho + "nk.gif";	}
		catch (e)
		{}
	}
	
	if (elem.src.substring(inicio, fim) == "nk.gif")
	{	var ref = foto.substr(0, foto.indexOf("_"));
	
		elem.src = caminho + "ok.gif";
		
		var resposta = func_ajax.atualiza(ref, foto).value;
	
		if (!resposta)
			alert("Houve um erro na atualização da referencia.");
	}
}

function apagar(foto)
{	var ref = foto.substr(0, foto.indexOf("_"));
	var resposta = func_ajax.apagarFoto(foto, ref).value;
	
	if (resposta)
	{	setInterval("location.href = location.href;", 500);	}
	else
	{	alert("Ocorreu um erro ao tentar apagar esta imagem.");	}
}

function verificaRef(obj)
{	var resposta = func_ajax.verificaRef(obj.value).value;
	
	if (resposta)
	{	alert("Já existe um produto cadastrado com esta referência!");
		obj.focus();
		obj.select(); }
}


/*****************************************************************************************************
*****										   VALIDAÇÕES										 *****
*****************************************************************************************************/
function verificaCPF(source, arguments)
{	var elem = document.getElementById("txtcpf");
	
	if (validaCPF(elem.value))
	{	arguments.IsValid = true;	}
	else
	{	arguments.IsValid = false;	}
}

function verificaCNPJ(source, arguments)
{	var elem = document.getElementById("txtcnpj");
	
	if (VerifyCNPJ(elem.value))
	{	arguments.IsValid = true;	}
	else
	{	arguments.IsValid = false;	}
}

function validaCPF(CPF)
{	var charinvalidos = '-/,.() ';	

	for (var i=0; i<=6; i++)
	{	while (( cx = CPF.indexOf(charinvalidos.substring(i, i + 1) )) != -1)
		{	CPF = CPF.substring(0,cx) + CPF.substring(cx+1);	}
	}
	
	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
		CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
		CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
		CPF == "88888888888" || CPF == "99999999999")
	{	return false;	}		
		
	soma = 0;
	
	for (i=0; i < 9; i ++)
	{	soma += parseInt(CPF.charAt(i)) * (10 - i);	}
	
	resto = 11 - (soma % 11);
	
	if (resto == 10 || resto == 11)
	{	resto = 0;	}
	
	if (resto != parseInt(CPF.charAt(9)))
	{	return false;	}
	
	soma = 0;
	
	for (i = 0; i < 10; i ++)
	{	soma += parseInt(CPF.charAt(i)) * (11 - i);	}
	
	resto = 11 - (soma % 11);
	
	if (resto == 10 || resto == 11)
	{	resto = 0;	}
	
	if (resto != parseInt(CPF.charAt(10)))
	{	return false;	}
	
	return true;

 }
 
function VerifyCNPJ(CNPJ)
{	var charinvalidos = '-/,.() ';	

	for (var i=0; i<=6; i++)
	{	while (( cx=CNPJ.indexOf( charinvalidos.substring(i,i+1) ))!=-1)
		{	CNPJ = CNPJ.substring(0,cx)+CNPJ.substring(cx+1);	}
	}
	
	if (isNUMB(CNPJ) != 1)
	{	return(0);	}
	else
	{	if(CNPJ == 0)
		{	return(0);	}
		else
		{	g = CNPJ.length-2;
		
			if(RealTestaCNPJ(CNPJ,g) == 1)
			{	g = CNPJ.length-1;
			
				if (RealTestaCNPJ(CNPJ,g) == 1)
				{	return(1);	} 
				else
				{	return(0); 	}
			} 
			else
			{	return(0); 	}
		}
	}
}
		
function RealTestaCNPJ(CNPJ,g)
{	var VerCNPJ=0;
	var ind=2;
	var tam;
	
	for (f=g;f>0;f--)
	{	VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
		
		if(ind>8)
		{	ind=2;	}
		else
		{	ind++;	}
	}
	
	VerCNPJ %= 11;
	
	if (VerCNPJ==0 || VerCNPJ==1)
	{	VerCNPJ=0;	}
	else
	{	VerCNPJ=11-VerCNPJ;	}
	
	if (VerCNPJ!=parseInt(CNPJ.charAt(g)))
	{	return(0);	}
	else
	{	return(1);	}
}

function isNUMB(c)
{ 	if ((cx=c.indexOf(","))!=-1)
	{ 	c = c.substring(0,cx) + "." + c.substring(cx+1);	} 
	
	if ((parseFloat(c) / c != 1))
	{ 	if (parseFloat(c) * c == 0)
		{	return(1); 	} 
		else
		{ 	return(0); 	} 
	}
	else
	{	return(1); 	} 
}


/*****************************************************************************************************
*****									FORMATAÇÕES E BLOQUEIOS									 *****
*****************************************************************************************************/
function soNumeros()
{	var tecla = window.event.keyCode;

	if (tecla < 48 || tecla > 57)
	{	window.event.returnValue = false;	}
}

function mascaraFone(t)
{	var tecla = window.event.keyCode;

	if (tecla < 48 || tecla > 57 && tecla < 96 || tecla > 105)
			return false;
	
	tam = t.value.length;
	
	if (tecla != 8 && tecla != 46)
	{	if (tam == 1)
			t.value =  '(' + t.value;
		if (tam == 3 )
			t.value = t.value + ') ';
		if (tam == 9 )
			t.value = t.value + '-';
	}
}

function mascaraCEP(t)
{	var tecla = window.event.keyCode;

	if (tecla < 48 || (tecla > 57 && tecla < 96) || tecla > 105)
			return false;
			
	if (tecla != 8 && tecla != 46)
	{	if (t.value.length == 5)
			t.value = t.value + '-';
	}
}

function mascaraCPF(t)
{	var tecla = window.event.keyCode;

	if (tecla != 8 && tecla != 46)
	{	if (t.value.length == 3)
			t.value = t.value + '.';
		else if (t.value.length == 7)
			t.value = t.value + '.';
		else if (t.value.length == 11)
			t.value = t.value + '-';
	}
}

function mascaraCNPJ(t)
{	var tecla = window.event.keyCode;

	if (tecla != 8 && tecla != 46)
	{	if (t.value.length == 3)
			t.value = t.value + '.';
		else if (t.value.length == 7)
			t.value = t.value + '.';
		else if (t.value.length == 11)
			t.value = t.value +'/';
		else if (t.value.length == 16)
			t.value = t.value + '-';					
	}
}

function mascaraReal(campo)
{	ie =  document.all ? true : false;

	if (ie)
		FormataValorIE(campo);
      else
		FormataValorNS(campo);
}

function FormataValorNS(campo)
{	campo.value = FiltraCampo(campo);
	
	vr = campo.value;
			
	if (vr.length == 0)
	{	campo.value = "0,00";	}
	
	while (vr.substr(0, 1) == 0)
	{	if (vr == "0,00")
			break;
		vr = vr.substr(1, campo.value.length - 1);	}
	
	campo.value = vr;
	tam = vr.length;

	if ( tam <= 2 ) 
		campo.value = tam == 1 ? "0,0" + vr.substr(tam - 1, 1) : "0," + vr.substr(tam - 2, 2); 
 	if ( (tam > 2) && (tam <= 5) ) 
 		campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; 
 	if ( (tam >= 6) && (tam <= 8) ) 
 		campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; 
 	if ( (tam >= 9) && (tam <= 11) ) 
 		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) ) 
 		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) )
 		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 FormataValorIE(campo)
{	var tecla = window.event.keyCode;

	if (tecla < 48 || tecla > 57 && tecla < 96 || tecla > 105)
		if (tecla != 8 && tecla != 46)
			return false;
	vr = campo.value;
			
	if (vr == "")
	{	campo.value = "0,00";
		vr = campo.value;	}
	
	while (vr.substr(0, 1) == 0)
	{	if (vr == "0,00" || vr == "")
			break;

	vr = vr.substr(1, campo.value.length - 1);	}
	
	campo.value = vr;
	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 <= 2 )
	{	campo.value = tam == 1 ? "0,0" + vr.substr(tam - 1, 1) : "0," + vr.substr(tam - 2, 2); }
	if ( (tam > 2) && (tam <= 5) )
	{	campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }
	if ( (tam >= 6) && (tam <= 8) )
	{	campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
	if ( (tam >= 9) && (tam <= 11) )
	{	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) )
	{	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) )
	{	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 );	}
}


/*****************************************************************************************************
*****									   OUTRAS FORMATAÇÕES									 *****
*****************************************************************************************************/
function validarExtensao(obj)
{	var retorno = 0;
	var arrImagens = new Array(1);
	arrImagens[0] = '.JPG'; //aki vc coloca as extensões que o arquivo pode aceitar
	//arrImagens[1] = '.GIF'; //aki vc coloca as extensões que o arquivo pode aceitar

	var extensao = null;
      
	if (obj.value == '')	//document.forms[0].elements[campo].value == '')
	{	return false; }
	else
	{	extensao = obj.value.substring(obj.value.lastIndexOf(".")).toUpperCase();
		//extensao = document.forms[0].elements[campo].value.substring(document.forms[0].elements[campo].value.lastIndexOf(".")).toUpperCase();
	
		for(i = 0; i < arrImagens.length; i++)
		{	if (extensao == arrImagens[i])
				retorno += 1;
		}     
                                 
  		if (retorno == 0)
		{	alert("Utilize imagens com extensão .JPG");
			document.forms[0].onsubmit = function() { return validarExtensao(obj); };
			obj.select();
			return false; }
		else
		{	return true; }
	}     
}

