// JavaScript Document
function pasarFotoCat(proy, slide){
	if(document.getElementById('proy_'+proy+'_slide_'+(slide+1))){
		var next = slide+1;
	}else{
		var next = 1;
	}
	if(slide != next){
		document.getElementById('proy_'+proy+'_slide_'+slide).fade('out');
		document.getElementById('proy_'+proy+'_slide_'+next).fade('in');
	}
}
function mostrar(que, como){
	if(document.getElementById(que).style.display!=como){
		document.getElementById(que).style.display=como;
	}else{
		document.getElementById(que).style.display='none';
	}
}
function agregarTexto(msjid,pa,texto){
	var msj = document.createElement("span");
	var txt = document.createTextNode(texto);
	var br = document.createElement("br");
	msj.setAttribute('id',msjid);
	br.setAttribute('id','br'+msjid);
	msj.className='error';
	pa.appendChild(br);
	pa.appendChild(msj);
	pa.setAttribute('class','td_error');
	document.getElementById(msjid).appendChild(txt);
}
function validar(){
	var errors=Array(),args=validar.arguments;
	// SELECTS OPCIONALES
	var selects = document.getElementsByTagName("select");
	for(a in selects){
		var spa = selects[a].parentNode;
		var msjsel = 'msj_select'+a;
		if(document.getElementById(msjsel) != null ){
			spa.removeChild(document.getElementById('br'+msjsel));
			spa.removeChild(document.getElementById(msjsel));
			spa.setAttribute('class','td_normal');
		}
		if(selects[a].value == "-- Seleccione --"){
			agregarTexto(msjsel,spa,"Requerido");
			errors.push("error");
		}
	}
	for (i=0;i<args.length;i+=2){
		var el = document.getElementById(args[i]);
		if(typeof(formu) == "undefined") formu = formuParent(el);
		var pa = el.parentNode;
		var msjid = 'msj'+i;
		if(document.getElementById(msjid) != null ){
			pa.removeChild(document.getElementById('br'+msjid));
			pa.removeChild(document.getElementById(msjid));
			pa.setAttribute('class','td_normal');
		}
		// CAMPO REQUERIDO
		if(args[i+1].indexOf('R')>=0){
			if(el.value==""){
				agregarTexto(msjid,pa,"Requerido");
				errors[i] = "error";
			}
		}
		// VALIDACIÓN DE EMAIL
		if(args[i+1].indexOf('isEmail')>=0 && !errors[i]){
			emailval = el.value;
			arroba = emailval.lastIndexOf('@');
			arrobas = emailval.match(/\@/g);
			punto = emailval.lastIndexOf('.');
			if(arroba<1 || punto<arroba || punto==arroba+1 || punto==emailval.length-1 || arrobas.length > 1 || emailval.indexOf("@.")>=0){
				agregarTexto(msjid,pa,"Por favor, ingrese un email válido");
				errors[i] = "error";
			}
		}
		// VALIDACIÓN DE CAMPO NUMÉRICO
		if(args[i+1].indexOf('isNum')>=0 && !errors[i]){
			val = el.value;
			if (isNaN(val)) {
				agregarTexto(msjid,pa,"Ingrese sólo números");
				errors[i] = "error";
			}
		}
		// VALIDACIÓN DE CAMPO NUMÉRICO ESTRICTO
		if(args[i+1].indexOf('isSnum')>=0 && !errors[i]){
			val = el.value;
			if (val.toString().search(/^[0-9]*$/) != 0) {
				agregarTexto(msjid,pa,"Ingrese sólo números");
				errors[i] = "error";
			}
		}
	}
	document.errores = (errors.length > 0);
	return !document.errores;
}
function confirmar(){
	if(!document.errores){
		var errors=Array(),args=confirmar.arguments, dea = args.length % 3 == 0 ? 3 : 2;
		for (i = 0; i < args.length; i += dea){
			var arg1 = dea == 3 ? document.getElementById(args[i+2]+args[i])   : document.getElementById(args[i]);
			var arg2 = dea == 3 ? document.getElementById(args[i+2]+args[i+1]) : document.getElementById(args[i+1]);
			var pa = arg2.parentNode;
			var msjid = 'conf'+i;
			if(typeof(formu) == "undefined") formu = formuParent(arg1);
			if(document.getElementById(msjid) != null ){
				pa.removeChild(document.getElementById('br'+msjid));
				pa.removeChild(document.getElementById(msjid));
			}
			if(arg1.value != arg2.value){
				agregarTexto(msjid,pa," * "+args[i+1]+" debe coincidir con "+args[i]);
				errors[i] = "error";
			}
		}
		document.errores = (errors.length > 0);
		return !document.errores;
	}
	return false;
}
function formuParent(el){
	if(el.parentNode.tagName == "FORM") return el.parentNode;
	else return formuParent(el.parentNode);
}
function enviar(ok){
	document.errores = false;
	if(typeof(enviando) == "undefined"){
		var texto = document.createTextNode("enviando...");
		enviando = document.createElement("div");
		enviando.className = "mensaje";
		enviando.id = "enviando";
		enviando.style.display = "none";
		enviando.appendChild(texto);
		formu.parentNode.appendChild(enviando);
	}
	if(ok){
		enviando.style.display = "block";
		formu.style.display = "none";
		return true;
	}
	return false
}
function trim(cadena){
	for(i=0; i<cadena.length; )	{
		if(cadena.charAt(i)==" "){
			cadena=cadena.substring(i+1, cadena.length);
		}else{
			break;
		}
	}
	for(i=cadena.length-1; i>=0; i=cadena.length-1)	{
		if(cadena.charAt(i)==" "){
			cadena=cadena.substring(0,i);
		}else{
			break;
		}
	}
	return(cadena);
}
function agregarTitulo(){
	var seccion = document.URL.indexOf("casos") > 0 ? "caso" : "proyecto";
	var tabla = document.getElementById("tabla_alta_"+seccion);
	var tr1 = tabla.insertRow(tabla.rows.length -1);
	var tr2 = tabla.insertRow(tabla.rows.length -1);
	var th1 = tr1.insertCell(0);
	var th2 = tr1.insertCell(1);
	var td1 = tr2.insertCell(0);
	var td2 = tr2.insertCell(1);
	var input1 = document.createElement("input");
	var input2 = document.createElement("textarea");
	var input3 = document.createElement("input");
		input3.type = "hidden";
		input3.name = "titulo_"+seccion+"_"+n+"_"+seccion;
		input3.id = input3.name;
		input3.value = document.getElementById("cambiar_"+seccion).value;
	var input4;
	var n = 0;
	tr1.className = "fila1";
	tr1.style.textAlign = "left";
	input1.type = "text";
	input1.className = "full";
	input2.className = "full";
	th1.appendChild(document.createTextNode("Título"));
	th2.appendChild(input1);
	td1.appendChild(document.createTextNode("Contenido"));
	td2.appendChild(input2);
	CKEDITOR.replace( input2 ); 
	td2.appendChild(input3);
	if(input4 = document.getElementById("agregar_titulo_"+seccion)){
		n = input4.value;
		input4.type = "hidden";
		input4.value++;
	}else{
		input4 = document.createElement("input");
		input4.name = "agregar_titulo_"+seccion;
		input4.id = input4.name;
		input4.type = "hidden";
		input4.value = 1;
		th1.appendChild(input4);
	}
	input1.name = "titulo_"+seccion+"_"+n+"_titulo";
	input2.name = "titulo_"+seccion+"_"+n+"_contenido";
	input3.name = "titulo_"+seccion+"_"+n+"_"+seccion;
	input1.id = input1.name;
	input2.id = input2.name;
	input3.id = input3.name;
}
function editarTituloCaso(cual, seccion){
	if(typeof(seccion) == "undefined") seccion = "caso";
	var hidden = document.getElementById('cambiar_titulo_'+seccion);
	var arrHidden = hidden.value == "" ? Array() : hidden.value.split(",");
	var yata = arrHidden.search(cual);
	if(yata){
		document.getElementById('titulo_'+seccion+'_titulo_'+cual).disabled = true;
		document.getElementById('titulo_'+seccion+'_contenido_'+cual).disabled = true;
		CKEDITOR.instances['titulo_'+seccion+'_contenido_'+cual].destroy();
		document.getElementById('img_editar_titulo_'+seccion+'_'+cual).src = 'img/editar.gif';
		arrHidden.splice(yata,1);
	}else{
		document.getElementById('titulo_'+seccion+'_titulo_'+cual).disabled = false;
		document.getElementById('titulo_'+seccion+'_contenido_'+cual).disabled = false;
		CKEDITOR.replace( document.getElementById('titulo_'+seccion+'_contenido_'+cual) ); 
		document.getElementById('img_editar_titulo_'+seccion+'_'+cual).src = 'img/noeditar.gif';
		arrHidden.push(cual);
	}
	hidden.value = arrHidden.join(",");
}
function eliminarTituloCaso(cual, seccion){
	if(typeof(seccion) == "undefined") seccion = "caso";
	var hidden = document.getElementById('eliminar_titulo_'+seccion);
	var arrHidden = hidden.value == "" ? Array() : hidden.value.split(",");
	var yata = arrHidden.search(cual);
	if(yata){
		document.getElementById('tr_titulo_'+seccion+'_'+cual+'_fila1').className = "fila1";
		document.getElementById('tr_titulo_'+seccion+'_'+cual+'_fila2').className = "";
		document.getElementById('tr_titulo_'+seccion+'_'+cual+'_fila3').className = "";
		document.getElementById('img_editar_titulo_'+seccion+'_'+cual).src = 'img/editar.gif';
		document.getElementById('img_editar_titulo_'+seccion+'_'+cual).style.display = 'inline';
		document.getElementById('img_eliminar_titulo_'+seccion+'_'+cual).src = 'img/elimina.gif';
		arrHidden.splice(yata,1);
	}else{
		var hidden2 = document.getElementById('cambiar_titulo_'+seccion);
		var arrHidden2 = hidden2.value == "" ? Array() : hidden2.value.split(",");
		var yata2 = arrHidden2.search(cual);
		if(yata2){
			arrHidden2.splice(yata2,1);
			hidden2.value = arrHidden2.join(",");
			CKEDITOR.instances['titulo_'+seccion+'_contenido_'+cual].destroy();
		}
		document.getElementById('tr_titulo_'+seccion+'_'+cual+'_fila1').className = "fila1 tr_eliminar";
		document.getElementById('tr_titulo_'+seccion+'_'+cual+'_fila2').className = "tr_eliminar";
		document.getElementById('tr_titulo_'+seccion+'_'+cual+'_fila3').className = "tr_eliminar";
		document.getElementById('titulo_'+seccion+'_titulo_'+cual).disabled = true;
		document.getElementById('titulo_'+seccion+'_contenido_'+cual).disabled = true;
		document.getElementById('img_editar_titulo_'+seccion+'_'+cual).style.display = 'none';
		document.getElementById('img_eliminar_titulo_'+seccion+'_'+cual).src = 'img/noelimina.gif';
		arrHidden.push(cual);
	}
	hidden.value = arrHidden.join(",");
}
function editarTituloProyecto(cual){ editarTituloCaso(cual, "proyecto"); }
function eliminarTituloProyecto(cual){ eliminarTituloCaso(cual, "proyecto"); }
function agregarImg(id_tabla){
	actual = typeof(actual) == "undefined" ? 2 : actual +1;
	var tabla = document.getElementById(id_tabla);
	var primeraCelda = tabla.rows[0].cells[0];
	var ultimaFila = tabla.rows.length-1;
	var row = tabla.insertRow(ultimaFila);
	var cellLeft = row.insertCell(0);
	var cellRight = row.insertCell(1);
	var numero = document.createTextNode(actual);
	var fileField = document.createElement("input");
	fileField.setAttribute("type","file");
	fileField.setAttribute("name","imagen"+actual);
	cellLeft.appendChild(numero);
	cellRight.appendChild(fileField);
	if(document.getElementById('bot_restarimg') == null){
		var restar = document.createElement("img");
		restar.setAttribute("src","img/restar.gif");
		restar.setAttribute("onclick","restarImg('"+id_tabla+"')");
		restar.setAttribute("id","bot_restarimg");
		restar.setAttribute("alt","Quitar");
		restar.setAttribute("title","Quitar");
		restar.className = "restar";
		primeraCelda.appendChild(restar);
	}
}
function restarImg(id_tabla){
	actual--;
	var tabla = document.getElementById(id_tabla);
	var ultimaFila = tabla.rows.length-2;
	tabla.deleteRow(ultimaFila);
	if(actual==1){
		var restar = document.getElementById("bot_restarimg");
		var pa = restar.parentNode;
		pa.removeChild(restar);
	}
}
function agregarCategorias(cual, donde){
	var seccion = document.URL.indexOf("casos") > 0 ? "caso" : "proyecto";
	if(typeof(arrCats) == "undefined"){
		var anterior = document.getElementById(seccion+'_'+donde+'_categorias').value;
		if(anterior == '') arrCats = Array();
		else arrCats = anterior.split(",");
	}
	if(arrCats.search(cual)) arrCats.splice(arrCats.indexOf(cual),1)
	else arrCats.push(cual);
	document.getElementById(seccion+'_'+donde+'_categorias').value = arrCats;
}
function flashLyteBox(url,width,height,caption) {
	var objLink = document.createElement('a');
	objLink.setAttribute('href',url);
	objLink.setAttribute('rel','lytebox');
	if(typeof caption != 'undefined') { objLink.setAttribute('title',caption); }
	if(typeof width != 'undefined') { objLink.setAttribute('width',width); }
	if(typeof height != 'undefined') { objLink.setAttribute('height',height); }
	myLytebox.start(objLink);
}
function GroupDelegate(id) {
   var objLink = document.getElementById(id);
   myLytebox.start(objLink);
}
Array.prototype.search = function(que){
	for(i in this){
		if(this[i] == que){
			return i;
		}
	}
	return false;
}
/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};
