function llamarasincrono(url,destiny){
	var pagina_requerida = false
	if (window.XMLHttpRequest) {// Si es Mozilla, Safari etc
		pagina_requerida = new XMLHttpRequest()
	} else if (window.ActiveXObject){ // pero si es IE
		try {
			pagina_requerida = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e){ // en caso que sea una versión antigua
			try{
				pagina_requerida = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	} else
		return false
	pagina_requerida.onreadystatechange=function(){ // función de respuesta
		if (destiny=='emails') cargar_emailXML(pagina_requerida);
		else if (destiny=='templates') cargar_templateXML(pagina_requerida);
	}
	pagina_requerida.open('GET', url, true) // asignamos los métodos open y send
	pagina_requerida.send(null)
}
function cargar(url, id_contenedor){
	var pagina_requerida = false
	if (window.XMLHttpRequest) {// Si es Mozilla, Safari etc
		pagina_requerida = new XMLHttpRequest()
	} else if (window.ActiveXObject){ // pero si es IE
		try {
			pagina_requerida = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e){ // en caso que sea una versión antigua
			try{
				pagina_requerida = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	} else
		return false
	pagina_requerida.onreadystatechange=function(){ // función de respuesta
		cargarpagina(pagina_requerida, id_contenedor)
	}
	pagina_requerida.open('GET', url, true) // asignamos los métodos open y send
	pagina_requerida.send(null)
}
function cargarpagina(pagina_requerida, id_contenedor){
	if (pagina_requerida.readyState == 4 && (pagina_requerida.status==200 || window.location.href.indexOf("http")==-1)){
		if (id_contenedor=='datos'){
			document.getElementById(id_contenedor).innerHTML=pagina_requerida.responseText;
		}
	}
}
// BEGIN - LEER CORREO
function cargar_correo(id,key){
	document.getElementById("tit").innerHTML="<h2>Cargando...</h2>";
	llamarasincrono("correos_xml.php?p="+id+'&key='+key,'emails');
}
function cargar_emailXML(pagina_requerida){
	if (pagina_requerida.readyState == 4 && (pagina_requerida.status==200 || window.location.href.indexOf("http")==-1)){
		document.getElementById('advise').innerHTML='';
		document.getElementById('del_reg').disabled = '';
		var xml = pagina_requerida.responseXML;
		document.getElementById("tit").innerHTML="<h2>"+xml.getElementsByTagName('email_autor').item(0).firstChild.data.toUpperCase()+"</h2>";
		document.getElementById('email_address').value = xml.getElementsByTagName('email_address').item(0).firstChild.data;
		document.getElementById('email_content').value = xml.getElementsByTagName('email_content').item(0).firstChild.data;
		document.getElementById('email_date').value = xml.getElementsByTagName('email_date').item(0).firstChild.data;
		document.getElementById('email_id').value = xml.getElementsByTagName('email_id').item(0).firstChild.data;
	}else if (pagina_requerida.readyState == 1){
		document.getElementById('advise').innerHTML='Cargando...';
		document.getElementById('email_address').value = '';
		document.getElementById('email_content').value = '';
		document.getElementById('email_date').value = '';
		document.getElementById('email_id').value = '';
	}
}
function clean_email(){
	document.getElementById('tit').innerHTML='<h2>Ingresar nuevo usuario</h2>';
	document.getElementById('email_id').value = '';
	document.getElementById('del_reg').disabled = 'true';
}
function buscar_email(keypres,key){
	if (keypres=="") keypres='all_users';
	document.getElementById("datos").innerHTML="<p class='tiny'>Buscando...</p>";
	cargar("correos_xml.php?n="+keypres+'&key='+key,"datos");
}
// END - LEER CORREO
// BEGIN - SEND EMAIL
function checkSendMail(form){
	if (form.email_subject.value=='') alert("Ingrese el asunto del e-mail");
	else if (form.email_content.value=='') alert("Ingrese el mensaje");
	else{
		return true;
	}
	return false;
}
function saveTemplate(){
	if (document.getElementById('email_subject').value=='') alert("Ingrese el asunto del e-mail");
	else if (document.getElementById('email_content').value=='') alert("Ingrese el mensaje");
	else{
		var fondo = document.createElement('div'); 
		fondo.setAttribute('id','fondoSavetemplate'); 
		fondo.style.backgroundImage = "url(http://www.samuray.net.ec/images/feedback_fondo.png)"; 
		fondo.style.position = "absolute"; 
		fondo.style.width = "100%";
		var alto = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight +"px" : '100%';
		fondo.style.height = alto; 
		fondo.style.top = "0px"; 
		fondo.style.left = "0px";
		fondo.style.verticalAlign = "middle"; 
		fondo.align = "center";
		
		var ventana = document.createElement('div'); 
		ventana.style.width = "280px";
		ventana.style.height= "100px";
		ventana.style.margin = "180px auto"; 
		ventana.style.border = "solid 5px #000000";
		ventana.style.backgroundColor = "#FFFFFF";
		ventana.style.textAlign = "left";
		ventana.style.fontFamily = "Verdana";
		
		var barraTitulo = document.createElement('div'); 
		barraTitulo.style.backgroundColor = "#000000";
		barraTitulo.style.width = "100%";
		barraTitulo.style.cssFloat = 'left';
		barraTitulo.style.styleFloat = 'left';
		barraTitulo.style.color = "#FFFFFF";
		barraTitulo.style.fontSize = "16px"; 
		barraTitulo.style.fontWeight = "bold";
		barraTitulo.style.padding = "2px";
		barraTitulo.innerHTML = "Guardar Template";
		
		var formu = document.createElement("form");
		formu.action = "./?p=4";
		formu.name = "SaveTemplate_form";
		formu.method= "post";
		formu.style.margin = "10px 0";
		formu.style.padding= "0";
		formu.onsubmit = function(){
			if(document.getElementById('nombre').value=='') alert('Debe ingresar un nombre');
			else return true;
			return false;
		} 
		var p1 = document.createElement('p');
		p1.style.height = "16px";
		p1.style.paddin = "0";
		p1.style.margin = "0";
	
		var nombre_label = document.createElement("label");
		nombre_label.style.fontWeight = "bold";
		nombre_label.style.margin = "5px";
		nombre_label.style.color = "#333333";
		nombre_label.style.cssFloat = 'left';
		nombre_label.style.styleFloat = 'left';
		nombre_label.style.fontSize = "10px";
		nombre_label.style.cursor = "pointer";
	
    	var nombre_txt = document.createTextNode("Nombre:");
   	 	nombre_label.appendChild(nombre_txt);
   		p1.appendChild(nombre_label);
	
		var nombre = document.createElement('input');
		nombre.setAttribute('id','nombre'); 
		nombre.name="nombre";
		nombre.type="text";
		nombre.style.width = "200px";
		nombre.style.fontFamily="Verdana";
		nombre.style.fontSize = "11px";
		nombre.style.border = "#DDD solid 1px";
		nombre.style.margin = "3px 0 0 5px";
		nombre.style.padding = "1px";
		nombre.style.cssFloat = 'none';
		nombre.style.styleFloat = 'none';
		nombre.style.clear = 'both';
		nombre.style.color = '#666';
		nombre.style.backgroundColor = "#FFFFFF";
		p1.appendChild(nombre);
		
		var asunto = document.createElement('input');
		asunto.name = "asunto";
		asunto.type = "hidden";
		asunto.value= document.getElementById('email_subject').value;
		formu.appendChild(asunto);
		
		var cuerpo = document.createElement('input');
		cuerpo.name = "cuerpo";
		cuerpo.type = "hidden";
		cuerpo.value= document.getElementById('email_content').value;
		formu.appendChild(cuerpo);
		
		var Submit = document.createElement('button');
		Submit.name="save_template";
		Submit.type="submit";
		Submit.style.margin = "7px 100px";
		Submit.style.fontSize="11px";
		Submit.style.padding="2px 10px";
		Submit.style.cursor = "pointer";
		Submit.style.fontFamily="Verdana";
		var Submit_txt = document.createTextNode("Guardar");
		Submit.appendChild(Submit_txt);
		p1.appendChild(Submit);
		formu.appendChild(p1);
       	
    	var cerrar = document.createElement('div');
		cerrar.style.position = "absolute";
    	cerrar.style.margin = "-15px 0 0 270px";
		cerrar.style.width = "30px";
		cerrar.style.height= "30px";

		var closelink = document.createElement('a');
		closelink.href="javascript:cerrarSaveTemplate();";
		closelink.title ="Cerrar";
	
		var closebox = document.createElement('img'); 
		closebox.src = "http://www.samuray.net.ec/images/closebox.png";
		closebox.border = "0";
	
		closelink.appendChild (closebox);
		cerrar.appendChild (closelink);

		ventana.appendChild (cerrar);
		ventana.appendChild (barraTitulo); 
		ventana.appendChild(formu);
		fondo.appendChild (ventana); 
		document.body.appendChild(fondo);
		window.scrollTo(0,0);
		document.body.style.overflow = 'hidden';
		document.body.scroll="no"
	}
}
function cerrarSaveTemplate(){
	var fondo = document.getElementById('fondoSavetemplate');
   	document.body.removeChild(fondo);
	document.body.style.overflow = 'auto';
	document.body.scroll=true;
};
function cargar_template(id,key){
	document.getElementById("tit").innerHTML="<h2>Cargando...</h2>";
	llamarasincrono("templates_xml.php?p="+id+'&key='+key,'templates');
}
function cargar_templateXML(pagina_requerida){
	if (pagina_requerida.readyState == 4 && (pagina_requerida.status==200 || window.location.href.indexOf("http")==-1)){
		document.getElementById('advise').innerHTML='';
		document.getElementById('del_reg').disabled = '';
		document.getElementById('save').disabled = '';
		var xml = pagina_requerida.responseXML;
		document.getElementById("tit").innerHTML="<h2>"+xml.getElementsByTagName('template_name').item(0).firstChild.data.toUpperCase()+"</h2>";
		document.getElementById('email_subject').value = xml.getElementsByTagName('template_asunto').item(0).firstChild.data;
		document.getElementById('email_content').value = xml.getElementsByTagName('template_content').item(0).firstChild.data;
		document.getElementById('email_id').value = xml.getElementsByTagName('template_id').item(0).firstChild.data;
	}else if (pagina_requerida.readyState == 1){
		document.getElementById('advise').innerHTML='Cargando...';
		document.getElementById('email_cc').value = '';
		document.getElementById('email_content').value = '';
		document.getElementById('email_subject').value = '';
		document.getElementById('email_id').value = '';
	}
}
function clean_template(){
	document.getElementById('tit').innerHTML='<h2>Prepare un template</h2>';
	document.getElementById('email_id').value = '';
	document.getElementById('del_reg').disabled = 'true';
	document.getElementById('save').disabled = 'true';
}
// END - SEND EMAIL
function popup(id){
	window.open('item_0_1.php?id='+id+'', 'Detalles','width=800,height=600,menubar=no,status=no, location=no,toolbar=no,scrollbars=yes');
}
