/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;

function showComplianceDialog(){
    block = $$('.ajaxcartpro_compliance')[0];
    alignBlock(block, 360, 104, 'compliance');
    block.style.display = 'block';
    if (typeof($$('.ajaxcartpro_compliance .focus')[0]) != 'undefined') $$('.ajaxcartpro_compliance .focus')[0].focus();
}

function showFailureDialog(){
    block = $$('.ajaxcartpro_failure')[0];
    alignBlock(block, 260, 104, 'failure');
    block.style.display = 'block';
    if (typeof($$('.ajaxcartpro_failure .focus')[0]) != 'undefined') $$('.ajaxcartpro_failure .focus')[0].focus();
}

function hideComplianceDialog(){

    $$('.ajaxcartpro_compliance')[0].style.display = 'none';
}

function hideFailureDialog(){

    $$('.ajaxcartpro_failure')[0].style.display = 'none';
}

if(!Prototype.Browser.IE6){
    document.observe("dom:loaded", function() {
        updateAddLinks();

        $('aw_acp_failure_continue').onclick = function(e){
            e = e||event;
            if(e.preventDefault)
                e.preventDefault()
            $$('.ajaxcartpro_failure')[0].style.display='none';
            return false;
        }

        $('aw_acp_failure_locator').onclick = function(e){
            $$('.ajaxcartpro_failure')[0].style.display='none';
            return true;
        }
    });
}

function ohw_alcoholshipping_ajaxcartsend(url, type, obj){
    url = getCommonUrl(url);

    showProgressAnimation();
    if (type == 'form'){
        $('product_addtocart_form').action += url;
        $('product_addtocart_form').request({
            onComplete: function(resp){
                if (typeof(resp.responseText) == 'string'){
                    try{
                        eval('resp = ' + resp.responseText);
                    }catch(e){
                        return obj.form.submit();
                    }
                }
                hideProgressAnimation();
                if (resp.r == 'compliance') {
                    if (resp.error) {
                        if(resp.redirect){
                            document.location.href=resp.redirect;
                        }else{
                            $('product_addtocart_form').action =
                            ($('product_addtocart_form').action).split('awacp/1/ohc')[0];
                            showFailureDialog();
                        }
                    } else {
                        $('aw_acp_compliance').onclick = function() {
                            var regionIdVal = $('compliance:region_id').value;
                            if (regionIdVal && regionIdVal != "") {
                                hideComplianceDialog();
                                ajaxcartsend('ohc/' + regionIdVal + '/', type, obj);
                            //ajaxcartsend('&ohc=' + regionIdVal, type, obj);
                            }
                            return false;
                        };
                        showComplianceDialog();
                    }
                } else if (resp.r != 'success'){
                    obj.form.submit();
                } else {
                    if(AW_ACP.useConfirmation){
                        showConfirmDialog(resp.product_name);
                    }
                    updateCartView(resp);
                }
            }
        });
    }
    if (type == 'url'){
        new Ajax.Request(url, {
            onSuccess: function(resp){
                try{
                    if (typeof(resp.responseText) == 'string') eval('resp = ' + resp.responseText);
                }catch(e){
                    win.location.href=url;
                    hideProgressAnimation();
                    return;
                }
                hideProgressAnimation();
                if (resp.r == 'compliance') {
                    if (resp.error) {
                        showFailureDialog();
                    } else {
                        $('aw_acp_compliance').onclick = function() {
                            var regionIdVal = $('compliance:region_id').value;
                            if (regionIdVal && regionIdVal != "") {
                                hideComplianceDialog();
                                url = getCommonUrl(url);
                                if (url[url.length-1] == '/') delim = '';
                                else delim = '/';
                                ajaxcartsend(url + delim + 'ohc/' + regionIdVal+'/', type, obj);
                            }
                            return false;
                        };
                        showComplianceDialog();
                    }
                } else if (resp.r != 'success'){
                    win.location.href=url;
                } else {
                    if(AW_ACP.useConfirmation){
                        showConfirmDialog();
                    }
                    updateCartView(resp);
                }
            }
        }
        );
    }
}
ajaxcartsend = ohw_alcoholshipping_ajaxcartsend;

function ohw_alcoholshipping_ajaxcartsendconfigurable(url)
{
    showProgressAnimation();
    urlToSend = url + '&ajaxcartpro=1';
    new Ajax.Request(urlToSend, {
        onSuccess: function(resp){
            if (resp.responseText == 'false')
            {
                window.location = url;
            }
            else
            {
                try{
                    if (typeof(resp.responseText) == 'string') eval('resp = ' + resp.responseText);
                }catch(e){
                    win.location.href=url;
                    hideProgressAnimation();
                    return;
                }
                hideProgressAnimation();
                if (resp.r == 'compliance') {
                    if (resp.error) {
                        showFailureDialog();
                    } else {
                        $('aw_acp_compliance').onclick = function() {
                            var regionIdVal = $('compliance:region_id').value;
                            if (regionIdVal && regionIdVal != "") {
                                hideComplianceDialog();
                                url = getCommonUrl(url);
                                if (url[url.length-1] == '/') delim = '';
                                else delim = '/';
                                ajaxcartsendconfigurable(url+'ohc/' + regionIdVal+'/', type, obj);
                            }
                            return false;
                        };
                        showComplianceDialog();
                    }
                } else {
                    tmpDiv = win.document.createElement('div');
                    var scripts = resp.responseText.extractScripts();

                    tmpDiv.innerHTML = resp.responseText.stripScripts();
                    win.document.body.appendChild(tmpDiv);

                    showOptionsDialog();
                    hideProgressAnimation();

                    productAddToCartForm = new VarienForm('product_addtocart_form');
                    decorateGeneric($$('#product-options-wrapper dl'), ['last']);
                    addSubmitEvent();
                    if (typeof($$('#acp_configurable_block .focus')[0]) != 'undefined') $$('#acp_configurable_block .focus')[0].focus();

                    for (i=0; i<scripts.length; i++)
                    {
                        eval(scripts[i].replace(/var/g, ''));
                    }
                }
            }
        }
    });
}
ajaxcartsendconfigurable = ohw_alcoholshipping_ajaxcartsendconfigurable;

function addASSubmitEvent()
{
    if (typeof productAddToCartForm != 'undefined') {
        productAddToCartForm.submit = function(url){
            if(this.validator && this.validator.validate()){
                ajaxcartsend('awacp/1/', 'form', this, '');
            }
            return false;
        }

        productAddToCartForm.form.onsubmit = function() {
            productAddToCartForm.submit();
            return false;
        };
    }
}
addSubmitEvent = addASSubmitEvent;

if(!Prototype.Browser.IE6){
    document.observe("dom:loaded", function(){
        ajaxcartsend = ohw_alcoholshipping_ajaxcartsend;
        ajaxcartsendconfigurable = ohw_alcoholshipping_ajaxcartsendconfigurable;
        addSubmitEvent = addASSubmitEvent;
        addSubmitEvent();

        // Prevent default form submit for products in grid and use button click (below)
        $$('.widget-category-products form').each(function (n) {
            $(n).observe('submit', function (event) {
                var button;
                Event.stop(event);
                button = $(Event.element(event)).down('.btn-cart');
                button.click();
            });
        });

        // Add onclick event to add to cart buttons to combine url and qty and then setLocation
        $$('.widget-category-products .btn-cart').each(function (n) {
            $(n).observe('click', function (event) {
                var parent,
                    url,
                    qty;
                parent = $(Event.element(event)).up('form');
                url = $F(parent.addtocarturl);
                qty = $F(parent.qty) || 1;
                setLocation(url + 'qty/' + qty + '/');
            });
        });

    });
}
