
	var allowedColors = 1;
	var selectedColors = 0;
	var selectedTextColor = 0;
	var selectedCoatColor = 0;

	selectedColorsArr = new Array();
	selectedColorsStr = new Array();

	var unit_price_array = new Array();
	var unit_extra_array = new Array();
	var order_extra_array = new Array();

	var currentUnitPrice = ('0.00').toFloat();
	var currentOrderPrice = ('0.00').toFloat();
	var currentDeliveryCharge = '0:0:Standard';
	//unit_extra_array.push('color_mode\:'+currentColorModeCharge);

	function setNumberOfColors(selectedIndex, count, unit_charge){
		if( selectedColors <= count ){
			allowedColors = count;
			unit_extra_array.remove('color_mode\:'+currentColorModeCharge);
			currentColorModeCharge = unit_charge;
			unit_extra_array.push('color_mode\:'+currentColorModeCharge);
			updateTotalPrice();
			currentColorModeID = selectedIndex;
		} else {
			$('color_mode_id'+currentColorModeID).setProperty('checked',true);
			alert('You are currently selecting more than '+count+' color'+(count>1 ? 's':'')+'. Please deselect '+(allowedColors-count)+' color'+(allowedColors-count>1 ? 's':'')+' then re-select the color mode');
		}
	}


	function getSelectedColors(){
		tmp1 = '';
		tmp2 = '';

		selectedColorsStr.each(function(item, index){ tmp1 += (index==0) ? item : (', '+item) });
		$('selected_colors_input').value = tmp1;

		selectedColorsArr.each(function(item, index){ tmp2 += (index==0) ? item : (','+item) });
		$('band_colors').value = tmp2;
	}

	function select_color(el, color_name, color_code){
		if( selectedColors < allowedColors ){
			selectedColors++;
			var inject_img = 	new Element('img', {
											'border': '0',
										    'styles': {
										        'position':'absolute',
										        'margin-top':'-5px',
										        'margin-left':'-5px',
										        'cursor':'pointer'
										    },
										    'events': {
										        'click': function(event){
										        	selectedColors--;
										            default_color_bg(this.getParent());
										            selectedColorsArr.remove(color_name+'\:'+color_code);
										            selectedColorsStr.remove(color_name);
										            getSelectedColors();
										            this.remove();
										            event.stopPropagation();
										        }
										    },
										    'src': 'images/checked2.png'
										});
			inject_img.injectTop(el);
			selectedColorsArr.include(color_name+':'+color_code);
			selectedColorsStr.include(color_name);
			getSelectedColors();
			select_color_bg(el);
		} else {
			alert('You can only choose '+allowedColors+' color(s) for this color mode');
		}

	}

	function IE6_select_color(el, color_name, color_code){
		if( selectedColors < allowedColors ){
			selectedColors++;
			var inject_img = 	new Element('div', {
											'border': '0',
										    'styles': {
										        'position':'absolute',
										        'margin-top':'-5px',
										        'margin-left':'-5px',
										        'height':'40px',
										        'width':'40px',
										        'filter':'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/checked2.png",sizingMethod="scale")',
										        'cursor':'pointer'
										    },
										    'events': {
										        'click': function(event){
										        	selectedColors--;
										            default_color_bg(this.getParent());
										            selectedColorsArr.remove(color_name+'\:'+color_code);
										            selectedColorsStr.remove(color_name);
										            getSelectedColors();
										            this.remove();
										            //event.stopPropagation();
										        }
										    }
										});
			inject_img.injectTop(el);
			selectedColorsArr.include(color_name+':'+color_code);
			selectedColorsStr.include(color_name);
			getSelectedColors();
			select_color_bg(el);
		} else {
			alert('You can only choose '+allowedColors+' color(s) for this color mode');
		}
	}

	function setSelectedTextColors(color_name, color_code){
		$('selected_text_color_input').value = color_name ? color_name : '';
		$('text_color').value = color_name ? color_name+':'+color_code : '';
	}

	function select_text_color(el, color_name, color_code){
		if( selectedTextColor < 1 ){
			selectedTextColor++;
			var inject_img = 	new Element('img', {
											'border': '0',
										    'styles': {
										        'position':'absolute',
										        'margin-top':'-5px',
										        'margin-left':'-5px',
										        'cursor':'pointer'
										    },
										    'events': {
										        'click': function(event){
										        	selectedTextColor--;
										            default_color_bg(this.getParent());
										            setSelectedTextColors('');
										            this.remove();
										            event.stopPropagation();
										        }
										    },
										    'src': 'images/checked2small.png'
										});
			inject_img.injectTop(el);
			setSelectedTextColors(color_name, color_code);
			select_color_bg(el);
		} else {
			alert('You can only choose '+allowedColors+' color(s) for this color mode');
		}

	}

	function IE6_select_text_color(el, color_name, color_code){
		if( selectedTextColor < 1 ){
			selectedTextColor++;
			var inject_img = 	new Element('div', {
											'border': '0',
										    'styles': {
										        'position':'absolute',
										        'margin-top':'-5px',
										        'margin-left':'-5px',
										        'height':'30px',
										        'width':'30px',
										        'filter':'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/checked2small.png",sizingMethod="scale")',
										        'cursor':'pointer'
										    },
										    'events': {
										        'click': function(event){
										        	selectedTextColor--;
										            default_color_bg(this.getParent());
										            setSelectedTextColors('');
										            this.remove();
										            //event.stopPropagation();
										        }
										    }
										});
			inject_img.injectTop(el);
			setSelectedTextColors(color_name, color_code);
			select_color_bg(el);
		} else {
			alert('You can only choose '+allowedColors+' color(s) for this color mode');
		}
	}

	function setSelectedCoatColors(color_name, color_code){
		$('selected_coat_color_input').value = color_name ? color_name : '';
		$('coat_color').value = color_name ? color_name+':'+color_code : '';
	}

	function select_coat_color(el, color_name, color_code){
		if( selectedCoatColor < 1 ){
			selectedCoatColor++;
			var inject_img = 	new Element('img', {
											'id': 'Testing',
											'border': '0',
										    'styles': {
										        'position':'absolute',
										        'margin-top':'-5px',
										        'margin-left':'-5px',
										        'cursor':'pointer'
										    },
										    'events': {
										        'click': function(event){
										        	selectedCoatColor--;
										            default_color_bg(this.getParent());
										            setSelectedCoatColors('');
										            this.remove();
										            event.stopPropagation();
										        }
										    },
										    'src': 'images/checked2small.png'
										});
			inject_img.injectTop(el);
			setSelectedCoatColors(color_name, color_code);
			select_color_bg(el);
		} else {
			alert('You can only choose '+allowedColors+' color(s) for this color mode');
		}

	}

	function IE6_select_coat_color(el, color_name, color_code){
		if( selectedCoatColor < 1 ){
			selectedCoatColor++;
			var inject_img = 	new Element('div', {
											'border': '0',
										    'styles': {
										        'position':'absolute',
										        'margin-top':'-5px',
										        'margin-left':'-5px',
										        'height':'30px',
										        'width':'30px',
										        'filter':'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/checked2small.png",sizingMethod="scale")',
										        'cursor':'pointer'
										    },
										    'events': {
										        'click': function(event){
										        	selectedCoatColor--;
										            default_color_bg(this.getParent());
										            setSelectedCoatColors('');
										            this.remove();
										            //event.stopPropagation();
										        }
										    }
										});
			inject_img.injectTop(el);
			setSelectedCoatColors(color_name, color_code);
			select_color_bg(el);
		} else {
			alert('You can only choose '+allowedColors+' color(s) for this color mode');
		}
	}

	function select_color_bg(el){
		el.setStyle('background-color','#eee');
	}

	function default_color_bg(el){
		el.setStyle('background-color','#fff');
	}

	function changeQty(cleanQty) {
		msg = $('price_message').innerHTML;
		qty = ($('qty').value).toInt();

		if( qty < 100 ){
			$('price_message').innerHTML = '(minimum 100)';
			if(cleanQty){
				$('qty').value = '';
			}
			currentUnitPrice = ('0.00').toFloat();
			updateTotalPrice();
		} else {
			found_price = false;
			prices.each(function(item, index){
				if((qty > (item.min_qty).toInt()-1) && (qty <(item.max_qty).toInt()+1)){
					currentUnitPrice = (item.unit_price).toFloat();
					$('price_message').innerHTML = '[$'+currentUnitPrice.toFixed(2)+' ea.]';
					updateTotalPrice();
					found_price = true;
				}
			});
		}
	}

	function changeDelivery(){
		tmp1 = currentDeliveryCharge.split('\:');
		unit_extra_array.remove('delivery\:'+tmp1[0]);
		order_extra_array.remove('delivery\:'+tmp1[1]);

		currentDeliveryCharge = $('delivery_time').getProperty('value');
		tmp2 = currentDeliveryCharge.split('\:');
		unit_extra_array.push('delivery\:'+tmp2[0]);
		order_extra_array.push('delivery\:'+tmp2[1]);
		updateTotalPrice();
	}

	function addExtra(name, unit_charge, order_charge){
		unit_extra_array.push(name+'\:'+unit_charge);
		order_extra_array.push(name+'\:'+order_charge);
		updateTotalPrice();
	}

	function removeExtra(name, unit_charge, order_charge){
		unit_extra_array.remove(name+'\:'+unit_charge);
		order_extra_array.remove(name+'\:'+order_charge);
		updateTotalPrice();
	}

	function updateOrderPrice(){
		final_order_price = 0.00;
		order_extra_array.each(function(item, index){
			tmp = item.split('\:');
			final_order_price += tmp[1].toFloat();
		});
		currentOrderPrice = final_order_price.toFloat();
		//updateTotalPrice();
	}

	function updateUnitPrice(){
		final_unit_price = 0.00;
		unit_extra_array.each(function(item, index){
			tmp = item.split('\:');
			final_unit_price += tmp[1].toFloat();
		});
		$('price').setProperty('value', final_unit_price.toFloat() + currentUnitPrice);
		//alert($('price').getProperty('value'));
		//updateTotalPrice();
	}

	function updateTotalPrice(){
		updateUnitPrice();
		updateOrderPrice()

		unit_price = $('price').getProperty('value').toFloat();
		total_qty = $('qty').getProperty('value') ? $('qty').getProperty('value').toInt() : 0;
		$('total_price').setHTML('$'+ (unit_price*total_qty + currentOrderPrice).toFixed(2));
	}

	window.addEvent('load', function(e){
		$('qty').addEvent('blur', function(e){
			changeQty(true);
		});

		$('qty').addEvent('keyup', function(e){
			changeQty(false);
		});

		$('delivery_time').addEvent('blur', function(e){
			changeDelivery();
		});

		$('delivery_time').addEvent('change', function(e){
			changeDelivery();
		});
	});
