// Cambia label de particular a empresa dependiendo de la seleccion
function showAdvertiser(_advertiser, _username) {
	var Name = document.getElementById(_username);
	var companyAd = document.getElementById(_advertiser);

	Name.innerHTML = '';
	var inner_html = '';
	if (_advertiser == "anunciode2") {
		inner_html = "Nombre&nbsp;de&nbsp;empresa:";
	} else {
		inner_html = "Nombre:";
	}
	Name.innerHTML = inner_html;
}

// Cookie Functions
// save/read/delete cookie functions for storing small chunks of data in the browser

function saveCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000))
		var expires = "; expires="+date.toGMTString()
	}
	else expires = ""
	document.cookie = name+"="+value+expires+"; path=/"
}

function readCookie(name) {
	var nameEQ = name + "="
	var ca = document.cookie.split(';')
	for(var i=0;i<ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length)
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length)
	}
	return null
}

function deleteCookie(name) {
	saveCookie(name,"",-1)
}

// Funciones para Mis Anuncios
var activar = new Object() ;

function favOn(id, txt, path) {
    favObj = document.getElementById("favorito"+id) ;
    favObj.src = path+"/images/clip-on.png" ;
    favObj.title = txt ;
}

function favOff(id, txt, path) {
    favObj = document.getElementById("favorito"+id) ;
    favObj.src = path+"/images/clip-off.png" ;
    favObj.title = txt ;
}

function changeFavIcon(id, txt_sub, txt_add, path) {
    if (activar["key_"+id] == undefined) activar["key_"+id] = false ;
    activar["key_"+id] = !activar["key_"+id] ;

    if (activar["key_"+id]) {
        favOn(id, txt_sub, path) ;
        meteEnCookie(id) ;
        //sumamos 1 al total
        add2total() ;
    } else {
        favOff(id, txt_add, path) ;
        sacaDeCookie(id) ;
        //restamos 1 al total
        sub2total() ;
    }
}

/* Las mismas pero cambiando ahora tambien el texto */
function favOnText(id, txt, path) {
    favObj = document.getElementById("favorito"+id) ;
    favObj.src = path+"/images/clip-on.png" ;
    favObj.title = txt ;

    //también hemos de cambiar el texto
    favTextObj = document.getElementById("favoritoText"+id) ;
    favTextObj.innerHTML = txt ;
    favTextObj.title = txt ;
}

function favOffText(id, txt, path) {
    favObj = document.getElementById("favorito"+id) ;
    favObj.src = path+"/images/clip-off.png" ;
    favObj.title = txt ;
    
    //también hemos de cambiar el texto
    favTextObj = document.getElementById("favoritoText"+id) ;
    favTextObj.innerHTML = txt ;
    favTextObj.title = txt ;
}

function changeFavIconText(id, txt_sub, txt_add, path) {
    if (activar["key_"+id] == undefined) activar["key_"+id] = false ;
    activar["key_"+id] = !activar["key_"+id] ;

    if (activar["key_"+id]) {
        favOnText(id, txt_sub, path) ;
        meteEnCookie(id) ;
        //sumamos 1 al total
        add2total() ;
    } else {
        favOffText(id, txt_add, path) ;
        sacaDeCookie(id) ;
        //restamos 1 al total
        sub2total() ;
    }
}

/* Funcion que cambia el texto para mostrar el telefono del anuncio*/
function showPhone(txt){
    favTextObj = document.getElementById("showphone") ;
    favTextObj.innerHTML = txt ;
    favTextObj.title = txt ;	
}

/* FUNCIONES PARA COOKIE */
function sacaDeCookie(id) {
    //sacamos de la cookie
    strFav = readCookie("fav");
    finder = ","+id+"," ;
    pos = strFav.indexOf(finder);
    fav = strFav.substr(0, (pos+1)) + strFav.substr((pos+finder.length), strFav.length) ;
    if (fav == ",") deleteCookie("fav") ;
    else saveCookie("fav", fav, 365) ;
}

function meteEnCookie(id) {
    //guardamos en la cookie
    strFav = readCookie("fav");
    if (strFav != null) strFav += id+"," ;
    else strFav = ","+id+"," ;    
    saveCookie("fav", strFav, 365) ;
}
/* FIN DE FUNCIONES PARA COOKIE */

function add2total() {
    total = document.getElementById("total_lista") ;
    if (total) 
    {
        value = parseInt(total.innerHTML)+1 ;
        total.innerHTML = value ;
    }
}

function sub2total() {
    total = document.getElementById("total_lista") ;
    if (total) 
    {
        value = parseInt(total.innerHTML)-1 ;
        total.innerHTML = value ;
    }
}

function loadtotal(value) {
    total = document.getElementById("total_lista") ;
    if (total) 
    {
        total.innerHTML = value ;
    }
}

function load_if_fav(id, texto_sub, path) {
    strFav = readCookie("fav") ;
    if(strFav){
        if (strFav.indexOf(","+id+",") != -1)
        {
            activar["key_"+id] = true ;
            favOn(id, texto_sub, path) ;
        }
    }
}

function vacia_mi_lista(id) {
   if (id == "") 
   {
       deleteCookie("fav") ;
   } else {
        sacaDeCookie(id) ;           
    }
   }


// Carga la cookie y muestra los anuncios marcados como mis anuncios
window.onload = function() {
    strFav = readCookie("fav") ;
    if (strFav != null) 
    {
        aFav = strFav.split(",");
        for (var id in aFav)
	    {
            if (document.getElementById("favorito"+aFav[id])) load_if_fav(aFav[id], txt_1, txt_2) ;	
        }
	total = document.getElementById("total_lista") ;	
	total.innerHTML = aFav.length-2 ;
    }
}

// Limita el tamaño del textarea
function maxTxt(field, maxlimit) {
	if (field.value.length > maxlimit) 
	field.value = field.value.substring(0, maxlimit);
}


function recordarPassAd(ad){
        var url = 'http://canariasanuncia.com/?page_id=112';
        var pars = 'anuncio='+ad;
        var myAjax = new Ajax.Updater('eliminaranuncio', url, { method: 'get', parameters: pars });
}



// Elimina el div que contiene la imagen
function removeDiv(imagediv) {
	// Obtenemos el elemento
	var elemento = document.getElementById(imagediv);
	// Obtenemos el padre de dicho elemento
	// con la propiedad “parentNode”
	var padre = elemento.parentNode;
	// Eliminamos el hijo (el) del elemento padre
	padre.removeChild(elemento);
	// Mostramos el input file
	vInput();
}

// Muestra el input file
function vInput()
{	
  var testFrame = document.getElementById("upload");

  // if contentDocument existe, entonces es compatible con el W3C (Mozilla)
  if (testFrame.contentDocument){
    doc = testFrame.contentDocument;
  } else {
    // IE
    doc = document.frames["upload"].document;
  }
  //var doc = testFrame.contentDocument;
  //if (doc == undefined || doc == null)
      //doc = testFrame.contentWindow.document;
  doc.getElementById("file").disabled = false;
}


// Top bar
	window.topbar={};(function(){;var a=window.topbar,b,h,j,k="visible";function p(c,g,f){c.visibility=c.visibility==k?"hidden":k;c.left=g+"px";c.top=f+"px"}a.tg=function(c){var g=0,f,d,l,e=0,i=0,m=window.navExtra;!h&&(h=document.getElementById("topbar"));!j&&(j=h.getElementsByTagName("span"));(c||window.event).cancelBubble=true;if(!b){b=document.createElement(Array.every||window.createPopup?"iframe":"div");b.frameBorder="0";b.id="gbi";b.scrolling="no";b.src="#";document.body.appendChild(b);if(m&&j[8])for(var q in m){var n=document.createElement("span");n.appendChild(m[q]);n.className="gb2";j[0].parentNode.insertBefore(n,j[8])}document.onclick=a.close}for(;j[e];e++){d=j[e];l=d.className;if(l=="gb3"){f=d.offsetLeft;while(d=d.offsetParent)f+=d.offsetLeft}else if(l=="gb2"){p(d.style,f+1,25+g);i=Math.max(i,d.offsetWidth);g+=20}}for(e=0;j[e];e++){if(j[e].className=="gb2"){j[e].style.width=i+"px"}}var o=b.style;o.height=g+"px";o.width=i+"px";p(o,f,22)};a.close=function(c){b&&b.style.visibility==k&&a.tg(c)};})();
	

