function osCommerce_login(user, pass){
    j.ajax({
        url: '/os/root/catalog/login.php?action=process',
        cache: false,
        async: false,
        type: 'POST',
        data: 'email_address='+user+'&password='+pass
    });
}

function osCommerce_addProduct(id){
    var product = (typeof(id) == 'undefined') ? 25 : id;
    var newUrl = '/os/root/catalog/product_info.php?products_id='+product+'&action=add_product';
    j.ajax({
        url: newUrl,
        cache: false,
        type: 'POST',
        data: 'products_id='+product,
        success: function(data){
            var miniCart = new Image();
            miniCart.src = '/images/minicart.gif';
            miniCart.border = 0;
            if(typeof(document.getElementById('item-status-'+id)) != 'undefined'){
                var o = document.getElementById('item-status-'+id);
                if(o != null)
                    o.appendChild(miniCart);
            }
            if(typeof(eval('window.osCommerce_item_'+id)) == 'undefined'){
                eval('window.osCommerce_item_'+id+' = 1');
            }else{
                eval('window.osCommerce_item_'+id+'++');
            }
        }
    });
}

function osCommerce_cart(){
    var iFrame = '<iframe id="osCommerce_window" name="osCommerce_window" allowtransparency frameborder="0" height="100%" width="100%" src="/os/root/catalog/shopping_cart.php" ></iframe>';
    darkScreen(true, function(){
        Dialog.create('shopping_window','',
        {
            width:600,
            height:550,
            title:'My Perimeter Cart',
            closebutton:true,
            content: iFrame
        });
    });
}

function osCommerce_resizeFrame(){
    var getHeight = function(){
        if(window.osCommerce_window.document != null){
            var h = window.osCommerce_window.document.height + 150;
            var w = window.osCommerce_window.document.width + 25;
            j('#shopping_window').css({
                width: w+'px',
                height: h+'px'
            });
        }
    }
    setTimeout(getHeight, 500);
}

function osCommerce_emptyCart(){
    var spans = document.getElementsByTagName('span');
    for(x in spans){
        if(typeof(spans[x].getAttribute) != 'undefined' && spans[x].hasAttribute('class')){
            if(spans[x].getAttribute('class') == 'osCommerce_items'){
                var itemId = spans[x].getAttribute('id').replace(/[^\d]+/g, '');
                eval('delete(osCommerce_item_'+itemId+')');
                spans[x].innerHTML = '';
            }
        }
    }
}

function osCommerce_displayItem(buttonLink, itemId){
    if(typeof(buttonLink) != 'undefined' && typeof(itemId) != 'undefined'){
        var item = new Object();
        /**
         * first, load item data and also check if is inside the 'forms' category
         * act accordingly.
         */
        j.ajax({
            url: '/os/root/catalog/item_data.php',
            type: 'GET',
            data: 'item='+itemId,
            async: false,
            dataType: 'json',
            error: function(){
                var o = document.getElementById('item-status-'+itemId);
                o.innerHTML = 'The item cannot be added. Contact the administrator.'
            },
            success: function(data){
                item = data;
            }
        });
        document.write('<div id="osCommerce_item_'+itemId+'"></div>');
        if(item.data.category != 'forms'){
            var link = document.createElement("a");
            link.href = 'javascript:void(osCommerce_addProduct('+itemId+'))';
            link.innerHTML = buttonLink;
            link.setAttribute('id', 'item-link-'+itemId);
            var span = document.createElement('span');
            span.setAttribute('id', 'item-status-'+itemId);
            span.setAttribute('class', 'osCommerce_items');
            var price = document.createElement('span');
            price.setAttribute('id', 'item-price-'+itemId);
            price.setAttribute('class', 'item-prices');
            price.setAttribute('style', 'font:bold 9px arial;color:green');
            price.innerHTML = '&nbsp;$ ' + item.data.price + '&nbsp;';
            document.getElementById('osCommerce_item_'+itemId).appendChild(link);
            document.getElementById('osCommerce_item_'+itemId).appendChild(price);
            document.getElementById('osCommerce_item_'+itemId).appendChild(span);
        }else{
            osCommerce_form(item.data.name, item.id);
        }
    }
}

/**
 * since we need also to sell registration fees, and osCommerce doesn't have a way to do this,
 * we need to check if an item is inside the 'forms' category, and if it does, then, we display
 * the form using a predefined function
 */

function osCommerce_form(formName, itemId){
    var filename = formName.replace(/[^\w]+/g, '').toLowerCase()+'.php';
    document.write('<div id="form-'+filename+'-container"></div>');
    j.ajax({
       url: '/os/root/catalog/forms/'+filename,
       cache: false,
       type: 'GET',
       success: function(data){
           document.getElementById('form-'+filename+'-container').innerHTML = data;
           document.getElementById('form-'+filename+'-container').lastChild.action = 'javascript:return function(){return false;};';
           document.getElementById('form-'+filename+'-container').lastChild.onsubmit = function(){
               osCommerce_addProduct(itemId);
               alert('\nCheck your cart!\nFollowing the rules, we can create easily a lot of forms and handle\nthe payment process without need to modify existing code.');
               return false;
           }
       }
    });
}