String.prototype.strip_tags = function(){
tags = this;
stripped = tags.replace(/<\/?[^>]+>/gi, '');
return stripped;
}
String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,'') }

// Devuelve el "value" de un "input" en funcion de su tipo
// NOTA:	faltan tipos por definir
function obtenerValor(formulario, campo, tags) {
	var s
	if (input = document.forms[formulario].elements[campo]) {
		if (input.type == undefined)
			tipo = document.forms[formulario].elements[campo][0].type
		else
			tipo = input.type;
		switch (tipo) {
			case "text":
			case "password":
			case "textarea":
			case "hidden":			
				if (tags) {
					return input.value.trim();
				}else { return input.value.strip_tags().trim();}
				break
			case "checkbox":		if (input.checked)
											return input.value
										break
			case "radio":			if (input.length) {
										for (s = 0; s < input.length; s++) {
											if (input[s].checked) {
												return input[s].value
											}
										}
									} else {
										if (input.checked) return input.value
									}
                                    return false;
									break
			case "select-one":	return input.options[input.selectedIndex].value
										break
			case "select-multiple": 
				var resul = '';
				for (var ind = 0; ind < input.options.length; ind++) {
					if (input.options[ind].selected) resul += input.options[ind].value + "-"; 
				}
				resul = resul.substring(0, resul.length - 1);
				return resul;
				break;

			case "file": 		return input.value
										break

			default: 			return false
		}
	} else {
		return false
	}
}

// Establece (o selecciona) el valor de un "input" en funcion de su tipo
// NOTA:	faltan tipos por definir
function cambiarValor(formul, elem, valor) {
	var i;
	if (input = document.forms[formul].elements[elem]) {
		if (input.type == undefined)
			tipo = document.forms[formul].elements[elem][0].type
		else
			tipo = input.type
		
		switch (tipo) {
			case "text":
			case "hidden":
			case "textarea":	input.value = valor
								break
			case "checkbox":	input.checked = (input.value == valor)
								break
			case "radio":		if (input.length) {
									for (s = 0; s < input.length; s++) {
										if (input[s].value == valor) {
											input[s].checked = true;
										} else input[s].checked = false;
									}
								} else {
									if (input.value == valor) input.checked = true;
									else input.checked = false;
								}
								break
			case "select-one":	for (i = 0; i < input.length; i++) {
									if (input.options[i].value == valor) {
										input.selectedIndex = i
									}
								}
								break
			case "select-multiple":  // NO ESTA PROBADA!!!
				for (var ind = 0; ind < elem.options.length; ind++) {
					for (ind2 = 0; ind2 < valor.length; ind2++) {
						if (valor[ind2] == elem.options[ind]) 
							elem.options[ind].selected = true;
					}
				}
				break;
				
			default:			return false
		}
	} else {
		return false
	}
}

// Asigna el foco a un "input" teniendo en cuenta su tipo
// NOTA:	faltan casos por definir
function foco(formulario, campo) {
	if (input = document.forms[formulario].elements[campo]) {
		if (input.type == undefined)
			tipo = document.forms[formulario].elements[campo][0].type
		else
			tipo = input.type
		switch (tipo) {
			case "radio":	if (input[0])
									input[0].focus()
								break
			default:			input.focus()
		}
	} else {
		return false
	}
}

// Devuelve un vector con los nombre de todos los campos del formulario
function nombres_elementos(formulario) {
	var e
	var res = new Array()
	for (e = 0; e < document.forms[formulario].elements.length; e++) {
		res[res.length] = document.forms[formulario].elements[e].name
	}
	return res
}

// Elimina todas las opciones de un "select"
function vaciarSelect(sel) {
	while (sel.options.length > 0) {
		sel.options[0] = null
	}
}

// Aņade una "option" seleccionada al final de un "select"
function agnadirOpcion(sel, option) {
	sel.options[sel.options.length] = new Option(option[0], option[1])
}

// Select anidado de categorias y subcategorias de Ofertas
function cargar_subcategorias() {
  var sc = document.getElementById('sel_categoria')
  var padre = sc.options[sc.selectedIndex].value
  var ss = document.getElementById('sel_subcategoria')

  vaciarSelect(ss)
  if (subcat = eval("subcategorias_" + padre)) {
    for (i = 0; i < subcat.length; i++) {
      agnadirOpcion(ss, subcat[i])
    }
  }
}

