function formulario(){
	var callingURL = document.location.href;
	var cod=0;
	var id=0;
	var ema=0;
	//obtener carpeta
	var cgiString2 = callingURL.substring(0,callingURL.indexOf('?')); 
	var DELIMETER = '/'; 
	var carp;
	var arrayParams2=cgiString2.split(DELIMETER);
	carp=arrayParams2[arrayParams2.length-2];
	//obtener cc
	var DELIMETER = '&'; 
	var cc;
	var cgiString = callingURL.substring(callingURL.indexOf('?')+1,callingURL.length); 
	if (cgiString.indexOf('#')!=-1){ 
		cgiString=cgiString.slice(0,cgiString2.indexOf('#')); 
	} 
	var arrayParams=cgiString.split(DELIMETER);

	for (var i=0;i<arrayParams.length;i++){ 
		var arrayBuscando=arrayParams[i].split("=");
		if(arrayBuscando[0]=='cc'){
			cc=arrayBuscando[1];
		}
		if(arrayBuscando[0]=='id'){
			id=arrayBuscando[1];
		}
		if(arrayBuscando[0]=='cod'){
			cod=arrayBuscando[1];
		}
		if(arrayBuscando[0]=='ema'){
			ema=arrayBuscando[1];
		}
	} 
	mostrarFormulario(carp,cc,id,cod,ema);
}
function mostrarFormulario(carp,cc,id,cod,ema){
	var peticion = false;
	if (window.XMLHttpRequest) {
		peticion = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		peticion = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var url = "../disenyar_form.php";
	if(peticion) {
		peticion.open("POST", url);
		peticion.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		peticion.onreadystatechange = function()
		{
			if (peticion.readyState == 4 && peticion.status == 200) {
				document.getElementById("cuerpo").innerHTML=peticion.responseText;
				/*
				codigo=document.getElementById("cod").value;
				if(codigo!=0){
					cargarCuentas(codigo,'0',codigo);
				}
				*/	
				delete peticion;
			}
		}
		peticion.send("mostrar=1&carp="+carp+"&cc="+cc+"&id="+id+"&cod="+cod+"&ema="+ema);
	}
}
function cargarCuentas(cod,tabla,elem){
	var peticion = false;
	if (window.XMLHttpRequest) {
	peticion = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
	peticion = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var url = "../disenyar_form.php";
	if(peticion) {
		peticion.open("POST", url);

		peticion.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		peticion.onreadystatechange = function()
		{
			if (peticion.readyState == 4 && peticion.status == 200) {
				document.getElementById("desp_cuentas").innerHTML=peticion.responseText;
				delete peticion;
			}
		}
		peticion.send("cargar=2&cod="+cod+"&id="+tabla+"&elem="+elem);
	}
}
function seleccionarCuenta(cod,tabla,elem){
	cuenta=document.getElementById("idCuenta").value;
	var peticion = false;
	if (window.XMLHttpRequest) {
	peticion = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
	peticion = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var url = "../disenyar_form.php";
	if(peticion) {
		peticion.open("POST", url);
		peticion.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		peticion.onreadystatechange = function()
		{
			if (peticion.readyState == 4 && peticion.status == 200) {
				var xmlDocument = peticion.responseXML;
				cuerpo=xmlDocument.getElementsByTagName("campos");
				actualizarFormulario();
				cargarContactos(cod,tabla,elem,cuenta);
				delete peticion;
			}
		}
		peticion.send("seleccionar="+cuenta+"&tipo=cuenta&cod="+cod+"&id="+tabla+"&elem="+elem);
	}
}
function seleccionarContacto(cod,tabla,elem){
	contacto=document.getElementById("idContacto").value;
	var peticion = false;
	if (window.XMLHttpRequest) {
	peticion = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
	peticion = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var url = "../disenyar_form.php";
	if(peticion) {
		peticion.open("POST", url);
		peticion.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		peticion.onreadystatechange = function()
		{
			if (peticion.readyState == 4 && peticion.status == 200) {
				var xmlDocument = peticion.responseXML;
				cuerpo=xmlDocument.getElementsByTagName("campos");
				actualizarFormulario();
				delete peticion;
			}
		}
		peticion.send("seleccionar="+contacto+"&tipo=contacto&cod="+cod+"&id="+tabla+"&elem="+elem);
	}
}

function cargarContactos(cod,tabla,elem,cuenta){
	var peticion = false;
	if (window.XMLHttpRequest) {
	peticion = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
	peticion = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var url = "../disenyar_form.php";
	if(peticion) {
		peticion.open("POST", url);
		peticion.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		peticion.onreadystatechange = function()
		{
			if (peticion.readyState == 4 && peticion.status == 200) {
				document.getElementById("desp_contactos").innerHTML=peticion.responseText;
				delete peticion;
			}
		}
		peticion.send("cargar=1&cod="+cod+"&id="+tabla+"&elem="+elem+"&cuenta="+cuenta);
	}
}
function actualizarFormulario(){
	for (loopIndex=0;loopIndex<cuerpo.length;loopIndex++)
	{
		if(cuerpo[loopIndex].childNodes[1].childNodes[0]){//evitar error cuando alguno de los campos está vacío
	
			document.getElementById(cuerpo[loopIndex].childNodes[0].childNodes[0].data).value=cuerpo[loopIndex].childNodes[1].childNodes[0].data;
		}
		else{
			document.getElementById(cuerpo[loopIndex].childNodes[0].childNodes[0].data).value="";
		}	
	}	
}
function validate(cadObligatorios){
	error=false;
	document.getElementById("mensajeError").style.visibility='hidden';
	/*
	if("<?php echo $campCue;?>"==true){
		validate_idCuenta();
	}
	else if("<?php echo $campCont;?>"==true){
		validate_idCuenta();
	}
	*/
	if(cadObligatorios!=""){//comprobar si realmente existen campos obligatorios
		vecObligatorios=cadObligatorios.split(",");
		for(cont=0;cont<vecObligatorios.length;cont++){
			if(document.getElementById(vecObligatorios[cont]).value==""){
				document.getElementById(vecObligatorios[cont]).className="campoTextoError";
				error=true;
			}
			else{
				document.getElementById(vecObligatorios[cont]).className="campoTexto";
			}
		}
	}	
	if(!error){
		//enviarFormulario
		f1.action = "../disenyar_form.php?guardar=1";
		f1.submit();
		document.getElementById("fila_botones").innerHTML = 'Sus respuestas al formulario se han registrado, Gracias.';
	}
	else{
		document.getElementById("mensajeError").style.visibility='visible';
	}
}
function enviarFormulario(){
	miForm=document.getElementById("f1");
	error=false;
	var peticion = false;
	var url = "../disenyar_form.php";
	if (window.XMLHttpRequest) {
		peticion = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		peticion = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(!error){//sólo si los campos son cumplimentados
		var longitudFormulario = miForm.elements.length;
		var cadenaFormulario = "guardar=1&";//la inicializo con el id del parrafo
		var sepCampos;
		sepCampos = "";
		for (var i=0; i <= miForm.elements.length-1;i++) {
			if(miForm.elements[i].type=="radio" || miForm.elements[i].type=="checkbox"){
				if(miForm.elements[i].checked==1){
					cadenaFormulario += sepCampos+miForm.elements[i].name+'='+encodeURI(miForm.elements[i].value);
				}
			}
			else if(miForm.elements[i].multiple==true){//en el caso de los múltiples, hay que devolver todas las respuestas  seleccionadas
				//cadenaFormulario += sepCampos+miForm.elements[i].name+'='+encodeURI(miForm.elements[i].value);
				vector=new Array();
				cont2=0;
				for(cont=0;cont<miForm.elements[i].options.length;cont++){
					if(miForm.elements[i].options[cont].selected==true){
						vector[cont2]=miForm.elements[i].options[cont].value;
						cont2++;
						//cadenaFormulario += sepCampos+miForm.elements[i].id+'='+encodeURI(miForm.elements[i].options[cont].value);
					}	
				}
				cadenaFormulario += sepCampos+miForm.elements[i].id+'='+encodeURI(vector);
			}
			else{
				cadenaFormulario += sepCampos+miForm.elements[i].name+'='+encodeURI(miForm.elements[i].value);
			}	
			sepCampos="&";
		}
		peticion.open("POST", url, true);
		peticion.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1;');
		peticion.onreadystatechange = function () {
			if (peticion.readyState == 4) {
				document.getElementById("fila_botones").innerHTML=peticion.responseText;
			}
		}
		peticion.send(cadenaFormulario);
	}	
}
