/**
 * This function ensures that all quantity fields on the current page in to 1.
 * If a field has empty value, or value 0, the value is set to 1.
 */
function ensure_all_quantities_one()
{
	objForm = document.productform;
	if (!objForm || !objForm.elements)
	{
		alert("ensure_all_quantities_one() => productform not defined");
		return false;
	}
	
	re = new RegExp("^tx_commerce_pi1\\[artAddUid\\]\\[\\d+\\]\\[count\\]$");
	if (!re)
	{
		alert("ensure_all_quantities_one() => could not create regular expression");
		return false;
	}

	success = true;
	count = objForm.elements.length;

	for (var i = 0; i < count; i++)
	{
		objField = objForm.elements[i];
		if (objField.name)
		{
			strFieldName = objField.name;
			if (strFieldName.match(re))
			{
				success &= ensure_quantity_one(strFieldName);
			}
		}
	}
	
	return success;
}

/**
 * This function ensures that the quantity field has a value of at least 1.
 * If the field has empty value, or value 0, the value is set to 1.
 */
function ensure_quantity_one(strFieldName)
{
	var objForm;
	var objField;

	objForm = document.productform;
	if (!objForm || !objForm.elements)
	{
		alert("ensure_quantity_one() => productform not defined");
		return false;
	}
	
	objField = objForm.elements[strFieldName];
	if (objField && objField.value != null)
	{
		if (trim11(objField.value) == "" || parseInt(objField.value) == 0)
		{
			objField.value = "1";
		}		
	}
	
	return true;
}


/**
 * This function gets the computed style of an element.
 * Source: http://www.robertnyman.com/2006/04/24/get-the-rendered-style-of-an-element/
 */
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle)
	{
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle)
	{
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

/**
 * This function toggles visibility of the side panel basket.
 */
function toggle_sidebasket(strElementId)
{
	var objElement = document.getElementById(strElementId);
	var objElementskjul = document.getElementById('kurvskjul');
	var objElementvis = document.getElementById('kurvvis');

	
	if (getStyle(objElement, "display") != "none")
	{
		document.cookie = "sidebasket=invisible; path=/";
		objElement.style.display = "none";
		objElementskjul.style.display = "none";
		objElementvis.style.display = "block";
		/*objElement.previousSibling.previousSibling.innerHTML = "Vis indholdet af kurven";*/
	}
	else
	{		
		document.cookie = "sidebasket=visible; path=/";
		objElement.style.display = "block";
		objElementskjul.style.display = "block";
		objElementvis.style.display = "none";

		/*objElement.previousSibling.previousSibling.innerHTML = "Skjul indholdet af kurven";*/
	}
}

/**
 * This function trims whitespace from a given string.
 * Source: http://blog.stevenlevithan.com/archives/faster-trim-javascript
 */
function trim11 (str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}



/**
 * This function places an item in the basket. If no quantity is specified
 * for the item, the quantity is set to "1" before the form is submitted.
 */
function put_in_basket(strFieldName)
{
	if (ensure_quantity_one(strFieldName))
	{
		document.productform.submit();
		
	}
	else
	{
		alert("put_in_basket() => failed to set quantity");
	}
}

/**
 * This functions returns the value of a client-side cookie.
 */
function get_cookie(search_name)
{
	var tab_cookies = document.cookie.split( ';' );
	
	for ( i = 0; i < tab_cookies.length; i++ )
	{
		var cookie_tmp = tab_cookies[i].split('=');
		var cookie_name = cookie_tmp[0].replace(/^\s+|\s+$/g, '');c
		if (cookie_name==search_name)
		{
			if (cookie_tmp.length>1)
			{
				return unescape( cookie_tmp[1].replace(/^\s+|\s+$/g, '') );
			}
			return null;
		}
	}

	return null;
}



/*************************************************/

/**
 * JavaScript functions handling CSS input elements
 */

function has_class(objElement, strClass)
{
	if (objElement.className)
	{
		var strClassUpper = strClass.toUpperCase();
		var strClasses = objElement.className.split(" ");
		
		for (var i = 0; i < strClasses.length; i++)
		{
			if (strClasses[i].toUpperCase() == strClassUpper)
			{
				return true;
			}
		}
		
		return false;
	}
	else
	{
		return false;
	}
}

function add_class(objElement, strClass)
{
	if (!has_class(objElement, strClass))
	{
		if (objElement.className && objElement.className.length > 0)
		{
			objElement.className += (" " + strClass);
		}
		else
		{
			objElement.className = strClass;
		}
	}
}

function rem_class(objElement, strClass)
{
	if (has_class(objElement, strClass))
	{
		var strClassUpper = strClass.toUpperCase();
		var strClasses = objElement.className.split(" ");
		
		for (var i = 0; i < strClasses.length; i++)
		{
			if (strClasses[i] == "" || strClasses[i].toUpperCase() == strClassUpper)
			{
				strClasses.splice(i, 1);
				i--;
			}
		}
		
		objElement.className = strClasses.join(" ");
	}
}

function list_mouseover(objElement)
{
	objElement.style.zIndex = 1;
	add_class(objElement, "hover");
}

function list_mouseout(objElement)
{
	objElement.style.zIndex = 0;
	rem_class(objElement, "hover");
}

function listitem_mouseover(objElement)
{
	add_class(objElement, "hover");
}

function listitem_mouseout(objElement)
{
	rem_class(objElement, "hover");
}

function listitem_click(objElement, strValue, strText, strValueField, strTextField)
{
	objValueField = document.getElementById(strValueField);
	objTextField = document.getElementById(strTextField);

	objValueField.value = strValue;
	objTextField.innerHTML = strText;
}

function text_return_submit(objEvent, strFormName)
{
	var keynum;

	if(window.event) // IE
	{
		keynum = objEvent.keyCode;
	}
	else if(objEvent.which) // Netscape/Firefox/Opera
	{
		keynum = objEvent.which;
	}

	if (keynum == 13)
	{
		document.forms[strFormName].submit();
	}
}

