﻿// ------------------------------------------------------------------------------------------
// Copyright AspDotNetStorefront.com, 1995-2011.  All Rights Reserved.
// http://www.aspdotnetstorefront.com
// For details on this license please visit  the product homepage at the URL above.
// THE ABOVE NOTICE MUST REMAIN INTACT.
// ------------------------------------------------------------------------------------------
if (typeof (aspdnsf) == "undefined") {
    Type.registerNamespace('aspdnsf');
}

if (typeof (aspdnsf.Controls) == "undefined") {
    Type.registerNamespace('aspdnsf.Controls');
}

aspdnsf.Controls.RequestObserver = new Object();
aspdnsf.Controls.RequestObserver.registerButton = function(bId) {
    var btn = $get(bId);
    var req = Sys.WebForms.PageRequestManager.getInstance();
    if (btn && req) {
        var fnHandler = function(enable) {
            btn.disabled = !enable;
        }

        req.add_beginRequest(function() { fnHandler(false); });
        req.add_endRequest(function() { fnHandler(true); });
        
    }
}


aspdnsf.Controls.AddToCartForm = function(pId, vId) {
    this.productId = pId;
    this.variantId = vId;
    this.defaultVid = vId;
    this.validationRoutine = null;
    this.useAjaxBehavior = false;

    this.buttons = new Array();
    
}
aspdnsf.Controls.AddToCartForm.registerClass('aspdnsf.Controls.AddToCartForm');
aspdnsf.Controls.AddToCartForm.prototype = {

    getProductId: function() {
        return this.productId;
    },

    getVariantId: function() {
        return this.variantId;
    },

    getDefaultVariantId: function() {
        return this.defaultVid;
    },

    getElementValue: function(name) {
        var el = $get(name + '_' + this.getProductId() + '_' + this.getVariantId());
        if (el) {
            return el.value;
        }
        return '';
    },

    getQuantity: function() {
        var el = $get('Quantity_' + this.getProductId() + '_' + this.getVariantId());
        if (el) {
            return el.value;
        }
        return '';
    },

    getShippingAddressId: function() {
        return this.getElementValue('ShippingAddressID');
    },

    getCustomerEnteredPrice: function() {
        var el = $get('Price_' + this.getProductId() + '_' + this.getVariantId());
        if (el) {
            return el.value;
        }
        return '';
    },

    getColorOptions: function() {
        return this.getElementValue('Color');
    },

    getSizeOptions: function() {
        return this.getElementValue('Size');
    },

    getTextOption: function() {
        return this.getElementValue('TextOption');
    },

    getVariantStyle: function() {
        return this.getElementValue('VariantStyle');
    },

    getIsEditKit: function() {
        return this.getElementValue('IsEditKit');
    },

    getCartRecordId: function() {
        return this.getElementValue('CartRecID');
    },

    getKitItems: function() {
        return this.getElementValue('KitItems');
    },

    getUpsellItems: function() {
        var upsellItems = '';
        if (theForm.Upsell) {
            var upsellIds = new Array();
            for (var i = 0; i < theForm.Upsell.length; i++) {
                var el = theForm.Upsell[i];
                if (el.checked) {
                    upsellIds.push(el.value);
                }
            }
            upsellItems = upsellIds.toString();
        }

        return upsellItems;
    },

    getUseAjaxBehavior: function() {
        return this.useAjaxBehavior;
    },

    setUseAjaxBehavior: function(useAjax) {
        this.useAjaxBehavior = useAjax;
    },


    setValidationRoutine: function(validator) {
        this.validationRoutine = validator;
    },

    registerButton: function(bId, cartType) {
    var btn = $get(bId);
        cartType = 0
        if (btn) {
            btn.onclick = Function.createDelegate(this, function() { this.onButtonClick(cartType); });

            this.buttons.push(btn);
            aspdnsf.Controls.RequestObserver.registerButton(bId);
        }
    },

    enableDisableButtons: function(enable) {
        for (var i = 0; i < this.buttons.length; i++) {
            var btn = this.buttons[i];
            btn.disabled = !enable;
        }
    },

    onButtonClick: function(cartType) {
        //alert(cartType);
        var ele2 = $get('Quantity_' + this.getProductId() + '_' + this.getDefaultVariantId());
        var qty = 0;
        if (ele2) {
            qty = ele2.value;
        }

        this.variantId = document.getElementById('VariantID_' + this.getProductId() + '_' + this.getDefaultVariantId()).value;

        if (qty == 0) {
            qty = this.getQuantity();
        }

        if (this.validationRoutine && this.validationRoutine()) {

            var useAjax = this.getUseAjaxBehavior();
            if (useAjax && cartType == 0) {
                if (typeof (aspdnsf.Controls.Minicart) != "undefined") {
                    var miniCart = aspdnsf.Controls.Minicart.getInstance();
                    if (miniCart) {
                        // make sure to suppress the autohiding of the minicart
                        // once something has been clicked outside it's panel
                        // since the last one to trigger the minicart popup display
                        // would be the addtocart button, let it stay visible
                        miniCart.set_suppressAutoHide(true);
                    }
                }

                var onCompleteCallBack = Function.createDelegate(this, this.onAddToCartComplete);

                this.enableDisableButtons(false);

                var service = new ActionService();
                if (service) {
                    //alert("service call");
                    service.AddToCart(this.getProductId(),
                    this.getVariantId(),
                    cartType,
                    qty,
                    this.getShippingAddressId(),
                    this.getVariantStyle(),
                    this.getTextOption(),
                    this.getCustomerEnteredPrice(),
                    this.getColorOptions(),
                    this.getSizeOptions(),
                    this.getUpsellItems(),
                    this.getCartRecordId(),
                    this.getIsEditKit(),
                    this.getKitItems(),
                    onCompleteCallBack);
                }

            }
            else {
                var args = cartType + '_' + this.getProductId() + '_' + this.getVariantId();
                __doPostBack('AddToCart', args);
                this.surpressFormPost(); // prevent unintentional second regular form posts.
            }
        }

        return false; // suppress       
    },

    surpressFormPost: function() {
        document.forms['aspnetForm'].onsubmit = function() { return false; }
    },

    onAddToCartComplete: function(status) {
        this.enableDisableButtons(true);

        if (typeof (aspdnsf.Controls.Minicart) != "undefined") {
            var miniCart = aspdnsf.Controls.Minicart.getInstance();
            if (miniCart) {
                //alert("drop cart");
                // add an event handler for the asyncpostback end
                // so that we can display the minicart panel
                var req = Sys.WebForms.PageRequestManager.getInstance();
                // alert("product:before func");
                var fx = function() {
                    //  alert("product:minicart.show");
                    miniCart.show();
                    // now that the panel has been already shown
                    // allow auto-hiding the panel once anything is clicked
                    // outside the content area of the minicart panel
                    miniCart.set_suppressAutoHide(false);
                    req.remove_endRequest(fx);
                }
                req.add_endRequest(fx);
                //alert("end request");
                // this invocation would cause a async postback
                // in order to update and refresh the minicart display
                //   alert("product:refresh cart");
                miniCart.refresh();

            }
        }
    }

}

function Container_$(id) {
    return document.getElementById(id);
}



/* Ajax Functions */
function sendAJAXRequest(url, params, callback_function, vid, pid) {
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    xmlHttp.open('POST', url);

    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            callback_function(xmlHttp.responseText);
            this.productId = pid;
            this.variantId = vid;
            this.defaultVid = vid;
            this.validationRoutine = null;
            this.useAjaxBehavior = true;

            this.buttons = new Array();
            
            var bId = 'AddToCartButton_' + pid + '_' + vid;
            var btn = $get(bId);
            if (btn) {
                btn.onclick = Function.createDelegate(this, function() { onButtonClick('0'); });

                this.buttons.push(btn);
                aspdnsf.Controls.RequestObserver.registerButton(bId);
            }
        }

    };

    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send(params);
}

function GetXmlHttpObject() {
    var xmlHttp1 = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp1 = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp1 = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp1 = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp1;
}

function AjaxCall(divid, url, vid, pid) {
    sendAJAXRequest(url, '', function(response) {

        var startTag = 'Section_BeginContent';
        var endTag = 'Section_EndContent';

        var str = xmlHttp.responseText;

        // alert(str);                        

        var starting = str.indexOf(startTag);
        var ending = str.indexOf(endTag);

        var innerHtmlString = str.substring(starting + startTag.length, ending);

        Container_$(divid).innerHTML = '' + innerHtmlString;
        EvalJSBlocks(innerHtmlString);


        
        
    }, vid, pid);
}

/* Ajax Functions */

function Get_varOptions(vid, pid, brand) {
    var query = '?vid=' + vid + '&pid=' + pid + '&brand=' + brand;
    AjaxCall('Results', 'VariantOptions.aspx' + query, vid, pid);

}

function EvalJSBlocks(gen_html) {
    var stripped_html = gen_html;
    var js_start = 0;
    do {
        js_start = stripped_html.toLowerCase().indexOf('<' + 'script', 0);
        if (js_start > -1) {
            var js_start2 = stripped_html.indexOf('>', js_start) + 1;
            var js_end = stripped_html.toLowerCase().indexOf('<' + '/' + 'script' + '>', js_start);
            var js_end2 = stripped_html.indexOf('>', js_end) + 1;
            var gen_js = stripped_html.substring(js_start2, js_end);
            stripped_html = stripped_html.substring(0, js_start) + stripped_html.substring(js_end2, stripped_html.length);
            gen_js = gen_js.replace('<' + '!--', '');
            gen_js = gen_js.replace('/' + '/--' + '>', '');
            eval(gen_js);
        }
    }
    while (js_start > -1);

    return stripped_html;
}


