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.png" onclick="return(oSMShipMethods.goBack())">&nbsp;';
		sTable += '<input type="image" src="' + SMOMAbsoluteRootURL + '/images/button_forward.png"></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_20Lager;;;1;0;;;1,2,14;DM_X201001";m_arrShipMethods[4]="4;Post_20Wein_X3Paket;Beim_20Wein_X3Paket_20handelt_20es_20sich_20um_20Kartons_20mit_20variierbaren_20Einlagen_20f_C3_BCr_20den_20sicheren_20Versand_20Ihrer_20Weinflaschen;;;1;1;;2,3;13,14;S_X201004";m_arrShipMethods[5]="5;Direktzustellung_20(nur_20f_C3_BCr_20den_20Raum_20LINZ);diese_20Versandart_20kann_20leider_20nur_20im_20Raum_20Linz_X4Wels_X4Steyr_20angeboten_20werden;;;1;1;;5;13,1,11,2;S_X201005";var m_arrShipCalculators = new Array();m_arrShipCalculators[1]='0;0;0;0;0;0;';var m_arrZoneCalculators = new Array();m_arrZoneCalculators[2]='0;2;11.2;0;0;250;';m_arrZoneCalculators[3]='0;2;19.9;0;0;350;';m_arrZoneCalculators[5]='0;0;6;0;0;80;';var m_arrShipZones = new Array();m_arrShipZones[2]='AT';m_arrShipZones[3]='DE';m_arrShipZones[5]='AT';var m_arrShipTables = new Array();m_arrShipTables[23]='0;19.9;12';m_arrShipTables[20]='0;11.2;12';m_arrShipTables[24]='0;39.8;24';m_arrShipTables[21]='0;22.4;24';m_arrShipTables[25]='0;39.8;36';m_arrShipTables[22]='0;22.4;36';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;};};