// JavaScript Document

function AddImage(campo)
{
 	fin = window.open('/admin/modulos/imagenes/index.php?action=insertar_imagen&tipo=2&campo='+campo, 'Images', 'width=467, height=450, scrollbars=yes, statusbar=no, resizable=yes');				
	centrarVentana(fin, 467, 450);
	fin.focus();
}

function AddDoc(campo)
{
 	vent = window.open('/admin/modulos/documentos/index.php?action=insertar_documento&tipo=2&campo='+campo, 'Images', 'width=466, height=392, scrollbars=yes, statusbar=no, resizable=yes');				
	centrarVentana(vent, 466, 392);
	vent.focus();
}

function AddEnlace(campo)
{
 	vent = window.open('/admin/modulos/enlaces/index.php?action=insertar_enlace&tipo=2&campo='+campo, 'Images', 'width=466, height=392, scrollbars=yes, statusbar=no, resizable=yes');				
	centrarVentana(vent, 466, 392);
	vent.focus();
}

function openImage(id_foto)
{
	vent = window.open('/admin/modulos/imagenes/index.php?action=getHTMLImagen&id_imagen='+id_foto, 'Images', 'width=466, height=392, scrollbars=yes, statusbar=no, resizable=yes');				
	centrarVentana(vent, 466, 392);
	vent.focus();
}

function centrarVentana(objvent, width, height)
{
	objvent.moveTo((screen.width-width)/2,(screen.height-height)/2);
}

function fieldNumber(objeto, numeroDecimales, e) 
{ 
	var valorCampo; 
	var evento_key = (document.all) ? e.keyCode : e.which; 
	var numPosPunto = 0; 
	var strParteEntera = ""; 
	var strParteDecimal = ""; 
	var NUM_DECIMALES = numeroDecimales; 

	switch (evento_key) 
	{ 
		case 48: 
		case 49: 
		case 50: 
		case 51: 
		case 52: 
		case 53: 
		case 54: 
		case 55: 
		case 56: 
		case 57: 
		break; 
		default: 
			if (((evento_key != 44) || (NUM_DECIMALES == 0)) && (evento_key != 8))
			{
				if (document.all) 
					e.keyCode = 0;
				else 
					e.stopPropagation();
				return false; 
			}
	} 

	valorCampo = objeto.value; 	
	if (evento_key == 44) 
	{
		if (valorCampo == '')
		{
			if (document.all) 
				e.keyCode = 0;
			else 
				e.stopPropagation();
			return false; 
		}
		else if (valorCampo.indexOf(",") != -1)
		{ 
			if (document.all) 
				e.keyCode = 0;
			else 
				e.stopPropagation();
			return false; 
		} 
	}
	/* Sólo puede teclear el número de decimales indicado en NUM_DECIMALES */ 
	if ((numPosPunto = valorCampo.indexOf(",")) != -1) 
	{ 
		strParteEntera = valorCampo.substr(0,(numPosPunto - 1)); 
		strParteDecimal = valorCampo.substr((numPosPunto + 1), valorCampo.length) 
		
		if ((strParteDecimal.length > (NUM_DECIMALES - 1)) && (evento_key != 8))
		{ 
			if (document.all) 
				e.keyCode = 0;
			else 
				e.stopPropagation();
			return false; 
		} 
	} 
	
	return true; 
} 
function nuevoAjax()
{
	var xmlhttp=false; 
	try 
	{ 
		// No IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e)
	{ 
		try
		{ 
			// IE 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(E) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); } 
	return xmlhttp; 
}


function pegarContenido(){
	
	var ajax=nuevoAjax();
		ajax.open("POST", urlDestino, true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
		
		ajax.send("action=pegar");

		ajax.onreadystatechange=function()
		{			
			if (ajax.readyState==4)
			{
	
				var respuesta=ajax.responseText;
				
				respuesta = decodificarCodigo(respuesta);

				if(respuesta!="")
					pegarRespuesta(respuesta);
			}
		}
	}
	
	
function pegarRespuesta(respuesta){

	datos =	respuesta.split("##");
	total = datos.length;
	for(i=0;i<total; i++)
	{
		if(datos[i]!="-")//no es idioma
		{	
			campo = datos[i].split("::");
			
			document.getElementById(campo[0]).value = campo[1]
		
		}else{
			i++;
			campo = datos[i].split("::");
		
			idioma_nuevo =  campo[1];
			document.getElementById("id_"+idioma_nuevo).style.display="block";
			i++;
			for(j=i;j<(i+3); j++)
			{
				if(datos[j])
				{
					campo = datos[j].split("::");

					document.getElementById(campo[0]+"_"+idioma_nuevo).value = campo[1]
					}
			}
			i=j
		}
	}
	
	
}
function copiarContenido(id, nombre_tabla, traduccion){
	
		var ajax=nuevoAjax();
		ajax.open("POST", urlDestino, true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("id="+id+"&nombre_tabla="+nombre_tabla+"&action=copiar");

		ajax.onreadystatechange=function(){	
					if (ajax.readyState==4)
						{
								var respuesta=ajax.responseText;
								alert(traduccion)
						}
			}
	
	}
function decodificarCodigo(respuesta){

		dev = respuesta.replace(/&#146;/g, "’");
		return dev;
	
	}
	
// CHECK UNCHEK CHECKBOXES
	function checkUncheck(field, val){
		if(field)
		{
			if(field.value)//cuando hay un solo elemento
				field.checked = val;
			//mas de un elemento	
			for (i = 0; i < field.length; i++){field[i].checked = val;}
		}
	}	