function getxmlhttp(){
	var xmlhttp = false;

	try{
		//If the javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		//alert('Explorer 6');
	}catch (e){
		//If not, then use the older active x object.
		try{
			//If we are using internet explorer.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			alert('Explorer 5');
		}catch (E){
			//Else we must be using a non-internet explorer browser.
			xmlhttp = false;
		}
	}
	
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		//alert('Firefox');
		xmlhttp = new XMLHttpRequest();
	}
	
	return xmlhttp;
}//cierra getxmlhttp

var procajax;
function processajax(serverPage, objID) {
	xmlhttp = getxmlhttp();
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			objID.innerHTML = "";
			objID.innerHTML = xmlhttp.responseText;
			procajax = xmlhttp.responseText;
		}else{
			objID.innerHTML = "<img src='images/loaderrojo.gif' />";
			}
	}
xmlhttp.send(null);
}

function showColors(){
	var link = document.getElementById('lnkColores');
	var div = document.getElementById('colores');
	
	if(div.innerHTML.length == 0){
		link.innerHTML = 'Oculta Colores';
		processajax('colores.php', div);	
	}else{
		link.innerHTML = 'Ver Colores';
		div.innerHTML = '';
	}
}

function ToggleFields(val){
	if(!Number(val)==0){
		document.getElementById('tituloCal').style.display='';
		document.getElementById('descripcionCal').style.display='';
	}else{
		document.getElementById('tituloCal').style.display='none';
		document.getElementById('descripcionCal').style.display='none';
	}
}


function ShowFields(value){
	HideShowFields(value.value);
	resetBorder(value);
}

function showButton(id, page){
	var conteo = document.getElementsByName('principal');
	for(var i=0;i<conteo.length;i++){
		document.getElementById('actualiza'+conteo[i].value).innerHTML='';
	}
	page = 'scripts/createButton.php?id='+id+'&page='+page;
	cont = document.getElementById('actualiza'+id);
	processajax(page, cont);
	
}

function changeGrid(idcat, pag){
	//var idcat = cat.value;
	var res = document.getElementById('resultado')
	var rand = new Date().getTime(); 
	if(idcat==0)
		processajax('scripts/gridGeneral.php?rand=' + rand + '&page=' + pag, res);
	if (idcat==1)
		processajax('scripts/gridNoticias.php?rand=' + rand + '&page=' + pag, res);
	if(idcat==2)
		processajax('scripts/gridCarteleras.php?rand=' + rand + '&page=' + pag, res);
	if(idcat==3)
		processajax('scripts/gridExpo.php?rand=' + rand + '&page=' + pag, res);
	if(idcat==4)
		processajax('scripts/gridConvocatorias.php?rand=' + rand + '&page=' + pag, res);
	if(idcat==5)
		processajax('scripts/gridCursos.php?rand=' + rand + '&page=' + pag, res);
}

/****************Ruben**************************/

function updateEstatus(id, pag){
	
	var radio = document.getElementById('radio'+id);
	var di = document.getElementById('update');
	var cont = document.getElementById('actualiza'+id);
	var rand = new Date().getTime(); 
	var page ='scripts/estatusUpdate.php?id=' + radio.value + '&rand=' + rand;
	processajax(page,di);
	
	if (procajax==0){
		alert('No se puede poner Noticia Principal si esta archivada');
	}else{
		alert('Noticia Principal Cambiada');
	}
	
	changeGrid(document.getElementById('cmbCategorias').value, pag);	
}

function ArchivarRegistro(registro){
	
	var req = getxmlhttp();
	//var e =registro.id;
	//e = e.replace("B","H");
	var divResultado = document.getElementById('elimina');
	var rand = new Date().getTime(); 
	valor = document.getElementById(registro);
	var icono = document.getElementById('modif' +  valor.value);
	var estatus = document.getElementById('estatus' +  valor.value);
	req.open("GET", 'scripts/archivarRegistro.php?registro=' + valor.value + '&rand=' + rand);
        req.onreadystatechange=function() {
               if (req.readyState==4 && req.status == 200) {
  				      
					if(req.responseText=='1'){
						icono.innerHTML = '';
						icono.innerHTML = '<a href="javascript:ActivarRegistro(\''+ registro +'\')"><img src="images/activar.png" style="border:none" width="20" height="20" title="Activar Registro" alt="Activar Registro" /></a>';
						estatus.innerHTML = '';
						estatus.innerHTML = 'Archivado';
						alert('Registro Archivado');
					}else{
						alert('Ningun Registro Archivado');
					}
               }
        }
        req.send(null);
}


function ActivarRegistro(registro){
	var req = getxmlhttp();
	var rand = new Date().getTime(); 
	valor = document.getElementById(registro);
	var icono = document.getElementById('modif' +  valor.value);
	var estatus = document.getElementById('estatus' +  valor.value);
	req.open("GET",'scripts/activarRegistro.php?registro=' + valor.value + '&rand=' + rand);
	req.onreadystatechange=function() {
               if (req.readyState==4 && req.status == 200) {
  				      
					if(req.responseText=='1'){
						icono.innerHTML = '';
						icono.innerHTML = '<a href="javascript:ArchivarRegistro(\''+ registro +'\')"><img src="images/archivar.png" style="border:none" width="20" height="20" title="Archivar Registro" alt="Archivar Registro" /></a>';
					   estatus.innerHTML = '';
					   estatus.innerHTML = 'Visible';
						alert('Registro Activado');
						   
					   }else{
						   alert('Ningun Registro Activado');
					   }
					   
               }
        }
        req.send(null);
}

/*****************/
function resetBorder(obj){
	obj.style.border = 'solid 1px #999';
	}
/******************/

function Validation(){
	var errormsg='';
	var categorias = document.getElementById('cmbCategorias');
	var cal = document.getElementById('cmbCal');
	if(categorias.value==0){
		categorias.style.border = "1px solid #c30";
		//alert('No selecciono categoria');
		errormsg = "<li>No se selecciono categoria.</li>";
		//return;
	}
	var titulo = document.getElementById('txtTitulo');
	if(titulo.value==''){
		titulo.style.border = "1px solid #c30";
		errormsg = errormsg + "<li>El titulo no puede estar vacio.</li>";
	}
	
	var descripcion = document.getElementById('txtDescripcion');
	if(descripcion.value==''){
		descripcion.style.border = "1px solid #c30";
		errormsg = errormsg + "<li>La descripcion no puede estar vacia.</li>";
	}
	
	if(categorias.value>2){
		if(categorias.value==3){
			var tipo1 = document.getElementById('cmbTipo1');
			if(tipo1.value==0){
				tipo1.style.border = "1px solid #c30";
				errormsg = errormsg + "<li>Debe seleccionar un tipo.</li>";
			}
		}
		
		if(categorias.value==4){
			var tipo2 = document.getElementById('cmbTipo2');
			if(tipo2.value==0){
				tipo2.style.border = "1px solid #c30";
				errormsg = errormsg + "<li>Debe seleccionar un tipo.</li>";
			}
		}
		
		if(categorias.value==5){
			var tipo3 = document.getElementById('cmbTipo3');
			if(tipo3.value==0){
				tipo3.style.border = "1px solid #c30";
				errormsg = errormsg + "<li>Debe seleccionar Curso o Taller.</li>";
			}
		}
	}
	
	if(cal.value>0){
	var titulocal = document.getElementById('txtTituloCal');
	if(titulocal.value==''){
		titulocal.style.border = "1px solid #c30";
		errormsg = errormsg + "<li>El titulo del calendario no puede estar vacio.</li>";
	}
	
	var descripcioncal = document.getElementById('txtDescripcionCal');
	if(descripcioncal.value==''){
		descripcioncal.style.border = "1px solid #c30";
		errormsg = errormsg + "<li>La descripcion del calendario no puede estar vacio.</li>";
	}
	
	}
	
	if(!errormsg==''){
		//alert(errormsg);
		var diverror = document.getElementById('errormsg');
		diverror.innerHTML = '<ul>'+errormsg+'</ul>';
		return false;
	}
}

function borrarRegistro(cat, id, pag){
	if (confirm("Esta seguro de querer borrar este registro?")) {
		var idcombo = document.getElementById('cmbCategorias');
		var req = getxmlhttp();
		req.open("GET",'scripts/borrarRegistro.php?cat='+cat+'&id='+id);
		req.onreadystatechange=function() {
			if (req.readyState==4 && req.status == 200) {
				changeGrid(idcombo.value, pag);					   
			}//cierra if
		}//cierra function
			req.send(null);
	}
}

function AgregaCampos(control){
	var id = control.value;
	var obj;
	var scriptajax = "scripts/muestracampo.php?id="+id;
	obj = document.getElementById("campoadicional");
	
	processajax(scriptajax, obj);
	}
	
	
	function llenacampo(tipo,categoria,vi){
	var obj;
	//alert(tipo);
	//alert(categoria);
	var scriptajax = "scripts/llenacampo.php?id="+categoria+'&tip='+tipo+'&vi='+vi;
	//alert(scriptajax);
	obj = document.getElementById("campoadicional");
	
	processajax(scriptajax, obj);
	}

function changePageNot(pag){
	var res = document.getElementById('noticiainterior');
	var rand = new Date().getTime(); 
	processajax('admin/scripts/changeNot.php?rand=' + rand + '&page=' + pag, res);
	}