function pinta_vermell( obj )
{
	if(obj.style.color=="rgb(204, 0, 0)")
	{
		obj.style.color="#666666";
	}
	else
	{
		obj.style.color="#CC0000";
	}
}

function abrir_pag( link , id )
{
	//alert(id);
	window.location=link+"?idi="+id;
}


function swapClass( id , c1 , c2 )
{
	if( $("#"+id).attr('class').search(c1) >= 0 ) //alert("si");;
	//if( $("#"+id).attr('class') == c1 )
	{
		//alert("quito "+c1+" por "+c2+"");
		$("#"+id).removeClass(c1);
		$("#"+id).addClass(c2);
		
		//$("#"+id).slideDown("slow"); //slow normal fast
	}
	else
	{
		//alert("quito "+c2+" por "+c1+"");
		if( $("#"+id).attr('class') == c2 )
		{
			$("#"+id).removeClass(c2);
		}
		$("#"+id).addClass(c1);
		
		//$("#"+id).slideUp("slow"); //slow normal fast
	}
}

function llamarPagina( url , id )
{
	div="#"+id;
	//$(div).html("Cargando...");
	$(div).html("");
	$('#peu_volatil').hide();
	$.ajax({
		type: 'GET',
		url: url,
		success: function(msg){
			$(div).html(msg);
		}
	});
}
function llamarPagina2( url2 , id2 )
{
	div2="#"+id2;
	//$(div2).html("Cargando...");
	$(div2).html("");
	$.ajax({
		type: 'GET',
		url: url2,
		success: function(msg2){
			if(msg2==''){
				location.href="http://www.peggysage.es/";
			}else{
				$(div2).html(msg2);
			}
		}
	});
}

function llamarPagina3( url3 , id3, referencia , paginacion)
{
	div2="#"+id3;
	//$(div2).html("Cargando...");
	$(div2).html("<img src='images/loading-spiral.gif' width='50px' style='margin:50px 200px;' />");
	$.ajax({
		type: 'POST',
		url: url3,
		data: "referencia="+referencia+"&paginacion="+paginacion,
		success: function(msg3){
			$('#peu_volatil').hide();
			$(div2).html(msg3);
		}
	});
}

function tornarEnrera ( cadena , nivell_ses )
{
	fragmentoTexto = cadena.split('_');
	if ( fragmentoTexto.length-1 > 2 )
	{
		id = fragmentoTexto[2];
		$("#tornarEnrera").html('<a class="tgris" style="border:0; text-decoration:none;" href="#" onclick="intro_menu_input( \''+id+'\' , \'branca\' , \''+nivell_ses+'\' );"><< VOLVER</a>');
	}
	else
	{
		$("#tornarEnrera").html("");
	}
}

function intro_menu_input ( id , tipus , nivel ){
	if (tipus == "branca"){
		var pag = "listado_productos.php?idi="+id;
	}
	else{
		valorTXT = document.getElementById('txtValClie').value;
		var pag = "detalle_producto.php?idi="+id+"&nvl="+nivel+"&validado="+valorTXT;
	}
	llamarPagina('funciones/returns_tree.php?id_fill='+id+'&tipus='+tipus+'&nivell='+nivel , 'tracta_menu' );
	document.getElementById('peu_volatil').innerHTML='';
	llamarPagina2( pag , 'continguts');
}
function intro_menu_input_busqueda ( id , ref,paginacion ){
	nivel='undefined';
	tipus='fulla';
	valorTXT = document.getElementById('txtValClie').value;
	var pag = "detalle_producto_busqueda.php?idi="+id+"&validado="+valorTXT+"&referencia="+ref+"&paginacion="+paginacion;
	
	llamarPagina('funciones/returns_tree.php?id_fill='+id+'&tipus='+tipus+'&nivell='+nivel , 'tracta_menu' );
	document.getElementById('peu_volatil').innerHTML='';
	llamarPagina2( pag , 'continguts');
	
}

function oculta_menu ()
{
	var cadena1 = $("#cami_menu").attr('value');
	var cadena2 = $("#cami_menu2").attr('value');
	
	var cadena = cadena2.substr(1,cadena2.length);
	cadena1 = cadena1 + cadena;
	document.getElementById('cami_menu').value = cadena1;
	
	// Agafo els id del cuadre 1.
	fragmentoTexto = cadena1.split('_');
	
	// Per a cada id agafat.
	for (i=1 ; i<fragmentoTexto.length-1 ; i++ )
	{
		id = fragmentoTexto[i];
		busca = new RegExp("_"+id+"_");
		
		// Si l'id no es troba al cuadre 2.
		if ( !busca.test(cadena2) )
		{
			$('#div2_llprod_'+id).hide();
			$('#span_titol_'+id).removeClass("menu_clicat");
		}
	}
	
	// Els inputs queden iniciats amb els valors adients.
	document.getElementById('cami_menu').value = document.getElementById('cami_menu2').value;
	document.getElementById('cami_menu2').value = "_";
}

function valida_form(){
	
	
	
	if(document.getElementById('tx_nom').value==''){
		alert('El campo Nombre no puede estar vac�o.');
		return false;
	}
	
	if(document.getElementById('tx_cognom').value==''){
		alert('El campo Apellido no puede estar vac�o.');
		return false;
	}
	
	if(document.getElementById('tx_telf').value==''){
		alert('El campo Tel�fono no puede estar vac�o');
		return false;
	}
	
	if(document.getElementById('tx_mail').value==''){
		alert('El campo Email no puede estar vac�o');
		return false;
	}
	
	if(document.getElementById('tx_dir').value==''){
		alert('El campo Direcci�n no puede estar vac�o.');
		return false;
	}
	
	if(document.getElementById('tx_cp').value==''){
		alert('El campo CP no puede estar vac�o.');
		return false;
	}
	
	if(document.getElementById('tx_localidad').value==''){
		alert('El campo Localidad no puede estar vac�o.');
		return false;
	}
	
	if(document.getElementById('tx_provincia').value==''){
		alert('El campo Provincia no puede estar vac�o.');
		return false;
	}
	
	if(document.getElementById('tx_pais').value==''){
		alert('El campo Pais no puede estar vac�o.');
		return false;
	}
	
	if(document.getElementById('aviso').checked==false){
		alert('Debe aceptar las condiciones generales de uso');
		return false;
	}
		
	return true;
	
}
function obj_ajax(){
	var ajax_loc;
	
	if( window.XMLHttpRequest ){
		ajax_loc = new XMLHttpRequest(); // No Internet Explorer		
	}else{
		try{
			ajax_loc = new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer
		}catch(err1){
			try{
				ajax_loc = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(err2){
				alert('Error version de navegador no soprtada');
				return 0;
			}
		}
	}
	return ajax_loc;
}
function buscar()
	{
	var referencia = $('#form_buscar #referencia').val();
	$('#msg_referencia').text('');
	if (referencia!="" && referencia.length>=3)
		{	
			$('#continguts').height(350);
			llamarPagina3( 'buscar_referencia.php' , 'continguts', referencia);
		}else{
			$('#msg_referencia').html('Debe escribir m&iacute;nimo 3 caracteres');
			
		}	
		return false;
	}
function busqueda(referencia,paginacion)
	{
	
	if (referencia!="")
		{	
			
			llamarPagina3( 'buscar_referencia.php' , 'continguts', referencia, paginacion);
		}	
		return false;
	}
