function isJavaScriptAPISelected() {
var radios = document.getElementsByName('api');
for (var i = 0, length = radios.length; i < length; i++) {
if (radios[i].checked) {
if (radios[i].value === 'JavaScript')
return true ;
else // only one radio can be logically checked
break;
}
}
return false;
}
function processFormForJSInvocation(e) {
if (!isJavaScriptAPISelected())
return;
if (e.preventDefault) e.preventDefault();
var serverurl = document.getElementById('serviceUrl').value ;
InteractAPI.init( { "url" : serverurl } );
var commandsToExecute = { "ssid" : null, "commands" : [] };
var callback = InteractAPI.Callback.create(onSuccess, onError);
callStartSession(commandsToExecute, callback);
callGetOffers(commandsToExecute, callback);
callGetOffersForMultipleInteractionPoints(commandsToExecute, callback);
callPostEvent(commandsToExecute, callback);
callSetAudience(commandsToExecute, callback);
callGetProfile(commandsToExecute, callback);
callEndSession(commandsToExecute, callback);
callSetDebug(commandsToExecute, callback);
callGetVersion(commandsToExecute, callback);
callExecuteBatch(commandsToExecute, callback);
// You must return false to prevent the default form behavior
return false;
}
function callStartSession(commandsToExecute, callback) {
//read configured start session
var ssId = document.getElementById('ss_sessionId').value;
var icName = document.getElementById('ic').value;
var audId = document.getElementById('audienceId').value;
var audLevel = document.getElementById('audienceLevel').value;
var params = document.getElementById('ss_parameters').value;
var relyOldSs = document.getElementById('relyOnOldSession').value;
var debug = document.getElementById('ss_isDebug').value;
if (commandsToExecute && !commandsToExecute.ssid) {
commandsToExecute.ssid = ssId;
}
if (commandsToExecute && commandsToExecute.commands) {
commandsToExecute.commands.push(InteractAPI.CommandUtil.
createStartSessionCmd(
icName, getNameValuePairs(audId),
audLevel, getNameValuePairs(params),
relyOldSs, debug));
}
else {
InteractAPI.startSession(ssId, icName,
getNameValuePairs(audId), audLevel,
getNameValuePairs(params), relyOldSs,
debug, callback) ;
}
}
function callGetOffers(commandsToExecute, callback) {
var ssId = document.getElementById('go_sessionId').value;
var ip = document.getElementById('go_ipoint').value;
var nofRequested = 5 ;
var nreqString = document.getElementById('offersRequested').value;
if (!nreqString && nreqString!== '')
nofRequested = Number(nreqString);
if (commandsToExecute && !commandsToExecute.ssid) {
commandsToExecute.ssid = ssId;
}
if (commandsToExecute && commandsToExecute.commands) {
commandsToExecute.commands.push(InteractAPI.CommandUtil.
createGetOffersCmd(ip, nofRequested));
}
else {
InteractAPI.getOffers(ssId, ip, nofRequested, callback);
}
}
function callPostEvent(commandsToExecute, callback) {
var ssId = document.getElementById('pe_sessionId').value;
var ev = document.getElementById('event').value;
var params = document.getElementById('parameters').value;
if (commandsToExecute && !commandsToExecute.ssid) {
commandsToExecute.ssid = ssId;
}
if (commandsToExecute && commandsToExecute.commands) {
commandsToExecute.commands.push(InteractAPI.
CommandUtil.createPostEventCmd
(ev, getNameValuePairs(params)));
}
else {
InteractAPI.postEvent(ssId, ev, getNameValuePairs(params), callback);
}
}
function callGetOffersForMultipleInteractionPoints
(commandsToExecute, callback) {
var ssId = document.getElementById('gop_sessionId').value;
var requestDetailsStr = document.getElementById('requestDetail').value;
//trim string
var trimmed = requestDetailsStr.replace(/\{/g, "");
var parts = trimmed.split("}");
//sanitize strings
for(i = 0; i < parts.length; i += 1) {
parts[i] = parts[i].replace(/^\s+|\s+$/g, "");
}
//build get offer requests
var getOffReqs = [];
for(var i = 0; i < parts.length; i += 1) {
var getofReqObj = parseGetOfferReq(parts[i]);
if (getofReqObj) {
getOffReqs.push(getofReqObj);
}
}
if (commandsToExecute && !commandsToExecute.ssid) {
commandsToExecute.ssid = ssId;
}
if (commandsToExecute && commandsToExecute.commands) {
commandsToExecute.commands.push(InteractAPI.CommandUtil.
createGetOffersForMultiple
InteractionPointsCmd(getOffReqs));
}
else {
InteractAPI.getOffersForMultipleInteractionPoints
(ssId, getOffReqs, callback);
}
}
function parseGetOfferReq(ofReqStr) {
if (!ofReqStr || ofReqStr==="")
return null;
var posIp = ofReqStr.indexOf(',');
var ip = ofReqStr.substring(0,posIp);
var posNmReq = ofReqStr.indexOf(',', posIp+1);
var numReq = ofReqStr.substring(posIp+1,posNmReq);
var posDup = ofReqStr.indexOf(',', posNmReq+1);
var dupPolicy = null;
var reqAttributes = null;
if (posDup===-1)
dupPolicy = ofReqStr.substring(posNmReq+1);
else
dupPolicy = ofReqStr.substring(posNmReq+1,posDup);
//check if request string has attributes
var reqAttrPos = ofReqStr.search(/\(/g);
if (reqAttrPos!==-1) {
var reqAttributesStr = ofReqStr.substring(reqAttrPos);
reqAttributesStr = trimString(reqAttributesStr);
reqAttributesStr = removeOpenCloseBrackets(reqAttributesStr);
reqAttributes = parseReqAttributes(reqAttributesStr);
}
return InteractAPI.GetOfferRequest.create(ip, parseInt(numReq),
parseInt(dupPolicy), reqAttributes);
}
//trim string
function trimString(strToTrim) {
if (strToTrim)
return strToTrim.replace(/^\s+|\s+$/g, "");
else
return null;
}
function trimStrArray(strArray) {
if (!strArray) return ;
for(var i = 0; i < strArray.length; i += 1) {
strArray[i] = trimString(strArray[i]);
}
}
//remove open and close brackets in the end
function removeOpenCloseBrackets(strToUpdate) {
if (strToUpdate)
return strToUpdate.replace(/^\(+|\)+$/g, "");
else
return null;
}
function parseReqAttributes(ofReqAttrStr) {
//sanitize string
ofReqAttrStr = trimString(ofReqAttrStr);
ofReqAttrStr = removeOpenCloseBrackets(ofReqAttrStr);
if (!ofReqAttrStr || ofReqAttrStr==="")
return null;
//get the number requested
var pos = ofReqAttrStr.indexOf(",");
var numRequested = ofReqAttrStr.substring(0,pos);
ofReqAttrStr = ofReqAttrStr.substring(pos+1);
//first part will be attribute and rest will be child attributes
var parts = [];
pos = ofReqAttrStr.indexOf(",");
if (pos!==-1) {
parts.push(ofReqAttrStr.substring(0,pos));
parts.push(ofReqAttrStr.substring(pos+1));
}
else {
parts.push(ofReqAttrStr);
}
for(var i = 0; i < parts.length; i += 1) {
//sanitize string
parts[i] = trimString(parts[i]);
parts[i] = removeOpenCloseBrackets(parts[i]);
parts[i] = trimString(parts[i]);
}
//build list of attributes
var attributes = [];
var idx = 0;
if (parts[0]) {
var attParts = parts[0].split(";");
for (idx=0; idx<attParts.length; idx++) {
attParts[idx] = trimString(attParts[idx]);
attParts[idx] = removeOpenCloseBrackets(attParts[idx]);
attParts[idx] = trimString(attParts[idx]);
var atrObj = parseAttribute(attParts[idx]);
if (atrObj) attributes.push(atrObj);
}
}
//build list of child attributes
var childAttributes = [];
if (parts[1]) {
var childAttParts = parts[1].split(")");
for (idx=0; idx<childAttParts.length; idx++) {
childAttParts[idx] = trimString(childAttParts[idx]);
childAttParts[idx] = removeOpenCloseBrackets(childAttParts[idx]);
childAttParts[idx] = trimString(childAttParts[idx]);
//get the number requested
var noReqPos = childAttParts[idx].indexOf(",");
var numReqAt = childAttParts[idx].substring(0,noReqPos);
childAttParts[idx] = childAttParts[idx].substring(noReqPos+1);
childAttParts[idx] = trimString(childAttParts[idx]);
var atrObjParsed = parseAttribute(childAttParts[idx]);
if (atrObjParsed) {
var childReq = InteractAPI.OfferAttributeRequirements.create
(parseInt(numReqAt), [atrObjParsed], null);
childAttributes.push(childReq);
}
}
}
return InteractAPI.OfferAttributeRequirements.create(parseInt(numRequested),
attributes, childAttributes);
}
function parseAttribute(attStr) {
attStr = trimString(attStr);
if (!attStr || attStr==="")
return null;
var pos1 = attStr.indexOf("=");
var pos2 = attStr.indexOf("|");
var nvp = InteractAPI.NameValuePair.create
( attStr.substring(0,pos1),
attStr.substring(pos1+1, pos2),
attStr.substring(pos2+1));
return nvp;
}
function callSetAudience(commandsToExecute, callback) {
if (!document.getElementById('checkSetAudience').checked)
return ;
var ssId = document.getElementById('sa_sessionId').value;
var audId = document.getElementById('sa_audienceId').value;
var audLevel = document.getElementById('sa_audienceLevel').value;
var params = document.getElementById('sa_parameters').value;
if (commandsToExecute && !commandsToExecute.ssid) {
commandsToExecute.ssid = ssId;
}
if (commandsToExecute && commandsToExecute.commands) {
commandsToExecute.commands.push(InteractAPI.CommandUtil.
createSetAudienceCmd
(getNameValuePairs(audId), audLevel, getNameValuePairs(params)));
}
else {
InteractAPI.setAudience(ssId, getNameValuePairs(audId),
audLevel, getNameValuePairs(params),
callback);
}
}
function callGetProfile(commandsToExecute, callback) {
var ssId = document.getElementById('gp_sessionId').value;
if (commandsToExecute && !commandsToExecute.ssid) {
commandsToExecute.ssid = ssId;
}
if (commandsToExecute && commandsToExecute.commands) {
commandsToExecute.commands.push(InteractAPI.CommandUtil.
createGetProfileCmd());
}
else {
InteractAPI.getProfile(ssId, callback);
}
}
function callEndSession(commandsToExecute, callback) {
var ssId = document.getElementById('es_sessionId').value;
if (commandsToExecute && !commandsToExecute.ssid) {
commandsToExecute.ssid = ssId;
}
if (commandsToExecute && commandsToExecute.commands) {
commandsToExecute.commands.push(InteractAPI.CommandUtil.
createEndSessionCmd());
}
else {
InteractAPI.endSession(ssId, callback);
}
}
function callSetDebug(commandsToExecute, callback) {
var ssId = document.getElementById('sd_sessionId').value;
var isDebug = document.getElementById('isDebug').value;
if (commandsToExecute && !commandsToExecute.ssid) {
commandsToExecute.ssid = ssId;
}
if (commandsToExecute && commandsToExecute.commands) {
commandsToExecute.commands.push(InteractAPI.CommandUtil.
createSetDebugCmd(isDebug));
}
else {
InteractAPI.setDebug(ssId, isDebug, callback);
}
}
function callGetVersion(commandsToExecute, callback) {
if (commandsToExecute && commandsToExecute.commands) {
commandsToExecute.commands.push(InteractAPI.CommandUtil.
createGetVersionCmd());
}
else {
InteractAPI.getVersion(callback);
}
}
function callExecuteBatch(commandsToExecute, callback) {
if (!commandsToExecute)
return ;
InteractAPI.executeBatch(commandsToExecute.ssid,
commandsToExecute.commands, callback);
}
function getNameValuePairs(parameters) {
if (parameters === '')
return null ;
var parts = parameters.split(';');
var nvpArray = new Array(parts.length);
for(i = 0; i < parts.length; i += 1) {
var nvp = parts[i].split(',') ;
var value = null;
if (nvp[2]===InteractAPI.NameValuePair.prototype.TypeEnum.NUMERIC) {
if (isNaN(nvp[1])) {
value = nvp[1]; //a non number was provided as number,
pass it to API as it is
}
else {
value = Number(nvp[1]);
}
}
else {
value = nvp[1];
}
//special handling NULL value
if (value && typeof value === 'string') {
if (value.toUpperCase() === 'NULL') {
value = null;
}
}
nvpArray[i] = InteractAPI.NameValuePair.create(nvp[0], value, nvp[2]) ;
}
return nvpArray;
}
function showResponse(textDisplay) {
var newWin = open('','Response','height=300,width=300,titlebar=no,
scrollbars=yes,toolbar=no,
resizable=yes,menubar=no,location=no,status=no');
if (newWin.locationbar !== 'undefined' && newWin.locationbar
&& newWin.locationbar.visible)
newWin.locationbar.visible = false;
var displayHTML = '<META HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=UTF-8">
<html><head><style>TD { border-width : thin; border-style : solid }</style.'
+ "<script language='Javascript'>"
+ "var desiredDomain = 'unicacorp.com'; "
+ "if (location.href.indexOf(desiredDomain)>=0) "
+ "{ document.domain = desiredDomain;} "
+ "</script></head><body> "
+ textDisplay
+ "</body></html>" ;
newWin.document.body.innerHTML = displayHTML;
newWin.focus() ;
}
function onSuccess(response) {
showResponse("********Response********<br> " + JSON.stringify(response)) ;
}
function onError(response) {
showResponse("********Error********<br> " + response) ;
}
function formatResoponse(response) {
}
function printBatchResponse(batResponse) {
}
function printResponse(response) {
}