/**
 *  Funciones referentes al cliente
 */
function removeSearch()
{
    if($( 'searchClientesDiv' ) )
    {
        $('searchClientesDiv').remove();
    }
}
function closeSearch()
{
    if( $( 'searchClientesDiv' ) )
    {
        setTimeout( "removeSearch()", 1000 );
    }
}
function chargeClieData( clie_id )
{
    new Ajax( urlProcesos,
        {   'method' : 'get',
            'data'      : { 'action' : 'charge_clie_data', 'type' : 'inputs', 'clie_id' : clie_id},
            'evalScripts' : true
        }
    ).request()
}
function validaNombre( el )
{
    searchBy( el, 'clie_nombre' );
}
function validaMail( el )
{
    searchBy( el, 'clie_email' );
}
function searchBy( el, by )
{
    if( el.value.length > 3 )
    {
        new Element( 'div', { 'id' : 'searchClientesDiv' } ).inject( document.body );
        $( 'searchClientesDiv' ).setStyles(
            {   'position' : 'absolute',
                'top'       :   ( $( el.id ).getPosition().y.toInt() + 18 ) + 'px',
                'left'      :   $( el.id ).getPosition().x
            }
        );
        $( 'searchClientesDiv' ).setHTML( 'Buscando....' );
        var aja = new Ajax( urlProcesos,
            {   'method'    :   'get',
                'data'      :   { 'action' : 'seach_clie_maes', 'search_by' : by, 'search_val' : el.value },
                'update'    :   $( 'searchClientesDiv' ),
                'evalScripts'   :   true
            }
        ).request();
    }
    else
    {
        if( $( 'searchClientesDiv' ) ) $( 'searchClientesDiv' ).remove();
    }
}
function newCotiza()
{
    var obj = {
        'action'    :   'new_cotiza'
    };
    generalProcess( obj );
}
    
/**
 *
 */
function resetForm()
{
    var frm = $( 'cotizaFormStep1' );
    for( var i = 0; i < frm.length; i++ )
    {
        if( $( frm[i].id ) )
        {
            $( frm[i].id ).value = '';
        }
    }
    $( 'action' ).value = 'cotiza_step_2';
}
function getCotizaStep2()
{
    var aja = new Ajax( urlProcesos,
        {   'method'    :   'post',
            'data'      :   $( 'cotizaFormStep1' ),
            'evalScripts' : true
        }
    ).request();
}
/**
 *
 */
function selectPlan()
{
    new Element( 'div', { 'id' : 'selectorPlanesDiv' } ).inject( document.body );
    $( 'selectorPlanesDiv' ).setStyles(
        {   'position'  :   'absolute',
            'width'     :   '270px',
            'height'    :   '220px'
        }
    );
    moveToCenter( 'selectorPlanesDiv',{} );
    var obj = {};
    if( arguments[ 0 ] )
    {
        obj = arguments[ 0 ]
    }
    obj.action = 'select_plan';
    var aja = new Ajax( urlProcesos,
        {   'method'    :   'get',
            'data'      :   obj,
            'update'    :   $( 'selectorPlanesDiv' ),
            'evalScripts' : true,
            'onComplete' : function()
            {
            }
        }
    ).request();
}
/**
 *
 */
function getCotiValues( obj )
{
    if( obj.plan )
    {
            $( 'plan_id_' + obj.column ).value = obj.plan;
            obj.phones = $( 'totalPhones' ).value;
            var pi = $$( 'input.phones_ids' );
            var cad = '';
            for( var i = 0; i < pi.length; i++ )
            {
                cad += pi[i].id + ':' + pi[i].value + ',';
            }
            obj.pIds = cad;
    }
    if( obj.equi > 0 )
    {
        var cad = '';
        var pl = $$( 'input.planes_ids' );
        for( var i = 0; i < pl.length; i++ )
        {
            cad += pl[i].id +':' + pl[i].value + ',';
        }
        obj.planesIds = cad;
        obj.planes = $( 'totalPlanes' ).value;
    }
    obj.action = 'get_coti_values';
    obj.plazo = $( 'plazo' ).value;
    var aja = new Ajax( urlProcesos,
        {   'method'    :   'get',
            'data'      :   obj,
            'evalScripts' : true
        }
    ).request();
}
/**
 *
 */
function changeTerm()
{
    var planes = $$( 'input.planes_ids' );
    var col = 1;
    for( var i = 0; i < planes.length; i++ )
    {
        if( planes[ i ] )
        {
            getCotiValues(
                {   'plan' : planes[ i ].value,
                    'column' : col
                }
            );
            col++;
        }
    }
}
/**
 *
 */
function scrollingDivs( el )
{
    var nt = 6
    var eq = $$( 'div.scrollerEquiDiv' );
    $( 'cotiPlanesDiv' ).scrollTo( el.scrollLeft, 0 );
    for( var i = 0; i < eq.length; i++ )
    {
        $( eq[ i ].id ).scrollTo( el.scrollLeft, 0 );
    }
}
/**
 *
 */
function selectEqui()
{
    new Element( 'div', { 'id' : 'selectorEquiposDiv' } ).inject( document.body );
    $( 'selectorEquiposDiv' ).setStyles(
        {   'position'  :   'absolute',
            'width'     :   '270px',
            'height'    :   '220px'
        }
    );
    moveToCenter( 'selectorEquiposDiv',{} );
    var obj = {};
    if( arguments[ 0 ] )
    {
        obj = arguments[ 0 ]
    }
    obj.action = 'select_equi';
    var aja = new Ajax( urlProcesos,
        {   'method'    :   'get',
            'data'      :   obj,
            'update'    :   $( 'selectorEquiposDiv' ),
            'evalScripts' : true,
            'onComplete' : function()
            {
            }
        }
    ).request();
}
/**
 *
 */
function addPlan()
{
    var id = $( 'totalPlanes' ).value.toInt() + 1 ;
    var nW = ( 121 * id ) + 'px';
    var nId = 'newPlan' + id;

    $('totalPlanes').value = id;
    $( 'divScroll' ).setStyle('width', nW );
    $( 'planesColumns' ).setStyle('width', nW );

    phonesScroll( nW );
    var DIV = new Element( 'div', {'id':nId } ).inject( $('planesColumns') );

    $( nId ).setStyles(
        {   'float' : 'left',
            'width' :   '121px'
        }
    );
    var aja = new Ajax( urlProcesos,
        {   'method'    :   'get',
            'data'      :   {   'action':   'add_plan',
                                'id'    :   id
                            },
            'update'    :   $( nId ),
            'onComplete' : function()
            {
                validatePhonesDivs();
            }
        }
    ).request();
}
/**
 *
 */
function deletePlan()
{
    var id = $( 'totalPlanes' ).value.toInt();
    if( id > 1 )
    {
        $( 'newPlan' + id ).remove();
        $('totalPlanes').value = ( id - 1 );
        var nW = 121 * ( id - 1 );
        $( 'divScroll' ).setStyle('width', nW );
        $( 'planesColumns' ).setStyle('width', nW );
        phonesScroll( nW );
    }
}
/**
 *
 */
function validatePhonesDivs()
{
    var tPh = $( 'totalPhones' ).value.toInt();
    var tPl = $( 'totalPlanes' ).value.toInt();
    for( var i = 1; i <= tPh; i++ )
    {
        for( var ii = 1; ii <= tPl; ii++ )
        {
            var id = 'phones_' + i + '_' + ii;
            if( !$( id ) )
            {
                var idAnt = 'phones_' + i + '_' + ( ii - 1);
                if( $( idAnt ) )
                {
                    var clon = $( idAnt ).clone(false).injectAfter( idAnt );
                    $( clon ).id = id;
                }
            }
        }
    }
}
/**
 *
 */
function phonesScroll( W )
{
    var ph = $$( 'div.toScrollerDiv' );
    for( var i = 0; i < ph.length; i++ )
    {
        $( ph[ i ] ).setStyle( 'width', W );
    }
}
/**
 *
 */
function addCotiEqui()
{
    var id = $( 'totalPhones' ).value.toInt() + 1;
    $( 'totalPhones' ).value = id;
    var planes = $( 'totalPlanes' ).value.toInt();
    var nId = 'newPhone' + id ;
    var DIV = new Element( 'div', {'id':nId } ).inject( $('cotiPhonesDiv' ) );
    var aja = new Ajax( urlProcesos,
            {   'method'    :   'get',
            'data'      :   {   'action':   'add_phone',
                                'id'    :   id,
                                'planes' : planes
                            },
            'update'    :   $( nId )
            }
    ).request();
}
/**
 *
 */
function deleteCotiEqui()
{
    var id = $( 'totalPhones' ).value.toInt();
    if( id > 1 )
    {
        $( 'newPhone' + id ).remove();
        $('totalPhones').value = ( id - 1 );
    }
}

function printCotiza()
{
    var x   =   $( 'totalPlanes' ).value.toInt();
    var y   =   $( 'totalPhones' ).value.toInt();
    var planes = [];
    var phones = [];
    var params = "?";
    for( var i = 1; i <= x; i++ )
    {
        if( $( 'plan_id_' + i ).value != '' )
        {
            planes.push( $( 'plan_id_' + i ).value );
        }
    }
    for( var i = 1; i <= y; i++ )
    {
        if( $( 'equi_id_' + i ).value != '' )
        {
            phones.push( $( 'equi_id_' + i ).value );
        }
    }
    params += "planes=" + planes + "&equipos=" + phones + "&plazo=" + $( 'plazo' ).value + "&display=xls";
    if( x < 1 || planes.length < 1)
    {
        alert('Necesita seleccionar algun plan');
        return;
    }
    if( y < 1 || phones.length < 1 )
    {
        alert(' Necesita seleccionar algun telefono' );
        return;
    }
    //window.open('print_cotiza.php' + params ,'Cotiza','width=600, height=400' );
    if( arguments[0] =='mail' )
    {
        generalProcess( {   'action'    :   'send_coti_mail', 'params' : params });
    }
    else
    {
        window.location = 'print_cotiza.php' + params;
    }
}
function sendCotiza()
{
    printCotiza( 'mail' );
}
