function [choosen2, fix2] = plotafsliveCAA(Axes1, Axes2, AxesSOL1, AxesSOL2, xl1, xl2, yl1, yl2, lin1, lin2, AFS1, AFS2, LR1, LR2, mk, sty, lcs, fix1, fix2, xin, ch, choosen1, choosen2, isregcon, REGCON, full, selectplot, givena, givenc, tall, tcll, istrace, plotoptions, isC)

if get(plotoptions(2), 'Value')
    step = 1.0;
else
    step = 0.0;
end
liftup = 0;

nch = setdiff(1:3,ch);

intx = 2*xl2;
inty = 2*yl2;

colormap([1:-0.03:0.6]'*[1 1 1]);

if isC == 1
    plot(AxesSOL1, lin1([1 end]), [0 0], '--k','Linewidth',1);
    plot(AxesSOL2, lin2([1 end]), [0 0], ':k','Linewidth',0.5);
else
    plot(AxesSOL1, lin1([1 end]), [0 0], ':k','Linewidth',0.5);
    plot(AxesSOL2, lin2([1 end]), [0 0], '--k','Linewidth',1);
end

hold(AxesSOL1, 'on');
hold(AxesSOL2, 'on');

if isregcon(1)
    [cp,cf] = contourf(Axes1,REGCON{1}.xl, REGCON{1}.yl, REGCON{1}.cl');
    set(cf,'Linestyle','none');
    hold(Axes1, 'on');
end
if max(selectplot) == 0
    plot(Axes1,1,0,'w');
    hold(Axes1, 'on');
else
    if selectplot(1)
        plot(Axes1, AFS1(:,1), AFS1(:,2),'-k', 0, 0, 'ok','Linewidth',1,'Markersize',4);
        hold(Axes1, 'on');
    end
    if (selectplot(2) == 1) && (full.is1)
        plotfullafsCAA(Axes1, full.AFS1, full.lines1, full.valid1, full.dim1);
        hold(Axes1, 'on');
    end
end

if istrace
    plot(Axes1, tall(:,1), tall(:,2), 'o', 'Color', [.8 .8 .8]);
end

if givena.is
    if isfield(givena,'adaptedis') == 1
        if givena.adaptedis == 0
            for i=1:givena.s
                plot(Axes1, givena.afs(i,1), givena.afs(i,2),'*k','Linewidth',1,'Markersize',8);
                plot(AxesSOL1, lin1, givena.line(i,:), '-k','Linewidth',1);
            end
        else
            for i=1:givena.s
                plot(Axes1, givena.afs(i,1), givena.afs(i,2),'*k','Linewidth',1,'Markersize',8);
                plot(AxesSOL1, lin1(givena.adaptedI(1):givena.adaptedI(2)), givena.line(i,:), '-k','Linewidth',1);
            end
        end
    else
        for i=1:givena.s
            plot(Axes1, givena.afs(i,1), givena.afs(i,2),'*k','Linewidth',1,'Markersize',8);
            plot(AxesSOL1, lin1, givena.line(i,:), '-k','Linewidth',1);
        end
    end
end

plot(Axes1, xin(1),xin(2),mk(1),'color',lcs(ch,:),'Linewidth',2,'Markersize',12);
if get(plotoptions(1), 'Value')*isC == 0 || sum(choosen1(nch)) < 2
    if isC == 0
        tmpa = [1 xin]*LR2;
        tmpa = tmpa/max(tmpa);
        plot(AxesSOL1, lin1([1 end]), step*(ch-1)*[1 1], 'k:','Linewidth',0.5);
        plot(AxesSOL1, lin1, step*(ch-1)+tmpa, 'color',lcs(ch,:),'Linewidth',2);
        if get(plotoptions(3), 'Value')
            autodetect = detectallpeaksCAA(lin1,tmpa);
            for l=1:length(autodetect)
                plot(AxesSOL1, lin1(autodetect(l)), (ch-1)*step+tmpa(autodetect(l)), 'k.','Linewidth',2, 'Markersize',12);
                text(AxesSOL1, lin1(autodetect(l)), (ch-1)*step+tmpa(autodetect(l)),[' ',num2str(round(lin1(autodetect(l))))]);
            end
        end
        liftup = liftup+1;
    else
        plot(AxesSOL1, lin1, [1 xin]*LR2/max([1 xin]*LR2), 'color',lcs(ch,:),'Linewidth',2);
    end
end

for i=setdiff(1:3,ch)
    if choosen1(i) == 1
        plot(Axes1, fix1(i,1), fix1(i,2),mk(1),'color',lcs(i,:),'Linewidth',2,'Markersize',12);
        if get(plotoptions(1), 'Value')*isC == 0 || sum(choosen1(nch)) < 2
            if isC == 0
                tmpa = [1 fix1(i,:)]*LR2;
                tmpa = tmpa/max(tmpa);
                plot(AxesSOL1, lin1([1 end]), step*(i-1)*[1 1], 'k:','Linewidth',0.5);
                plot(AxesSOL1, lin1, step*(i-1)+tmpa, 'color',lcs(i,:),'Linewidth',2);
                if get(plotoptions(3), 'Value')
                    autodetect = detectallpeaksCAA(lin1,tmpa);
                    for l=1:length(autodetect)
                        plot(AxesSOL1, lin1(autodetect(l)), (i-1)*step+tmpa(autodetect(l)), 'k.','Linewidth',2, 'Markersize',12);
                        text(AxesSOL1, lin1(autodetect(l)), (i-1)*step+tmpa(autodetect(l)),[' ',num2str(round(lin1(autodetect(l))))]);
                    end
                end
                liftup = liftup+1;
            else
                plot(AxesSOL1, lin1, [1 fix1(i,:)]*LR2/max([1 fix1(i,:)]*LR2), 'color',lcs(i,:),'Linewidth',2);
            end
        end
    end
end

if isregcon(2)
    [cp,cf] = contourf(Axes2,REGCON{2}.xl, REGCON{2}.yl, REGCON{2}.cl');
    set(cf,'Linestyle','none');
    hold(Axes2, 'on');
end
if max(selectplot) == 0
    plot(Axes2,1,0,'w');
    hold(Axes2, 'on');
else
    if selectplot(1)
        plot(Axes2, AFS2(:,1), AFS2(:,2),'-k', 0, 0, 'ok','Linewidth',1,'Markersize',4);
        hold(Axes2, 'on');
    end
    if (selectplot(2) == 1) && (full.is2)
        plotfullafsCAA(Axes2, full.AFS2, full.lines2, full.valid2, full.dim2);
        hold(Axes2, 'on');
    end
end

if istrace
    plot(Axes2, tcll(:,1), tcll(:,2), 'o', 'Color', [.8 .8 .8]);
end

if givenc.is
    if isfield(givenc,'adaptedis') == 1
        if givenc.adaptedis == 0
            for i=1:givenc.s
                plot(Axes2, givenc.afs(i,1), givenc.afs(i,2),'*k','Linewidth',1,'Markersize',8);
                plot(AxesSOL2, lin2, givenc.line(i,:), '-k','Linewidth',1);
            end
        else
            for i=1:givenc.s
                plot(Axes2, givenc.afs(i,1), givenc.afs(i,2),'*k','Linewidth',1,'Markersize',8);
                plot(AxesSOL2, lin2(givenc.adaptedI(1):givenc.adaptedI(2)), givenc.line(i,:), '-k','Linewidth',1);
            end
        end
    else
        for i=1:givenc.s
            plot(Axes2, givenc.afs(i,1), givenc.afs(i,2),'*k','Linewidth',1,'Markersize',8);
            plot(AxesSOL2, lin2, givenc.line(i,:), '-k','Linewidth',1);
        end
    end
end

if sum(choosen1(nch)) == 2
    t = ones(3,3);
    t(nch,2:3) = fix1(nch,:);
    t(ch,2:3) = xin;

    pt = pinv(t);
    if min(pt(1,:)) < 0
        sty = '--';
    end
    pt = pt*diag(1./pt(1,:))*diag(sign(pt(1,:)));

    plot(Axes1, fix1(nch,1), fix1(nch,2),sty,'color',lcs(ch+3,:),'Linewidth',2);
    
    if get(plotoptions(1), 'Value') == 1
        if isC
            tmpC = (t*LR2)';
            al = tmpC\ones(size(LR2,2),1);
            tmpC = tmpC*diag(al);
            for i=1:3
                plot(AxesSOL2, lin2, LR1*pt(:,i)/max(abs(LR1*pt(:,i))), 'color',lcs(3+i,:),'Linewidth',2);
            end
        else
            tmpC = LR1*pt;
            al = tmpC\ones(size(LR1,1),1);
            tmpC = tmpC*diag(al);
            for i=1:3
                plot(AxesSOL2, lin2, tmpC(:,i), 'color',lcs(i,:),'Linewidth',2);
            end
            plot(AxesSOL2, lin2, tmpC*[1;1;1], 'k:','Linewidth',1);
        end
    end
    
    for i=1:3
        plot(Axes2, pt(2,i), pt(3,i),mk(2),'color',lcs(3+i,:),'Linewidth',2,'Markersize',12);
        if get(plotoptions(1), 'Value') == 1 && isC
            plot(AxesSOL1, lin1, tmpC(:,i), 'color',lcs(3+i,:),'Linewidth',2);
        end
        if get(plotoptions(1), 'Value') == 0 
            if isC == 1
                tmpa = LR1*pt(:,i);
                tmpa = tmpa/max(tmpa);
                plot(AxesSOL2, lin2([1 end]), (i-1)*step*[1 1], 'k:','Linewidth',0.5);
                plot(AxesSOL2, lin2, (i-1)*step+tmpa, 'color',lcs(3+i,:),'Linewidth',2);
                if get(plotoptions(3), 'Value')
                    autodetect = detectallpeaksCAA(lin2',tmpa');
                    for l=1:length(autodetect)
                        plot(AxesSOL2, lin2(autodetect(l)), (i-1)*step+tmpa(autodetect(l)), 'k.','Linewidth',2, 'Markersize',12);
                        text(AxesSOL2, lin2(autodetect(l)), (i-1)*step+tmpa(autodetect(l)),[' ',num2str(round(lin2(autodetect(l))))]);
                    end
                end
                liftup = liftup+1;
            else
                plot(AxesSOL2, lin2, LR1*pt(:,i)/max(abs(LR1*pt(:,i))), 'color',lcs(3+i,:),'Linewidth',2);
            end
        end
        plot(Axes2, pt(2,setdiff(1:3,i)), pt(3,setdiff(1:3,i)),sty,'color',lcs(i,:),'Linewidth',2);
    end
    if get(plotoptions(1), 'Value') == 1 && isC
        plot(AxesSOL1, lin1, tmpC*[1;1;1], 'k:','Linewidth',1);
    end
else
    fix2 = zeros(3,2);
    choosen2 = [0 0 0];
    for i=setdiff(1:3,ch)
        if choosen1(i) == 1
            if abs(fix1(i,1)) > 1.E-7
                plot(Axes2, -1/fix1(i,1)-(fix1(i,2)/fix1(i,1))*inty,inty,'color',lcs(i,:),'Linewidth',2);
            elseif abs(xin(2)) > 1.E-7
                plot(Axes2, intx,-1/fix1(i,2)-(fix1(i,1)/data.fix1(i,2))*intx,'color',lcs(i,:),'Linewidth',2);
            end
            
            t = ones(2,3);
            t(1,2:3) = xin;
            t(2,2:3) = fix1(i,:);
            
            pt = null(t);
            pt = pt/pt(1);
            
            col = lcs(3+setdiff(1:3,[i ch]),:);
            plot(Axes2, pt(2,1), pt(3,1), mk(2),'color',col,'Linewidth',2,'Markersize',12);
            if isC == 1
                tmpa = LR1*pt(:,1);
                tmpa = tmpa/max(tmpa);
                plot(AxesSOL2, lin2([1 end]), step*(setdiff(1:3,[i ch])-1)*[1 1], 'k:','Linewidth',0.5);
                plot(AxesSOL2, lin2, step*(setdiff(1:3,[i ch])-1)+tmpa, 'color',col,'Linewidth',2);
                if get(plotoptions(3), 'Value')
                    autodetect = detectallpeaksCAA(lin2',tmpa');
                    for l=1:length(autodetect)
                        plot(AxesSOL2, lin2(autodetect(l)), (setdiff(1:3,[i ch])-1)*step+tmpa(autodetect(l)), 'k.','Linewidth',2, 'Markersize',12);
                        text(AxesSOL2, lin2(autodetect(l)), (setdiff(1:3,[i ch])-1)*step+tmpa(autodetect(l)),[' ',num2str(round(lin2(autodetect(l))))]);
                    end
                end
                liftup = liftup+1;
            else
                plot(AxesSOL2, lin2, LR1*pt(:,1)/max(LR1*pt(:,1)), 'color',col,'Linewidth',2);
            end
            
            fix2(setdiff(1:3,[ch i]),:) = pt(2:3)';
            choosen2(setdiff(1:3,[ch i])) = 1;
        end
    end

    if abs(xin(1)) > 1.E-7
        plot(Axes2, -1/xin(1)-(xin(2)/xin(1))*inty,inty,'color',lcs(ch,:),'Linewidth',2);
    elseif abs(xin(2)) > 1.E-7
        plot(Axes2, intx,-1/xin(2)-(xin(1)/xin(2))*intx,'color',lcs(ch,:),'Linewidth',2);
    end
end

for i=nch
    if choosen1(i)
        plot(Axes1, [fix1(i,1) xin(1)], [fix1(i,2) xin(2)],sty,'color',lcs(setdiff(1:3,[i ch])+3,:),'Linewidth',2);
    end
end

set(Axes1, 'Xlim',xl1, 'Ylim',yl1);
set(Axes2, 'Xlim',xl2, 'Ylim',yl2);

if isC == 1
    set(AxesSOL1, 'Xlim', lin1([1 end]), 'Ylim' ,[-0.2 1.05]);
    set(AxesSOL2, 'Xlim', lin2([1 end]), 'Ylim' ,[-0.2 step*2+1.05])
    if get(plotoptions(2), 'Value')
        set(AxesSOL2, 'ytick',[]);
    end
    if get(plotoptions(4), 'Value')
        set(AxesSOL2, 'xdir','reverse');
    end
else
    set(AxesSOL1, 'Xlim', lin1([1 end]), 'Ylim' ,[-0.2 step*2+1.05]);
    if get(plotoptions(2), 'Value')
        set(AxesSOL1, 'ytick',[]);
    end
    if get(plotoptions(4), 'Value')
        set(AxesSOL1, 'xdir','reverse');
    end
    set(AxesSOL2, 'Xlim', lin2([1 end]), 'Ylim' ,[-0.2 1.05]);
end

hold(Axes1, 'off');
hold(Axes2, 'off');

hold(AxesSOL1, 'off');
hold(AxesSOL2, 'off');
