//CÛdigo que checa quÈ tipo de browser es... las variables est·n disponibles para todas las funciones
var bV=parseInt(navigator.appVersion);
NS4=(document.layers) ? true : false;
IE4=((document.all)&&(bV>=4)) ? true : false;
DOM=(!document.layers && !document.all && bV>=4) ? true : false; // A hack to guess if the browser supports the DOM
capable = (NS4 || IE4 || DOM) ? true : false;

function confirmarBorrarMensaje(titulo, url){
	if (titulo != "") {
		msg = "øSeguro que desea borrar el mensaje\n\""+titulo+"\"?" ;
	} else {
		msg = "øSeguro que desea borrar el mensaje?" ;
	}
	
	if (confirm(msg))
		window.location.replace(url);
}

function confirmarBorrarDiscusion(titulo, url){
  if(confirm("øSeguro que desea borrar la discusiÛn\n\""+titulo+"\"?"))
    window.location.replace(url);
}

function confirmarBorrarTema(titulo, url){
  if(confirm("øSeguro que desea borrar el tema\n\""+titulo+"\"?"))
    window.location.replace(url);
}

function confirmarBorrarCategoria(titulo,url){
  if(confirm("øSeguro que desea borrar la categorÌa\n\""+titulo+"\"?"))
    window.location.replace(url);
}

function confirmarBorrarPeula(titulo, url){
  if(confirm("øSeguro que desea borrar la Peul·\n\""+titulo+"\"?"))
    window.location.replace(url);
}

function confirmarBorrarArticulo(titulo, url){
  if(confirm("øSeguro que desea borrar el artÌculo\n\""+titulo+"\"?"))
    window.location.replace(url);
}

function Padre(nombre, id, hijos){
	this.nombre = nombre;		//Nombre
	this.id = id;				//Id en la base de datos
	this.hijos = hijos;
}

function Hijo(nombre, id){
	this.nombre = nombre;		//Nombre
	this.id = id;				//Id en la base de datos
}

function escribirSelect(select, arreglo, id){
	tam=arreglo.length;
	for(i=1; i<tam; i++){
		select.options[i]=new Option();
		select.options[i].value = arreglo[i].id;
		select.options[i].text = arreglo[i].nombre;
		if(arreglo[i].id==id)
			select.options[i].selected = true;
	}
}

function borrarSelect(select){
	tam = select.length;
	for (i=tam-1; i>0; i--){
		select.options[i]=null;			
	}
}

function modificarSelect(select, indice, arreglo){
	borrarSelect(select);
	if(indice>0){
		arregloHijos = arreglo[indice].hijos;
		escribirSelect(select, arregloHijos);
	}
}

function verificaFormaAddMensaje (oForm) {
	retVal = true;
	if (  (oForm.titulo.value =="")  || (oForm.cuerpo.value =="")  ) {
		retVal = false;
		window.alert("Debes llenar el tÌtulo y el cuerpo del mensaje");
	}
	return retVal;
}

function verificaFormaAddTema (oForm) {
	retVal = true;
	if (oForm.temanombre.value == "") {
		retVal = false;
		window.alert("Debes dar un titulo al tema");
	}
	return retVal;
}

function verificaFormaAddCategoria (oForm) {
	retVal = true;
	if (oForm.categorianombre.value == "") {
		retVal = false;
		window.alert("Debes dar un titulo a la categoria");
	}
	return retVal;
}

function arrange() {
  nextY = document.layers[firstInd].pageY + document.layers[firstInd].document.height;
  for (i=firstInd+1; i<document.layers.length; i++) {
    whichEl = document.layers[i];
    if (whichEl.visibility != "hide") {
      whichEl.pageY = nextY;
      nextY += whichEl.document.height;
    }
  }
}

function showHideNS (oWhat, imgControl, hrefOpen, hrefClose) {
	alert("NS");
	if (oWhat.visibility == "hide") {
		oWhat.visibility = "show";
		imgControl.src = hrefClose;
	} else {
		oWhat.visibility = "hide";
		imgControl.src = hrefOpen;
	}
}

function showHideIE (oWhat, imgControl, hrefOpen, hrefClose) {
	//alert("IE");
	//oWhat = eval(oWhat);
	//imgControl = eval(imgControl);
	
	//imgControl = eval("imgTree_"+imgControl);
	imgControl = document.all[imgControl];
	oWhat = document.all[oWhat];

	if (oWhat.style.display == "none") {
		oWhat.style.display = "";
		imgControl.src = hrefClose;
	} else {
		oWhat.style.display = "none";
		imgControl.src = hrefOpen;
	}
	//window.event.cancelBubble = true;
}

function showHideDOM (oWhat, imgControl, hrefOpen, hrefClose) {
	//alert("DOM");
	imgControl = document.getElementById(imgControl);
	oWhat = document.getElementById(oWhat);

/*	if (oWhat.style.visibility != "visible") {
		oWhat.style.visibility = "visible";
		imgControl.src = hrefClose;
	} else {
		oWhat.style.visibility = "hidden";
		imgControl.src = hrefOpen;
	}*/

	if (oWhat.style.display != "") {
		oWhat.style.display = "";
		imgControl.src = hrefClose;
	} else {
		oWhat.style.display = "none";
		imgControl.src = hrefOpen;
	}
}

//function showHide (oImgControl, oWhat, hrefOpen, hrefClose) {
function showHide (oImgControl, oWhat) {
	//	Abre y cierra ramas del arbol de categorias
	var userNav = "kurejwowski"+navigator.platform;
	//alert(userNav);
	//alert(navigator.platform);
	hrefOpen = "imagenes/tree/plusmac.gif";
	hrefClose = "imagenes/tree/minusmac.gif";

	if (userNav.indexOf("Win")>1) {
		hrefOpen = "imagenes/tree/plus.gif";
		hrefClose = "imagenes/tree/minus.gif";
	} else{
		hrefOpen = "imagenes/tree/plusmac.gif";
		hrefClose = "imagenes/tree/minusmac.gif";
	}

	if (!capable) {return}
	
	//alert(oWhat);
	//oImgControl = document

	if (IE4) {
		showHideIE (oWhat, oImgControl, hrefOpen, hrefClose);
		return;
	} else if (NS4) {
		showHideNS (oWhat, oImgControl, hrefOpen, hrefClose);
		return;
	} else if (DOM) {
		showHideDOM (oWhat, oImgControl, hrefOpen, hrefClose);
		return;
	}
}

function hideCategoryLeafs () {
	//showHide('imgTree_<?PHP echo $categorias[$i]['id'] ?>', 'cat_<?PHP echo $categorias[$i]['id'] ?>'); 			
	//catIdsArray.push('<?PHP echo $categorias[$i]['id'] ?>');
	for (x = 0 ; x < catIdsArray.length ; x++) {
		showHide('imgTree_'+catIdsArray[x], 'cat_'+catIdsArray[x]); 			
	}
}
function prepareCategoryImages () {
	/*
	var bV=parseInt(navigator.appVersion);
	NS4=(document.layers) ? true : false;
	IE4=((document.all)&&(bV>=4)) ? true : false;
	DOM=(!document.layers && !document.all && bV>=4) ? true : false; // A hack to guess if the browser supports the DOM
	capable = (NS4 || IE4 || DOM) ? true : false;
	*/

	hrefOpen = "imagenes/tree/plusmac.gif";
	hrefClose = "imagenes/tree/minusmac.gif";

	//var userNav = "kurejwowski"+navigator.platform;
	if ((navigator.platform.indexOf("Win")>=0)) {
		hrefOpen = "imagenes/tree/plus.gif";
		hrefClose = "imagenes/tree/minus.gif";
	} else{
		hrefOpen = "imagenes/tree/plusmac.gif";
		hrefClose = "imagenes/tree/minusmac.gif";
	}
	
	for (imgIndex = 0; imgIndex < (document.images.length -1) ; ++imgIndex) {
		imgObj = document.images.item(imgIndex);

		if (imgObj.id.indexOf("imgTree") >= 0) {
			//alert("rep "+imgIndex+" "+document.images(imgIndex).id);
			_pos = imgObj.id.indexOf("_");
			idNumber = imgObj.id.substr(_pos+1);
			catObj = document.getElementById("cat_"+idNumber);
			//alert ("idNumbre: "+idNumber +"\ncatObj: "+ catObj);
			if (catObj.style.display == "none") {
				imgObj.src = hrefOpen;
			} else {
				imgObj.src = hrefClose;
			}
		}
	}
	
}

function doHilight() {
	//agregar cÛdigo que checa el hijo, y si es un "A", que le cambie el color
	
	if ((window.event.srcElement.tagName != "A")&&(window.event.srcElement.tagName != "INPUT")&&(window.event.srcElement.tagName != "SELECT"))	 {
		var wrkObject = window.event.srcElement;
	} else {
		var wrkObject = window.event.srcElement.parentNode;
		//alert(wrkObject.tagName);		
	}
	tmpIndex = wrkObject.className.indexOf("Lowlighted");
	if (tmpIndex != -1) {
		wrkObject.className = wrkObject.className.substr(0, tmpIndex) + "Hilighted"
	}
}

function doUnHilight() {
	if (window.event.srcElement.tagName == "A")	 {
		var wrkObject = window.event.srcElement.parentNode;
	} else {
		var wrkObject = window.event.srcElement;
	}
	//if  ( (window.event.toElement && !window.event.srcElement.contains(window.event.toElement))  ||  !window.event.toElement ){
	tmpIndex = wrkObject.className.indexOf("Hilighted");
	if (tmpIndex != -1) {
		wrkObject.className = wrkObject.className.substr(0, tmpIndex) + "Lowlighted";
	}
}




function getParentTable(el) {
	if (el == null || el.tagName == null){
		return null;
	}
	while (el.tagName != "TABLE"){
		el = el.parentElement;
	}
	return el;
}
function getSrcParentTD(e){
	if (e == null){
		return null;
	}
	//var e = event.srcElement
	while (e.tagName != "TD") {
		e = e.parentElement;
	}
	return e;
}

var oLastPopItem = null;
function highLightPopItem (e) {
	if (oLastPopItem != e){
		dimPopItem((oLastPopItem));
	}
	eTD = getSrcParentTD(e);
	eTD.className = "highPop";
	oLastPopItem = e;
}

function dimPopItem (e) {
	eTD = getSrcParentTD(e);
	if (eTD) {
		eTD.className = "dimPop";
	}
}

function mouseOutPopMenu (menuID, parentID) {
}
function mouseOutPopParent (menuID, parentID) {
}

function aceptarPeula ( nuevoEstadoVal ) {
	//usada en peulot_ver para aceptar, rechazar o marcar una peula como indebida
	//agregar confirmaciÛn
	if (	confirm("øEstas seguro de querer cambiar la peul·?") ) {
		var nuevoEstado;
		var theForm;
		nuevoEstado = document.getElementById("nuevoEstado");
		nuevoEstado.value = nuevoEstadoVal;
		//peulot_aprueba.
		theForm = document.getElementById('peulot_aprueba');
		theForm.submit();
	} else {
		return false;
	}
}


function validarSeleccionOrg(laforma){
	if(laforma.organizacion.value == -1){
		window.alert("Seleccione otra organizacion");
		return false;
	}
	else{
		laforma.submit();
	}
}

//className
//window.event.srcElement

/*function menuIzquierdaHilight (what) {
	what.style.background="RoyalBlue";
	what.style.border="solid 1px LightSteelBlue";
}

function menuIzquierdaUnHilight (what) {
   what.style.background="transparent";
   what.style.border="solid 1px CornFlowerBlue";
}

function activeHilight (what) {
   what.style.background="RoyalBlue" ;
   //what.style.border = "solid 1px lightsteelblue" ;
}
function activeUnHilight (what) {
   what.style.background="CornFlowerBlue";
   //what.style.border = "solid 1px cornflowerblue";
}
*/


//className
//window.event.srcElement
/*
function doHilight() {
	//agregar cÛdigo que checa el hijo, y si es un "A", que le cambie el color
	
	if ((window.event.srcElement.tagName != "A")&&(window.event.srcElement.tagName != "INPUT"))	 {
		var wrkObject = window.event.srcElement;
	} else {
		var wrkObject = window.event.srcElement.parentNode;
	}
	tmpIndex = wrkObject.className.indexOf("Lowlighted");
	if (tmpIndex != -1) {
		wrkObject.className = wrkObject.className.substr(0, tmpIndex) + "Hilighted"
	}
}

function doUnHilight() {
	if (window.event.srcElement.tagName == "A")	 {
		var wrkObject = window.event.srcElement.parentNode;
	} else {
		var wrkObject = window.event.srcElement;
	}
	//if  ( (window.event.toElement && !window.event.srcElement.contains(window.event.toElement))  ||  !window.event.toElement ){
	tmpIndex = wrkObject.className.indexOf("Hilighted");
	if (tmpIndex != -1) {
		wrkObject.className = wrkObject.className.substr(0, tmpIndex) + "Lowlighted";
	}
}

*/

function poblarOptionKvutzot(arregloNombres, arregloValores){
	for(var i = 0 ; i <= arregloNombres.length ; i++){
		var tmpOption = new Option(arregloNombres[i], arregloValores[i]);
		var sel = document.formaPeula.kvutza;
		sel.options[sel.options.length] = tmpOption;
	}
}


var win = null;
function openWinInfo(nick){
	myleft = (screen.width)?(screen.width-300)/2:100;
	mytop = (screen.height)?(screen.height-450)/2:100;
	mytop = mytop - 50;
	settings = "width=300, height=450, top=" + mytop + ", left=" + myleft + ", scrollbars=no, location=no, directories=no, status=no, menubar=no, toolbar=no, resizable=no";
	win = window.open('info_user.php?usuario='+nick, 'miniventana', settings);
	win.focus();
	return false;
}