/*
*	Variáveis
*/
PAGINADEFAULT = "Default.aspx";
PAGINADEFAULTEVENTO = "Default.aspx?Page=Evento.ascx";
PAGINADEFAULTGERAL = "Default.aspx?Page=Eventos.ascx";


/*
* Acerta Focus no IE
*/
/*baFocus = function() {
	var sfEls = document.getElementsByTagName("INPUT");
	for (var i=0; i<sfEls.length; i++) 
	{
		sfEls[i].onfocus=function() {
			this.className+=" bafocus";
		}
		sfEls[i].onblur=function() {
			this.className=this.className.replace(new RegExp(" bafocus\\b"), "");
		}
	}
	var sfEls = document.getElementsByTagName("SELECT");
	for (var i=0; i<sfEls.length; i++) 
	{
		sfEls[i].onfocus=function() {
			this.className+=" bafocus";
		}
		sfEls[i].onblur=function() {
			this.className=this.className.replace(new RegExp(" bafocus\\b"), "");
		}
	}

}
if (window.attachEvent) window.attachEvent("onload", baFocus);
*/


function teste()
{
	alert("teste");
}
/*
* Variaveis Globais
*/
//var _localimagens = '//testesrv/Inetpub/wwwroot/Internet/Imagens/Imagens/Noticias/' ;


/*
*
*/
function trim(str)
{
	return str.replace(/^\s*|\s*$/g,"");
}

/*
* Recupera o Id correto de um controle
* funcao modificada em 13/12/2006 por adailton para acertar commando javascript 
* com o netscape/firefox que nao aceita documento.all
*/
	function FindControlId(id_parametro)
	{	

	if (document.getElementById(id_parametro) == null)
	{
		var alltags=document.all? document.all : document.getElementsByTagName("*");
		//for(i in document.all)
//		alert(alltags.length);
		var i = 0;
		for (i=0; i < alltags.length-1; i++)
		//for (element in alltags);
		{
//			alert(alltags[i].id);
			//texto += "[ "+i+" ] - ";
			//name = new String(i);
			name = new String(alltags[i].id);
			//alert(element);
			//name = new String(element);
			if (name.indexOf(id_parametro) > 0)
			{
//				alert("substring = "+name.substring(name.indexOf("fontechange"),name.length))
				achou = name.substring(name.indexOf(id_parametro),name.length);
				if (achou == id_parametro)
				{
//					alert(alltags[i].id);
//					//alert("achou = "+i);
//					//id = i;
//					//alert(i);
//					//return i;
					return alltags[i].id;
//					return element;
				}
			}
		}
	
	}
	else
	{
		return id_parametro;
	}
	
}


//function FindControlId(id_parametro)
//{	

//	return "teste";	
/*
	if (document.getElementById(id_parametro) == null)
	{
		var alltags=document.all? document.all : document.getElementsByTagName("*");
		for (i=0; i<alltags.length; i++)
		//for(i in document.all)
		{
			//texto += "[ "+i+" ] - ";
			//name = new String(i);
			name = new String(alltags[i].id);
			if (name.indexOf(id_parametro) > 0)
			{
			//alert("substring = "+name.substring(name.indexOf("fontechange"),name.length))
				achou = name.substring(name.indexOf(id_parametro),name.length);
				if (achou == id_parametro)
				{
					//alert("achou = "+i);
					//id = i;
					//alert(i);
					//return i;
					return alltags[i].id;
				}
			}
		}
	}
	else
	{
		return id_parametro;
	}
*/	
//}

/*
* Recupera o Id correto de um controle
*/
function FindControlName(id_parametro)
{	
	
	if (document.getElementByName(id_parametro) == null)
	{		
		for(i in document.all)
		{
			//texto += "[ "+i+" ] - ";
			name = new String(i);
			if (name.indexOf(id_parametro) > 0)
			{
			//alert("substring = "+name.substring(name.indexOf("fontechange"),name.length))
				achou = name.substring(name.indexOf(id_parametro),name.length);
				if (achou == id_parametro)
				{
					//alert("achou = "+i);
					//id = i;
					//alert(i);
					return i;
				}
			}
		}
	}
	else
	{
		return id_parametro;
	}
}

function pickDateControl(controle)
{
	//alert(controle);
	window.open("Componentes/CalendarDialog.aspx?src=" + controle, "_blank", "height=260, width=250, left=100, top=100, " +
	"location=no, menubar=no, resizable=no, " +
	"scrollbars=no, titlebar=no, toolbar=no", true);

}

function pickDate(Src)
{
		//alert(src);
		window.open("Componentes/CalendarDialog.aspx?src=" + Src, "_blank", "height=260, width=250, left=100, top=100, " +
		"location=no, menubar=no, resizable=no, " +
		"scrollbars=no, titlebar=no, toolbar=no", true);
		
		//document.all[Src].innerText = "testando";
		//alert(document.all[Src].innerText);
}


function maskTime(controle)
{
	//alert(controle);
	//alert(document.all(controle).Text);
	//alert(document.all[controle].innertext);
	//var len = document.all[controle].inne..value.length;
    //for(var i=0,a='';i<len;i++) {
    //  a += "x";
    //}
    //controle.setAttribute('hidVal',controle.value);
    //controle.value = a; 
}

var _verAnoMaiorAtual = true; 
var _Message = 'Data invalida, favor corrigir a data.'; 
var _MessageAnoMaior = 'Ano maior que ano atual.';

/*
* Ativado quando usuário clica dentro do campo de edição de uma data
*/
function campodata_OnKeyDown(textboxID, evento)
{
	//alert(evento);
	var caractere = (evento.which) ? evento.which : event.keyCode ;
	//alert(caractere);
	if (caractere > 31)
	{
		if (caractere < 48)
			return false;
		if (caractere > 57)
			if (caractere < 96 || caractere > 105)
				return false;
	}
	return true ;
}

/*
* Ativado quando usuário libera a tecla de edição de uma data
*/
function campodata_OnKeyUp(textboxID)
{
	var campo = document.getElementById(FindControlId(textboxID + ''));
	//alert(campo);
	if (( campo.value.length == 2 ) && ( window.event.keyCode != 8 ) )
	campo.value  = campo.value + '/'  ;
	if (( campo.value.length == 5 ) && ( window.event.keyCode != 8 ) )
	campo.value  = campo.value + '/'  ;
}

/*
* Ativado quando usuário sai deum campo de data
*/
function validaCampoData(textboxID) 
{		
	var oDate = document.all[FindControlId(textboxID)];		
	//var oDate = comp;	
	//alert(oDate);
	var sDate = oDate.value;
	if (sDate == '') 
	return true;
	var iDay, iMonth, iYear;
	var arrValues;
	var today = new Date();
			
	arrValues = sDate.split('/');				
	iDay = arrValues[0];
	iMonth = arrValues[1];
	iYear = arrValues[2];		
	if ((iMonth == null) || (iYear == null))
	{
		alert(_Message);
		oDate.focus();
		return false;			
	}
	if ((iDay > 31) || (iMonth > 12) || (iYear < 1900 )) 
	{					
		alert(_Message);
		oDate.focus();
		return false;
	}
	if (!_verAnoMaiorAtual && (iYear > today.getFullYear())) 
	{
		alert(_MessageAnoMaior);
		oDate.focus();
		return false;
	}
			
	var dummyDate = new Date(iYear, iMonth - 1, iDay);
			
	if ((dummyDate.getDate() != iDay) || (dummyDate.getMonth() != iMonth - 1) || (dummyDate.getFullYear() != iYear)) 
	{
		alert(_Message);
		oDate.focus();
		return false;		
	}
	return true;			
}



/*
* Script para fazer preview da imagem carregada
*
*/
	// width to resize large images to
	var maxWidth=100;
    // height to resize large images to
	var maxHeight=100;
	// valid file types
	var fileTypes=["bmp","gif","png","jpg","jpeg"];
	// what to display when the image is not valid
	var defaultPic="spacer.gif";
	// the id of the preview image tag
	//var outImage="previewField";
	var outImage=defaultPic;

function setcampo(what, outImg, campo)
{
	//alert('');
	//alert(_localimagens);
	var source=what.value;
	var cp = document.getElementById(FindControlId(campo));
	cp.value = source; 
//	alert(cp);
//	alert(cp.id);
	//cp.onchange(cp.id, outImg, campo);
	preview(cp.id, outImg, campo);
}

setTimeout("applyChanges()",200);
	
function preview(what, outImg, campo)
{  
  outImage = outImg;
  //var source=what.value;
  var obj = document.getElementById(FindControlId(what))
//  alert(what);
//  alert(obj);
  var source= obj.value;
//  alert(source);
 // alert("["+trim(source)+"]");
  if (source != "")
  {
	var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
	for (var i=0; i<fileTypes.length; i++) if (fileTypes[i]==ext) break;
	globalPic=new Image();
	if (i<fileTypes.length)
	{
	//	alert(source.indexOf("\\"));
		if (source.indexOf("\\") < 1)
		{
			globalPic.src=_localimagens_preview + source;
		}
		else
		{
			globalPic.src=source;	
		}
	//	alert(globalPic.src);
		var cp = document.getElementById(FindControlId(campo));
		//alert(source);
		cp.value = source; 
	//	alert(cp.value);
		applyChanges();
	}
	else {
		globalPic.src=defaultPic;
		alert("Essa nao e uma imagem valida\nPor favor, selecione uma imagem com uma das extensoes a seguir:\n\n"+fileTypes.join(", "));
	}
	//setTimeout("applyChanges()",200);
  }
}
var globalPic;
function applyChanges()
{
	var field=document.getElementById(outImage);
	//alert(field);
	if (field == null)
	{
	}
	else
	{
		var x=parseInt(globalPic.width);
		var y=parseInt(globalPic.height);
  		if (x>maxWidth) {
			y*=maxWidth/x;
			x=maxWidth;
		}
		if (y>maxHeight) 
		{
			x*=maxHeight/y;
			y=maxHeight;
		}
		field.style.display=(x<1 || y<1)?"none":"";
		field.src=globalPic.src;
		field.width=x;
		field.height=y;
	}
}

function waitimage(_top, _left, _mensagem)
{
	_retorno = "";
	_retorno = "<span style='position:absolute;top:"+top+";left:"+_left+";'>"
		     + "<table border='0' style='background-color:#FFFFCC;border-width:1px;border-color:black;border-style:solid;font-size:10;'>"
			 + "<tr>"
			 + "<td>"
			 + "<br>"
			 + "<Center><img src='Imagens\\wait30trans.gif'><br>&nbsp;"+_mensagem+"</Center>"
			 + "<br>"
			 + "</td>"
			 + "</tr>"
			 + "</table>"
			 + "</span>";

	return _retorno;
}

function findPosTop(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curtop];
}

function findPosLeft(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft];
}

/*
* Função utilizada para Calculo da Idade
*/
function calculaIdade(data,dataHoje) 
{
	x = data.split("/");
	h = dataHoje.split("/");

	anosProvisorio = h[2] - x[2];

	if(h[1] < x[1]) 
	{
		anosProvisorio -= 1;
	}
	else if(h[1] == x[1]) 
	{
		if(h[0] < x[0]) 
		{
			anosProvisorio -= 1;
		}
	}

	return anosProvisorio;
}

function CalcIdade(dataNasc) {
    var dataAtual = new Date();
    var anoAtual = dataAtual.getFullYear();
    var anoNascParts = dataNasc.split('/');
    var diaNasc = anoNascParts[0];
    var mesNasc = anoNascParts[1];
    var anoNasc = anoNascParts[2];
    var idade = anoAtual - anoNasc;
    var mesAtual = dataAtual.getMonth() + 1;
    //se mês atual for menor que o nascimento, nao fez aniversario ainda; (03/10/2009) 
    if (mesAtual < mesNasc) {
        idade--;
    } else {
        //se estiver no mes do nasc, verificar o dia
        if (mesAtual == mesNasc) {
            if (dataAtual.getDate() < diaNasc) {
                //se a data atual for menor que o dia de nascimento ele ainda nao fez aniversario
                idade--;
            }
        }
    }
    return idade;
}

