function validar()
{
function _substr(str,ini){
  var v='';
  for(i=ini;i<str.length;i++) v+=str.charAt(i);
  return v;
}
function _substring(str,ini,fin){
  var v='';
  var bound=(fin>str.length) ? str.length : fin;
  for(i=ini;i<bound;i++) v+=str.charAt(i);
  return v;
}
// elimina Espacios vacíos pricipio y fin
function trim(str)
{
  var ini=0;
  var fin=str.length-1;
  while(ini<=fin && str.charAt(ini)==" ")
     ini++;
  if (ini<=fin)
  {
     while(str.charAt(fin)==" ") fin--;
     if (fin<str.length-1)
     {
        fin++;
        return _substring(str,ini,fin);
     }
     else return _substr(str,ini);
  }
  else return '';
}
//---------------------seleccione curso
	 if(window.document.form1.cur.value == "-2")
	  {
		alert("Seleccione curso");
		window.document.form1.cur.focus();
		return false;
      }

//-------------------------rut
   if(trim(window.document.form1.Rut.value) == "")
	  {
		alert("Ingrese Rut");
		window.document.form1.Rut.focus();
		return false;
      }

   if (!validar_rut(window.document.form1.Rut.value))
	{
		alert("¡Error! Rut incorrecto");
                window.document.form1.Rut.select();
		window.document.form1.Rut.focus();
		return false;
	}
	window.document.form1.Rut.value = eliminar_puntos(window.document.form1.Rut.value);

//------------------------nombre
	if(trim(window.document.form1.Nombre.value) == "")
	  {
		alert("Ingrese nombre");
		window.document.form1.Nombre.focus();
		return false;
      }
     window.document.form1.Nombre.value = trim(window.document.form1.Nombre.value);
//-------------------------apellido
	if(trim(window.document.form1.Apellido.value) == "")
	  {
		alert("Ingrese Apellido");
		window.document.form1.Apellido.focus();
		return false;
      }
     window.document.form1.Apellido.value = trim(window.document.form1.Apellido.value);
//---------------------------------------direccion
	 if(trim(window.document.form1.dir.value) == "")
	  {
		alert("Ingrese dirección");
		window.document.form1.dir.focus();
		return false;
      }
     window.document.form1.dir.value = trim(window.document.form1.dir.value);
//-------------telefono
	 if(trim(window.document.form1.Telefono.value) == "")
	  {
		alert("Ingrese teléfono");
		window.document.form1.Telefono.focus();
		return false;
      }
	 window.document.form1.Telefono.value = trim(window.document.form1.Telefono.value);

//------------rut empresa
/*if(trim(window.document.form1.Rut2.value) == "")
	  {
		alert("Ingrese Rut empresa");
		window.document.form1.Rut2.focus();
		return false;
      }

   if (!validar_rut(window.document.form1.Rut2.value))
	{
		alert("¡Error! Rut incorrecto");
                window.document.form1.Rut2.select();
		window.document.form1.Rut2.focus();
		return false;
	}
	window.document.form1.Rut2.value = eliminar_puntos(window.document.form1.Rut2.value);
	//-----------------------empresa
if(trim(window.document.form1.Empresa.value) == "")
	  {
		alert("Ingrese empresa o institución");
		window.document.form1.Empresa.focus();
		return false;
      }
     window.document.form1.Empresa.value = trim(window.document.form1.Empresa.value);
//----------------------direcion empresa
	 if(trim(window.document.form1.dir2.value) == "")
	  {
		alert("Ingrese dirección empresa");
		window.document.form1.dir2.focus();
		return false;
      }
     window.document.form1.dir2.value = trim(window.document.form1.dir2.value);
//-------------telefono empresa
if(trim(window.document.form1.Telefono2.value) == "")
	  {
		alert("Ingrese teléfono empresa");
		window.document.form1.Telefono2.focus();
		return false;
      }
	 window.document.form1.Telefono2.value = trim(window.document.form1.Telefono2.value);
//------------mail */
	 if(trim(window.document.form1.Email.value) == "")
	{
		alert("Ingrese su E-mail");
		window.document.form1.Email.focus();
		return false;
		}
		else
        if (window.document.form1.Email.value.indexOf('@', 0) == -1 ||
          window.document.form1.Email.value.indexOf('.', 0) == -1)
           { 
           alert("Dirección de E-mail inválida"); 
              window.document.form1.Email.focus(); 
              return false; 
           }
     window.document.form1.Email.value = trim(window.document.form1.Email.value);
	 //---------------------forma de pago
	 if(window.document.form1.cur2.value == "nada")
	  {
		alert("Seleccione forma de pago");
		window.document.form1.cur2.focus();
		return false;
      }
	//-----------------------------------------------------------------------------------

function validar_rut( crut )
{
  var tmpstr = "";

  for ( i=0; i < crut.length ; i++ )
    if ( crut.charAt(i) != ' ' && crut.charAt(i) != '.' && crut.charAt(i) != ',' && crut.charAt(i) != '-' )
      tmpstr = tmpstr + crut.charAt(i);
  crut = tmpstr;

  largo = crut.length;
  if ( largo < 2 )
  {
    return false;
  }

  if ( largo > 2 )
    rut = crut.substring(0, largo - 1);
  else
    rut = crut.charAt(0);
  dv = crut.charAt(largo-1);

  var dvr = '0';

  suma = 0;
  mul  = 2;

  for (i= rut.length -1 ; i >= 0; i--)
  {
    suma = suma + rut.charAt(i) * mul;
    if (mul == 7)
      mul = 2;
    else    
      mul++;
  }

  res = suma % 11;
  if (res==1)
    dvr = 'k';
  else if (res==0)
    dvr = '0';
  else
  {
    dvi = 11-res;
    dvr = dvi + "";
  }

  if ( dvr != dv.toLowerCase() )
  {
    return false;
  }
  return true;
}
function eliminar_puntos(crut)
{
	var tmprut = "";
	var tmprut1 = "";
	
	for (i=0; i < crut.length; i++)
	{
		if (crut.charAt(i) != ' ' && crut.charAt(i) != '.' && crut.charAt(i) != ',' && crut.charAt(i) != '-')
		{
			tmprut = tmprut + crut.charAt(i);
		}
	}
	crut = tmprut;

	for (i=0; i < crut.length - 1; i++)
	{
		tmprut1 = tmprut1 + crut.charAt(i);
	}
	tmprut1 = tmprut1 + '-' + crut.charAt(crut.length - 1);

	crut = tmprut1;
	return crut;
}
 		window.document.form1.submit();
	return true;
}
