String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/, '');
};

String.prototype.checkEmail = function() {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return this.match(re);
};

function major(n1, n2, n3)
{
	var max = 1;
	var mayor=n1; /* Colocamos como mayor el valor de n1 */
	if (n2 > mayor)
	{ 
		mayor = n2; /* Si n2 es mayor guardarlo en la variable */
		max = 2;
	}
	if (n3 > mayor)
		max = 3;
	
	return max;
}

function contentHeight() 
{
	var altLeft = $("#contentLeft").height();
	var altCenter = $("#contentCenter").height();
//	var altCenter = $("#contentCenter").outerHeight();
	var altRight = $("#contentRight").height();
//	alert("left: "+altLeft+"\ncenter: "+altCenter+"\nright: "+altRight);
	var max = major(altLeft, altCenter, altRight);

	var juntaH = $("#juntaLaboral").height();
	
	if(max == 1)
	{
		$("#contentCenter").height(altLeft);
		$("#contentRight").height(altLeft);
		$("#content").height(altLeft);
		$("#juntaLaboral").css("top",(248+altLeft-juntaH) + "px");
	}
	else if(max == 2)
	{
		$("#contentLeft").height(altCenter);
		$("#contentRight").height(altCenter);
		$("#content").height(altCenter);
		$("#juntaLaboral").css("top",(248+altCenter-juntaH) + "px");
	}
	else
	{
		$("#contentLeft").height(altRight);
		$("#contentCenter").height(altRight);
		$("#content").height(altRight);
		$("#juntaLaboral").css("top",(248+altRight-juntaH) + "px");
	}
}

function submenu2()
{
	$(".submenu2a").mouseover(function() {
		$(this).addClass("hover");
	}).mouseout(function(){
		$(this).removeClass("hover");
	});
}

function submenu3()
{
	$('.submenu3').each(function() {
		$(this).height($(this).children(".submenu3t").height());
	});
}

function canviCB(obj, id)
{
	if (obj.checked) document.getElementById(id).value = '1';
	else document.getElementById(id).value = '0';
}

function EnviarRecomana(obligatori, noCorrecte, emailPersona, nomPersona, emailAmic, missatge)
{
  var f = document.forms["samsaraRecomanaForm"];
  var errorsStr = "";
  
  if(f.elements['recomana.emailPersona'].value.trim() == "") errorsStr = errorsStr.concat(replaceValidacio(obligatori, emailPersona));
  else
  {
      if (!f.elements['recomana.emailPersona'].value.checkEmail())
          errorsStr = errorsStr.concat(replaceValidacio(noCorrecte, emailPersona));
  }
  
  if(f.elements['recomana.nomPersona'].value.trim() == "") errorsStr = errorsStr.concat(replaceValidacio(obligatori, nomPersona));
  
  if(f.elements['recomana.emailAmic'].value.trim() == "") errorsStr = errorsStr.concat(replaceValidacio(obligatori, emailAmic));
  else
  {
      if (!f.elements['recomana.emailAmic'].value.checkEmail())
          errorsStr = errorsStr.concat(replaceValidacio(noCorrecte, emailAmic));
  }
  
  if(f.elements['recomana.missatge'].value.trim() == "") errorsStr = errorsStr.concat(replaceValidacio(obligatori, missatge));
  
  if(errorsStr == "") f.submit();
  else alert(errorsStr);
}

function replaceValidacio(frase, camp)
{
	return frase.replace("XXXX", camp);
}

function solicitarEntrevista(obligatori, noCorrecte, noValid, nom, cognom1, cognom2, email, ciutat, edat, sexe, coneixGabinet, idioma, telefon)
{
  var f = document.forms["solicitudEntrevistaForm"];
  var errorsStr = "";
  
  if(f.elements['contacte.nom'].value.trim() == "") errorsStr = errorsStr.concat(replaceValidacio(obligatori, nom));
  //if(f.elements['contacte.cognom1'].value.trim() == "") errorsStr = errorsStr.concat(replaceValidacio(obligatori, cognom1));
  //if(f.elements['contacte.cognom2'].value.trim() == "") errorsStr = errorsStr.concat(replaceValidacio(obligatori, cognom2));
  if(f.elements['contacte.telefon'].value.trim() == "") errorsStr = errorsStr.concat(replaceValidacio(obligatori, telefon));
  
  //if(f.elements['contacte.email'].value.trim() == "") errorsStr = errorsStr.concat(replaceValidacio(obligatori, email));
  //else
  if(!f.elements['contacte.email'].value.trim() == "")
  {
      if (!f.elements['contacte.email'].value.checkEmail())
          errorsStr = errorsStr.concat(replaceValidacio(noCorrecte, email));
  }
  
  if(f.elements['contacte.ciutat'].value.trim() == "") errorsStr = errorsStr.concat(replaceValidacio(obligatori, ciutat));
  if(f.elements['contacte.edatC'].value.trim() == "") errorsStr = errorsStr.concat(replaceValidacio(obligatori, edat));

  if($("#sexe").val() == "") errorsStr = errorsStr.concat(replaceValidacio(noValid, sexe));
  if($("#codiConeixGabinet").val() == "") errorsStr = errorsStr.concat(replaceValidacio(noValid, coneixGabinet));
  if($("#idiomaC").val() == "") errorsStr = errorsStr.concat(replaceValidacio(noValid, idioma));
  
  if(errorsStr == "") f.submit();
  else alert(errorsStr);
}

function canviIdioma(formName)
{
	var f = document.forms[formName];
	f.submit();
}

this.imagePreview = function(){	
	/* CONFIG */
		
		//xOffset = 10;
		//yOffset = 30;

		xOffset = 320;
		yOffset = -150;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
		/* END CONFIG */
		$("a.preview").hover(function(e){
			this.t = this.title;
			this.title = "";	
			var c = (this.t != "") ? "<br/>" + this.t : "";
			$("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");								 
			$("#preview").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px").fadeIn("fast");						
    },
		function(){
			this.title = this.t;
			$("#preview").remove();
	  });	
		$("a.preview").mousemove(function(e){
			$("#preview").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px");
		});			
};

function theRotator() {
	//Set the opacity of all images to 0
	$('div.rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div.rotator ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	
	setInterval('rotate()',6000);
	
}

function rotate() {	
	//Get the first image
	var current = ($('div.rotator ul li.show')?  $('div.rotator ul li.show') : $('div.rotator ul li:first'));

    if ( current.length == 0 ) current = $('div.rotator ul li:first');

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator ul li:first') :current.next()) : $('div.rotator ul li:first'));
	
	//Un-comment the 3 lines below to get the images in random order
	
	//var sibs = current.siblings();
    //var rndNum = Math.floor(Math.random() * sibs.length );
    //var next = $( sibs[ rndNum ] );
			

	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
};
