// JavaScript Document

function ajaxInit() {
	
	var xmlhttp;
	
	try {
		xmlhttp = new XMLHttpRequest();
	} catch(ee) {
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(E) {
				xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}


//---------------CPF--------------------
	function valida_cpf(obj,erro){
		var cpf=obj.value;
		if(cpf.length==11){
			for(var i=0; i<cpf.length; i++){
				if(isNaN(cpf.charAt(i))==true){
					return false;
				}
			}
			cpf = obj.value=obj.value.substr(0,3)+"."+obj.value.substr(3,3)+"."+obj.value.substr(6,3)+"-"+obj.value.substr(9);
		}else if(cpf.length==14){
			obj.value=obj.value.substr(0,3)+"."+obj.value.substr(4,3)+"."+obj.value.substr(8,3)+"-"+obj.value.substr(12);
			cpf = obj.value;
		} else{
			return false;			
		}
		if (cpf.length<14 || cpf.charAt(3)!="." || cpf.charAt(7)!="." || cpf.charAt(11)!="-" || cpf=="000.000.000-00" || cpf=="111.111.111-11" || cpf=="222.222.222-22" || cpf=="333.333.333-33" || cpf=="444.444.444-44" || cpf=="555.555.555-55" || cpf=="666.666.666-66" || cpf=="777.777.777-77" || cpf=="888.888.888-88" || cpf=="999.999.999-99"){
			return false;
		} else {
		// Primeiro Calculo
		calculo1 = new Array(13)
			for (i=0; i<=13; i++){
				calculo1[i]=cpf.charAt(i);
			} 
			valor_cal=(calculo1[0]*10)+(calculo1[1]*9)+(calculo1[2]*8)+(calculo1[4]*7)+(calculo1[5]*6)+(calculo1[6]*5)+(calculo1[8]*4)+(calculo1[9]*3)+(calculo1[10]*2);
			valor_cal=valor_cal%11;
				if (valor_cal<2){
					valor_cal=0;
				} else {
					valor_cal=11-valor_cal;
				}
				
				
			// Segundo Calculo
			calculo2 = new Array(10)
			for (i=0; i<=10; i++){
				calculo2[i]=cpf.charAt(i);
			} 
			valor_cal2=(calculo2[0]*11)+(calculo2[1]*10)+(calculo2[2]*9)+(calculo2[4]*8)+(calculo2[5]*7)+(calculo2[6]*6)+(calculo2[8]*5)+(calculo2[9]*4)+(calculo2[10]*3)+(valor_cal*2);
			valor_cal2=valor_cal2%11;
			if (valor_cal2<2){
				valor_cal2=0;
			} else {
				valor_cal2=11-valor_cal2;
			}
			if (valor_cal == calculo1[12] && valor_cal2 == calculo1[13]){
				for(var i=0; i<cpf.length; i++){
					if(isNaN(cpf.charAt(i))==true && i!=3 && i!=7 && i!=11){
						return false;
					}
				}			
			} else {
				return false;
			}
		}
	}
	//---------------Final CPF--------------
	
	
	
	//------------Telefone------------------
	function valida_telefone(obj){
		var tel = obj.value;
		if(tel.length==10){
			for(var i=0; i<tel.length; i++){
				if(isNaN(tel.charAt(i))==true){
					return false;
				}
			}
			obj.value="("+obj.value.substr(0,2)+") "+obj.value.substr(2,4)+"-"+obj.value.substr(6);
			tel = obj.value;
		}else if(tel.length==14){
			obj.value="("+obj.value.substr(1,2)+") "+obj.value.substr(5,4)+"-"+obj.value.substr(10,4);
			tel = obj.value;
		} else{
			return false;			
		}
		if (tel.length<14 || tel.charAt(4)!=" " || tel.charAt(9)!="-" || tel.charAt(0)!="(" || tel.charAt(3)!=")"){
			return false;
		} else {
			for(var i=0; i<tel.length; i++){
				if(isNaN(tel.charAt(i))==true && i!=0 && i!=3 && i!=4 && i!=9){
					return false;
				}
			}
		}
	}
	//------------Final Telefone------------
	
	//------------Quantidade de Caracteres Números------------------
	function valida_length(obj, maior, menor){
		var campo = obj.value;
		if (campo.length<menor || campo.length>maior){
			return false;
		} else {
			return true;
		}
	}
	//------------Final Quantidade de Caracteres Números------------
	
	//---------Emal--------------------
	function valida_email(obj){
		var email = obj.value;
		obj.value=email.toLowerCase();
		email = obj.value;
		var comando = /^[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9-]+\.[a-z0-9-]+(\.[a-z0-9-]+)*$/;
		var valida = comando.exec(email);
		if (!valida){
			return false;
		}
	}
	//-----------Final Email----------------


//------Somente Números-----------------------		
function numeros(e){
	var tecla=new Number();
	if(window.event) {
		tecla = e.keyCode; 
	} else if(e.which) {
		tecla = e.which; 
	} else {
		return true;
	}
	
	if(((tecla < 48) || (tecla > 57)) && (tecla!=8)){
		return false;
	}
	
}
//-------------Final Somente Números----------------


//-------Pontuação Telefone-------------
function traco_tel(obj, e){			
	var tecla=new Number();
	if(window.event) {
		tecla = e.keyCode;
	} else if(e.which) {
		tecla = e.which;
	} else {
		return true;
	}
	
	if (tel.length==4 && tecla!=8){
		obj.value=tel+"-";
	}
}
//-------Final Pontuação Telefone-------------

//----------pontuação do CPF----------------------
function pontuacao_cpf(obj, e){
	var tecla=new Number();
	if(window.event) {
		tecla = e.keyCode;
	} else if(e.which) {
		tecla = e.which;
	} else {
		return true;
	}
	
	cpf=obj.value;
	if ((cpf.length==3 || cpf.length==7) && tecla!=8){
		obj.value=cpf+".";
	}
	if (cpf.length==11 && tecla!=8){
		obj.value=cpf+"-";
	}
}

//----------Final pontuação do CPF----------------------

//================================ Checa Radio =========================================

function check_radio(obj){
	var checked=0;
	for(var i=0; i<obj.length; i++){
		if (obj[i].checked==true){
			return obj[i].value;
			break;
		}
	}
	return false;
}

//================================ Final Checa Radio ====================================


//================================ Checa Radio =========================================

function uncheck_radio(obj){
	for(var i=0; i<obj.length; i++){
		obj[i].checked=false;
	}
}

//================================ Final Checa Radio ====================================

//===================================== Checa Vendedor ==============================================
function checa_indicador(obj){
	if(check_radio(obj)==1){
		document.getElementById('Indicador').style.display="block";
	} else {
		document.getElementById('Indicador').style.display="none";
	}
}
//===================================== Checa Vendedor ==============================================

//===================================== Checa Vendedor ==============================================
function checa_segmento(obj){
	if(obj.value=="1" || obj.value=="8" || obj.value=="22" || obj.value=="2"){
		var lis=document.getElementById('cursos').getElementsByTagName("li");
		for(var i=0; i<lis.length; i++){
			lis[i].style.display="none";
		}
		document.getElementById(obj.value).style.display="block";
	}
}
//===================================== Checa Vendedor ==============================================

//===================================== Checa Formulário ==============================================

function checar_cartao(obj){
	checa_indicador(document.form.opt_ind);
}

//================================== Final Checa Formulário ===========================================
var animacao = 0;
function valida_cartao(obj){
	var erro = 0
	var select_curso=0;
	var desc_erro = new Array();
	
	if (check_radio(obj.opt_ind)==1){
		if (valida_length(obj.nome_indicador,1200,4)==false){
			desc_erro[erro]="Digite o nome de seu Indicador corretamente!";
			erro = erro + 1;
		}
		
		if (check_radio(obj.tipo_indicador)==false){
			desc_erro[erro]="Selecione se seu indicador é Aluno ou Funcionário ou Professor.";
			erro = erro + 1;
		}
	}
	
	/* if (valida_cpf(obj.cpf)==false){
		desc_erro[erro]="Digite seu CPF Corretamente!";
		erro = erro + 1;
	}
	
	if(obj.segmento.value!="1" && obj.segmento.value!="8" && obj.segmento.value!="22" && obj.segmento.value!="2"){
		desc_erro[erro]="Escolha um segmento: Graduação ou Pós-graduação ou EJA/Supletivo ou CAS/ITS";
		erro = erro + 1;
		select_curso=1;
	} else {
		if(obj.segmento.value=="1" && obj.curso_graducao.value==""){
			desc_erro[erro]="Escolha algum curso de Graduação";
			erro = erro + 1;
		}
		
		if(obj.segmento.value=="8" && obj.curso_pos.value==""){
			desc_erro[erro]="Escolha algum curso de Pós-graduação";
			erro = erro + 1;
		}
		
		if(obj.segmento.value=="22" && obj.curso_eja.value==""){
			desc_erro[erro]="Escolha algum curso do Eja/Supletivo";
			erro = erro + 1;
		} 
		
		if(obj.segmento.value=="2" && obj.curso_cas.value=="") {
			desc_erro[erro]="Escolha algum curso do CAS/ITS";
			erro = erro + 1;
		}
	} */
	
	if (obj.curso.value==""){
		desc_erro[erro]="Escolha o Curso Desejado!";
		erro = erro + 1;
	}
	
		
	if (valida_length(obj.nome,1200,4)==false){
		desc_erro[erro]="Digite seu Nome Corretamente!";
		erro = erro + 1;
	}
	
	
	if (obj.telefone.value=="" && obj.tel_alt.value=="" && obj.celular.value==""){
		desc_erro[erro]="Preencha com no mínimo 2 telefones para contato!";
		erro = erro + 1;
	} else {
		var tel_contato=0;
		if (obj.telefone.value.length>0){
			if (valida_length(obj.telefone,14,8)==false){
				desc_erro[erro]="Digite seu Telefone corretamente!";
				erro = erro + 1;
			}else{
				tel_contato=tel_contato+1;
			}
		}
		
		if (obj.tel_alt.value.length>0){
			if (valida_length(obj.tel_alt,14,8)==false){
				desc_erro[erro]="Digite seu Celular corretamente!";
				erro = erro + 1;
			}else{
				tel_contato=tel_contato+1;
			}
		}
		
		if (obj.celular.value.length>0){
			if (valida_length(obj.celular,14,8)==false){
				desc_erro[erro]="Digite seu Celular corretamente!";
				erro = erro + 1;
			}else{
				tel_contato=tel_contato+1;
			}
		}
		
		if (tel_contato<2){
			desc_erro[erro]="Preencha com no mínimo 2 telefones para contato!";
			erro = erro + 1;
		}
	}
	
	if (valida_email(obj.email)==false){
		desc_erro[erro]="Digite seu E-mail corretamente!";
		erro = erro + 1;
	}
	
	if (erro>0){ 	
		document.getElementById('erros').innerHTML="";
		var li = new Array();
		var total_erros = desc_erro.length;
		var div = document.createElement('div');
		div.setAttribute("id","fechar");
		var a = document.createElement('a');
		a.setAttribute("href","javascript://");
		a.onclick=function (){fecha_lightbox('erros')}
		a.appendChild(document.createTextNode("[-]Fechar"));
		div.appendChild(a);
		document.getElementById('erros').appendChild(div);
		var elementos = document.form.elements;
		var lis = document.getElementById('erros').getElementsByTagName('li');
		for (var i=0; i<total_erros; i++){
			li[i] = document.createElement('li');
			li[i].appendChild(document.createTextNode(desc_erro[i]));
			document.getElementById('erros').appendChild(li[i]);
		}
		/*========================================================
		if(select_curso>0){
			var segmentos = document.createElement('div');
			segmentos.setAttribute("id","segmentos");
			var input=new Array(4);
			for (var a=0; a<input.length; a++){
				input[a] = document.createElement('input');
				input[a].setAttribute("name","segmentos");
				input[a].setAttribute("type","radio");
				input[a].setAttribute("style","margin-left:15px;");
				input[a].onclick=function (){
					document.form.segmento.value=this.value;
					checa_segmento(document.form.segmento);
				}
			}
			input[0].setAttribute("value","1");
			input[1].setAttribute("value","8");
			input[2].setAttribute("value","22");
			input[3].setAttribute("value","2");
			segmentos.appendChild(input[0])
			segmentos.appendChild(document.createTextNode("Graduação "));
			segmentos.appendChild(input[1]);
			segmentos.appendChild(document.createTextNode("Pós-graduação "));
			segmentos.appendChild(input[2]);
			segmentos.appendChild(document.createTextNode("EJA/Supletivo "));
			segmentos.appendChild(input[3]);
			segmentos.appendChild(document.createTextNode("CAS/ITS"));
			document.getElementById('erros').appendChild(segmentos);
		}
		========================================================================*/
		if (animacao==0){
			anima_erros();
		}
		return false;
	} else {
		document.getElementById('erros').innerHTML="";
		if(obj.turno){
			var valor_curso="&turno="+obj.turno.value;
		} else {
			var valor_curso="";
		}
		var params = "opt_ind="+check_radio(obj.opt_ind)+"&nome_indicador="+obj.nome_indicador.value+"&tipo_indicador="+check_radio(obj.tipo_indicador)+"&nome="+obj.nome.value+"&telefone="+obj.telefone.value+"&tel_alt="+obj.tel_alt.value+"&celular="+obj.celular.value+"&email="+obj.email.value+"&curso="+obj.curso.value+"&segmento="+obj.segmento.value+valor_curso;
		ajax=ajaxInit();
		ajax.open('POST','insconline2/cartao_amizade.php',true);
		ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajax.setRequestHeader("Content-length", params.length);
		ajax.setRequestHeader("Connection", "close");
		ajax.onreadystatechange=function(){
			if(ajax.readyState == 1){
				loading('erros');
			}
			if(ajax.readyState == 4){
				if(ajax.status == 200){
					document.getElementById('erros').innerHTML=ajax.responseText;
					anima_erros();
					obj.reset();
				}
			}
		}
		ajax.send(params);
	}
	return false;
}

function anima_erros(){	
	animacao=1;
	var erros = document.getElementById('erros');
	var lightbox = document.getElementById('lightbox');
	var height_pg = document.body.offsetHeight;
	var width_pg = document.body.offsetWidth;
	lightbox.style.height=height_pg+"px";
	lightbox.style.width=width_pg+"px";
	lightbox.style.opacity=0;
	lightbox.style.filter="alpha(opacity=0)";
	lightbox.style.display="block";
	var i=0;
	var intervalo = window.setInterval(function (){
		if(i<=9){
			lightbox.style.opacity=i/10;
			lightbox.style.filter="alpha(opacity="+i*10+")";
			i++
		} else {    
			clearInterval(intervalo);
			var distancia = window.pageYOffset || document.documentElement.scrollTop;
			var h_tela=window.innerHeight || document.documentElement.clientHeight;
			document.getElementById('erros').style.display="block";
			var h_obj = document.getElementById('erros').offsetHeight;
			var recuo=h_obj/2;
			if(h_obj>=h_tela){
				recuo=(h_tela*50)/100;
			}
			h_final_obj=((((h_tela*50)/100)+distancia)-recuo);
			document.getElementById('erros').style.marginTop=h_final_obj+"px";
			if(h_final_obj+h_obj > height_pg){
				lightbox.style.height=(h_final_obj+h_obj)+"px";
			}
			animacao=0;
		}
		lightbox.onclick=function (){    
			clearInterval(intervalo);
			lightbox.style.display="none";
			document.getElementById('erros').style.display="none";
		}
	},40);
}

/*===================== Fechar Light =========================*/
		
function fechar(){
	document.getElementById('lightbox').style.display='none';
	document.getElementById('erros').style.display='none';
}

/*===================== Final Fechar Light =========================*/
//----------------------Loading------------------------------------

function loading(obj){
	document.getElementById(obj).innerHTML=""				
	var img=document.createElement('img');
	img.setAttribute('src','imagens/loading.gif');
	img.setAttribute('alt','Carregando Dados');
	document.getElementById(obj).appendChild(img);
}

//================================Lightbox==================================
function lightbox(obj){	
	animacao=1;
	if(!document.getElementById('lightbox')){
		var lightbox = document.createElement('div');
		lightbox.setAttribute('id','lightbox');	
		document.body.insertBefore(lightbox, document.getElementById('principal'));
	} else {
		var lightbox = document.getElementById('lightbox');
	}
	var height_pg = document.body.offsetHeight;
	var width_pg = document.body.offsetWidth;
	if(height_pg<(window.innerHeight || document.documentElement.clientHeight)){
		var height_pg = window.innerHeight || document.documentElement.clientHeight;
		var width_pg = window.innerWidth || document.documentElement.clientWidth;
	}
	lightbox.style.height=height_pg+"px";
	lightbox.style.width=width_pg+"px";
	lightbox.style.opacity=0;
	lightbox.style.filter="alpha(opacity=0)";
	lightbox.style.display="block";
	var i=0;
	var intervalo = window.setInterval(function (){
		if(i<=9){
			lightbox.style.opacity=i/10;
			lightbox.style.filter="alpha(opacity="+i*10+")";
			i++
		} else {    
			clearInterval(intervalo);
			var distancia = window.pageYOffset || document.documentElement.scrollTop;
			var h_tela=window.innerHeight || document.documentElement.clientHeight;
			document.getElementById(obj).style.display="block";
			var h_obj = document.getElementById(obj).offsetHeight;
			var recuo=h_obj/2;
			if(h_obj>=h_tela){
				recuo=(h_tela*50)/100;
			}
			h_final_obj=((((h_tela*50)/100)+distancia)-recuo);
			document.getElementById(obj).style.marginTop=h_final_obj+"px";
			if(h_final_obj+h_obj > height_pg){
				lightbox.style.height=(h_final_obj+h_obj)+"px";
			}
			animacao=0;
		}
		lightbox.onclick=function (){    
			clearInterval(intervalo);
			fecha_lightbox(obj)
		}
	},40);
}

//===================== Final Lightbox =========================

function fecha_lightbox(obj){
	document.getElementById("lightbox").style.display="none";
	document.getElementById(obj).style.display="none";
}