var SM_PAGEURL_WAIT_FOR_PAY_INFO = SMOMAbsoluteRootURL + "/warten-auf-zahlungsbestaetigung.html";
var SM_PAGEURL_SEND_ORDER_INFO = SMOMAbsoluteRootURL + "/bestellung-senden.html";
var SM_PAGEURL_DATA_ENTRY_VERIFY = SMOMAbsoluteRootURL + "/pruefen-und-senden.html";
var SM_PAGEURL_PURCHASE_ERROR = SMOMAbsoluteRootURL + "/fehler-bestellversand.html";
var SM_PAGEURL_POPPAY_SUCCESS = SMOMAbsoluteRootURL + '/modules/verify-order/redirect_poppay_success.html';
var SM_PAGEURL_POPPAY_CANCEL = SMOMAbsoluteRootURL + '/modules/verify-order/redirect_poppay_cancel.html';
var SM_PAGEURL_POPPAY_ERROR = SMOMAbsoluteRootURL + '/modules/verify-order/redirect_poppay_error.html';
var SM_PAGEURL_FRAMEPAY_SUCCESS = SMOMAbsoluteRootURL + '/modules/verify-order/redirect_framepay_success.html';
var SM_PAGEURL_FRAMEPAY_CANCEL = SMOMAbsoluteRootURL + '/modules/verify-order/redirect_framepay_cancel.html';
var SM_PAGEURL_FRAMEPAY_ERROR = SMOMAbsoluteRootURL + '/modules/verify-order/redirect_framepay_error.html';
var xScroll, yScroll;
var windowWidth, windowHeight;
function verifyOrder(oForm){
	var sPayMethodId = sPayMethodFormName = sPayMethodFunction = '';
	var oPayForm = document.getElementById('SMPayMethodPluginForm');
	var sTargetURL = "";

	switch(SMShop.validateDataForCheckout()){
		case 1:
			alert("Ihr Warenkorb enthält keine Einträge, bitte legen Sie mindestens einen Artikel in den Warenkorb.");
			sTargetURL = "index.html";
			
			/*sBasketURL = SMOMAbsoluteRootURL + "/" + sTargetURL;*/
			sBasketURL = SMShop.getAttribute('sarurl') + "/" + sTargetURL;
			
			location.href = sTargetURL;
			return(false);
			break;
		case 2:
			alert("Der Mindestbestellwert wurde unterschritten. Der Mindestbestellwert beträgt " + cprimary.format(SMShop.getAttribute(_SMAMinAmount), SM_CSYMBOL + SM_CGROUP));
			sTargetURL = "warenkorb.html";
			
			/*sTargetURL = SMOMAbsoluteRootURL + "/" + sTargetURL;*/
			sTargetURL = SMShop.getAttribute('sarurl') + "/" + sTargetURL;
			
			location.href = sTargetURL;
			return(false);
			break;
		};

	if(oForm.cust_accept_gtb){
		if(!oForm.cust_accept_gtb.checked){
			alert(SX_uEsc('Bitte_20best_C3_A4tigen_20Sie_20unsere_20AGB_20als_20gelesen_X3'));
			oForm.cust_accept_gtb.focus();
			return(false);
		};
	};
	if(oForm.cust_accept_info){
		if(!oForm.cust_accept_info.checked){
			alert(SX_uEsc('Bitte_20best_C3_A4tigen_20Sie_20die_20Kundeninformationen_20als_20gelesen_X3'));
			oForm.cust_accept_info.focus();
			return(false);
		};
	};
	if(oForm.cust_newsletter_abo){
		SMShop.setAttribute('newsletter-abo', (oForm.cust_newsletter_abo.checked?'1':''));
	};
	if(oForm.cust_send_copy){
		SMShop.setAttribute('send-order-copy', (oForm.cust_send_copy.checked?'1':''));
	};

	try{oForm.disabled = "disabled"}
	catch(e){};

	setPayMethodFormFields();

	try{
		sPayMethodFormName = SMShop.basket.payMethodInfo.xml.selectSingleNode("form").getAttribute("name").toUpperCase();
		sPayMethodId = SMShop.basket.payMethodInfo.xml.getAttribute("uid").toString();
		sPayMethodFormName = sPayMethodFormName.toString().replace(sPayMethodId, '');
		if(window[sPayMethodFormName + '_doPay'](oPayForm, true)){
			location.href = SM_PAGEURL_WAIT_FOR_PAY_INFO;
		};
	} catch(e){
		location.href = SM_PAGEURL_SEND_ORDER_INFO;
	};
	return(false);
};

function setPayMethodFormFields(){
	var oPayForm = document.getElementById('SMPayMethodPluginForm');
	var oForm = SMShop.basket.payMethodInfo.xml.selectSingleNode("form");
	var sPayId = SMShop.basket.payMethodInfo.xml.getAttribute("uid").toString();
	var sFunction = oForm.getAttribute("name").toUpperCase().replace(sPayId, '') + '_setFieldValue';
	var oElement = null, sFields = sName = sValue = '';

	for(var i=0;i<oForm.childNodes.length();i++){
		oElement = oForm.childNodes.item(i);
		sName = oElement.getAttribute("name");

		if(sName.indexOf('SMPAYMETHOD') > -1) continue;

		if(oElement.getAttribute("value")){
			sValue = oElement.getAttribute("value");
		} else {
			sValue = oElement.text;
		};

		try{sValue = window[sFunction](sName, sValue)}catch(e){};

		if(oElement.getAttribute("value")){
			oElement.setAttribute(sName, sValue);
		} else {
			oElement.text = sValue;
		};

		sFields += '<input type="hidden" name="' + sName + '" value="' + sValue + '">';
	};

	SMShop.update();

	oPayForm.innerHTML = sFields;
};

function initDimensions(){

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	};

	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	};

	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	};

	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	};
};

function showPayTerminalFrame(){

	initDimensions();

	var oBody = document.getElementsByTagName("body").item(0);
	var oImage = document.createElement("img");
	oBody.style.overflow = 'hidden';
	oImage.setAttribute('src', SMOMAbsoluteRootURL + '/modules/verify-order/framepayoverlay.png');
	with(oImage.style){
		position = 'absolute';
		width = '100%'; //pageWidth + 'px';
		height = pageHeight + 'px';
		top = 0;
		left = 0;
		zIndex = 1000;
	};
	oBody.insertBefore(oImage, oBody.firstChild);

	var iFrameWidth = 800, iFrameHeight = 540;

	var sLeft = (pageWidth - iFrameWidth) / 2;
	if (sLeft < 0) sLeft = 0;
	var sTop = (windowHeight - iFrameHeight) / 2;
	if (sTop < 0) sTop = 0;	

	var oFrame = document.getElementById('InlinePayment');

	Position.prepare();
  	var mArr = Position.realOffset(oFrame);

	with(oFrame.style){
		background = '#ffffff';
		border = '3px solid black';
		position = 'absolute';
		width = '800px';
		height = '540px';
		if (bd_isIE || navigator.userAgent.match("rv:1\.9")) top = (mArr[1] + sTop - 50) + 'px';
		else if (bd_isOpera) top = (windowHeight/2 - 140);
		else if (bd_isSafari) top = (pageHeight - windowHeight) + 20;
  		else top = (windowHeight/2 - 270);
		left = sLeft + 'px';
		zIndex = 1500;
		display = 'block';
	};

	oBody.insertBefore(oFrame, oBody.firstChild);

};

Event.observe(window, 'resize', window_onResize);

function window_onResize(){

	initDimensions();

	var oFrame = document.getElementById('InlinePayment');

	var iFrameWidth = oFrame.style.width.substring(0, oFrame.style.width.length - 2), iFrameHeight = oFrame.style.height.substring(0, oFrame.style.width.length - 2);

	if(oFrame.style.display != 'block') return;

	var sLeft = ((pageWidth - iFrameWidth) / 2);
	if (sLeft < 0) sLeft = 0;
	var sTop = ((windowHeight - iFrameHeight) / 2);
	if (sTop < 0) sTop = 0;

	Position.prepare();
  	var mArr = Position.realOffset(oFrame);

	with(oFrame.style){
		background = '#ffffff';
		border = '3px solid black';
		position = 'absolute';

		width = iFrameWidth + "px";
		height = iFrameHeight + "px";

		if (bd_isIE || navigator.userAgent.match("rv:1\.9")) top = (mArr[1] + sTop) + 'px';
		else if (bd_isOpera) top = (windowHeight/2 - 140);
		else if (bd_isSafari) top = (pageHeight - windowHeight) + 20;
  		else top = ((windowHeight - iFrameHeight) / 2) + 'px';

		left = sLeft + 'px';
		zIndex = 1500;
		display = 'block';
	};
};