$(document).ready(function()  {
	$(function() {
		$(".external").attr("target","_blank");
	});

	$("a").focus(function() {
		$(this).blur();
	});

	/*Accés usuaris*/
	$("a.user-acccess-btn").click(function()  {
		$("#user-acces").animate({
		marginTop: "0"
		}, 500 );
	}); 
	$("a.user-access-cancel").click(function()  {
		$("#user-acces").animate({
		marginTop: "-40px"
		}, 250 );
	}); 

	/*Menu principal*/
	
	// set opacity on page load
	$("#menu span").css("opacity","0");
	// on mouse over
	$("#menu span").hover(function () {
		$(this).stop().animate({opacity: 1}, 150);
	},
	// on mouse out
	function () {
		$(this).stop().animate({opacity: 0}, 200);
	});	
	
	$("#menu a.submenu").mouseover(function()  {
		$(this).addClass("active");
		var idMenu = $(this).attr("rel");
		$("#"+idMenu).slideDown(250);
		return false; 
	}); 

	$("#menu a.submenu").click(function()  {
		return false; 
	}); 

	
	$("#menu a:not(.submenu)").mouseover(function()  {
		$("#menu a.active").removeClass("active");
		$("ul.submenu").slideUp(125);
	});

	/* Tabs menu lateral */
	$(".tab-content").hide();  //Hide all content
	$("ul#tabs li:first").addClass("active").show();  //Activate first tab
	$(".tab-content:first").show();  //Show first tab content
	
	$("ul#tabs li").click(function()  {
		$(this).blur();			   	
		$("ul#tabs").removeClass();  //Remove any class	
		$("ul#tabs li").removeClass();  //Remove any class	
		var activeColor = $(this).find("a").attr("class"); //Find active tab color
		$("ul#tabs").addClass("border-"+activeColor);  //Add color to bottom-border
		$(this).addClass("active");  //Add "active" class to selected tab
		$(this).addClass(activeColor); //Add color to active tab
		
		$("#tab-container").removeClass();  
		$("#tab-container").addClass(activeColor);
		
		$(".tab-content").hide();  //Hide all tab content
		var activeTab = $(this).find("a").attr("href");  //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(125);  //Fade in the active content
		return false; 
	}); 


}); 


// Declaring valid date character, minimum year and maximum year
var ctrl=0
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr,source){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear

	if (strDay.length!=2){
		alert(source + " no vàlida; el format ha de ser dd/mm/aaa")
		return false
	}
	if (strMonth.length!=2){
		alert(source + " no vàlida; el format ha de ser dd/mm/aaaa")
		return false
	}
	if (strYear.length != 4){
		alert(source + " no vàlida; el format ha de ser dd/mm/aaaa")
		return false
	}
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (month<1 || month>12){
		alert(source + " no vàlida; el format ha de ser dd/mm/aaaa")
		return false
	}
	if (day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert(source + " no vàlida; el format ha de ser dd/mm/aaaa")
		return false
	}
	if (year==0 || year<minYear || year>maxYear){
		alert(source + " no vàlida; el format ha de ser dd/mm/aaaa")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert(source + " no vàlida; el format ha de ser dd/mm/aaaa")
		return false
	}
return true;
}


function validarfrmInscripcio() {
	var nom = document.getElementById("frmInscripcio.sNom").value;
	var birth = document.getElementById("frmInscripcio.sBirth").value;
	var address = document.getElementById("frmInscripcio.sAddress").value;
	var localitat = document.getElementById("frmInscripcio.sLocalitat").value;
	var cp = document.getElementById("frmInscripcio.sCP").value;
	var tel = document.getElementById("frmInscripcio.sTel").value;
	var mobil = document.getElementById("frmInscripcio.sMobil").value;
	var mail = document.getElementById("frmInscripcio.sMail").value;
	var text1 = document.getElementById("frmInscripcio.ajudar").value;
	var text2 = document.getElementById("frmInscripcio.temes").value;
	var text3 = document.getElementById("frmInscripcio.aportacions").value;

	if (nom == "") { 
		alert("Ha d'indicar un nom.")
		return false;}

	if (birth == "") { 
		alert("Ha d'indicar una data de naixement en format dd-mm-aaaa.")
		return false;}
		
	if (isDate(birth,"Data de naixament")==false){
			return false;}

	if (address == "") { 
		alert("Ha d'indicar una adreça.")
		return false;}

	if (localitat == "") { 
		alert("Ha d'indicar una localitat.")
		return false;}

	if (cp == "") { 
		alert("Ha d'indicar una codi postal.")
		return false;}

	if (tel == "" && mobil == "") { 
		alert("Ha d'indicar un telefon de contacte.")
		return false;}

	/*
	if (mail == "") { 
		alert("Ha d'indicar una direcció de correu electrònic.")
		return false;}
	*/
	
	if (mail != "") { 
		if (validaEmail(mail)==false) {
			alert("Format de correu electrònic no vàlid.");
			return false;}
	}

	var Birth1 = document.getElementById("frmInscripcio.sf1Birth").value;
	var Birth2 = document.getElementById("frmInscripcio.sf2Birth").value;
	var Birth3 = document.getElementById("frmInscripcio.sf3Birth").value;
	var Birth4 = document.getElementById("frmInscripcio.sf4Birth").value;
	var Birth5 = document.getElementById("frmInscripcio.s5fBirth").value;

	if (Birth1!=""){
		if (isDate(Birth1,"Data fill 1")==false){
			return false;
		}
	}

	if (Birth2!=""){
		if (isDate(Birth2,"Data fill 2")==false){
			return false;
		}
	}

	if (Birth3!=""){
		if (isDate(Birth3,"Data fill 3")==false){
			return false;
		}
	}

	if (Birth4!=""){
		if (isDate(Birth4,"Data fill 4")==false){
			return false;
		}
	}

	if (Birth5!=""){
		if (isDate(Birth5,"Data fill 5")==false){
			return false;
		}
	}


	if (text1 != "" ) { 
		if (isLegal(text1)==false) {
			alert("Caracters no permessos: #$^&*~<>/\|");
			return false;}
	}

	if (text2 != "" ) { 
		if (isLegal(text2)==false) {
			alert("Caracters no permessos: #$^&*~<>/\|");
			return false;}
	}

	if (text2 != "" ) { 
		if (isLegal(text3)==false) {
			alert("Caracters no permessos: #$^&*~<>/\|");
			return false;}
	}

return true;}

function validarfrmContactar() {
	var nom = document.getElementById("frmContactar.sNom").value;
	var tel = document.getElementById("frmContactar.sTel").value;
	var mail = document.getElementById("frmContactar.sMail").value;
	var text = document.getElementById("frmContactar.sText").value;

	if (nom == "") { 
		alert("Ha d'indicar un nom.")
		return false;}

	if (mail == "") { 
		alert("Ha d'indicar una direcció de correu electrònic.")
		return false;}

	if (mail != "") { 
		if (validaEmail(mail)==false) {
			alert("Format de correu electrònic no vàlid.");
			return false;}
	}

	if (text == "") { 
		alert("Ha d'especificar una consulta.")
		return false;}

	if (text != "" ) { 
		if (isLegal(text)==false) {
			alert("Caracters no permessos: #$^&*~<>/\|");
			return false;}
	}

return true;}


function validarfrmAlta() {
	var nom = document.getElementById("frmAlta.sNickname").value;
	var mail = document.getElementById("frmAlta.sEmail").value;
	var pwd = document.getElementById("frmAlta.sPassword").value;

	if (nom == "") { 
		alert("Ha d'indicar un nom d'usuari.")
		return false;}

	if (mail == "") { 
		alert("Ha d'indicar una direcció de correu electrònic.")
		return false;}

	if (mail != "") { 
		if (validaEmail(mail)==false) {
			alert("Format de correu electrònic no vàlid.");
			return false;}
	}

	if (pwd.length >=0 && pwd.length <4) {
		alert("La contrassenya ha de tenir un mínim de 4 caràcters.")
		return false;}

return true;}


function validarfrmEdit() {
	var nom = document.getElementById("frmEdit.sNickname").value;
	var mail = document.getElementById("frmEdit.sEmail").value;
	var pwd = document.getElementById("frmEdit.sPassword").value;

	if (nom == "") { 
		alert("Ha d'indicar un nom d'usuari.")
		return false;}

	if (mail == "") { 
		alert("Ha d'indicar una direcció de correu electrònic.")
		return false;}

	if (mail != "") { 
		if (validaEmail(mail)==false) {
			alert("Format de correu electrònic no vàlid.");
			return false;}
	}

	if (pwd.length >=0 && pwd.length <4) {
		alert("La contrassenya ha de tenir un mínim de 4 caràcters.")
		return false;}

return true;}


function validarfrmLogin() {
	var mail = document.getElementById("frmLogin.sEmail").value;
	var pwd = document.getElementById("frmLogin.sPassword").value;

	if (mail == "") { 
		alert("Ha d'indicar una direcció de correu electrònic.")
		return false;}

	if (mail != "") { 
		if (validaEmail(mail)==false) {
			alert("Format de correu electrònic no vàlid.");
			return false;}
	}

	if (pwd.length >=0 && pwd.length <4) {
		alert("La contrassenya ha de tenir un mínim de 4 caràcters.")
		return false;}

return true;}


function validarfrmRightLogin() {
	var mail = document.getElementById("frmRightLogin.sEmail").value;
	var pwd = document.getElementById("frmRightLogin.sPassword").value;

	if (mail == "") { 
		alert("Ha d'indicar una direcció de correu electrònic.")
		return false;}

	if (mail != "") { 
		if (validaEmail(mail)==false) {
			alert("Format de correu electrònic no vàlid.");
			return false;}
	}

	if (pwd.length >=0 && pwd.length <4) {
		alert("La contrassenya ha de tenir un mínim de 4 caràcters.")
		return false;}

return true;}


function validarfrmPassword() {
	var mail = document.getElementById("frmPassword.sEmail").value;

	if (mail == "") { 
		alert("Ha d'indicar una direcció de correu electrònic.")
		return false;}

	if (mail != "") { 
		if (validaEmail(mail)==false) {
			alert("Format de correu electrònic no vàlid.");
			return false;}
	}

return true;}

function validarNewTopic() {
	var title = document.getElementById("sTitle").value;
	var text = document.getElementById("sText").value;

	if (title == "") { 
		alert("Ha d'indicar un títol.")
		return false;}

	if (title != "") { 
		if (isLegal(title)==false) {
			alert("Caracters no permessos: #$^&*~<>/\|");
			return false;}
	}
	if (text == "") { 
		alert("El text no pot estar en blanc.")
		return false;}

	if (text != "") { 
		if (isLegal(text)==false) {
			alert("Caràcters no permessos: #$^&*~<>/\|");
			return false;}
	}
	
return true;}


function validarReply() {
	var text = document.getElementById("sText").value;

	if (text == "") { 
		alert("El text no pot estar en blanc.")
		return false;}

	if (text != "") { 
		if (isLegal(text)==false) {
			alert("Caràcters no permessos: #$^&*~<>/\|");
			return false;}
	}

return true;}