// JavaScript Document

//INICIA AJAX
function ajaxInit() {
	var req;
	
	try {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(ex) {
				try {
				req = new XMLHttpRequest();
			} catch(exc) {
				alert("Esse browser não tem recursos para uso do Ajax");
				req = null;
			}
		}
	}
		
	return req;
}


//FUNÇÃO PARA OVER DO MENU
over = function() {
	var sfEls = document.getElementById("menu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" over";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" over\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", over);


//VALIDA CAMPOS DO FORMULARIO
function checaForm(form) {
		//theform = document.getElementById('forms');
		theform = form;
		if (document.all || document.getElementById) {
			
			var label = '';
			var errors = 0;
			
			for (i = 0; i < theform.length; i++) {
			var formElement = theform.elements[i];
				if (true) {
					if(theform.elements[i].type != 'button' && theform.elements[i].type != undefined) {
						document.getElementById(formElement.id).style.border = '1px solid #CCC';
						//document.getElementById(formElement.id).style.backgroundColor='#FFF';
						
						if (formElement.alt == 'required' || formElement.title == 'required') {
							if (!formElement.value) {
								//document.getElementById(formElement.id).style.backgroundColor='#D7E9F7';	
								document.getElementById(formElement.id).style.border = '1px solid #FF0000';
								errors++;
							}
						}
					}
				}
			}
		}
		if (errors == 0) {
			return true;
		} else {
			alert('Os campos em destaque são obrigatórios');
			return false;
		}
}

//FUNCAO PARA TOOLTIP
var divName = 'tip';
var offX = 15; 
var offY = 15;
var drag = 0;

function init() {
	if (document.getElementById('tip')) {
		document.getElementById('tip').style.top = '533px';
		document.getElementById('tip').style.left = '758px';
	}
}
window.onload = init;

function mouseX(evt) {if (!evt) evt = window.event; if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}
function mouseY(evt) {if (!evt) evt = window.event; if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return 0;}

function follow(evt) {
	if (drag == 1) {
		if (document.getElementById) {
			var obj = document.getElementById(divName).style;
			if (parseInt(mouseX(evt))+offX < 758)
				obj.left = (parseInt(mouseX(evt))+offX) + 'px';
			if (parseInt(mouseY(evt))+offY < 533)
				obj.top = (parseInt(mouseY(evt))+offY) + 'px';
		}
	}
}
document.onmousemove = follow;

//MOSTRA INFORMACOES DAS VELAS DA UP
function showTip(vela,fab) {
	document.getElementById('tip').style.display = 'block';	
	
	switch (vela) {
		case 'ascent':
			titulo = 'SENSATIONAL PROGRESS';
			img = vela;
			txt = 'Once you have left the training hill behind you it is normal to yearn for that little bit extra – more performance, crisper handling, and sexier looks. But you still need to progress in safety, and many pilots choose to remain within the DHV 1 category. We have built the Ascent for these pilots!';
			break;
			
		case 'kuna':
			titulo = 'LEARNING SENSATIONS';
			img = vela;
			txt = 'The UP Kuna is the perfect companion for the transgression from earth bound sky-gazer to sky bound earth gazer. Its simple, durable construction makes it ideal for the daily training hill exertion whilst the performance and handling will make those first thermal flights and even cross country flights into pure bliss. Very forgiving, just as a school wing should be.';
			break;
			
		case 'makalu':
			titulo = 'RELAXED SENSATIONS';
			img = vela;
			txt = 'Take off, kick back and enjoy! The Makalu 2 has already won many hearts among weekend pilots around the world prioritising absolute user-friendliness, bomb-proof safety and ample performance for their needs.';
			break;
			
		case 'kantega':
			titulo = 'LOOK SENSATIONAL';
			img = vela;
			txt = 'Pilots will always enjoy the racy look of a sleek, sharp-looking wing; and for a good reason, since wings with good looks and smooth, racy surfaces often have better performance.';
			break;
			
		case 'summit':
			titulo = 'TOURING SENSATIONS';
			img = vela;
			txt = 'The Summit 3 is a completely new wing, designed to take you further on your XC adventures. It is based on the incredibly successful Trango 2 and Targa 2 wings, and has inherited the glide and the speed that makes these wings so special.';
			break;
			
		case 'trango':
			titulo = 'WINNING SENSATIONS!';
			img = vela;
			txt = 'The Trango 2 has already cemented its position as THE only DHV 2-3 wing with performance comparable to the Open Class hot ships. This has given the wing a huge following among competitive pilots wishing to perform to their personal best without compromising safety';
			break;
			
		case 'targa':
			titulo = 'SENSATIONALLY SMOOTH';
			img = vela;
			txt = 'The Targa 3 is the latest flagship from the UP R&D department, filled to the brim with every useful innovation we could think of. Addressing the very specific needs of the top echelons of competition pilots, this is the weapon you need to keep you ahead in the performance race.';
			break;
			
		case 'sherpa':
			titulo = 'SHARED SENSATIONS';
			img = vela;
			txt = 'You are an experienced pilot with a tandem license. You want to earn money with your tandem glider, or simply enjoy all the wonderful aspects of paragliding together with others.';
			break;
			
		case 'rock':
			titulo = 'SENSATIONALLY RADICAL';
			img = vela;
			txt = 'The Extreme version is a no-compromise super dynamic design for winning international Acro events, while the Freestyle version appeals to pilots who will not be exclusively doing acro on their wing. The wings are designed to let their pilot progress at the fastest possible rate, but the Extreme requires previous acro experience';
			break;
			
		case 'merlin':
			titulo = 'MERLIN';
			img = vela;
			txt = 'The goal during the development of the Merlin was to create a glider for beginners, which is on the one hand absolute suitable for students because it is very safe, and offers on the other hand pure flying pleasure for the beginning paragliding pilot.';
			break;
		
		case 'dragon':
			titulo = 'DRAGON III';
			img = vela;
			txt = 'The new Dragon III will continue the success history of the independence-intermediates within the DHV1-2 range. The new Dragon3 is a completely new designed glider and not only an update of the predecessor model!';
			break;
			
		case 'akron':
			titulo = 'AKRON';
			img = vela;
			txt = 'The Akron is our high-performance glider in the DHV 2 segment. Because of the Flycad FD usage the Akron reaches a performance level that is normally only reachable for high performer.';
			break;
			
		case 'excalibur':
			titulo = 'EXCALIBUR';
			img = vela;
			txt = 'The Excalibur succeeds to the Radical. Our researches made on the Radical´s surface with FD, our CAD software, enabled us to optimize further the performances of the Excalibur, without denying its exceptional flight behaviour in extreme situations, main asset which made the Radical so popular.';
			break;
			
		case 'raptor':
			titulo = 'RAPTOR';
			img = vela;
			txt = 'The Raptor is a product out of our big experience with closed cells, high aspect ratio wings and the new FD software. Er zeichnet sich durch eine überragende Steigleistung in der Thermik und eine besonders flache Polare aus. The principal features are the incredible climb capability and the very flat speed polar.';
			break;
			
		case 'speed':
			titulo = 'SPEED';
			img = vela;
			txt = 'The Speed tandem: a real pleasure for two. As we were used to it with all the gliders from the Independence range, the Speed spoils the pilot and his passenger: perfect behaviour at launch, light and precise handling, very high security level, perfect finishing, and long lifetime are the keywords for this tandem glider.';
			break;
			
		case 'duke':
			titulo = 'DUKE';
			img = vela;
			txt = 'The independence "Duke" is an uncompromising aerobatic wing. Pure dynamic together with a very direct handling enables the experienced pilot to fly all aerobatic maneuvers with a maximum of precision. ';
			break;
			
		case 'cyber':
			titulo = 'CYBER';
			img = vela;
			txt = 'Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. ';
			break;
			
		case 'force':
			titulo = 'FORCE 2';
			img = vela;
			txt = 'Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. ';
			break;
			
		case 'instinct':
			titulo = 'INSTINCT';
			img = vela;
			txt = 'Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. ';
			break;
			
		case 'incanto':
			titulo = 'INCANTO';
			img = vela;
			txt = 'Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. ';
			break;
			
		case 'ice':
			titulo = 'ICE 2XC';
			img = vela;
			txt = 'Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. ';
			break;
			
		case 'cidan':
			titulo = 'CIDAN';
			img = vela;
			txt = 'Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. ';
			break;
			
		case 'twice':
			titulo = 'TWICE - BIPLACE';
			img = vela;
			txt = 'Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. ';
			break;
			
		case 'nikita':
			titulo = 'LA FEMME FATALLE OF ICARO';
			img = vela;
			txt = 'Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. Aqui entra a descrição do equipamento. ';
			break;
	}
	
	drag = 1;
	document.getElementById('titulo_tip').innerHTML = titulo;
	document.getElementById('imagem').src = '../images/'+fab+'/img_'+img+'.jpg';
	document.getElementById('texto_tip').innerHTML = txt;
}

//ESCONDE INFORMACOES DAS VELAS DA UP
function hideTip() {
	document.getElementById('tip').style.display = 'none';	
	drag = 0;
}

//ABRE POPUP DAS FOTOS MAIORES
function showFoto(img) {
	window.open('showImg.php?img='+img+'','imagem','height=10,width=10,top=20,left=20');
}

//MUDA TAMANHO DA POPUP
function changeSize(img) {
	var largura = img.width + 7;
	var altura = img.height + 53;
	window.resizeTo(largura,altura);
	window.focus();
}

//ABRE FORMULARIO DE CADASTRO DOS CLASSIFICADOS
show = 0;
function showClassi(el) {
	div = document.getElementById(el);
	if (show == 0) {
		div.style.display = 'block';
		show = 1;
	} else {
		div.style.display = 'none';
		show = 0;
	}
	
}

//MUDA CATEGORIA
function mudaCat(v) {
	window.location = '?s=classificados&categoria='+v;
}

//CHECA CAMPO DE CADASTRO DE NEWSLETTER - ONFOCUS
function checaNin(campo) {
	if (campo == 'n_nome') {
		if (document.getElementById(campo).value == 'Seu nome')	document.getElementById(campo).value = '';
	}
	
	if (campo == 'n_email') {
		if (document.getElementById(campo).value == 'Seu e-mail') document.getElementById(campo).value = '';
	}
}

//CHECA CAMPO DE CADASTRO DE NEWSLETTER - ONBLUR
function checaNout(campo) {
	if (campo == 'n_nome') {
		if (document.getElementById(campo).value == '')	document.getElementById(campo).value = 'Seu nome';
	}
	
	if (campo == 'n_email') {
		if (document.getElementById(campo).value == '') document.getElementById(campo).value = 'Seu e-mail';
	}
}

//VERIFICA CAMPOS PARA CADASTRO DE NEWSLETTER
function checaNewsletter(n) {
	form = document.getElementById(n);
	
	if (form.n_nome.value == 'Seu nome') {
		alert('Forneça seu nome!');	
		form.n_nome.focus();
	} else if (form.n_email.value == 'Seu e-mail') {
		alert('Forneça seu e-mail!');	
		form.n_email.focus();
	} else cadNewsletter(form.n_nome.value,form.n_email.value,n);
}

//VALIDA EMAIL
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}


//CADASTRA NEWSLETTER
function cadNewsletter(nome,email,n) {
	if (echeck(email)) { 
		ajax = ajaxInit();
		if(ajax) {
			noCache = new Date().getTime();
			var param = 'nc='+noCache+'&nome='+nome+'&email='+email+'';
			document.getElementById(n).innerHTML = 'Aguarde, seu cadastro está sendo efetuado...';
	
			ajax.open('POST','../includes/cad_newsletter.php', true);
			ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			ajax.send(param);
			ajax.onreadystatechange = function() {
				if(ajax.readyState == 4) {
					if(ajax.status == 200) {
						var retorno = ajax.responseText;
						//alert(retorno);
						if (retorno != "") {
							document.getElementById(n).innerHTML = '<p>Cadastre-se para receber as novidades e promo&ccedil;&otilde;es da YNOVAR em seu e-mail.</p><input type="text" name="n_nome" id="n_nome" value="Seu nome" style="margin-bottom:3px;width:145px" onfocus="checaNin(\'n_nome\')" onblur="checaNout(\'n_nome\')" /><input type="text" name="n_email" id="n_email" value="Seu e-mail" onfocus="checaNin(\'n_email\')" onblur="checaNout(\'n_email\')" /><button type="button" id="bt_newsletter" name="bt_newsletter" onclick="checaNewsletter(\'newsletter\')">Ok</button>';
							alert(retorno);
						} 
					} else {
						alert(ajax.statusText);
					}
				}
			}
		}
	} else alert('Forneça um e-mail válido!');
}
