function get_xmlhttp(){
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(E){
			xmlhttp = false;
		}
	}
	
	if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp
}

function enviar(xmlhttp,metodo,url,async,callback,vars){
	xmlhttp.open(metodo, url,async);
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			callback(xmlhttp)
		}
	}
	if(metodo == 'POST')
	{
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp.send(vars);
	}else{
		xmlhttp.send(null);
	}
}

//funciones que me sacan en pantalla el select con cada una de las imágenes del servidor
function nuevasActividades(valor) {
	var uid = new Date();
	
	xmlhttp=get_xmlhttp()	
	enviar(xmlhttp,"GET","includes/actividadesAJAX.php?page="+encodeURIComponent(valor)+"&uid="+uid,true,verActividades,'');
}
function verActividades(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	document.getElementById("contAct").innerHTML = xmlhttp.responseText;
}



//funcion para votar
function voto() {
	var valor = document.votar.voto.options[document.votar.voto.selectedIndex].value;
	var id = document.votar.id.value;
	var uid = new Date();	
	
	xmlhttp=get_xmlhttp()	
	enviar(xmlhttp,"GET","includes/votarAJAX.php?puntos="+encodeURIComponent(valor)+"&uid="+uid,true,updateVoto,'');
}
function updateVoto(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	//document.getElementById("contAct").innerHTML = xmlhttp.responseText;
}

/**
  *
  *
  *		ÁREA PRIVADA
  *
  *
  */

var apUserDest;
function apUsuario(valor,id,dest){
	var uid = new Date();
	apUserDest = dest;
	
	xmlhttp=get_xmlhttp()	
	enviar(xmlhttp,"GET","includes/APusuarios.php?accn="+encodeURIComponent(valor)+"&id="+ id +"&uid="+uid,true,apGetUsuario,'');
}
function apGetUsuario(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	//alert('open' + apUserDest);
	document.getElementById(apUserDest).innerHTML = xmlhttp.responseText;
}
var apAsignarDest;
function asignarAcceso(valor,dest){
	var usuario = document.datosAcceso.usuario.value;
	var contrasenya = document.datosAcceso.contrasenya.value;
	var id = document.datosAcceso.id.value;
	var saccn = document.datosAcceso.subAccn.value;
	var empresa = document.datosAcceso.empresa.value;
	var uid = new Date();
	apAsignarDest = dest;
	
	xmlhttp=get_xmlhttp()	
	enviar(xmlhttp,"POST","includes/APusuarios.php",true,apGetAcceso,'accn=' + encodeURIComponent(valor) + '&u=' + usuario + '&id='+ id +'&c=' + contrasenya + '&saccn=' + saccn + '&empresa=' + empresa + '&uid=' + uid);
}
function apGetAcceso(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	//alert('open' + apUserDest);
	apUsuario('','','listadoUsuarios');
	document.getElementById(apAsignarDest).innerHTML = xmlhttp.responseText;
}

var apGroupDest;
function apGrupo(valor,id,dest){
	var uid = new Date();
	apGroupDest = dest;
	
	xmlhttp=get_xmlhttp()	
	enviar(xmlhttp,"GET","includes/APgrupos.php?accn="+encodeURIComponent(valor)+"&id="+ id +"&uid="+uid,true,apGetGrupo,'');
}
function apGetGrupo(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	
	document.getElementById(apGroupDest).innerHTML = xmlhttp.responseText;
}


var apAddGroupDest;
function apAddGrupo(valor,id,dest){
	var uid = new Date();
	apAddGroupDest = dest;
	
	xmlhttp=get_xmlhttp()	
	enviar(xmlhttp,"GET","includes/APgrupos.php?accn="+encodeURIComponent(valor)+"&id="+ id +"&uid="+uid,true,apGetAddGrupo,'');
}
function apGetAddGrupo(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	
	document.getElementById(apAddGroupDest).innerHTML = xmlhttp.responseText;
}

var apAsignarDest;
function formAddGrupo(valor,dest){
	var gNombre = document.formAddGroup.gNombre.value;
	var gContrato = document.formAddGroup.gContrato.value;
	var gCl = document.formAddGroup.gCl.options[document.formAddGroup.gCl.selectedIndex].value;
	var gCm = document.formAddGroup.gCm.options[document.formAddGroup.gCm.selectedIndex].value;
	var gCn = document.formAddGroup.gCn.options[document.formAddGroup.gCn.selectedIndex].value;
	var ucl = document.formAddGroup.u_cliente.value;
	var pcl = document.formAddGroup.p_cliente.value;
	var ucm = document.formAddGroup.u_comercial.value;
	var pcm = document.formAddGroup.p_comercial.value;
	var ucn = document.formAddGroup.u_conferenciante.value;
	var pcn = document.formAddGroup.p_conferenciante.value;
	
	var uid = new Date();
	apAsignarDest = dest;
	
	xmlhttp=get_xmlhttp()	
	enviar(xmlhttp,"POST","includes/APgrupos.php",true,getFormAddGrupo,'accn=' + encodeURIComponent(valor) + '&ucl=' + encodeURIComponent(ucl) + '&pcl=' + encodeURIComponent(pcl) + '&ucm=' + encodeURIComponent(ucm) + '&pcm=' + encodeURIComponent(pcm) + '&ucn=' + encodeURIComponent(ucn) + '&pcn=' + encodeURIComponent(pcn) + '&gNombre=' + encodeURIComponent(gNombre) +  '&gContrato=' + encodeURIComponent(gContrato) +  '&gCl=' + encodeURIComponent(gCl) +  '&gCm=' + encodeURIComponent(gCm) +  '&gCn=' + encodeURIComponent(gCn) + '&uid=' + encodeURIComponent(uid));
}
function getFormAddGrupo(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	apGrupo('','','listadoGrupos');
	document.getElementById(apAsignarDest).innerHTML = xmlhttp.responseText;
}


var apFichaDest;
function apFicha(valor,id,dest){
	var uid = new Date();
	apFichaDest = dest;
	
	xmlhttp=get_xmlhttp()	
	enviar(xmlhttp,"GET","includes/APfichas.php?accn="+encodeURIComponent(valor)+"&id="+ id +"&uid="+uid,true,apGetFicha,'');
}
function apGetFicha(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	
	document.getElementById(apFichaDest).innerHTML = xmlhttp.responseText;
}

var apListDest;
function apListFicha(valor,id,dest){
	var uid = new Date();
	apListDest = dest;
	
	xmlhttp=get_xmlhttp()	
	enviar(xmlhttp,"GET","includes/APfichas.php?accn="+encodeURIComponent(valor)+"&id="+ id +"&uid="+uid,true,apGetListFicha,'');
}
function apGetListFicha(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	
	document.getElementById(apListDest).innerHTML = xmlhttp.responseText;
}


var apAsignarDest;
function formUpdateFicha(valor,dest){
	var post = '';
	
	for(var i=0; i < document.formFicha['datos[]'].length; i++)
	{
		var v = document.formFicha['datos[]'][i].value;
		if(post != '') post += '&';
		post += 'd' + i + '=' + encodeURIComponent(v);
	}
	
	for(var i=0; i < document.formFicha['acto[]'].length; i++)
	{
		var v = document.formFicha['acto[]'][i].value;
		if(post != '') post += '&';
		post += 'a' + i + '=' + encodeURIComponent(v);
	}
	
	for(var i=0; i < document.formFicha['cliente[]'].length; i++)
	{
		var v = document.formFicha['cliente[]'][i].value;
		if(post != '') post += '&';
		post += 'c' + i + '=' + encodeURIComponent(v);
	}
	
	for(var i=0; i < document.formFicha['viaje[]'].length; i++)
	{
		var v = document.formFicha['viaje[]'][i].value;
		if(post != '') post += '&';
		post += 'v' + i + '=' + encodeURIComponent(v);
	}
	
	for(var i=0; i < document.formFicha['objetivos[]'].length; i++)
	{
		var v = document.formFicha['objetivos[]'][i].value;
		if(post != '') post += '&';
		post += 'o' + i + '=' + encodeURIComponent(v);
	}
	
	for(var i=0; i < document.formFicha['economica[]'].length; i++)
	{
		var v = document.formFicha['economica[]'][i].value;
		if(post != '') post += '&';
		post += 'e' + i + '=' + encodeURIComponent(v);
	}
	
	id = document.formFicha.fid.value;
	accn = 'actualizar';
	
	post += '&id=' + id + '&accn=' + accn;
	
	var uid = new Date();
	apAsignarDest = dest;
	
	xmlhttp=get_xmlhttp()	
	//alert(post);
	enviar(xmlhttp,"POST","includes/APfichas.php",true,getFormUpdateFicha,post);
}
function getFormUpdateFicha(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	document.getElementById(apAsignarDest).innerHTML = xmlhttp.responseText;
}


var apAsignarDest;
function formMensaje(dest){
	var post = '';
	
	id = document.formMensajes.usuario_id.value;
	grupo = document.formMensajes.usuario_grupo.value;
	ficha = document.formMensajes.id_ficha.value;
	asunto = document.formMensajes.asunto.value;
	mensaje = document.formMensajes.mensaje.value;
	
	var uid = new Date();
	
	post = 'id=' + id + '&grupo=' + grupo + '&ficha=' + ficha + '&asunto=' + asunto + '&mensaje=' + mensaje + '&uid=' + uid;
	
	
	apAsignarDest = dest;
	
	xmlhttp=get_xmlhttp();	
	
	enviar(xmlhttp,"POST","includes/APmensajes.php",true,getformMensaje,post);
}
function getformMensaje(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	document.getElementById(apAsignarDest).innerHTML = xmlhttp.responseText;
}

var bfAsignarDest;
function bloquearFicha(dest){
	var post = '';
	
	var inpt = document.bloqueos.length;
	var obj = document.bloqueos['bloquear[]'];
	
	for(var i = 0; i < inpt; i++)
	{	
		if(post != '') post += '&';
		if(obj[i].checked){			
			post += 'cbc[' + i + ']=' + obj[i].value;
		}else{
			post += 'cbn[' + i + ']=' + obj[i].value;			
		}
	}
	//alert(post);
	
	var uid = new Date();
	
	bfAsignarDest = dest;
	
	xmlhttp=get_xmlhttp();	
	
	enviar(xmlhttp,"POST","includes/APbloquearFicha.php",true,getbloquearFicha,post);
}
function getbloquearFicha(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	document.getElementById(bfAsignarDest).innerHTML = xmlhttp.responseText;
	apListFicha('ver','','apContenido');
}


function votar() {
	
	var uid = new Date();
	var id_voto = getCheckedValue(document.formVotacion.voto);
	var num_voto = document.formVotacion.num_voto.value;
	if(!id_voto)
	{
		alert('Seleccione el item que desea votar');
		return false;
	}
	
	xmlhttp=get_xmlhttp()
	enviar(xmlhttp,"GET","includes/fnc.votacion.php?id_voto=" + encodeURIComponent(id_voto) + "&num_voto=" + encodeURIComponent(num_voto) + "&uid=" + uid,true,getVotacion,'');
	
}
function getVotacion(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	//alert(xmlhttp.responseText);
	if(xmlhttp.responseText == 'ok')
	{
		alert('Su voto ha sido registrado.\nGracias por participar.');	
	}else{
		alert('Usted ya ha participado en esta votación.\nSu voto no será registrado.');
	}
	setCheckedFalse(document.formVotacion.voto);
	
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
			
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	
	return "";
}
function setCheckedFalse(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
				
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			radioObj[i].checked = false;
		}
	}
	
}