var messaggio="tasto destro disabilitato";
var finestrazoom=null;

function confermaEliminaProprioAnnuncio(idannuncio)
{
  conf=confirm("Eliminare questo annuncio?");
  if (conf) EliminaProprioAnnuncio(idannuncio);
}

function modificaProprioAnnuncio(idannuncio)
{
  document.location.href=("?pag=modificaproprioannuncio&idannuncio="+idannuncio);
}

function EliminaProprioAnnuncio(idannuncio)
{
  referer=document.location;
  document.location.href=("eliminaProprioAnnuncio.php?idannuncio="+idannuncio);
}

function mostraModuloRispondia()
{
	mostra("div_modulorispondi");
	document.getElementById("rispondia_mittente_nome").focus();
}

function limitaCaratteri(evento)
{
  <!-- Limita l'inserimento di alcuni caratteri -->
  key =  evento.keyCode ? evento.keyCode : evento.which ? evento.which : evento.charCode;
  if ( (key==32) || (key==34) || (key==39) || (key > 32 && key <= 47) || (key > 57 && key < 65) || (key > 90 && key < 97) || (key > 122 && key < 127))
  { alert ('Carattere non consentito'); return (false); }
  else return true;
}

function comunicaCosto()
{ 
 var str="";
 var costo=""
 str=document.forms.formannuncio.categoria.options[document.forms.formannuncio.categoria.selectedIndex].text;
 if (str.indexOf('€')!=-1) 
 { 
   costo=str.substr(str.indexOf('€'),str.indexOf(')')-str.indexOf('€'));
   alert("L'annuncio in questa rubrica costa "+costo+"\nPotrai effettuare il pagamento con PayPal nella prossima pagina");
 }  
 return false;
}

function checkClick(e)
{
  if (document.all) 
  { if (event.button==2||event.button==3) 
     { alert(messaggio);	return false;	}
  }
  else if (document.layers||document.getElementById) 
  {	if (e.which == 3) 
    {	alert(messaggio); return false;	}
  }
}

function cerca()
{
   frm=document.forms.status;
   zona=frm.zona.value;
   var location="?pag=cerca"+"&cosacercare="+frm.cosacercare.value+"&cerca_categoria="+frm.cerca_categoria.options[frm.cerca_categoria.selectedIndex].value;
   
   if (zona.indexOf("provincia=")==0) location=location+"&provincia="+frm.zona.value.substring(new String("provincia=").length);
   else if (zona.indexOf("regione=")==0) location=location+"&regione="+frm.zona.value.substring(new String("regione=").length);
   //alert (location);
   document.location=location;
}

function cambia_zona(selObj)
{ 
  /* if (finestrazoom!=null) finestrazoom.close();
   if (finestrazoom!=null) finestrazoom.close();
   document.forms.status.provincia.value=selObj.options[selObj.selectedIndex].value;
   */
}


function conferma_cookie_provincia()
{
    var  unanno=365*24*60*60*1000;
    var expDate=new Date();
    expDate.setTime(expDate.getTime()+unanno);
    accetta=confirm ("RICORDA LA PROVINCIA ("+document.forms['status'].provincia.value+") ALLA PROSSIMA VISITA ?\r\r");
    if (accetta) 
    { document.cookie = "provincia="+escape(document.forms['status'].provincia.value)+"; expires="+expDate.toGMTString();}
}

function textCounter(field,counter,maxlimit,linecounter) 
{
	// text width//
	var fieldWidth =  parseInt(field.offsetWidth);
	var charcnt = field.value.length;        

	// trim the extra text
	if (charcnt > maxlimit) { field.value = field.value.substring(0, maxlimit);	}
	else 
	{ 
	// progress bar percentage
	var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
	document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/100)+"px";
	document.getElementById(counter).innerHTML=""+percentage+"%";
	// color correction on style from CCFFF -> CC0000
	setcolor(document.getElementById(counter),percentage,"background-color");
	}
}

function limitaCaratteriTextarea (field,maxlimit) 
{
	// text width//
	var fieldWidth =  parseInt(field.offsetWidth);
	var charcnt = field.value.length;        

	// trim the extra text
	if (charcnt > maxlimit) { field.value = field.value.substring(0, maxlimit);	}
}

function setcolor(obj,percentage,prop)
{
	obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}

function setPointer(theRow, thePointerColor, theNormalBgColor)
{
    var theCells=null;
    if(thePointerColor == '' || typeof(theRow.style) == 'undefined') {
        return false;
    }
    if(typeof(document.getElementsByTagName) != 'undefined') {
        theCells=theRow.getElementsByTagName('td');
    }
    else if(typeof(theRow.cells) != 'undefined') {
        theCells=theRow.cells;
    }
    else {
        return false;
    }
    var rowCellsCnt =theCells.length;
    var currentColor=null;
    var newColor    =null;
    // Opera does not return valid values with "getAttribute"
    if(typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined' && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor=theCells[0].getAttribute('bgcolor');
        newColor    =(currentColor.toLowerCase() == thePointerColor.toLowerCase())
                     ? theNormalBgColor
                     : thePointerColor;
        for (var c=0; c < rowCellsCnt; c++) {
            theCells[c].setAttribute('bgcolor', newColor, 0);
        } // end for
    }
    else {
        currentColor=theCells[0].style.backgroundColor;
        newColor    =(currentColor.toLowerCase() == thePointerColor.toLowerCase())
                     ? theNormalBgColor
                     : thePointerColor;
        for (var c=0; c < rowCellsCnt; c++) {
            theCells[c].style.backgroundColor=newColor;
        }
    }

    return true;
} // end of the 'setPointer()' function

function seleziona_deselezionaTutti(valore)
{
  n=document.forms[0].elements.length;
  for (i=0;i <n; i++)
    if (document.forms[0].elements[i].type=="checkbox")
	  document.forms[0].elements[i].checked=valore;
}

function visualizzaBarreProgresso(lunghezzamaxannuncio)
{
  n=document.forms[0].elements.length;
  for (i=0;i <n; i++)
  if (document.forms[0].elements[i].type=="textarea")
	textCounter(document.forms[0].elements[i],"progressbar"+(document.forms[0].elements[i].name.substr(document.forms[0].elements[i].name.indexOf('_'))),lunghezzamaxannuncio)
}


function validaformannuncio()
{  //frm e un form

  frm=document.forms.formannuncio;
 
  var messaggio="";
  var valorecampo="";
  var quanticampi=frm.elements.length;
  var requiredfields=frm.elements['requiredfields'].value.split(',');

  for(var i=0;i<requiredfields.length;i++)
  { //alert (frm[requiredfields[i]].name);
   field=frm[requiredfields[i]];
   switch (field.type)
   {
	 case "text":
		if (field.value=="")
		messaggio+="-"+field.name+"\n";
	 break;
	 case "textarea":
		if (field.value=="")
		messaggio+="-"+field.name+"\n";
	 break;
	 case "select-one":
		if (field.options[field.selectedIndex].value=="")
		{ 
		  messaggio+="-seleziona una ";
		  switch (field.name)
		  {
			 case "provinciadiresidenza":
				messaggio+="Provincia di Residenza\n";
			 break;
			 case "categoria":
				messaggio+="Categoria\n";
			 break;
			 case "portaleprovincia":
				messaggio+="Pubblica nel portale di ...\n";
			 break;
		   }
		 }
	 break;
   }//end switch
  }//end for
	 
  // caratteri non consentiti nel corpo del messaggio:
  if ((frm.elements['annuncio'].value.indexOf('<'))>-1)
     messaggio+="-il carattere < non è consentito nel corpo del messaggio!\n";
   if ((frm.elements['annuncio'].value.indexOf('>'))>-1)
     messaggio+="-il carattere > non è consentito nel corpo del messaggio!\n";
 
  if (messaggio.length>0)
     alert ("Devi compilare i campi:\r\n\r\n"+messaggio);
  else 
  { // chiede se si sono inseriti i dati per poter essere contattati
    //conferma=confirm('ATTENZIONE!!\r\n\r\nDevi inserire un numero di telefono, un\'email, un numero di fax, ecc per poter essere contattato dagli inserzionisti\r\n\r\nHai inserito tali contatti?\r\n\r\nRicorda che senza queste informazioni non potrai essere contattato !\r\n\r\nFai click su ANNULLA per tornare al messaggio e verificarlo\r\n\r\nFai click su OK per confermare l\'annuncio.');
	//if (conferma) frm.submit();
	
	 document.getElementById('agileUploaderSWF').submit();
  }
    
}//fine funzione


function openimage(URL)
{
  if (finestrazoom!=null) finestrazoom.close();
  finestrazoom=window.open (URL,"mywindow","resizable=yes, toolbar=no, menubar=no, status=no, scrollbars=yes");
}

function aprioroscopodelgiorno(segno)
{
  window.open ("oroscopodelgiorno.php?segno="+segno,"finestraoroscopo","width=450, height=300, toolbar=no,menubar=no, status=no, scrollbars=yes, resisable=yes");
}

function handleEnter (field, event) 
{
 var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
 if (keyCode == 13)
 {
   var i;
   for (i = 0; i < field.form.elements.length; i++)
   if (field == field.form.elements[i])
   break;
   i = (i + 1) % field.form.elements.length;
   field.form.elements[i].focus();
   return false;
 } 
 else return true;
}

function thumbnailBorder(thumb, image_num, ad_id)
{
  var styles = [];
	var ad_id = ad_id ? ad_id : "";

	if (typeof(thumb.style) == 'undefined') 
	{
		return false;
	}

	for (i = 1; i < (1+image_num); i++) {
		var thumb_obj = document.getElementById('thumb' + i + ad_id);
		if (!styles[i]) {
			styles[i] = thumb_obj.className;
			if (styles[i].indexOf('ad_border_solid_black') > 0) {
				styles[i] = styles[i].substring(0, styles[i].indexOf('ad_border_solid_black')) + 'ad_border_solid_grey';
			}
		}
		thumb_obj.className = styles[i];

		if (thumb.id == thumb_obj.id) {
			thumb_obj.className = "ad_thumb ad_border_solid_black";
			document.getElementById('display_image' + ad_id).className = 'ad_pict' + ((styles[i].indexOf('ad_border_dotted') > 0)?' ad_border_dotted':'');
		}
	}
}

function showLargeImage(strDisplayPath, ad_id, admin) 
{
	var ad_id = ad_id ? ad_id : "";
	var admin = admin ? admin : false;
	var image = document.getElementById("display_image" + ad_id).firstChild;


	if (admin)
	 {
		resizeImage(image, strDisplayPath, null, admin);
	} else 
	{
		if (navigator.userAgent.toLowerCase().indexOf('safari') > 0) 
		{
			var next_image = new Image;
			next_image.src = strDisplayPath;

			image.src = next_image.src;
			waitForNextImage(next_image, ad_id);
		} else {
			image.src = strDisplayPath;
		}
	}
}

/*
 * Display images. Show border and display large image
 */
var next_image;
function waitForNextImage(next_image, ad_id) {
	var ad_id = ad_id ? ad_id : "";
	var image = document.getElementById("display_image" + ad_id).firstChild;

	if (next_image.width > 0) {
		image.width = next_image.width;
		image.height = next_image.height;
		next_image = null;
	} else {
		setTimeout("waitForNextImage(next_image, ad_id)", 100);
	}
}

function nascondi(id)
{
var el=document.getElementById(id);
el.style.display="none";

}

function mostra(id)
{
var el=document.getElementById(id);
el.style.display="block";
}

function reset_cosacercare_focus()
 {
 var campo = document.getElementById('cosacercare');
 campo.style.color = '#000000';
 campo.style.fontStyle = 'normal';
 campo.value = '';
 }
 
 function reset_cosacercare_blur()
 {
 var campo = document.getElementById('cosacercare');
 if(campo.value.length > 0)  return;									 
 campo.style.color = '#ACA899';
 campo.style.fontStyle = 'italic';
 campo.value = 'Cosa cerchi ?';
 }
 
 function condividi_facebook() 
 {
   width=630;
   height=440;
   LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
   TopPosition = (screen.height) ? (screen.height-height)/2 : 0;
   
   settings="width="+width+", height="+height+",top="+TopPosition+",left="+LeftPosition+", toolbar=0,status=0,";
   
	 u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'facebooksharer',settings);return false;
 }
