show_price_upon_input();

function swap_image(img) {
	document.getElementById('product_image').src = '/uploads/action_shot/' + img;
}

function swap_action_shot_image (img, img_large)
{
	document.getElementById('product_image').src = '/uploads/action_shot/' + img;
	if(img_large != '')
	{
	document.getElementById('large_product_image').value = '/uploads/action_shot/' + img_large;
	}
	else
	{
		document.getElementById('large_product_image').value = '';
	}
}

function swap_product_image(img, full_sized_img) {
        document.getElementById('product_image').src = img;
	document.getElementById('large_product_image').value = full_sized_img;
}

function show_price_upon_input() {
	
	document.getElementById('calculated_price').size = "8";
	
	var price = 'Invalid Quantity';
	var value = document.getElementById('product_quantity').value;
	
	// reset colors
	for(i = 0; i < quantities.length; i++) {		
		if(document.getElementById('pricing_tier_' + i)) {
			document.getElementById('pricing_tier_' + i).style.backgroundColor = 'FFFFFF';
		}
	}
	
	if(
		value != '' && 
		!isNaN(value) && 
		(value % increment) == 0 && 
		value >= increment && 
		value <= max_quantity &&
		value >= min_order_quantity
		) {
	
		found_level = -1;
		for(i = 0; i < quantities.length; i++) {													
			
			if(value == quantities[i]) { found_level = i; }
			else if(value < quantities[i]) { found_level = (i - 1); }
			
			if(found_level != -1) {
				
				price = (value * prices[found_level]) / increment;
				price = price.toFixed(2);
				document.getElementById('product_price').value = price;
				
				if(document.getElementById('tier_' + found_level + '_price_pre_discount')) {
					document.getElementById('product_price_pre_discount').value = (value * document.getElementById('tier_' + found_level + '_price_pre_discount').value) / increment;
				}
				
				// do some formatting for display
				price = '\$' + price;
				document.getElementById('pricing_tier_' + found_level).style.backgroundColor = 'CCCCCC';				
				document.getElementById('calculated_price').value = price;					
				break;
			}
		}			
	}		
	
	if(price == "Invalid Quantity") {
		document.getElementById('calculated_price').size = "13";
	}
	document.getElementById('calculated_price').value = price;	
}

function validate_quantity() {
	var value = document.getElementById('product_quantity').value;
    
	if(isNaN(value) || value % increment != 0 || value < increment || value > max_quantity || value < min_order_quantity) { 
		message = 'Please enter a valid quantity.\n\nThe quantity for this product must be in increments of ' + increment + '.\n\n';
		if(min_order_quantity > 0) { message += 'The minimum order quantity is ' + min_order_quantity + '\n\n'; }
		message += 'Please call on quantities over ' + max_quantity + '.\n\nThank you.'; 
		alert(message);
		return false;
	}
	
	return true;
}

function submit_discount_code() {
	var dc = new String(document.getElementById('discount_code').value);
	if(dc.length >= 5) {
		document.location.href = '/shopping/product.php?category_id=' + document.getElementById('category_id').value + "&id=" + document.getElementById('product_id').value + "&product_quantity=" + document.getElementById('product_quantity').value + "&discount_code=" + dc;
	}
}
