function setFileName(theForm, input, valor)
{
	if ( (input == null) || (input == "") ||
		 (valor == null) || (valor == "") )
		return;

	var aspl = valor.split("\\");
	var str;

	document.form.encoding = "multipart/form-data";
	document.form.action = "{POST}abm_det.asp?tabla={TABLA}&id={ROW_ID}&volver={VOLVER}&preset={PRESET}";
	str = "";

	if (aspl.length > 0) {
		str = aspl[aspl.length - 1];
	}
	var p = 'theForm.' + input.substr(2) + '.value="' + str + '"';
	eval(p);
}

function isblank (o)
{
	if (o == null) return true;

	var s;
	s = o.value;

	if ((s == null) || (s == ""))
		return true;

	for (var i=0;i<s.length;i++) {
		var c = s.charAt(i);
		if (o.type == "select")
		{
			if (o.options[o.selectedIndex].value == "") return false;
		} else {
			if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
		}
	}
	return true;
}

/////////////////////////////////////////////////////////////////////
function checkRequiredOK(e)
{
	if (((e.type == "text") ||
		(e.type == "textarea") ||
		(e.type == "password") ||
		(e.type == "select-one")) &&
		!e.optional)
	{
		if (isblank(e) || (e.type == "select-one" && e.value == 0))
		{
			return false;
		}
	}

	return true;
}

function getName(e)
{
	if (e.nombre != null)
		return e.nombre;
	else
		return e.name;
}
/////////////////////////////////////////////////////////////////////
function verify(f)
{
	var msg;
	var empty_fields = "";
	var errors = "";

	for (var i=0;i<f.length;i++)
	{
		var e = f.elements[i];

		if (!checkRequiredOK(e))
		{
			if (empty_fields == "")
			{
				f.elements[i].focus();
				empty_fields = "\n";
			}

			empty_fields += "\n- " + getName(e);
			continue;
		}


		if (e.date && !isblank(e))
		{
			var error;
			error = '';

			/*--------------------------------------
			  Cambia los "-" por "/" para aceptar
			  el formato de fecha "dd-mm-aaaa"
			  CC 20050513
			--------------------------------------*/

			out = "-";
			add = "/";
			temp = "" + e.value;

			while (temp.indexOf(out)>-1) {
			pos= temp.indexOf(out);
			temp = "" + (temp.substring(0, pos) + add +
			temp.substring((pos + out.length), temp.length));
			}

			/*-------------------------------------*/

			var d = temp.split("/");

			if ((e.length < 8) || (d.length != 3) ||
				isNaN(parseFloat(d[0])) ||
				isNaN(parseFloat(d[1])) ||
				isNaN(parseFloat(d[2])) ||
				(d[0] < 1) || (d[0] > 31) ||
				(d[1] < 1) || (d[1] > 12) ||
				(d[2] < 1900)) {

				if (errors == "")
				{
					f.elements[i].focus();
					errors = "\n";
				}
				errors += "\n- " + "Fecha incorrecta: " + getName(e) + ". El formato es dd/mm/aaaa.";
			}
		}

		if (e.numeric || (e.min != null) || (e.max != null))
		{
			if (isblank(e))
				continue;
			var v = parseFloat(e.value);
			if (isNaN(v) ||
				((e.min != null) && (v < e.min)) ||
				((e.max != null) && (v > e.max))) {

				if (errors == "")
				{
					f.elements[i].focus();
					errors = "\n";
				}

				errors += "- El campo " + getName(e) + " debe ser numérico.";

				if (e.min != null)
					errors += " mayor que " + e.min;
				if (e.max != null && e.min != null)
					errors += " y menor que " + e.max;
				else if (e.max != null)
					errors += " menor que " + e.max;
				errors += ".\n";
			}
		}

		if ((e.file) && !(isblank(e)))
		{
			if(validar_nombre_file(e))
			{
				errors += "El nombre del archivo de " + getName(e) + " posee caracteres invalidos. Recuerde que no son caracteres válidos los acentos, signos de puntuación ni la letra eñe. Si puede utilizar letras, números, esacios y guiones.";
			}
		}


	}

	if (!empty_fields && !errors) return true;

	msg = "Se detectaron los siguientes errores. Por favor corríjalos e intente nuevamente.\n\n";

	if (empty_fields) {
		msg += "Los siguientes campos obligatorios están vacíos: " + empty_fields + "\n";

		if (errors) msg += "\n";
	}

	msg += errors;
	alert(msg);
	return false;
}

///////////////////////////////////////////////////////////////
function validar_nombre_file(archivo)
{
	var lRet = false;
	cadena = new String(archivo.value);
	var posicion = cadena.lastIndexOf("\\");
	cadena2 = cadena.substr(posicion+1);

	var struct = new RegExp("^([a-zA-Z0-9_]|-|\\.| )+$","i");
	if(0 != cadena2.search(struct))
	{
		lRet = true;
	}
	return lRet;
}
