
SMOPrice = new cSMPrice(), SMDPrice = new cSMPrice(), sSubtotal = sDiscSubtotal = "";
sSubtotal = SMShop.basket.getAttribute(_SMASubTotal);
sDiscSubtotal = SMShop.basket.getAttribute(_SMADSubTotal);
SMOPrice.decode(sSubtotal);
SMDPrice.decode(sDiscSubtotal);
var iDiscount = cprimary.format(SMOPrice.gross - SMDPrice.gross, SM_CNOFORMAT).toFixed(2);

function transformSpecialChar(value){
	value = value.replace(new RegExp(String.fromCharCode(220), "gi"), "ue");
	value = value.replace(new RegExp(String.fromCharCode(228), "gi"), "ae");
	value = value.replace(new RegExp(String.fromCharCode(246), "gi"), "oe");
	value = value.replace(new RegExp(String.fromCharCode(223), "gi"), "ss");
	value = value.replace(/(?:&#\d{2,5};)|(?:[^\w\s])/gi, "");
	return(value);
};

function CompleteDateValues(value){
	if(value.toString().length == "1") 
		value = "0" + value;
	return value;
}

var date = new Date();
var sDate = "";
sDate = date.getFullYear() + "-" + CompleteDateValues(date.getMonth()) + "-" + CompleteDateValues(date.getDay()) + "." + CompleteDateValues(date.getHours()) + ":" + CompleteDateValues(date.getMinutes()) + ":" + CompleteDateValues(date.getSeconds());

function ScriptInclude(){
	this.src = "";
	this.args = new Array();
	this.set = ScriptInclude_Set;
};

function ScriptInclude_Set(){
	var tmpHTML = "";
	var tmpArgs = "";
	tmpHTML = "<script type='text\/javascript'"
		+ " src='" + this.src + "?";
	for(var i=0; i<this.args.length; i++){
		tmpArgs += this.args[i].replace("/","\/") + "&";
	};
	tmpHTML += tmpArgs;
	tmpHTML	+= "'><\/script>";
	document.write(tmpHTML);
};

function createInputElement(name, value){
	if (bd_isIE) var InputElementItemNumber = document.createElement('<input name="' + name + '" />');
	else {
		var InputElementItemNumber = document.createElement('input');
		InputElementItemNumber.name = name;
	}
	InputElementItemNumber.setAttribute("value", value);
	InputElementItemNumber.type = "hidden";

	return InputElementItemNumber;
};

var G2SPayment = null;
G2SPayment = new ScriptInclude();
G2SPayment.src = "plugins/payment/smpaygate2shop/decrypt.php";

var sHash = "";
var oPayForm = document.getElementById('SMPayMethodPluginForm');
var products = SMShop.basket.xml.selectNodes(_SMPrd), product = null;
var sNumberOfProducts = products.length();

sHash += "801542568579250247";
sHash += "EUR";

var oAmount = new cSMPrice();
oAmount.decode(SMShop.basket.getAttribute(_SMAFinalSum));
sHash += cprimary.format(oAmount.gross, SM_CNOFORMAT);

var realShippingCost = cprimary.format(oAmount.gross, SM_CNOFORMAT);

for(var i=1; i < products.length() + 1; i++){
	product = new cSMProduct();
	product.init(products.item(i-1));

	sHash += transformSpecialChar(product.name);
	sHash += cprimary.format(product.getPrice(false, true), SM_CNOFORMAT)/product.getAttribute(_SMAAmount);
	/*
	sHash += cprimary.format(product.getPrice(true, true), SM_CNOFORMAT).toFixed(2);
	*/
	sHash += product.getAttribute(_SMAAmount);
};

sHash += sDate;

G2SPayment.args[0] = "param=" + sHash;
G2SPayment.set();

function GATE2SHOP_doPay(oPayForm, bShowWaitForPay){
	var sFormTarget = 'PaymentTerminal';
	var oWin = null;

	var products = SMShop.basket.xml.selectNodes(_SMPrd), product = null;

	for(var i=1; i < products.length() + 1; i++){
		product = new cSMProduct();
		product.init(products.item(i-1));

		oPayForm.appendChild(createInputElement('item_name_' + i, transformSpecialChar(product.name)));
		/*
		if (bd_isIE) var InputElementItemName = document.createElement('<input name="item_name_' + i + '" />');
		else {
			var InputElementItemName = document.createElement('input');
			InputElementItemName.name = 'item_name_' + i;
		}
		InputElementItemName.setAttribute("value", transformSpecialChar(product.name));
		InputElementItemName.type = "hidden";
		oPayForm.appendChild(InputElementItemName);
		*/

		oPayForm.appendChild(createInputElement('item_number_' + i, product.getAttribute("cde")));
		/*
		if (bd_isIE) var InputElementNumber = document.createElement('<input name="item_number_' + i + '" />');
		else {
			var InputElementNumber = document.createElement('input');
			InputElementNumber.name = 'item_number_' + i;
		}
		InputElementNumber.setAttribute("value", product.getAttribute("cde"));
		InputElementNumber.type = "hidden";
		oPayForm.appendChild(InputElementNumber);
		*/

		oPayForm.appendChild(createInputElement('item_amount_' + i, cprimary.format(product.getPrice(false, true), SM_CNOFORMAT)/product.getAttribute(_SMAAmount)));
		/*
		if (bd_isIE) var InputElementItemAmount = document.createElement('<input name="item_amount_' + i + '" />');
		else {
			var InputElementItemAmount = document.createElement('input');
			InputElementItemAmount.name = 'item_amount_' + i;
		}
		InputElementItemAmount.setAttribute("value", cprimary.format(product.getPrice(false, true), SM_CNOFORMAT)/product.getAttribute(_SMAAmount));
		/*
		InputElementItemAmount.setAttribute("value", cprimary.format(product.getPrice(true, true), SM_CNOFORMAT).toFixed(2));
		*/
		/*
		InputElementItemAmount.type = "hidden";
		oPayForm.appendChild(InputElementItemAmount);
		*/

		realShippingCost -= cprimary.format(product.getPrice(false, true), SM_CNOFORMAT);

		oPayForm.appendChild(createInputElement('item_quantity_' + i, product.getAttribute(_SMAAmount)));
		/*
		if (bd_isIE) var InputElementQuantity = document.createElement('<input name="item_quantity_' + i + '" />');
		else {
			var InputElementQuantity = document.createElement('input');
			InputElementQuantity.name = 'item_quantity_' + i;
		}
		InputElementQuantity.setAttribute("value", product.getAttribute(_SMAAmount));
		InputElementQuantity.type = "hidden";
		oPayForm.appendChild(InputElementQuantity);
		*/
	};

	oPayForm.appendChild(createInputElement('checksum', sServerHashSB));
	/*
	if (bd_isIE) var InputElementHash = document.createElement('<input name="checksum" />');
	else {
		var InputElementHash = document.createElement('input');
		InputElementHash.name = 'checksum';
	}
	InputElementHash.setAttribute("value", sServerHashSB);
	InputElementHash.type = "hidden";
	oPayForm.appendChild(InputElementHash);
	*/
	/* Begin Version */

	oPayForm.appendChild(createInputElement('version', "3.0.0"));
	/*
	if (bd_isIE) var InputElementVersion = document.createElement('<input name="version" />');
	else {
		var InputElementVersion = document.createElement('input');
		InputElementVersion.name = 'version';
	}
	InputElementVersion.setAttribute("value", "3.0.0");
	InputElementVersion.type = "hidden";
	oPayForm.appendChild(InputElementVersion);
	*/

	/* End Version */

	/* Begin shipping, handling and total tax */

	var SMShippingCost = new cSMPrice();
	SMShippingCost.decode(SMShop.basket.shipMethodInfo.xml.getAttribute(_SMAPrice));
	var ShippingCost = cprimary.format(SMShippingCost.gross, SM_CNOFORMAT).toFixed(2);

	var SMShipSurcharge = new cSMPrice();
	SMShipSurcharge.decode(SMShop.basket.getAttribute(_SMAFinalShipSurcharges));
	var ShippingSurcharge = cprimary.format(SMShipSurcharge.gross,SM_CNOFORMAT).toFixed(2);

	ShippingCost = parseFloat(ShippingCost) + parseFloat(ShippingSurcharge);

	realShippingCost = parseFloat(realShippingCost) + parseFloat(iDiscount);

	if (realShippingCost.toFixed(2) != 0){
		oPayForm.appendChild(createInputElement('shipping', realShippingCost.toFixed(2)));
		/*
		if (bd_isIE) var InputElementShipping = document.createElement('<input name="shipping" />');
		else {
			var InputElementShipping = document.createElement('input');
			InputElementShipping.name = 'shipping';
		}
		InputElementShipping.setAttribute("value", realShippingCost.toFixed(2));
		InputElementShipping.type = "hidden";
		oPayForm.appendChild(InputElementShipping);
		*/
	}

	if (iDiscount != 0){
		oPayForm.appendChild(createInputElement('discount', iDiscount));
		/*
		if (bd_isIE) var InputElementDiscount = document.createElement('<input name="discount" />');
		else {
			var InputElementDiscount = document.createElement('input');
			InputElementDiscount.name = 'discount';
		}
		InputElementDiscount.setAttribute("value", iDiscount);
		InputElementDiscount.type = "hidden";
		oPayForm.appendChild(InputElementDiscount);
		*/
	}

	/* End shipping, handling and total tax */

	
	bShowWaitForPay = false;
	
	with(oPayForm){
		action = 'https://secure.gate2shop.com/ppp/purchase.do';

		submit();
	};

	return(bShowWaitForPay);
};

function GATE2SHOP_setFieldValue(fieldName, fieldValue){
	switch(fieldName){

		case 'merchant_site_id':
			fieldValue = "32745";
			break;
		case 'merchant_id':
			fieldValue = "801542568579250247";
			break;
		case 'time_stamp':
			fieldValue = sDate;
			break;
		case 'total_amount':
			var oAmount = new cSMPrice();
			oAmount.decode(SMShop.basket.getAttribute(_SMAFinalSum));
			/*
			fieldValue = cprimary.format(oAmount.gross, SM_CNOFORMAT).toFixed(2);
			*/
			fieldValue = cprimary.format(oAmount.gross, SM_CNOFORMAT);
			break;
		case 'currency':
			fieldValue = "EUR";
			break;
		case 'numberofitems':
			fieldValue = sNumberOfProducts;
			break;
		case 'merchant_unique_id':
			fieldValue = SMShop.getAttribute("sid");
			break;
		case 'invoice_id':
			fieldValue = SMShop.getAttribute("sid");
			break;
		case 'total_tax':
			/*
			var oTaxAmount = new cSMPrice();
			oTaxAmount.decode(SMShop.basket.getAttribute(_SMAFinalSum));
			fieldValue = Math.round(((cprimary.format(oTaxAmount.gross, SM_CNOFORMAT) / cprimary.format(oTaxAmount.net, SM_CNOFORMAT)) - 1) * 100);
			*/
			break;
		case 'payment_method':
			break;
		case 'Token':
			break;
		case 'cc_card_number':
			break;
		case 'cc_name_on_card':
			break;
		case 'cc_exp_year':
			break;
		case 'cc_exp_month':
			break;
		case 'cc_cvv2':
			break;

		/* User Information */
		case 'first_name':
			fieldValue = transformSpecialChar(SMShop.getFormValue("BILLTO", "BILLTO_FIRSTNAME"));
			break;
		case 'last_name':
			fieldValue = transformSpecialChar(SMShop.getFormValue("BILLTO", "BILLTO_LASTNAME"));
			break;
		case 'email':
			fieldValue = SMShop.getFormValue("BILLTO", "BILLTO_EMAIL");
			break;
		case 'address1':
			fieldValue = transformSpecialChar(SMShop.getFormValue("BILLTO", "BILLTO_STREET_1"));
			break;
		case 'address2':
			fieldValue = transformSpecialChar(SMShop.getFormValue("BILLTO", "BILLTO_STREET_2"));
			break;
		case 'city':
			fieldValue = transformSpecialChar(SMShop.getFormValue("BILLTO", "BILLTO_CITY"));
			break;
		case 'country':
			fieldValue = transformSpecialChar(SMShop.getFormValue("BILLTO", "BILLTO_COUNTRY", true));
			break;
		case 'zip':
			fieldValue = SMShop.getFormValue("BILLTO", "BILLTO_POSTCODE");
			break;
		case 'phone1':
			fieldValue = SMShop.getFormValue("BILLTO", "BILLTO_PHONE_NUMBER");
			break;

		/* Navigation Parameters */
		case 'skip_billing_tab':
			fieldValue = "false";
			break;
		case 'skip_review_tab':
			fieldValue = "false";
			break;	
	};
	return(fieldValue);
};

function GATE2SHOP_removePayFormFields(){
	return(true);
};

function GATE2SHOP_setForwardPayformFieldNames(){
	var sFields = 'Status;totalAmount;TransactionID;ClientUniqueID;ErrCode;ExErrCode;AuthCode;Reason;Token;ReasonCode';
	return(sFields);
};

function GATE2SHOP_setResponseQueryCaptions(param){
	var sCaption = '';
	switch(param.toLowerCase()){

		case 'status': 
			sCaption = 'Status'; 
			break;
		case 'totalamount': 
			sCaption = 'Gesamtbetrag'; 
			break;
		case 'transactionid': 
			sCaption = 'Transaktionsnr.'; 
			break;
		case 'clientuniqueid': 
			sCaption = 'Kundennr.'; 
			break;
		case 'authcode': 
			sCaption = 'Authentifizierung'; 
			break;
		case 'token': 
			sCaption = 'Token'; 
			break;

		default: sCaption = param;

	};
	return(sCaption);
};
