//SVNet - Junio 2006 - Gerardo Meléndez - gerardomelendez@gmail.com/gmelendez@gawab.com

//quitar blancos
function str_trim(str){
	return str.replace(/^\s*|\s*$/g,"");
}

//hacer trim a todos los elementos de tipo text y password de un form
function form_trim(f){
	var idx;
	for(idx=0;idx<f.length;idx++)
		if(f.elements[idx].type=='text' || f.elements[idx].type=='password' || f.elements[idx].type=='textarea')
			f.elements[idx].value=f.elements[idx].value.replace(/^\s*|\s*$/g,"");
}

//mover un ìtem de un select a otro
function mover_item(origen,destino){
	var tam=destino.length;
	if(origen.selectedIndex>0){
		destino.options[tam]=new Option(origen.options[origen.selectedIndex].text);
		destino.options[tam].value=origen.options[origen.selectedIndex].value;
		origen.options[origen.selectedIndex]=null;
	}
	return false;
}

//mover todos los ìtems de un select a otro
function mover_todos(origen,destino){
	var tam=destino.length;
	var tam2=origen.length-1;
	while(tam2>=1){
		destino.options[tam]=new Option(origen.options[tam2].text);
		destino.options[tam].value=origen.options[tam2].value;
		origen.options[tam2]=null;
		tam2--;
		tam++;
	}
	return false;
}

//dado un combo, seleccionar todos sus items, excepto el primero...
function seleccionar_todos(combo){
	var idx;
	combo.options[0].selected=false;
	for(idx=1;idx<combo.length;idx++)
		combo.options[idx].selected=true;
}

//función que reeemplaza a window.open para poder validar la presencia de un popup blocker
function popup(url,nbr,prop){
	var w=null;
	w=window.open(url,nbr,prop);
	if(!w || (w.closed || !w.focus))
		alert("Su navegador tiene activado un 'popup blocker', es necesario que lo desactive para el buen funcionamiento del sistema.");
}

//el contenido de todos los campos es ''
function limpiar(f){
	var idx=0;
	while(idx<f.length){
		if(f.elements[idx].type=='text' || f.elements[idx].type=='password' || f.elements[idx].type=='textarea' || f.elements[idx].type=='hidden')
			f.elements[idx].value='';
		if(f.elements[idx].type=='select-one')
			f.elements[idx].selectedIndex=0;
		if(f.elements[idx].type=='checkbox')
			f.elements[idx].checked=false;
		idx++;
	}
	return false;
}

//¿un caracter es dígito?
function es_digito(d){
	return "0123456789".indexOf(d) == -1 ? 0 : 1//bajada pero comprensible
}

//validar una fecha
function validar_fecha(str){
	var dd, mm, aa;
	if(str.length==10 && es_digito(str.charAt(0)) && es_digito(str.charAt(1))
		&& es_digito(str.charAt(3)) && es_digito(str.charAt(4)) && es_digito(str.charAt(6))
		&& es_digito(str.charAt(7)) && es_digito(str.charAt(8)) && es_digito(str.charAt(9))
		&& (str.charAt(2)=='-' || str.charAt(2)=='/') && (str.charAt(5)=='-' || str.charAt(5)=='/')){
		//dos digitos para dia, dos para mes, cuatro para año, separados por - o /
		dd=eval(str.substring(0,2));
		mm=eval(str.substring(3,5));
		aa=eval(str.substring(6,10));
		if(dd>31 || dd==0 || mm>12 || mm==0 || aa<1965)//limite inferior de año: 1965
			return false;
		else//por el momento todo bien
			if(dd==31 && (mm==2 || mm==4 || mm==6 || mm==9 || mm==11))//estos meses no son de 31
				return false;
			else{
				if(dd==30 && mm==2)//malo
					return false;
				if(dd==29 && mm==2){//29 de febrero... solo si es bisiesto....
					if((aa%4)==0 && (aa%100)!=0)//divisible entre cuatro y no entre 100
						return true;
					if((aa%100)==0 && (aa%400)==0)//divisible entre 100 y entre 400 -> bisiesto
						return true;
					return false;
				}
				else
					return true;
			}
	}
	return false;
}

//función que verifica una pareja de fechas y a su vez valida su cronología
function validar_fechas_y_cronologia(fini,ffin){
	var err='';
	if(!validar_fecha(fini) && fini!='')
		err+='-Fecha inicial incorrecta\n';
	if(!validar_fecha(ffin) && ffin!='')
		err+='-Fecha final incorrecta\n';
	if(0==err.length && fini!='' && ffin!=''){ //si no ha caido en un nuevo error, verificar cronología
		var f1, f2;
		var campos=new Array();
		campos=fini.split('/');
		f1=campos[2] + campos[1] + campos[0];
		campos=ffin.split('/');
		f2=campos[2] + campos[1] + campos[0];
		if(eval(f1)>eval(f2))
			err+='-La fecha final debe ser posterior o igual a la fecha inicial\n';
	}
	return err;
}

function foco_a_(ff,cc){
	if(typeof document.forms[ff]!='undefined')
		if(typeof document.forms[ff].elements[cc]!='undefined')
			document.forms[ff].elements[cc].focus();
}

function agregar_a_combo(codigo,texto,combo){
	var tam=combo.length;
	if(codigo.value!=''){
		combo.options[tam]=new Option(texto.value);
		combo.options[tam].value=codigo.value;
		codigo.value=texto.value='';
	}
	return false;
}

function quitar_elem_seleccionado(combo){
	if(combo.selectedIndex>=0)
		combo.options[combo.selectedIndex]=null;
	return false;
}
