//PONE LA BUSQUEDA A CERO
////////////////////////////////////////////////////////////////////////////////
// extraido pedidos2
// 16/03/2005

function resetBuscar()
{
    if(document.frmBusqueda.str != undefined)
    {
        document.frmBusqueda.str.value = '';
    }

    if(document.frmBusqueda.marca != undefined)
    {
        document.frmBusqueda.marca.value = '';
    }

    if(document.frmBusqueda.familia != undefined)
    {
        document.frmBusqueda.familia.value = '';
    }

    if(document.frmBusqueda.subfamilia != undefined)
    {
        document.frmBusqueda.subfamilia.value = '';
    }

	if(document.frmBusqueda.rango_precios != undefined)
    {
        document.frmBusqueda.rango_precios.value = '';
    }

    document.frmBusqueda.submit();
}

//SI PAIS NO ES ESPAÑA PONE EN EL CAMPO PROVINCIA FUERA DE ESPAÑA
////////////////////////////////////////////////////////////////////////////////
// extraido tuweb
// 16/03/2005

function cambia_prov(nombreformulario, pais)
{

    if (pais != 'ES')
    {
        valor = eval('document.'+nombreformulario+'.provincia.value');
        valor = '99';
    }
}

//Codificar URL
/////////////////////////////////////////////////////////////////////////////////

function URLEncode( plaintext )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +	// Numeric
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
		"abcdefghijklmnopqrstuvwxyz" +
		"-_.!~*'()";			// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

/////////////////////////////////

function comprobar_expresion_regular(str, pattern)
{
	reg = new RegExp(pattern);

	if (reg.exec(str))
	{
		ret = (RegExp.$1 == str);
	}
	else
	{
		ret = false;
	}

	return ret;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkEditKey(keycode)
{
	return(keycode == 8 || keycode == 37 || keycode == 38 || keycode == 39 || keycode == 40);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function comprobar_numero(str)
{
	return comprobar_expresion_regular(str, "([0-9]\\d*)");
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function comprobar_entero(str)
{
	return comprobar_expresion_regular(str, "((-?[1-9]\\d*)|(0)|(-))");
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function comprobar_Uentero(str)
{
	return comprobar_expresion_regular(str, "(([1-9]\\d*)|(0))");
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function comprobar_flotante(str, decimals, decsep)
{
	if(decimals == 0)
	{
		if (comprobar_entero(str))
		{
			ret = true;
		}
		else if (comprobar_expresion_regular(str, "((-?[1-9]\\d*\\" + decsep + "\\d*)|(-?0\\" + decsep + "\\d*))"))
		{
			ret = true;
		}
		else
		{
			ret = false;
		}
	}
	else
	{
	    if (comprobar_entero(str))
		{
			ret = true;
		}
		else if (comprobar_expresion_regular(str, "((-?[1-9]\\d*\\" + decsep + "\\d{0," + decimals + "})|(-?0\\" + decsep + "\\d{0," + decimals + "}))"))
		{
			ret = true;
		}
		else
		{
			ret = false;
		}
	}

	return ret;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function comprobar_Uflotante(str, decimals, decsep)
{
    alert('num:' + str);
    alert('decimals:' + decimals);
    alert('decsep:' + decsep);
	if(decimals == 0)
	{
		if (comprobar_Uentero(str))
		{
			ret = true;
		}
		else if (comprobar_expresion_regular(str, "(([1-9]\\d*\\" + decsep + "\\d*)|(0\\" + decsep + "\\d*))"))
		{
			ret = true;
		}
		else
		{
			ret = false;
		}
	}
	else
	{
	    if (comprobar_Uentero(str))
		{
			ret = true;
		}
		else if (comprobar_expresion_regular(str, "(([1-9]\\d*\\" + decsep + "\\d{0," + decimals + "})|(0\\" + decsep + "\\d{0," + decimals + "}))"))
		{
			ret = true;
		}
		else
		{
			ret = false;
		}
	}

	return ret;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkCustom(str, pattern)
{
	return comprobar_expresion_regular(str, "([" + pattern + "]+)");
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function onKeyPressIntTextBox(str, e)
{
    var keyCode = e.which ? e.which : e.keyCode;
    return comprobar_entero(str + String.fromCharCode(keyCode)) || checkEditKey(keyCode);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function onKeyPressUIntTextBox(str, e)
{
    var keyCode = e.which ? e.which : e.keyCode;
    return comprobar_Uentero(str + String.fromCharCode(keyCode)) || checkEditKey(keyCode);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function onKeyPressFloatTextBox(decimals, decsep, str, e)
{
    var keyCode = e.which ? e.which : e.keyCode;
    return comprobar_flotante(str + String.fromCharCode(keyCode), decimals, decsep) || checkEditKey(keyCode);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function onKeyPressUFloatTextBox(decimals, decsep, str, e)
{
    var keyCode = e.which ? e.which : e.keyCode;
    return comprobar_Uflotante(str + String.fromCharCode(keyCode), decimals, decsep) || checkEditKey(keyCode);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function onKeyPressCustomTextBox(pattern, str, e)
{
    var keyCode = e.which ? e.which : e.keyCode;
    return checkCustom(str + String.fromCharCode(keyCode), pattern) || checkEditKey(keyCode);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function saltaEnter(e,obj) {

  /*tecla=(document.all) ? e.keyCode : e.which;
  if(tecla!=13) return;*/

    var tecla = e.charCode;

    if (!tecla) {
        tecla = e.keyCode;
    }

  if(tecla!=13 && tecla!=9) return;

  frm=obj.form;
  for(i=0;i<frm.elements.length;i++)
    if(frm.elements[i]==obj) {
      if (i==frm.elements.length-1) i=-1;
      break }
  frm.elements[i+1].focus();
  return false;
}

//comprobar si un campo está vacio
function vacio(campo)
{
    for(i=0;i<campo.value.length;i++)
    {
        campo_actual = campo.value.charAt(i);
        if(campo_actual != " ")
        {
            return true;
        }
    }

    return false;
}

//en javascript no existe trim, lo hacemos a mano
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function trim_js(campo)
{
    while(''+campo.value.charAt(0)==' ')campo.value=campo.value.substring(1,campo.value.length);
}

//impedir vaciar un campo
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function comprobar_campo_lleno1(campo)
{
    trim_js(campo);

    if(campo.value=='')
    {
        alert('campo_vacio_no_permitido');
        campo.focus();
        return false;
    }
    return true;
}

function comprobar_campo_lleno(campo,texto)
{
    if(vacio(campo) == false)
    {
        alert(texto);
        campo.focus();
        return false;
    }

    return true;
}

function ltrim(cadena)
{
    while(cadena.charAt(0) == ' ')
    {
        cadena = cadena.substring(1,cadena.length);
    }

    return cadena;
}

function rtrim(cadena)
{
    while(cadena.charAt(cadena.length-1) == ' ')
    {
        cadena = cadena.substring(0,cadena.length-1);
    }

    return cadena;
}

function trim(cadena)
{
    return ltrim(rtrim(cadena));
}

function empty(cadena)
{
	if(cadena == undefined)
	{
		return 1;
    }

    cadena = trim(cadena);

    if(cadena.length == 0)
    {
        return 1;
    }
    return 0;
}

function stripHTML(cadena)
{
    return cadena.replace(/<(.|\n)*?>/g, '');
//	return (cadena+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');

}

function control_form_descarga_importador()
{
    cadena = '';

    if(empty(document.formImportador.usuario_tienda.value))
    {
        cadena += 'Introduzca el usuario administrador \n';
    }

    if(empty(document.formImportador.password_tienda.value))
    {
        cadena += 'Introduzca la contraseña de administrador \n';
    }

    if(document.formImportador.tipo_eurowin.value == '-1')
    {
        cadena += 'Seleccione el tipo de Eurowin que utilizará \n';
    }

    if(document.formImportador.tipo_base_datos.value == '-1')
    {
        cadena += 'Seleccione el tipo de base de datos que utiliza su Eurowin \n';
    }

    if(empty(document.formImportador.ruta_eurowin.value ))
    {
        cadena += 'Introduzca la ruta de eurowin \n';
    }

    if(empty(document.formImportador.email.value))
    {
        cadena += 'Introduzca su dirección de correo correctamente \n';
    }

    if(!empty(cadena))
    {
        alert(cadena);
        return false;
    }

    return true;
}

function comprobar_Uflotante_entero(cadena)
{
   // return comprobar_Uflotante(cadena, '2', '.');
}

/*
http://www.iec.csic.es/CRIPTONOMICON/javascript/validarformularios.html
http://www.elcodigo.com/cgi-bin/DBread.cgi?tabla=scripts&campo=0&clave=131&info=1
*/

//$url_imagen = "data/".$gp->nombre_pagina."/"."img/articulos_zoom/".$datos_articulo->articulo_zoom;
//$img_zoom = "img/articulos_zoom/".$datos_articulo->articulo_zoom;
//list($ancho, $alto, $tipo, $atr) = getimagesize($url_imagen);
//print "<a href='#' onclick='abrir_ventana_flotante(\"$img_zoom\",\"$alto\",\"$ancho\");'><IMG src='". $img ."'></a>";
function abrir_ventana_flotante(url_imagen,alto,ancho)
{
    var winl = (screen.width-ancho)/2;
    var wint = (screen.height-alto)/2;
    if (winl < 0) winl = 0;
    if (wint < 0) wint = 0;

    windowprops = "height="+alto+",width="+ancho+",top="+ wint +",left="+ winl +",location=no,"
    + "scrollbars=yes,menubars=no,toolbars=no,resizable=no,status=no";

    window.open(url_imagen, "Popup", windowprops);
}


function ocultarSubfamilias(familia)
{
    if (document.getElementById('ocultarSubfamilias'+ familia).style.height == "0px")
    {
        document.getElementById('ocultarSubfamilias'+ familia).style.height = "";
    }
    else
    {
        document.getElementById('ocultarSubfamilias'+ familia).style.height = "0px";
    }
}


/* POPUP WINDOW CENTER
   La siguiente función permite la apertura de ventanas en el centro del navegador.
   Para su correcto funcionamiento deberemos pasarle los sigueintes valores desde el evento onClick:
       - page : Página que deseamos abrir.
       - w : Ancho de la página.
       - h : Alto de la página
*/
var n_popup=0;
function popup(page,w,h) {
var newwindow;
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;

	windowprops = "height="+h+",width="+w+",top="+ wint +",left="+ winl +",location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=yes,status=yes";
	newwindow=window.open(page, "popup"+n_popup, windowprops);
	n_popup = n_popup + 1;
	if (window.focus) {
		newwindow.focus()
	}
	setTimeout("hideProcesando()",200);
}


/*FUNCION PARA GENERAR EL IBAN CODE*/
/*Eric Corbella - 08/10/2010 13:11:54*/
function getCtrlIban (pais, entidad, oficina, dc, cuenta)
{
    iban = entidad + oficina + dc + cuenta + pais + '00';

    var arrayIban = iban.split('');

    var arrayAbecedario = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

    var iban = "";

    for (i = 0; i < arrayIban.length; i++)
    {
        letra = 0;
        for (w = 0; w < arrayAbecedario.length; w++)
        {
            if (arrayIban[i].toUpperCase() == arrayAbecedario[w]) { iban += w+10; letra = 1;}
        }

        if (letra == 0) iban += arrayIban[i];
    }


    var divident = 0;

    var arrayIban = iban.split('');

    for (i = 0; i < iban.length; i++)
    {
        divident = divident * 10 + parseInt(arrayIban[i], 10);

        if (divident >= 97) divident %= 97;
    }

    result = 98 - divident;

    result = result.toString();

    if (result.length == 1) result = '0'+ result;

    return pais + result;
}

function controlKey(evento) {
    var key = evento.charCode;
    if (!key) {
        key = evento.keyCode;
    }
    if (key == 8 || key == 9 || key == 13) {
        return true;
    }
    if (key < 48 || key > 57) {
        alert ('Porfavor, introduzca valores numéricos');
        return false;
    }
}

