/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *  JAVASCRIPT FOR CHECKBOX
 */

function confqty(box,sel){
    
//    alert(object.value);
//    alert($("input[id="+box+"]").val());
    object = $("input[id=header]");
    value= $("input[id=header]").val();
    for(var i=value.length-1; i>0; i--){        
        if(value.charAt(i) == ','){            
            value = value.substr(0,i)+","+sel.value+")";
            break;         
        }        
    }
    $(object).attr('value',value);

}

function sel_prm(value1,object){



    if(value1 > 0){
        var limite = value1;
        var group = object.id;
        var cont =0;

        $("input[id="+group+"]:checked").each(function(id) {
            cont++;

        });

        if(cont >= limite){

            $("input[id="+group+"]").each(function() {
                if( $(this).attr('checked') ) {
                }else{
                    $(this).attr('disabled','disabled');
                }
            });
        }else{
            $("input[id="+group+"]").each(function() {
                if( $(this).attr('checked') ) {
                }else{
                    $(this).attr('disabled','');
                }
            });

        }
    }

};


/**
 *  JAVASCRIPT FOR SELECT_LIST
 */



function sel_count(group){

    var count = 0;

    $("select[id="+group+"]").each(function () {
        var seleccion = parseInt($(this).val());
        count = count+seleccion;

    });

    return count;
}

function sel_cut(group,product_id,limite){

    var contador = sel_count(group);
    var l = limite-contador;
    $("select[id="+group+"]").each(function () {

        var value = parseInt($(this).val());
        if( $(this).attr('product') != product_id ){



            if(value == 0){

                $(this).children().remove().end();
                for(var i=0; i<=(limite-contador);i++){


                    if(i==value){
                        $(this).append("<option value="+i+" selected='selected'>"+i+"</option>");
                    }else{
                        $(this).append("<option value="+i+">"+i+"</option>");
                    }
                }

            }else{

                $(this).children().remove().end();
                for(var i=0; i<=(value+(limite-contador));i++){


                    if(i==value){
                        $(this).append("<option value="+i+" selected='selected'>"+i+"</option>");
                    }else{
                        $(this).append("<option value="+i+">"+i+"</option>");
                    }

                }

            }
        }

    });




}


function sel_unlock(group,product_id){

    $("select[id="+group+"]").each(function () {

        if( $(this).val() == 0){
            $(this).removeAttr("disabled");
        }

    });
}

function sel_lock(group,product_id){

    $("select[id="+group+"]").each(function () {

        if( $(this).val() == 0){
            $(this).attr("disabled","disabled");
        }

    });
}

function sel(value1,value2,object){


    var product_id = value1;
    var limite = value2;
    var group = object.id;
    var cont = sel_count(group);


    var val1 = value2;
    var val2 = object.id;
    var val3 = 0;

    if(cont == limite){
        //generamos que los select sin uso queden disable y los que tienen cantidad no pueda aumentar.
        sel_lock(group,product_id);
        sel_cut(group,product_id,limite);
    }else{
        sel_cut(group,product_id,limite);
        // si es menor, entonces reducimos las cantidad hasta alcanzar el limite en todos
        sel_unlock(group,product_id);

    }

}