// JavaScript Document
function getxmlhttp (){

	//Create a boolean variable to check for a valid Microsoft active x instance.
	var xmlhttp = false;

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


function processAjaxPost(serverPage, str, obj){
	
	xmlhttp = getxmlhttp();

	xmlhttp.open("POST", serverPage, true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = "";
				obj.innerHTML = xmlhttp.responseText;
			}//cierra if
		}// cierra function de onreadystatechagne
	
	xmlhttp.send(str);
	
}//cierra processAjaxPost

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

//funcion de ajax para post de formas
function processAjaxForm(serverPage, str, obj){
	
	xmlhttp = getxmlhttp();
	
	xmlhttp.open("POST", serverPage, true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				
					obj.innerHTML = xmlhttp.responseText;		
				
				
			}else{
					obj.innerHTML = "";
					obj.innerHTML = "<img src='images/loading.gif' />";
				}
		}// cierra function de onreadystatechagne
	
	xmlhttp.send(str);
	
}//cierra processAjaxForm
		
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function UpdateEncuesta1(idencuesta, enc){	
	var i = 0;
	for( i = 0; i < document.form1.rdenc1.length; i++ ){
		if( document.form1.rdenc1[i].checked == true ){
			val = document.form1.rdenc1[i].value;
			break; 
		}
	}
	//alert(val);
	var url = 'library/updateEncuesta.php?idresp=' + val + '&idenc=' + idencuesta;
	var obj = "resp1";
	processAjaxGet(url, obj);
	document.getElementById("btnenc1").innerHTML = '';
	createCookie('Enc'+idencuesta, 'Enc'+idencuesta, 30);
	}

function UpdateEncuesta2(idencuesta, enc){	
	var i = 0;
	for( i = 0; i < document.form2.rdenc2.length; i++ ){
		if( document.form2.rdenc2[i].checked == true ){
			val = document.form2.rdenc2[i].value;
			break; 
		}
	}
	//alert(val);
	var url = 'library/updateEncuesta.php?idresp=' + val + '&idenc=' + idencuesta;
	var obj = "resp2";
	processAjaxGet(url, obj);
	document.getElementById("btnenc2").innerHTML = '';
	createCookie('Enc'+idencuesta, 'Enc'+idencuesta, 30);
	}
	
function SubmitSearch(){
 document.frmBusqueda.submit();
 }


function UpdateTitulo(titulo){
	var url = '../library/updateTitulo.php?action=change&titulo=' + titulo;
	var obj = 'titulo'+titulo;
	processAjaxGet(url, obj);
	
	var obj = document.getElementById('mod'+titulo);
	obj.innerHTML = '<a href="javascript:SetTitulo('+titulo+')">Actualizar</a>';
}

function SetTitulo(titulo){
	var val = document.getElementById('txtTitulo'+titulo).value;
	var url = '../library/updateTitulo.php?action=update&titulo=' + titulo+'&val='+val;

	var obj = 'titulo'+titulo;
	processAjaxGet(url, obj);
	
	var obj = document.getElementById('mod'+titulo);
	obj.innerHTML = '<a href="javascript:UpdateTitulo('+titulo+')">Modificar</a>';
}


