/*
 *
 *  Funciones de oliver, lo mas probable que se borre este archivo.
 *
 *
 */



function identificar(name)
{
    var res = "";
    var tam = name.length;
    var miString = name;
    var b = 0;

    for (i=0;i<tam;i++)
    {
        if((miString.charAt(i) == '_')||(miString.charAt(i) == '-'))
        {
            if(miString.charAt(i) == '_')
            {
                b = 1;
            }
            if(miString.charAt(i) == '-')
            {
                b = 2;
                i = ( tam + 1 );
            }
        }else
        {
            if(b == 1)
            {
                res += miString.charAt(i);
            }
        }

    }

    return res;
}
function iden_val(nam)
{
    var res = "";
    var tam = nam.length;
    var miString = nam;
    var b = 0;

    for (i=0;i<tam;i++)
    {
        if(miString.charAt(i) == '_')
        {
            b = 1;
        }else
        {
            if(b==1)
            {
                res += miString.charAt(i);
            }
        }
    }
    alert(res);
    return res;
}

function submitform()
{

    document.myform.submit();
}

function habilitar(id,n,value)
{
    var rad = "rad_pp-"+n+"_"+value;
    ;
    //alert("ID :"+id+" N: "+n+" VALUE"+value);
    var che = "che_pc-"+n+"_"+value;
    //alert(che);
    var checs = document.getElementById(che);
    var radio = document.getElementById(rad);

    if (radio.checked)
    {

        habilitar_che(false,che);

    }else
    {
        alert("radiobox no checked"+value);
    }
}
function habilitar_che(estado,che)
{

    var cont = 0;
    checks = document.myform[che];
    for(var x=0;x<checks.length;x++)
    {
        if(checks[x].checked != true)
        {
            checks[x].disabled= estado;
        }
    }

}
function contar(num,n,che_)
{



    var che = "che_pc-"+n+"_"+che_;

    var checkboxes = document.myform[che];

    var cont = 0; //Variable que lleva la cuenta de los checkbox pulsados
    var tamc = checkboxes.length;

    for (var x=0; x < checkboxes.length; x++)
    {
        if (checkboxes[x].checked)
        {
            cont = cont + 1;
            if(cont == num)
            {
                habilitar_che(true,che);
            }else
            {
                habilitar_che(false,che);
            }
        }
    }
}
function cannets(id,name)
{
    /*var cannt = document.getElementById("myform").cannet0;*/
    var cannt = document.getElementById(id);
    //alert(id+" "+name+" checked: "+cannt.checked+" / "+document.getElementById("myform").cannet0.checked);

    if (cannt.checked)
    {

        document.getElementById("myform").can0.disabled = false;
        document.getElementById("myform").can1.disabled = false;
        document.getElementById("myform").can2.disabled = false;

    }else
    {

        document.getElementById("myform").can0.disabled = true;
        document.getElementById("myform").can1.disabled = true;
        document.getElementById("myform").can2.disabled = true;
    }
}

function contc(co,con,name)
{
    var vp = document.getElementById("myform").can0.value;
    var vc = document.getElementById("myform").can1.value;
    var vs = document.getElementById("myform").can2.value;
    var tot = parseInt(vp) + parseInt(vc) + parseInt(vs);
    var cont = parseInt(con);
    var lim = 0;
    var lim = (cont - tot);

    var i_ = 0;
    var opC;
    var opS;
    var opP;

    /*var comboCoke = myform.can0.options;
                                        var comboSprite = myform.can1.options;
                                        var comboPepsi = myform.can2.options;*/

    var valP = vp;/*document.getElementById('can0').options.selectedIndex;*/
    var valC = vc;/*document.getElementById('can1').options.selectedIndex;*/
    var valS = vs;/*document.getElementById('can2').options.selectedIndex;*/

    /*alert("MAX "+cont+" valo pepsi :"+valP+": valor coke :"+valC+": valor sprite :"+valS+"sum "+tot);
                                        alert(" Valor sumas "+tot+" con "+cont+" valor del limite "+lim);*/
    if(tot < cont)
    {
        cannets();
        switch (name)
        {
            case 'can0':

                if(valP != 0)
                {

                    myform.can1.options.length = 0;
                    myform.can2.options.length = 0;

                    for(i_= 0;i_<lim+1;i_++)
                    {

                        opC = new Option(i_,i_,"","");
                        opS = new Option(i_,i_,"","");

                        comboCoke[i_] = opC;
                        comboSprite[i_] = opS;
                    }


                }else
                {
                    myform.can_Pepsi.options.length = 0;
                    myform.can_Coke.options.length = 0;
                    myform.can_Sprite.options.length = 0;

                    for(i_= 0;i_<lim+1;i_++)
                    {

                        opP = new Option(i_,i_,"","");
                        opC = new Option(i_,i_,"","");
                        opS = new Option(i_,i_,"","");
                        comboPepsi[i_] = opP;
                        comboCoke[i_] = opC;
                        comboSprite[i_] = opS;
                    }
                }

                break
            case 'can_Coke':

                if(valC != 0)
                {
                    myform.can_Pepsi.options.length = 0;

                    myform.can_Sprite.options.length = 0;
                    for(i_= 0;i_<lim+1;i_++)
                    {
                        opP = new Option(i_,i_,"","");

                        opS = new Option(i_,i_,"","");

                        comboPepsi[i_] = opP;

                        comboSprite[i_] = opS;
                    }

                }else
                {
                    myform.can_Pepsi.options.length = 0;
                    myform.can_Coke.options.length = 0;
                    myform.can_Sprite.options.length = 0;

                    for(i_= 0;i_<lim+1;i_++)
                    {

                        opP = new Option(i_,i_,"","");
                        opC = new Option(i_,i_,"","");
                        opS = new Option(i_,i_,"","");
                        comboPepsi[i_] = opP;
                        comboCoke[i_] = opC;
                        comboSprite[i_] = opS;
                    }
                }
                break
            case 'can_Sprite':

                if(valS != 0)
                {

                    myform.can_Pepsi.options.length = 0;
                    myform.can_Coke.options.length = 0;

                    for(i_= 0;i_<lim+1;i_++)
                    {
                        opP = new Option(i_,i_,"","");
                        opC = new Option(i_,i_,"","");

                        comboPepsi[i_] = opP;
                        comboCoke[i_] = opC;

                    }

                }else
                {
                    myform.can_Pepsi.options.length = 0;
                    myform.can_Coke.options.length = 0;
                    myform.can_Sprite.options.length = 0;

                    for(i_= 0;i_<lim+1;i_++)
                    {

                        opP = new Option(i_,i_,"","");
                        opC = new Option(i_,i_,"","");
                        opS = new Option(i_,i_,"","");
                        comboPepsi[i_] = opP;
                        comboCoke[i_] = opC;
                        comboSprite[i_] = opS;
                    }
                }
                break
        }
    }else
    {
        /*alert("valo pepsi :"+vp+": valor coke :"+vc+": valor sprite :"+vs);*/

        if(valP == 0)
        {
            document.getElementById("myform").can0.disabled = true;
        }
        if(valC == 0)
        {
            document.getElementById("myform").can1.disabled = true;
        }
        if(valS == 0)
        {
            document.getElementById("myform").can2.disabled = true;
        }


    }
    document.getElementById('can0').options.selectedIndex = parseInt(valP);
    document.getElementById('can1').options.selectedIndex = parseInt(valC);
    document.getElementById('can2').options.selectedIndex = parseInt(valS);

}
