// JavaScript Document

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function isEmailAddress(email){
	var s = email;
	var filter=/^[A-Za-z][-.\w]*[A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ){
		return true;
	}
	
	if (filter.test(s)){
		return true;
	}else{
		//alert("Ingrese una dirección de correo válida");
		return false;
	}
}

function objetoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	   xmlhttp = new XMLHttpRequest();	
	}
	return xmlhttp;
}

function enviar(){
	var ok;
	ok=1;
	
	try{
		if(document.formulari.nom_ob.value=="1"){
			if(document.formulari.nom.value==""){
				ok=0;	
			}
		}
	}catch(e){
		//Salta error
	}
	
	try{
		if(document.formulari.cognom_ob.value=="1"){
			if(document.formulari.cognom.value==""){
				ok=0;	
			}
		}
	}catch(e){
		//Salta error
	}
	
	try{
		if(document.formulari.empresa_ob.value=="1"){
			if(document.formulari.empresa.value==""){
				ok=0;	
			}
		}
	}catch(e){
		//Salta error
	}
	
	try{
		if(document.formulari.adresa_ob.value=="1"){
			if(document.formulari.adresa.value==""){
				ok=0;	
			}
		}
	}catch(e){
		//Salta error
	}
	
	try{
		if(document.formulari.cp_ob.value=="1"){
			if(document.formulari.cp.value==""){
				ok=0;	
			}
		}
	}catch(e){
		//Salta error
	}
	
	try{
		if(document.formulari.ciutat_ob.value=="1"){
			if(document.formulari.ciutat.value==""){
				ok=0;	
			}
		}
	}catch(e){
		//Salta error
	}
	
	try{
		if(document.formulari.provincia_ob.value=="1"){
			if(document.formulari.provincia.value==""){
				ok=0;	
			}
		}
	}catch(e){
		//Salta error
	}
	
	try{
		if(document.formulari.telefon_ob.value=="1"){
			if(document.formulari.telefon.value==""){
				ok=0;	
			}
		}
	}catch(e){
		//Salta error
	}
	
	try{
		if(document.formulari.mobil_ob.value=="1"){
			if(document.formulari.mobil.value==""){
				ok=0;	
			}
		}
	}catch(e){
		//Salta error
	}
	
	try{
		if(document.formulari.fax_ob.value=="1"){
			if(document.formulari.fax.value==""){
				ok=0;	
			}
		}
	}catch(e){
		//Salta error
	}
	
	try{
		if(document.formulari.email_ob.value=="1"){
			if(document.formulari.email.value==""){
				ok=0;	
			}else{
				var correcte;
				correcte=isEmailAddress(document.formulari.email.value);
				if(correcte==false){
					ok=0;
				}
			}
		}
	}catch(e){
		//Salta error
	}
	
	try{
		if(document.formulari.comentari_ob.value=="1"){
			if(document.formulari.comentari.value==""){
				ok=0;	
			}
		}
	}catch(e){
		//Salta error
	}
	
	try{
		if(document.formulari.archivo_ob.value=="1"){
			if(document.formulari.archivo.value==""){
				ok=0;	
			}
		}
	}catch(e){
		//Salta error
	}
	
	
	if(ok){
		//alert("envie'm el formulari");
		document.getElementById("bt_enviar").innerHTML="enviando...";
		document.formulari.submit();
	}else{
		alert(sms);	
	}
}

function contacte(){
	var error=0;
	if(document.form_contacte.persona_contacte.value==""){
		error=1;
		document.getElementById("persona").style.color="#BC2A15";
	}else{
		document.getElementById("persona").style.color="#28282A";
	}
	
	if(document.form_contacte.email.value==""){
		error=1;
		document.getElementById("mail").style.color="#BC2A15";
	}else{
		var correcte;
		correcte=isEmailAddress(document.form_contacte.email.value);
		if(correcte==false){
			error=1;
			document.getElementById("mail").style.color="#BC2A15";
		}else{
			document.getElementById("mail").style.color="#28282A";	
		}
		
	}
	
	if(document.form_contacte.comentaris.value==""){
		error=1;
		document.getElementById("comments").style.color="#BC2A15";
	}else{
		document.getElementById("comments").style.color="#28282A";
	}
	
	if(!error){
		//alert("enviem");
		document.form_contacte.submit();
		//document.getElementById("bt_petit").innerHTML="enviant...";
	}
}

function puntuar(name,id,ip,unitats){
	ajax=objetoAjax();
	ajax.open("POST", "db.php?j="+name+"&q="+id+"&t="+ip+"&c="+unitats);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			ajax2=objetoAjax();
			ajax2.open("POST", "mostra_puntuacio.php?id="+id);
			ajax2.onreadystatechange=function() {
				if (ajax2.readyState==4) {
					//alert(ajax2.responseText);
					$(".puntuacio"+id).html(ajax2.responseText);
				}
			}
			ajax2.send(null);
		}else{
			$(".puntuacio"+id).html("calculant...");
		}
	}
	ajax.send(null);
}

function enviar_amic(){
	var nom=$("#nom").val();
	var email=$("#email").val();
	var emails_amics=$("#emails_amics").val();
	var comentaris=$("#comentaris").val();
	
	if(nom!="" && email!="" && emails_amics!=""){
		if(isEmailAddress(email)){
			alert("enviem");
		}
	}
}

$(document).ready(function(){
	$('#boto').hover(function(){
		$(this).addClass("boto-hover-jquery");
	},
	function(){
		$(this).removeClass("boto-hover-jquery");
	});
	
	$("#navigation #boto-menu").each(function(){
		$(this).hover(function(){
			$(this).addClass("boto-menu-hover-jquery");
		},
		function(){
			$(this).removeClass("boto-menu-hover-jquery");
		});
	});
	
	$("#leftcolumn a#boto2").each(function(){
		$(this).hover(function(){
			$(this).addClass("boto-hover-categoria-jquery");
		},
		function(){
			$(this).removeClass("boto-hover-categoria-jquery");
		});
	});
	
	$("#botons a#boto").each(function(){
		$(this).hover(function(){
			$(this).addClass("boto-hover-jquery");
		},
		function(){
			$(this).removeClass("boto-hover-jquery");
		});
	});
	
	$("a#avis_legal").fancybox({
		'overlayShow'	: true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'width'			: 610,
		'height'		: 520,
		'type'			: 'iframe'
	});
	
	$("a#proteccio_dades").fancybox({
		'overlayShow'	: true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'width'			: 610,
		'height'		: 520,
		'type'			: 'iframe'
	});
	
	$("a#banner_fancy").each(function(){
		$(this).fancybox({
			'overlayShow'	: true,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic'
		});
	});
	
	$("a#galeria").each(function(){
		$(this).fancybox({
			'overlayShow'	: true,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic'
		});
	});
	
	$("a#enviar_amic").fancybox({
		'overlayShow'	: true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'width'			: 610,
		'height'		: 520,
		'type'			: 'iframe'
	});
	
	$('#slider').nivoSlider({
		effect:'sliceDownLeft',
		controlNav:true
	});
	
	$('.nivo-quisom').nivoSlider({
		effect:'fade',
		controlNav:false,
		directionNav:false,
		pauseOnHover:false
	});
	
});
