// JavaScript Document

function AbreJanela(arquivoAJ,nomejanelaAJ,opcoesAJ){
	window.open(arquivoAJ,nomejanelaAJ,'width=1000,height=700,resizable=' + opcoesAJ + ',scrollbars=' + opcoesAJ);
}

function isDate(data){

    pos = InStr(data,"/");
    if (pos=="NotFound"){
        return false;
    }else{
        dia = data.substring(0,pos);
        data = data.substring(pos+1,data.length);
        pos = InStr(data,"/");
        if (pos=="NotFound"){
            return false;
        }else{
          mes = data.substring(0,pos);
          ano = data.substring(pos+1,data.length);
		  if (isNaN(dia)){
			 return false;
		  }else{
			 //dia = parseInt(dia);
			 if (isNaN(mes)){
				    return false;
			 }else{
				    if (isNaN(ano)){
					   return false;
				    }else{
					   //ano = parseInt(ano);
					   //mes = parseInt(mes);
					   if (mes<13 && mes>0){
						  if (mes==01 || mes==03 || mes==05 || mes==07 || mes==08 || mes==10 || mes==12){
							 if (dia<32 && dia>0){
								    return true;
							 }else{
								    return false;
							 }
						  }else{
							 if (mes==04 || mes==06 || mes==09 || mes==11){
								    if (dia<31 && dia>0){
									   return true;
								    }else{
									   return false;
								    }
							 }else{
								    anoint = ano/4;
								    anox = parseInt(ano/4);
								    if (anoint==anox){
									   //bissexto
									   if (dia<30 && dia>0){
										  return true;
									   }else{
										  return false;
									   }
								    }else{
									   //nao bissexto
									   if (dia<29 && dia>0){
										  return true;
									   }else{
										  return false;
									   }
								    }
							 }
						  }
					   }else{
						  //alert(mes);
						  return false;
					   }
                    }
				}
			}
		}
	}
}

function Trim(texto){
	pos = 0;
	tamanho = texto.length;
	
	for (a=0;a<=tamanho;a++){
		if (texto.substring(a,a+1)!=" "){
			pos = a;
		}
	}
	
	if (pos==0){
		pos = tamanho;
	}
	
	for (b=0;b<=pos;b++){
		if (texto.substring(0,1)==" "){
			texto = texto.substring(1,texto.length);
		}
	}
	
	return texto;
}

function InStr(texto,string){
	for(x=0;x<=texto.length-1;x++){
		if (texto.substring(x,x+string.length)==string){
			return x;
		}
	}
	return "NotFound";
}

function InStrRev(texto,string){
	for(x=texto.length;x>0;x--){
		if (texto.substring(x,x+string.length)==string){
			return x;
		}
	}
	return "NotFound";
}

function VerificaValor(vlr,cdecimais){
	pos=InStr(vlr,",");
	if(pos=="NotFound"){
		pos=InStr(vlr,".");
	}
	if (pos!="NotFound"){
		esq = vlr.substring(0,pos);
		dir = vlr.substring(pos+1,vlr.length);
		if (isNaN(esq) || isNaN(dir)){
			return "NotNumber";
		}else{
			if (dir.length>cdecimais){
				dir = dir.substring(0,cdecimais);
			}
			if (dir.length<cdecimais){
				x = dir.length;
				for (a=0;a<cdecimais;a++){
					if (dir.length<cdecimais){
						dir = dir + "0";
					}
				}
			}
			if (Trim(esq)==""){
				esq = "0";
			}
			if (cdecimais>0){
				vlr = esq + "," + dir;
			}else{
				vlr = esq
			}
		}
	}else{
		if (isNaN(vlr)){
			return "NotNumber";
		}
		if (vlr.length==0){
			vlr = "0";
			if (cdecimais>0){
				vlr = vlr + ",";
			}
		}else{
			if (cdecimais>0){
				vlr = vlr + ",";
			}
		}
		for(h=1; h<=cdecimais; h++){
			vlr = vlr + "0";
		}
	}
	return vlr;
}

function isTime(hora){
	if (hora.length>5 || hora.length<5){
		return false;
	}
	pos = InStr(hora,":")
	if (pos=="NotFound"){
		return false;
	}
	hh = hora.substring(0,2);
	mm = hora.substring(3,5);
	posh = InStr(hh,".");
	posm = InStr(mm,".");
	if (posh!="NotFound" || posm!="NotFound"){
		return false;
	}
	if (isNaN(hh) || isNaN(mm)){
		return false;
	}
	if (parseInt(hh)<0 || parseInt(hh)>23){
		return false;
	}
	if (parseInt(mm)<0 || parseInt(mm)>59){
		return false;
	}
	return true;
}

function ValidEMail(mail){
	if (Trim(mail)==""){
		return false;
	}
	posa = InStr(mail,"@");
	if (posa=="NotFound"){
		return false;
	}
	if (posa==mail.length-1){
		return false;
	}
	posp = InStr(mail,".");
	if (posp=="NotFound"){
		return false;
	}
	if (posp==mail.length-1){
		return false;
	}
	pos=InStrRev(mail,".");
	if (posa>=pos){
		return false;
	}
	return true;
}

function Valida(CNUMB,CTYPE){
	var qtde = 0;
	if (CTYPE=='CNPJ'){
		qtde=14;
	}else{
		qtde = 11;
	}
	 if (CNUMB!='' && CNUMB.length==qtde && isNaN(CNUMB)!=true){
		  if (Verify(CNUMB, CTYPE)){
			return true;  
		  }else{
			return false;
		  }
	 }else{
		return false;
	 }
}

function Verify(CNUMB,CTYPE){
  CNUMB=ParseNumb(CNUMB);
  if(CNUMB == 0)
  {
	return(false);
  }
  else
  {
	g=CNUMB.length-2;
	if(TestDigit(CNUMB,CTYPE,g))
	{
	  g=CNUMB.length-1;
	  if(TestDigit(CNUMB,CTYPE,g))
	  {	
		return(true);
	  }
	  else
	  {
		return(false);
	  }
	}
	else
	{
	  return(false);
	}
  }
}

function TestDigit(CNUMB,CTYPE,g)
{
  var dig=0;
  var ind=2;
  for(f=g;f>0;f--)
  {
	dig+=parseInt(CNUMB.charAt(f-1))*ind;
	if (CTYPE=='CNPJ')
	{ if(ind>8) {ind=2} else {ind++} }
	else
	{ ind++ }
  }
  dig%=11;
  if(dig<2)
  {
	dig=0;
  }
  else
  {
	dig=11-dig;
  }
  if(dig!=parseInt(CNUMB.charAt(g)))
  {
	return(false);
  }
  else
  {
	return(true);
  }
}

function ParseNumb(c)
{
  c=ClearStr(c,'-');
  c=ClearStr(c,'/');
  c=ClearStr(c,',');
  c=ClearStr(c,'.');
  c=ClearStr(c,'(');
  c=ClearStr(c,')');
  c=ClearStr(c,' ');
  if((parseFloat(c) / c != 1))
  {
	if(parseFloat(c) * c == 0)
	{
	  return(c);
	}
	else
	{
	  return(0);
	}
  }
  else
  {
	return(c);
  }
}

function ClearStr(str, char)
{
  while((cx=str.indexOf(char))!=-1)
  {		
	str = str.substring(0,cx)+str.substring(cx+1);
  }
  return(str);
}

function TestaPeriodos(data1,data2){
	ano1 = data1.substring(6,10);
	ano2 = data2.substring(6,10);
	mes1 = data1.substring(3,5);
	mes2 = data2.substring(3,5);
	dia1 = data1.substring(0,2);
	dia2 = data2.substring(0,2);
	if (ano2>ano1 || ano2==ano1){
		if (ano2==ano1){
			if (mes2>mes1 || mes2==mes1){
				if (mes2==mes1){
					if (dia2>dia1 || dia2==dia1){
						return true;
					}else{
						return false;
					}
				}else{
					return true;
				}
			}else{
				return false;
			}
		}else{
			return true;
		}
	}else{
		return false;
	}
}

// _z.jpg retorna a imagem com 640 pixels de largura
// _m.jpg retorna o thumbnail da imagem
// .jpg retorna a imagem com 500 pixels de largura

function FlickrShow(idSet, idUser, container, showThumb, showAll, showLink){
	
		$.getJSON("http://api.flickr.com/services/feeds/photoset.gne?set=" + idSet + "&nsid=" + idUser + "&lang=pt-br&format=json&jsoncallback=?", function(data){
			
		if (showAll==true){

			$.each(data.items, function(i,item){
				
				if (showThumb==true){
					image = item.media.m;
				}else{
					image = item.media.m.replace("_m.jpg",".jpg");
				}
				
				if (showLink==true){
					cont = "<a href='" + data.link + "' target='_blank'><img src='" + image + "' border='0'></a>";
				}else{
					cont = "<img src='" + image + "' border='0'>"
				}
				
				$(container).append(cont);
												
			});
			
		}else{

			if (showThumb==true){
				image = data.items[0].media.m;
			}else{
				image = data.items[0].media.m.replace("_m.jpg",".jpg");
			}
			
			
			if (showLink==true){
				cont = "<a href='" + data.link + "' target='_blank'><img src='" + image + "' border='0'></a>";
			}else{
				cont = "<img src='" + image + "' border='0'>"
			}
			
			$(container).append(cont);

			
		}
	});
};

function FlickrShowBackground(idSet, idUser, container){
	
		$.getJSON("http://api.flickr.com/services/feeds/photoset.gne?set=" + idSet + "&nsid=" + idUser + "&lang=pt-br&format=json&jsoncallback=?", function(data){

		image = data.items[0].media.m.replace("_m.jpg","_z.jpg");
		
		$(container).css("background-image", "url(" + image + ")");

	});
};

function FlickrShowGaleria(idSet, idUser, container){
	
		$.getJSON("http://api.flickr.com/services/feeds/photoset.gne?set=" + idSet + "&nsid=" + idUser + "&lang=pt-br&format=json&jsoncallback=?", function(data){

		$(container).html("<img src='" + data.items[0].media.m + "'>");

	});
};


function LinksCompartilhar(div){
	$(div).html("<div id='Compartilhar'>"
					+ "<div id='Compartilhe'>Compartilhe: </div>"
					+ "<a href='http://promote.orkut.com/preview?nt=orkut.com&du="
					+ encodeURIComponent(window.location.href)
					+ "&tt=" + $(".Titulo").text()
					+ " : Leandro Balardin' title='Compartilhe no Orkut' target='_blank' class='Orkut'>Orkut</a> "
					+ "<a href='http://twitter.com/home?status="
					+ encodeURIComponent(window.location.href)
					+ " @leandrobalardin' title='Compartilhe no Twitter' target='_blank' class='Twitter'>Twitter</a> "
					+ "<a href='http://www.facebook.com/sharer.php?u=" 
					+ encodeURIComponent(window.location.href)
					+ "' title='Compartilhe no Facebook' target='_blank' class='Facebook'>Facebook</a>"
					+ "</div>"
					+ "<a href='#' onClick='window.history.back(); return false;' class='Voltar'>&lt; voltar</a>");
}

