/**
 * trim:
 * Elimina los espacios blancos al principio y al final de la cadena de textoi via RegEX
 */
function trim(string) {
	return string.replace(/(^\s*)|(\s*$)/g,'') ;
	}

/**
 * calc_long:
 * Trnca eli contenido del texto a una longitud en textarea
 */
function calc_long(txt, maximo) {
	var largo ;
	largo = txt.value.length ;
	if (largo > maximo) txt.value = txt.value.substring(0, maximo) ;
	}

//checking email
function check_mail(str)
{
	//var filter= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/i ;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i ;
	if (filter.test(str)) return true ;
	else return false ;
}

//Encontrar un objeto cross-browser a partir de
function getObject(objname)
{    
    var obj = null ;
    if (document.getElementById) {
        obj = document.getElementById(objname) ;
    } else {
        if (document.all) {
            obj = document.all[objname] ;
            }
        }

    return obj ;
}

function goURL(url) {
 	self.location=url ;
}

// Bookmark del site CROSS BROWSER
function addFavorite(url,title) {

	if (document.all && window.external) {
		// Explorer
		window.external.AddFavorite(url, title);
	} else {
		if (window.sidebar) {
			// NS6+
			window.sidebar.addPanel(title, url, "")
		} else {
			var agt=navigator.userAgent.toLowerCase();
			if(agt.indexOf("opera")!= -1) {
				alert(msg_Opera_bookmark) ;
				}
			}
		}
}


function openWindow(url,name,w,h) {
	if (document.all)
		window.open(url,name,"height="+h+",width="+w+",scrollbars=yes,resizable=no,location=no,menubar=no,status=yes,toolbar=no") ;
	else
		window.open(url,name,"outerHeight="+h+",outerWidth="+w+",scrollbars=yes,resizable=no,location=no,menubar=no,status=yes,toolbar=no") ;
	}

function openFullWindow(url,name) {
	w = screen.availWidth ;
	h = screen.availHeight ;
	if (document.all)
		hWnd = window.open(url,name,"height="+h+",width="+w+",scrollbars=yes,resizable=yes,location=yes,menubar=yes,status=yes,toolbar=yes") ;
	else
		hWnd = window.open(url,name,"outerHeight="+h+",outerWidth="+w+",scrollbars=yes,resizable=yes,location=yes,menubar=yes,status=yes,toolbar=yes") ;
	hWnd.moveTo(0,0) ;

}

/**
 * ShowHideBlock:
 * Función que hace visible/invisible un bloque a partir de su nombre
 * El div debe tener style con display inicial válido
 */
 function ShowHideBlock(nom) {
 	var capa ;
	capa = getObject(nom) ;
	if (capa) {
		if (arguments.length==2) {
			//explicit mode
			if (arguments[1]) capa.style.display="block" ;
			else capa.style.display="none" ;
		} else {
			//toggle mode
			if (capa.style.display!="block") capa.style.display="block";
			else capa.style.display="none";
			}
		}
	}
function hideObject(sId) {
	var oObj = document.getElementById(sId);
	if (oObj) oObj.style.display = "none";
}
 
function showObject(sId) {
	var oObj = document.getElementById(sId);
	if (oObj) {
		switch (oObj.tagName.toLowerCase()) {
			case "tr":
				oObj.style.display = "";
			break;
			default:
				oObj.style.display = "block";
			break;
		
		}
	}
}
// gets a child element using ID
function getChildElementById(oParent, childId) {
	var oElement = false;
	if (!oParent || !oParent.hasChildNodes) return false;

	for (var i = 0; i < oParent.childNodes.length; i++) {
		if (oParent.childNodes[i].id == childId)
			return oParent.childNodes[i];

		// if child has childs do a recursive call
		if (oParent.childNodes[i].hasChildNodes) {
			oElement = getChildElementById(oParent.childNodes[i], childId);
			if (oElement) return oElement;
		}
	}
}

function getChildElementByName(oParent, sName) {
	var oElement = false;
	if (!oParent || !oParent.hasChildNodes) return false;

	for (var i = 0; i < oParent.childNodes.length; i++) {
		if (oParent.childNodes[i].name == sName)
			return oParent.childNodes[i];

		// if child has childs do a recursive call
		if (oParent.childNodes[i].hasChildNodes) {
			oElement = getChildElementByName(oParent.childNodes[i], sName);
			if (oElement) return oElement;
		}
	}
}
// Animación del Div
function xGetElementById(e) {
  if(typeof(e)!='string') return e;
  if(document.getElementById) e=document.getElementById(e);
  else if(document.all) e=document.all[e];
  else e=null;
  return e;
}

function isNum(n) {
	for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])!='number') return false;}
	return true;
}

function xClip(e,iTop,iRight,iBottom,iLeft) {
	if(!(e=xGetElementById(e))) return;
	if(e.style) {
		if (isNum(iLeft))
			e.style.clip='rect('+iTop+'px '+iRight+'px '+iBottom+'px '+iLeft+'px)';
		else
			e.style.clip='rect(0 '+parseInt(e.style.width)+'px '+parseInt(e.style.height)+'px 0)';
	}
}

clip_top = 0;
clip_right = 165;
clip_bottom = 0;
clip_left = 0;
maximo_clip_bottom = 110;
alerta = true;
var timer;

function animaDiv(divId) {
	clip_bottom++;
	clip_bottom = clip_bottom % maximo_clip_bottom;
	xClip (divId, clip_top, clip_right, clip_bottom, clip_left);
	if (clip_bottom < maximo_clip_bottom-1)
		timer = setTimeout("animaDiv('" + divId + "')",5);
	else timer = null;
}
// Fin animación del Div
//Funciones que crean y destruyen un div que bloquea la web
function createBlockingDiv() {
	var oBlockDiv = document.createElement("DIV");
		oBlockDiv.id = "blockingDiv";
		oBlockDiv.className = "blockDiv";
	document.body.appendChild(oBlockDiv);
	//ie fix
	if (document.all) {
		var aSelect = document.getElementsByTagName("select");
		for (var i = 0; i < aSelect.length; i++)
			aSelect[i].disabled = "yes";
	}
	//document.documentElement.style.overflow = "hidden";

	return(oBlockDiv);
}

function destroyBlockingDiv() {
	if (document.getElementById("blockingDiv"))
		document.getElementById("blockingDiv").parentNode.removeChild(document.getElementById("blockingDiv"));
	if (document.all) {
		var aSelect = document.getElementsByTagName("select");
		for (var i = 0; i < aSelect.length; i++)
			 aSelect[i].disabled = !aSelect[i].disabled;
	}

	//document.documentElement.style.overflow = "auto";
}

//Funcion para aviso de darse de alta si quieren chatear

function alertaAltaLogin(idImage, sNombre, sEdad, sPoblacion) {
	var oBlocking = createBlockingDiv();
	var iY = 0;
	iY = (document.documentElement.scrollTop || document.body.scrollTop) + (window.innerHeight ||  document.documentElement.clientHeight)/3;
	//alert("scroll -> " + (document.documentElement.scrollTop || document.body.scrollTop) + "\nheight -> " + (window.innerHeight ||  document.documentElement.clientHeight));
	oDivAlerta = document.getElementById("alertaAltaLogin");
	if (!oDivAlerta)
		return(false);

	//Clonamos la alerta, así luego la podemos eliminar directamente con un remove
	var oDivAlertaVis = oDivAlerta.cloneNode(true);
		oDivAlertaVis.id = "alertaAltaLoginCloned";
		oDivAlertaVis.style.top = iY + "px";
	// attach element to documente, if not, we can't find it
	if (document.getElementById(idImage)) {
		var oImg = document.getElementById(idImage).cloneNode(true);
		var oImgParent = getChildElementById(oDivAlertaVis, "alertaALFoto");
			oImgParent.appendChild(oImg);
	}
	
	var oNombre = getChildElementById(oDivAlertaVis,"alertaALNombre");
		if (oNombre) oNombre.innerHTML = sNombre;

	var oEdad = getChildElementById(oDivAlertaVis,"alertaALEdad");
		if (oEdad) oEdad.innerHTML = sEdad;

	var oPoblacion = getChildElementById(oDivAlertaVis,"alertaALPoblacion");
		if (oPoblacion) oPoblacion.innerHTML = sPoblacion;
	//ie fix
	if (document.all) {
		var oIF = document.createElement("iframe");
			oIF.className="blockDiv";
			oIF.style.position = "absolute";
			oIF.style.top = iY;
			oIF.className = "alertaCentral";
			oIF.style.display = "block";
			oIF.style.height = "310px"; //por aproximación :P
			oIF.style.border = "0px";
			oIF.style.backgroundColor = "#EDF2F8";
		oBlocking.appendChild(oIF);
	}
	oDivAlertaVis.style.display = "block";
	document.body.appendChild(oDivAlertaVis);

}

function destroyalertaAltaLogin() {
	destroyBlockingDiv();
	document.getElementById("alertaAltaLoginCloned").parentNode.removeChild(document.getElementById("alertaAltaLoginCloned"));
}

function showLogin() {
	//Block web content
	var oDivLogin = document.getElementById("loginFloatingForm");
	if (!oDivLogin) {
		return(false);
	}
	createBlockingDiv();
	var iY = 0;
	iY = (document.documentElement.scrollTop || document.body.scrollTop) + (window.innerHeight ||  document.documentElement.clientHeight)/3;
	//Clonamos la alerta, así luego la podemos eliminar directamente con un remove
	oDivLogin = oDivLogin.cloneNode(true);
		oDivLogin.id = "loginFloatingFormCloned";
		oDivLogin.style.top = iY + "px";
	/*if (document.all) { //ie fix
		var oIF = document.createElement("iframe");
			oIF.className="blockDiv";
			oIF.style.position = "absolute";
			oIF.style.top = iY;
			oIF.className = "alertaCentral";
			oIF.style.display = "block";
			oIF.style.height = "310px"; //por aproximación :P
			oIF.style.border = "0px";
			oIF.style.backgroundColor = "#EDF2F8";
		oBlocking.appendChild(oIF);
	}*/
	//
	document.body.appendChild(oDivLogin);
	oDivLogin.style.display = "block";
	//Damos el foco al mail
	oEmail = getChildElementByName(oDivLogin, "sEmail");
	if (oEmail)
		oEmail.focus();
}
function closeLogin() {
	var oDiv = document.getElementById("loginFloatingFormCloned");
	oDiv.parentNode.removeChild(oDiv);
	destroyBlockingDiv();
}
function loginResponse(httpResponse) {
	var sResponse = httpResponse.responseText;
	var tResponse = eval('(' + sResponse + ')');
	if (tResponse.bStatus) {
		document.location.href = document.location.href;
		closeLogin();
	}
	else {
		alert("Los datos introducidos son incorrectos.");
	}
}
function doAjaxLogin(oForm) {
	if (oForm.sPass.value.indexOf ("md5@") != 0) {
		oForm.sPass.value = "md5@" + hex_md5(trim(oForm.sPass.value)); 
	}
	makePostRequest("/_src_/ajax/basic.php", oForm, "loginResponse(httpRequest);");
}
/*
COOKIES
*/
function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) +";path=/";
}
function getCookie(c_name)
{
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}

function array_unique(aArray) {
	var aReturn = new Array();
	var bFound;
	for (var i = 0; i < aArray.length; i++) {
		bFound = false;
		if (aArray[i] != "") {
			for (var k = 0; k < aReturn.length; k++) {
				if (aReturn[k] == aArray[i] && !bFound) {
					bFound = true;
				}
			}
		}
		if (!bFound) aReturn[aReturn.length] = aArray[i];
	}
	return(aReturn);
}