// 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, url, redirect,imgId)
{
	var divContent = "CatBtnCnt" + id;
	var img = $i('arrow' + id);
	
	if (id!=currentMenu)
	{
		if (currentMenu.length>0)
		{
			var cDivContent = "CatBtnCnt" + currentMenu; 
			var cImg = $i('arrow' + currentMenu); 
			cImg.src = "images/arrows_02" + imgId + ".gif";
			$(cDivContent).style.display = "none";
		}
		
		img.src = "images/arrows_01" + imgId + ".gif";
		$(divContent).style.display = "block";
		currentMenu = id;
		if (redirect)
			document.location.href = url;
	} 
	else
	{
		img.src = "images/arrows_02" + imgId + ".gif";
		$(divContent).style.display = "none";
		currentMenu = "";
	}

}

// 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 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 sendMessage (name, email, abody)
{
	$('Progreso').style.display = 'block';
	var postArgs = 'name='+name+'&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);
}

//////////////////////////////////////////////////////
// Pressupost a mida
//////////////////////////////////////////////////////
function sendPressuImagen()
{
  var fAction = $('fDatos').action;
  var fMethod = $('fDatos').method;
  var fTarget = $('fDatos').target;
  $('fDatos').action = "servicios/pressuimg.php";
  $('fDatos').method = "post";
  $('fDatos').target = "iframeUpload";
  $('fDatos').submit();
  $('fDatos').action = fAction;
  $('fDatos').method = fMethod;
  $('fDatos').target = fTarget;
 }

function sendPressu (imgURL)
{
  var enviarRegistros = true;
	var aform = $('fDatos');
  var cSoporte = "";
  var frto = "";
  var clrs = "";
  var nColrs = "";
  for (x=0;x<aform.ColorSoporte.length;x++)
  {
    if (aform.ColorSoporte[x].checked)
      cSoporte = aform.ColorSoporte[x].value;
  }
  for (x=0;x<aform.Formato.length;x++)
  {
    if (aform.Formato[x].checked)
      frto = aform.Formato[x].value;
  }
  for (x=0;x<aform.Colores.length;x++)
  {
    if (aform.Colores[x].checked)
      clrs = aform.Colores[x].value;
  }
  for (x=0;x<aform.NumColores.length;x++)
  {
    if (aform.NumColores[x].checked)
      nColrs = aform.NumColores[x].value;
  }
	aform.nombre.className="REGINPUT";
	aform.correo.className="REGINPUT";
	// COMPRUEBA CAMPOS
	enviarRegistros=sendRegField(aform.nombre)&&sendRegField(aform.correo);
	if (enviarRegistros==true)
	{
  	$('Progreso').style.display = 'block';
  	var postArgs = 'nombre='+aform.nombre.value+'&correo='+aform.correo.value
    +'&colorSoporte='+(cSoporte)+'&formato='+(frto)+'&colores='+(clrs)
    +'&numColores='+(nColrs)+'&medidaW='+(aform.medidaW.value)+'&medidaH='+(aform.medidaH.value)
    +'&diametroW='+(aform.diametroW.value)+'&diametroH='+(aform.diametroH.value)+'&imgURL='+imgURL;
  	new Ajax('servicios/pressuamida.php', {method: 'post', postBody: postArgs, onSuccess: sendPressuSuccess, onFailure: sendPressuFailure}).request();
  }	
}

function sendPressuSuccess(response)
{
	$('Progreso').style.display = 'none';
	alert(response);
}

function sendPressuFailure(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;
	}
}
//////////////////////////////////////////////////////
// Nuevo Cliente
//////////////////////////////////////////////////////
function sendRegField(campo)
{
	if (campo.value=="")
	{
		campo.className="REGINPUTON";
		campo.focus();
		return false;
	}
	return true;
}

function sendReg(origen)
{
	var enviarRegistros = true;
	var aform = $('fDatos');
	aform.nombre.className="REGINPUT";
	aform.correo.className="REGINPUT";
	aform.cif_nif.className="REGINPUT";
	aform.usuario.className="REGINPUT";
	aform.pwd.className="REGINPUT";
	aform.telefono.className="REGINPUT";
	aform.dir_fact.className="REGINPUT";
	aform.cp_fact.className="REGINPUT";
	aform.pob_fact.className="REGINPUT";
	aform.prov_fact.className="REGINPUT";
	aform.pais_fact.className="REGINPUT";
	aform.dir_env.className="REGINPUT";
	aform.cp_env.className="REGINPUT";
	aform.pob_env.className="REGINPUT";
	aform.prov_env.className="REGINPUT";
	aform.pais_env.className="REGINPUT";
	// COMPRUEBA CAMPOS
	enviarRegistros=sendRegField(aform.nombre)
    &&sendRegField(aform.correo)
    &&sendRegField(aform.cif_nif)
	  &&sendRegField(aform.usuario)
	  &&sendRegField(aform.pwd)
	  &&sendRegField(aform.telefono)
	  &&sendRegField(aform.dir_fact)
	  &&sendRegField(aform.cp_fact)
	  &&sendRegField(aform.pob_fact)
	  &&sendRegField(aform.prov_fact)
	  &&sendRegField(aform.pais_fact);
	//enviarRegistro=sendRegField(aform.dir_env);
	//enviarRegistro=sendRegField(aform.cp_env);
	//enviarRegistro=sendRegField(aform.pob_env);
	//enviarRegistro=sendRegField(aform.prov_env);
	//enviarRegistro=sendRegField(aform.pais_env);
	if (aform.esdistribuidor.checked)
		distribuidor = "1";
	else
		distribuidor = "0";
	if (aform.exempto_iva.checked)
		exempto_iva = "1";
	else
		exempto_iva = "0";
	if (enviarRegistros==true)
	{
		var postArgs = 'NOMBRE='+encodeURIComponent(aform.nombre.value);
		postArgs+='&EMAIL='+encodeURIComponent(aform.correo.value);
		postArgs+='&CIF_NIF='+encodeURIComponent(aform.cif_nif.value);
		postArgs+='&USUARIO='+encodeURIComponent(aform.usuario.value);
		postArgs+='&PWD='+encodeURIComponent(aform.pwd.value);
		postArgs+='&TELEFONO='+encodeURIComponent(aform.telefono.value);
		postArgs+='&URL='+encodeURIComponent(aform.url.value);
		postArgs+='&DIRECCION_FACT='+encodeURIComponent(aform.dir_fact.value);
		postArgs+='&CP_FACT='+encodeURIComponent(aform.cp_fact.value);
		postArgs+='&PAIS_FACT='+encodeURIComponent(aform.pais_fact.value);
		postArgs+='&PROVINCIA_FACT='+encodeURIComponent(aform.prov_fact.value);
		postArgs+='&POBLACION_FACT='+encodeURIComponent(aform.pob_fact.value);
		postArgs+='&DIRECCION_ENV='+encodeURIComponent(aform.dir_env.value);
		postArgs+='&CP_ENV='+encodeURIComponent(aform.cp_env.value);
		postArgs+='&PAIS_ENV='+encodeURIComponent(aform.pais_env.value);
		postArgs+='&PROVINCIA_ENV='+encodeURIComponent(aform.prov_env.value);
		postArgs+='&POBLACION_ENV='+encodeURIComponent(aform.pob_env.value);
		postArgs+='&ESDISTRIBUIDOR='+encodeURIComponent(distribuidor);
		postArgs+='&EXEMPTO_IVA='+encodeURIComponent(exempto_iva);
		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);
}
