
	/*
	* monik.if.ua
	* (c) Devtrix, 2009
	* Author: Alex Baskov, 2009
	*/



	////
	// shows big product image
	//
	function showImage(imgFileName)
	{
		if (imgFileName != "" && (imgElm = document.getElementById('box_image')))
		{
			imgFile = '/images/products/b/' + imgFileName;
			currentImg = imgElm.style.backgroundImage;

			if (imgFile != "")
			{
				imgElm.style.backgroundImage = 'url(/img/clear.gif)';
				imgElm.style.backgroundImage = 'url(' + imgFile + ')';
			}
		}

		return true;
	} // showBigImage()


	////
	// additional function for FILTER() method
	//
	function get_selected_records(category)
	{
		category = category || null;
		var frm = document.form_options;
		var selected_records = [];
		var atLeastOneCheckedInCategory = false;

		if (category != null)
		{
			category_options_count = options_count[category];

			for (var i = 1; i <= category_options_count; i++)
			{
				categoryElmName = 'check_' + category + '_' + i;
				categoryElm = frm.elements[categoryElmName];

				if (categoryElm != null)
				{
					if (categoryElm.checked)
					{
						atLeastOneCheckedInCategory = true;
						tmpArr = records[category][categoryElm.value];
						if (tmpArr && tmpArr != null && tmpArr.length > 0)
						{
							selected_records = selected_records.concat(tmpArr);
						}
						tmpArr = [];
					}
				}
			}

			// if nothing filtered, all must be selected for a category
			if ( (typeof(selected_records) == "object") && (!(selected_records.length > 0)) )
			{
				// if not checked - catergory wasn't included in filter, showing all. if at least one was CHECKED - then such records were not found.
				if (!atLeastOneCheckedInCategory)
				{
					selected_records = records_all;
				}
			}
		}

		atLeastOneChecked = atLeastOneChecked || atLeastOneCheckedInCategory;

		return selected_records;
	}


	////
	// filters products
	//
	function filter()
	{
		atLeastOneChecked = false;
		atLeastOneFound = false;
		divNotFound = document.getElementById('div_not_found');
		divFeatured = document.getElementById('div_featured');
		divFound = document.getElementById('div_found');
		divLoading = document.getElementById('div_loading');

		//divLoading.style.display = "block";

		filtered_records_brand = get_selected_records('brand');
		filtered_records_display = get_selected_records('display');
		filtered_records_resolution = get_selected_records('resolution');
		filtered_records_aspect_ratio = get_selected_records('aspect_ratio');
		filtered_records_response_time = get_selected_records('response_time');
		filtered_records_tv = get_selected_records('tv');
		filtered_records_dvi = get_selected_records('dvi');
		filtered_records_hdmi = get_selected_records('hdmi');
		filtered_records_price = get_selected_records('price');

		filtered_records = (atLeastOneChecked) ? array_intersect(filtered_records_brand, filtered_records_display, filtered_records_resolution, filtered_records_aspect_ratio, filtered_records_response_time, filtered_records_tv, filtered_records_dvi, filtered_records_hdmi, filtered_records_price) : records_featured;

		for (var i = 0; i < records_all.length; i++)
		{
			tmpID = records_all[i];

			if (tmpID > 0 && (boxElm = document.getElementById("monitor_" + tmpID)))
			{
				if (in_array(tmpID, filtered_records))
				{
					atLeastOneFound = true;
					boxElm.style.display = "block";
				}
				else
				{
					boxElm.style.display = "none";
				}
			}
		}

		if (atLeastOneChecked)
		{
			divFeatured.style.display = "none";
			divNotFound.style.display = (atLeastOneFound == false) ? "block" : "none";
			divFound.style.display = (atLeastOneFound == false) ? "none" : "block";
		}
		else
		{
			divFeatured.style.display = "block";
			divNotFound.style.display = "none";
			divFound.style.display = "none";
		}

		//divLoading.style.display = "none";

		return true;
	} // /filter()



	////
	// checks/un-checks all options. if category is specified - checks/un-checks only for this category.
	//
	function toggleAll(category)
	{
		checkAllElm = document.getElementById('check_toggle_all');
		category = category || "all";
		checkState = (category == "all") ? checkAllElm.checked : !document.form_options.elements['check_' + category + '_1'].checked;

		for (cat in options_count)
		{
			if (category == "all" || category == cat)
			{
  			for (var i = 1; i <= options_count[cat]; i++)
  			{
	  			categoryElmName = 'check_' + cat + '_' + i;
					categoryElm = document.form_options.elements[categoryElmName];
					categoryElm.checked = checkState;
				}
			}
		}

		filter();
		return true;
	} // /toggleAll()



	////
	// adds product to the shopping cart
	//
	function addToCart(productID)
	{
		if (productID > 0)
		{
			var frm = document.getElementById("form_shopping_cart");

			if (frm)
			{
				frm.elements.action.value = "add_item";
				frm.elements.id.value = productID;
				frm.submit();
				return true;
			}
		}

		return false;
	}
	// /addToCart();



	// removes specific item from shopping cart
	function deleteItem(itemID)
	{
		str = "Видалити цей товар?";

		if (itemID > 0 && (frm = document.getElementById('form_shopping_cart')))
		{
			if (confirm(str))
			{
				frm.elements.action.value = "delete_item";
				frm.elements.id_to_delete.value = itemID;
				frm.submit();
			}
		}

		return true;
	} // /deleteItem()



	// removes items from the shopping cart
	function clearItems()
	{
		str = "Очистити кошик?";

		if (frm = document.getElementById('form_shopping_cart'))
		{
			if (confirm(str))
			{
				frm.elements.action.value = "clear_items";
				frm.submit();
			}
		}

		return true;
	} // /clearItems()



	// updates quantity of items
	function updateItems()
	{
		if (frm = document.getElementById('form_shopping_cart'))
		{
			frm.elements.action.value = "update_items";
			frm.submit();
		}

		return true;
	} // /updateItems()




	/*
	* ARRAY FUNCTIONS
	* php2js equivalents
	*/

	function array_intersect()
	{
		// http://kevin.vanzonneveld.net
		// +   original by: Brett Zamir (http://brettz9.blogspot.com)
		// %        note 1: These only output associative arrays (would need to be
		// %        note 1: all numeric and counting from zero to be numeric)
		// *     example 1: $array1 = {'a' : 'green', 0:'red', 1: 'blue'};
		// *     example 1: $array2 = {'b' : 'green', 0:'yellow', 1:'red'};
		// *     example 1: $array3 = ['green', 'red'];
		// *     example 1: $result = array_intersect($array1, $array2, $array3);
		// *     returns 1: {0: 'red', a: 'green'}

		var arr1 = arguments[0], retArr = {};
		var k1 = '', arr = {}, i = 0, k = '';

		arr1keys:
		for (k1 in arr1)
		{
			arrs:
			for (i=1; i < arguments.length; i++)
			{
				arr = arguments[i];
				for (k in arr)
				{
					if (arr[k] === arr1[k1])
					{
						if (i === arguments.length-1)
						{
							retArr[k1] = arr1[k1];
						}
						// If the innermost loop always leads at least once to an equal value, continue the loop until done
						continue arrs;
					}
				}
				// If it reaches here, it wasn't found in at least one array, so try next value
				continue arr1keys;
			}
		}

		return retArr;
	}



	function in_array(needle, haystack, argStrict)
	{
		// Checks if the given value exists in the array
		//
		// version: 903.1614
		// discuss at: http://phpjs.org/functions/in_array
		// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
		// *     returns 1: true
		var found = false, key, strict = !!argStrict;

		for (key in haystack)
		{
			if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle))
			{
				found = true;
				break;
			}
		}

		return found;
	}



	function array_unique( array )
	{
		// http://kevin.vanzonneveld.net
		// +   original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
		// +      input by: duncan
		// +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +   bugfixed by: Nate
		// +      input by: Brett Zamir (http://brettz9.blogspot.com)
		// +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +   improved by: Michael Grier
		// %        note 1: the second argument, sort_flags is not implemented
		// *     example 1: array_unique(['Kevin','Kevin','van','Zonneveld','Kevin']);
		// *     returns 1: ['Kevin','van','Zonneveld']
		// *     example 2: array_unique({'a': 'green', 0: 'red', 'b': 'green', 1: 'blue', 2: 'red'});
		// *     returns 2: {'a': 'green', 0: 'red', 1: 'blue'}

		var key = '', tmp_arr1 = {}, tmp_arr2 = {};
		var val = '';
		tmp_arr1 = array;

		var __array_search = function (needle, haystack)
		{
			var fkey = '';
			for (fkey in haystack)
			{
				if ((haystack[fkey] + '') === (needle + ''))
				{
					return fkey;
				}
			}
			return false;
		};

		for (key in tmp_arr1)
		{
			val = tmp_arr1[key];
			if (false === __array_search(val, tmp_arr2))
			{
				tmp_arr2[key] = val;
			}

			delete tmp_arr1[key];
		}

		return tmp_arr2;
	} // /array_unique()
