function disable_input_field(domId){
	$$('.' + domId).each(function(element) {
		element.writeAttribute("disabled");
	});
};

function disable_and_submit(action, form, criteria_type){
	if (criteria_type == "main"){
		submit_form(action, form);
		disable_input_field('main_criteria_cbs');
	}
	else if (criteria_type == "photo"){
		submit_form(action, form);
		disable_input_field('main_criteria_cbs');
		disable_input_field('photo_criteria_cbs');
	}
	else if (criteria_type == "sub") {
		submit_form(action, form);
		disable_input_field('sub_criteria_cbs');
		disable_input_field('main_criteria_cbs');
	};
};

function submit_form(action, form) {
	new Ajax.Request(action, {asynchronous:true, evalScripts:true,  parameters:Form.serialize($(form))});
	return false;
};

function enable_form(domId) { $$('.' + domId).each(function(element) {element.removeAttribute("disabled");})};

function change_photographers_counter(){
	counter = count_photographers("photo_criteria_cbs");
}

function count_photographers(domId) {
	counter = 0;
	html_string = ""
	$$('.' + domId).each(function(n){
		if (n.checked) {
			counter += 1
			html_string += $('photo_span_'+ n.value).innerHTML;
			html_string += "<br />";
		};
	});
	
	if(html_string == "") {
		$('photographers_list_content_none').show();
		$('photographers_list_content').hide();
	} else {
		$('photographers_list_content_none').hide();
		$('photographers_list_content').innerHTML = html_string;
		$('photographers_list_content').show();
	}
	
	return counter;
}

function uncheck_checkboxes(class_name) {
	$$('.' + class_name).each(function(check_box){ check_box.checked = false;});
	if (class_name == "photo_criteria_cbs"){$('photographers_list_content').innerHTML = "none";}	
}

function uncheck_all_checkboxes() {
	uncheck_checkboxes("main_criteria_cbs");
	uncheck_checkboxes("sub_criteria_cbs");
	uncheck_checkboxes("photo_criteria_cbs");
	new Ajax.Request('/pictures/reset_search', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent(getAuthKey())});
	return false;
}

function updateLightboxLinks(id) {
	new Ajax.Request('/lightboxes/' + id + '/lightbox_items_count', {asynchronous:true, evalScripts:true,
		parameters:'authenticity_token=' + encodeURIComponent(getAuthKey())});
}

function addPicturesToLightbox(id) {
	new Ajax.Request('/lightboxes/' + id + '/lightbox_items', { asynchronous:true, evalScripts:true,
		parameters:Form.serialize($('lightbox_pictures_form')) + "&authenticity_token=" + encodeURIComponent(getAuthKey())
	});
}

function addPicturesToShoppingCart(id) {
	new Ajax.Request('/shopping_carts/' + id + '/cart_items', { asynchronous:true, evalScripts:true,
		parameters:Form.serialize($('lightbox_pictures_form')) + "&authenticity_token=" + encodeURIComponent(getAuthKey())
	});
}

function updateShoppingCartLinks(id) {
	new Ajax.Request('/shopping_carts/' + id + '/cart_items_count', {asynchronous:true, evalScripts:true,
		parameters:'authenticity_token=' + encodeURIComponent(getAuthKey())});
}

function updateShoppingCartDropdown() {
	new Ajax.Request('/pictures/refresh_result_shopping_carts_dropdown', {asynchronous:true, evalScripts:true,
		parameters:'authenticity_token=' + encodeURIComponent(getAuthKey())});
}

function updateShoppingCartViewDropdown() {
	new Ajax.Request('/cart_items/refresh_result_shopping_carts_dropdown', {asynchronous:true, evalScripts:true,
		parameters:'authenticity_token=' + encodeURIComponent(getAuthKey())});
}

function updateLightboxDropdown() {
	new Ajax.Request('/pictures/refresh_result_lightboxes_dropdown', {asynchronous:true, evalScripts:true,
		parameters:'authenticity_token=' + encodeURIComponent(getAuthKey())});
}

function updateLightboxesDropdown() {
	new Ajax.Request('/lightboxes/refresh_lightboxes_dropdown', {asynchronous:true, evalScripts:true,
		parameters:'authenticity_token=' + encodeURIComponent(getAuthKey())});
}

function updateLightboxItems() {
	new Ajax.Request('/lightbox_items/refresh_lightbox_items', {asynchronous:true, evalScripts:true,
		parameters:'authenticity_token=' + encodeURIComponent(getAuthKey())});
}

function setSessionLightbox(id) {
	new Ajax.Request('/lightboxes/set_session_lightbox/'+id, {asynchronous:true, evalScripts:true,
		parameters:'authenticity_token=' + encodeURIComponent(getAuthKey())});
}

function setSessionCart(id) {
	new Ajax.Request('/shopping_carts/set_session_cart/'+id, {asynchronous:true, evalScripts:true,
		parameters:'authenticity_token=' + encodeURIComponent(getAuthKey())});
}

// Deletes a list of selected (checkboxes) pictures from a given lightbox.
function deletePicturesFromLightbox(id) {
	new Ajax.Request('/lightboxes/' + id + '/destroy_lightbox_items', { asynchronous:true, evalScripts:true,
		parameters:Form.serialize($('lightbox_pictures_form')) + "&authenticity_token=" + encodeURIComponent(getAuthKey())
	});
}

// get the meta date values for the Meta Date with the given id
function getMetaDateValues() {
	new Ajax.Request('/meta_dates/get_meta_date_values/' + $F('meta_dates_id'), { asynchronous:true, evalScripts:true,
		parameters:Form.serialize($('meta_date_form')) + "&authenticity_token=" + encodeURIComponent(getAuthKey())
	});
}

function saveImageSelection(id) {
	new Ajax.Request('/pictures/save_selected_images/' + id, { asynchronous:true, evalScripts:true,
		parameters:"authenticity_token=" + encodeURIComponent(getAuthKey()) + "&selected=" + $('checkbox_for_image_'+id).checked
	});
}

function addMetaDateValueToForm() { 
new Ajax.Request('/meta_date_values/update_edit_form/' + $F("meta_date_values_id"), { asynchronous:true, evalScripts:true,
	parameters:Form.serialize($('meta_date_form')) + "&authenticity_token=" + encodeURIComponent(getAuthKey())
});
}

function deletePicturesFromShoppingCart(id) {
	ids = getSelectableCartItems();

	new Ajax.Request('/shopping_carts/' + id + '/destroy_cart_items', { asynchronous:true, evalScripts:true,
		parameters:ids.join("&") + "&authenticity_token=" + encodeURIComponent(getAuthKey())
	});

	if ($$('.cart_item_checkboxes').size() == 0) {
		$('request_quote_button').disabled = true;
	}
}

function movePicturesToShoppingCart(fromId, toId) {
	ids = getSelectableCartItems();

	new Ajax.Request('/shopping_carts/' + fromId + '/move_cart_items', { asynchronous:true, evalScripts:true,
		parameters:ids.join("&") + "&to_id=" + toId + "&authenticity_token=" + encodeURIComponent(getAuthKey())
	});
	
	if ($$('.cart_item_checkboxes').size() == 0) {
		$('request_quote_button').disabled = true;
	}
}

function getSelectableCartItems() {
	ids = [];

	$$('.cart_item_checkboxes').each(function(e) {
		if (e.checked) {
			ids.push("selectable_cart_item_ids[]=" + e.value);
			$('cart_item1_' + e.value).remove();
			$('cart_item2_' + e.value).remove();
		}
	});

	return ids;
}

// Shopping Cart - Quote Request

// Internal State of cart items
var formModelHashes = {
	"CartItem": {},
	"CartItemDistribution": {},
	"CartItemMediaUse": {},
	"CartItemPostProductionService": {}
};

// Adds a single item (attribute value) to a given model.
function addSingleItemToCartItem(cartItemId, index, model, attribute, domIdToGetValueFrom, appendToDomId) {
	if (formModelHashes[model][cartItemId] == null) {
		formModelHashes[model][cartItemId] = [];
	}
	
	//formModelHashes[model][cartItemId][attribute] = $(domIdToGetValueFrom).value;
	formModelHashes[model][cartItemId][attribute] = $(domIdToGetValueFrom).options[$(domIdToGetValueFrom).selectedIndex].text;
	$(appendToDomId).innerHTML = formModelHashes[model][cartItemId][attribute];
	
	if (model == 'CartItem') {
		new Ajax.Request('/cart_items/' + cartItemId, { asynchronous:true, evalScripts:true, method:"PUT",
			parameters:"cart_item[" + attribute + "]=" + $(domIdToGetValueFrom).value +
			"&authenticity_token=" + encodeURIComponent(getAuthKey())
		});
	}
}

// Adds an item (attribute value) to a "has to many" association.
function addMultipleItemsToCartItem(cartItemId, index, model, attribute, domIdToGetValueFrom, appendToDomId) {
	if (formModelHashes[model][cartItemId] == null) {
		formModelHashes[model][cartItemId] = [];
	}

	if (formModelHashes[model][cartItemId][attribute] == null) {
		formModelHashes[model][cartItemId][attribute] = [];
	}
	
//	if (!formModelHashes[model][cartItemId][attribute].include($(domIdToGetValueFrom).value)) {
	if (!formModelHashes[model][cartItemId][attribute].include($(domIdToGetValueFrom).options[$(domIdToGetValueFrom).selectedIndex].text)) {
		formModelHashes[model][cartItemId][attribute].push($(domIdToGetValueFrom).options[$(domIdToGetValueFrom).selectedIndex].text);
		//formModelHashes[model][cartItemId][attribute].push($(domIdToGetValueFrom).value);
		
		new Ajax.Request('/cart_items/' + cartItemId + "/add", { asynchronous:true, evalScripts:true, method:"POST",
			parameters:"model=" + model + "&attribute=" + attribute + "&value=" + $(domIdToGetValueFrom).value +
			"&authenticity_token=" + encodeURIComponent(getAuthKey())
		});
	}
	$(appendToDomId).innerHTML = formModelHashes[model][cartItemId][attribute].join(", ");
}

// Special case for adding a post production service item from a checkbox.
function addPpsToCartItem(cartItemId, index, ppsValue) {
	if (formModelHashes['CartItemPostProductionService'][cartItemId] == null) {
		formModelHashes['CartItemPostProductionService'][cartItemId] = [];
	}
	
	if (formModelHashes['CartItemPostProductionService'][cartItemId]['post_production_service_value'] == null) {
		formModelHashes['CartItemPostProductionService'][cartItemId]['post_production_service_value'] = [];
	}
	
	if (formModelHashes['CartItemPostProductionService'][cartItemId]['post_production_service_value'].include(ppsValue)) {
		formModelHashes['CartItemPostProductionService'][cartItemId]['post_production_service_value'] =
			formModelHashes['CartItemPostProductionService'][cartItemId]['post_production_service_value'].without(ppsValue);
			
		new Ajax.Request('/cart_items/' + cartItemId + "/remove", { asynchronous:true, evalScripts:true, method:"DELETE",
			parameters:"model=CartItemPostProductionService&attribute=post_production_service_value&value=" + ppsValue +
			"&authenticity_token=" + encodeURIComponent(getAuthKey())
		});
	} else {
		formModelHashes['CartItemPostProductionService'][cartItemId]['post_production_service_value'].push(ppsValue);
		
		new Ajax.Request('/cart_items/' + cartItemId + "/add", { asynchronous:true, evalScripts:true, method:"POST",
			parameters:"model=CartItemPostProductionService&attribute=post_production_service_value&value=" + ppsValue +
			"&authenticity_token=" + encodeURIComponent(getAuthKey())
		});
	}
}

// Clears the display and removes all values from a given cart item on the server side.
function clearCartItemValues(cartItemId, index) {
	$('display_ci_duration_' + cartItemId + '_' + index).innerHTML = '';
	$('display_cid_distribution_value_' + cartItemId + '_' + index).innerHTML = '';
	$('display_cimu_media_use_value_' + cartItemId + '_' + index).innerHTML = '';
	$('display_ci_circulation_' + cartItemId + '_' + index).innerHTML = '';
	$$('.pps_checkbox_' + cartItemId + '_' + index).each(function(e) {e.checked = false});

	if (formModelHashes["CartItem"][cartItemId] != null) {
		formModelHashes["CartItem"][cartItemId] = [];
	}
	if (formModelHashes["CartItemDistribution"][cartItemId] != null) {
		formModelHashes["CartItemDistribution"][cartItemId] = [];
	}
	if (formModelHashes["CartItemMediaUse"][cartItemId] != null) {
		formModelHashes["CartItemMediaUse"][cartItemId] = [];
	}
	if (formModelHashes["CartItemPostProductionService"][cartItemId] != null) {
		formModelHashes["CartItemPostProductionService"][cartItemId] = [];
	}
	
	new Ajax.Request('/cart_items/' + cartItemId + "/clear", { asynchronous:true, evalScripts:true, method:"DELETE",
		parameters:"authenticity_token=" + encodeURIComponent(getAuthKey())
	});
}

// Sets the initial state from the database values to the internal hashmaps.
function prefillFromString(cartItemId, model, attribute, str) {
	if (str != null && str.length > 0) {
		if (formModelHashes[model][cartItemId] == null) {
			formModelHashes[model][cartItemId] = [];
		}

		if (formModelHashes[model][cartItemId][attribute] == null) {
			formModelHashes[model][cartItemId][attribute] = [];
		}

		str.split(", ").each(function(v) {
			formModelHashes[model][cartItemId][attribute].push(v);
		});
	}
}

// Copies all values from cart item to another and persists the changes via ajax.
function copyAllCartItems(cartItemIdToClone, toArray) {
	if (toArray != null && cartItemIdToClone != null) {
		index = 0;
		toArray.each(function(cartItem) {
			if (cartItem != cartItemIdToClone) {
				clearCartItemValues(cartItem, index);
				
				if (formModelHashes["CartItem"][cartItemIdToClone] != null) {
					formModelHashes["CartItem"][cartItem] = {};
					formModelHashes["CartItem"][cartItem]["duration"] = formModelHashes["CartItem"][cartItemIdToClone]["duration"];
					formModelHashes["CartItem"][cartItem]["circulation"] = formModelHashes["CartItem"][cartItemIdToClone]["circulation"];
					
					$('display_ci_duration_' + cartItem + '_' + index).innerHTML = formModelHashes["CartItem"][cartItem]["duration"];
					$('display_ci_circulation_' + cartItem + '_' + index).innerHTML = formModelHashes["CartItem"][cartItem]["circulation"];
					
					new Ajax.Request('/cart_items/' + cartItem, { asynchronous:true, evalScripts:true, method:"PUT",
						parameters:"cart_item[duration]=" + formModelHashes["CartItem"][cartItem]["duration"] +
						"&authenticity_token=" + encodeURIComponent(getAuthKey())
					});
					
					new Ajax.Request('/cart_items/' + cartItem, { asynchronous:true, evalScripts:true, method:"PUT",
						parameters:"cart_item[circulation]=" + formModelHashes["CartItem"][cartItem]["circulation"] +
						"&authenticity_token=" + encodeURIComponent(getAuthKey())
					});
				}
				
				if (formModelHashes["CartItemDistribution"][cartItemIdToClone] != null) {
					formModelHashes["CartItemDistribution"][cartItem] = {};
					formModelHashes["CartItemDistribution"][cartItem]["distribution_value"] =
						formModelHashes["CartItemDistribution"][cartItemIdToClone]["distribution_value"];
						
					$('display_cid_distribution_value_' + cartItem + '_' + index).innerHTML =
						formModelHashes["CartItemDistribution"][cartItem]["distribution_value"];
						
					new Ajax.Request('/cart_items/' + cartItem + "/add", { asynchronous:true, evalScripts:true, method:"POST",
						parameters:"model=CartItemDistribution&attribute=distribution_value&value=" +
						formModelHashes["CartItemDistribution"][cartItem]["distribution_value"] +
						"&authenticity_token=" + encodeURIComponent(getAuthKey())
					});
				}
				
				if (formModelHashes["CartItemMediaUse"][cartItemIdToClone] != null) {
					formModelHashes["CartItemMediaUse"][cartItem] = {};
					formModelHashes["CartItemMediaUse"][cartItem]["media_use_value"] =
						formModelHashes["CartItemMediaUse"][cartItemIdToClone]["media_use_value"];
						
					$('display_cimu_media_use_value_' + cartItem + '_' + index).innerHTML =
						formModelHashes["CartItemMediaUse"][cartItem]["media_use_value"];
						
					new Ajax.Request('/cart_items/' + cartItem + "/add", { asynchronous:true, evalScripts:true, method:"POST",
						parameters:"model=CartItemMediaUse&attribute=media_use_value&value=" +
						formModelHashes["CartItemMediaUse"][cartItem]["media_use_value"] +
						"&authenticity_token=" + encodeURIComponent(getAuthKey())
					});
				}
				
				if (formModelHashes["CartItemPostProductionService"][cartItemIdToClone] != null) {
					formModelHashes["CartItemPostProductionService"][cartItem] = {};
					formModelHashes["CartItemPostProductionService"][cartItem]["post_production_service_value"] =
						formModelHashes["CartItemPostProductionService"][cartItemIdToClone]["post_production_service_value"];
					
					ppsValues = formModelHashes["CartItemPostProductionService"][cartItem]["post_production_service_value"];
					
					$$('.pps_checkbox_' + cartItem + '_' + index).each(function(e) {
						if (ppsValues.include(e.value)) {
							e.checked = true;
							
							new Ajax.Request('/cart_items/' + cartItem + "/add", { asynchronous:true, evalScripts:true, method:"POST",
								parameters:"model=CartItemPostProductionService&attribute=post_production_service_value&value=" + e.value +
								"&authenticity_token=" + encodeURIComponent(getAuthKey())
							});
						}
					});
				}
			}
			
			index++;
		});
	}
}

function fixHeight() {
	$('suchergebnisse_container').setStyle({'height': (document.body.clientHeight - 300) + "px"});
}

function submitFormular(theAction)
{
	document.forms[0].action = theAction;
	document.forms[0].submit();
} 

