var valido=0;
var rodaSWF = 0; //0 - não roda swf. 1 - roda swf

//INICIO VERIFICAÇÃO SE O FLASH PLAYER ESTÁ INSTALADO
	var MM_contentVersion = 8;
	var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
	if ( plugin ) {
			var words = navigator.plugins["Shockwave Flash"].description.split(" ");
			for (var i = 0; i < words.length; ++i)
			{
			if (isNaN(parseInt(words[i])))
			continue;
			var MM_PluginVersion = words[i]; 
			}
		var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
	}
	else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.appVersion.indexOf("Win") != -1)) {
		document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
		document.write('on error resume next \n');
		document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
		document.write('</SCR' + 'IPT\> \n');
	}
	if ( MM_FlashCanPlay ) {
		rodaSWF = 1;
	} else{
		rodaSWF = 0;
	}
//FIM VERIFICAÇÃO SE O FLASH PLAYER ESTÁ INSTALADO

function arrumaObjs(){
	var divmae = document.getElementById("divmae");
	var taca = document.getElementById("taca");
	var posTop = divmae.offsetHeight - taca.offsetHeight + 100 - 50;
	var posLeft = divmae.offsetLeft;
	taca.style.position = "absolute";
	taca.style.top = posTop+"px";
	taca.style.left = posLeft+"px";
	taca.style.visibility = "visible";
	
	if(screen.width <= 1024){
		var posdiv = divmae.offsetLeft - 118;
		var postaca = taca.offsetLeft - 118;
		divmae.style.position = "relative";
		divmae.style.left = posdiv + "px";
		taca.style.left = postaca+"px";
	}
}

function colocaSWF(dir, wid, hei){
	if(!rodaSWF){
		document.write("Flash player non installato. <a href=\"http://www.macromedia.com/go/getflashplayer\">Clicca qui</a> per installarlo.");
	} else {
		document.write('<object width="' + wid + '" height="' + hei + '">');
		document.write('<param name="movie" value="' + dir + '">');
		document.write('<param name="wmode" value="transparent">');
		document.write('<param name="quality" value="high">');
		document.write('<embed src="' + dir + '" wmode="transparent" width="' + wid + '" height="' + hei + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">');
		document.write('</embed>');
		document.write('</object>');
	}
}

function mudaCaptcha(){
	var lugar = document.getElementById("imagemCaptcha");
	var date = new Date();
	var str = date.getMilliseconds()+date.getMinutes();
	lugar.innerHTML="<img src=\"captcha/captcha.php?a=" + str + "\">";
}

function pegaRequest(){
	try{
		try{
			var ajax = new XMLHttpRequest();
		} catch(e){
			try{
				var ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e){
				var ajax = new ActiveXObject("Microsoft.Msxml2");
			}
		}
		return ajax;
	} catch(e){
		alert(e);
		return false;
	}
}

function verifLogin(){
	var login = document.getElementById("login").value;
	var areaResp = document.getElementById("available");
	if(login.length < 4){
		areaResp.innerHTML = "inserire un login";
	} else {
		var ajax = pegaRequest();
		ajax.onreadystatechange = function(){
			if(this.readyState == 4){
				if(this.status == 200){
					areaResp.innerHTML = this.responseText;
				}
			} else {
				areaResp.innerHTML = "<img src=\"images/loading.gif\">";
			}
		}
		ajax.open("POST", "checklogin.php", true);
		ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajax.setRequestHeader("Cache-Control", "no-store, no-cache, mustrevalidate");
		ajax.setRequestHeader("Pragma", "no-cache");
		ajax.setRequestHeader("Connection", "close");
		ajax.send("login="+login);
	}
}

function validaForm(){
	var status = document.getElementById('statusCadastro');
	var codigo = document.getElementById('campoCaptcha').value;
	if(codigo.length != 6){
		status.innerHTML = "Invalide captcha";
	} else {
		var ajax = pegaRequest();
		ajax.onreadystatechange = function(){
			if(this.readyState == 4){
				if(this.status == 200){
					var resposta = this.responseText;
					if(resposta == "1" && validaCampos()){
						status.innerHTML = "&nbsp;";
						valido=1;
						cadastra();
					} else {
						if(resposta == 0){
							status.innerHTML = "Invalide captcha";
						}
						valido=0;
					}
				}
			}
		}
		ajax.open("POST", "captchacheck.php", true);
		ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajax.setRequestHeader("Cache-Control", "no-store, no-cache, mustrevalidate");
		ajax.setRequestHeader("Pragma", "no-cache");
		ajax.setRequestHeader("Connection", "close");
		ajax.send("codigo="+codigo);
	}
}

function validaEmail(email){
	var posArroba = email.indexOf("@");
	var posPto = email.indexOf(".", posArroba);
	var lugarEnd = posPto - posArroba;
	if(posArroba < 3 || posPto <= posArroba || lugarEnd < 4){
		return false;
	} else {
		var dpsPto = email.charAt(posPto+2);
		if(dpsPto == ""){
			return false;
		} else {
			return true;
		}
	}
}

function validaCampos(){
	var status = document.getElementById('statusCadastro');
	var email = document.getElementById("email").value;
	var aceito = document.getElementById("aceito").checked;
	var login = document.getElementById("login").value;
	var senha = document.getElementById("password").value;
	var conf = document.getElementById("confirm").value;
	var campos = false;
	var obrig = ["lastname", "name", "login", "password"];
	
	for(i=0;i<obrig.length;i++){
		var campo = document.getElementById(obrig[i]);
		if(campo.value.length > 3){
			campos = true;
		} else {
			campos = false;
		}
	}
	if(validaEmail(email) && aceito && campos && senha == conf){
		return true;
	} else {
		if(!validaEmail(email)){
			status.innerHTML = "Inserire la mail";
		} else if(!aceito){
			status.innerHTML = "È necessario accettare lo termino";
		} else if(senha != conf){
			status.innerHTML = "Password non corrispondono";
		} else if(login == ""){
			status.innerHTML = "Inserire il login";
		} else {
			status.innerHTML = "Campi non validi";
		}
		return false;
	}
}




function validaCampos2(){

	var email = document.getElementById("email").value;
	var aceito = document.getElementById("aceito").checked;

	if(validaEmail(email) && aceito){
		return true;
	} else {
		if(!validaEmail(email)){
			status.innerHTML = "Inserire la mail";
		} else if(!aceito){
			status.innerHTML = "È necessario accettare";
		} else {
			status.innerHTML = "Campi non validi";
		}
		return false;
	}
}

function cadastra(){
	if(valido && validaCampos()){
		var status = document.getElementById('statusCadastro');
		var params = "";
		var form = document.forms[0];
		for(i=0;i<form.length;i++){
			if(i>0 && form[i].type != "radio" && form[i].type != "submit"){
				params += "&";
			}
			if(form[i].type == "text" || form[i].type == "password" || form[i].type == "textarea"){
				params += form[i].id + "=" + form[i].value;
			} else if(form[i].type == "checkbox"){
				if(form[i].checked){
					params += form[i].id + "=1";
				} else {
					params += form[i].id + "=0";
				}
			}
		}
		var ajax = pegaRequest();
		ajax.onreadystatechange = function(){
			if(this.readyState == 4){
				if(this.status == 200){
					status.innerHTML = this.responseText;
				}
			} else {
				status.innerHTML = "inviando...";
			}
		}
		ajax.open("POST", "beerhouse/cadastrar.php", true);
		ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajax.setRequestHeader("Cache-Control", "no-store, no-cache, mustrevalidate");
		ajax.setRequestHeader("Pragma", "no-cache");
		ajax.setRequestHeader("Connection", "close");
		ajax.send(params);
	}
}









function cadastra2(){
	if(validaCampos2()){
		var status = document.getElementById('statusCadastro');
		var params = "";
		var form = document.forms[0];
		for(i=0;i<form.length;i++){
			if(i>0 && form[i].type != "radio" && form[i].type != "submit"){
				params += "&";
			}
			if(form[i].type == "text" || form[i].type == "password" || form[i].type == "textarea"){
				params += form[i].id + "=" + form[i].value;
			} else if(form[i].type == "checkbox"){
				if(form[i].checked){
					params += form[i].id + "=1";
				} else {
					params += form[i].id + "=0";
				}
			}
		}
		var ajax = pegaRequest();
		ajax.onreadystatechange = function(){
			if(this.readyState == 4){
				if(this.status == 200){
					status.innerHTML = this.responseText;
				}
			} else {
				status.innerHTML = "inviando...";
			}
		}
		ajax.open("POST", "cadastrar.php", true);
		ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajax.setRequestHeader("Cache-Control", "no-store, no-cache, mustrevalidate");
		ajax.setRequestHeader("Pragma", "no-cache");
		ajax.setRequestHeader("Connection", "close");
		ajax.send(params);
	}
}












function sendMail(){
	var nome = document.getElementById('nome');
	var email = document.getElementById('email');
	var msg = document.getElementById('mensagem');
	var status = document.getElementById('resultado');
	
	if(nome.value.length < 3){
		status.innerHTML = "Inserire i vostri dati"; //caso nome nao seja preenchido
	} else if(!validaEmail(email.value)){
		status.innerHTML = "Inserire i vostri dati"; //caso email nao seja preenchido
	} else if(msg.value.length < 3){
		status.innerHTML = "Inserire i vostri dati"; //caso mensagem nao seja preenchida
	} else {
		var ajax = pegaRequest();
		ajax.onreadystatechange = function(){
			if(this.readyState == 4){
				if(this.status == 200){
					status.innerHTML = this.responseText;
				}
			} else {
				status.innerHTML = "Inviando...";
			}
		}
		ajax.open("POST", "invia.php", true);
		ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajax.setRequestHeader("Cache-Control", "no-store, no-cache, mustrevalidate");
		ajax.setRequestHeader("Pragma", "no-cache");
		ajax.setRequestHeader("Connection", "close");
		ajax.send("name="+nome.value+"&email="+email.value+"&mensagem="+msg.value);
	}
}
