 function checkemail(strFieldValue){
  var str=strFieldValue
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (filter.test(str))
   return true
  else{
   /*if(strAlert != "")alert(strAlert);*/
   return false
  }
}

function revisarDigito( dvr )
{
	dv = dvr + ""
	if ( dv != '0' && dv != '1' && dv != '2' && dv != '3' && dv != '4' && dv != '5' && dv != '6' && dv != '7' && dv != '8' && dv != '9' && dv != 'k'  && dv != 'K')
	{
		alert("Debe ingresar un digito verificador valido");
	/*	window.document.forms[0].rut.focus();
		window.document.forms[0].rut.select();		*/
		return false;
	}
	return true;
}

function revisarDigito2( crut )
{
	largo = crut.length;
	if ( largo < 2 )
	{
		alert("Debe ingresar el rut completo")
		/*window.document.forms[0].rut.focus();
		window.document.forms[0].rut.select();	*/
		return false;
	}
	if ( largo > 2 )
		rut = crut.substring(0, largo - 1);
	else
		rut = crut.charAt(0);
	dv = crut.charAt(largo-1);
	revisarDigito( dv );

	if ( rut == null || dv == null )
		return 0

	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() )
	{
		/*alert("EL rut es incorrecto")*/
		/*window.document.forms[0].rut.focus();
		window.document.forms[0].rut.select();	*/
		return false
	}

	return true
}

function Rut(texto)
{
	var tmpstr = "";
	for ( i=0; i < texto.length ; i++ )
		if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
			tmpstr = tmpstr + texto.charAt(i);
	texto = tmpstr;
	largo = texto.length;

	if ( largo < 2 )
	{
		/*alert("Debe ingresar el rut completo")*/
		/*window.document.forms[0].rut.focus();
		window.document.forms[0].rut.select();	*/
		return false;
	}

	for (i=0; i < largo ; i++ )
	{
		if ( texto.charAt(i) !="0" && texto.charAt(i) != "1" && texto.charAt(i) !="2" && texto.charAt(i) != "3" && texto.charAt(i) != "4" && texto.charAt(i) !="5" && texto.charAt(i) != "6" && texto.charAt(i) != "7" && texto.charAt(i) !="8" && texto.charAt(i) != "9" && texto.charAt(i) !="k" && texto.charAt(i) != "K" )
 		{
			/*alert("El valor ingresado no corresponde a un R.U.T valido");*/
			/*window.document.forms[0].rut.focus();
			window.document.forms[0].rut.select();	*/
			return false;
		}
	}

	var invertido = "";
	for ( i=(largo-1),j=0; i>=0; i--,j++ )
		invertido = invertido + texto.charAt(i);
	var dtexto = "";
	dtexto = dtexto + invertido.charAt(0);
	dtexto = dtexto + '-';
	cnt = 0;

	for ( i=1,j=2; i<largo; i++,j++ )
	{
		//alert("i=[" + i + "] j=[" + j +"]" );
		if ( cnt == 3 )
		{
			dtexto = dtexto + '.';
			j++;
			dtexto = dtexto + invertido.charAt(i);
			cnt = 1;
		}
		else
		{
			dtexto = dtexto + invertido.charAt(i);
			cnt++;
		}
	}

	invertido = "";
	for ( i=(dtexto.length-1),j=0; i>=0; i--,j++ )
		invertido = invertido + dtexto.charAt(i);

	/*window.document.forms[0].rut.value = invertido.toUpperCase()		*/

	if ( revisarDigito2(texto) )
		return true;

	return false;
}


function carga_pagina(pagina,div)
{
    $("#"+div).load(pagina, { }, function(){ });
}

function carga1(pagina,div,input_dato)
{
    $("#"+div).load(pagina, {dato: input_dato }, function(){ });
}

function valida_login() {
    var_nombre = $('#nombre').val();
    var_rut = $('#rut').val();
    var_mail = $('#mail').val();
    var_telefono = $('#telefono').val();
    var_rut_login = $('#rut_login').val();


    if (var_rut_login != '') {
        document.frm_login.submit();
    }
    else
    {

        valida_rut = Rut(var_rut);
        valida_mail = checkemail(var_mail);

        error = 0;
        tipo = 0;
        mensaje_error = '';

        if (var_nombre == '') {
            error = 1;
            mensaje_error = mensaje_error + "Nombre\n";
        }
        if (var_rut == '') {
            error = 1;
            mensaje_error = mensaje_error + "Rut\n";
        }
        if (valida_mail == '') {
            error = 1;
            mensaje_error = mensaje_error + "Mail\n";
        }
        if (var_telefono == '') {
            error = 1;
            mensaje_error = mensaje_error + "Tel\xe9fono\n";
        }


        if (valida_rut == false) {
            error = 1;
            mensaje_error = mensaje_error + "Rut Incorrecto\n";
        }



        if (error == 0) {

            document.frm_login.submit();

        } else {
            alert('Por Favor verifique \n' + mensaje_error);
        }    
    
    }
}


function valida_invitapagina() {
    var_nombre = $('#nombre').val();
    var_correo = $('#correo').val();
    var_nombre_amigo = $('#nombre_amigo').val();
    var_correo_amigo = $('#correo_amigo').val();

    
    
    valida_mail = checkemail(var_correo);
    valida_mail_amigo = checkemail(var_correo_amigo);

    error = 0;
    tipo = 0;
    mensaje_error = '';

    if (var_nombre == '') {
        error = 1;
        mensaje_error = mensaje_error + "Nombre\n";
    }
    if (valida_mail == '') {
        error = 1;
        mensaje_error = mensaje_error + "Mail\n";
    }
    if (var_nombre_amigo == '') {
        error = 1;
        mensaje_error = mensaje_error + "Nombre Amigo\n";
    }
    if (valida_mail_amigo == '') {
        error = 1;
        mensaje_error = mensaje_error + "Mail Amigo\n";
    }


    if (error == 0) {

        document.frm_invita.submit();

    } else {
        alert('Por Favor verifique \n' + mensaje_error);
    }


}
