// JavaScript Document
function Init(images)
{
	new Asset.images(images);
}
////////////////////////////////////////////////////////////////////////////////////////////////
function goUrl(url)
{
	document.location.href = url;
}
// 	Dado el nombre de una imagen recupera el objeto DOM
function $i(name)
{
	DI = document.getElementsByTagName('img');
	for (i=0;i<DI.length;i++)
	{
		if (DI[i].name == name)
			return DI[i];
	}
	return null;
}
	
// Cambia el menú activo de categorías
function toggleMenu(id, nameparent, name, url, redirect)
{
	var divContent = name + id;
	var divParent = nameparent + id;

	// On/Off
	if ($(divParent).className != "menutbltdcontenton")
			$(divParent).className = "menutbltdcontenton";
	else
			$(divParent).className = "menutbltdcontentoff";

	// Desplega/Inplega caegorías hijos
	if (id!=currentMenu)
	{
		if (currentMenu.length>0)
		{
			var cDivContent = name + currentMenu; 
			$(cDivContent).style.display = "none";
		}
		$(divContent).style.display = "block";
		currentMenu = id;
	} 
	else
	{
		$(divContent).style.display = "none";
		currentMenu = "";
	}

	// Redirecciona aunque el menú ya esté activo
	if (redirect)
		document.location.href = url;
}

// Cambia una imagen por otra
function imageReplace(img, src)
{
	img.src = src;
}

// Solo permite número en el textbox
function numeralsOnly(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
	if ((charCode >= 48 && charCode <=57) || charCode==8 || charCode==37 || charCode==39) {
        return true;
    }
	
    return false;
}

// Submit la forma al URL
function submitForm(url, method)
{
	var aform = $('fDatos');
	aform.method = method;
	aform.action = url;
	aform.submit();
}
		
function inputClearLabel(obj)
{
	if (obj.hadFocused != true)
	{
		obj.hadFocused = true;
		obj.value = "";
	}
}

function sendAuth (origen)
{
	$('Progreso').style.display = 'block';
	var aform = $('fDatos');
	var lgn = aform.login.value;
	var pswd = aform.password.value;
	var postArgs = 'username='+lgn+'&password='+pswd+'&redir='+encodeURIComponent(origen);
	new Ajax('servicios/validar.php', {method: 'post', postBody: postArgs, onSuccess: authSuccess, onFailure: authFailure}).request();	
}

function sendPasswd (origen)
{
	$('Progreso').style.display = 'block';
	var aform = $('fDatos');
	var recemail = aform.recemail.value;
	var postArgs = 'recemail='+recemail+'&redir='+encodeURIComponent(origen);
	new Ajax('servicios/recordar.php', {method: 'post', postBody: postArgs, onSuccess: authSuccess, onFailure: authFailure}).request();	
}

function authSuccess(response)
{
	var respArray = eval(response);
	$('Progreso').style.display = 'none';
	alert(respArray[0]);
	if (respArray[1].length>0)
	{
		document.location.href = decodeURIComponent(respArray[1]);
	}
	else
	{
		$('login').value = '';
		$('password').value = '';
		$('login').focus();
	}
}

function authFailure(response)
{
	$('Progreso').style.display = 'none';
	alert(response);
}

function sendRegField(campo)
{
	if (campo.value=="")
	{
		campo.className="autenticarinputtexton";
		campo.focus();
		return false;
	}
	return true;
}

function sendReg(origen,escliente)
{
	var enviarRegistro = true;
	var aform = $('fDatos');
	//aform.NOMBRE.className="autenticarinputtext";
	//aform.EMPRESA.className="autenticarinputtext";
	//aform.CIF_NIF.className="autenticarinputtext";
	aform.EMAIL.className="autenticarinputtext";
	aform.PWD.className="autenticarinputtext";
	//aform.TELEFONO.className="autenticarinputtext";
	//aform.URL.className="autenticarinputtext";
	//aform.DIRECCION_FACT.className="autenticarinputtext";
	//aform.CP_FACT.className="autenticarinputtext";
	//aform.PAIS_FACT.className="autenticarinputtext";
	//aform.PROVINCIA_FACT.className="autenticarinputtext";
	//aform.POBLACION_FACT.className="autenticarinputtext";
	//aform.DIRECCION_ENV.className="autenticarinputtext";
	//aform.CP_ENV.className="autenticarinputtext";
	//aform.PAIS_ENV.className="autenticarinputtext";
	//aform.PROVINCIA_ENV.className="autenticarinputtext";
	//aform.POBLACION_ENV.className="autenticarinputtext";
	// COMPRUEBA CAMPOS
	//enviarRegistros=sendRegField(aform.NOMBRE);
	//enviarRegistro=sendRegField(aform.CIF_NIF);
	enviarRegistro=sendRegField(aform.EMAIL)&&sendRegField(aform.PWD);
	//enviarRegistro=sendRegField(aform.TELEFONO);
	//enviarRegistro=sendRegField(aform.DIRECCION_FACT);
	//enviarRegistro=sendRegField(aform.CP_FACT);
	//enviarRegistro=sendRegField(aform.PAIS_FACT);
	//enviarRegistro=sendRegField(aform.PROVINCIA_FACT);
	//enviarRegistro=sendRegField(aform.POBLACION_FACT);
	//enviarRegistro=sendRegField(aform.DIRECCION_ENV);
	//enviarRegistro=sendRegField(aform.CP_ENV);
	//enviarRegistro=sendRegField(aform.PAIS_ENV);
	//enviarRegistro=sendRegField(aform.PROVINCIA_ENV);
	//enviarRegistro=sendRegField(aform.POBLACION_ENV);
	//var recibirInfo = "";
	//aform.RECIBIR_INFO.checked?(recibirInfo = 1):(recibirInfo = 0);
	if (enviarRegistro==true)
	{
		var postArgs = '';
		if (escliente==1)
		{
			postArgs+='NOMBRE='+encodeURIComponent(aform.NOMBRE.value);
			postArgs+='&EMPRESA='+encodeURIComponent(aform.EMPRESA.value);
			postArgs+='&CIF_NIF='+encodeURIComponent(aform.CIF_NIF.value);
		}
		postArgs+='&EMAIL='+encodeURIComponent(aform.EMAIL.value);
		postArgs+='&PWD='+encodeURIComponent(aform.PWD.value);
		if (escliente==1)
		{
			postArgs+='&TELEFONO='+encodeURIComponent(aform.TELEFONO.value);
			postArgs+='&URL='+encodeURIComponent(aform.URL.value);
			postArgs+='&DIRECCION_FACT='+encodeURIComponent(aform.DIRECCION_FACT.value);
			postArgs+='&CP_FACT='+encodeURIComponent(aform.CP_FACT.value);
			postArgs+='&PAIS_FACT='+encodeURIComponent(aform.PAIS_FACT.value);
			postArgs+='&PROVINCIA_FACT='+encodeURIComponent(aform.PROVINCIA_FACT.value);
			postArgs+='&POBLACION_FACT='+encodeURIComponent(aform.POBLACION_FACT.value);
			postArgs+='&DIRECCION_ENV='+encodeURIComponent(aform.DIRECCION_ENV.value);
			postArgs+='&CP_ENV='+encodeURIComponent(aform.CP_ENV.value);
			postArgs+='&PAIS_ENV='+encodeURIComponent(aform.PAIS_ENV.value);
			postArgs+='&PROVINCIA_ENV='+encodeURIComponent(aform.PROVINCIA_ENV.value);
			postArgs+='&POBLACION_ENV='+encodeURIComponent(aform.POBLACION_ENV.value);
		}
		//postArgs+='&RECIBIR_INFO='+encodeURIComponent(recibirInfo);
		postArgs+='&redir='+encodeURIComponent(origen);
		$('Progreso').style.display = 'block';
		new Ajax('servicios/registro.php', {method: 'post', postBody: postArgs, onSuccess: regSuccess, onFailure: regFailure}).request();	
	}
}

function regSuccess(response)
{
	var respArray = eval(response);
	$('Progreso').style.display = 'none';
	alert(respArray[0]);
	if (respArray[1].length>0)
	{
		document.location.href = decodeURIComponent(respArray[1]);
	}
}

function regFailure(response)
{
	$('Progreso').style.display = 'none';
	alert(response);
}

function sendMessage (name, nameaviso, subject, subjectaviso, email, emailaviso, abody, abodyaviso)
{
	if (name=="")
	{
		alert(nameaviso);
	}
	else
		if (subject=="")
		{
			alert(subjectaviso);
		}
		else
			if (email=="")
			{
				alert(emailaviso);
			}
			else
				if (abody=="")
				{
					alert(abodyaviso);
				}
				else
				{
					$('Progreso').style.display = 'block';
					var postArgs = 'name='+name+'&subject='+subject+'&email='+email+'&body='+abody;
					new Ajax('servicios/enviar.php', {method: 'post', postBody: postArgs, onSuccess: sendSuccess, onFailure: sendFailure}).request();	
				}
}

function sendSuccess(response)
{
	$('Progreso').style.display = 'none';
	alert(response);
}

function sendFailure(response)
{
	$('Progreso').style.display = 'none';
	alert(response);
}
//////////////////////////////////////////////////////
// Cistella
//////////////////////////////////////////////////////
function updateQtty(objName, baseUri)
{
	var qtty = $(objName).value;
	if (qtty==0)
	{
		removeProduct(baseUri);
	}
	else
	{
		baseUri = baseUri + "&cantidad="+qtty;
		document.location.href = baseUri;
	}
}

function removeProduct(Uri)
{
	if (confirm(removeProductMessage))
	{
		document.location.href = Uri;
	}
}

// VOTOS

function sendVoteField(campo)
{
	if (campo.value=="")
	{
		campo.className="autenticarinputtexton";
		campo.focus();
		return false;
	}
	return true;
}

function sendVote(origen,noreg,idv,idvi)
{
	var enviarVoto = true;
	var idviValor = 0;
	if (idvi.length>0)
	{
		for (x=0; x<idvi.length; x++)
		{
			if (idvi[x].checked)
			{
				idviValor = idvi[x].value;
			}
		}
	}
	else
		idviValor = idvi.value;
	var aform = $('fDatos');
	if (noreg == 0)
	{
		aform.NOMBRE.className="autenticarinputtext";
		aform.EMAIL.className="autenticarinputtext";
		aform.EDAD.className="autenticarinputtext";
		// COMPRUEBA CAMPOS
		enviarVoto=sendVoteField(aform.NOMBRE);
		enviarVoto=sendVoteField(aform.EMAIL);
		enviarVoto=sendVoteField(aform.EDAD);
	}
	else
		enviarVoto=sendVoteField(aform.EMAILSR);
	if (enviarVoto==true)
	{
		var postArgs='NOREG='+noreg;
		if (noreg == 0)
		{
			postArgs+= '&NOMBRE='+encodeURIComponent(aform.NOMBRE.value);
			postArgs+='&EMAIL='+encodeURIComponent(aform.EMAIL.value);
			postArgs+='&EDAD='+encodeURIComponent(aform.EDAD.value);		
		}
		else
			postArgs+= '&EMAIL='+encodeURIComponent(aform.EMAILSR.value);
		postArgs+='&redir='+encodeURIComponent(origen);
		postArgs+='&idv='+encodeURIComponent(idv);
		postArgs+='&idvi='+encodeURIComponent(idviValor);
		$('Progreso').style.display = 'block';
		new Ajax('servicios/votar.php', {method: 'post', postBody: postArgs, onSuccess: voteSuccess, onFailure: voteFailure}).request();	
	}
}

function voteSuccess(response)
{
	var respArray = eval(response);
	$('Progreso').style.display = 'none';
	alert(respArray[0]);
	if (respArray[1].length>0)
	{
		document.location.href = decodeURIComponent(respArray[1]);
	}
}

function voteFailure(response)
{
	$('Progreso').style.display = 'none';
	alert(response);
}

function goPaginas(modulo, id, esenlace,enlace)
{
	if (esenlace==1)
		window.open(enlace,"_blank");
	else
		document.location.href="index.php?ver="+modulo+"&cid="+id;
}