function LoginCrypto()
{
	this.E  = 3;
	this.N  = 391;

	this.CI = "A";
	this.CF = "A";
	this.CS = "A";

	this.pin = "";
	this.dni = "";
	this.usr = "";

	this.oper = "VALIDAC ";

    this.enc        = LoginCrypto_enc;
    this.encchar    = LoginCrypto_encchar;
    this.ASC        = LoginCrypto_ASC;
	this.getEncData = LoginCrypto_getEncData;

	this.setPIN     = LoginCrypto_setPIN;
	this.setDNI     = LoginCrypto_setDNI;
	this.setUSR     = LoginCrypto_setUSR;
	this.setOper    = LoginCrypto_setOper;

	this.getCI      = LoginCrypto_getCI;
	this.getCF      = LoginCrypto_getCF;
}


function LoginCrypto_setPIN( p )
{
	this.pin = p;
}


function LoginCrypto_setDNI( d )
{
	this.dni = d;
}


function LoginCrypto_setUSR( u )
{
	this.usr = u;
}


function LoginCrypto_setOper( o )
{
	this.oper = o;
}


function LoginCrypto_getCI()
{
	return this.CI;
}


function LoginCrypto_getCF()
{
	return this.CF;
}


function LoginCrypto_getEncData()
{
	var fmt = new LoginFormat();

    var encData = "";

    if ( this.oper == "VALIDAC " ) {
	     encData = this.enc( this.CI +
	               fmt.formatDNI( this.dni ) +
	               fmt.formatUSR( this.usr ) +
	               fmt.formatUSR( "" ) +
	               fmt.formatPIN( this.pin ) +
	               fmt.formatPIN( "" ) ) +
	               this.CF;
    }

    return encData;
}


function LoginCrypto_enc( tcstring )
{
    var lcstring  = tcstring;
    var lcencrypt = "";

    for ( var i = 0; i < lcstring.length; i++ ) {
          lcencrypt += this.encchar( lcstring.substr( i, 1 ), this.E, this.N );
          lcencrypt += this.CS;
    }

    return ( lcencrypt );
}


function LoginCrypto_encchar( tcchar, E, N )
{
    var F, G;
    var C = this.ASC( tcchar );

    if ( E % 2 == 0 ) {
         G = 1;
         for ( var i = 1; i <= E/2; i++ ) {
               F = (C * C) % N;
               G = (F * G) % N;
         }
    } else {
         G = C;
         for ( var i = 1; i <= E/2; i++ ) {
               F = (C * C) % N;
               G = (F * G) % N;
         }
    }

    return ( G );
}


function LoginCrypto_ASC( tcchar )
{
    return ( tcchar.charCodeAt( 0 ) );
}
