<?php
use_helper ( 'UrlFunctions' );
$sErrorNickname = "";
$sErrorPasswor = "";
// temporal
$sMessgaeError = "";

if ( isset ( $_POST ['commit'] ) )
{
    $o_MyLoginValidator = new myLoginValidator ();
    $preLogin = $o_MyLoginValidator->execute ( $_POST ['nickname'], $_POST ['password'] );
    if ( $preLogin === true )
    {
        $o_businessAdapter = new businessAdapter();
        $o_businessAdapter->setIdCompania( $IdCompania );
        if( isset( $IdSucursal ) )
        {
            $o_businessAdapter->setIdSucursal( $IdSucursal );
        }
        $o_businessAdapter->signInWithBusiness();
    }
    elseif ( $preLogin == "2" )
    {
        $sMessgaeError = "<font color=red><b>Error con el c&oacute;digo ingresado</b></font>";
    }
    elseif ( ! $preLogin )
    {
        $sMessgaeError = "<font color=red><b>Error al iniciar sesi&oacute;n</b></font>";
    }
}
?>
<script>
mostrarMensajeError = function( s_MensajeError )
{
    $( "#resultContent" ).html( '<div id="mensaje_error_content" class="error">' + s_MensajeError + '</div>' );
    setTimeout( function(){ $( "#mensaje_error_content" ).fadeOut( 800 ).fadeIn( 800 ).fadeOut( 500 ).fadeIn( 500 ).fadeOut( 300 ); }, 4000 );
}
</script>
<script>
validarIngreso = function()
{
    var IdCompania = $( "#IdCompania" ).val();
    
    
    if( IdCompania == "" || !IdCompania )
    {
        mostrarMensajeError( "Se debe de seleccionar una empresa." );
        
    }
    else
    {
        $( "#myFormLogin" ).submit();
        console.log( IdCompania );
    }
}
</script>
<script>
cargarCboSucursal = function( id_usu )
{ 
    $( '#IdSucursal' )[0].options.length=0;
    
    publicForSelectAjaxJSON
    ( 
        '<?php echo buildUrl( 'user/request', 'block=001') ?>', 
        '&tiporeq=lst_sucu&nickname=' + id_usu, 
        'IdSucursal',
        function()
        {
            ordernarSelect( "IdSucursal" );
        }
    );
};
</script>
<style>
label
{
    margin-right: 0;
}
.grid
{
    border: 1px solid #77d5f7;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px;
    border-bottom-left-radius: 5px;
}
.grid th 
{
	border-color: #77d5f7;
    background-color: #0a7db1;
    font-weight: normal;
    font-size: 11px;
    font-family: Verdana,Arial,sans-serif;
    height: 24px;
    padding: 0 2px;
}
.grid td 
{
    border-left: 0 none;
    border-right-color: #77d5f7;
    border-right-style: solid;
    border-right-width: 1px;
    border-bottom-color: #77d5f7;
    border-bottom-style: solid;
    border-bottom-width: 1px;
    padding: 0 2px;
    /*white-space: pre;*/
    height: 22px;
    /*border: 1px solid #77d5f7;*/
}
select{ padding: 2px; }
</style>

<table class="cuerpo" width="100%" border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td valign="top">
        
            <table style="border: 1px solid #e1e1e1; background-color: #fff; box-shadow: 0px 2px 5px #a5a5a5;" cellpadding="0" cellspacing="0" align="center" width="25%">
                <tr>
                    <td height="35" align="center" style="text-transform: uppercase; color: rgb(0, 161, 203); padding: 28px 0px 0px; font-size: 1.5em;" colspan="3">
                        INGRESO AL SISTEMA
                    </td>
                </tr>
                <tr>
                    <td valign="top">
                        <table cellpadding="0" cellspacing="0" align="center">
                            <tr>
                                <td>&nbsp;</td>
                            </tr>
                            <tr>
                                <td>
                                    <?=addImgButton( "sesion.png", array( "src"=>"common/", "style"=>"margin-left: 20px;" ) ); ?>
                                </td>
                            </tr>
                        </table>
                    </td>
                    <td>
                        <table cellpadding="0" cellspacing="0" width="100%">
                            <tr>
                                <td valign="top">
                                    <div id="login">
                                        <div style="padding: 10px;"><?=$sMessgaeError?></div>
                                        <div id="resultContent" style=""></div>
                                        <form id="myFormLogin" name="myFormLogin" method="post" action="">
                                            <input type="hidden" name="commit" value="ok">
                                            <div>
                                                <h5>Usuario:</h5>
                                                <p>
                                                    <input type="text" name="nickname" id="nickname" value="" maxlength="30" class="texto" onfocus="this.select();" />
                                                    <script type="text/javascript">
                                                    $( "#nickname" )
                                                    .blur
                                                    (
														function( obj )
														{
															var value = obj.target.value;
															$( '#IdCompania' )[0].options.length=0;
															executeAjaxUpdater
															(
                                                                'content_data',
                                                                '<?php echo buildUrl( 'user/request' ) ?>',
                                                                'tiporeq=lst_comp&nickname=' + value, 
                                                                function()
                                                                {
                                                                    console.log( "Request: OK" );
                                                                }
															);
														}
                                                    )
                                                    </script>
                                                </p>
                                            </div>
                                            <div>
                                                <h5>Contrase&ntilde;a:</h5>
                                                <p>
                                                    <input type="password" name="password" id="password" value="" maxlength="30" class="texto" />
                                                </p>
                                            </div>
                                            <div id="content_data" style="display: inline;">
                                                <div>
                                                    <h5>Empresa:</h5>
                                                    <p>
                                                        <select style="width: 240px;" name="IdCompania" id="IdCompania" >
                                                        </select>
                                                    </p>
                                                </div>
                                            </div>
                                            <div align="right" style="padding: 20px 0px 0px 0px">
                                                <span style="margin-right: 171px;">
                                                    <input type="button" value="Ingresar" class="button" onclick="validarIngreso();" />
                                                </span>
                                            </div>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
            
            <div id="content_alert" style="text-align: center;">
                
            </div>
            
            
        </td>
    </tr>
    <tr>
        <td valign="top">
            <table cellpadding="0" cellspacing="0" width="97%" border="0" height="450">
                <tr>
                    <td style="vertical-align: bottom; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; color: #333; text-align: right;">Copyright &copy; <?=date("Y")?>. Todos los derechos reservados.</td>
                <tr>
            </table>
        </td>
    <tr>
</table>
<script type="text/javascript">
$( document ).bind
(
    'keyup', 
    function( e ) 
    {
        if( e.which === 13 ) //ESCAPE
        {
        	validarIngreso();
        }
    }
);
</script>
<script>
$( "#nickname" ).focus();
</script>