// JavaScript Document

function eliminar(ver_num_f, sel_num_f, numero, id_producto)
{
	if (confirm("¿Esta seguro que desea eliminar la foto?"))
	{
		document.getElementById(ver_num_f).style.display="none";
		document.getElementById(sel_num_f).style.display="none";

		ajax = nuevoAjax();
		ajax.open('GET', '/admin/modulos/catalogo/index.php?'+'action=eliminarFoto&id_prod='+id_producto+'&numero='+numero, true);
		ajax.onreadystatechange=function() 
		{
			if (ajax.readyState == 4) {}
		}
		ajax.send(null);
	}
}


function eliminar_doc(ver_num_f, sel_num_f, numero, id_producto)
{
	if (confirm("¿Esta seguro que desea eliminar el documento?"))
	{
		document.getElementById(ver_num_f).style.display = "none";
		document.getElementById(sel_num_f).style.display = "none";

		ajax = nuevoAjax();
		ajax.open('GET', '/admin/modulos/catalogo/index.php?'+'action=eliminarDocumento&id_prod='+id_producto+'&numero='+numero, true);
		ajax.onreadystatechange=function() 
		{
			if (ajax.readyState == 4) {}
		}
		ajax.send(null);
	}
}


function rs_getHijos(obj_origen, destino, padre, idioma)
{
	if (padre != 0)
	{
		var ajax;

		ajax = nuevoAjax();
		ajax.open('GET', '/admin/modulos/catalogo/index.php?'+'action=getHijos&destino='+destino+'&padre='+padre+'&idioma='+idioma, true);
		ajax.onreadystatechange=function() 
		{
			if (ajax.readyState == 4) 
			{
				cargarSelector(ajax.responseText);
			}
		}
		ajax.send(null);
	}
}

function rs_getTipofromTrans(obj_origen, destino, trans, idioma, idinmo)
{
	if ( !isNaN(trans) )
	{
		var ajax;

		ajax = nuevoAjax();
		ajax.open('GET', '/admin/modulos/catalogo/index.php?action=getTipofromTrans&destino='+destino+'&trans='+trans+'&idioma='+idioma+'&idInmo='+idinmo, true);
		ajax.onreadystatechange=function() 
		{
			if (ajax.readyState == 4) 
			{
				cargarSelector(ajax.responseText);
			}
		}
		ajax.send(null);
	}
}

function rs_getLocalidadfromTipo(obj_origen, destino, tipo, idioma, trans, zona, idinmo)
{
	if (!isNaN(tipo) && tipo != 0)
	{
		var ajax;

		ajax = nuevoAjax();
		ajax.open('GET', '/admin/modulos/catalogo/index.php?'+'action=getLocalidadfromTipo&destino='+destino+'&tipo='+tipo+'&idioma='+idioma+'&trans='+trans+'&zona='+zona+'&idInmo='+idinmo, true);
		ajax.onreadystatechange=function() 
		{
			if (ajax.readyState == 4) 
			{
				cargarSelector(ajax.responseText);
			}
		}
		ajax.send(null);
	}
}

function cargarSelector(retorno_rs)
{
	array_retorno 		= retorno_rs.split("|");
	name_obj_destino 	= array_retorno[0];
	obj_destino			= document.getElementById(name_obj_destino);
	str_elementos 		= array_retorno[1];
	array_elementos		= str_elementos.split(";");

	//inicializo los selectores hijos
	inicializarSelectores(obj_destino);

	//agrego el resto de los elementos
	for (i=0 ; i<array_elementos.length-1 ; i++)
	{
		array_elemento = array_elementos[i].split(",");
		addOpt(obj_destino, i+1, array_elemento[1], array_elemento[0]);
	}
}

function inicializarSelectores(obj_destino)
{
	switch (obj_destino.name)
	{
		case 'isla':
				inicializarSelector(document.getElementById('isla'));
				inicializarSelector(document.getElementById('zona'));
				inicializarSelector(document.getElementById('poblacion'));
				break;
		case 'zona':
				inicializarSelector(document.getElementById('zona'));
				inicializarSelector(document.getElementById('poblacion'));
				break;
		case 'poblacion': 
				inicializarSelector(document.getElementById('poblacion'));
				break;
		case 'tipo_prop':
				inicializarSelector(document.getElementById('tipo_prop'));
				inicializarSelector(document.getElementById('poblacion'));
				break;
		default:
			switch(obj_destino.id)
			{
			
				case 'isla':
					inicializarSelector2(document.getElementById('isla'));
					inicializarSelector2(document.getElementById('zona'));
					inicializarSelector2(document.getElementById('poblacion'));
					break;
				case 'zona':
					inicializarSelector2(document.getElementById('zona'));
					inicializarSelector2(document.getElementById('poblacion'));
					break;
				case 'poblacion': 
					inicializarSelector2(document.getElementById('poblacion'));
					break;
				case 'tipo_prop':
					inicializarSelector2(document.getElementById('tipo_prop'));
					inicializarSelector2(document.getElementById('poblacion'));
					break;
				default:
					break;
			}			
	}
}

function inicializarSelector(obj_destino)
{
    while (obj_destino.length) 
		obj_destino.remove(0);

	addOpt(obj_destino, 0, '--- Seleccione ---', 0); //agrego la primer opción --- Seleccione ---
}

function inicializarSelector2(obj_destino)
{
    var daOpt = new Option(obj_destino.options[0].text, obj_destino.options[0].value);
	
	while (obj_destino.length) 
		obj_destino.remove(0);
		
	obj_destino.options[0] = daOpt;
}


function addOpt(oCntrl, iPos, sTxt, sVal)
{
    var selOpcion=new Option(sTxt, sVal);
	eval(oCntrl.options[iPos]=selOpcion);	
}


function nuevoAjax()
{
	var xmlhttp = false;
 	try 
 	{
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} 
 	catch (e) 
 	{
 		try 
 		{
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} 
 		catch (E) 
 		{
 			xmlhttp = false;
 		}
  	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
	{
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function checkReferenciaDuplicada(referencia, idCliente, idProducto)
{
	var ajax;
	
	ajax = nuevoAjax();
	ajax.open('GET', './index.php?'+'action=checkRef&referencia='+referencia+'&idCliente='+idCliente+'&idProducto='+idProducto+'&idCat='+categoriaActual, true);
	ajax.onreadystatechange=function() 
	{
		if (ajax.readyState == 4) 
		{
			//alert( referencia + " : " + ajax.responseText );
			if (ajax.responseText == "true")
			{
				alert("Por favor verifique los datos ingresados:\n\nReferencia duplicada.")
			}
			else
			{
				ValidarFormulario();
			}
		}
	}
	ajax.send(null);
}

function mostrar(id)
{
	if(document.getElementById(id).style.display=="")
		document.getElementById(id).style.display="none";
	else
		document.getElementById(id).style.display="";
}


function getSelectedClient()
{
	if (document.getElementById('id_cliente').value == "")
	{
		objCliente = document.getElementById('cliente');
		return objCliente.options[objCliente.selectedIndex].value;
	}
	else
	{
		return document.getElementById('id_cliente').value;
	}
}

function pasarAmiles(numero)
{	
	if (numero != "")
	{ 
		num = numero.toString().replace(/\./g, '');
		num = Math.floor(num*1000+0.50000000001);
		num = Math.floor(num/1000).toString(); 
		
		j = 1;
		retorno = ""; 
		for (var i = num.length-1 ; i >= 0 ; i--)
		{
			if (j != 3)
			{
				retorno = num.substring(i+1,i) + retorno;
				j++;
			}
			else
			{
				if (i!=0)
				{
					retorno = ',' + num.substring(i+1,i) + retorno;
					j = 1;
				}
				else
				{
					retorno = num.substring(i+1,i) + retorno;
					j = 1;
				}
			}
		}
		return retorno.toString().replace(/,/g, '.');
	}	
	return numero;
}

function calcularValorPeseta(activo, desactivo, peseta, camposVacios)
{ 
	if (!camposVacios)
	{
		var euros = document.getElementById(activo).value;
		if (euros != "")
		{
			var num			= euros.toString().replace(/\./g, '');
			var num			= parseFloat(num);
			var valorPesetas	= num * peseta; 	 	 
			valorPesetas		= parseFloat(valorPesetas);
			document.getElementById(activo+'PTS').value = pasarAmiles(valorPesetas.toFixed(0));	 	 
			document.getElementById(activo).value = pasarAmiles(num.toFixed(0)) ;
		}
		else
		{
			document.getElementById(activo+'PTS').value= "";	 	 
		}
		habilitarDeshabilitar(activo, desactivo);
	}
}


function habilitarDeshabilitar(activo, desactivo)
{
	if (document.getElementById(activo).value == "")
	{
		document.getElementById(activo).disabled	= false;
		document.getElementById(desactivo).disabled	= false;
		document.getElementById(desactivo).readonly	= false;
		document.getElementById(activo).readonly	= false;
	}
	else
	{
		document.getElementById(desactivo).disabled = true;
		document.getElementById(desactivo).readonly = true;	
	}
}


function convertir_a_miles(obj)
{
	obj.value = pasarAmiles(obj.value);
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
