function doLogin()
{
	//GOOGLE ANALYTICS
	pageTracker._trackEvent("home", "login");
	
	var form = document.getElementById("login_form");
	form.submit();
}

function checkInvitation()
{
	//GOOGLE ANALYTICS
	pageTracker._trackEvent("home", "register");
	
	var form = document.getElementById("invitation_form");
	form.submit();
}

function fieldDefaultValue(event,idform,fieldname,defvalue)
{
	var form = document.getElementById(idform);
	if(form != null)
	{
		for(var i=0; i<form.elements.length; i++)
		{
			if(form.elements[i].name == fieldname)
				var element = form.elements[i];
		}
		if(event == "onblur")
		{
			if(element.value == "")
				element.value = defvalue;
		}
		if(event == "onfocus")
		{
			if(element.value == defvalue)
				element.value = "";
		}
	}
}
	
var DIALOG_WIDTH = 560;
var DIALOG_HEIGHT = 340;
	
function openDialog()
{
	//veilscreen
	var div_veilscreen = document.createElement("div");
	div_veilscreen.setAttribute("id","veilscreen");
	div_veilscreen.setAttribute("className", "veilscreen");
	div_veilscreen.setAttribute("class", "veilscreen");
	document.body.appendChild(div_veilscreen);
	//dialog
	var div = document.createElement("div");
	div.setAttribute("id","dialog");
	div.setAttribute("className", "dialog_white");
	div.setAttribute("class", "dialog_white");
	div.style.top = Math.floor((document.documentElement.clientHeight-DIALOG_HEIGHT)/2) + "px";
	div.style.left = Math.floor((document.documentElement.clientWidth-DIALOG_WIDTH)/2) + "px";
	div.style.height = DIALOG_HEIGHT + "px";
	div.style.width = DIALOG_WIDTH + "px";
	document.body.appendChild(div);
	return div;
}

function closeDialog()
{
	var div_veilscreen = document.getElementById("veilscreen");
	document.body.removeChild(div_veilscreen);
	var div = document.getElementById("dialog");
	div.innerHTML = "";
	document.body.removeChild(div);
}

function showVideo(idvideo,station,title)
{
	//GOOGLE ANALYTICS
	pageTracker._trackEvent("video", station, title);

	DIALOG_WIDTH = 560;
	DIALOG_HEIGHT = 340;
	var div = openDialog();
	div.innerHTML = "<div class='close_dialog'><a href='javascript:void(0);' onclick='closeDialog();'><img src='img/close.gif'></a></div>";
	if(station == "youtube")
		div.innerHTML += eval("\"<object width='560' height='340'><param name='movie' value='http://www.youtube.com/v/" + idvideo + "&hl=es&fs=1&rel=0&color1=0x234900&color2=0x4e9e00'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src='http://www.youtube.com/v/" + idvideo + "&hl=es&fs=1&rel=0&color1=0x234900&color2=0x4e9e00' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='560' height='340'></embed></object>\"");
	if(station == "fonyoutv")
		div.innerHTML += eval("\"<iframe id='player' src='http://users.fonyou.com:8080/fonyoutv/frame_player.jsp?type=normal&div=help_player&width=560&height=340&color=0xff7b00&fullscreen=true&autostart=true&id_video=" + idvideo + "&id_preroll=' scrolling='no' border='0' frameborder='0' width='560' height='340' marginwidth='0' marginheight='0' AllowTransparency></iframe>\"");
}

var error_color = "red";
var correct_color = "silver";

function resetFormErrors(form)
{
	for (var i=0;i<form.length;i++)
	  form.elements[i].style.borderColor = correct_color;
}

function validateEmails(email1, email2)
{
	var error = "";
	if(email1.value != "" && email2.value != "")
	{
		if(email1.value.indexOf("@") == -1 || email1.value.indexOf(".") == -1)
		{
			error = "Email format incorrect";
			email1.style.borderColor = error_color;
		}
		else
		{
			if(email1.value != email2.value)
			{
				error = "The emails don�t match";
				email1.style.borderColor = error_color;
				email2.style.borderColor = error_color;
			}	
		}
	}
	return error;
}

function validateEmptyFields()
{
	var error = "";
	var form = document.getElementById("invitation_form");
	if(form.name.value == "")
	{
		form.name.style.borderColor = error_color;
		error = "Please complete all mandatory fields";
	}
	if(form.surnames.value == "")
	{
		form.surnames.style.borderColor = error_color;
		error = "Please complete all mandatory fields";
	}
	if(form.email.value == "")
	{
		form.email.style.borderColor = error_color;
		error = "Please complete all mandatory fields";
	}
	if(form.confirm_email.value == "")
	{
		form.confirm_email.style.borderColor = error_color;
		error = "Please complete all mandatory fields";
	}
	if(form.captcha.value == "")
	{
		form.captcha.style.borderColor = error_color;
		error = "Please complete all mandatory fields";
	}
	return error;
}

function sendPetition()
{
	var form = document.getElementById("invitation_form");
	resetFormErrors(form);
	var error = validateEmptyFields();
	if(error == "")
		error = validateEmails(form.email, form.confirm_email);
	if(error == "")
	{
		//GOOGLE ANALYTICS
		pageTracker._trackEvent("home", "send petition");
		
		form.submit();
	}
	else
		alert(error);
}

function sendContactUs()
{
	var form = document.getElementById("contactus_form");
	form.submit();
}

function petitionSuccess()
{
	//GOOGLE ANALYTICS
	pageTracker._trackEvent("home", "send petition", "thanks");
	
	window.location.replace("invitation_ok.html");
}

function contactSuccess()
{
	//GOOGLE ANALYTICS
	pageTracker._trackEvent("contact", "form", "thanks");
	
	window.location.replace("contact_ok.html");
}

function generateCaptcha(id)
{
	document.getElementById(id).innerHTML = "<img class='captcha' src='https://users.fonyou.com/web/captcha?id=" + new Date().getTime() + "'>";
}

function showImage(photo, width, height, title)
{	
	//GOOGLE ANALYTICS
	pageTracker._trackEvent("press", "image", title);
	
	DIALOG_WIDTH = width;
	DIALOG_HEIGHT = height;
	var div = openDialog();
	div.innerHTML = "<div class='close_dialog'><a href='javascript:void(0);' onclick='closeDialog();'><img src='img/close.gif'></a></div>";
	div.innerHTML += eval("\"<img src='img/photos/" + photo + "' />\"");
}

function analyticsHowWorks(how)
{
	how = replaceSpecialChars(how);

	//GOOGLE ANALYTICS
	pageTracker._trackEvent("how to use", how);
}

function analyticsQuestion(question)
{
	question = replaceSpecialChars(question);
	
	//GOOGLE ANALYTICS
	pageTracker._trackEvent("FAQ", question);
}

function replaceSpecialChars(str) 
{
		if (str != null) 
		{
			//encoding nativo de javascript
			str = escape(str);
			while(str.split("%E1").length > 1) str = str.replace("%E1","a"); //� -> a
			while(str.split("%E9").length > 1) str = str.replace("%E9","e"); //� -> e
			while(str.split("%ED").length > 1) str = str.replace("%ED","i"); //� -> i
			while(str.split("%F3").length > 1) str = str.replace("%F3","o"); //� -> o
			while(str.split("%FA").length > 1) str = str.replace("%FA","u"); //� -> u
			while(str.split("%C1").length > 1) str = str.replace("%C1","A"); //� -> A
			while(str.split("%C9").length > 1) str = str.replace("%C9","E"); //� -> E
			while(str.split("%CD").length > 1) str = str.replace("%CD","I"); //� -> I
			while(str.split("%D3").length > 1) str = str.replace("%D3","O"); //� -> O
			while(str.split("%DA").length > 1) str = str.replace("%DA","U"); //� -> U
			while(str.split("%BF").length > 1) str = str.replace("%BF",""); //� -> 
			str = unescape(str);
		} 
		return str;
	}