function CartAction(formName, stockQuantity){
	var FormObject = document.forms[formName];
	var Quantity = Math.abs(FormObject.elements['Quantity'].value);
	var CurrentCartQuantity = (FormObject.elements['CurrentCartQuantity']) ? Math.abs(FormObject.elements['CurrentCartQuantity'].value) : 0;
	//window.status = "q:" + Quantity + "cq:" + CurrentCartQuantity;
	if((Quantity+CurrentCartQuantity) > stockQuantity){
		alert('\nOBS!\n\nDen bestilte mængde overstiger lagerbeholdningen.\nKontakt venligst Vintjeneren.dk for mere info.\n')
	} else {
		var PricePerItem;
        var PriceTList = FormObject.elements['ProductPrice'].value;
        var PriceTArr = PriceTList.split("*");
        var PriceList = PriceTArr[0];
        
        var PriceListArray = PriceList.split(';');
        for(i=0;i<PriceListArray.length;i++){
			var PriceArray = PriceListArray[i].split('=');
			if((Quantity + CurrentCartQuantity) >= PriceArray[0]){
                PricePerItem = PriceArray[1];
                PricePerItemInclVat = PriceArray[2];
            }
        }
        
		if(arguments[2] == 1) {
			var ProductTitle = FormObject.elements['ProductTitle'].value;
			var ProductNumber = FormObject.elements['ProductNumber'].value;
			var ConfirmMessage = 'ÆNDRING AF INDKØBSKURVEN\n\n';
			ConfirmMessage += 'Du er ved at ændre i din indkøbskurv\n\n';
			ConfirmMessage += 'Produkt: ' + ProductTitle + '\n';
			ConfirmMessage += 'Produktnummer: ' + ProductNumber + '\n';
			ConfirmMessage += 'Antal: ' + Quantity + '\n';
			ConfirmMessage += 'Pris/stk: ' + PricePerItemInclVat + ' (inkl moms)\n';
			if(confirm(ConfirmMessage)){
				FormObject.submit();
			}
		} else {
			var ProductTitle = FormObject.elements['ProductTitle'].value;
			var ProductNumber = FormObject.elements['ProductNumber'].value;
			var ConfirmMessage = 'LÆG I INDKØBSKURVEN\n\n';
			ConfirmMessage += 'Du er ved at føje følgende produkt til din indkøbskurv\n\n';
			ConfirmMessage += 'Produkt: ' + ProductTitle + '\n';
			ConfirmMessage += 'Produktnummer: ' + ProductNumber + '\n';
			ConfirmMessage += 'Antal: ' + Quantity + '\n';
			ConfirmMessage += 'Pris/stk: ' + PricePerItemInclVat + ' (inkl moms)\n';
			if(confirm(ConfirmMessage)){
				FormObject.submit();
			}
		}
	}
}


// GLOBAL FUNCTIONS
// ----------------------------------------------------------

	function getArgs(){	
		var args = new Object(); 
		var query = location.search.substring(1); 
		var pairs = query.split("&"); 
		for (i=0;i<pairs.length;i++){ 
			var pos = pairs[i].indexOf('='); 
			if (pos == -1) continue; 
			var argname = pairs[i].substring(0,pos); 
			var value = pairs[i].substring(pos+1); 
			args[argname] = unescape(value); 
		} 
		return args;	
	} 

	var args = getArgs();

	function fncValidateForm(aryArgs, lngLCID)  { 
		// v3.0 browsers
		// This function validates forms
		// The input is an array of arguments with 3 arguments per form field
		// The array must have this syntax: 'Form field name', 'Text to alert in case of error', 'Type of validation'
		// Example: 'companyName', 'Company name', 'R', 'email', 'Email', 'RisEmail'
		// Requires the function fncFindObj()
		// Version 1.0, dec 2000, by S&#248;ren Larsen
		var objField; // The form field object matching the given field name
		var strFieldAlertText; // The given text that represent the form field in the alert box
		var strTypeOfValidation; // The given validation string
			// Possible values: 
			// - R - input is required
			// - RisEmail - input is required, must be an email address (one or more characters + @ + 3 or more characters and at least one dot after the first lette after the @)
			// - NisEmail - input is NOT required, but if input is given it must be an email address
			// - RisChecked2:3 - input is required, between 2 and 3 of the checkboxes must checked
			// - NisChecked0:3 - input is not required, no more that 3 of the checkboxes can be checked
			// - RisNum - input is required, must be a number
			// - NisNum - input is NOT required, but if input is given it must be a number
			// - RinRange10:400 - input is required, must be a number in this case in the range 10 to 400
			// - NinRange1:5 - input is NOT required, but if input is given it must be a number in this case in the range 1 to 5
		var i;	// Counter for iterations
		var pAt;	// Pointer for email '@'
		var pDot;	// Pointer for email '.'
		var lngCheckedFields; // Count number of checked checkboxes
		var n; // Counter for array of checkboxes / radio buttons
		var pColon;	// Pointer for ':' in numeric ranges
		var lngMin;	// Minimum value for numeric ranges
		var lngMax;	// Maximum value for numeric ranges
		var strErrors = ''; // Used to build string for alert box
		
		if (lngLCID == 1030) {
			var strMsgGeneral1 = 'Du mangler at udfylde nogle felter.';
			var strMsgGeneral2 = 'Udfyld venligst:';
			var strMsgGeneral3 = 'N&#229;r du har udfyldt felterne, kan du gemme brugeren.';
			var strMsgIsEmail = 'skal indeholde en gyldig email-adresse';
			var strMsgIsChecked1 = ' (du m&#229; ikke markere flere end ';
			var strMsgIsChecked2 = ' (du skal markere mindst ';
			var strMsgIsChecked3 = ' (du skal markere ';
			var strMsgIsChecked4 = ' (du skal markere mellem ';
			var strMsgIsChecked5 = ' af checkboksene';
			var strMsgIsNum = 'skal v&#230;re et tal';
			var strMsgInRange1 = ' (skal v&#230;re et tal mellem ';
			var strMsgAnd = ' og ';
		}

		for (i = 0; i < (aryArgs.length - 2); i += 3) { 
			objField = fncFindObj(aryArgs[i], null, arguments[2] || 0);
			if (objField) { 
				valField = objField.value;
				strFieldAlertText = aryArgs[i+1];
				strTypeOfValidation = aryArgs[i+2]; 

				// save status of array of checkboxes or radio buttons in variable.
				lngCheckedFields = 0
				if (objField.type == "checkbox" || objField.type == "radio" || (isArray(objField) && (objField[1].type == "checkbox" || objField[1].type == "radio")))
					if (objField.checked) // Mark if only one checkbox exists i.e. there is no array (possible in dynamic arrays)
						lngCheckedFields = 1;
					else
						for (n = 0; n < objField.length; n++)
							if (objField[n].checked)
								lngCheckedFields++;

				if ((valField == '') || (valField == null && lngCheckedFields == 0 ) || ((objField.type == "checkbox" || objField.type == "radio") && objField.checked == false)) { // If no input was given
					if (strTypeOfValidation.charAt(0) == 'R') { // and input is required
						strErrors += '- ' + strFieldAlertText + '\n'; //build error string
					}
				}
				else { // Input was given
					if (strTypeOfValidation.indexOf('isEmail') != -1) { 
						pAt = valField.indexOf('@')
						pDot = valField.indexOf('.', pAt)
						if (pAt < 1 || pAt == (valField.length - 3) || pDot < 1|| pDot == (valField.length - 1) )
							strErrors += '- ' + strFieldAlertText + ' (' + strMsgIsEmail + ')\n';
					}
					else if (strTypeOfValidation.indexOf('isChecked') != -1) {
						pColon = strTypeOfValidation.indexOf(':');
						lngMin = strTypeOfValidation.substring( 10, pColon ); 
						lngMax = strTypeOfValidation.substring( pColon + 1 );
						if (lngCheckedFields < lngMin || lngMax < lngCheckedFields) {
							if (lngMin == 0)
								strErrors += '- ' + strFieldAlertText + strMsgIsChecked1 + lngMax + strMsgIsChecked5 + ')\n';
							else if ((lngMax == 9999) && (lngCheckedFields > 9998)) //9999 means unlimited
								strErrors += '- ' + strFieldAlertText + strMsgIsChecked2 + lngMin + strMsgIsChecked5 + ')\n';
							else if (lngMin == lngMax)
								strErrors += '- ' + strFieldAlertText + strMsgIsChecked3 + lngMin + strMsgIsChecked5 + ')\n';
							else
								strErrors += '- ' + strFieldAlertText + strMsgIsChecked4 + lngMin + strMsgAnd + lngMax + strMsgIsChecked5 + ')\n';
						}
					}
					else if (strTypeOfValidation != 'R') { // The rest of the validation types are numeric
						if (valField != '' + parseFloat(valField)) 
							strErrors += '- ' + strFieldAlertText + ' (' + strMsgIsNum + ')\n';
						else { // Value is a number
							if (strTypeOfValidation.indexOf('inRange') != -1) { 
								pColon = strTypeOfValidation.indexOf(':');
								lngMin = strTypeOfValidation.substring( 8, pColon ); 
								lngMax = strTypeOfValidation.substring( pColon + 1 );
								if (lngMin*1 > valField*1 || valField*1 > lngMax*1)
									strErrors += '- ' + strFieldAlertText + strMsgInRange1 + lngMin + strMsgAnd + lngMax + ')\n';
							}
						} 
					} 
				}
			} // Field not found
		} // End of loop
		if (strErrors) {
			alert(strMsgGeneral1 + '\n\n' + strMsgGeneral2 + '\n' + strErrors + '\n' + strMsgGeneral3 + '\n\n');
		}
		document.blnReturnValue = (strErrors == '');
		return (strErrors == '');
	}
	
	
	function fncValidateNewsletterForm(aryArgs, lngLCID)  { 
		// v3.0 browsers
		// This function validates forms
		// The input is an array of arguments with 3 arguments per form field
		// The array must have this syntax: 'Form field name', 'Text to alert in case of error', 'Type of validation'
		// Example: 'companyName', 'Company name', 'R', 'email', 'Email', 'RisEmail'
		// Requires the function fncFindObj()
		// Version 1.0, dec 2000, by Søren Larsen
		var objField; // The form field object matching the given field name
		var strFieldAlertText; // The given text that represent the form field in the alert box
		var strTypeOfValidation; // The given validation string
			// Possible values: 
			// - R - input is required
			// - RisEmail - input is required, must be an email address (one or more characters + @ + 3 or more characters and at least one dot after the first lette after the @)
			// - NisEmail - input is NOT required, but if input is given it must be an email address
			// - RisChecked2:3 - input is required, between 2 and 3 of the checkboxes must checked
			// - NisChecked0:3 - input is not required, no more that 3 of the checkboxes can be checked
			// - RisNum - input is required, must be a number
			// - NisNum - input is NOT required, but if input is given it must be a number
			// - RinRange10:400 - input is required, must be a number in this case in the range 10 to 400
			// - NinRange1:5 - input is NOT required, but if input is given it must be a number in this case in the range 1 to 5
		var i;	// Counter for iterations
		var pAt;	// Pointer for email '@'
		var pDot;	// Pointer for email '.'
		var lngCheckedFields; // Count number of checked checkboxes
		var n; // Counter for array of checkboxes / radio buttons
		var pColon;	// Pointer for ':' in numeric ranges
		var lngMin;	// Minimum value for numeric ranges
		var lngMax;	// Maximum value for numeric ranges
		var strErrors = ''; // Used to build string for alert box
		
		if (lngLCID == 1030) {
			var strMsgGeneral1 = 'Du mangler at udfylde nogle felter.';
			var strMsgGeneral2 = 'Udfyld venligst:';
			var strMsgGeneral3 = 'og send formularen igen.';
			var strMsgIsEmail = 'skal indeholde en gyldig email-adresse';
			var strMsgIsChecked1 = ' (du m&#229; ikke markere flere end ';
			var strMsgIsChecked2 = ' (du skal markere mindst ';
			var strMsgIsChecked3 = ' (du skal markere ';
			var strMsgIsChecked4 = ' (du skal markere mellem ';
			var strMsgIsChecked5 = ' af checkboksene';
			var strMsgIsNum = 'skal v&#230;re et tal';
			var strMsgInRange1 = ' (skal v&#230;re et tal mellem ';
			var strMsgAnd = ' og ';
		}

		for (i = 0; i < (aryArgs.length - 2); i += 3) { 
			objField = fncFindObj(aryArgs[i]);
			if (objField) { 
				valField = objField.value;
				strFieldAlertText = aryArgs[i+1];
				strTypeOfValidation = aryArgs[i+2]; 

				// save status of array of checkboxes or radio buttons in variable.
				lngCheckedFields = 0
				if (objField.type == "checkbox" || objField.type == "radio" || (isArray(objField) && (objField[1].type == "checkbox" || objField[1].type == "radio")))
					if (objField.checked) // Mark if only one checkbox exists i.e. there is no array (possible in dynamic arrays)
						lngCheckedFields = 1;
					else
						for (n = 0; n < objField.length; n++)
							if (objField[n].checked)
								lngCheckedFields++;

				if ((valField == '') || (valField == null && lngCheckedFields == 0 ) || ((objField.type == "checkbox" || objField.type == "radio") && objField.checked == false)) { // If no input was given
					if (strTypeOfValidation.charAt(0) == 'R') { // and input is required
						strErrors += '- ' + strFieldAlertText + '\n'; //build error string
					}
				}
				else { // Input was given
					if (strTypeOfValidation.indexOf('isEmail') != -1) { 
						pAt = valField.indexOf('@')
						pDot = valField.indexOf('.', pAt)
						if (pAt < 1 || pAt == (valField.length - 3) || pDot < 1|| pDot == (valField.length - 1) )
							strErrors += '- ' + strFieldAlertText + ' (' + strMsgIsEmail + ')\n';
					}
					else if (strTypeOfValidation.indexOf('isChecked') != -1) {
						pColon = strTypeOfValidation.indexOf(':');
						lngMin = strTypeOfValidation.substring( 10, pColon ); 
						lngMax = strTypeOfValidation.substring( pColon + 1 );
						if (lngCheckedFields < lngMin || lngMax < lngCheckedFields) {
							if (lngMin == 0)
								strErrors += '- ' + strFieldAlertText + strMsgIsChecked1 + lngMax + strMsgIsChecked5 + ')\n';
							else if ((lngMax == 9999) && (lngCheckedFields > 9998)) //9999 means unlimited
								strErrors += '- ' + strFieldAlertText + strMsgIsChecked2 + lngMin + strMsgIsChecked5 + ')\n';
							else if (lngMin == lngMax)
								strErrors += '- ' + strFieldAlertText + strMsgIsChecked3 + lngMin + strMsgIsChecked5 + ')\n';
							else
								strErrors += '- ' + strFieldAlertText + strMsgIsChecked4 + lngMin + strMsgAnd + lngMax + strMsgIsChecked5 + ')\n';
						}
					}
					else if (strTypeOfValidation != 'R') { // The rest of the validation types are numeric
						if (valField != '' + parseFloat(valField)) 
							strErrors += '- ' + strFieldAlertText + ' (' + strMsgIsNum + ')\n';
						else { // Value is a number
							if (strTypeOfValidation.indexOf('inRange') != -1) { 
								pColon = strTypeOfValidation.indexOf(':');
								lngMin = strTypeOfValidation.substring( 8, pColon ); 
								lngMax = strTypeOfValidation.substring( pColon + 1 );
								if (lngMin*1 > valField*1 || valField*1 > lngMax*1)
									strErrors += '- ' + strFieldAlertText + strMsgInRange1 + lngMin + strMsgAnd + lngMax + ')\n';
							}
						} 
					} 
				}
			} // Field not found
		} // End of loop
		if (strErrors) {
			alert(strMsgGeneral1 + '\n\n' + strMsgGeneral2 + '\n' + strErrors + '\n' + strMsgGeneral3 + '\n\n');
		}
		document.blnReturnValue = (strErrors == '');
		return (strErrors == '');
	}

	function fncFindObj(strFormFieldName, d) { //v3.0
		var p, i, x;
		
		if(arguments[2]) {
			var ret = 0;
			var finputs = arguments[2].getElementsByTagName('INPUT');
			var retval = "";
			for(var it = 0 ; it < finputs.length ; it++) {
				retval += (((finputs[it].name) ? finputs[it].name : "") + " - " + ((finputs[it].value) ? finputs[it].value : "") + "\n");
				if(finputs[it].name && (finputs[it].name.toLowerCase() == strFormFieldName.toLowerCase())) {
					var ret = finputs[it];	
					break;
				} 
			} 
			if(ret)
				return ret;
		}
			
		if (!d) 
			d = document; 
		if ((p = strFormFieldName.indexOf("?") ) > 0 && parent.frames.length) {
			d = parent.frames[strFormFieldName.substring(p + 1)].document; 
			strFormFieldName = strFormFieldName.substring(0, p);
		}
		if (! (x = d[strFormFieldName]) && d.all) 
			x = d.all[strFormFieldName]; 
		for (i = 0; ! x && i < d.forms.length; i++) 
			x = d.forms[i][strFormFieldName];
		for (i = 0; ! x && d.layers && i < d.layers.length; i++) 
			x = fncFindObj(strFormFieldName,d.layers[i].document); 
		return x;
	}

	function isArray(obj) {
		return(typeof(obj.length)=="undefined")?false:true;
	}

	function fncGetRadioValue(aryName){
		var aryFields;
		var objValue = '';
		aryFields = fncFindObj(aryName);
		if (aryFields) {
			if (aryFields.checked) // Mark if only one radiobutton exists i.e. there is no array (possible in dynamic arrays)
				objValue = aryFields.value;
			else
				for (n = 0; n < aryFields.length; n++)
					if (aryFields[n].checked)
						objValue = aryFields[n].value;
			return objValue;
		}
	}
	

// OPEN HELP WINDOW //
function openHelp(url){
	var win = window.open(url,'Help','width=550, height=550, scrollbars=yes');
	win.focus();
}


// SEARCH RESULTS PAGER //
/*
function createPager(numberOfResults, resultsPerPage, index){
	var Pages = Math.round(numberOfResults/resultsPerPage);
	if(Pages == 0) { Pages = 1 }
	if(Pages >= 25) { Pages = 25 }
	for(i=0;i<Pages;i++){
		if(i==0){ ii = '0' } else { ii = i }
		if(ii==index){
			document.write('&#160;<strong>' + (i+1) + '</strong>');
		} else {
			document.write('&#160;<a href="javascript:PagerForm.Page.value=\'' + i + '\';PagerForm.submit();" class="ResultsNavigation" title="' + (i+1) + '">' + (i+1) + '</a>');
		}
	}
}	
*/

// SEARCH RESULTS PAGER //
var MaxNumberOfPagesInView = 20;
var ResultsPerPage = 30;
var View = 1;

function createPager(numberOfResults, resultsPerPage, index, viewIndex){
	if(viewIndex == ''){ viewIndex = 0 }
	var Pages = Math.ceil(numberOfResults/resultsPerPage);
	var ViewStart;
	var ViewEnd;
	
	if(index % MaxNumberOfPagesInView == 0){ 
		ViewStart = Math.round(index);
		document.forms['Shop'].ViewStart.value = ViewStart;
	} else {
		ViewStart = Math.round(viewIndex);
	}
	
	if(ViewStart + MaxNumberOfPagesInView <= Pages){
		ViewEnd = ViewStart + MaxNumberOfPagesInView;
	} else {
		ViewEnd = Pages;
	}
	if(Pages == 0) { Pages = 1 }



	if(index > 0){
		var PreviousViewIndex = (Math.round(viewIndex)-1);
		if(PreviousViewIndex < 0){ PreviousViewIndex = 0 } else { PreviousViewIndex = PreviousViewIndex }
		document.write('<a class="ResultsNavigation" href="javascript:document.forms[\'Shop\'].ViewStart.value=\'' + PreviousViewIndex + '\';document.forms[\'Shop\'].Page.value=\'' + (Math.round(index) - 1) + '\';document.forms[\'Shop\'].submit();">< Forrige</a>');
	}
	for(i=ViewStart;i<ViewEnd;i++){
		if(i==0){ ii = 0 } else { ii = i }
		if(ii==index){
			document.write('&#160;<strong>' + (i+1) + '</strong>');
		} else {
			document.write('&#160;<a href="javascript:document.forms[\'Shop\'].Page.value=\'' + ii + '\';document.forms[\'Shop\'].submit();" class="ResultsNavigation" title="' + (i+1) + '">' + (i+1) + '</a>');
		}
	}
	if(Pages > (Math.round(index)+1)){
		document.write('&#160;<a class="ResultsNavigation" href="javascript:document.forms[\'Shop\'].Page.value=\'' + (Math.round(index) + 1) + '\';document.forms[\'Shop\'].submit();">N&#230;ste ></a>');
	}
}

function setOrder(key){
	var OrderType = 'ASC';
	var Order = document.forms['Shop'].SortKey.value;
	var OrderType = document.forms['Shop'].SortType.value;
	if(Order == ''){ Order = 'T1.PricePerItem, T1.ProductNumber' }
	if(key == Order){
		if(OrderType == 'ASC'){
			OrderType = 'DESC';
		} else {
			OrderType = 'ASC';
		}
	} else {
		OrderType = 'ASC';
	}
	document.forms['Shop'].SortKey.value = key;
	document.forms['Shop'].SortType.value = OrderType;
	document.forms['Shop'].submit();
}



// LOAD CITY LIST //

function loadCityList(){
	var RegionId = SearchForm.RegionId.options[SearchForm.RegionId.selectedIndex].value;
	var CitySelector = SearchForm.PostalNumber;
	var CitySelection = new String(SelectedCities);
	
	if(RegionId != ''){
		var ii = 1;
		CitySelector.options[0] = new Option('V&#230;lg alle postnumre','');
		CitySelector.options[0].selected = true;
		for(i=0;i<Cities.length;i++){
			var CityData = Cities[i].split(',');
			var CityRegionId = CityData[0];
			var CityName = CityData[1];
			var CityPostalNumber = CityData[2];
			if(CityRegionId == RegionId){
				CitySelector.options[ii] = new Option(CityName + ', ' + CityPostalNumber, CityPostalNumber);
				if(CitySelection.indexOf(CityPostalNumber) >= 0){ CitySelector.options[ii].selected = true }
				ii++;
			}
		}
	} else {
		CitySelector.options.length = 0;
		CitySelector.options[0] = new Option('V&#230;lg alle postnumre','')
		CitySelector.options[0].selected = true;
	}
	if(CitySelection.length = 0){
		CitySelector.options[0].selected = true;
	}
}

