/**
 *  Gestor de productos
 */
function generalProdProcess( obj )
{
    if( !obj.method )       obj.method = 'get';
    if( !obj.evalScripts )  obj.evalScripts = true;
    if( obj.dataId )
    {   
        obj.data = $( obj.dataId );
    }
    else
    {
        obj.data    =   obj;
    }

    new Ajax( urlProductos, obj ).request();
}
/**
 *  IMPORTACION
 */
function getImportProductForm()
{
    $( 'filtraEquiposInput' ).disabled = 'disabled';
    $('hiddenIframe' ).setStyle('display','none' );
    if( $( 'prodImportDiv' ) ) $( 'prodImportDiv' ).remove();
    new Element( 'div',
        {   'id'    :   'prodImportDiv'   
        }
    ).inject( document.body );
    $( 'prodImportDiv' ).setStyles(
        {   
            'position'  :   'absolute',
            'top'       :   $( 'prodListTable' ).getPosition().y,
            'left'      :   $( 'prodListTable' ).getPosition().x,
            'width'     :   '760px',
            'height'    :   '450px',
            'background-color'  :   '#ffffff'
        }
    );
    var obj = {
        'action'    :   'prod_import_form',
        'update'    :   $( 'prodImportDiv' )
    };
    generalProdProcess( obj );
}
function cancelImport()
{
    $('hiddenIframe' ).setStyle('display','none' );
    $( 'hiddenIframe' ).src = urlProductos;
    $('filtraEquiposInput').disabled=false;
    $('prodImportDiv').remove();
}
/**
 *  Valida importacion
 */
function validaImportData()
{
    if( $( 'importTXT' ).value != '' || $( 'importFile' ).value != '' )
    {
        $( 'action' ).value = 'import_step_2';
        $( 'prodImportForm' ).submit();
        $( 'hiddenIframe' ).setStyles( 
            {   'position'  :   'absolute',
                'display'   :   'block',
                'top'       :   $( 'importTXTDiv' ).getPosition().y,
                'left'      :   $( 'importTXTDiv' ).getPosition().x,
                'width'     :   $( 'importTXTDiv' ).getStyle('width'),
                'height'    :   $( 'importTXTDiv' ).getStyle('height')
            }
        );
        showOver( 'hiddenIframe' );
        $( 'btnStep2' ).setStyle('display','none');
    }
    else
    {
        alert( 'Seleccione un archivo para importar los productos o \ncopie y pege la informacion desde un archivo de excel' )
    }
}
function importStep3()
{
    $( 'btnStep3' ).setStyle('display','none');
    window.hiddenIframe.document.forms[0].submit();
}
/**
 *  Carga el detalle del producto
 */
function chargeProdDeta( prod_id )
{
    var obj = {
        'action'    :   'prod_deta_form',
        'prod_id'   :   prod_id,
        'update'    :   'prodDetaFormDiv'
    };
    generalProdProcess( obj );
}

/**
 *  Pendiente
 */
function validaDataToSave()
{
    $( 'processSaveDiv' ).setHTML( 'Guardando....' );
    var cad = '';
    var frm = $( 'crmm_productos' );
    var obj = {};
    var error = false;

    for( i = 0; i < frm.length; i++ )
    {
        if( frm[i].id != '' )
        {
            eval( "obj." + frm[i].id + "='" + frm[i].value +  "';" );
            cad += "obj." + frm[i].id + "='" + frm[i].value +  "';\n";
        }
    }

    if( obj.prod_linea_prod.trim() == '' )
    {
        alert( "SE NECESITA UNA LINEA DE PRODUCTO\n" + cad );
        error = true;
    }

    if( error == false )
    {
        $( 'btnSaveProd' ).disabled = true;
        obj.action = 'save_prod_data';
        generalProdProcess( obj );
    }
}


function reloadProdList()
{
    generalProdProcess(
        {   'action'    :   'filtra_prod',
            'update'    :   'productsListDiv'
        }
    );
}

/**
 * Filtrado de productos
 */

function filtraProdList( obj )
{
    if( $( obj.update ))
    {
        $( obj.update ).setHTML( 'Filtrando.....' );
        obj.action = 'filtra_prod';
        generalProdProcess( obj );
    }
}

