function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}
function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; } 
    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

function focusinp(inp, helptext) {
    if (inp.value == helptext)
        inp.value = '';
    inp.style.color = 'Black';
}
function blurinp(inp, helptext) {
    if (inp.value == '') {
        inp.value = helptext;
        inp.style.color = '#999999';
    }
}

function loadRollovers(MEDIA_URL){
    MM_preloadImages(
        MEDIA_URL + 'images/bt1_2.png',
        MEDIA_URL + 'images/bt2_2.png',
        MEDIA_URL + 'images/bt3_2.png',
        MEDIA_URL + 'images/bt4_2.png',
        MEDIA_URL + 'images/bt5_2.png'
    );
}



carousel_NODES = [];
carousel_NODE_W = 0;
carousel_POS = 0;
carousel_AUTO = true;

function carousel_getNodes(p0,N, rt){
    if(!rt) rt = [];
    if(N<=0) return rt;
    var n = carousel_NODES.length;
    var np = p0 - n * parseInt(Math.floor(p0/n));
    rt.push(carousel_NODES[np]);
    return carousel_getNodes(np + 1, N-1, rt);
}

function carousel_mv(id, cnt, doitnow){
    carousel_render(carousel_POS,4);
    var n = $(id);
    n.set('tween', {duration: 'short'});
    n.tween('left', n.offsetLeft - cnt*carousel_NODE_W);
    carousel_POS += cnt;
}

function carousel_render(p0, N){
    var cont = $('contenedor_productos');
    var UL = cont.getElements('UL')[0];

    // Clear Nodes
    var rmnodes = UL.getElements('LI');
    for(var i=0;i<rmnodes.length;i++){
        UL.removeChild(rmnodes[i]);
    }

    var nodes = carousel_getNodes(p0-N,N*3);
    for(var i=0;i<nodes.length;i++){
        var node = nodes[i].cloneNode(true);
        UL.appendChild(node);
        if(i==0) node.className="p1";
    }
    
    //set container width
    cont.style.width = nodes.length*(carousel_NODE_W+10) + 'px';
    cont.style.left = -N*(carousel_NODE_W) + 'px';
}

function carousel_load(){
    if($$('#contenedor_productos.home').length==0) return;
    var UL = $('contenedor_productos').getElements('UL')[0];
    carousel_NODES = UL.getElements('LI');
    carousel_NODE_W = carousel_NODES[0].offsetWidth;
    for(var i=0;i<carousel_NODES.length;i++){
        UL.removeChild(carousel_NODES[i]);
    }
    
    carousel_render(0,4);
    
    $$("#arrow_left").addEvent('click',function(){carousel_mv('contenedor_productos', -1);})
    $$("#arrow_right").addEvent('click',function(){carousel_mv('contenedor_productos', 1);})
    
    $('contenedor_productos').addEvent('mouseleave', function(){
        carousel_AUTO = true;
    });
    
    $('contenedor_productos').addEvent('mouseover', function(){
        carousel_AUTO = false;
    });
    
    //carousel_auto();
}

function carousel_auto(){
    if(carousel_AUTO) carousel_mv('contenedor_productos', 4);
    window.setTimeout("carousel_auto()",6000);
}


SHIPPING_OPTIONS = null;
ALERT_NONE = false;
function update_shipping_modes() {
    var sn = $('id_shipping_mode');
    var country = $('id_ship_country').value;
    var state = $('id_location_tax').value;
    var val = sn.value;

    if(SHIPPING_OPTIONS==null)
        SHIPPING_OPTIONS = sn.getElements('option');
    var ops = SHIPPING_OPTIONS;
    var values = {};
    var option_cnt = 0;
    for(var i=0;i< ops.length;i++){
        ops[i].dispose();
        var vc = DCMODES[ops[i].value];
        var vs = DSMODES[ops[i].value];
        
        if((vc != null && country != '' && vc.search("'" + country + "'")>=0) || (vs != null && state != '' && vs.search("'" + state + "'")>=0)){
            ops[i].inject(sn);
            values[ops[i].value]=true;
            option_cnt++;
        }
    }
    
    if(values[val]==null)
        sn.selectedIndex = 0;

    if(ALERT_NONE && option_cnt == 0){
        if(($('id_ship_country').value=='1' || $('id_ship_country').value=='') && $('id_location_tax').value==''){
            //alert($('id_ship_country').value + ',' + $('id_location_tax').value);
        }else{
            alert('Shipping is not available for your order to the selected shipping location.');
        }
    }else{
    }

    return;
    var pn = $('id_shipping_mode').getParent();
    var sel = SELECTED_SHIPPING_MODE;
    if (country.length == 0) country = '0'
    if (state.length == 0) state = '0'
    if (sel.length == 0) sel = '0'
    var url = SECURE_ROOT + 'mdscart/shippingmodes/' + country + '/' + state + '/' + sel;


    var ul = pn.getElement('UL');
    if (ul) {
        ul = ul.clone(true);
    }
    pn.innerHTML = '&nbsp;';
    
    var s = new Request.HTML({ update: pn});
    s.addEvent('complete', function(e) {
        try { pn.grab(ul, 'top'); } catch (e) { }
    });
    s.get(url);
    
}


function ismaxlength(obj){
    var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";
    if (obj.getAttribute && obj.value.length>mlength)
        obj.value=obj.value.substring(0,mlength);
}

window.addEvent('domready', function() {
    $$(".orderform INPUT[type=text]").each(function(item){
        item.addEvent('focus', function(e){
            this.select();
        });
    });
    
    $$(".orderform INPUT[type=checkbox]").each(function(item){
        item.addClass('CHECKBOX');
        item.getParent().getElement('LABEL').addClass('CHECKBOX_LBLS');
    });

    if($('id_gift_message')) $('id_gift_message').addEvent('keydown',function(e){
        if(this.value.length>64)
            this.value = this.value.substring(0,64);
    });
    
    
    carousel_load();
    
    
    /*Billing equals shipping*/
    var SFS = ['id_email','id_ship_first_name', 'id_ship_last_name', 'id_ship_address', 'id_ship_address_2', 'id_ship_city', 'id_location_tax', 'id_ship_region', 'id_ship_zip', 'id_ship_country','id_ship_phone'];
    var BFS = ['id_billing_email','id_billing_fname', 'id_billing_lname', 'id_billing_address', 'id_billing_address_2', 'id_billing_city', 'id_billing_state', 'id_billing_region', 'id_billing_zip', 'id_billing_country','id_billing_phone'];
    var n = $('id_bill_eq_ship');
    if (n) {
        n.checked = id_bill_eq_ship;
        for (var i = 0; i < SFS.length; i++) {
            $(SFS[i]).addEvent('change', function() {
                if ($('id_bill_eq_ship').checked) {
                    this.billfield.value = this.value;
                    this.billfield.fireEvent('change');
                }
            });
        }

        n.addEvent('change', function() {
            if (this.checked) {
                for (var i = 0; i < SFS.length; i++) {
                    $(BFS[i]).value = $(SFS[i]).value;
                    $(BFS[i]).disabled = 'disabled';
                    $(SFS[i]).billfield = $(BFS[i]);
                }
            } else {
                for (var i = 0; i < BFS.length; i++) {
                    $(BFS[i]).disabled = '';
                }
            }
            $$('#id_billing_country').fireEvent('change');
        });
        n.fireEvent('change');
    }
    
    /*Is Gift flipping*/
    if ($('id_is_gift') && false) {
        $('id_is_gift').onchange = function() {
            var msg = $('id_gift_message').parentNode.parentNode;
            if (this.checked) {
                msg.style.display = '';
            } else {
                msg.style.display = 'none';
            }
        }
        $('id_is_gift').onchange();
    }    

    /*Contry State Flipping - Shipping Form*/
    if ($$('#id_ship_country').length > 0) {
        $$('LABEL[for=id_location_tax]')[0].addClass('required');
        $$('#id_ship_country').addEvent('change', function(e) {
            var ns = $('id_location_tax').getParent().style;
            var nr = $('id_ship_region').getParent().style;
            var nz = $$('LABEL[for=id_ship_zip]');
            var nlr = $$('LABEL[for=id_ship_region]');
            if (this.value == "1") {
                nz.addClass('required');
                nlr.removeClass('required');
                ns.display = '';
                nr.display = 'none';
                $('id_ship_region').value = '';
            } else {
                nz.removeClass('required');
                nlr.addClass('required');
                ns.display = 'none';
                nr.display = '';
                $('id_location_tax').selectedIndex = 0;
            }
        });
        $$('#id_ship_country').addEvent('change', update_shipping_modes);
        $$('#id_location_tax').addEvent('change', update_shipping_modes);

        if ($('id_ship_country').selectedIndex == 0) $('id_ship_country').selectedIndex = 1;
        $$('#id_ship_country').fireEvent('change');
        ALERT_NONE = true;
    }

    /*Contry State Flipping - Billing Form*/
    if ($$('#id_billing_country').length > 0) {
        $$('LABEL[for=id_billing_state]')[0].addClass('required');
        $$('#id_billing_country').addEvent('change', function(e) {
            var ns = $('id_billing_state').getParent().style;
            var nr = $('id_billing_region').getParent().style;
            var nz = $$('LABEL[for=id_billing_zip]');
            var nlr = $$('LABEL[for=id_billing_region]');
            ns.className = 'required';

            if (this.value == "1") {
                nz.addClass('required');
                nlr.removeClass('required');
                ns.display = '';
                nr.display = 'none';
                $('id_billing_region').value = '';
            } else {
                nz.removeClass('required');
                nlr.addClass('required');
                ns.display = 'none';
                nr.display = '';
                $('id_billing_state').selectedIndex = 0;
            }
        });
        if ($('id_billing_country').selectedIndex == 0) $('id_billing_country').selectedIndex = 1;
        $$('#id_billing_country').fireEvent('change');
    }

}); 



