function trim(cadena)
{
	var i;
	var cad2
	for(i=0;i<cadena.length;i++)
	{
		if(cadena.charAt(i) != " ")
			break;
	}
	cad2 = cadena.substring(i,cadena.length);
	for(i=cad2.length-1;i>=0;i--)
	{
		if(cad2.charAt(i) != ' ')
			break;
	}
	if(i<cad2.length-1)
		cad2 = cad2.substring(0,i+1);
	return cad2;
}

function vacio(obj,msg)
{
	if(!(/\S/.test(obj.value))) { if(msg != "") { alert(msg); } obj.focus(); return true; }
	else return false;
}

function cancela_registro()
{
	if(window.confirm("¿ Desea cancelar su registro ?") == true)
	{
		window.location.href = "empresa_salir.php";
	}
	else
		return;
}

function cerrar_sesion()
{
	if(window.confirm("¿ Desea cerrar su sesión ?") == true)
	{
		window.location.href = "empresa_salir.php";
	}
	else
		return;
}

function abre_ayuda(tema)
{
	 window.open("ayuda.php?tema="+tema,"ayuda","width=600,height=520,menu=no,toolbar=no,scrollbars=yes");
}

function item_select(objselect,valor)
{
	if(objselect.options.length == 0) return;
	objselect.value = valor;
	if(objselect.selectedIndex == -1)
		objselect.selectedIndex = 0;
}

function n_inst(obj)
{
	if(obj.length != null)
	{
		return obj.length;
	}
	else
		if(obj != null)
			return 1;
		else
			return 0;
}

function select_option(obj,valor)
{
	var c = obj;
	var n = n_inst(c);
	for(i = 0;i<n;i++)
	{
		if(c[i].value == valor)
		{
			c[i].checked = true;
			break;		
		}
	}
}

function select_checkbox(obj,valores)
{
	var c = obj;
	var n = n_inst(c);
	for(i = 0;i<n;i++)
	{
		for(j=0;j<valores.length;j++)
		{
			if(c[i].value == valores[j])
			{
				c[i].checked = true;
				break;		
			}
		}
	}
}

function validaNumero(objtxt,opcion)
{
	if(opcion == 1)	// tlf
		var numOk = "1234567890- ()";	
	else
		var numOk = "1234567890";
		
	for(i=0;i<objtxt.value.length;i++)
	{
			if(numOk.indexOf(objtxt.value.charAt(i),0) == -1)
			{
				if(opcion == 1)
					alert("Por favor ingrese correctamente su número de teléfono.");
				else
					alert("Por favor ingrese correctamente su número de RUC.");
				objtxt.focus();
				return false;
			}
	} 
	return true;	
}

function validaEmail(objtxt)
{
	var charError = " ;/:<>*|&$!#()[]{}\'\"";
	var error = false;
	var email = trim(objtxt.value);

	for(i=0;i<email.length;i++)
	{
		if(charError.indexOf(email.charAt(i),0) != -1)
		{
			error = true;
			break;
		}
	}

	if(email.length <6) error = true;
	
	var indexarroba = email.indexOf("@",0);
	if(indexarroba == -1)	error = true;
	else
	{
		var indexpunto = email.indexOf(".",indexarroba+1);
		if(indexpunto == -1)
			error = true;
		else
		{
			if(indexpunto == indexarroba+1)
				error = true;
			if(indexpunto >= email.length-2)
				error = true;
		}
	}
	
	if(error)
	{
		alert("la dirección de correo ingresada no es válida.");
		objtxt.focus();		
		return false;
	}
	else
	{
		objtxt.value = email;	
		return true;
	}
}

function fecha_actual()
{
	var d = new Date;
	var meses = Array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
	var dias = new Array("Domingo", "Lunes", "Martes","Miércoles","Jueves", "Viernes","Sábado");
	
	fecha_str = dias[d.getDay()].substr(0,3) + ". " + d.getDate()+" de "+meses[d.getMonth()]+" del "+d.getYear();
	return fecha_str;
}

