// JavaScript
//
//  pageranker.js
//	rev001
//
function getObject(obj) {
	var theObj;
	if(document.all) {
		if(typeof obj=="string") {
			return document.all(obj);
		} else {
			return obj.style;
		}
	}
	if(document.getElementById) {
		if(typeof obj=="string") {
			return document.getElementById(obj);
		} else {
			return obj.style;
		}
	}
	return null;
}

//----------------------------------------------------------------
// Alexa Interactive Graphics
// v1.0
// (c) Emilio José Jiménez Jiménez
//----------------------------------------------------------------
function AlexaGraph(imgID,w,h) {
	this.imgID=imgID;
	this.width=w;
	this.height=h;
	this.r='6m';
	this.y='n';			//"r","n","p"
	this.a=1;
	this.z=2;
	this.url1='';
	this.url2='';
	this.peticion='';
	this.oTD=getObject('nav');
	this.oImg=getObject(imgID+'_img');
	this.img=new Image();
}
AlexaGraph.prototype.Selector=function() {
	var contenido="";
	contenido="<span style='color: #FFFFFF; background: #000000; border: 1px solid #000000;'>TYPE:</span> ";
	contenido+="<a class='azul' href='javascript:;' onClick='"+this.imgID+".setType(\"r\");'>&nbsp;Reach&nbsp;</a>";
	contenido+="<a class='azul' href='javascript:;' onClick='"+this.imgID+".setType(\"n\");'>&nbsp;Rank&nbsp;</a>";
	contenido+="<a class='azul' href='javascript:;' onClick='"+this.imgID+".setType(\"p\");'>&nbsp;Pageviews&nbsp;</a>";
	contenido+=" <span style='color: #FFFFFF; background: #000000; border: 1px solid #000000;'>RANGE:</span> ";
	contenido+="<a class='azul' href='javascript:;' onClick='"+this.imgID+".setRange(\"7d\");'>&nbsp;7d&nbsp;</a>";
	contenido+="<a class='azul' href='javascript:;' onClick='"+this.imgID+".setRange(\"14d\");'>&nbsp;14d&nbsp;</a>";
	contenido+="<a class='azul' href='javascript:;' onClick='"+this.imgID+".setRange(\"1m\");'>&nbsp;1m&nbsp;</a>";
	contenido+="<a class='azul' href='javascript:;' onClick='"+this.imgID+".setRange(\"3m\");'>&nbsp;3m&nbsp;</a>";
	contenido+="<a class='azul' href='javascript:;' onClick='"+this.imgID+".setRange(\"6m\");'>&nbsp;6m&nbsp;</a>";
	contenido+="<a class='azul' href='javascript:;' onClick='"+this.imgID+".setRange(\"1y\");'>&nbsp;1y&nbsp;</a>";
	contenido+="<a class='azul' href='javascript:;' onClick='"+this.imgID+".setRange(\"2y\");'>&nbsp;2y&nbsp;</a>";
	contenido+="<a class='azul' href='javascript:;' onClick='"+this.imgID+".setRange(\"3y\");'>&nbsp;3y&nbsp;</a>";
	contenido+=" <span style='color: #FFFFFF; background: #000000; border: 1px solid #000000;'>SMOOTHING:</span> ";
	contenido+="<a class='azul' href='javascript:;' onClick='"+this.imgID+".setSmoothing(0);'>&nbsp;-1&nbsp;</a>";
	contenido+="<a class='azul' href='javascript:;' onClick='"+this.imgID+".setSmoothing(1);'>&nbsp;0&nbsp;</a>";
	contenido+="<a class='azul' href='javascript:;' onClick='"+this.imgID+".setSmoothing(2);'>&nbsp;+1&nbsp;</a>";
	contenido+="<a class='azul' href='javascript:;' onClick='"+this.imgID+".setSmoothing(3);'>&nbsp;+2&nbsp;</a>";
	if(this.y=='r') contenido=contenido.replace("&nbsp;Reach&nbsp;"," <span style='color: #FFFFFF; background: #999999; border: 1px solid #999999;'>&nbsp;Reach&nbsp;</span> ");
	if(this.y=='n') contenido=contenido.replace("&nbsp;Rank&nbsp;"," <span style='color: #FFFFFF; background: #999999; border: 1px solid #999999;'>&nbsp;Rank&nbsp;</span> ");
	if(this.y=='p') contenido=contenido.replace("&nbsp;Pageviews&nbsp;"," <span style='color: #FFFFFF; background: #999999; border: 1px solid #999999;'>&nbsp;Pageviews&nbsp;</span> ");
	contenido=contenido.replace("&nbsp;"+this.r+"&nbsp;","<span style='color: #FFFFFF; background: #999999; border: 1px solid #999999;'>&nbsp;"+ this.r +"&nbsp;</span>");
	var grado=this.z-1;
	if(grado>0) grado="+"+grado;
	contenido=contenido.replace("&nbsp;"+grado+"&nbsp;","<span style='color: #FFFFFF; background: #999999; border: 1px solid #999999;'>&nbsp;"+ grado +"&nbsp;</span>");
	this.oTD.innerHTML=contenido;
}
AlexaGraph.prototype.Mostrar=function() {
	this.peticion="http://traffic.alexa.com/graph?";
	this.peticion+="w="+ this.width +"&h="+ this.height;
	this.peticion+="&r="+ this.r +"&y="+ this.y +"&a="+ this.a +"&z="+ this.z;
	if(this.url1.length>3) this.peticion+="&u="+ this.url1;
	if(this.url2.length>3) this.peticion+="&u="+ this.url2;
	this.Cargar();
}
AlexaGraph.prototype.Cargar=function() {
	this.img.src=this.peticion;
	this.oTD.innerHTML='Loading...';
	setTimeout(this.imgID+'.Estado()',1000);
}
AlexaGraph.prototype.Estado=function() {
	if(this.img.complete) {
		this.Selector();
		this.oImg.src=this.img.src;
	} else {
		setTimeout(this.imgID+'.Estado()',500);
	}
}
AlexaGraph.prototype.setType=function(tipo) {
	this.y=tipo;
	this.Mostrar();
}
AlexaGraph.prototype.setRange=function(periodo) {
	this.r=periodo;
	this.Mostrar();
}
AlexaGraph.prototype.setSmoothing=function(grado) {
	this.z=grado;
	this.Mostrar();
}
AlexaGraph.prototype.setURL1=function(url) {
	this.url1=url;
}
AlexaGraph.prototype.setURL2=function(url) {
	this.url2=url;
}
//----------------------------------------------------------------

//Reloj
function Reloj(iSeg,oObj,sTexto,iRecargar) {
	if(typeof iSeg=="string") {
		if(iSeg=='') {
			var ahora=new Date();
			iSeg=ahora.getTime();
			iSeg=Math.floor(iSeg/1000);
		} else {
			var ahora=new Date(iSeg.substring(0,2),iSeg.substring(2,4),iSeg.substring(4,6),iSeg.substring(6,8),iSeg.substring(8,10),iSeg.substring(10,12));
			iSeg=ahora.getTime();
			iSeg=Math.floor(iSeg/1000);
		}
	}
	this.iSeg=iSeg;
	this.oObj=oObj;
	this.sTexto=sTexto;
	this.iRecargar=iRecargar;
}
Reloj.prototype.Fecha = function(oObj) {
	var meses=['I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII'];
	var fecha=new Date(1000*this.iSeg);
	var objeto=getObject(oObj);
	objeto.innerHTML=fecha.getDate()+'-'+meses[fecha.getMonth()]+'-'+fecha.getFullYear();
}
Reloj.prototype.Normal = function() {
	this.iSeg=this.iSeg+1;
	var reloj=new Date(1000*this.iSeg);
	var objeto=getObject(this.oObj);
	var seg=reloj.getSeconds();
	var min=reloj.getMinutes();
	var hor=reloj.getHours();
	seg=(seg<10)? "0"+seg : seg;
	min=(min<10)? "0"+min : min;
	hor=(hor<10)? "0"+hor : hor;
	//objeto.innerHTML=this.sTexto.replace('{CLOCK}',hor+':'+min+'&#39;'+seg+'&#34; ('+this.iSeg+')');
	objeto.innerHTML=this.sTexto.replace('{CLOCK}',hor+':'+min+'&#39;'+seg+'&#34;');
}
Reloj.prototype.Adelante = function() {
	segundos=this.iSeg++;
	objeto=getObject(this.oObj);
	horas=Math.floor(segundos/3600);
	segundos-=3600*horas;
	min=Math.floor(segundos/60);
	segundos-=60*min;
	seg=segundos;
	horas=(horas<10)? "0"+horas : horas;
	min=(min<10)? "0"+min : min;
	seg=(seg<10)? "0"+seg : seg;
	horas=(horas=="00")? "" : horas+":";
	min=(min=="00" && horas=="")? "" : min+":";
	//seg=(seg=="00" && min=="")? "Booommm!!" : seg+"''";
	//if(seg=="Booommm!!" && this.iRecargar==1) window.location.reload();
	//if(this.iSeg < 0) {
		//objeto.innerHTML=this.sTexto.replace('{CLOCK}',"Booommm!!");
	//} else {
		objeto.innerHTML=this.sTexto.replace('{CLOCK}',horas+min+seg);
	//}
}
Reloj.prototype.Atras = function() {
	segundos=this.iSeg--;
	objeto=getObject(this.oObj);
	horas=Math.floor(segundos/3600);
	segundos-=3600*horas;
	min=Math.floor(segundos/60);
	segundos-=60*min;
	seg=segundos;
	horas=(horas<10)? "0"+horas : horas;
	min=(min<10)? "0"+min : min;
	seg=(seg<10)? "0"+seg : seg;
	horas=(horas=="00")? "" : horas+":";
	min=(min=="00" && horas=="")? "" : min+":";
	seg=(seg=="00" && min=="")? "Booommm!!" : seg+"''";
	if(seg=="Booommm!!" && this.iRecargar==1) window.location.reload();
	if(this.iSeg < 0) {
		objeto.innerHTML=this.sTexto.replace('{CLOCK}',"Booommm!!");
	} else {
		objeto.innerHTML=this.sTexto.replace('{CLOCK}',horas+min+seg);
	}
}
Reloj.prototype.AtrasBoton = function(destinoURL) {
	segundos=this.iSeg--;
	objeto=getObject(this.oObj);
	horas=Math.floor(segundos/3600);
	segundos-=3600*horas;
	min=Math.floor(segundos/60);
	segundos-=60*min;
	seg=segundos;
	horas=(horas<10)? "0"+horas : horas;
	min=(min<10)? "0"+min : min;
	seg=(seg<10)? "0"+seg : seg;
	horas=(horas=="00")? "" : horas+":";
	min=(min=="00" && horas=="")? "" : min+":";
	seg=(seg=="00" && min=="")? "Booommm!!" : seg+"''";
	if(seg=="Booommm!!" && this.iRecargar==1) window.location.href=destinoURL;
	if(this.iSeg < 0) {
		objeto.value=this.sTexto.replace('{CLOCK}',"0''");
	} else {
		objeto.value=this.sTexto.replace('{CLOCK}',horas+min+seg);
	}
}

//Contador de caracteres.
function Contar(entrada,salida,texto,caracteres) {
	var entradaObj=getObject(entrada);
	var salidaObj=getObject(salida);
	var longitud=caracteres - entradaObj.value.length;
	if(longitud <= 0) {
		longitud=0;
		texto='<span class="disable">&nbsp;'+texto+'&nbsp;</span>';
		entradaObj.value=entradaObj.value.substr(0,caracteres);
	}
	salidaObj.innerHTML = texto.replace("{CHAR}",longitud);
}

function Imagen(url,botID,imgID) {
	this.url=url;
	this.imgObj=getObject(imgID);
	this.botObj=getObject(botID);
	this.cadenaTimeOut=botID.replace('bot','boton');
	this.img=new Image();

	this.Cargar();
}
Imagen.prototype.Cargar=function() {
	this.img.src=this.url;
	this.botObj.disabled=true;
	this.Estado();
}
Imagen.prototype.Estado=function() {
	if(this.img.complete) {
		this.botObj.disabled=false;
	} else {
		setTimeout(this.cadenaTimeOut+'.Estado()',1000);
	}
}
Imagen.prototype.Mostrar=function() {
	this.imgObj.src=this.img.src;
}

//Cambia color de las celdas
function mOvr(src,front, back) {
    if (!src.contains(event.fromElement)) {
        src.style.cursor = 'default';
        src.bgColor = back;
        src.Color = front;
    }
}
function mOut(src,front, back) {
    if (!src.contains(event.toElement)) {
        src.style.cursor = 'default';
        src.bgColor = back;
        src.Color = front;
    }
}

//Abre nueva ventana centrada en el centro de la pantalla
function ventana(theURL,winName,winWidth,winHeight) {
    var w = (screen.width - winWidth)/2;
    var h = (screen.height - winHeight)/2 - 50;
	if((winHeight%2)==1) {
		features = 'directories=no,location=no,menubar=no,scrollbars=yes,status=yes,toolbar=no,resizable=yes,width='+winWidth+',height='+winHeight+',top='+h+',left='+w;
	} else {
		features = 'directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no,width='+winWidth+',height='+winHeight+',top='+h+',left='+w;
	}
    window.open(theURL,winName,features);
}

function submitOnce(theform) {
	if (document.all || document.getElementById) {
		for(i=0 ; i < theform.length ; i++) {
			var tempobj = theform.elements[i];
			if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset") {
				tempobj.disabled=true;
			}
		}
	}
}

function validate(obj,modo,porDefecto, min) {
	var texto = obj.value;
	do {
		texto = texto.replace(/\|\|/,"|");
	} while(/\|\|/.test(texto));
	if(modo=="url") {
		if(texto.substr(0,7)!="http://" && texto.substr(0,8)!="https://") texto = "http://" + texto;
		texto = texto.replace(/ /,"");
	}
	if(modo=="email") {
		if(!/(.+)@(.+)\.(.+)/.test(texto)) texto = porDefecto;
		if(/[\s]+/.test(texto)) texto = porDefecto;
		if(texto.indexOf('@') != texto.lastIndexOf('@')) texto = porDefecto;
		var dominio = texto.substring(1+texto.lastIndexOf('.'));
		if( dominio.length>3 || dominio.length<2) texto = porDefecto;
	}
	if(modo=="icq") {
		do {
			texto = texto.replace(/[^0-9]/,"");
		} while(/[^0-9]/.test(texto));
	}
	if(modo=="number") {
		do {
			texto = texto.replace(/[^0-9]/,"");
		} while(/[^0-9]/.test(texto));
		if(texto=='') texto = porDefecto;
	}
	if(modo=="numberMin") {
		do {
			texto = texto.replace(/[^0-9]/,"");
		} while(/[^0-9]/.test(texto));
		if(texto=='') texto = porDefecto;
		if(parseInt(texto) < min) texto = porDefecto;
	}
	if(modo=="text") {
		if(texto=='') texto = porDefecto;
	}
	obj.value = texto;
}

