// JavaScript Document
function wopen(url, name, w, h){ 
	w += 32;
	h += 96;
	wleft = (screen.width - w) / 2;
	wtop = (screen.height - h) / 2;
	var win = window.open(url,
	name,
	'width=' + w + ', height=' + h + ', ' +
	'left=' + wleft + ', top=' + wtop + ', ' +
	'location=no, menubar=no, ' +
	'status=no, toolbar=no, scrollbars=yes, resizable=no');
	// Just in case width and height are ignored
	win.resizeTo(w, h);
	// Just in case left and top are ignored
	win.moveTo(wleft, wtop);
	win.focus();
}

function subeimagen(f){ 
	document.getElementById('formularioUpload').innerHTML="<img align='left' src='images/loading.gif'>";
	f.submit();
}
function resultadoUpload(estado, file, nfinal, tamanio){ 
	
	if (estado == 0){
		var mensaje = 'Foto Enviada Correctamente';
		document.registerform.nimagen.value=nfinal;
		document.registerform.noriginal.value=file;
		document.registerform.tamanio.value=tamanio;		
	}
	if (estado == 1)
		var mensaje = 'Error 1 ! - El Archivo no llego al servidor';
	if (estado == 2)
		var mensaje = 'Error 2 ! - Formato Erroneo.';
	if (estado == 3)
		var mensaje = 'Error 3 ! - No se pudo copiar Archivo.';
		
		
	document.getElementById('formularioUpload').innerHTML=mensaje;
	
}

function lTrim(sStr){
	while (sStr.charAt(0) == " ") 
		sStr = sStr.substr(1, sStr.length - 1);
		return sStr;
}
function rTrim(sStr){
	while (sStr.charAt(sStr.length - 1) == " ") 
		sStr = sStr.substr(0, sStr.length - 1);
		return sStr;
}	

function allTrim(sStr){
	return rTrim(lTrim(sStr));
}

function arreglaTextoInsertado(t){
	var sb="";
	var re = new RegExp ('&nbsp;', 'gi') ;
	sb = t.replace(re, '') ;
	re=new RegExp('&nbsp;','gi');
	t=sb.replace(re,'');
	
	return t;	
}

function fndocumento(formu){ 	
	var seleccion=false;
	var recomendado="no";
	for (var i=0;i<formu.length;i++) {	 		
		switch(formu.elements[i].type ){	
			case 'checkbox':
				if(formu.elements[i].checked){
					seleccion=true;
				}
				break;	
		}
	}
	
	if(formu.titulo.value==""){
		alert("Escriba un titulo");
		return;
	}
	else if(formu.noriginal.value==""){
		alert("Seleccione un documento");
		return;
	}
	else if(formu.descripcion.value==""){
		alert("Seleccione un documento");
		return;
	}
	else if(!seleccion){
		alert("Seleccione un tema");
		return;
	}
	else if(formu.recomendado[0].checked){ 
		recomendado="si";
	}
	

	
	var titulo=arreglaTextoInsertado(formu.titulo.value);
	var descripcion=arreglaTextoInsertado(formu.descripcion.value);
	var categoria=formu.categoria.value;
	var archivo=arreglaTextoInsertado(formu.nimagen.value);
	var noriginal=formu.noriginal.value;
	var tamanio=formu.tamanio.value;
	var tipodocumento=formu.tipodocumento.value;
	var temas="";
	
	for (var i=0;i<formu.length;i++) {	  
		switch(formu.elements[i].type ){	
			case 'checkbox':
				if(formu.elements[i].checked){
					temas=temas+formu.elements[i].value+"_";
				}
				break;	
		}
	}
	
	nuevoDocumento_("<div align=right><img src='images/loading.gif'></div>");

	doAjax('scripts/n_documento.scr.php','tamanio='+tamanio+'&noriginal='+noriginal+'&titulo='+titulo+'&descripcion='+descripcion+'&categoria='+categoria+'&archivo='+archivo+'&temas='+temas+'&tipodocumento='+tipodocumento+'&recomendado='+recomendado+'','nuevoDocumento_','post',0);		
}

function feditardocumento(formu){ 
	var recomendado="no";
	var seleccion=false;
	for (var i=0;i<formu.length;i++) {	 		
		switch(formu.elements[i].type ){	
			case 'checkbox':
				if(formu.elements[i].checked){
					seleccion=true;
				}
				break;	
		}
	}
	
	if(formu.titulo.value==""){
		alert("Escriba un titulo");
		return;
	}
	else if(formu.noriginal.value==""){
		alert("Seleccione un documento");
		return;
	}
	else if(formu.descripcion.value==""){
		alert("Seleccione un documento");
		return;
	}
	else if(!seleccion){
		alert("Seleccione un tema");
		return;
	}
	else if(formu.recomendado[0].checked){ 
		recomendado="si";
	}
		
	var iddoc=formu.iddoc.value;
	var titulo=arreglaTextoInsertado(formu.titulo.value);
	var descripcion=arreglaTextoInsertado(formu.descripcion.value);
	var categoria=formu.categoria.value;
	var archivo=arreglaTextoInsertado(formu.nimagen.value);
	var noriginal=formu.noriginal.value;
	var tamanio=formu.tamanio.value;
	var tipodocumento=formu.tipodocumento.value;
	var temas="";
	
	for (var i=0;i<formu.length;i++) {	  
		switch(formu.elements[i].type ){	
			case 'checkbox':
				if(formu.elements[i].checked){
					temas=temas+formu.elements[i].value+"_";
				}
				break;	
		}
	}

	editarDocumento_("<div align=right><img src='images/loading.gif'></div>");
	doAjax('scripts/mod_documento.scr.php','iddoc='+iddoc+'&tamanio='+tamanio+'&noriginal='+noriginal+'&titulo='+titulo+'&descripcion='+descripcion+'&categoria='+categoria+'&archivo='+archivo+'&temas='+temas+'&tipodocumento='+tipodocumento+'&recomendado='+recomendado+'','editarDocumento_','post',0);			
}

function eliminar(iddoc){
	var respuesta = confirm("Seguro que desea eliminar este documento? ");
	if(respuesta){
		editarDocumento_("<div align=right><img src='images/loading.gif'></div>");			
		doAjax('scripts/del_documento.scr.php','iddoc='+iddoc+'','editarDocumento_','post',0);	
	}
}

function resultadoUploadDoc(estado, file, nfinal, tamanio) {
	if (estado == 0){
		var mensaje = 'El Archivo se ha subido correctamente';
		document.fnnoticia.nimagen.value=nfinal;
		document.fnnoticia.noriginal.value=file;
		document.fnnoticia.tamanio.value=tamanio;
		document.fnnoticia.btnenviar.disabled=false;	

	}
	if (estado == 1)
		var mensaje = 'Error 1 ! - El Archivo no llego al servidor';
	if (estado == 2)
		var mensaje = 'Error 2 ! - Formato Erroneo.';
	if (estado == 3)
		var mensaje = 'Error 3 ! - No se pudo copiar Archivo.';
	
	document.getElementById('formUpload').innerHTML=mensaje;
} 

function subedocumento(f){
	f.btnenviar.disabled=true;	
	document.getElementById('formUpload').innerHTML="<img align='left' src='images/loading.gif'>";
	f.submit();
}

function cargaUsuario(id){
	cargaUsuario_("<div align=right><img src='images/loading.gif'></div>");			
	doAjax('usuario.php','id='+id+'','cargaUsuario_','post',0);	
}

function cargaUsuario_(html){
	document.getElementById('infouser').innerHTML=html;
}
