AddToCartSingleRequest = Class.create();

AddToCartSingleRequest.prototype = {

    initialize: function(url, errorMsgDiv) {
        this.url = url;
        this.errorMsgDiv = errorMsgDiv;
        this.onSuccess = this.processResponse.bindAsEventListener(this);        
    },
    submit : function(frmName)
    {
        this.myForm = frmName;
        var request = new Ajax.Request(
            this.url,
            {
                method: 'post',
                onSuccess: this.onSuccess,
                parameters: Form.serialize(frmName)
            }
        );
    },
    processResponse: function(transport)
    {
    	if (transport && transport.responseText){
            try{
                response = eval('(' + transport.responseText + ')');
                
                //if (response.redirectUrl)
                //{
                //    document.location.href = response.redirectUrl;
                //}
                //else 
                if (response.failed)
                {
                    $(this.errorMsgDiv).innerHTML = response.errorMessage;
                    Element.show(this.errorMsgDiv);
                }
                else if (response.pageRegions)
                {
                    var myhtml;
                    
                    var usedform = document.getElementById(this.myForm);
                    
                    var price = usedform.elements["price"].value;
                    var name = usedform.elements["item_name"].value;
                    var qty = usedform.elements["qty"].value;
                    var itemno_self = usedform.elements["item_no_self"].value;
                    var edpno = usedform.elements["edp_no"].value;
                    
                    var firstspace = itemno_self.indexOf(' ');
                    var itemno_length = itemno_self.length;
                    var itemno_self_short = itemno_self.substring(firstspace + 1, itemno_length).replace(' ', '-');
                    
                    myhtml = "<div id=\"popup_addtobasket_inner\">";
                    myhtml += "<h3 id=\"heading_sifr3\">You\'ve added the following item to your bag</h3>";
                    myhtml += "<div id=\"popup_addtobasket_left\">";
        	        myhtml += "<div id=\"popup_product_image\"><img src=\"http://s7d5.scene7.com/is/image/Mackenziechilds/" + itemno_self_short + "_w?layer=comp&wid=160&hei=160&fmt=jpeg&qlt=70,1&op_sharpen=1&resMode=bicub&op_usm=1.0,1.0,5,0&iccEmbed=0\" alt=\"\" /></div>";
                    myhtml += "<p>Applicable shipping and handling and taxes may be added during checkout.</p>";
                    myhtml += "<p>";
                    myhtml += "<a href=\"/\" title=\"Continue Shopping\" onclick=\"Modalbox.hide(); return false;\"><img src=\"/images/btn_continueshopping.png\" alt=\"\" /></a>";
                    myhtml += "</p>";
                    myhtml += "</div>";
                    myhtml += "<div id=\"popup_addtobasket_right\">";
        	        myhtml += "<h2>" + name + "</h2>";
                    myhtml += "<p>Product #" + itemno_self + "</p>";
                    myhtml += "<p>Quantity:&nbsp;" + qty + "</p>";
                    myhtml += "<p>";
                    myhtml += "Price:&nbsp;" + price;
                    myhtml += "</p>";
                    myhtml += "<div class=\"img_divider\"></div>";
                    myhtml += "<a href=\"/shop.axd/AccountCreate?redirect_url=%2fshop.axd%2fCart\"><img src=\"/images/btn_registernow.png\" alt=\"\" /></a>";
                    myhtml += "<a href=\"/shop.axd/LoginForm?redirect_url=%2fshop.axd%2fCart\"><img src=\"/images/btn_signin.png\"></a>";
                    myhtml += "<a href=\"/shop.axd/Cart\"><img src=\"/images/btn_checkoutnow.png\"> </a>";
		            myhtml += "</div>";
                    myhtml += "</div>";
                    Modalbox.show(myhtml, {width: 600}, {height: 300});
                }                
                
            }
            catch (e) {
                response = {};
            }
        }
    }
}

