function SMShipMethods(){
	var SMPrice = new cSMPrice(), totalPrice = 0, usingShipToAddress = false;
	SMPrice.decode(SMShop.basket.getAttribute(_SMADSubTotal));

	this.relTotalPrice = SMShop.getAttribute(_SMAOutGross)!="1" ? SMPrice.net : SMPrice.gross;
	/* find country */
	usingShipToAddress = (SMShop.getFormElement("BILLTO", "BILLTO_SHIPTO_DIFFERS") != null);
	if(usingShipToAddress){
		usingShipToAddress = cbool(SMShop.getFormValue("BILLTO", "BILLTO_SHIPTO_DIFFERS", true));
		};
	if(usingShipToAddress){
		this.relCountryCode = SMShop.getFormValue('SHIPTO','SHIPTO_COUNTRY', true).toUpperCase();
	}else{
		this.relCountryCode = SMShop.getFormValue('BILLTO','BILLTO_COUNTRY', true).toUpperCase();
	};
	/* continue */
	this.relTotalWeight = SMShop.basket.getAttribute(_SMAWeightTotal);
	this.relTotalQty = SMShop.basket.getAttribute(_SMATotalItems);
	this.relSelectedItemId = SMShop.getActiveShipMethodId();

	this.getSelectedShipMethod = SMShipMethods_getSelectedShipMethod;
	this.setShipMethod = SMShipMethods_setShipMethod;
	this.saveRedirect = SMShipMethods_saveRedirect;
	this.calcDynamic = SMShipMethods_calcDynamic;
	this.calcCharge = SMShipMethods_calcCharge;
	this.autoSelect = SMShipMethods_autoSelect;
	this.saveMethod = SMShipMethods_saveMethod;
	this.printTable = SMShipMethods_printTable;
	this.htmlTable = SMShipMethods_htmlTable;
	this.goBack = SMShipMethods_goBack;

	this.selectdItemIndex = 0;

	this.arrMethods = function(){
		var sWeight = '', fromWeight = maxWeight = 0, arrWeight = null;
		var dblCharge = 0, arrSource = null, arrTarget = new Array();
		var oCalculator = oMethod = null, sPluginRef = sPluginFunction = '';
		for(pkid in m_arrShipMethods){
			if(isNaN(pkid)) continue;
			arrSource = m_arrShipMethods[pkid].split(';');

			oMethod = new SMShipMethod();
			oMethod.id = SX_uEsc(arrSource[0]);
			oMethod.name = SX_uEsc(arrSource[1]);
			oMethod.desc = SX_uEsc(arrSource[2]);
			oMethod.thumb = SX_uEsc(arrSource[3]);
			oMethod.exclPayMethods = arrSource[9];
			oMethod.publicId = SX_uEsc(arrSource[10]);

			oCalculator = new SMShipCalculator(pkid, arrSource[8], this.relCountryCode); // zones
			if(oCalculator == null || oCalculator.chargeValues == null) continue;

			oMethod.calculator = oCalculator;
			oMethod.calcShipSurcharge = false;

			sPluginRef = arrSource[4].toString(); // pluginref
			if(sPluginRef.length > 0){
				sPluginFunction = 'SMShipMethodPlugin_' + SX_uEsc(sPluginRef);
				try{window[sPluginFunction](oMethod)}
				catch(e){continue};
			};

			sWeight = arrSource[7].toString(); // fromWeight, maxWeight
			if(sWeight.length > 0){
				arrWeight = sWeight.split(',');
				fromWeight = parseFloat(arrWeight[0]);
				maxWeight = parseFloat(arrWeight[1]);
				if(this.relTotalWeight < fromWeight || (maxWeight > 0 && this.relTotalWeight > fromWeight 
					&& this.relTotalWeight > maxWeight)) continue;
			};

			dblCharge = this.calcCharge(oCalculator);
			if(dblCharge < 0) continue;

			oMethod.charge = parseFloat(dblCharge);
			oMethod.taxkey = parseInt(arrSource[5]);

			if(parseInt(arrSource[6]) == 1){ // calcProductSurcharges 
				oMethod.calcShipSurcharge = true;
			};

			if(oMethod.id == this.relSelectedItemId){
				this.selectdItemIndex = parseInt(arrTarget.length);
			};

			arrTarget[arrTarget.length] = oMethod;
		};
		return(arrTarget);
	};

	this.shipMethods = this.arrMethods();
	this.selectedMethod = null;
	this.doAutoSelect = false;

};

function SMShipMethod(){
	this.id = '';
	this.publicId = '';
	this.name = '';
	this.desc = '';
	this.thumb = '';
	this.charge = 0;
	this.taxkey = 0;
	this.exclPayMethods = '';
	this.calculator = null;
};

function SMShipMethods_setShipMethod(shipMethodId){
	this.selectedMethod = null;
	for(var i=0;i<this.shipMethods.length;i++){
		if(this.shipMethods[i].id == shipMethodId){
			this.selectedMethod = this.shipMethods[i];
			break;
		};
	};
	return((this.selectedMethod!=null));
};

function SMShipMethods_autoSelect(){
	if(this.doAutoSelect && this.shipMethods.length == 1){
		this.selectedMethod = this.shipMethods[0];
		SMShop.setShipMethod(this.selectedMethod)
		location.replace(SMGetCheckoutStep('SM_RESERVED_DATA_ENTRY_SHIPMENT', false));
	};
};

function SMShipMethods_calcDynamic(calculator,isTable){
	var dblBase = dblCharge = 0;
	switch(calculator.chargeBase){
		case 0: dblBase = this.relTotalPrice; break; // price
		case 1: dblBase = this.relTotalWeight; break; // weight
		case 2: dblBase = this.relTotalQty; break; // qty
	};

	dblCharge = dblBase*calculator.chargeMultiplier;
	if(!isTable){
		if(calculator.chargeBase == 0){
			dblCharge = dblCharge/100; // price-surcharge in percent
		};
		dblCharge += calculator.chargeValue;

		if(calculator.chargeMax > 0 && dblCharge > calculator.chargeMax){
			dblCharge = calculator.chargeMax;
		};
	};
	return(dblCharge);
};

function SMShipMethods_calcCharge(calculator){
	var oCalc = null, bytBase = 0, bCalculate = false;
	if(calculator.chargeFreeFrom > 0 && this.relTotalPrice >= calculator.chargeFreeFrom){
		return(0); // charge free
	} else if(calculator.chargeType == 0){ // fix
		return(calculator.chargeValue);
	} else if(calculator.chargeType == 1){ // dynamic
		return(this.calcDynamic(calculator,false));
	} else if(calculator.shipTables != null){ // table
		bytBase = calculator.chargeBase;
		for(var i=0;i<calculator.shipTables.length;i++){
			oCalc = calculator.shipTables[i];
			if(bytBase == 0){
				bCalculate = cbool(this.relTotalPrice <= oCalc.upToAmount);
			} else if(bytBase == 1){
				bCalculate = cbool(this.relTotalWeight <= oCalc.upToAmount);
			} else if(bytBase == 2){
				bCalculate = cbool(this.relTotalQty <= oCalc.upToAmount);
			};

			if(bCalculate){
				if(oCalc.chargeType == 0){ // fix
					return(oCalc.chargeValue);
				} else if(oCalc.chargeType == 1){ // dynamic
					return(this.calcDynamic(oCalc,true));
				};
			};
		};
	};
	return(-1);
};

function SMShipCalculator(pkid, zones, country){
	var arrCharge = function (){
		var arrZones = zones.length>0 ? zones.split(',') : null;
		if(arrZones == null){
			if(m_arrShipCalculators[pkid]){
				return(m_arrShipCalculators[pkid].split(';'));
			};
		} else {
			for(var i=0; i<arrZones.length;i++){
				if(!m_arrShipZones[arrZones[i]]) continue;
				if((';' + m_arrShipZones[arrZones[i]].toUpperCase() + ';').indexOf(';' + country + ';') > -1){
					return(m_arrZoneCalculators[arrZones[i]].split(';'));
				};
			};
		};
	};
	this.chargeValues = arrCharge();
	if(this.chargeValues != null){
		this.chargeType = parseInt(this.chargeValues[0]);
		this.chargeBase = parseInt(this.chargeValues[1]);
		this.chargeValue = parseFloat(this.chargeValues[2]);
		this.chargeMultiplier = parseFloat(this.chargeValues[3]);
		this.chargeMax = parseFloat(this.chargeValues[4]);
		this.chargeFreeFrom = parseFloat(this.chargeValues[5]);
		this.shipTables = null;
		if(this.chargeType == 2){
			this.shipTables = new SMShipTables(this.chargeValues).tables;
		};
	};
};

function SMShipTables(cvalues){
	var arrTables = function(){
		var arrTable = cvalues[6].length>0 ? cvalues[6].split(',') : null;
		var arrValues = new Array();
		if(arrTable!=null){
			for(var i=0;i<arrTable.length;i++){
				if(!m_arrShipTables[arrTable[i]]) continue;
				arrValues[arrValues.length] = new SMShipTable(cvalues, arrTable[i]);
			};
		};
		return(arrValues);
	};
	this.tables = arrTables();
};

function SMShipTable(cvalues, pkid){
	var arrValues = m_arrShipTables[pkid].split(';');
	this.chargeType = parseInt(arrValues[0]);
	this.chargeValue = parseFloat(arrValues[1]);
	this.upToAmount = parseFloat(arrValues[2]);
	this.chargeBase = parseInt(cvalues[1]);
	this.chargeMultiplier = parseFloat(arrValues[1]);
	this.chargeMax = parseFloat(cvalues[4]);
};

function SMShipMethods_htmlTable(){
	var arrMethods = this.shipMethods;
	var oSMPrice = oSMPriceSurcharge = imgThumb = null;
	var sProductSurcharges = '', sProductSurchargesCaption = 'zzgl. %su Transportzuschlag';
	var sTable = '<form name="shipmethods" onsubmit="return(oSMShipMethods.saveRedirect())" style="display:inline"><table id="shipmethods" border="0" cellpadding="4" cellspacing="0" width="100%">';
	if(arrMethods.length > 0){
		for(var i=0;i<arrMethods.length;i++){
			oSMPrice = new cSMPrice();
			oSMPriceSurcharge = new cSMPrice();
			oSMPrice.calculate(arrMethods[i].charge, !cbool(SMShop.getAttribute(_SMAInGross)), arrMethods[i].taxkey);
			// 
			// oSMPriceSurcharge.decode(SMShop.basket.getAttribute(_SMATotalShipSurcharges));
			oSMPriceSurcharge.calculate(SMShop.basket.getAttribute(_SMATotalShipSurcharges), !cbool(SMShop.getAttribute(_SMAInGross)),  arrMethods[i].taxkey);

			if(arrMethods[i].calcShipSurcharge && oSMPriceSurcharge.net > 0){
				sProductSurcharges = cprimary.format((SMShop.getAttribute(_SMAOutGross)!="1") ? oSMPriceSurcharge.net : oSMPriceSurcharge.gross, SM_CGROUP + SM_CSYMBOL);
				sProductSurchargesCaption = sProductSurchargesCaption.replace(/%su/gi, sProductSurcharges);
			} else {
				sProductSurcharges = '';
				sProductSurchargesCaption = '';
			};
			sTable += '<tr valign="top" class="shipmethod shipmethod-line">'
				+ '<td align="center" style="width:24px"><input id="' + arrMethods[i].id + '" type="radio" ' + (i==this.selectdItemIndex?"checked ":"") + 'name="optShipMethod" value="' + arrMethods[i].id + '"></td>'
				+ '<td>';
			if(arrMethods[i].thumb.length > 0){
				imgThumb = new Image();
				imgThumb.src = arrMethods[i].thumb;
				sTable += '<img class="shipmethod-thumb" src="' + imgThumb.src + '" border="0" width="' + imgThumb.width + '" height="' + imgThumb.height + '">';
			};
			sTable += '<h3><label for="' + arrMethods[i].id + '">' + arrMethods[i].name + '</label></h3><div>' + arrMethods[i].desc;
			if(sProductSurchargesCaption != ''){
				sTable += '<br>' + sProductSurchargesCaption;
			};
			sTable += '</div>'
				+ '</td>'
				+ '<td align="right">' + cprimary.format(cbool(SMShop.getAttribute(_SMAOutGross)) ? oSMPrice.gross : oSMPrice.net, SM_CGROUP + SM_CSYMBOL); + '</td>'
				+ '</tr>';
		};
		sTable += '<tr align="right"><td colspan="3">';
		sTable += '<input type="image" src="' + SMOMAbsoluteRootURL + '/images/button_back.gif" onclick="return(oSMShipMethods.goBack())">&nbsp;';
		sTable += '<input type="image" src="' + SMOMAbsoluteRootURL + '/images/button_forward.gif"></td></tr>';
	} else {
		sTable += '<tr><strong>Verzeihung</strong><br>Das System konnte leider keine entsprechende Versandart bereitstellen. '
			+ 'Bitte kontaktieren Sie uns, um Ihnen schnellstmöglich ein passendes Angebot zu machen.</tr>'
			+ '<tr><td><input type="reset" value="&lt zurück" onclick="oSMShipMethods.goBack()"></td></tr>';
	};
	sTable += '</table></form>';
	return(sTable);
};

function SMShipMethods_printTable(){
	document.write(this.htmlTable());
};

function SMShipMethods_saveMethod(selectedMethodId){
	if(this.setShipMethod(selectedMethodId)){
		return(SMShop.setShipMethod(this.selectedMethod));
	};
	return(false);
};

function SMShipMethods_saveRedirect(){
	if(this.saveMethod(this.getSelectedShipMethod())){
		location.href = SMGetCheckoutStep('SM_RESERVED_DATA_ENTRY_SHIPMENT', false);
	};
	return(false);
};

function SMShipMethods_goBack(){
	location.href = SMGetCheckoutStep('SM_RESERVED_DATA_ENTRY_SHIPMENT', true);
	return(false);
};

function SMShipMethods_getSelectedShipMethod(){
	var elem = null;
	for(var i=0;i<document.shipmethods.elements.length;i++){
		elem = document.shipmethods.elements[i];
		if(elem.name == 'optShipMethod' && elem.checked){
			return(elem.value);
		};
	};
	return('');
};

var m_arrShipMethods = new Array();m_arrShipMethods[1]="1;Selbstabholung;Abholung_20ab_20Werk_20in_204974_20Ort_20im_20Innkreis;;;1;0;;;;DM_X201001";m_arrShipMethods[2]="2;Packetdienst_20DHL_20_X2_20AT;Frachtkosten_20richten_20sich_20nach_20dem_20Brutto_X2_20Bestellwert;;;1;1;;3;;DM_X201002";m_arrShipMethods[11]="11;Packetdienst_20DHL_20_X2_20DE;Frachtkosten_20richten_20sich_20nach_20dem_20Brutto_X2Bestellwert;;;1;1;;7;;3";m_arrShipMethods[10]="10;Packetdienst_20DHL_20_X2_20CH;Frachtkosten_20richten_20sich_20nach_20der_20bestellten_20St_C3_BCckzahl_X8_20_20_0D_0AFracht,_20Ausfuhrerkl_C3_A4rung_20und_20Zoll_20werden_20_C3_BCber_20uns_20abgerechnet_0D_0A;;;1;1;;6;;2";m_arrShipMethods[13]="13;Packetdienst_20DHL_20_X2_20CZ;;;;1;1;;8;;5";m_arrShipMethods[14]="14;Packetdienst_20DHL_20_X2_20SI;;;;1;1;;9;;6";m_arrShipMethods[12]="12;Packetdienst_20DHL_20_X2_20FR;;;;1;1;;10;;4";m_arrShipMethods[15]="15;Packetdienst_20DHL_20_X2_20IT;;;;1;1;;11;;7";m_arrShipMethods[16]="16;Packetdienst_20DHL_20_X2_20HU;;;;1;1;;12;;8";m_arrShipMethods[17]="17;Packetdienst_20DHL_20_X2_20BE;;;;1;1;;13;;9";m_arrShipMethods[18]="18;Packetdienst_20DHL_20_X2_20NL;;;;1;1;;14;;10";m_arrShipMethods[19]="19;Packetdienst_20DHL_20_X2_20LU;;;;1;1;;15;;11";m_arrShipMethods[20]="20;Packetdienst_20DHL_20_X2_20UK;;;;1;1;;16;;12";m_arrShipMethods[21]="21;Packetdienst_20DHL_20_X2_20IE;;;;1;1;;17;;13";m_arrShipMethods[22]="22;Packetdienst_20DHL_20_X2_20ES;;;;1;1;;18;;14";m_arrShipMethods[23]="23;Packetdienst_20DHL_20_X2_20PT;;;;1;1;;19;;15";m_arrShipMethods[24]="24;Packetdienst_20DHL_20_X2_20_20FI;;;;1;1;;20;;16";m_arrShipMethods[25]="25;Packetdienst_20DHL_20_X2_20NO;;;;1;1;;21;;17";m_arrShipMethods[26]="26;Packetdienst_20DHL_20_X2_20SE;;;;1;1;;22;;18";m_arrShipMethods[27]="27;Packetdienst_20DHL_20_X2_20DK;;;;1;1;;23;;19";m_arrShipMethods[28]="28;Packetdienst_20DHL_20_X2_20PL;;;;1;1;;24;;20";var m_arrShipCalculators = new Array();m_arrShipCalculators[1]='0;0;0;0;0;0;';var m_arrZoneCalculators = new Array();m_arrZoneCalculators[3]='2;0;10.68;0;0;0;127,128,129';m_arrZoneCalculators[6]='2;2;0;0;0;0;74,75,76';m_arrZoneCalculators[7]='2;0;10.68;0;0;0;71,72,73';m_arrZoneCalculators[8]='2;2;22.96;0;0;0;77,78';m_arrZoneCalculators[9]='2;0;22.96;0;0;0;79,80,81';m_arrZoneCalculators[10]='2;0;28.78;0;0;0;82,83,84';m_arrZoneCalculators[11]='2;0;28.78;0;0;0;85,86,87';m_arrZoneCalculators[12]='2;0;28.78;0;0;0;88,89,90';m_arrZoneCalculators[13]='2;0;28.78;0;0;0;91,92,93';m_arrZoneCalculators[14]='2;0;28.78;0;0;0;94,95,96';m_arrZoneCalculators[15]='2;0;28.78;0;0;0;97,98,99';m_arrZoneCalculators[16]='2;0;35.57;0;0;0;100,101,102';m_arrZoneCalculators[17]='2;0;35.57;0;0;0;103,104,105';m_arrZoneCalculators[18]='2;0;35.57;0;0;0;106,107,108';m_arrZoneCalculators[19]='2;0;35.57;0;0;0;109,110,111';m_arrZoneCalculators[20]='2;0;39.88;0;0;0;112,113,114';m_arrZoneCalculators[21]='2;0;39.88;0;0;0;115,116,117';m_arrZoneCalculators[22]='2;0;39.88;0;0;0;118,119,120';m_arrZoneCalculators[23]='2;0;39.88;0;0;0;121,122,123';m_arrZoneCalculators[24]='2;0;39.88;0;0;0;124,125,126';var m_arrShipZones = new Array();m_arrShipZones[3]='AT';m_arrShipZones[6]='CH';m_arrShipZones[7]='DE';m_arrShipZones[8]='CZ';m_arrShipZones[9]='SI';m_arrShipZones[10]='FR';m_arrShipZones[11]='IT';m_arrShipZones[12]='HU';m_arrShipZones[13]='BE';m_arrShipZones[14]='NL';m_arrShipZones[15]='LU';m_arrShipZones[16]='GB';m_arrShipZones[17]='IE';m_arrShipZones[18]='ES';m_arrShipZones[19]='PT';m_arrShipZones[20]='FI';m_arrShipZones[21]='NO';m_arrShipZones[22]='SE';m_arrShipZones[23]='DK';m_arrShipZones[24]='PL';var m_arrShipTables = new Array();m_arrShipTables[74]='0;59;3';m_arrShipTables[75]='0;73;6';m_arrShipTables[85]='0;35.52;180';m_arrShipTables[103]='0;59.52;180';m_arrShipTables[97]='0;35.52;180';m_arrShipTables[106]='0;59.52;180';m_arrShipTables[94]='0;35.52;180';m_arrShipTables[109]='0;59.52;180';m_arrShipTables[91]='0;35.52;180';m_arrShipTables[112]='0;59.52;180';m_arrShipTables[88]='0;23.52;180';m_arrShipTables[71]='0;23.52;180';m_arrShipTables[115]='0;59.52;180';m_arrShipTables[79]='0;23.52;180';m_arrShipTables[118]='0;59.52;180';m_arrShipTables[82]='0;53.52;180';m_arrShipTables[127]='0;11.76;180';m_arrShipTables[121]='0;59.52;180';m_arrShipTables[100]='0;53.52;180';m_arrShipTables[124]='0;53.52;180';m_arrShipTables[98]='0;47.28;360';m_arrShipTables[72]='0;35.28;360';m_arrShipTables[95]='0;47.28;360';m_arrShipTables[86]='0;47.28;360';m_arrShipTables[92]='0;47.28;360';m_arrShipTables[89]='0;35.28;360';m_arrShipTables[80]='0;35.28;360';m_arrShipTables[83]='0;65.28;360';m_arrShipTables[77]='0;35.28;360';m_arrShipTables[110]='0;71.28;360';m_arrShipTables[128]='0;23.52;360';m_arrShipTables[101]='0;65.28;360';m_arrShipTables[104]='0;71.28;360';m_arrShipTables[113]='0;71.28;360';m_arrShipTables[125]='0;65.28;360';m_arrShipTables[107]='0;71.28;360';m_arrShipTables[116]='0;71.28;360';m_arrShipTables[122]='0;71.28;360';m_arrShipTables[119]='0;71.28;360';m_arrShipTables[76]='0;97;999';m_arrShipTables[78]='0;47.04;99999';m_arrShipTables[126]='0;77.04;99999';m_arrShipTables[120]='0;83.04;99999';m_arrShipTables[73]='0;47.04;99999';m_arrShipTables[81]='0;47.04;99999';m_arrShipTables[123]='0;83.04;99999';m_arrShipTables[117]='0;83.04;99999';m_arrShipTables[99]='0;59.04;99999';m_arrShipTables[114]='0;83.04;99999';m_arrShipTables[87]='0;59.04;99999';m_arrShipTables[111]='0;83.04;99999';m_arrShipTables[90]='0;47.04;99999';m_arrShipTables[108]='0;83.04;99999';m_arrShipTables[93]='0;59.04;99999';m_arrShipTables[105]='0;83.04;99999';m_arrShipTables[96]='0;59.04;99999';m_arrShipTables[102]='0;77.04;99999';m_arrShipTables[129]='0;35.28;99999';m_arrShipTables[84]='0;77.04;99999';var oSMShipMethods = null;oSMShipMethods = new SMShipMethods();SMShop.base.addMember("oSMShipMethods");function oSMShipMethods_windowOnLoad(args){var oShipSelector = null;oSMShipMethods = new SMShipMethods();if(document.getElementById("shipmethodselector")){oShipSelector = document.getElementById("shipmethodselector");oShipSelector.innerHTML = oSMShipMethods.htmlTable();oShipSelector.style.height = oShipSelector.offsetHeight;};};