//////////////////////////////////////////////////////////
//                                                      //
//     Funciones para formularios en JavaSccript        //
//                 v 0.1 (21-06-2005)                   //
//                                                      //
//////////////////////////////////////////////////////////

// Variables globales
var isExplorer = (navigator.appName.search("Explorer") > 0) ;

// Cambiar idioma en un textarea traducible
function cambiarBloque(objId, banderaId){
	obj = document.getElementById(objId);
	bandera = document.getElementById(banderaId);
	if(!obj.idioma)	 		cambioCampo(objId, obj, bandera, 'es', 'en', 1);
	else if(obj.idioma == 1)	cambioCampo(objId, obj, bandera, 'en', 'de', 2);
	else if(obj.idioma == 2)	cambioCampo(objId, obj, bandera, 'de', 'fr', 3);
	else if(obj.idioma == 3)	cambioCampo(objId, obj, bandera, 'fr', 'it', 4);
	else if(obj.idioma == 4)	cambioCampo(objId, obj, bandera, 'it', 'fi', 5);
	else if(obj.idioma == 5)	cambioCampo(objId, obj, bandera, 'fi', 'sv', 6);
	else if(obj.idioma == 6)	cambioCampo(objId, obj, bandera, 'sv', 'da', 7);
	else if(obj.idioma == 7)	cambioCampo(objId, obj, bandera, 'da', 'es', 0);
}

// Funcion auxiliar de cambiarBloque (cambia idioma y setea el siguiente)
function cambioCampo(objId, obj, bandera, idG, idP, sigId){
	obj.idioma = sigId;
	bandera.src = rutaIco + "/banderas/" + idP + ".gif";
	document.getElementById(objId + idG).value = obj.value;
	obj.value = document.getElementById(objId + idP).value;
}

// Para inputs numéricos
function input_num(e) {
	if (isExplorer) key = e.keyCode ;
	else            key = e.which ;
	if ((key>=48 && key<=57) || key==0 || key==8) return true ;
	else return false;
}

// Para input numericos con puntos decimales
function input_num2(e) {
	if (isExplorer) key = e.keyCode ;
	else            key = e.which ;
	if ((key>=48 && key<=57) || key==0 || key==8 || key==46 || key==44) return true;
	else return false;
}

// Para input alfanumericos sin espacios (DNI/CIF)
function input_num3(e) {
	if (isExplorer) key = e.keyCode ;
	else            key = e.which ;
	if ((key>=48 && key<=57) || (key>=65 && key<=90) || (key>=97 && key<=122) || key == 0 || key == 8) return true ;
	else return false ;
}

// para telefonos (cifras y el + (prefijo))
function input_num4(e,val) {
   if (isExplorer) key = e.keyCode ;
	else            key = e.which ;
   if (val.length) {
      if ((key>=48 && key<=57) || key==0 || key==8) return true ;
      else return false;
   }
   else {
      if ((key>=48 && key<=57) || key==0 || key==8 || key==43) return true ;
      else return false;
   }
}

// Borra todas las opciones de un combo
function BorrarOpciones (sel) {
	cuenta = sel.options.length ;
  	for (i=cuenta-1; i>=0; i--) sel.options[i] = null;
}

// Añade opciones a un combo
function AddOpciones(sel,values,texts) {
	inicio = sel.options.length ;
	cuenta = values.length ;
  	for (i=0; i<cuenta; i++) {
		var o = new Option(texts[i],values[i]) ;
	  	sel.options[inicio+i] = o ;
  	}
}

// Comprueba un e-mail mediante expresion regular
function checkMail(temail){
    remail = /.+@.+\..+/ ;
	if(temail.search("@") < 0) return 1;
	else if(temail.search(remail) < 0) return 2;
	else return 0;
}

// Borra un input text
function borrarText(element) {
	element.value="";
}
// Desmarca un checkbox
function desmarcarCheck(element) {
	element.checked=false;
}
// Demarca un select
function desmarcarSelect(element) {
	element[0].selected = true;
}
	
// Marca todas las opciones de un select multiple
function marcarTodaLista(obj){
	for(var i = 0;i < obj.length;i++) obj.options[i].selected = true;
}

// Desmarca todas las opciones de un select multiple
function desmarcarTodaLista(obj){
	for(var i = 0;i < obj.length;i++) obj.options[i].selected = false;
}

// Marca/desmarca opciones en un select multiple
function rellenarListaMultiple(obj){
	var arrNValores;
	var intNPos;
	var valoresTemp = capturarValoresMarcados(obj);
	arrNValores = valoresTemp.split(",");
	intNPos = obj.selectedIndex;

	for(var i=0; i<obj.valoresAnt.length-1; i++){
		if(i != intNPos){
			if(obj.valoresAnt[i]==1)			obj.options[i].selected= true;
			else if(obj.valoresAnt[i]==0)		obj.options[i].selected= false;
		}else{
			if(obj.valoresAnt[intNPos]== 1)	obj.options[intNPos].selected = false;
			else							obj.options[intNPos].selected = true;
		}
	}
}

// Devuelve todas las opciones marcadas en un select multiple
function capturarValoresMarcados(obj){
	var strTemp = "";
	for(var i = 0;i < obj.length;i++){
		if(obj.options[i].selected == true) strTemp += "1,";
		else strTemp += "0,";
	}
	return strTemp;
}

// Captura los valores marcados y los guarda en el propio select multiple
function capturarValores(obj){
	var valores = "";
	valores = capturarValoresMarcados(obj);
	obj.valoresAnt = valores.split(",");
}


function in_array(elem,array) {
	for (var i=0; i<array.length; i++)
		if (array[i]==elem) return i;
	return -1;
}


function remove(elem,array) {
	for (var i=0; i<array.length; i++)
		if (array[i]==elem)
			array.splice(i,1);
	return array;
}

function resaltarSelectMultiple(nombre,texto) {

	var nSel = 0;
	var obj = document.f[nombre+'[]'];
	for (var i=0; i<obj.length;i++) {
		if (obj[i].checked) {
			nSel++;
			obj[i].parentNode.parentNode.style.backgroundColor = '#D8EBF1';
		}
		else {
			obj[i].parentNode.parentNode.style.backgroundColor = '';
		}
	}
	document.getElementById("lbl_"+nombre).innerHTML = texto+" <font color='"+(nSel>0?"#FF0000":"#CCCCCC")+"'>("+nSel+" marcado"+(nSel>1?"s":"")+")</font>";

	return nSel;
}

function desmarcarSM(nombre) {
	var obj = document.f[nombre+"[]"];
	for (var i=0; i<obj.length; i++)
		obj[i].checked = false;
}