//======================================================================
// oci interface


function oci2(w) { // w == windowReference
	var d = w.document;
//	d.write('<form name="ociForm" action="'+belenusHome.getValidOCI_HOOK_URL()+'" method="post" target="'+belenusHome.name+'">');
	var formTarget = (belenusHome.transferParameters['~TARGET'] != null) ? belenusHome.transferParameters['~TARGET'] : belenusHome.name;
	d.write('<form name="ociForm" action="'+belenusHome.getValidOCI_HOOK_URL()+'" method="post" target="'+formTarget+'">');	
	var quoteID = new Date();
	quoteID = quoteID.getTime();
	
	var desField =		belenusHome.getFieldPos(belenusHome.basketFields, belenusHome.descriptionField);
	var matNrField =	belenusHome.getFieldPos(belenusHome.basketFields, belenusHome.ociMasternum);
	var _quantityField =	belenusHome.getFieldPos(belenusHome.basketFields, belenusHome.quantityField);
	var _priceField =	belenusHome.getFieldPos(belenusHome.basketFields, belenusHome.priceField);
	var partnoField =	belenusHome.getFieldPos(belenusHome.basketFields, belenusHome.basketFields[0]); // partno...
	
	for(i in belenusHome.belenusBasket[0]) {
		var description = belenusHome.belenusBasket[desField][i];
		if(description.length >= 40) {
			description = description.substring(0, 39);
		}
    
    var priceValue = "";
    if(belenusHome.priceField != null) {
      priceValue = belenusHome.belenusBasket[_priceField][i];
    }    
    
  // internal mapping
  var ociInternalMapping = new Array();
    ociInternalMapping["NEW_ITEM-DESCRIPTION"] = description;
    ociInternalMapping["NEW_ITEM-MATNR"] = belenusHome.belenusBasket[matNrField][i];
    ociInternalMapping["NEW_ITEM-MATGROUP"] = "";
    ociInternalMapping["NEW_ITEM-QUANTITY"] = belenusHome.belenusBasket[_quantityField][i];
    ociInternalMapping["NEW_ITEM-UNIT"] = 'EA';
    ociInternalMapping["NEW_ITEM-PRICE"] = priceValue;
    ociInternalMapping["NEW_ITEM-PRICEUNIT"] = '';
    ociInternalMapping["NEW_ITEM-CURRENCY"] = 'EUR';
    ociInternalMapping["NEW_ITEM-LEADTIME"] = '';
    ociInternalMapping["NEW_ITEM-VENDOR"] = belenusHome.checkValue(belenusHome.supNo);
    ociInternalMapping["NEW_ITEM-VENDORMAT"] = belenusHome.belenusBasket[partnoField][i];
    ociInternalMapping["NEW_ITEM-MANUFACTCODE"] = belenusHome.checkValue(belenusHome.supNo);
    ociInternalMapping["NEW_ITEM-MANUFACTMAT"] = belenusHome.belenusBasket[partnoField][i];
    ociInternalMapping["NEW_ITEM-CONTRACT"] = '';
    ociInternalMapping["NEW_ITEM-CONTRACT_ITEM"] = '';
    ociInternalMapping["NEW_ITEM-SERVICE"] = '';
    ociInternalMapping["NEW_ITEM-EXT_QUOTE_ID"] = quoteID;
    ociInternalMapping["NEW_ITEM-EXT_QUOTE_ITEM"] = eval(parseInt(i)+1);
    ociInternalMapping["NEW_ITEM-EXT_PRODUCT_ID"] = belenusHome.belenusBasket[partnoField][i];
    ociInternalMapping["NEW_ITEM-LONGTEXT_1:132"] = description;
    ociInternalMapping["NEW_ITEM-CUST_FIELD1"] = '';
    ociInternalMapping["NEW_ITEM-CUST_FIELD2"] = '';
    ociInternalMapping["NEW_ITEM-CUST_FIELD3"] = '';
    ociInternalMapping["NEW_ITEM-CUST_FIELD4"] = '';
    ociInternalMapping["NEW_ITEM-CUST_FIELD5"] = '';          
		
		if(ociInternalMapping["NEW_ITEM-MATNR"] == "" || ociInternalMapping["NEW_ITEM-MATNR"] == "null") {
			d.write('<input type="hidden" name="NEW_ITEM-DESCRIPTION['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-DESCRIPTION"]+'">');
		} else {
			d.write('<input type="hidden" name="NEW_ITEM-DESCRIPTION['+eval(parseInt(i)+1)+']" value="">');
		}	
		d.write('<input type="hidden" name="NEW_ITEM-MATNR['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-MATNR"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-MATGROUP['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-MATGROUP"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-QUANTITY['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-QUANTITY"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-UNIT['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-UNIT"]+'">');
    d.write('<input type="hidden" name="NEW_ITEM-PRICE['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-PRICE"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-PRICEUNIT['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-PRICEUNIT"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-CURRENCY['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-CURRENCY"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-LEADTIME['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-LEADTIME"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-VENDOR['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-VENDOR"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-VENDORMAT['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-VENDORMAT"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-MANUFACTCODE['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-MANUFACTCODE"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-MANUFACTMAT['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-MANUFACTMAT"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-CONTRACT['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-CONTRACT"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-CONTRACT_ITEM['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-CONTRACT_ITEM"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-SERVICE['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-SERVICE"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-EXT_QUOTE_ID['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-EXT_QUOTE_ID"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-EXT_QUOTE_ITEM['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-EXT_QUOTE_ITEM"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-EXT_PRODUCT_ID['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-EXT_PRODUCT_ID"]+'">');		
		d.write('<input type="hidden" name="NEW_ITEM-LONGTEXT_'+eval(parseInt(i)+1)+':132[]" value="'+ociInternalMapping["NEW_ITEM-LONGTEXT_1:132"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-CUST_FIELD1['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-CUST_FIELD1"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-CUST_FIELD2['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-CUST_FIELD2"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-CUST_FIELD3['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-CUST_FIELD3"]+'">');
		d.write('<input type="hidden" name="NEW_ITEM-CUST_FIELD4['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-CUST_FIELD4"]+'">');
    d.write('<input type="hidden" name="NEW_ITEM-CUST_FIELD5['+eval(parseInt(i)+1)+']" value="'+ociInternalMapping["NEW_ITEM-CUST_FIELD5"]+'">');		
	}	
	//d.write('<input type="hidden" name="OCI_VERSION" value="2.0">');
	d.write('</form>');
}

function sendOCIBasket() {
	var dest = (belenusHome.oci_sendSwitcher != 0) ? "xmlForm" : "ociForm";
	belenusHome.whereIsTheForm.document.forms[dest].submit();
	belenusHome.delAllItems(false);
	belenusHome.WM.closeWindow('printPopup');
	//belenusHome.WM.closeWindow('basketPopup');	
  if(!belenusHome.includedOrPopupBasket) belenusHome.WM.closeWindow(belenusHome.basketWinName);
  else belenusHome.WM.closeWindow(belenusHome.basketWinName+'Extra');  
}

		
function SAP_encode_b64(Str) {
	var encStr = "";
	var base64 = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' ];
	for (var i = 0; i < Str.length; i += 3) {
	encStr += base64[(Str.charCodeAt(i) >>> 2)];
	if(!Str.charAt(i+1)) {encStr += '=='; break;}
	encStr += base64[(((Str.charCodeAt(i) & 0x03) << 4) | Str.charCodeAt(i+1) >>> 4)];
	if(!Str.charAt(i+2)) {encStr += '='; break;}
	encStr += base64[(((Str.charCodeAt(i+1) & 0x0F) << 2) |Str.charCodeAt(i+2) >>> 6)];
	encStr += base64[(Str.charCodeAt(i+2) & 0x3F)];
	}
	return encStr;
}

function generateXMLString() {
  returnString = "";
  returnString += '<?xml version="1.0" encoding="'+document.defaultCharset+'" ?> ';
  returnString += "<BusinessDocument><CatalogHeader/><Catalog>";
  
  var desField =		belenusHome.getFieldPos(belenusHome.basketFields, belenusHome.descriptionField);
  var matNrField =	belenusHome.getFieldPos(belenusHome.basketFields, belenusHome.ociMasternum);
  var _quantityField =	belenusHome.getFieldPos(belenusHome.basketFields, belenusHome.quantityField);
  var _priceField =	belenusHome.getFieldPos(belenusHome.basketFields, belenusHome.priceField);
  var partnoField =	belenusHome.getFieldPos(belenusHome.basketFields, belenusHome.basketFields[0]); // partno...

  for(i in belenusHome.belenusBasket[0]) {
    var description = belenusHome.belenusBasket[desField][i];
    if(description.length >= 40) {
      description = description.substring(0, 39);
    }
  	var productID ="";
  	if(belenusHome.belenusBasket[matNrField][i] != "" && belenusHome.belenusBasket[matNrField][i] != null) {
  		productID = '<ProductID Code="Buyer">'+belenusHome.belenusBasket[matNrField][i]+'</ProductID>';
    }    
    var priceValue = "";
    if(belenusHome.priceField != null) {
      priceValue = belenusHome.belenusBasket[_priceField][i];
    }    
  	
    returnString+= '<Product ProductType = "Good">'+
    '<CatalogKey>'+belenusHome.belenusBasket[partnoField][i]+'</CatalogKey>'+productID+
    '<Description Language = "DE">'+description+'</Description>'+
    '<ShoppingBasketItem RefVendorDescription = "0" RefManufacturerDescription = "1">'+
    '<Quantity UoM = "PCE">'+belenusHome.belenusBasket[_quantityField][i]+'</Quantity>'+
    '<NetPrice>'+
    '<Price Currency = "EUR">'+priceValue+'</Price>'+
    '<PriceUnit>1</PriceUnit>'+
    '</NetPrice>'+
    '<LeadTime>1</LeadTime>'+
    '<Quote>'+
    '<QuoteID>1</QuoteID>'+
    '<QuoteItemID>'+eval(parseInt(i)+1)+'</QuoteItemID>'+
    '</Quote>'+
    '<ItemText Language = "DE">'+description+'</ItemText>'+
    '</ShoppingBasketItem>'+
    '<ManufacturerDescription ID = "1">'+
    '<PartnerProductID Code = "Other">'+belenusHome.belenusBasket[partnoField][i]+'</PartnerProductID>'+
    '<PartnerID Code = "Other">'+belenusHome.checkValue(belenusHome.supNo)+'</PartnerID>'+
    '</ManufacturerDescription>'+
    '<VendorDescription ID = "0">'+
    '<PartnerProductID Code = "Other">'+belenusHome.belenusBasket[partnoField][i]+'</PartnerProductID>'+
    '<PartnerID Code = "Other">'+belenusHome.checkValue(belenusHome.supNo)+'</PartnerID>'+
    '<LeadTime>1</LeadTime>'+
    '<BuyerContract>'+
    '<ContractID/>'+
    '<ContractItemID/>'+
    '</BuyerContract>'+
    '</VendorDescription>'+
    '</Product>';
  
  }
  return returnString += "</Catalog></BusinessDocument>   ";
}

function xml(winRef) {
	var d = winRef.document;
	d.write('<form name="xmlForm" action="'+belenusHome.getValidOCI_HOOK_URL()+'" method="post" target="'+belenusHome.name+'">');
	d.write('<input type="hidden" name="~xmlDocument" value="'+SAP_encode_b64(generateXMLString())+'">');
	d.write('<input type="hidden" name="~xml_type" value="ESAPO">');
	d.write('</form>');
}

//==========================================================================