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="87" height="29" onclick="return(oSMShipMethods.goBack())">&nbsp;';
		sTable += '<input type="image" alt="Weiter" title="Weiter" src="' + SMOMAbsoluteRootURL + '/images/button_forward.gif" width="81" height="29"></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[1]="1;Selbstabholung;Die_20Abholung_20erfolgt_20durch_20den_20Kunden_20aus_20unserem_20Lager_X3;;;1;0;;;;DM_X201001";m_arrShipMethods[6]="6;Paketversand_20_X2_20versichert;Die_20Ware_20wird_20als_20versichertes_20Paket_20zu_20Ihnen_20geliefert_X3;;;1;1;;2;10;S_X201006";m_arrShipMethods[7]="7;Versandkostenfrei_20innerhalb_20Deutschlands;Gesondert_20ausgewiesene_20Artikel_20sind_20in_20manchen_20F_C3_A4llen_20Versandkostenfrei_20innerhalb_20Deutschlands_X3_20In_20diesem_20Fall_20steht_20neben_20dem_20Produktpreis_20_22Versandkostenfrei_20innerhalb_20Deutschlands_22_X3;;;1;0;1498,1500;4;10;S_X201007";m_arrShipMethods[5]="5;Versand_20mit_20Spedition_20innerhalb_20Deutschlands;Teichfilter_20und_20Regentonnen_20m_C3_BCssen_20auf_20der_20Europalette_20mit_20Spedition_20versendet_20werden_X3_20Sie_20bekommen_20die_20gro_C3_9Fen_20und_20sperrigen_20Waren_20bequem_20nach_20Hause_20geliefert_X3_20Der_20Preis_20gilt_20innerhalb_20Deutschlands_X3;;;1;1;;1;10;S_X201005";m_arrShipMethods[8]="8;Parcel_20Post_20DHL_20International;Parcel_20post_20in_20the_20foreign_20countries_20with_20DHL_20as_20insured_20package_X3_20For_20special_20questions_20about_20shipping_20please_20use_20our_20contact_20form_X3_20We_20are_20also_20for_20you,_20an_20individual_20may_20find_20shipping_X3Thank_20you_20very_20much_X3;;;1;1;;5,6,7,8;10;S_X201008";m_arrShipMethods[9]="9;International_20shipment_20by_20forwarding_20agency_X3;Shipment_20by_20forwarding_20agency_X3_20The_20dispatch_20takes_20place_20on_20a_20pallet_X3_20Please,_20in_20case_20of_20questions_20consult_20our_20service_X3_20We_20like_20to_20advise_20you_X3;;;1;1;;11,12,13,14;10;S_X201009";m_arrShipMethods[10]="10;Versand_20in_20die_20Schweiz_20mit_20Spedition;Diese_20Versandart_20ist_20f_C3_BCr_20unsere_20Kunden_20aus_20der_20Schweiz_X3_20Gro_C3_9Fe_20Waren_20wie_20Teichfilter_20oder_20Teichfolie_20m_C3_BCssen_20auf_20der_20Europalette_20versendet_20werden_X3_20Die_20Versandkosten_20werden_20anhand_20Ihrer_20Postleitzahl_20ermittelt_X3_20Die_20Zollgeb_C3_BChren_20werden_20gesondert_20ermittelt_X3;;;1;0;;10;10;S_X201010";var m_arrShipCalculators = new Array();m_arrShipCalculators[1]='0;0;0;0;0;0;';var m_arrZoneCalculators = new Array();m_arrZoneCalculators[1]='2;1;0;0;0;0;36,37,38';m_arrZoneCalculators[2]='2;1;0;0;0;0;13,14,15';m_arrZoneCalculators[4]='2;1;0;0;0;0;32';m_arrZoneCalculators[5]='2;1;0;0;0;0;16,17,18';m_arrZoneCalculators[6]='2;1;0;0;0;0;19,20,21';m_arrZoneCalculators[7]='2;1;0;0;0;0;22,23,24';m_arrZoneCalculators[8]='2;1;0;0;0;0;25,26,27';m_arrZoneCalculators[10]='2;1;0;0;0;0;';m_arrZoneCalculators[11]='2;1;0;0;0;0;42,43,44,45';m_arrZoneCalculators[12]='2;1;0;0;0;0;41';m_arrZoneCalculators[13]='2;1;0;0;0;0;40';m_arrZoneCalculators[14]='2;1;0;0;0;0;39';var m_arrShipZones = new Array();m_arrShipZones[1]='DE';m_arrShipZones[2]='DE';m_arrShipZones[4]='DE';m_arrShipZones[5]='AT;BE;BG;CY;CZ;DK;EE;FI;FR;EL;HU;IE;IT;LV;LT;LU;MT;NL;PL;PT;RO;SK;SI;ES;SE;GB';m_arrShipZones[6]='CH;BA;FO;LI;MD;NO;RU;SM;CS;TR;UA;VA';m_arrShipZones[7]='EG;KW;SA;TN;AE;US';m_arrShipZones[8]='DO;JM;JP;MQ;MX;MC;PY;PH;TH;VE;VN';m_arrShipZones[10]='CH';m_arrShipZones[11]='AT;CZ';m_arrShipZones[12]='BE;LU;NL';m_arrShipZones[13]='PL';m_arrShipZones[14]='IT';var m_arrShipTables = new Array();m_arrShipTables[25]='0;40;4';m_arrShipTables[16]='0;16;4';m_arrShipTables[19]='0;30;4';m_arrShipTables[22]='0;35;4';m_arrShipTables[17]='0;21;9';m_arrShipTables[20]='0;35;9';m_arrShipTables[23]='0;45;9';m_arrShipTables[13]='0;6.8;9';m_arrShipTables[26]='0;55;9';m_arrShipTables[21]='0;45;19';m_arrShipTables[14]='0;8.8;19';m_arrShipTables[27]='0;85;19';m_arrShipTables[18]='0;31;19';m_arrShipTables[24]='0;65;19';m_arrShipTables[15]='0;12.9;30';m_arrShipTables[42]='0;79;150';m_arrShipTables[40]='0;140;150';m_arrShipTables[36]='0;39;150';m_arrShipTables[39]='0;160;150';m_arrShipTables[41]='0;140;150';m_arrShipTables[43]='0;126;250';m_arrShipTables[37]='0;149;500';m_arrShipTables[44]='0;149;500';m_arrShipTables[38]='0;199;1000';m_arrShipTables[45]='0;219;1000';m_arrShipTables[32]='0;0;1500';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;};};