function selectAddress() {
    var form = document.forms['checkoutform'];
    var selected_id = form.saved_addresses.options[form.saved_addresses.selectedIndex].value;

    for (var i=0; i < addresses.length; i++) {
        if (addresses[i].id == selected_id) {
            form.shipping_companyname.value = decodeEntities(addresses[i].companyname);
            form.shipping_title.value = decodeEntities(addresses[i].title);
            form.shipping_firstname.value = decodeEntities(addresses[i].firstname);
            form.shipping_sirname.value = decodeEntities(addresses[i].sirname);
            form.shipping_street1.value = decodeEntities(addresses[i].street1);
            form.shipping_street2.value = decodeEntities(addresses[i].street2);
            form.shipping_streetnumber.value = decodeEntities(addresses[i].streetnumber);
            form.shipping_zipcode.value = decodeEntities(addresses[i].zipcode);
            form.shipping_city.value = decodeEntities(addresses[i].city);
            form.shipping_saveaddress.checked = true;
            
            // Find country
            for (var j=0; j < form.shipping_country.options.length; j++) {
                if (form.shipping_country.options[j].text == addresses[i].country) {
                    form.shipping_country.selectedIndex = j;
                    
                    //form.submit();
                    return;
                }
            }
            break;
        }
    }
}

function saveShoppingCart(submitButton) {
    form = document.getElementById('shoppingCartForm');
    form['submitValue'].name = submitButton.name;
    form.submit();
}

function clearShoppingCart(submitButton) {
    form = document.getElementById('shoppingCartForm');

    if (confirm("Den Warenkorb wirklich löschen?")) {
        form['submitValue'].name = submitButton.name;
        form.submit();
    }
}

function fundaShopProduct(_productId, _basePrice) {
    var productId = 0;
    var basePrice = 0.0;
    var featurePriceData = {};
    var photosData = {};
    var photoThumbnailUrl = '';
    var photoUpdateCallbackFunction = null;
    var priceDigitSeperator = '.';
    
    this.getProductId = function() {
        return productId;
    }
    
    this.getBasePrice = function() {
        return basePrice;
    }
    
    this.setFeaturePriceData = function(_featurePriceData) {
        featurePriceData = _featurePriceData;
    }
    
    this.setPhotosData = function(_photosData) {
        photosData = _photosData;
    }
    
    this.setPhotoThumbnailUrl = function(_photoThumbnailUrl) {
        photoThumbnailUrl = _photoThumbnailUrl;
    }
    
    this.setPhotoUpdateCallback = function(callback) {
        photoUpdateCallbackFunction = callback;
    }
    
    this.setPriceDigitSeperator = function(_priceDigitSeperator) {
        priceDigitSeperator = _priceDigitSeperator;
    }
    
    function getFeatureValueId(featureId) {
		featureFormElement = document.getElementById('feature_' + productId + '_' + featureId);
		
		if (featureFormElement == null) return null;

		switch (featureFormElement.nodeName.toLowerCase()) {
		    case 'select':
		        featureValueId = featureFormElement.options[featureFormElement.selectedIndex].value;
		        break;

		    case 'input':
		        featureValueId = featureFormElement.value;
		        break;
		}

		return featureValueId;
    }
    
    function getObjectLength(object) {
        objectLength = 0;

		$.each(object, function(i, n) {
		    objectLength++;
		});
		
		return objectLength;
    }
    
    this.updatePriceLabel = function() {
        newPrice = basePrice;
        priceLabel = document.getElementById('productPrice_' + productId);
        
        // Get quantity
        //quantity = document.getElementById('productQuantity_' + productId).value;
        
        priceRegex = /[0-9]+([,\.][0-9]+)?/;
        priceRegexSingleDigit = /^[0-9]+[,\.][0-9]$/;
        priceRegexNoDigit = /^[0-9]+$/;
        
        // Step through each feature
        $.each(featurePriceData, function(featureId, featureData) {
            if (getObjectLength(featureData) == 0) return;

            // Get selected feature value
            featureValueId = getFeatureValueId(featureId);

            if (featureValueId == null) return;
            
            // Get price change
            priceChange = featureData[featureValueId];
            
            // Increase the product price
            newPrice += priceChange;
        });

        // Make sure the price is shown with the correct number of digits.
        if (priceRegexSingleDigit.test(newPrice)) {
            newPrice = newPrice + '0';
        } else if (priceRegexNoDigit.test(newPrice)) {
            newPrice = newPrice + priceDigitSeperator + '00';
        }
    
        priceLabel.innerHTML = priceLabel.innerHTML.replace(priceRegex, newPrice);
    }
    
    this.updatePhotos = function() {
        div = document.getElementById('productPhotos_' + productId);

        // Only proceeed if photos are listed
        if (div != undefined) {
            div.innerHTML = '';

            $.each(photosData, function(photoId, photoData) {
                photoAcceptable = true;

                $.each(photoData.associations, function(featureId, association) {
                    // Test if feature value matches assocation.
                    if (featureId == 0) {
                        featureValueId = association;
                    } else {
                        featureValueId = getFeatureValueId(featureId);
                    }

                    if ((association == '*' || association == featureValueId) == false) {
                        photoAcceptable = false;
                        return;
                    }
                });
                
                if (photoAcceptable) {
                    // Add photo to div
                    var link = document.createElement('A');
                    if (photoThumbnailUrl == '') {
                        src = photoData.filename;
                    } else {
                        src = photoThumbnailUrl + '?photo_id=' + photoData.id + '&size=large';
                    }
                    
	                link.setAttribute('href', photoData.filename);
	                link.setAttribute('class', 'thickbox');
	                link.setAttribute('rel', 1);
	
	                var img = document.createElement('img');
	                img.setAttribute('src', src);
	                img.setAttribute('border', 0);
	                link.appendChild(img);
	                
	                // Check if we are adding first photo
	                if (div.innerHTML != '') {
	                    img.style.display = 'none';
	                }
	
	                div.appendChild(link);
                }
            });
        }
        
        photoUpdateCallbackFunction = photoUpdateCallbackFunction || function(){};
        photoUpdateCallbackFunction();
    }
    
    this.updateView = function() {
        this.updatePriceLabel();
        this.updatePhotos();
    }
    
    // Initialize
    productId = _productId;
    basePrice = _basePrice;
}