if (self.location==top.location) top.location="index.html"
var cookie_pedido = new Object();
var numero_item = 0;
var subtotal = 0;
var importe_total = 0;
var lista_articulos = new Object();
total = 0;
var unidades = 0;
var compra_minima =0;
var direccion = '';
function ventana(pagina)
{
window.open(pagina,'','width=450,height=350,location=0,status=0,directories=no,toolbar=no,resizable=yes,menubar=no,scrollbars=yes');
};
function buscar(){
window.top['pantalla'].location.href = 'buscador.htm';
};
function inicio() {
window.top.location.href = 'index.html';
};

function corregir_decimales(total,corregir,dec){
var result=Math.floor(total)+'.';
var cents=100*(total-Math.floor(total))+0.5;
result += Math.floor(cents/10);
result += Math.floor(cents%10);
var valor="";
var pasos=0;
var x=result.length-1;
if (corregir!="no"){
while (x>=0){
    if (result.charAt(x)!="."){
        if (pasos==3){
            valor = result.charAt(x) + "," + valor;
            pasos = 0;
        }else{
             valor = (result.charAt(x)) + valor;
        };        pasos = pasos + 1;
       x = x - 1;    }else{
    pasos=0;
    valor = (result.charAt(x)) + valor
    x = x - 1;
    };};
    if (dec!='no'){
        return valor;
    }else{
        return valor.substring(0,valor.length-3);
    }
}else{
    if (dec!='no'){
        return result;
    }else{
        return result.substring(0,result.length-3);
    };
};
};
function cesta(id, opciones,cantidad) {
id = eval(id);
var cant = parseInt(cantidad);
var cant2 = cant.toString();
if ( ( cant2 != 'NaN') && (cantidad != null) && (cant2 != '0') ){
lista_articulos [numero_item] = id;
lista_articulos [numero_item+1] = cant;
lista_articulos [numero_item+2] = parent.frames['pantalla'].articulo[id][0];
lista_articulos [numero_item+3] = parent.frames['pantalla'].articulo[id][1];
lista_articulos [numero_item+4] = parent.frames['pantalla'].articulo[id][2];
lista_articulos [numero_item+5] = parent.frames['pantalla'].articulo[id][3];
lista_articulos [numero_item+6] = opciones;
numero_item = numero_item + 7;
total= eval(importe_total) + (cant* eval(parent.frames['pantalla'].articulo[id][1])); 
document.forms[0].total.value= corregir_decimales(total,'si','si');
importe_total = total
chequear_cesta();
}else{
alert('Valor no valido');
};
};

function reload_pedido(){
    numero_item=0;
    var cadena='';
    var ped= buscar_cookie('pedido');
    var id;
    var cant=0;
    if ((ped!=0) && (ped!=null) ) {
        var tam = ped.length;
        var i = 0;
        var t = 0;
        var tr=0;
        while (i < tam) {
            i = ped.indexOf('#',i);
            t = ped.indexOf('#',i+1);
            if (t!=-1) {
                if (tr<7) {
                    if (tr==0){
                        id=eval(ped.substring(i+1,t));
                        lista_articulos [numero_item] = id;
                    }
                    if (tr==1){
                        lista_articulos [numero_item+2] = ped.substring(i+1,t);
                    }
                    if (tr==2){
                        cant =  eval(ped.substring(i+1,t));
                        lista_articulos [numero_item+1] =cant;
                    }
                    if (tr==3){
                        lista_articulos [numero_item+3] = eval(ped.substring(i+1,t));
                    }
                    if (tr==4){
                        lista_articulos [numero_item+5] = eval(ped.substring(i+1,t));
                    }
                    if (tr==5){
                        lista_articulos [numero_item+4] = ped.substring(i+1,t);
                    }
                if (tr==6){
                    lista_articulos [numero_item+6] = ped.substring(i+1,t);
                    total= eval(importe_total) + eval(eval(cant)* eval(lista_articulos [numero_item+3]));
                   importe_total=total;
                    numero_item = numero_item + 7;
                    document.forms[0].total.value= corregir_decimales(total,'si','si');
                };
                tr = tr+1;
                }else{
                    tr = 0 ;
                    i--;
                };
            i++;
            }else{
                lista_articulos [numero_item+6] = ped.substring(ped.lastIndexOf('#')+1,tam);
                total= eval(importe_total) + eval(eval(cant)* eval(lista_articulos [numero_item+3]));
                importe_total=total;
                numero_item = numero_item + 7;
                document.forms[0].total.value= corregir_decimales(total,'si','si');
                break;
            }
        }
    }
};
function buscar_cookie(nombre){
var dc = document.cookie;
var prefix = nombre +'=';
var begin = dc.indexOf('; ' + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}else{
begin +=2};
var end = document.cookie.indexOf(';', begin);
if (end == -1){
end = dc.length;};
return unescape(dc.substring(begin + prefix.length, end));
};
function seleccion() {
if (document.forms[0].cesta.selectedIndex == 0){
chequear_cesta();
}else if (document.forms[0].cesta.selectedIndex == 1){
 vaciar_cesta();
 }else if (document.forms[0].cesta.selectedIndex == 2){
 inicio();
 } else if (document.forms[0].cesta.selectedIndex == 3){
 buscar();};
};
function mas_unidad(ref){
var i;
var total = 0;
ref = eval(ref);
lista_articulos [ref+1] = lista_articulos [ref+1] +1;
chequear_cesta();
document.forms[0].total.value= corregir_decimales(importe_total,'si','si');
};
function menos_unidad(ref){
var matriz_pedido='';
ref = eval(ref);
if (lista_articulos[ref+1]-1 >=1){
lista_articulos [ref+1] = lista_articulos [ref+1] -1;
chequear_cesta();
document.forms[0].total.value= corregir_decimales(importe_total,'si','si');
}else{
borrar_articulo(ref);
};
};
function chequear_cesta() {
var dir =parent.frames['pantalla'].location.href;
if (dir.substring(dir.lastIndexOf('/')+1, dir.length)!='cesta.htm'){
direccion = parent.frames['pantalla'].location.href;
}
var cesta = parent.frames['pantalla'];
cesta.document.write('<html><head><title>Cesta</title>');
cesta.document.write('</head>');
cesta.document.write('<body bgcolor=#6289B3 background="imagenes/">');
cesta.document.write('<center>');
cesta.document.write('<font face="Comic Sans MS" color=FFFFFF size=-1>');cesta.document.write('<b>Artículos seleccionados</b></font><p>');
cesta.document.write('<form>');
crear_cesta('si');
cesta.document.write('<input type=button value=Volver onClick=parent.frames["pantalla"].location.href="' + direccion.substring(direccion.lastIndexOf("/")+1, direccion.length) + '">');
if (importe_total!=0){
cesta.document.write('<input type=button value="Vaciar Cesta" onClick=parent.frames["cesta"].vaciar_cesta()>');
cesta.document.write('<input type=button value="Confirmar compra" onClick=parent.frames["cesta"].comprar()>');
};
cesta.document.write('</form></body></html>');
cesta.document.close();
};
function crear_cesta(borrado) {
var cesta = parent.frames['pantalla'];
subtotal=0;
importe_total=0;
unidades=0;
cookie_pedido = '' + '\n' ;
cesta.document.write('<table cellspacing=2 cellpadding=4  width=85% border=0>');
cesta.document.write('<tr><td bgcolor="1650C0" width="1%" align=center nowrap><font face="Times New Roman" size=-1 color="FFFFFF">');
cesta.document.write('<b>Unid.</b> </font></td>');
cesta.document.write('<td bgcolor="1650C0" width="1%" nowrap><font face="Times New Roman" size=-1 color="FFFFFF"> ');
cesta.document.write('<b>Referencia</b> </font></td>');
cesta.document.write('<td bgcolor="1650C0" align=center width="1%" nowrap><font face="Times New Roman" size=-1 color="FFFFFF"> ');
cesta.document.write('<b>Precio</b> </font></td>');
cesta.document.write('<td bgcolor="1650C0" align=center width="1%" nowrap><font face="Times New Roman" size=-1 color="FFFFFF">');cesta.document.write('<b>Importe</b> </font></td>');
cesta.document.write('<td bgcolor="1650C0" nowrap width="10%" align=center nowrap><font face="Times New Roman" size=-1 color="FFFFFF">');cesta.document.write('<b>Nombre</b> </font></td>');
cesta.document.write('<td bgcolor="1650C0" width="1%" nowrap><font face="Times New Roman" size=-1 color="FFFFFF">');cesta.document.write('<b>Opciones</b> </font></td>');
if (borrado!='no'){
cesta.document.write('<td bgcolor="1650C0" width="1%" align=center nowrap><font face="Times New Roman" size=-1 color="FFFFFF">');cesta.document.write('<b>Borrar</b> </font></td>');
};
cesta.document.write('</tr>');
for (k=0; k<=numero_item-7; k=k+7){
if (lista_articulos[k] != -1){
cookie_pedido = cookie_pedido + '#' + lista_articulos[k] +  '#' + lista_articulos[k+2] + '' ;
cesta.document.write('<tr><td bgcolor="1674BB" width="1%" align=center nowrap><font face="Times New Roman"  size=-1 color="FFFFFF" align=center>');
cesta.document.write('<input type=text name=unidades size=1 value=' + lista_articulos[k+1] + ' onfocus=blur()>');
unidades = unidades + lista_articulos[k+1]
cesta.document.write('<input type=button value=" + " onClick=parent.frames["cesta"].mas_unidad(' + k + ')>');
cesta.document.write('<input type=button value=" - " onClick=parent.frames["cesta"].menos_unidad(' + k + ')>');
cesta.document.write('</td>');
cookie_pedido = cookie_pedido + '#' + lista_articulos[k+1] + '' ;
cesta.document.write('<td bgcolor="1674BB" width=10><font face="Times New Roman" size=-1 color="FFFFFF">' + lista_articulos[k+2] + '</td>');
cesta.document.write('<td bgcolor="1674BB" width="1%" align=right nowrap><font face="Times New Roman" size=-1 color="FFFFFF" align=right >' + corregir_decimales(lista_articulos[k+3],'si','si')+ '&nbsp;Euro</b></td>');
cookie_pedido = cookie_pedido + '#'+ corregir_decimales(lista_articulos[k+3],'no','si');
subtotal=(lista_articulos[k+1] * lista_articulos[k+3]);
cookie_pedido = cookie_pedido + '#'+ lista_articulos[k+5];;
importe_total = importe_total + subtotal;
cesta.document.write('<td bgcolor="1674BB" width="1%" align=right nowrap><font face="Times New Roman" align=right size=-1 color="FFFFFF">' + corregir_decimales(subtotal,'si','si') + '&nbsp;Euro</b></td>');
cesta.document.write('<td bgcolor="1674BB" width="1%" nowrap><font face="Times New Roman" align=left size=-1 color="FFFFFF">' + lista_articulos[k+4] + '</b></td>');
cookie_pedido = cookie_pedido + '#'+ lista_articulos[k+4] + '' ;
cesta.document.write('<td bgcolor="1674BB" width="1%" nowrap><font face="Times New Roman" size=-1 color="FFFFFF">' + lista_articulos[k+6] + '<font color=#ffffff>.</b></td>');
cookie_pedido = cookie_pedido + '#'+ lista_articulos[k+6] + '\n' ;
if (borrado!='no'){
cesta.document.write('<td bgcolor="1674BB" align=center width="1%" nowrap><center><input type=button value=Borrar onClick=parent.frames["cesta"].borrar_articulo(' + k + ')></center>');
};
cesta.document.write('</tr>');
};
};
cesta.document.write('<tr><td bgcolor="1650C0" colspan=3><font face="Times New Roman" size=-1 color="FFFFFF">');
cesta.document.write('Importe total</b></td>');
cesta.document.write('<td bgcolor="1650C0" align=right width="1%" nowrap><font face="Times New Roman" align=right size=-1 color="FFFFFF">');
cesta.document.write('<b>' + corregir_decimales(importe_total,'si','si') + '</b>&nbsp;Euro</font>');
document.cookie = 'pedido=' + escape(cookie_pedido);
document.cookie = 'total=' + escape(corregir_decimales(importe_total,'no','si'));
cesta.document.write('</td></tr></table>');
};
function borrar_articulo(id) {
var pric =id;
pric = eval(pric);
var cantidad = eval(lista_articulos[pric+1]);
var precio = eval(lista_articulos[pric+3]);
total = cantidad*precio;
document.forms[0].total.value= corregir_decimales(importe_total - total,'si','si');
lista_articulos[pric] = -1;
lista_articulos[pric+1] = '';
lista_articulos[pric+2] = '';
lista_articulos[pric+3] = '';
lista_articulos[pric+4] = '';
lista_articulos[pric+5] = '';
lista_articulos[pric+6] = '';
chequear_cesta('si');
total = eval(importe_total);
};
function vaciar_cesta() {
if(confirm('¿Esta seguro de vaciar su cesta?')){
total = 0;
document.forms[0].total.value=0;
for (i=0; i<=numero_item-7; i=i+7){
lista_articulos[i] = -1;
};};
chequear_cesta();
};
function comprar(){
window.top.location.href = 'caja.htm';
};

