
function isPayform(oForm){
	var retval = false;
	for(var i=0;i<oForm.elements.length;i++){
		if(oForm.elements[i].name == 'SMPAYMETHOD_SELECTOR'){
			retval = true;
			break;
		};
	};
	return(retval);
};

function setActiveForm(formname){
	formname = formname?formname:document.forms[0].name;
	for(var i=0;i<document.forms.length;i++){
		if(isPayform(document.forms[i])){
			if(document.forms[i].name == formname){
				document.forms[i].SMPAYMETHOD_SELECTOR.checked = true;
			} else {
				document.forms[i].SMPAYMETHOD_SELECTOR.checked = false;
			};
		};
	};
};

function totalPayMethodCount(){
	var formCount = 0;
	for(var i=0;i<document.forms.length;i++){
		if(isPayform(document.forms[i])){
			formCount++;
		};
	};
	return(formCount);
};

function hideExclMethods(){
	var relCountryCode = '';
	var relShipToCountryCode = '';
	var relBillToCountryCode = SMShop.getFormValue('BILLTO','BILLTO_COUNTRY', true).toUpperCase();
	var usingShipToAddress = (SMShop.getFormElement("BILLTO", "BILLTO_SHIPTO_DIFFERS") != null);
	var relSelectedItemId = SMShop.getActivePayMethodId();
	var exclMethods = oSMPayMethods.exclMethods, arrExcl = null;
	var sId = '', bAdd = true, sRelId = sSelectFormName = '';
	var iActiveCount = totalPayMethodCount();
	var sErrDesc = getVar('errdesc');

	if(usingShipToAddress){
		usingShipToAddress = cbool(SMShop.getFormValue("BILLTO", "BILLTO_SHIPTO_DIFFERS", true));
	};
	if(usingShipToAddress){
		relShipToCountryCode = SMShop.getFormValue('SHIPTO','SHIPTO_COUNTRY', true).toUpperCase();
	};

	if(sErrDesc.length > 0){
		$('SMPAYERRDESC').innerHTML = sErrDesc;
		$('SMPAYERRDESC').style.display = '';
	} else {
		$('SMPAYERRDESC').style.display = 'none';
	};
	for(var pkid in m_exclPayMethodRegions){
		if(isNaN(pkid)) continue;
		if(usingShipToAddress && oSMPayMethods.payMethods[pkid].isShipCountryBased){
			relCountryCode = relShipToCountryCode;
		} else {
			relCountryCode = relBillToCountryCode;
		};

		if((m_exclPayMethodRegions[pkid].toUpperCase() + ',').indexOf(relCountryCode + ',') > -1){
			arrExcl = exclMethods.split(','); bAdd = true;
			for(var i=0;i<arrExcl.length;i++){
				if(parseInt(arrExcl[i]) == parseInt(pkid)){
					bAdd = false; break;
				};
			};
			if(bAdd){
				if(exclMethods.length > 0) exclMethods += ',';
				exclMethods += pkid.toString();
			};
		};
	};

	if(exclMethods.length > 0){
		arrExcl = exclMethods.split(',');
		for(var j=0;j<arrExcl.length;j++){
			sId = 'SMPAYMETHOD' + arrExcl[j];
			if($(sId)){
				$(sId).style.display = 'none';
				iActiveCount--;
			};
		};
	};

	if(iActiveCount > 0){
		for(var i=0;i<document.forms.length;i++){
			if(isPayform(document.forms[i])){
				sRelId = 'SMPAYMETHOD' + document.forms[i].SMPAYMETHOD_SELECTOR.value;
				if($(sRelId)){
					if($(sRelId).style.display != 'none'){
						sSelectFormName = document.forms[i].name;
						break;
					};
				};
			};
		};
		if(relSelectedItemId.length > 0){
			sRelId = 'SMPAYMETHOD' + relSelectedItemId;
			if($(sRelId)){
				if($(sRelId).style.display != 'none'){
					sRelId = 'SMPAYMETHOD_SELECTOR' + relSelectedItemId;
					if($(sRelId)){
						sSelectFormName = $(sRelId).form.name.toString();
					};
				};
			};
		};
		setActiveForm(sSelectFormName);
	} else {
		if($('SMPAYACTION')){
			$('SMPAYACTION').style.display = 'none';
		};
		if($('SMPAYNOTE')){
			$('SMPAYNOTE').style.display = '';
		};
	};
};

function SMPayMethods_saveRedirect(){
	var oSelectedForm = null, sId = '';
	var oActiveMethod = null, sVerify = '';

	for(var i=0;i<document.forms.length;i++){
		if(isPayform(document.forms[i])){
			if(document.forms[i].SMPAYMETHOD_SELECTOR.checked){
				oSelectedForm = document.forms[i];
				sId = document.forms[i].SMPAYMETHOD_SELECTOR.value;
				oActiveMethod = oSMPayMethods.payMethods[sId];
				oActiveMethod.payForm = oSelectedForm;
				break;
			}
		}
	};

	sVerify = 'SMFRMVerify_' + oSelectedForm.name;
	if(window[sVerify] != null){
		if(window[sVerify]() == false){
			return(false);
		};
	};

	if(SMShop.setPayMethod(oActiveMethod)){
		location.href = SMGetCheckoutStep('SM_RESERVED_DATA_ENTRY_PAYMENT', false);
	};
	return(false);
};

function SMPayMethods_goBack(){
	location.href = SMGetCheckoutStep('SM_RESERVED_DATA_ENTRY_PAYMENT', true);
};

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

	this.payMethods = new Array();
	this.exclMethods = SMShop.getActiveShipMethodExclusions();
	this.relTotalPrice = SMShop.getAttribute(_SMAOutGross)!="1" ? SMPrice.net : SMPrice.gross;

	this.addPayMethod = SMPayMethods_addMethod;
	this.addExclMethod = SMPayMethods_addExclMethod;
};

function SMPayMethod(){
	this.id = '';
	this.publicId = '';
	this.name = '';
	this.desc = '';
	this.thumb = '';
	this.chargeType = 0;
	this.chargeValue = 0;
	this.taxkey = 0;
	this.payForm = null;
	this.isShipCountryBased = false;
};

function SMPayMethods_addExclMethod(pkid){
	var arrExcl = null, bAdd = true;
	arrExcl = this.exclMethods.split(',');
	for(var i=0;i<arrExcl.length;i++){
		if(parseInt(arrExcl[i]) == parseInt(pkid)){
			return;
		};
	};
	if(this.exclMethods.length > 0) this.exclMethods += ',';
	this.exclMethods += pkid.toString();
};

function SMPayMethods_addMethod(values){
	var arr = values.split(';');
	var sOrderValue = arr[7];
	var arrOrderValue = null;
	var fromValue = maxValue = 0;

	if(sOrderValue.length > 0){
		arrOrderValue = sOrderValue.split(',');
		fromValue = parseFloat(arrOrderValue[0]);
		maxValue = parseFloat(arrOrderValue[1]);
		if(this.relTotalPrice < fromValue || (maxValue > 0 && this.relTotalPrice > fromValue && this.relTotalPrice > maxValue)){
			this.addExclMethod(arr[0].toString());
			return;
		};
	};

	var oMethod = new SMPayMethod();
	oMethod.id = SX_uEsc(arr[0]);
	oMethod.name = SX_uEsc(arr[1]);
	oMethod.desc = SX_uEsc(arr[2]);
	oMethod.thumb = SX_uEsc(arr[3]);
	oMethod.chargeType = parseFloat(arr[4]);
	oMethod.chargeValue = parseFloat(arr[5]);
	oMethod.taxkey = parseInt(arr[6]);
	oMethod.publicId = SX_uEsc(arr[8]);
	oMethod.isShipCountryBased = cbool(arr[9]);

	this.payMethods[arr[0].toString()] = oMethod;

};

var m_exclPayMethodRegions = new Array();
var oSMPayMethods = new SMPayMethods();

oSMPayMethods.addPayMethod('1;Vorkasse;Versand_20der_20Bestellung_20erfolgt_20nach_20Eingang_20des_20Rechnungsbetrags_20auf_20unser_20Konto_X3;;1;-2;1;;PM_X201001;1');if(SMShop.basket.payMethodInfo.xml.getAttribute(_SMAUniqueID)==""){SMShop.setPayMethod(oSMPayMethods.payMethods[(1).toString()]);};oSMPayMethods.addPayMethod('18;PayPal;Schnelle_20und_20sichere_20Zahlung_20mit_20PayPal_X3_20Ihre_20Daten_20werden_20in_20einem_20eigenen_20Terminal_X2Fenster_20_C3_BCber_20eine_20sichere_20Verbindung_20zu_20PayPal_20erfasst_X3;media_X4images_X4paypal_X3gif;1;-2;1;;Z_X201018;1');if(SMShop.basket.payMethodInfo.xml.getAttribute(_SMAUniqueID)==""){SMShop.setPayMethod(oSMPayMethods.payMethods[(1).toString()]);};m_exclPayMethodRegions[2] = 'CH,NL';oSMPayMethods.addPayMethod('2;Nachnahme;Bezahlen_20Sie_20die_20Rechnung_20bequem_20bei_20Erhalt_20der_20Ware_20per_20Nachnahme_20bei_20Ihrem_20Paketboten_X3_20Dies_20ist_20die_20teuerste_20Versandart_X3;;0;0;1;;PM_X201002;1');if(SMShop.basket.payMethodInfo.xml.getAttribute(_SMAUniqueID)==""){SMShop.setPayMethod(oSMPayMethods.payMethods[(1).toString()]);};m_exclPayMethodRegions[13] = 'AT,CH,NL';oSMPayMethods.addPayMethod('13;Bar;Keine_20weiteren_20Kosten!;;0;0;1;;P_X201014;0');if(SMShop.basket.payMethodInfo.xml.getAttribute(_SMAUniqueID)==""){SMShop.setPayMethod(oSMPayMethods.payMethods[(1).toString()]);};