var Sapsync = Class.create();
Sapsync.prototype = {
     initialize: function(form, getUrl){
        this.form = form;
        this.getUrl = getUrl;
		//this.onAddressLoad = this.fillForm.bindAsEventListener(this);
		this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
		this.containerSuffix = '';
		this.error = false;
		this.errorMessage = '';
		this.configs = ''
    },
	
	_disableEnableAll: function(element, isDisabled) {
        var descendants = element.descendants();
        for (var k in descendants) {
            descendants[k].disabled = isDisabled;
        }
        element.disabled = isDisabled;
    },
	
	resetLoadWaiting: function(transport){
        this.setLoadWaiting(false);
    },
	setLoadWaiting: function(enable) {
		if (enable) {
			var container = $('disable-container'+this.containerSuffix);
            container.setStyle({opacity:.5});
            this._disableEnableAll(container, true);
            Element.show('sap-price-please-wait'+this.containerSuffix);
        }
		else {
			if(!this.error){
				var container = $('disable-container'+this.containerSuffix);
				container.setStyle({opacity:1});
				this._disableEnableAll(container, false);
				Element.hide('sap-price-please-wait'+this.containerSuffix);
			}else{
				var container = $('disable-container'+this.containerSuffix);
				container.setStyle({opacity:.5});
				this._disableEnableAll(container, true);
				$('messages_product_view').innerHTML = this.errorMessage;
				Element.hide('sap-price-please-wait'+this.containerSuffix);
			}
        }
    },
	getPrice: function(){  
		//var item_part_no = $("part_no").value;
		//var product = $("product").value;
		this.setLoadWaiting(true);
	   	var request = new Ajax.Request(
		    this.getUrl,
                {
                    method: 'get',
					//parameters: {product: product},
					onComplete: this.onComplete,
                    onFailure: function(response){
						alert('Pricing for this product is temporarily unavailable. We have been notified of the problem and are working on correcting the issue.\n We encourage you to try again in a few hours or alternatively call 1800 777 458 to place your order offline.');
						Sapsync.error = true;
						Sapsync.errorMessage = '<ul class="messages"><li class="error-msg"><ul><li>Pricing for this product is temporarily unavailable. We have been notified of the problem and are working on correcting the issue.<br />We encourage you to try again in a few hours or alternatively call 1800 777 458 to place your order offline.</li></ul></li></ul>';
						this.onComplete;
					},
					onSuccess: function(response){
						if(response.responseText!=''){
							
							eval("text = "+ response.responseText +";");
							if( text.error == '1'){
								alert(text.message);
								Sapsync.error = true;
								Sapsync.errorMessage = text.display_message;
							}
							// set the price for the child sku
							if(text.child != ''){
								this.configs = text.child;
								spConfig = new Product.Config(this.configs);

							}
							if(text.parent != ''){
								var parentProducts = text.parent;
								// set the price for the parent sku
								for(var i=0; parentProducts.length >=0; i++){
									var priceFromLabel='';
									if(text.child != ''){
										priceFromLabel = '<span class="label" id="configurable-price-from-'+parentProducts[i]['ITM_NUMBER']+'"><span class="configurable-price-from-label">Price From: </span></span>'
										$('item_'+parentProducts[i]['ITM_NUMBER']).innerHTML = priceFromLabel+'<span class="regular-price"><span class="price">$'+parentProducts[i]['NET_EXT']+'</span></span>';
									}
									else{
										$('item_'+parentProducts[i]['ITM_NUMBER']).innerHTML = priceFromLabel+'<span class="regular-price"><span class="price">$'+parentProducts[i]['NET_EXT']+'</span></span>';
									}
								}
							}
							//$('item_'+text[0]['ITM_NUMBER']).innerHTML = text[0]['NET_EXT'];	
						}
						else{
							
						}
					},
					parameters: Form.serialize("product_addtocart_form")
                    
                }
            );
	},
    fillPrice: function(transport){
        var elementValues = {};
        if (transport && transport.responseText){
            try{
                elementValues = eval('(' + transport.responseText + ')');
            }
            catch (e) {
                elementValues = {};
            }
        }
        else{
            
        }
        arrElements = Form.getElements(this.form);
        
    }
}

    


