var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
   xmlhttp=false
  }
 }
@else
 xmlhttp=false
@end @*/


if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 try {
  xmlhttp = new XMLHttpRequest();
 } catch (e) {
  xmlhttp=false
 }
}

function showSearch(searching, linksearch, searchform) {
	document.getElementById(linksearch).style.display = 'none'
	document.getElementById(searching).style.display = 'block'
	document.searching.searchform.focus();
}
function isAvailable(input, type) {
	  var usernameField = document.getElementById(type);
    	  var username = usernameField.value;
	  if (username != '') {
		  var availability = document.getElementById("available-"+type);
		  url="/ajax/isavailable"+type+"/"+username;
		  xmlhttp.open("GET",url,true)
		  xmlhttp.onreadystatechange=function() {
		  	if (xmlhttp.readyState==4) {
				chkbx = xmlhttp.responseText;
				availability.innerHTML = chkbx;
			  }
		  }
		  xmlhttp.setRequestHeader('Accept','message/x-formresult')
		  xmlhttp.send(null)
		  return false
	}
}

function respondTo(id) {
	  var usernameField = document.getElementById(text);
    	  usernameField.value = '#' + id + ' ' + usernameField.value;
}

function getMicroformats(input) {
		document.postmap["url"].value= input;
		document.postmap["url_img"].value= input;
		ocultar("url_submit");
		ocultar("repeat");
		showInline("loading");
		document.postmap["title"].value="";
		document.postmap["tag"].value="";
		document.postmap["lang"].value= "";
		if (document.getElementById("trackback")) {
			document.postmap["trackback"].value= "";
		}
		mod_id = document.postmap["mod_id"];
		if (mod_id) {
			mod_id.value = "";
		}
		var image_current = document.getElementById("image-current");
		if (image_current) {
			document.getElementById("image-current").innerHTML = "";
		}
		  inputdos = input;
		 input = input.replace(/\//gi, '@rep@');
		  url="/ajax/geturl/"+input;
		  xmlhttp.open("GET",url,true)
		  xmlhttp.onreadystatechange=function() {
		  	if (xmlhttp.readyState==4) {
   				if (xmlhttp.status == 200) {
					if (xmlhttp.responseText==1) {
						 ocultar("loading");
						 showInline("repeat");
						 alert('the site '+inputdos+' is banned');
						return false;
					}
					mostrar("mapform");
					ocultar("loading");
					showInline("repeat");
      					var json_data = xmlhttp.responseText; 
      					var the_object = eval("(" + json_data + ")");
					if ((the_object.microformats.latitude) && (the_object.microformats.longitude)) {
						loadmap(the_object.microformats.latitude, the_object.microformats.longitude, 13);
					}
					if (the_object.microformats.title) document.postmap["title"].value= the_object.microformats.title;
					if (the_object.microformats.image) document.postmap["image"].value= the_object.microformats.image;
					if (the_object.microformats.tags) document.postmap["tag"].value= the_object.microformats.tags;
					if (the_object.microformats.trackback) document.postmap["trackback"].value= the_object.microformats.trackback;
					if (the_object.microformats.lang) {
						document.postmap["lang2"].value= the_object.microformats.lang;
						num_lang = document.postmap["lang"].length;
						for(i=0;i<num_lang;i++){
							if (the_object.microformats.lang==document.postmap["lang"].options[i].value) { 	document.postmap["lang"].options[i].selected=true;
							}
       						} 
					}
					if (the_object.microformats.mod_id) {
						document.postmap["mod_id"].value= the_object.microformats.mod_id;
						document.postmap["mapit"].value= "Modify";
					}
    				} else {
					ocultar("loading");
					showInline("repeat");
				}
			  }
		  }
		  xmlhttp.setRequestHeader('Accept','message/x-formresult')
		  xmlhttp.send(null)

		  return false
}
function ocultar(id) {
	var ocultar = document.getElementById(id);
	ocultar.style.display = 'none';
}
function mostrar(id) {
	var mostrar = document.getElementById(id);
	mostrar.style.display = 'block';
}

function showInline(id) {
	var mostrar = document.getElementById(id);
	mostrar.style.display = 'inline';
}
function addclassname(id1, id2, clase) {
	document.getElementById(id1).className=clase;
	document.getElementById(id2).className='';
}
function getval(id) {
	if (document.getElementById(id)) var resultadovalor = document.getElementById(id).value;
	else var resultadovalor = '';
	return resultadovalor;
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0,limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}




function getGoogleUser() {
var valor = document.searchlocation.q.value;

  geocoder.getLatLng(
    valor,
    function(point) {
	loadmapuser();
        map.setCenter(point, 13, G_NORMAL_MAP);
    }
  );
}

function toggle(id1, id2) {
	document.getElementById(id1).style.display = 'none'
	document.getElementById(id2).style.display = 'block'
}
function add_friend(url, id_star, id_text) {
	var starton = document.getElementById(id_star);
	var startontext = document.getElementById(id_text);
	  xmlhttp.open("GET",url,true)
	  xmlhttp.onreadystatechange=function() {
	   if (xmlhttp.readyState==4) {
		if (xmlhttp.responseText==0) {
			alert('Oops, something is wrong');
		} else if (xmlhttp.responseText==1) {
			
			starton.src = '/images/unstar.png';
			startontext.innerHTML = '+ add as a contact';
		} else if (xmlhttp.responseText==2) {
			starton.src = '/images/star.png';
			startontext.innerHTML = '- remove as a contact';
		}
		
	   }
	  }
	  xmlhttp.setRequestHeader('Accept','message/x-formresult')
	  xmlhttp.send(null)
	  return false
}
function carga_imaxes() {
	document.getElementById("fotosget").innerHTML = "<img src='/images/ajax-loader.gif' style='vertical-align:middle;padding-right:5px'/> Loading images from the URL ... (This can take up to a minute, please be patient.)";
	var url = document.postmap.url_img.value;
	var aurl =  '/backend/getimx.php?url='+url;
					xmlhttp.open ("GET", aurl, true);
					xmlhttp.send (null);
					xmlhttp.onreadystatechange = function () {
						if (xmlhttp.readyState == 4) {
							response = xmlhttp.responseText;
							targ = document.getElementById("fotosget");
							if(targ) targ.innerHTML = response;
		}
	}
}
var imaxe_activa="";	
function escolle(id,url){
	if(imaxe_activa!="") document.getElementById(imaxe_activa).style.borderColor="#ccc";
	ob=document.getElementById(id);
	ob.style.borderColor='red';
	imaxe_activa=id;
	document.getElementById("image").value=url;
}
function addTag(ele) {
    var thisTag = ele.innerHTML;
    var taglist = document.getElementById('tag');
    var tags = taglist.value.split(', ');
    
    // If tag is already listed, remove it
    if (tags.contains(thisTag)) {
        tags = tags.remove(thisTag);
        ele.className = 'unselected';
        
    // Otherwise add it
    } else {
        tags.splice(0, 0, thisTag);
        ele.className = 'selected';
    }
    
    taglist.value = tags.join(', ');
    
    document.getElementById('tag').focus();
}
Array.prototype.contains = function (ele) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == ele) {
            return true;
        }
    }
    return false;
};

Array.prototype.remove = function (ele) {
    var arr = new Array();
    var count = 0;
    for (var i = 0; i < this.length; i++) {
        if (this[i] != ele) {
            arr[count] = this[i];
            count++;
        }
    }
    return arr;
};


// Get voters by Beldar <beldar.cat at gmail dot com>
// Generalized for other uses (gallir at gmail dot com)
function get_votes(program,type,container,page,id) {
	var url = base_url + 'backend/'+program+'?id='+id+'&p='+page+'&type='+type;
	$('#'+container).load(url);
}

function changeCommentSize(d) {
	var el = document.getElementById("text");
	var height = parseInt(el.style.height);
	if(!height && el.offsetHeight)
		height = el.offsetHeight;
	height += d;
	if(height < 20) 
		height = 20;
	el.style.height = height+"px";
}


function add_group(url, id_star, id_text, text_on, text_off, text_pending) {
	var starton = document.getElementById(id_star);
	var startontext = document.getElementById(id_text);
	  xmlhttp.open("GET",url,true)
	  xmlhttp.onreadystatechange=function() {
	   if (xmlhttp.readyState==4) {
		if (xmlhttp.responseText==0) {
			alert('Oops, something is wrong');
		} else if (xmlhttp.responseText==1) {
			starton.src = '/images/unstar.png';
			startontext.innerHTML = text_on;
		} else if (xmlhttp.responseText==2) {
			starton.src = '/images/star.png';
			startontext.innerHTML = text_off;
		} else if (xmlhttp.responseText==3) {
			starton.src = '/images/star.png';
			startontext.innerHTML = text_pending;
		}
		
	   }
	  }
	  xmlhttp.setRequestHeader('Accept','message/x-formresult')
	  xmlhttp.send(null)
	  return false
}

function admin_group(url, id) {
	var divobject = document.getElementById(id);
	  xmlhttp.open("GET",url,true)
	  xmlhttp.onreadystatechange=function() {
	   if (xmlhttp.readyState==4) {
		if (xmlhttp.responseText==1) {
			divobject.style.display = 'none';
		} else if (xmlhttp.responseText==2) {
			divobject.style.display = 'none';
		}
		
	   }
	  }
	  xmlhttp.setRequestHeader('Accept','message/x-formresult')
	  xmlhttp.send(null)
	  return false
}

/**************************************
Tooltips functions
***************************************/
/**
  Stronglky modified, onky works with DOM2 compatible browsers.
  	Ricardo Galli
  From http://ljouanneau.com/softs/javascript/tooltip.php
 */


// create the tooltip object
function tooltip(){}

// setup properties of tooltip object
tooltip.id="tooltip";
tooltip.main=null;
tooltip.offsetx = 10;
tooltip.offsety = 10;
tooltip.shoffsetx = 8;
tooltip.shoffsety = 8;
tooltip.x = 0;
tooltip.y = 0;
tooltip.tooltipShadow=null;
tooltip.tooltipText=null;
tooltip.title_saved='';
tooltip.saveonmouseover=null;
tooltip.timeout = null;
tooltip.active = false;

tooltip.cache = new JSOC();

tooltip.ie = (document.all)? true:false;		// check if ie
if(tooltip.ie) tooltip.ie5 = (navigator.userAgent.indexOf('MSIE 5')>0);
else tooltip.ie5 = false;
tooltip.dom2 = ((document.getElementById) && !(tooltip.ie5))? true:false; // check the W3C DOM level2 compliance. ie4, ie5, ns4 are not dom level2 compliance !! grrrr >:-(




/**
* Open ToolTip. The title attribute of the htmlelement is the text of the tooltip
* Call this method on the mouseover event on your htmlelement
* ex :  <div id="myHtmlElement" onmouseover="tooltip.show(this)"...></div>
*/

tooltip.show = function (event, text) {
      // we save text of title attribute to avoid the showing of tooltip generated by browser
	if (this.dom2  == false ) return false;
	if (this.tooltipShadow == null) {
		this.tooltipShadow = document.createElement("div");
		this.tooltipShadow.setAttribute("id", "tooltip-shadow");
		document.body.appendChild(tooltip.tooltipShadow);

		this.tooltipText = document.createElement("div");
		this.tooltipText.setAttribute("id", "tooltip-text");
		document.body.appendChild(this.tooltipText);
	}
	this.saveonmouseover=document.onmousemove;
	document.onmousemove = this.mouseMove;
	this.mouseMove(event); // This already moves the div to the right position
	this.setText(text);
	this.tooltipText.style.visibility ="visible";
	this.tooltipShadow.style.visibility ="visible";
	this.active = true;
	return false;
}


tooltip.setText = function (text) {
	tooltip.tooltipShadow.style.width = 0+"px";
	tooltip.tooltipShadow.style.height = 0+"px";
	this.tooltipText.innerHTML=text;
	setTimeout('tooltip.setShadow()', 1);
	return false;
}

tooltip.setShadow = function () {
	tooltip.tooltipShadow.style.width = tooltip.tooltipText.clientWidth+"px";
	tooltip.tooltipShadow.style.height = tooltip.tooltipText.clientHeight+"px";
}


/**
* hide tooltip
* call this method on the mouseout event of the html element
* ex : <div id="myHtmlElement" ... onmouseout="tooltip.hide(this)"></div>
*/
tooltip.hide = function (event) {
	if (this.dom2  == false) return false;
	document.onmousemove=this.saveonmouseover;
	this.saveonmouseover=null;
	if (this.tooltipShadow != null ) {
		this.tooltipText.style.visibility = "hidden";
		this.tooltipShadow.style.visibility = "hidden";
		this.tooltipText.innerHTML='';
	}
	this.active = false;
}



// Moves the tooltip element
tooltip.mouseMove = function (e) {
   // we don't use "this", but tooltip because this method is assign to an event of document
   // and so is dreferenced

	if (tooltip.ie) {
		tooltip.x = event.clientX;
		tooltip.y = event.clientY;
	} else {
		tooltip.x = e.pageX;
		tooltip.y = e.pageY;
	}
	tooltip.moveTo( tooltip.x +tooltip.offsetx , tooltip.y + tooltip.offsety);
}

// Move the tooltip element
tooltip.moveTo = function (xL,yL) {
	if (this.ie) {
		xL +=  document.documentElement.scrollLeft;
		yL +=  document.documentElement.scrollTop;
	}
	if (this.tooltipText.clientWidth > 0  && document.documentElement.clientWidth > 0 && xL > document.documentElement.clientWidth * 0.55) {
		xL = xL - this.tooltipText.clientWidth - 2*this.offsetx;
	}
	this.tooltipText.style.left = xL +"px";
	this.tooltipText.style.top = yL +"px";
	xLS = xL + this.shoffsetx;
	yLS = yL + this.shoffsety;
	this.tooltipShadow.style.left = xLS +"px";
	this.tooltipShadow.style.top = yLS +"px";
}

// Show the content of a given comment
tooltip.c_show = function (event, type, element, link) {

      // we save text of title attribute to avoid the showing of tooltip generated by browser
	if (this.dom2  == false ) return false;
	if (element == 0 && link > 0) { // It's a #0  from a comment
		this.ajax_delayed(event,'get_link.php',link);
		return;
	}
	if (type == 'id') {
		target_text = 'comment-' + element;
		target_author = 'cauthor-'+element;

		target = document.getElementById(target_text);
		author_target = document.getElementById(target_author);
		if (! target || ! author_target)  {
			this.ajax_delayed(event,'get_comment_tooltip.php',element+"&link="+link);
			return;
		}
		text = '<strong>'+author_target.innerHTML+'</strong><br/>'+target.innerHTML;
	} else if (type == 'order') {
		this.ajax_delayed(event,'get_comment_tooltip.php',element+"&link="+link);
		return;
	} else {
		text = element;
	}
	return this.show(event, text);
}


tooltip.clear = function (event) {
	if (this.timeout != null) {
		clearTimeout(this.timeout);
		this.timeout = null;
	}
	this.hide(event);
}

tooltip.ajax_delayed = function (event, script, id, maxcache) {
	maxcache = maxcache || 600000; // 10 minutes in cache
	if (this.active) return false;
	if ((object = this.cache.get(script+id)) != undefined) {
		tooltip.show(event, object[script+id]);
	} else {
		this.show(event, 'cargando...'); // Translate this to your language: it's "loading..." ;-)
		this.timeout = setTimeout("tooltip.ajax_request('"+script+"', '"+id+"', "+maxcache+")", 100);
	}
}

tooltip.ajax_request = function(script, id, maxcache) {
	var url = base_url + 'backend/'+script+'?id='+id;
	tooltip.timeout = null;
	$.ajax({
		url: url,
		dataType: "html",
		success: function(html) {
			tooltip.cache.set(script+id, html, {'ttl':maxcache});
			tooltip.setText(html);
		}
	});
}
