function disableSubmit()
{
	document.getElementById("div5").className = 'mainDivOff';
}

function enableSubmit()
{
	document.getElementById("div5").className = 'mainDivOn';
}

function leerTerminos()
{
	document.getElementById("terminosCondiciones").className = 'mainDivOn';
}

function backToTop() {
    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;

    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }

    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }

    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;

    var x = Math.max(x1, Math.max(x2, x3));
    var y = Math.max(y1, Math.max(y2, y3));

    window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

    if (x > 0 || y > 0) {
        window.setTimeout("backToTop()", 1);
    }
}

function enableBusqueda()
{
	document.getElementById("busqueda").className = 'mainDivOn';
	document.getElementById("busquedaSimple").className = 'mainDivOff';
	document.getElementById("busquedaAvanzada").className = 'mainDivOn';
}

function disableBusqueda()
{
	document.getElementById("busqueda").className = 'mainDivOff';
	document.getElementById("busquedaSimple").className = 'mainDivOn';
	document.getElementById("busquedaAvanzada").className = 'mainDivOff';
}


function popupwindow(url,ancho,alto)
{
        var winl = (screen.width - 750) / 2;
        var wint = ((screen.height - 540) / 2) - 30;

        mywindow = window.open(url, 'Tracker', 'status=no,toolbar=no,location=no,menubar=no,scrollbars=no,resizable=no,width='+ancho+',height='+alto+',top='+wint+',left='+winl);
        mywindow.location = url;
}

function popupshare(url)
{
	var ancho=1024;
	var alto=650;
    mywindow = window.open(url, 'Tracker', 'status=no,toolbar=no,location=no,menubar=no,scrollbars=yes,resizable=no,width='+ancho+',height='+alto);
    mywindow.location = url;
}


function enableButton(field)
{
	document.getElementById("okButton" + field).style.visibility="visible";
}


function textCounter(field, countfield, minlimit,maxlimit,nombre) 
{
	countfield.value = field.value.length;
	
	if (((countfield.value) < minlimit) || ((countfield.value) > maxlimit))
	{
		document.getElementById(nombre+"Error").style.visibility = 'visible';
	}
	else
	{
		document.getElementById(nombre+"Error").style.visibility = 'hidden';
	}
}

function activaEditaCampo(campo)
{
   	var i;
    for (i=1;i<12;i++)
    {
    	try
    	{
	   		document.getElementById("divA" + i).className = 'noactivo';
	   	}
	   	catch (mierror){}
    }
	document.getElementById("divA" + campo).className = 'activo';
	document.getElementById("divA" + campo).blur();
}

function activaJuego(juego)
{
	document.getElementById("jugar").innerHTML = '<embed src='+juego+' width=800 height=600>';
}


function enableAddImagen(nombre)
{
	document.getElementById("rutaImagen").value = nombre;
}

function enableSubmitAddImagen()
{
	document.getElementById("divSubmitAddImagen").className = 'mainDivOn';
	document.getElementById("submitButton").focus();
}


function anadeComentario(id)
{
	document.editJuegoForm.inputNota.value += "\n" + id + ": ";
}


function muestraMapa(latitud,longitud,zoom)
{
	var map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(latitud,longitud), zoom);
	map.addControl(new GSmallZoomControl());
	var punto = new GLatLng(latitud,longitud);
	var marker = new GMarker(punto,{draggable: false});
	map.addOverlay(marker);
}


function dibujaMapa(latitud,longitud,zoom)
{
	var map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(latitud,longitud), zoom);
	map.addControl(new GSmallMapControl());
	//map.addControl(new GMapTypeControl());
	var punto = new GLatLng(latitud,longitud);
	var marker = new GMarker(punto,{draggable: false});
	
	  GEvent.addListener(map,"click", function(overlay,latlng) {     
          if (latlng) {   
            marker.setLatLng(latlng);
			document.profileForm.inputGeolocaliza0.value = latlng;
			document.profileForm.inputZoom0.value = map.getZoom();
          }
        });
	
	map.addOverlay(marker);
}


function createMarker(point, name, imageName, rjugador) {
    icon.image = imageName ;
	  var marker = new GMarker(point, icon);
	  GEvent.addListener(marker, "click", function() {
	    marker.openInfoWindowHtml(' <b><a href="/'+rjugador+'/'+ name + '/">'+name+'</a></b>');
	  });
	  return marker;
	}


function yupisWorld(milat, milon, mizoom, rjugador)
{
	map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(milat,milon), mizoom);
	map.addControl(new GLargeMapControl());
    GDownloadUrl("/users.xml?a="+Math.random(), function(data, responseCode) 
    {
	    var xml = GXml.parse(data);
	    markers = xml.documentElement.getElementsByTagName("marker");
	    updateMarkers(rjugador);
     });
     	
    GEvent.addListener(map,'zoomend',function() {
        updateMarkers(rjugador);
    });
    GEvent.addListener(map,'moveend',function() {
        updateMarkers(rjugador);
    });     
}

function updateMarkers(rjugador)
{
    	map.clearOverlays();
    	var mapBounds = map.getBounds();
	    for (var i = 0; i < markers.length; i++) 
	    {
	            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
	                            parseFloat(markers[i].getAttribute("lng")));
	            if(!mapBounds.contains(point))
	            {
	            	continue;
	            }
	            map.addOverlay(createMarker(point, markers[i].getAttribute("name"),markers[i].getAttribute("imageFile"),rjugador));
	    }
}

function confirmaEliminaComentario(comentario)
{
	document.getElementById("borrarA"+comentario).className = 'mainDivOff';
	document.getElementById("borrarB"+comentario).className = 'mainDivOn';
}

function cancelaEliminaComentario(comentario)
{
	document.getElementById("borrarB"+comentario).className = 'mainDivOff';
	document.getElementById("borrarA"+comentario).className = 'mainDivOn';

}

function disableText(id)
{
	document.getElementById("textoB"+id).innerHTML = '';
	document.getElementById("textoC"+id).className = 'mainDivOn';
}

function jumpToGame() {
   window.location = String(window.location).replace(/\#.*$/, "") + "#play";
}


function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
    return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}

function preloadJuego() 
{
	var flashMovie=getFlashMovieObject("juegoYupis");
	if (flashMovie.PercentLoaded() == 100)
	{
		window.clearInterval(miPreload);
		document.getElementById("yupisGame").style.visibility = 'visible';
		document.getElementById("preloadJuego").style.display = 'none';
	}
	else
	{
		progBar(3*flashMovie.PercentLoaded());
	}
}

function progBar(i)
{
   if (i<=(300))
   {
	  if (i>40)
	  {
	  	document.getElementById("porcentaje").innerHTML=parseInt(i/3)+"%";
	  }
      document.getElementById("barra").style.width=i+"px";
   }
}


function popupwindow(url,ancho,alto)
{
        var winl = (screen.width - 750) / 2;
        var wint = ((screen.height - 540) / 2) - 30;

        mywindow = window.open(url, 'Tracker', 'status=no,toolbar=no,location=no,menubar=no,scrollbars=no,resizable=yes,width='+ancho+',height='+alto+',top='+wint+',left='+winl);
        mywindow.location = url;
}

function ProbarJuego(url)
{
		if (document.getElementById('inputAlto').value>600)
		{
			if (confirm("El juego tiene un ALTO mayor de 600px, estas seguro ?"))
			{
				popupwindow(url,document.getElementById('inputAncho').value,document.getElementById('inputAlto').value);
			}
		}
		else
		{
				popupwindow(url,document.getElementById('inputAncho').value,document.getElementById('inputAlto').value);
		}
}

function ucfirst( str ) {
    var f = str.charAt(0).toUpperCase();
    return f + str.substr(1, str.length-1);
}
function remplazaAlternativo(id,texto)
{
	var mitext = texto.replace(/@@@/,document.getElementById('inputTag'+id).value);
	document.getElementById('inputAlternativo'+id).value=ucfirst(mitext);
}


/*********
* Javascript for file upload demo
* Copyright (C) Tomas Larsson 2006
* http://tomas.epineer.se/

* Licence:
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
* 
* Software distributed under this License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*/
function PeriodicalAjax(url, parameters, frequency, decay, onSuccess, onFailure) {
	function createRequestObject() {
		var xhr;
		try {
			xhr = new XMLHttpRequest();
		}
		catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
		return xhr;
	}
	
	function send() {
		if(!stopped) {
			xhr.open('post', url, true);
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhr.onreadystatechange = function() { self.onComplete(); };
			xhr.send(parameters);
		}
	}
	
	this.stop = function() {
		stopped = true;
		clearTimeout(this.timer);
	}
	
	this.start = function() {
		stopped = false;
		this.onTimerEvent();
	}
	
	this.onComplete = function() {
		if(this.stopped) return false;
		if ( xhr.readyState == 4) {
			if(xhr.status == 200) {
				if(xhr.responseText == lastResponse) {
					decay = decay * originalDecay;
				} else {
					decay = 1;
				}
				lastResponse = xhr.responseText;
				if(onSuccess instanceof Function) {
					onSuccess(xhr);
				}
				this.timer = setTimeout(function() { self.onTimerEvent(); }, decay * frequency * 1000);
			} else {
				if(onFailure instanceof Function) {
					onFailure(xhr);
				}
			}
		}
	}
	
	this.getResponse = function() {
		if(xhr.responseText) {
			return xhr.responseText;
		}
	}
	
	this.onTimerEvent = function() {
		send();
	}

	var self = this;
	var stopped = false;
	var originalDecay = decay || 1.2;
	decay = originalDecay;
	var xhr = createRequestObject();
	var lastResponse = "";
	this.start();
}

function ProgressTracker(sid, options) {

	this.onSuccess = function(xhr) {
		if(parseInt(xhr.responseText) >= 100) {
			periodicalAjax.stop();
			if(options.onComplete instanceof Function) {
				options.onComplete();
			}
		} else if(xhr.responseText && xhr.responseText != lastResponse) {
			if(options.onProgressChange instanceof Function) {
				options.onProgressChange(xhr.responseText);
			}
			if(options.progressBar && options.progressBar.style) {
				options.progressBar.style.width = parseInt(xhr.responseText) + "%";
				options.progressBar2.innerHTML = parseInt(xhr.responseText) + "%";
			}
		}
	}
	
	this.onFailure = function(xhr) {
		if(options.onFailure instanceof Function) {
			options.onFailure(xhr.responseText);
		} else {
			alert(xhr.responseText);
		}
		periodicalAjax.stop();
	}

	var self = this;
	var lastResponse = -1;
	options = options || {};
	var url = options.url || '/fileprogress.php';
	var frequency = options.frequency || 0.5;
	var decay = options.decay || 2;
	var periodicalAjax = new PeriodicalAjax(url, 'sid=' + sid, frequency, decay, function(request){self.onSuccess(request);},function(request){self.onFailure(request);});
}


function setDimensions(movie,url,divName) {
   var movieObj = getFlashMovieObject(movie);
   var width = movieObj.TGetProperty("/", 8);
   var height= movieObj.TGetProperty("/", 9);

   if (parseFloat(width)>800)
   {
   		height=parseInt(parseFloat(height)/(parseFloat(width)/800));
   		width=800;
   }
   else if (parseFloat(width)<400)
   {
   		height=parseInt(parseFloat(height)/(parseFloat(width)/400));
   		width=400;
   }
   var respuesta = "";
   respuesta = '<embed name="'+movie+'2" width="'+width+'" height="'+height+'" src="'+url+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed>';
   document.getElementById(divName).innerHTML = respuesta;
}
    
function preloadJuegoDimensions(movie,url,divName) 
{
	var flashMovie=getFlashMovieObject(movie);
	if (flashMovie.PercentLoaded() == 100)
	{
		window.clearInterval(relojDimensiones);
		setDimensions(movie,url,divName);
	}
}

function resizeIframe(divName,newWidth, newHeight)
{
	document.getElementById(divName).width = newWidth;
	document.getElementById(divName).height = newHeight;
}

function resizeJuego(divName){
	var winWidth, winHeight, d=document;
	if (typeof window.innerWidth!='undefined') {
	winWidth = window.innerWidth;
	winHeight = window.innerHeight;
	} else if ( d.documentElement
	&& typeof d.documentElement.clientWidth!='undefined'
	&& d.documentElement.clientWidth!=0 ) {
	winWidth = d.documentElement.clientWidth;
	winHeight = d.documentElement.clientHeight;
	} else if ( d.body
	&& typeof d.body.clientWidth!='undefined') {
	winWidth = d.body.clientWidth;
	winHeight = d.body.clientHeight;
	}
	document.getElementById(divName).style.width = (winWidth-260) + "px";
	document.getElementById(divName).style.height = (winHeight-70) + "px";
}


function popUp(URL) 
{
id = 'YupisGame';
if (navigator.appName == "Microsoft Internet Explorer")
{
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,fullscreen=yes');");
}
else
{
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,height="+(screen.height-30)+",width="+(screen.width-5)+"');");
}
location.reload();
}

 function vacio(q) {  
         for ( i = 0; i < q.length; i++ ) {  
                 if ( q.charAt(i) != " " ) {  
                         return true  
                 }  
         }  
         return false  
 }  

function ValidaFormulario (formulario)
{
	var devuelve=1;
	if (!vacio(formulario.input1.value))
	{
		document.getElementById("input1").style.border="1px solid #ee0000";
		devuelve=0;
	}
	else
	{
		document.getElementById("input1").style.border="1px solid #60C1E1";
	}
	if (!vacio(formulario.input2.value))
	{
		document.getElementById("input2").style.border="1px solid #ee0000";
		devuelve=0;
	}
	else
	{
		document.getElementById("input2").style.border="1px solid #60C1E1";
	}
	if (!vacio(formulario.input3.value))
	{
		document.getElementById("input3").style.border="1px solid #ee0000";
		devuelve=0;
	}
	else
	{
		document.getElementById("input3").style.border="1px solid #60C1E1";
	}
	if (!vacio(formulario.input4.value))
	{
		document.getElementById("input4").style.border="1px solid #ee0000";
		devuelve=0;
	}
	else
	{
		document.getElementById("input4").style.border="1px solid #60C1E1";
	}
	
	if (!(formulario.inputTerminos.checked))
	{
		devuelve=0;
	}

	if (devuelve==1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function stripHTML()
{
	var re = /(<([^>]+)>)/gi;
	for (i=0; i<arguments.length; i++)
	arguments[i].value=arguments[i].value.replace(re, "")
}

