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

	this.relTotalPrice = SMShop.getAttribute(_SMAInGross)!="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">';
			};
			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" alt="Zurück" title="Zurück" src="' + SMOMAbsoluteRootURL + '/images/button_back.gif" width="72" height="16" onclick="return(oSMShipMethods.goBack())">&nbsp;';
		sTable += '<input type="image" alt="Weiter" title="Weiter" src="' + SMOMAbsoluteRootURL + '/images/button_forward.gif" width="69" height="16"></td></tr>';
	} else {
		sTable += '<tr><td>Verzeihung. Das System konnte leider keine entsprechende Versandart bereitstellen. Bitte kontaktieren Sie uns, um Ihnen ein Angebot zu machen.</td></tr>'
			+ '<tr><td><input type="reset" value="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[13]="13;Versand_20per_20DPD;Versand_20per_20Paketdienst_20DPD_X3_20Deutschland_20und_20Europa;media_X4images_X4visual_X5specials_X2medium_X3jpg;;1;0;;3,18,19,20,21,22,23,24,25,26;16;40013";m_arrShipMethods[1]="1;Selbstabholung;Abholung_20ab_20Lager;;;1;0;;;2,13,16;40093";m_arrShipMethods[16]="16;Versand_20per_20Spedition;Die_20Ware_20wird_20durch_20die_20Spedtion_20DSV_20geliefert_X3;media_X4images_X4road_X50130_X2medium_X3jpg;;1;0;;27,28,29,30,31,32,33;2,13;40092";var m_arrShipCalculators = new Array();m_arrShipCalculators[1]='0;0;0;0;0;0;';var m_arrZoneCalculators = new Array();m_arrZoneCalculators[3]='2;1;0;0;0;0;208,209,210,211,212,213,214';m_arrZoneCalculators[18]='2;1;0;0;0;0;237,238,239,240,241,242,243';m_arrZoneCalculators[19]='2;1;0;0;0;0;244,245,246,247,248,249,250';m_arrZoneCalculators[20]='2;1;0;0;0;0;251,252,253,254,255,256,257';m_arrZoneCalculators[21]='2;1;0;0;0;0;258,259,260,261,262,263,264';m_arrZoneCalculators[22]='2;1;0;0;0;0;265,266,267,268,269,270,271';m_arrZoneCalculators[23]='2;1;0;0;0;0;272,273,274,275,276,277,278';m_arrZoneCalculators[24]='2;1;0;0;0;0;279,280,281,282,283,284,285';m_arrZoneCalculators[25]='2;1;0;0;0;0;286,287,288,289,290,291,292';m_arrZoneCalculators[26]='2;1;0;0;0;0;293,294,295,296,297,298,299';m_arrZoneCalculators[27]='2;1;0;0;0;0;215,216,217,218,219,220';m_arrZoneCalculators[28]='2;1;0;0;0;0;231,232,233,234,235,236';m_arrZoneCalculators[29]='2;1;0;0;0;0;300,301,302,303,304,305';m_arrZoneCalculators[30]='2;1;0;0;0;0;311,312,313,314,315,316';m_arrZoneCalculators[31]='2;1;0;0;0;0;317,318,319,320,321,322';m_arrZoneCalculators[32]='2;1;0;0;0;0;323,324,325,326,327,328';m_arrZoneCalculators[33]='2;1;0;0;0;0;329,330,331,332,333,334';var m_arrShipZones = new Array();m_arrShipZones[3]='DE';m_arrShipZones[18]='AT;BE;CZ;DK;LU;NL';m_arrShipZones[19]='FR;HU;IE;IT;PL;PT;SK;SI;ES;SE;GB';m_arrShipZones[20]='EE;FI;LV;LT';m_arrShipZones[21]='BA;HR;CS';m_arrShipZones[22]='BG;RO';m_arrShipZones[23]='EL';m_arrShipZones[24]='IS';m_arrShipZones[25]='NO';m_arrShipZones[26]='CH;LI';m_arrShipZones[27]='DE;NL';m_arrShipZones[28]='AT';m_arrShipZones[29]='FR';m_arrShipZones[30]='FI';m_arrShipZones[31]='ES';m_arrShipZones[32]='LV';m_arrShipZones[33]='PT';var m_arrShipTables = new Array();m_arrShipTables[265]='0;60;5';m_arrShipTables[293]='0;30;5';m_arrShipTables[237]='0;8.5;5';m_arrShipTables[244]='0;16;5';m_arrShipTables[286]='0;55;5';m_arrShipTables[251]='0;29;5';m_arrShipTables[258]='0;70;5';m_arrShipTables[272]='0;60;5';m_arrShipTables[279]='0;75;5';m_arrShipTables[208]='0;5;5';m_arrShipTables[287]='0;65;10';m_arrShipTables[280]='0;85;10';m_arrShipTables[273]='0;65;10';m_arrShipTables[294]='0;35;10';m_arrShipTables[259]='0;80;10';m_arrShipTables[252]='0;36;10';m_arrShipTables[245]='0;20;10';m_arrShipTables[238]='0;11;10';m_arrShipTables[209]='0;8.5;10';m_arrShipTables[266]='0;65;10';m_arrShipTables[267]='0;70;25';m_arrShipTables[288]='0;70;25';m_arrShipTables[274]='0;75;25';m_arrShipTables[253]='0;40;25';m_arrShipTables[295]='0;45;25';m_arrShipTables[260]='0;90;25';m_arrShipTables[281]='0;95;25';m_arrShipTables[246]='0;26;25';m_arrShipTables[239]='0;16;25';m_arrShipTables[210]='0;12.5;25';m_arrShipTables[268]='0;130;35';m_arrShipTables[254]='0;69;35';m_arrShipTables[240]='0;25;35';m_arrShipTables[296]='0;75;35';m_arrShipTables[282]='0;125;35';m_arrShipTables[275]='0;100;35';m_arrShipTables[211]='0;17.5;35';m_arrShipTables[261]='0;110;35';m_arrShipTables[289]='0;90;35';m_arrShipTables[247]='0;42;35';m_arrShipTables[269]='0;140;50';m_arrShipTables[276]='0;150;50';m_arrShipTables[283]='0;190;50';m_arrShipTables[255]='0;80;50';m_arrShipTables[262]='0;160;50';m_arrShipTables[297]='0;90;50';m_arrShipTables[248]='0;52;50';m_arrShipTables[241]='0;32;50';m_arrShipTables[212]='0;25;50';m_arrShipTables[290]='0;140;50';m_arrShipTables[256]='0;120;75';m_arrShipTables[270]='0;210;75';m_arrShipTables[249]='0;78;75';m_arrShipTables[263]='0;240;75';m_arrShipTables[242]='0;48;75';m_arrShipTables[213]='0;37.5;75';m_arrShipTables[277]='0;225;75';m_arrShipTables[298]='0;135;75';m_arrShipTables[291]='0;210;75';m_arrShipTables[284]='0;285;75';m_arrShipTables[300]='0;90;80';m_arrShipTables[311]='0;150;80';m_arrShipTables[215]='0;50;80';m_arrShipTables[231]='0;95;80';m_arrShipTables[329]='0;130;80';m_arrShipTables[317]='0;150;80';m_arrShipTables[323]='0;160;80';m_arrShipTables[250]='0;105;99';m_arrShipTables[285]='0;380;99';m_arrShipTables[243]='0;95;99';m_arrShipTables[264]='0;320;99';m_arrShipTables[257]='0;160;99';m_arrShipTables[278]='0;300;99';m_arrShipTables[292]='0;280;99';m_arrShipTables[214]='0;50;99';m_arrShipTables[271]='0;280;99';m_arrShipTables[299]='0;180;99';m_arrShipTables[330]='0;160;160';m_arrShipTables[301]='0;170;160';m_arrShipTables[232]='0;125;160';m_arrShipTables[216]='0;80;160';m_arrShipTables[324]='0;240;160';m_arrShipTables[312]='0;250;160';m_arrShipTables[318]='0;210;160';m_arrShipTables[302]='0;250;240';m_arrShipTables[325]='0;310;240';m_arrShipTables[319]='0;280;240';m_arrShipTables[233]='0;180;240';m_arrShipTables[313]='0;360;240';m_arrShipTables[331]='0;220;240';m_arrShipTables[217]='0;110;240';m_arrShipTables[234]='0;210;320';m_arrShipTables[218]='0;135;320';m_arrShipTables[320]='0;350;320';m_arrShipTables[332]='0;300;320';m_arrShipTables[303]='0;330;320';m_arrShipTables[314]='0;500;320';m_arrShipTables[326]='0;365;320';m_arrShipTables[333]='0;330;500';m_arrShipTables[327]='0;490;500';m_arrShipTables[304]='0;400;500';m_arrShipTables[315]='0;560;500';m_arrShipTables[219]='0;185;500';m_arrShipTables[321]='0;440;500';m_arrShipTables[235]='0;340;500';m_arrShipTables[236]='0;500;1000';m_arrShipTables[220]='0;290;1000';m_arrShipTables[316]='0;850;1000';m_arrShipTables[305]='0;625;1000';m_arrShipTables[322]='0;550;1000';m_arrShipTables[334]='0;550;1000';m_arrShipTables[328]='0;785;1000';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;};};