// JavaScript Document
function mostraFlash(src, larg, alt, vars){
	var flash = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+ larg +'" height="'+ alt +'">';
	flash += '<param name="movie" value="'+ src +'" />';	
	flash += '<param name="menu" value="false" />';	
	flash += '<param name="wmode" value="transparent" />';	
	flash += '<embed src="'+ src +'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+ larg +'" height="'+ alt +'" menu = "false" wmode="transparent"></embed>';
	flash += '</object>';	
	document.write(flash);
}

var popUpWin=0;

function popUp(URLStr, width, height) {
	var left = (screen.width) ? (screen.width-width)/2 : 0;
	var top = (screen.height) ? (screen.height-height)/2 : 0;
  	if(popUpWin) {
		if(!popUpWin.closed) popUpWin.close();
	}
	popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}


function isVisible(obj){  //verifica se o elemento está visivel
	if(!obj.parentNode){return false;}
	while(obj.parentNode!=null){
		if(obj.style.display.toLowerCase()=='none' || obj.style.visibility.toLowerCase()=='hidden'){return false;}
		obj=obj.parentNode;
	}
	return true;
}

String.prototype.trim=function(){  //trim leading or trailing whitespace and extra spaces
	return this.replace(/^\s*/, "").replace(/\s*$/, "").replace(/\s{2,}/, " ");
}

function showImg(src, title)
{
  //criando um link falso, para o LightBox captar as informações
  var a_false = document.createElement("A");
  a_false.rel = "lightbox";
  a_false.href = src;
  a_false.title = title;
  //chamando a função do lightbox responsável pelo início das ações
  myLightbox.start(a_false);
  //anulando o link
  a_false = null;
}

function mudaSrc (id, src) {
	document.getElementById(id).src = src;
	return;
}

var mostrandoMenu = false;
function showHide(id) {
	style = document.getElementById(id).style;
	if (style.display=='none') {
		style.display='';
		mostrandoMenu = true;
	} else {
		style.display='none';
		mostrandoMenu = false;
	}
	return;
}


function show(id) {
	style = document.getElementById(id).style;
	style.display='';
	mostrandoMenu = true;
	return;
}
function hide(id) {
	style = document.getElementById(id).style;
	style.display='none';
	mostrandoMenu = false;
	return;
}

var tempo;
function timeOut(funcao, tempo){
	tempo = setTimeout(funcao, parseFloat(tempo));
	clearTimeout(tempo);
}

function checkEmail(valor){
	if(valor==''){return true;}
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){return (true);}
	return (false);
}

function checkdate(d,m,y)
{
	if(!IsNumeric(d) || !IsNumeric(m) || !IsNumeric(y) || d.length<2 || m.length<2 || y.length<4){return false;}
	var yl=1900; // least year to consider
	var ym=2500; // most year to consider
	if (m<1 || m>12) return(false);
	if (d<1 || d>31) return(false);
	if (y<yl || y>ym) return(false);
	if (m==4 || m==6 || m==9 || m==11)
	if (d==31) return(false);
	if (m==2)
	{
		var b=parseInt(y/4);
		if (isNaN(b)) return(false);
		if (d>29) return(false);
		if (d==29 && ((y/4)!=parseInt(y/4))) return(false);
	}
	return(true);
}
function isCnpj(cnpj){
	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
	digitos_iguais = 1;
	if (cnpj.length < 14 && cnpj.length < 15)
		return false;
	for (i = 0; i < cnpj.length - 1; i++)
		if (cnpj.charAt(i) != cnpj.charAt(i + 1))
			  {
			  digitos_iguais = 0;
			  break;
			  }
	if (!digitos_iguais)
		{
		tamanho = cnpj.length - 2
		numeros = cnpj.substring(0,tamanho);
		digitos = cnpj.substring(tamanho);
		soma = 0;
		pos = tamanho - 7;
		for (i = tamanho; i >= 1; i--)
			  {
			  soma += numeros.charAt(tamanho - i) * pos--;
			  if (pos < 2)
					pos = 9;
			  }
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(0))
			  return false;
		tamanho = tamanho + 1;
		numeros = cnpj.substring(0,tamanho);
		soma = 0;
		pos = tamanho - 7;
		for (i = tamanho; i >= 1; i--)
			  {
			  soma += numeros.charAt(tamanho - i) * pos--;
			  if (pos < 2)
					pos = 9;
			  }
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(1))
			  return false;
		return true;
		}
	else
		return false;
} 

function IsNumeric(sText)
{
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}

function isHour(value){
	if(value==''){alert('Favor digite uma hora.');return false;}
	var splitted=value.split(':');
	var hora=splitted[0];
	var minuto=splitted[1];
	var segundo=splitted[2];
	if(!IsNumber(hora) || !IsNumber(minuto) || !IsNumber(segundo) || hora>24 || minuto>59 || segundo>59){return false;}
	return true;
}




function checacpf (cpf)
{
	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 checaCampos(form){  //verifica o preenchimento obrigatorios dos campos antes de enviá-los
	if(!form){return false;}
	var element,enviar=true;
	for(var i=0;i<form.elements.length;i++){
		element=form.elements[i];
		if(element.className.indexOf('required')>=0){  //campo requerido
			if(!isVisible(element)){continue;}
			if(element.type.toUpperCase()=='CHECKBOX'){  //se for um checkbox
				checkboxes=document.getElementsByName(element.name);
				for(var c=0;c<checkboxes.length;c++){
					if(checkboxes[c].checked){break;}
				}
				if(c<checkboxes.length){continue;}
				alert(element.title);
				element.focus();
				return false;
			}
			if(element.value.length<=0){  //nao preenchido
				if(element.type.toUpperCase().indexOf('SELECT')>=0){
					alert(element.title);
				}else{
					if(!isVisible(element)){continue;}
					alert(element.title);
				}
				element.focus();
				enviar=false;
				break;
			}
		}
		if(element.className.indexOf('email')>=0){  //campo tipo email
			if(!checkEmail(element.value.trim())){
				alert(element.title);
				element.focus();
				enviar=false;
				break;
			}
		}
		if(element.className.indexOf('hour')>=0){  //campo tipo hora HH:mm:ss
			if(!isHour(element.value.trim())){
				alert(element.title);
				element.focus();
				enviar=false;
				break;
			}
		}
		if(element.className.indexOf('number')>=0){  //campo tipo numero
			if(!IsNumeric(element.value.trim())){
				alert(element.title);
				element.focus();
				enviar=false;
				break;
			}
		}
		if(element.className.indexOf('cnpj')>=0){  //campo tipo numero
			if(!isCnpj(element.value.trim())){
				alert(element.title);
				element.focus();
				enviar=false;
				break;
			}
		}
		if(element.className.indexOf('cpf')>=0){  //campo tipo numero
			if(!checacpf(element.value.trim())){
				alert(element.title);
				element.focus();
				enviar=false;
				break;
			}
		}
	}
	return enviar;
}

