﻿var ProductDetails = new Class({

    initialize: function(options){
        if (!this.initalizeChecks(options)) return;
        this.version = options.version || 1;
	    this.priceElementId = options.priceElementId || null;
	    this.priceIncludingTaxElementId = options.priceIncludingTaxElementId || null;
	    this.salePriceElementId = options.salePriceElementId || null;
	    this.salePriceIncludingTaxElementId = options.salePriceIncludingTaxElementId || null;
	    this.quickFindCodeElementId = options.quickFindCodeElementId || null;
	    this.productCodeElementId = options.productCodeElementId || null;	    
	    this.stockMessageElementId = options.stockMessageElementId || null;	
	    this.currentStockElementId = options.currentStockElementId || null;
	    this.weightId = options.weightId || 'Weight';
	    this.selectElements = options.selectElements;
	    this.productId = options.productId;	   
	    this.maximumQuantity = 0;
	    this.minimumQuantity = 0;	    	
	    this.onCompleted = options.onCompleted || null;
	    this.onUpdating = options.onUpdating || null;
	    this.onInStock = options.onInStock || null; 
	    this.onOutOfStock = options.onOutOfStock || null;	    
	    this.onPreorder = options.onPreorder || null;	    
	    this.ajaxUrl = options.ajaxUrl || '/AjaxScripts/ProductItemPrice.aspx'; 
	    
	     $(this.selectElements[this.selectElements.length-1]).addEvent('change', this.updateProductDetails.bind(this));
	     
	    for(var element = 0;element < this.selectElements.length;element++) {
	        var select = $(this.selectElements[element]);  
	        select.addEvent('change', this.updateProductDetails.bind(this));
	    }	  
	      
	    this.updateProductDetails();	    
	},
	
	updateProductDetails: function() {	  
	    if (typeof(this.onUpdating) == 'function') this.onUpdating();	    
	    var priceElement = $(this.priceElementId);
	    if (priceElement) priceElement.innerHTML = 'Updating...';	      
	    var productOptionIds = '';	      
	    for(var element = 0;element < this.selectElements.length;element++) {
            var select = $(this.selectElements[element]);    
            var productOptionId = select.options[select.selectedIndex].value;
            if (element > 0) productOptionIds += ",";            
            productOptionIds += productOptionId;
	    }	 
	    var myAjax = new Ajax(this.ajaxUrl, {onComplete:this.updatePrice.bind(this), data:"ProductId=" + this.productId + "&ProductOptionIds=" + productOptionIds, method: 'get'}).request();
	},
	
	updatePrice: function(response) {
	    if (response && response.length > 0) {	
	       
	        var product = eval('(' + response + ')');	        
	        
	        	                
	        if (product.currentStock == 0 && product.allowPreorder == 'False') {
	            if (typeof(this.onOutOfStock) == 'function') this.onOutOfStock();
	        } else if (product.currentStock == 0 && product.allowPreorder == 'True') {
	            if (typeof(this.onPreorder) == 'function') this.onPreorder();
	        } else {
	            if (typeof(this.onInStock) == 'function') this.onInStock();
	        }  
	        
	        if (this.quickFindCodeElementId) {
	            var quickFindCodeElement = $(this.quickFindCodeElementId);
	            if (quickFindCodeElement && product.quickFindCode) quickFindCodeElement.innerHTML = product.quickFindCode;
	        }
	        
	        if (this.productCodeElementId) {
	            var productCodeElement = $(this.productCodeElementId);
	            if (productCodeElement && product.code) productCodeElement.innerHTML = product.code;
	        }
	        
	        if (this.weightId) {
	            var weightElement = $(this.weightId);
	            if (weightElement && product.weight) weightElement.innerHTML = product.weight;
	        }
	        
	        if (this.stockMessageElementId) {
	            var stockMessageElement = $(this.stockMessageElementId);
	            if (stockMessageElement && product.stockMessage) stockMessageElement.innerHTML = product.stockMessage;
	        }
	        
	        if (this.currentStockElementId) {
	            var currentStockElement = $(this.currentStockElementId);
	            if (currentStockElement) currentStockElement.innerHTML = product.currentStock;
	        }
	        
	        if (this.priceElementId) {
	            var priceElement = $(this.priceElementId);
	            if (priceElement && product.price) { 
	                var price = parseFloat(product.price);                
	                if (priceElement.type == 'text'){
	                    priceElement.value = price;   
	                } else {
	                    priceElement.innerHTML = product.currencySymbol + price.toFixed(2);   
	                }                 
                }
	        }    
	        
	        if (this.priceIncludingTaxElementId) {
	            var priceElement = $(this.priceIncludingTaxElementId);
	            if (priceElement && product.price) { 
	                var price = (parseFloat(product.price) + parseFloat(product.tax));                
	                if (priceElement.type == 'text'){
	                    priceElement.value = price.toFixed(2);   
	                } else {
	                    priceElement.innerHTML = product.currencySymbol + price.toFixed(2);   
	                }                 
                }
	        } 
	        
	        if (this.salePriceElementId) {
	            var priceElement = $(this.salePriceElementId);
	            if (priceElement && product.salePrice) {
	                var price = parseFloat(product.salePrice);    
	                if (product.salePrice == 0) {
	                    priceElement.style.display = 'none';
	                } else {
	                    priceElement.style.display = '';	                                
	                    if (priceElement.type == 'text'){
	                        priceElement.value = price.toFixed(2);   
	                    } else {
	                        priceElement.innerHTML = product.currencySymbol + price.toFixed(2);   
	                    }	                
	                }          
                }
	        }    
	        
	        if (this.salePriceIncludingTaxElementId) {
	            var priceElement = $(this.salePriceIncludingTaxElementId);
	            if (priceElement && product.price) { 
	                var price = (parseFloat(product.salePrice) + parseFloat(product.salePriceTax));                
	                if (product.salePrice == 0) {
	                    priceElement.style.display = 'none';
	                } else {
	                    priceElement.style.display = '';
	                    if (priceElement.type == 'text'){
	                        priceElement.value = price.toFixed(2);   
	                    } else {
	                        priceElement.innerHTML = product.currencySymbol + price.toFixed(2);   
	                    }
	                }	                                     
                }
	        } 
	        
	        if (typeof(this.onCompleted) == 'function') this.onCompleted(product);	        
	    }
	},
	
	initalizeChecks: function(options){
	    if (!options.priceElementId) {
	        alert('No priceElement was supplied in the ProductDetails options.');
	        return false;
	    }	    
	    
	    if (!options.selectElements) {
	        alert('No selectElements were supplied in the ProductDetails options.');
	        return false;
	    }
	    
	    return true;
	}	
});