
var state = 'hidden';

function showhide(div1,div2) {
	if (state == 'visible') {
		state = 'hidden';
	} else {
		state = 'visible';
	}
	if (document.all) {
		eval( "document.all." + div1 + ".style.visibility = state");
		eval( "document.all." + div2 + ".style.visibility = state");
	}
	if (document.getElementById && !document.all) {
		div1 = document.getElementById(div1);
		div2 = document.getElementById(div2);
		div1.style.visibility = state;
		div2.style.visibility = state;
	}
}

// Copyright © 2000 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.
//
// ************************
// layer utility routines *
// ************************

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
	return false;
    }
} // changeObjectVisibility

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.left = newXCoordinate;
	styleObject.top = newYCoordinate;
	return true;
    } else {
	// we couldn't find the object, so we can't very well move it
	return false;
    }
} // moveObject

// Copyright © 2000 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.
// ********************************
// application-specific functions *
// ********************************

// store variables to control where the popup will appear relative to the cursor position
// positive numbers are below and to the right of the cursor, negative numbers are above and to the left
var xOffset = 15;
var yOffset = -5;

function showPopup (targetObjectId, eventObj) {
    if(eventObj) {
	// hide any currently-visible popups
	hideCurrentPopup();
	// stop event from bubbling up any farther
	eventObj.cancelBubble = true;
	// move popup div to current cursor position 
	// (add scrollTop to account for scrolling for IE)
	var newXCoordinate = (eventObj.pageX)?eventObj.pageX + xOffset:eventObj.x + xOffset + ((document.body.scrollLeft)?document.body.scrollLeft:0);
	var newYCoordinate = (eventObj.pageY)?eventObj.pageY + yOffset:eventObj.y + yOffset + ((document.body.scrollTop)?document.body.scrollTop:0);
	moveObject(targetObjectId, newXCoordinate, newYCoordinate);
	// and make it visible
	if( changeObjectVisibility(targetObjectId, 'visible') ) {
	    // if we successfully showed the popup
	    // store its Id on a globally-accessible object
	    window.currentlyVisiblePopup = targetObjectId;
	    return true;
	} else {
	    // we couldn't show the popup, boo hoo!
	    return false;
	}
    } else {
	// there was no event object, so we won't be able to position anything, so give up
	return false;
    }
} // showPopup

function hideCurrentPopup() {
    // note: we've stored the currently-visible popup on the global object window.currentlyVisiblePopup
    if(window.currentlyVisiblePopup) {
	changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
	window.currentlyVisiblePopup = false;
    }
} // hideCurrentPopup



// ***********************
// hacks and workarounds *
// ***********************

// initialize hacks whenever the page loads
window.onload = initializeHacks;

// setup an event handler to hide popups for generic clicks on the document
document.onclick = hideCurrentPopup;

function initializeHacks() {
    // this ugly little hack resizes a blank div to make sure you can click
    // anywhere in the window for Mac MSIE 5
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	window.onresize = explorerMacResizeFix;
    }
    resizeBlankDiv();
    // this next function creates a placeholder object for older browsers
    createFakeEventObj();
}

function createFakeEventObj() {
    // create a fake event object for older browsers to avoid errors in function call
    // when we need to pass the event object to functions
    if (!window.event) {
	window.event = false;
    }
} // createFakeEventObj

function resizeBlankDiv() {
    // resize blank placeholder div so IE 5 on mac will get all clicks in window
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	getStyleObject('blankDiv').width = document.body.clientWidth - 20;
	getStyleObject('blankDiv').height = document.body.clientHeight - 20;
    }
}

function explorerMacResizeFix () {
    location.reload(false);
}

/* PROPIO */

function gt() {
	document.write("<a href='mai&#108;to:")
	a=gt.arguments;
	if (a.length==1) { a=a[0]; b='&#118;&#105;&#114;&#117;&#115;&#116;&#111;&#116;&#097;&#108;&#046;&#099;&#111;&#109;';
	} else { b=a[0]; a=a[1]; }
	
	document.write(a+"&#064;"+b+"'>"+a+"&#064;"+b+"</a>");
}
function gt3() {
	document.write("<a href='mai&#108;to:");
	a=gt3.arguments;
	b=a[0]; c=a[2]; a=a[1];
	document.write(a+"&#064;"+b+"'>"+a+"&#064;"+c+"</a>");
}

function envioSeguro() {
	var locationtxt=document.location+'';
	if (locationtxt.indexOf('https') == 0) {
		document.location=locationtxt.replace(/https/g, 'http');
	} else {
		document.location=locationtxt.replace(/http/g, 'https');
	}
}

function marcaDistribucion() {
	if (document.consulta.distribucion.checked) {
		document.consulta.distribuir.value=0;
	} else {
		document.consulta.distribuir.value=1;
	}
	//alert(document.consulta.distribuir.value);
}

//////// FUNCIONES PARA AJAX PINO
var http_request = false;
function ajaxPino(direccion, backf) {
	if (window.XMLHttpRequest) {
		try {
			http_request = new XMLHttpRequest();
	  } catch(e) {
			http_request = false;
	  }
	  if (http_request.overrideMimeType) http_request.overrideMimeType('text/xml');
	  
	} else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if (http_request) {
	    http_request.onreadystatechange = backf;
	    http_request.open("GET", direccion, true);
	    http_request.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" ); // P IE :p
	    http_request.send(null);
	}
}

function cargaCarga() {
	ajaxPino('/vt/en/colainfo', muestraCarga);
}

var nivel=-1;
function muestraCarga() {
	var enviar=false;
	
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			if (http_request.responseText.length == 1) {
				nivel=http_request.responseText;
			}
		}
	}
	
	if (nivel != -1) {
		document.getElementById('imgcarga').innerHTML='<img src="/img/nivel-'+nivel+'.png" alt="Nivel de carga '+nivel+'" width="70" height="13" hspace="5" vspace="0" border="0" />';
		if (nivel > 4) {
			document.getElementById('mensaje-aviso').style.display="block";
		}
	}
}

function ordena(a, b) {
	a=a.toLowerCase();
	b=b.toLowerCase();

    if(a == b) return 0;
    if(a < b)  return -1;
    return 1;
}

function introduceTablaResultados(tablaid, objetos, orden) {
	if (typeof orden == "undefined") {
		orden=0;
	}
	
	var tbl = document.getElementById(tablaid);
	var lastRow = tbl.rows.length;
	
	var insertPos = lastRow;
	if (orden) {
		for (var i=1; i < lastRow; i++) {
			var row = tbl.getElementsByTagName("tr")[i];
			var cel = row.getElementsByTagName("td")[0].childNodes[0].data;
			
			if (ordena(objetos[0],cel) < 0) {
				insertPos=i;
				break;
			}
		}
	}
	var row = tbl.insertRow(insertPos);
	
	for (var i=0; i < objetos.length; i++) {
		var cell = row.insertCell(i);
		var textN = document.createTextNode(objetos[i]);

		// Coloreo de resultados
		if (i==3) {
			if ( (objetos[i] != '-') && (objetos[i] != 'no ha encontrado virus') && (objetos[i] != 'no virus found') && (objetos[i] != 'No threat detected') && (objetos[i] != 'Not analyzed yet')) {
				cell.className="positivo";
			}
		}
		cell.appendChild(textN);
		if ( (objetos[i].indexOf('<br/>') > 0) || (objetos[i].indexOf('<br />') > 0) ) {	// modo no intepretacion
			cell.innerHTML=objetos[i];
		}
	}
	
	// Pintamos
	lastRow = tbl.rows.length;
	for (var i=1; i < lastRow; i++) {
		row=tbl.rows[i];
		if (i % 2 == 0) {
			row.className="odd";
		} else {
			row.className="";
		}
	}
}


function cargaMuestras() {
	ajaxPino('/vt/en/estadisticas', introduceMuestras);
}

function introduceMuestras() {
	var respuesta='';
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			respuesta=http_request.responseText;
		} else {
			introduceTablaResultados('tablaMuestras', ['Loading ERROR...','']);
		}
	}
	
	if (respuesta.length > 0) {
		var muestras= eval("(" + respuesta + ")");
	
		for (var i=0; i < muestras.length; i++) {
			introduceTablaResultados('tablaMuestras',muestras[i]);
		}
	}
}

function setLanguage(nuevoIdioma) {
	var expires= new Date (2015, 12, 31);
	document.cookie='idioma=' + nuevoIdioma + "; path=/; expires=" + expires.toGMTString();
	return true;
}

function abreCompacto() {
	var a=window.open('/compacto.html','CompactWindow','width=640,height=480,resizable=yes,scrollbars=yes'); 
	a.focus();
	return false;
}

// ************************************************
var identificador='';
function identificadorSubida() {
	ajaxPino('/vt/en/identificador', function () {
				if (http_request.readyState == 4) {
					if (http_request.status == 200) {
						identificador=http_request.responseText;
						document.forms["consulta"].action+='?'+identificador;
						document.forms["consulta"].submit();
						iniciaControlSubida();
					}
				}
			});
	return false;
}

function iniciaControlSubida() {
	ajaxPino('/vt/en/progreso?'+identificador, function () {
				if (http_request.readyState == 4) {
					if (http_request.status == 200) {
						var recogido=http_request.responseText;
						if (recogido == '') {
							document.getElementById('controlsubida').style.display="none";
						} else {
							document.getElementById('controlsubida').style.display="block";
						}
						document.getElementById('progreso').innerHTML=recogido;
						setTimeout("iniciaControlSubida();",1000);
					}
				}
			});
	
}
