//var ruta="http://localhost/sarda/";
var ruta="http://www.joansarda.com/";
var colorTag=0;

function cambiarIdioma(pagina,idioma,stringquery){
	intPos = stringquery.indexOf("&");
	if(intPos!=-1){
		stringquery = stringquery.substr(intPos + 1,stringquery.length - intPos);
	}
	intPosIdioma = stringquery.indexOf("idioma");
	if(intPosIdioma!=-1){
		stringquery = "";
	}
	window.location.href = pagina + "?idioma=" + idioma + "&" + stringquery;
	//window.location.href = "default.asp?idioma=ca";

}

var estat = 0;

function creaAjax(){
  var objetoAjax=false;
  try {
   /*Para navegadores distintos a internet explorer*/
   objetoAjax = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try {
     /*Para explorer*/
     objetoAjax = new ActiveXObject("Microsoft.XMLHTTP");
     } 
     catch (E) {
     objetoAjax = false;
   }
  }

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

function obrir_submenu(tab,lang, sublloc){	
	var ajax=creaAjax();	
	ajax.open ('POST',ruta+"includes/submenus.php", true);
	ajax.onreadystatechange = function() {
		//document.getElementById(tab).innerHTML=ajax.responseText; 
		//if (ajax.readyState==1) {
               //  capaContenedora.innerHTML="Carregant.......";
         //}
         //else 
		 if (ajax.readyState==4){
            //if(ajax.status==200)
            //{
				document.getElementById(tab).innerHTML=ajax.responseText; 
			//}
		 }
	}	
	
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("tab="+tab+"&lang="+lang+"&sublloc="+sublloc);
		
	return
}

function desplegar_menus(lloc, lang, sublloc){
	if (lloc == "productes") obrir_submenu("tab1", lang, sublloc)
	else if (lloc == "celebracions") obrir_submenu("tab2", lang, sublloc)
	else if (lloc == "capcreus"){
		//moure_carousel('inici', 4);
		obrir_submenu("tab3", lang, sublloc);
	}
}

/* --------------------------------------------------- */
/*	FUNCIONS PER LES PÀGINES PRINCIPALS DELS PRODUCTES */
/* --------------------------------------------------- */

//funció per canviar l'estil quan es passa el mouse per sobre de la foto d'un producte
function canviar_estil_imatge_producte(accio,id){
	if(accio == "over")	{
		document.getElementById("i"+id).className="producte_over";
		document.getElementById("t"+id).style.color="#666633";
	}else{ 
		document.getElementById("i"+id).className="producte_out";
		document.getElementById("t"+id).style.color="#FFFFFF";
	}
}

//funció per canviar la imatge i l'estil de la cistella quan hi passes el ratolí per sobre
function canviar_imatge_cistella(accio){

	if(accio == "over")
	{
		document.images["imatge_cistella"].src = "../img/carro_over.gif";
		document.getElementById("link_cistella").className = "link_cistella_over";
	}else{
		document.images["imatge_cistella"].src = "../img/carro.gif";
		document.getElementById("link_cistella").className = "link_cistella";
	}

}

/* --------------------------------------- */
/* FUNCIONS PER A A LA PÀGINA DELS USUARIS */
/* --------------------------------------- */

function cambiar(tag, email, dni, nom, cognoms, tipus_via, direccio, numero, pis_porta, cp, poblacio, provincia, telefon, fax, contrassenya_vella, contrassenya_nova1, contrassenya_nova2, submit_done){
	var ajax=creaAjax();
	var capaContenedora = document.getElementById("content");
	var lloc = "noticies";
	switch(tag){
		case 1:
			lloc = "inici";
			break;
		case 2:
			lloc = "compres";
			break;
		case 3:
			lloc = "perfil";
			break;
		case 4:
			lloc = "perfil_update";
			break;
		case 5:
			lloc = "perfil_update_contrassenya";
			break;
	}
	
	if(email) lloc = "perfil_update";
	else if(contrassenya_nova1) lloc = "perfil_update_contrassenya";
	ajax.open ('POST',ruta+"users/"+lloc+".php", true);
	
	ajax.onreadystatechange = function() {
		 if (ajax.readyState==1) {
                 capaContenedora.innerHTML="Carregant.......";
         }
         else if (ajax.readyState==4){
            if(ajax.status==200)
            {
				document.getElementById("content").innerHTML=ajax.responseText; 
			}
		 }
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("email="+email+"&dni="+dni+"&nom="+nom+"&cognoms="+cognoms+"&tipus_via="+tipus_via+"&direccio="+direccio+"&numero="+numero+"&pis_porta="+pis_porta+"&cp="+cp+"&poblacio="+poblacio+"&provincia="+provincia+"&telefon="+telefon+"&fax="+fax+"&contrassenya_vella="+contrassenya_vella+"&contrassenya_nova1="+contrassenya_nova1+"&contrassenya_nova2="+contrassenya_nova2+"&submit_done="+submit_done);
	
	//modifiquem el color de letiqueta corresponent
	if(tag==1) {
		colorTag=1;
		document.getElementById("tag1").style.backgroundColor = "#B4B45A";
		document.getElementById("tag2").style.backgroundColor = "black";
		document.getElementById("tag3").style.backgroundColor = "black";
		document.getElementById("content").style.borderColor = "#B4B45A";
		document.getElementById("tag1").style.color = "black";
		document.getElementById("tag2").style.color = "white";
		document.getElementById("tag3").style.color = "white";
	}else if(tag==2){
		colorTag=2;
		document.getElementById("tag2").style.backgroundColor = "#B4B45A";
		document.getElementById("tag1").style.backgroundColor = "black";
		document.getElementById("tag3").style.backgroundColor = "black";
		document.getElementById("content").style.borderColor = "#B4B45A";
		document.getElementById("tag2").style.color = "black";
		document.getElementById("tag2").style.color = "black";
		document.getElementById("tag1").style.color = "white";
		document.getElementById("tag3").style.color = "white";
	}else if(tag==3 || tag==4 || tag==5){
		colorTag=3;
		document.getElementById("tag3").style.backgroundColor = "#B4B45A";
		document.getElementById("tag2").style.backgroundColor = "black";
		document.getElementById("tag1").style.backgroundColor = "black";
		document.getElementById("content").style.borderColor = "#B4B45A";
		document.getElementById("tag3").style.color = "black";
		document.getElementById("tag3").style.color = "black";
		document.getElementById("tag2").style.color = "white";
		document.getElementById("tag1").style.color = "white";
	}
	return
}

//per modificar el color dels tags quan hi passa el ratolí per sobre
function color(tag){
	if(tag==1 && colorTag!=1){
		document.getElementById("tag1").style.backgroundColor = "#666633";
		document.getElementById("tag1").style.color = "black";
	}
	else if(tag==2 && colorTag!=2){
		document.getElementById("tag2").style.backgroundColor = "#666633";
		document.getElementById("tag2").style.color = "black";
	}
	else if(tag==3 && colorTag!=3) {
		document.getElementById("tag3").style.backgroundColor = "#666633";
		document.getElementById("tag3").style.color = "black";
	}
	
}

//per modificar el color dels tags quan hi passa el ratolí per sobre
function color2(tag){
	if(tag==1) {
		if(colorTag!=1){
			document.getElementById("tag1").style.backgroundColor = "black";
		 	document.getElementById("tag1").style.color = "white";
		}
	}
	else if(tag==2){
		if(colorTag!=2){
			document.getElementById("tag2").style.backgroundColor = "black";
			document.getElementById("tag2").style.color = "white";
		}
	}
	else if(tag==3) {
		if(colorTag!=3){
			document.getElementById("tag3").style.backgroundColor = "black";
		 	document.getElementById("tag3").style.color = "white";
		}
	}
	
	
}

/* Funció per fer submit al form del update perfil */
function form_update_perfil(){
	document.form_update_perfil.submit();
}


/* -------------------------------- */
/* FUNCIONS PER A LA BOTIGA VIRTUAL */
/* -------------------------------- */

/* Funció que es crida quan s'afegeix un producte a la cistella */

function form_submit(formulari){
	if (formulari == "form_cistella") document.form_cistella.submit();
}

/* Funció que canvia la quantitat d'elements d'un producte */

function canviar_quantitat(id){
	/*alert(document.getElementById("quantitat").value);*/
	//document.frm2.submit();
	var id2 = document.getElementById("id_"+id).value;
	var quantitat = document.getElementById("quantitat_"+id).value;
	window.location = "cistella.php?quantitat="+quantitat+"&id2="+id2;
}


/* ----------------------------------------- */
/* FUNCIONS PER A L'APARTAT DEL CAL DE CREUS */
/* ----------------------------------------- */

//CAROUSEL

//funció per fer correr el carousel cap a la dreta i esquerre
function moure_carousel(accio, num_imatges){
	if(accio == "inici"){
		imatge_inici=1;
	}else if(accio == "dreta"){
		imatge_inici++;
	}else if(accio == "esquerre"){
		imatge_inici--;
	}

	//Mirem quina fletxa hem de mostrar a l'esquerre
	/*if(imatge_inici < 2){
		document.getElementById("boto_esquerre").style.visibility = "hidden";
	}else{
		document.getElementById("boto_esquerre").style.visibility = "visible";
	}
	
	//mirem quina fletxa hem de mostrar a la dreta
	if(imatge_inici > num_imatges - 4){
		document.getElementById("boto_dreta").style.visibility = "hidden";
	}else{
		document.getElementById("boto_dreta").style.visibility = "visible";
	}*/
	
	//mostrem les imatges que toquen
	var i=imatge_inici;
	
	document.images["img1_img"].src = "img/fotos_vinyes/petites/"+i+".jpg";	
	document.getElementById("img1_link").href = "img/fotos_vinyes/grans/"+i+".jpg";
	
	i++;
	document.images["img2_img"].src = "img/fotos_vinyes/petites/"+i+".jpg";	
	document.getElementById("img2_link").href = "img/fotos_vinyes/grans/"+i+".jpg";
	
	i++;
	document.images["img3_img"].src = "img/fotos_vinyes/petites/"+i+".jpg";	
	document.getElementById("img3_link").href = "img/fotos_vinyes/grans/"+i+".jpg";
	
	i++;
	document.images["img4_img"].src = "img/fotos_vinyes/petites/"+i+".jpg";	
	document.getElementById("img4_link").href = "img/fotos_vinyes/grans/"+i+".jpg";
	
}