window.onload=function(){
	//para el encabezado
	MM_preloadimages1('img/boton2.2.jpg','img/boton1.1.jpg');
	//Para el aviso de primera carga de contenido
	document.getElementById("cargando").style.display="none";
	//botones('');
	document.getElementById("contenido").style.display="block";
	if(getCookie("user")){
		validarusuario_inicio();
		document.getElementById("cuerpo").innerHTML=document.getElementById("cuerpo_miprefil").innerHTML;
		mostrar_perfil();
	}else{
		salir();
	}
}

function ocultando(i){
	for(y=0; y<=4; y++)
	{
		if(y!=i){
			document.getElementById("formulario"+y).style.display="none";
			document.getElementById("mas"+y)['onclick']=new Function("formulario"+y+"('mostrar')");
			document.getElementById("img"+y).src="img/plus.gif";
		}
	}
}

/***************************************muestra de datos generales********************************/
function mostrar_perfil(){
	var conexion = conexionajax();
	conexion.open('POST', '..'+document.getElementById("carpeta_raiz").innerHTML+'includes/perfil.php');
	conexion.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
	conexion.onreadystatechange = function() {
		if (conexion.readyState==4) {	
			var texto = conexion.responseXML;
			document.getElementById("txt_login").innerHTML=texto.getElementsByTagName("login")[0].firstChild.nodeValue;
			document.getElementById("txt_email").innerHTML=texto.getElementsByTagName("email")[0].firstChild.nodeValue;
		}
	}
	data='';
	data+="acciones=mostrar_perfil";
	data+="&idusuario="+getCookie("user");
	conexion.send(data);
}

/********************************************cambio de contraseña****************************************/

function formulario0(accion){
	if(accion=='mostrar'){
		document.getElementById("formulario0").style.display="block";
		document.getElementById("mas0")['onclick']=new Function("formulario0('ocultar')");
		document.getElementById("img0").src="img/minus.gif";
		ocultando(0);
	}
	
	if(accion=='ocultar'){
		document.getElementById("formulario0").style.display="none";
		document.getElementById("mas0")['onclick']=new Function("formulario0('mostrar')");
		document.getElementById("img0").src="img/plus.gif";
		document.getElementById("txt_npassword").value="";
		document.getElementById("txt_npassword2").value="";
	}
}

function cambiar_contrasena(pass1,pass2){
	if(pass1!="" && pass2!=""){
		if(pass1 == pass2){
			var conexion = conexionajax();
			conexion.open('POST', '..'+document.getElementById("carpeta_raiz").innerHTML+'includes/perfil.php');
			conexion.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
			conexion.onreadystatechange = function() {
				if (conexion.readyState==4) {	
					var texto = conexion.responseXML;
					if(texto.getElementsByTagName("data")[0].firstChild.nodeValue=='1'){
						formulario0('ocultar');
						alert("Su clave ha sido modificada exitosamente");
						document.getElementById("txt_npassword").value="";
						document.getElementById("txt_npassword2").value="";
					}else{
						alert("Se ha registrado un error al procesar su petición\nPor favor intente de nuevo");
					}
				}
			}
			data='';
			data+="acciones=cambio_clave";
			data+="&idusuario="+getCookie("user");
			data+="&pass1="+pass1;
			conexion.send(data);
		}else{
			alert("Su clave no coincide con el campo de comprobación");
			document.getElementById("txt_npassword").value="";
			document.getElementById("txt_npassword2").value="";
		}
	}else{
		alert("Debe llenar ámbos campos para cambiar su clave");
	}
}

/********************************************cambio de pregunta****************************************/

function formulario1(accion){
	if(accion=='mostrar'){
	var conexion = conexionajax();
		conexion.open('POST', '..'+document.getElementById("carpeta_raiz").innerHTML+'includes/perfil.php');
		conexion.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		conexion.onreadystatechange = function() {
			if (conexion.readyState==4) {	
				var texto = conexion.responseXML;
				document.getElementById("formulario1").style.display="block";
				document.getElementById("mas1")['onclick']=new Function("formulario1('ocultar')");
				document.getElementById("img1").src="img/minus.gif";
				document.getElementById("txt_preguntaclave").value=texto.getElementsByTagName("pregunta")[0].firstChild.nodeValue;
				document.getElementById("txt_respuestaclave").value=texto.getElementsByTagName("respuesta")[0].firstChild.nodeValue;
				ocultando(1);
			}
		}
		data='';
		data+="acciones=mostrar_preguntaclave";
		data+="&idusuario="+getCookie("user");
		conexion.send(data);
	}
	
	if(accion=='ocultar'){
		document.getElementById("formulario1").style.display="none";
		document.getElementById("mas1")['onclick']=new Function("formulario1('mostrar')");
		document.getElementById("img1").src="img/plus.gif";
		document.getElementById("txt_preguntaclave").value="";
		document.getElementById("txt_respuestaclave").value="";
	}
}

function cambiar_pregunta(pregunta,respuesta){
	if(pregunta!="" && respuesta!=""){
		var conexion = conexionajax();
		conexion.open('POST', '..'+document.getElementById("carpeta_raiz").innerHTML+'includes/perfil.php');
		conexion.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		conexion.onreadystatechange = function() {
			if (conexion.readyState==4) {	
				var texto = conexion.responseXML;
				if(texto.getElementsByTagName("data")[0].firstChild.nodeValue=='1'){
					formulario1('ocultar');
					alert("Sus datos de recuperación han sido modificados exitosamente");
				}else{
					alert("Se ha registrado un error al procesar su petición\nPor favor intente de nuevo");
				}
			}
		}
		data='';
		data+="acciones=cambiar_pregunta";
		data+="&idusuario="+getCookie("user");
		data+="&pregunta="+pregunta;
		data+="&respuesta="+respuesta;
		conexion.send(data);
	}else{
		alert("Debe llenar ámbos campos para cambiar sus datos de recuperación de clave");
	}
}

/********************************************cambio de datos personales****************************************/

function formulario2(accion){
	if(accion=='mostrar'){
	var conexion = conexionajax();
		conexion.open('POST', '..'+document.getElementById("carpeta_raiz").innerHTML+'includes/perfil.php');
		conexion.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		conexion.onreadystatechange = function() {
			if (conexion.readyState==4) {	
				var texto = conexion.responseXML;
				document.getElementById("formulario2").style.display="block";
				document.getElementById("mas2")['onclick']=new Function("formulario2('ocultar')");
				document.getElementById("img2").src="img/minus.gif";
				document.getElementById("txt_nombre").value=texto.getElementsByTagName("nombre")[0].firstChild.nodeValue;
				document.getElementById("txt_apellido").value=texto.getElementsByTagName("apellido")[0].firstChild.nodeValue;
				document.getElementById("txt_ciudad").value=texto.getElementsByTagName("ciudad")[0].firstChild.nodeValue;
				document.getElementById("txt_direccion").value=texto.getElementsByTagName("direecion")[0].firstChild.nodeValue;
				if(texto.getElementsByTagName("boletin")[0].firstChild.nodeValue=='S'){
					document.getElementById("txt_boletin").checked=true;
				}else{
					document.getElementById("txt_boletin").checked=false;
				}
				listar_pais('paises1');
				combo1=document.getElementById("paises1");
				combo1.style.width="135px";
				option1=document.createElement("option");
				option1.setAttribute("selected","true");
				option1.setAttribute("value",texto.getElementsByTagName("idpais")[0].firstChild.nodeValue);
				option1.innerHTML=texto.getElementsByTagName("pais")[0].firstChild.nodeValue;
				combo1.appendChild(option1);
				listar_estado('estados1',combo1);
				setTimeout("mostrar_estado('"+texto.getElementsByTagName("idestado")[0].firstChild.nodeValue+"','"+texto.getElementsByTagName("estado")[0].firstChild.nodeValue+"')",100);
				ocultando(2);
			}
		}
		data='';
		data+="acciones=mostrar_datospersonales";
		data+="&idusuario="+getCookie("user");
		conexion.send(data);
	}
	
	if(accion=='ocultar'){
		document.getElementById("formulario2").style.display="none";
		document.getElementById("mas2")['onclick']=new Function("formulario2('mostrar')");
		document.getElementById("img2").src="img/plus.gif";
	}
}

function mostrar_estado(idestado,nombre){
	combo2=document.getElementById("estados1_select");
	option2=document.createElement("option");
	option2.setAttribute("selected","true");
	option2.setAttribute("value",idestado);
	option2.innerHTML=nombre;
	combo2.appendChild(option2);
}

function cambiar_datospresonales(){
	if(
	document.getElementById("txt_nombre").value!="" && 
	document.getElementById("txt_apellido").value!="" && 
	document.getElementById("paises1").value!="0" && 
	document.getElementById("estados1_select").value!="0" && 
	document.getElementById("txt_ciudad").value!="" && 
	document.getElementById("txt_direccion").value!=""){
		var conexion = conexionajax();
		conexion.open('POST', '..'+document.getElementById("carpeta_raiz").innerHTML+'includes/perfil.php');
		conexion.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		conexion.onreadystatechange = function() {
			if (conexion.readyState==4) {	
				var texto = conexion.responseXML;
				if(texto.getElementsByTagName("data")[0].firstChild.nodeValue=='1'){
					formulario2('ocultar');
					alert("Sus datos han sido actualizados exitosamente");
				}else{
					alert("Se ha registrado un error al procesar su petición\nPor favor intente de nuevo");
				}
			}
		}
		boletin='';
		if(document.getElementById("txt_boletin").checked==true){
			boletin='S';
		}else{
			boletin='N';
		}
		data='';
		data+="acciones=cambiar_datospresonales";
		data+="&idusuario="+getCookie("user");
		data+="&txt_nombre="+document.getElementById("txt_nombre").value;
		data+="&txt_apellido="+document.getElementById("txt_apellido").value;
		data+="&paises="+document.getElementById("paises1").value;
		data+="&estados="+document.getElementById("estados1_select").value;
		data+="&txt_ciudad="+document.getElementById("txt_ciudad").value;
		data+="&txt_direccion="+document.getElementById("txt_direccion").value;
		data+="&boletin="+boletin;
		conexion.send(data);
	}else{
		alert("Debe llenar todos los campos obligatorios para actualizar sus datos");
	}
}


/********************************************datos de facturación****************************************/

function formulario3(accion){
	if(accion=='mostrar'){
	var conexion = conexionajax();
		conexion.open('POST', '..'+document.getElementById("carpeta_raiz").innerHTML+'includes/perfil.php');
		conexion.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		conexion.onreadystatechange = function() {
			if (conexion.readyState==4) {	
				var texto = conexion.responseXML;
				document.getElementById("formulario3").style.display="block";
				document.getElementById("mas3")['onclick']=new Function("formulario3('ocultar')");
				document.getElementById("img3").src="img/minus.gif";
				if(texto.getElementsByTagName("nombre")[0].firstChild.nodeValue!=""){
					document.getElementById("txt_facnombre").value=texto.getElementsByTagName("nombre")[0].firstChild.nodeValue;
					document.getElementById("txt_cafapellido").value=texto.getElementsByTagName("apellido")[0].firstChild.nodeValue;
					document.getElementById("txt_numdoc").value=texto.getElementsByTagName("numero_documento")[0].firstChild.nodeValue;
					document.getElementById("txt_dirfiscal").value=texto.getElementsByTagName("direecion_fiscal")[0].firstChild.nodeValue;
					combo1=document.getElementById("lst_documento");
					option1=document.createElement("option");
					option1.setAttribute("selected","true");
					option1.setAttribute("value",texto.getElementsByTagName("tipo_documento")[0].firstChild.nodeValue);
					option1.innerHTML=texto.getElementsByTagName("documento")[0].firstChild.nodeValue;
					combo1.appendChild(option1);
					setTimeout("textCounter(document.getElementById('txt_dirfiscal').value,document.getElementById('result_dirfis'),500)",100);
					ocultando(3);
				}
			}
		}
		data='';
		data+="acciones=mostrar_datosfacturacion";
		data+="&idusuario="+getCookie("user");
		conexion.send(data);
	}
	
	if(accion=='ocultar'){
		document.getElementById("formulario3").style.display="none";
		document.getElementById("mas3")['onclick']=new Function("formulario3('mostrar')");
		document.getElementById("img3").src="img/plus.gif";
	}
}

function cambiar_datosfacturacion(){
	if(
		document.getElementById("txt_facnombre").value!="" && 
		document.getElementById("txt_cafapellido").value!="" && 
		document.getElementById("lst_documento").value!="" && 
		document.getElementById("txt_numdoc").value!="" && 
		document.getElementById("txt_dirfiscal").value!=""
	){
		var conexion = conexionajax();
		conexion.open('POST', '..'+document.getElementById("carpeta_raiz").innerHTML+'includes/perfil.php');
		conexion.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		conexion.onreadystatechange = function() {
			if (conexion.readyState==4) {	
				var texto = conexion.responseXML;
				if(texto.getElementsByTagName("data")[0].firstChild.nodeValue=='1'){
					formulario3('ocultar');
					alert("Sus datos de facturación han sido modificados exitosamente");
				}else{
					alert("Se ha registrado un error al procesar su petición\nPor favor intente de nuevo");
				}
			}
		}
		data='';
		data+="acciones=cambiar_facdatos";
		data+="&idusuario="+getCookie("user");
		data+="&txt_facnombre="+document.getElementById("txt_facnombre").value;
		data+="&txt_cafapellido="+document.getElementById("txt_cafapellido").value;
		data+="&lst_documento="+document.getElementById("lst_documento").value;
		data+="&txt_numdoc="+document.getElementById("txt_numdoc").value;
		data+="&txt_dirfiscal="+document.getElementById("txt_dirfiscal").value;
		conexion.send(data);
	}else{
		alert("Debe llenar ámbos campos para cambiar sus datos de recuperación de clave");
	}
}
/*****************************Funciones de lista de teléfonos - Perfil***********************************/

function formulario4(accion){
	if(accion=='mostrar'){
		document.getElementById("formulario4").style.display="block";
		document.getElementById("mas4")['onclick']=new Function("formulario4('ocultar')");
		document.getElementById("img4").src="img/minus.gif";
		listar_telefonos('lista_telefonos',getCookie("user"));
		ocultando(4);
	}
	
	if(accion=='ocultar'){
		document.getElementById("formulario4").style.display="none";
		document.getElementById("mas4")['onclick']=new Function("formulario4('mostrar')");
		document.getElementById("img4").src="img/plus.gif";
	}
}


function listar_telefonos(div,idusuario){
	var conexion = conexionajax();
	conexion.open('POST', '..'+document.getElementById("carpeta_raiz").innerHTML+'includes/principal.php');
	conexion.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
	conexion.onreadystatechange = function() {
		if (conexion.readyState==4) {
			var texto = conexion.responseText;
			document.getElementById(div).innerHTML="";
			document.getElementById(div).innerHTML+=texto;
		}else{
			document.getElementById(div).innerHTML="Cargando listado de tel&eactuefonos";
		}
	}
	//generando y creando cookie
	conexion.send("acciones=listar_telefonos&idusuario="+idusuario);
}


function agregar_telefono(idusuario){
	tipo = document.getElementById("lst_telefonos").options[document.getElementById("lst_telefonos").selectedIndex].value;
	if(document.getElementById("txt_codigo").value !="" &&
	document.getElementById("txt_numero").value !=""&& 
	tipo !=""){
		var conexion = conexionajax();
		conexion.open('POST', '..'+document.getElementById("carpeta_raiz").innerHTML+'includes/principal.php');
		conexion.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		conexion.onreadystatechange = function() {
			if (conexion.readyState==4) {
				var texto = conexion.responseXML;
				if(texto.getElementsByTagName("resultado")[0].firstChild.nodeValue == '1'){
					//mensaje de registro satisfactorio
					alert("Nuevo teléfono agregado satisfactoriamente");
					document.getElementById("txt_codigo").value =="";
					document.getElementById("txt_numero").value ==""; 
					listar_telefonos("lista_telefonos",idusuario);
				}else if(texto.getElementsByTagName("resultado")[0].firstChild.nodeValue == '0'){
					//mensaje d eerror
					alert("Ha ocurrido un error al procesar su información\nVerifique sus datos e intente de nuevo");
				}
			}
		}
		conexion.send("acciones=agregar_telefono&txt_codigo="+document.getElementById("txt_codigo").value+"&txt_numero="+document.getElementById("txt_numero").value+"&tipo="+tipo+"&idusuario="+idusuario);
	}else{
		alert("Todos los campos para el ingreso de teléfono son obligatorios\n Para continuar ingrese la información faltante");
	}
}

function eliminar_telefono(num,idusuario){
	var conexion = conexionajax();
	var idtelefono='';
	for(var i=0; i< num; i++){
		if(document.getElementById("telf_"+i).checked==true){
			idtelefono+="@"+document.getElementById("telf_"+i).value;		
		}
	}
	conexion.open('POST', '..'+document.getElementById("carpeta_raiz").innerHTML+'includes/principal.php');
	conexion.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
	conexion.onreadystatechange = function() {
		if (conexion.readyState==4) {
			var texto = conexion.responseXML;
			if(texto.getElementsByTagName("resultado")[0].firstChild.nodeValue == '1'){
				//mensaje de registro satisfactorio
				alert("Número de teléfono eliminado satisfactoriamente");
				listar_telefonos("lista_telefonos",idusuario);
			}else if(texto.getElementsByTagName("resultado")[0].firstChild.nodeValue == '0'){
				//mensaje d eerror
				alert("Ha ocurrido un error al procesar su información\nVerifique sus datos e intente de nuevo");
			}
		}
	}
	conexion.send("acciones=eliminar_telefono&idtelefono="+idtelefono);
}