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" alt="Zurück" src="' + SMOMAbsoluteRootURL + '/images/button_back.gif" onclick="return(oSMShipMethods.goBack())">&nbsp;';
		sTable += '<input type="image" alt="Weiter" src="' + SMOMAbsoluteRootURL + '/images/button_forward.gif"></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[2]="2;Standard_X2Versand_20(_203_X25_20Werktage_20)_20versichert;Die_20Zustellung_20erfolgt_20durch_20Hermes,_20DHL,_20DPD_20oder_20ein_20anderes_20Logistikunternehmen_X3_0D_0AIn_20der_20Regel_20wird_20die_20Ware_20innerhalb_20von_20_0D_0A3_20_X2_205_20Werktagen_20(Mo_X2Fr)_20zugestellt_X3;;;0;1;;;;S_X201001";m_arrShipMethods[8]="8;Schnell_X2Versand_20(_201_X22_20Werktage_20)_20versichert;Die_20Zustellung_20erfolgt_20durch_20DHL_20oder_20DPD_X3_0D_0AIn_20der_20Regel_20wird_20die_20Ware_20innerhalb_20von_20_0D_0A1_20_X2_202_20Werktagen_20(Mo_X2Fr)_20zugestellt_X3;;;0;1;;;;S_X201002";var m_arrShipCalculators = new Array();m_arrShipCalculators[2]='2;2;0.17;1;0;0;730,731,732,733,734,735,736,737,738,739,740,741,742,743,744,745,746,747,748,749,750,751,752,753,754,755,756,757,758,759,760,761,762,763,764,765,766,767,768,769,770,771,772,773,774,775,776,777';m_arrShipCalculators[8]='2;2;0;0;0;0;778,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,795,796,797,798,799,800,801,802,803,804,805,806,807,808,809,810,811,812,813,814,815,816,817,818,819,820,821,822,823,824,825';var m_arrZoneCalculators = new Array();var m_arrShipZones = new Array();var m_arrShipTables = new Array();m_arrShipTables[730]='0;4.95;1';m_arrShipTables[778]='0;5.75;1';m_arrShipTables[779]='0;11.5;2';m_arrShipTables[731]='0;9.9;2';m_arrShipTables[780]='0;17.25;3';m_arrShipTables[732]='0;14.85;3';m_arrShipTables[733]='0;19.8;4';m_arrShipTables[781]='0;23;4';m_arrShipTables[734]='0;24.75;5';m_arrShipTables[782]='0;28.75;5';m_arrShipTables[735]='0;29.7;6';m_arrShipTables[783]='0;34.5;6';m_arrShipTables[736]='0;34.65;7';m_arrShipTables[784]='0;40.25;7';m_arrShipTables[737]='0;39.6;8';m_arrShipTables[785]='0;46;8';m_arrShipTables[738]='0;44.55;9';m_arrShipTables[786]='0;51.75;9';m_arrShipTables[739]='0;49.5;10';m_arrShipTables[787]='0;57.5;10';m_arrShipTables[788]='0;63.25;11';m_arrShipTables[740]='0;54.45;11';m_arrShipTables[789]='0;69;12';m_arrShipTables[741]='0;59.4;12';m_arrShipTables[790]='0;74.75;13';m_arrShipTables[742]='0;64.35;13';m_arrShipTables[743]='0;69.3;14';m_arrShipTables[791]='0;80.5;14';m_arrShipTables[744]='0;74.25;15';m_arrShipTables[792]='0;86.25;15';m_arrShipTables[793]='0;92;16';m_arrShipTables[745]='0;79.2;16';m_arrShipTables[746]='0;84.15;17';m_arrShipTables[794]='0;97.75;17';m_arrShipTables[747]='0;89.1;18';m_arrShipTables[795]='0;103.5;18';m_arrShipTables[748]='0;94.05;19';m_arrShipTables[796]='0;109.25;19';m_arrShipTables[749]='0;99;20';m_arrShipTables[797]='0;115;20';m_arrShipTables[750]='0;103.95;21';m_arrShipTables[798]='0;120.75;21';m_arrShipTables[751]='0;108.9;22';m_arrShipTables[799]='0;126.5;22';m_arrShipTables[752]='0;113.85;23';m_arrShipTables[800]='0;132.25;23';m_arrShipTables[753]='0;118.8;24';m_arrShipTables[801]='0;138;24';m_arrShipTables[802]='0;140;25';m_arrShipTables[754]='0;120;25';m_arrShipTables[755]='0;120;26';m_arrShipTables[803]='0;140;26';m_arrShipTables[804]='0;140;27';m_arrShipTables[756]='0;120;27';m_arrShipTables[757]='0;120;28';m_arrShipTables[805]='0;140;28';m_arrShipTables[758]='0;120;29';m_arrShipTables[806]='0;140;29';m_arrShipTables[807]='0;140;30';m_arrShipTables[759]='0;120;30';m_arrShipTables[808]='0;140;31';m_arrShipTables[760]='0;120;31';m_arrShipTables[809]='0;140;32';m_arrShipTables[761]='0;120;32';m_arrShipTables[810]='0;140;33';m_arrShipTables[762]='0;120;33';m_arrShipTables[811]='0;140;34';m_arrShipTables[763]='0;120;34';m_arrShipTables[764]='0;120;35';m_arrShipTables[812]='0;140;35';m_arrShipTables[765]='0;120;36';m_arrShipTables[813]='0;140;36';m_arrShipTables[766]='0;120;37';m_arrShipTables[814]='0;140;37';m_arrShipTables[767]='0;120;38';m_arrShipTables[815]='0;140;38';m_arrShipTables[768]='0;120;39';m_arrShipTables[816]='0;140;39';m_arrShipTables[769]='0;0;40';m_arrShipTables[817]='0;160;40';m_arrShipTables[770]='0;0;41';m_arrShipTables[818]='0;160;41';m_arrShipTables[771]='0;0;42';m_arrShipTables[819]='0;160;42';m_arrShipTables[772]='0;0;43';m_arrShipTables[820]='0;160;43';m_arrShipTables[773]='0;0;44';m_arrShipTables[821]='0;160;44';m_arrShipTables[774]='0;0;45';m_arrShipTables[822]='0;160;45';m_arrShipTables[823]='0;160;46';m_arrShipTables[775]='0;0;46';m_arrShipTables[776]='0;0;47';m_arrShipTables[824]='0;160;47';m_arrShipTables[825]='0;160;48';m_arrShipTables[777]='0;0;48';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;};};