function sel_cat(anchor){
	$("#left-column li[class=main_cat] a[rel="+anchor.rel+"]").each(function(){
		if ($(this).hasClass("minus"))
			$(this).attr("class","plus");
		else
			$(this).attr("class","minus");
	});
	$("#left-column li[class~="+anchor.rel+"]").each(function(){
		if ($(this).hasClass("cat_selected"))
			$(this).removeClass("cat_selected");
		else
			$(this).addClass("cat_selected");
	});
}

function occat(cat_id)
{
	var catid = document.getElementById(cat_id);
	if (catid)
	{
		if (catid.style.display == 'block')
			catid.style.display = 'none';
		else
			catid.style.display = 'block';
	}
}

function cat(catstring)
{
	var form2 = document.forms['ricerca2'];
	form2.categoria.value = catstring;
	form2.submit();
}

function ord(ordinamento)
{
	var form2 = document.forms['ricerca2'];
	form2.ordinamento.value = ordinamento;
	form2.submit();
}

function segnalalink(tagid,id)
{

	  // variabili di funzione
	  var
	    // assegnazione oggetto XMLHttpRequest
	    ajax = assegnaXMLHttpRequest(),
	    // assegnazione elemento del documento
	    elemento = prendiElementoDaId(tagid),
	    // risultato booleano di funzione
	    usaLink = true;
	  
	  // se l'oggetto XMLHttpRequest non e' nullo
	  if(ajax) {
	    // il link al file non deve essere usato
	    usaLink = false;
	
	    // impostazione richiesta asincrona in GET
	    // del file specificato
	    ajax.open("get", "/brokenlink.php?id="+id, true);
		
	    // rimozione dell'header "connection" come "keep alive"
	    ajax.setRequestHeader("connection", "close");
	
	    // impostazione controllo e stato della richiesta
	    ajax.onreadystatechange = function() {
	      
	      // verifica dello stato
	      if(ajax.readyState === readyState.COMPLETATO) {
	        // verifica della risposta da parte del server
	        if(ajax.status == 200)
	        {
	          // operazione avvenuta con successo
		alert("Il link e' stato segnalato. Grazie per la collaborazione!");
	          		elemento.innerHTML = ajax.responseText;
	        }
	        else {
	          // errore di caricamento
	          elemento.innerHTML = "";
	        }
	      } 
	    }
	
	    // invio richiesta
	    ajax.send(null);
		}
	   
	  return usaLink;
}

		function prendiElementoDaId(id_elemento) {
			var elemento;
			if(document.getElementById)
				elemento = document.getElementById(id_elemento);
			else
				elemento = document.all[id_elemento];
			return elemento;
		};

		function assegnaXMLHttpRequest() {
			var
				XHR = null,
				browserUtente = navigator.userAgent.toUpperCase();
			if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
				XHR = new XMLHttpRequest();
			else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
				if(browserUtente.indexOf("MSIE 5") < 0)
					XHR = new ActiveXObject("Msxml2.XMLHTTP");
				else
					XHR = new ActiveXObject("Microsoft.XMLHTTP");
			}
			return XHR;
		};

	// oggetto di verifica stato
		var readyState = {
			INATTIVO:	0,
			INIZIALIZZATO:	1,
			RICHIESTA:	2,
			RISPOSTA:	3,
			COMPLETATO:	4
		};

function chk_dispo(prodid)
{
	var container = $("#dispo_"+prodid);
	if (container)
	{
        container.html('<img src="/img/loading.gif" border="0" />');

        tabContentUrl="/chk_dispo.php?id="+prodid;
        $.ajax({
            url: tabContentUrl,
            success: function(data) {
                container.html(data);
            }
        });
	}
}

function chk_price(prodid)
{
	var container = $("#price_"+prodid);
	if (container)
	{
        container.html('<img src="/img/loading.gif" border="0" />');

        tabContentUrl="/chk_price.php?id="+prodid;
        $.ajax({
            url: tabContentUrl,
            success: function(data) {
                container.html(data);
            }
        });
	}
}

function chk_prod(myform)
{
	var returnValue = true;
	if (typeof myform.prodid != "undefined")
	{
		var ricercafrm = myform.prodid.value;
		if (ricercafrm	== '')
		{
			myform.prodid.value = '_';
		}
	}
	
	return returnValue;
}

function findValue(li) {
	if( li == null ) return alert("No match!");
	
	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];
	
	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	//alert("The value you selected was: " + sValue);
}

function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	return row[0];
}

function lookupAjax(){
	var oSuggest = $("#brandid")[0].autocompleter;
	oSuggest.findValue();
	return false;
}

function openwindow(el){
	window.open(el.href,'cosacosta');
	return false;
}

$(document).ready(function() {
	/*
	$("#brandid").autocomplete("/autocomplete.php", {	delay:10,
													minChars:3,
													matchSubset:1,
													matchContains:1,
													cacheLength:10,
													onItemSelect:selectItem,
													onFindValue:findValue,
													formatItem:formatItem,
													autoFill:true
												}
							);
	$("#brandid").keyup(function(e) {	
		if(e.keyCode == 13) {
			return false;
		}
	});
	*/
	$.mask.masks.prezzo = {mask: '999.999.999', type:'reverse'}
	$('input.input_prezzo_sx,input.input_prezzo_dx').setMask();
	if ($("#codici_sconto").length > 0)
	{
		$.ajax({
			url: "/codici_sconto.php?"+Math.random(),
			success: function(data) {
				$("#codici_sconto").html(data);
			}
		});
	}
});

