var firstTime = true;//the script is run for Pages[0]

function page_change(page_no){
    document.getElementById('Pages').innerHTML=Pages[page_no];
    void(0);
    apply_logo_format();
}

function apply_logo_format(){
    // Get rid of the message "CLICK HERE TO ORDER"
    var x = document.getElementsByTagName("a");
    
    
    for (i=0;i<x.length;i++){
        
        if (x[i].firstChild.nodeName != "#text"){
            continue;
        }
        
        if (x[i].firstChild.nodeValue.indexOf("CLICK HERE TO ORDER")==-1){
            continue;
        }
        
        // remove the parent of the link containing 
        // the text "CLICK HERE TO ORDER". That parent is a <b>
        x[i].parentNode.parentNode.removeChild(x[i].parentNode);
    }
    
    // Add the image with the cart
    x = document.getElementsByTagName("td");
    var y;
    for (i=0;i<x.length;i++){
//         if (!x[i].getAttribute("class")){
//             continue;
//         }
//         
//         if (x[i].getAttribute("class").indexOf("DialogBox")==-1){
//             continue;
//         }
        if (x[i].className != "DialogBox"){
            continue;
        }
        
        if (x[i].firstChild.nodeName=="TABLE"){
            continue;
        }
        
        y = x[i].childNodes;
        for (j=0;j<y.length;j++){
            if (y[j].nodeName=="B"){
                y[j].firstChild.innerHTML = y[j].firstChild.innerHTML + "&nbsp;<img style=\"display: inline\" src=\"share/img/cart.png\" width=\"21\" height=\"13\" alt=\"[Cart]\" />";
            }
        }
    }
    
    if (firstTime){
        // Format the page numbers
        
        x = document.getElementById("Pages");
        x = x.previousSibling;
        do {
            if (x.tagName == "DIV"){
                break;
            }
        } while (x = x.previousSibling);
        
        x.id = "page_numbers_jtop";
        var new_node = x.cloneNode(x);
        new_node.id = "page_numbers_jbottom";
        new_node.align = "right";
        //alert ("am alc un alt nod, cu continutul:\n"+new_node.innerHTML);
        x.parentNode.appendChild(new_node);
        x.align = "right";
        
        x.parentNode.removeChild(x.nextSibling);
        
    }
    
    // Delete some useless P
//     x = document.getElementsByTagName("CENTER");
//     
//     if (x.length > 0){
//         x = x[0].firstChild;
//         do {
//             if (x.tagName=="P"){
//                 x.parentNode.removeChild(x);
//             }
//         } while (x = x.nextSibling);
//     }
    
    // Delete first useless TR
    x = document.getElementsByTagName("TBODY");
    
    if (x.length > 0){
        x = x[0].firstChild;
        do {
            if (x.tagName=="TR"){
                x.parentNode.removeChild(x);
                break;
            }
        } while (x = x.nextSibling);
    }
    
    // Delete second useless TR
    x = document.getElementsByTagName("TBODY");
    
    if (x.length > 0){
        x = x[0].firstChild;
        do {
            if (x.tagName=="TR"){
                x.parentNode.removeChild(x);
                break;
            }
        } while (x = x.nextSibling);
    }
    
    x = document.getElementById("page_numbers_jtop");
    x = x.firstChild;
    var i = 0;
    do {
        if (x.tagName=="A"){
            x.href = "javascript: page_change("+i+");"
            i++;
        }
    } while (x = x.nextSibling);
    
    x = document.getElementById("page_numbers_jbottom");
    x = x.firstChild;
    var i = 0;
    do {
        if (x.tagName=="A"){
            x.href = "javascript: page_change("+i+");"
                    i++;
        }
    } while (x = x.nextSibling);
    
    firstTime = false;
}