//This page needs to be configured for each site

//Base url for content match. Each site has a different hostname
var g_baseOvertureCMUrl = "http://cm.mtv.overture.com/js_flat_1_0/";
//source paramater for content match. Each site has its own source code
var g_overtureCMSource = "viacom_mtv_ctxt";
//Config code for each site
var g_overtureCMConfig = "1458724563";
//Base url for partner search. 
var g_baseOvertureSearchUrl = "http://html.overture.com/d/search/p/mtv/js/flat/";
//partner paramater for search. 
var g_overtureSearchPartner = "viacom_mtv_search";
var g_baseOvertureLinkspotUrl = "http://cmls.overture.com/ls_js_1_0/";
//default linkspot id.
var g_defaultLinkspotId = "mtv_games";
//source paramater for linkspots. Each site must configure its own.
var g_overtureLinkspotSource = "mtv_games_ls_ctxt_test";
//Config code for each site. Each site must configure its own.
var g_overtureLinkspotConfig = "1458724563";

var g_contextIdMap = new Object();
g_contextIdMap["/news/youtellus"] = "news";
g_contextIdMap["/music/artist"] = "music";
g_contextIdMap["/music/the_leak"] = "music";
g_contextIdMap["/news"] = "news";
g_contextIdMap["/mobile"] = "mobile";
g_contextIdMap["/ontv"] = "ontv";
g_contextIdMap["/music"] = "music";
g_contextIdMap["/movies"] = "movies";
g_contextIdMap["/games"] = "games";
g_contextIdMap["/thinkmtv"] = "think";

var g_typeIdMap = new Object();
g_typeIdMap["/news/youtellus"] = "youtellus";
g_typeIdMap["/music/artist"] = "artist_page";
g_typeIdMap["/music/the_leak"] = "theleak";
g_typeIdMap["/news"] = "news_article";
g_typeIdMap["/mobile"] = "mobile";
g_typeIdMap["/ontv"] = "ontv";
g_typeIdMap["/music"] = "music";
g_typeIdMap["/movies"] = "movies";
g_typeIdMap["/games"] = "games";
g_typeIdMap["/thinkmtv"] = "think";

//Default links to use in case we don't get results back.
zSr_dummy = new Array("Reach 80% of active Internet users with Overture.","","","List your site with Overture","http://www.overture.com/","",
"See the latest Accord offers at the Honda Official Site.",
"",
"http://ypn-100.overture.com/d/sr/?xargs=SjGTYHM2WSfAwg8ep-u2ASFl94ZiTOXSU7PrBi34b0dkMRpxIz_DgoDqDHPG_NIidq9XtjihuvhsnU-qzNGWIEpI0WYkcoPdGWF6vHcW_pmyJkWX7VREKmh2o6lFQdzPF1NYiCCDFPBMzTW1E9ywARKkbBHDxtdkScwJOM-imi-fb8Z061AriZIDa1EDdkfqf3QyaFsV2ZtTR7fzb3-6K7zuu9vXH0AppemWRteQ9SJW7Z1IuIHVxAIrOsp7BDr91kty-i4Ghjhbnh_NMBWcNKIHMwuOzg_br8ZdyL97n9w8jcEZNvfaURyWMRn97xi0YtD5kOy-QPA",
"Honda Vehicles",
"www.honda.com",
"",
"Save up to 40% JVC, Pioneer, Clarion, Alpine, Kenwood.",
"",
"http://ypn-100.overture.com/d/sr/?xargs=k-vWdwvJp78awRZ3Qhx-bNIsqKMyFJwxG5e0pmbgOXJ8w7TZD_t_g3yvErHu8Xl4I3_x8ptDV3A0LIMF7OlPTj5IkMVeVfJ7YImiuZU1-p7rVyQOV4pyMgRkXrvbW_C6knIw1bUvr88gdVHsT-HA77YeNJVDGHkfjWgmNwyqxub9CYdDO8qHDr1b2pamYZbeIy8ZdhbSa1DvmkyROP-YJHS26-bDnYiyF5Aqyi-iI6d3UaXPeWNhzBfWVqBbIRd7wn767p7eqywIgT6oNriUfKJKA9YnOYxl9msgYv75FJF4Yrr5i8pEpPbf8orxrGg5OiPTaHCp4Qw",
"Car Audio/Multimedia SALE",
"carsaudio.co.uk",
"",
"Electronics, Games, Toys, Digital Cameras, and more.",
"",
"http://ypn-100.overture.com/d/sr/?xargs=cc9OZjd2BnLDSEYSO0xFQYOLbyfpbe27TqGUg90i-jKS-gcU8lXMkhuNGMm3DJcx2oriBHlOMItEsUbii_mZooTh1ploLYXV4BBkox4A4Qm4e-UnspwI6DfPw1Y_7s2sDBsD0wcAgFkwVB-VviXebuFDhuCZJseHhCWH6u2I3yH5D1X0OIGIQdhCQIYNL7WVQvzPz69wffyUTyqnItDPX1OFjCZ3Kqnwomn_TFmZeibU291SbvoyNP4mMKIn6VmD3D4rOoid-bTxDP63P19rs3ZAqnClMlJZXOimusbL77w3hK72W7aVtG2VBll-pXPpohzEDJ2nt8A",
"Hundreds of Products at Low Prices",
"www.advancedtechtoys.com",
"");

//zSr is the array that is populated by the javacsript call to Overture
var zSr;
var mapkey;
var mapkey_dummy=[{title: 'placeholder', keywords: 'fitness equipment, fitness, diet program, weight loss program, home gyms, vitamin, exercise bike, nutrition, healthy diet, fitness program'}];

//Below are some example funtions to draw the acutall link boxes
//Each site can use this same pattern to define new functions that can draw the links
//in various ways.

function titleCase(str) {
return str.toLowerCase().replace(/\b\w+\b/g, cnvrt);
function cnvrt() {
if (arguments[arguments.length -2] == 0)
return arguments[0].replace(/^[a-z]/, cnvrt2);
else if (/^(a|about|after|an|and|at|by|for|from|in|into|nor|of|on|onto|over|the|to|up|with|within)$/.test(arguments[0]) )
return arguments[0];
else
return arguments[0].replace(/^[a-z]/, cnvrt2);
}
function cnvrt2() {
return arguments[0].toUpperCase();
}
}

//A function for drawing a set of sponsored links
function showLinks1(linkData, startIndex, endIndex, placement) {
if (startIndex == undefined) {
startIndex = 0;
}
var links = linkData.getItems();
var outString = '<ol class="slat rich sponsored-results">\n';
var arrLength = links.length;
if (endIndex == undefined || endIndex == 0) {
endIndex = arrLength;
}
else {
if (endIndex > arrLength) {
endIndex = arrLength;
}
}

var emptyText = "";
if (endIndex <= startIndex) {
//If there are not links to show, then return an alternative text
return emptyText;
}

var i = startIndex;
var oneKeyword = keywords.split("/")[0];
while ( i <= endIndex && links[i] != null) {
var currItem = links[i];

var ctText = currItem.getTitle().replace(eval('/' + oneKeyword.toUpperCase() + '/g'), '<eM>' + oneKeyword.toUpperCase() + '</eM>');
ctText = ctText.replace(eval('/' + oneKeyword.toLowerCase() + '/g'), '<eM>' + oneKeyword.toLowerCase() + '</eM>');
ctText = ctText.replace(eval('/' + titleCase(oneKeyword) + '/g'), '<eM>' + titleCase(oneKeyword) + '</eM>');

var cdText = currItem.getDescription().replace(eval('/' + oneKeyword.toUpperCase() + '/g'), '<em>' + oneKeyword.toUpperCase() + '</eM>');
cdText = cdText.replace(eval('/' + oneKeyword.toLowerCase() + '/g'), '<eM>' + oneKeyword.toLowerCase() + '</eM>');
cdText = cdText.replace(eval('/' + titleCase(oneKeyword) + '/g'), '<eM>' + titleCase(oneKeyword) + '</eM>');

if (!(currItem == undefined)) {
if (i == endIndex) outString += "<li class=\"last\">\n";
else outString += "<li>\n";
outString += "<p class=\"info\"><strong><a href=\"" + currItem.getClickUrl() + "\" target=\"_blank\">" + ctText + "</a></strong><a href=\"" + currItem.getClickUrl() + "\" target=\"_blank\">" + cdText + "</a></p>";
outString += "<p class=\"meta\"><a href=\"" + currItem.getClickUrl() + "\" target=\"_blank\">" + currItem.getSitehost() + "</a></p>\n";
outString += "</li>\n"
}
i++;
} 
outString += '</ol>\n';
outString += '<p class="spons-footer"><strong>Sponsored Results</strong></p>\n';
return outString;
}

//An example function for drawing a set of content match sponsored links with linksposts
function showCMWithLinkspots(linkData, startIndex, endIndex, linkspotData, rowIndex, maxLinks, version ) {
if (startIndex == undefined) {
startIndex = 0;
}

var links = linkData.getItems();

var olClass = (version == 'charlie') ? 'lst' : 'slat';

var outString = '<ol class="' + olClass + ' rich sponsored-results">\n';
var arrLength = links.length;
if (endIndex == undefined || endIndex == 0) {
endIndex = arrLength;
}
else {
if (endIndex > arrLength) {
endIndex = arrLength;
}
}

var emptyText = "";
if (endIndex <= startIndex) {
//If there are not links to show, then return an alternative text
return emptyText;
}

var i = startIndex;
while ( i <= endIndex) {
var currItem = links[i];

if (!(currItem == undefined)) {
if (i == endIndex) outString += "<li class=\"last\">\n";
else outString += "<li>\n";
outString += "<p class=\"info\"><strong><a href=\"" + currItem.getClickUrl() + "\" target=\"_blank\">" + currItem.getTitle() + "</a></strong><a href=\"" + currItem.getClickUrl() + "\" target=\"_blank\">" + currItem.getDescription() + "</a></p>";
outString += "<p class=\"meta\"><a href=\"" + currItem.getClickUrl() + "\" target=\"_blank\">" + currItem.getSitehost() + "</a></p>\n";
outString += "</li>\n"
}
i++;
} 

/**

outString += "<li class=\"last\">\n";
outString += "<ul class=\"spons-links\">\n";

var isEmpty = false;
var ls_keywords;	
var linkspot = linkspotData.getItem(rowIndex);
if (linkspot == undefined) {
isEmpty = true;
}
else {
ls_keywords = linkspot.getKeywords();
}

var len = ls_keywords.length;

if (len <= 0) {
isEmpty = true;
}

var endIndex;
if (len > maxLinks) {
endIndex = maxLinks;
}
else {
endIndex = len;
}
if (!isEmpty) {
for (var i=0; i < endIndex; i++) {
var keyword = ls_keywords[i];

outString += "<li><a href=\"http://" + window.location.hostname + "/search/?usecm=1&searchterm=" + urlEncode(keyword) + "\">" + keyword + "</a></li>\n";

}	
}
else outString += "";
outString += "</ul>\n";
outString += "</li>\n";

**/ 

outString += '</ol>\n';
outString += '<p class="spons-footer"><strong>Sponsored Links</strong></p>\n';
return outString;
}

//An example function for drawing one row of linkspots. pass in the linkData, index of the row you want
//(we will pretty much always be using index 0 for the first row), and the max number of link
//that we want to show.

function loadOvertureSrchArray() {

var conf = new overtureSearchConf();
conf.setKeywords(keywords);
conf.setOutputCharEnc("latin1");

if (!isEmpty(urlFilters)) {
conf.setUrlFilters(urlFilters);
}
if (!isEmpty(termFilters)) {
conf.setTermFilters(termFilters);
}
// var myUrl = createOvertureSearchLink(conf);
// document.writeln("<P>url = " + myUrl + "<BR>");
loadOvertureSearchInclude(conf);

}

function loadOvertureCMArray() {

var conf = new overtureContentMatchConf();
// conf.setCtxtKeywords(keywords);
conf.setOutputCharEnc("latin1");

if (!isEmpty(ctxtId)) {
conf.setCtxtId(ctxtId);
}
else {
//use the map to get the ctxtId
conf.setCtxtIdMap(g_contextIdMap);
}
if (!isEmpty(type)) {
conf.setType(type);
}
else {

conf.setTypeMap(g_typeIdMap);

}
if (!isEmpty(keywordCharEnc)) {
conf.setKeywordCharEnc(keywordCharEnc);
}
if (!isEmpty(outputCharEnc)) {
conf.setOutputCharEnc(outputCharEnc);
}
if (!isEmpty(maxCount)) {
conf.setMaxCount(maxCount);
}

// var myUrl = createOvertureCMLink(conf);
// document.writeln("<P>url = " + myUrl + "<BR>");
loadOvertureCMInclude(conf);

}

function generateOvertureArray() {
if(zSr) {
overture_links = populateOvertureLinks(zSr);
if (overture_links == undefined) {
//If this in not populated use dummy data
zSr = zSr_dummy;
}
}
else {
//If this in not populated use dummy data
zSr = zSr_dummy;
}
}

function generateOvertureLSArray() {
if(mapkey) {
overtureLinkspots = populateOvertureLinkspots(mapkey);

if (overtureLinkspots == undefined) {
//If this in not populated use dummy data
mapkey = mapkey_dummy;
}
}
else {
//If this in not populated use dummy data
mapkey = mapkey_dummy;
}

}

function printOverture_1() {

var arg = printOverture_1.arguments;
if(arg[0] != undefined) {

document.writeln(showLinks1(arg[0], arg[1], arg[2], arg[3]));
}

}

function printOverture_search() {
var arg = printOverture_search.arguments;
var links = arg[0].getItems();

if (links[arg[1]]!= null){
document.writeln("<div class=\"overture-results\">");
if(arg[0] != undefined) {
document.writeln(showLinks1(arg[0], arg[1], arg[2], arg[3]));
}
document.writeln("</div>");
}
}

function printOverture_search_column() {
var arg = printOverture_search_column.arguments;
var links = arg[0].getItems();

if (links[arg[1]]!= null){
document.writeln("<div class=\"mdl overture-wrap\">");
if(arg[0] != undefined) {
document.writeln(showLinks1(arg[0], arg[1], arg[2], arg[3]));
}
document.writeln("</div>");
}
}

function printOverture_ls() {

var arg = printOverture_ls.arguments;
if(arg[0] != undefined) {
document.writeln(showCMWithLinkspots(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6]));
}

}

//This is a general library for working with Overture Content Match links
//Version: 1.0 
//Last Modified: 6-14-2007
//Author: Michael Stenzler

//This is a Javascript object to encapsulate one row of overture data
var overtureItem = function(pdescription, punused, pclickUrl, ptitle, psitehost, pbidded) {
var description = pdescription;
var unused = punused;
var clickUrl = pclickUrl;
var title = ptitle;
var sitehost = psitehost;
var bidded = pbidded;

this.setDescription = function(ldescription) { description = ldescription; }
this.setUnused = function(lunused) { unused = lunused; }
this.setClickUrl = function(lclickUrl) { clickUrl = lclickUrl; }
this.setTitle = function(ltitle) { title = ltitle; }
this.setSitehost = function(lsitehost) { sitehost = lsitehost; }
this.setBidded = function(lbidded) { bidded = lbidded; }

this.getDescription = function() { return description; }
this.getUnused = function() { return unused; }
this.getClickUrl = function() { return clickUrl; }
this.getTitle = function() { return title; }
this.getSitehost = function() { return sitehost; }
this.getBidded = function() { return bidded; }

this.printItem = function() {
var printString = "<p>description = " + description + "<br>" +
"unused = " + unused + "<br>" +
"clickUrl = " + clickUrl + "<br>" +
"title = " + title + "<br>" +
"sitehost = " + sitehost + "<br>" +
"bidded = " + bidded + "</p>";
document.writeln(printString);
}
}

var overtureLinkSpotItem = function(ptitle, pkeywords) {

var title = ptitle;
var keywords = pkeywords.split(", ");

this.setTitle = function(ltitle) { title = ltitle; }
this.setKeywords = function(lkweywords) { keywords = lkeywords; }
this.setKeywordsFromString = function (lstring) { keywords = lstring.split(", "); }

this.getTitle = function() { return title; }
this.getKeywords = function() { return keywords; }
this.getKeyword = function(i) { return keywords[i]; }

this.printItem = function() {
var printString = "<p>title = " + title + "<br>" +
"keywords = ";
for(i=0; i<keywords.length; i++) {
if(i>0) { printString += ", " }
printString += keywords[i];
}
document.writeln(printString);
}

}

//This is a Javascript object to encapsulate an array of Overture Items 
var overtureLinks = function() {
var listItems = new Array();
this.addItem = function(item) {
var i = listItems.length;
listItems[i] = item;
}

this.getItems = function() { return listItems; }
this.getItem = function(i) { return listItems[i]; }
this.length = function() { return listItems.length; }

this.printItems = function() {
var i = 0;
while (i < listItems.length) {
var currItem = listItems[i];
currItem.printItem();
i++;
}
}

}

//This is a Javascript object to hold configuration data for the creation of an
//Overture Search link
var overtureSearchConf = function() {
var baseUrl = g_baseOvertureSearchUrl;
var partner = g_overtureSearchPartner;
var keywords;
var type;
var keywordCharEnc;
var outputCharEnc;
var urlFilters;
var termFilters;
var serveUrl;
var maxCount;

this.setBaseUrl = function(lbaseUrl) { baseUrl = lbaseUrl; }
this.setPartner = function(lpartner) { partner = lpartner; }
this.setKeywords = function(lkeywords) { keywords = lkeywords; }
this.setType = function(ltype) { type = ltype; }
this.setKeywordCharEnc = function(lkeywordCharEnc) { keywordCharEnc = lkeywordCharEnc; } 
this.setOutputCharEnc = function(loutputCharEnc) { outputCharEnc = loutputCharEnc; } 
this.setUrlFilters = function(lurlFilters) { urlFilters = lurlFilters; }
this.setTermFilters = function(ltermFilters) { termFilters = ltermFilters; }
this.setServeUrl = function(lserveUrl) { serveUrl = lserveUrl; }
this.setMaxCount = function(lmaxCount) { maxCount = lmaxCount; }

this.getBaseUrl = function() { return baseUrl; }
this.getPartner = function() { return partner; }
this.getKeywords = function() { return keywords; }
this.getType = function() { return type; }
this.getKeywordCharEnc = function() { return keywordCharEnc; }
this.getOutputCharEnc = function() { return outputCharEnc; }
this.getUrlFilters = function() { return urlFilters; }
this.getTermFilters = function() { return termFilters; }
this.getServeUrl = function() { return serveUrl; }
this.getMaxCount = function() { return maxCount; }
}

//This is a Javascript object to hold configuration data for the creation of a content
//match link
var overtureContentMatchConf = function() {
var baseUrl = g_baseOvertureCMUrl;
var source = g_overtureCMSource;
var config = g_overtureCMConfig;
var ctxtId;
var ctxtIdMap;
var ctxtUrl;
var ctxtCat;
var ctxtCatMap;
var ctxtKeywords;
var mkt;
var type;
var typeMap;
var keywordCharEnc;
var outputCharEnc;
var maxCount;

this.setBaseUrl = function(lbaseUrl) { baseUrl = lbaseUrl; }
this.setSource = function(lsource) { partner = lsource; }
this.setCtxtId = function(lctxtId) { ctxtId = lctxtId; }
this.setCtxtIdMap = function(lctxtIdMap) { ctxtIdMap = lctxtIdMap; }
this.setCtxtUrl = function(lctxtUrl) { ctxtUrl = lctxtUrl; }
this.setCtxtCat = function(lctxtCat) { ctxtCat = lctxtCat; }
this.setCtxtCatMap = function(lctxtCatMap) { ctxtCatMap = lctxtCatMap; }
this.setCtxtKeywords = function(lctxtKeywords) { ctxtKeywords = lctxtKeywords; }
this.setMkt = function(lmkt) { mkt = lmkt; }
this.setType = function(ltype) { type = ltype; }
this.setTypeMap = function(ltypeMap) { typeMap = ltypeMap; }
this.setKeywordCharEnc = function(lkeywordCharEnc) {keywordCharEnc = lkeywordCharEnc; }
this.setOutputCharEnc = function(loutputCharEnc) { outputCharEnc = loutputCharEnc; }
this.setConfig = function(lconfig) { config = lconfig; }
this.setMaxCount = function(lmaxCount) { maxCount = lmaxCount; }

this.getBaseUrl = function() { return baseUrl; }
this.getSource = function() { return source; }
this.getCtxtId = function() { return ctxtId; }
this.getCtxtIdMap = function() { return ctxtIdMap; }
this.getCtxtUrl = function() { return ctxtUrl; }
this.getCtxtCat = function() { return ctxtCat; }
this.getCtxtCatMap = function() { return ctxtCatMap; }
this.getCtxtKeywords = function() { return ctxtKeywords; }
this.getMkt = function() { return mkt; }
this.getType = function() { return type; }
this.getTypeMap = function() { return typeMap; }
this.getKeywordCharEnc = function() { return keywordCharEnc; }
this.getOutputCharEnc = function() { return outputCharEnc; }
this.getConfig = function() { return config; }
this.getMaxCount = function() { return maxCount; }
} 

//This is a Javascript object to hold configuration data for the creation of an
//Overture Linkspot link
var overtureLinkspotConf = function() {
var baseUrl = g_baseOvertureLinkspotUrl;
var source = g_overtureLinkspotSource;
var config = g_overtureLinkspotConfig;
var linkspotId;
var linkspotIdMap;
var nGrp;
var nKw;

this.setBaseUrl = function(lbaseUrl) { baseUrl = lbaseUrl; }
this.setSource = function(lsource) { source = lsource; }
this.setLinkspotId = function(llinkspotId) { linkspotId = llinkspotId; }
this.setLinkspotIdMap = function(llinkspotIdMap) { linkspotIdMap = llinkspotIdMap; }
this.setConfig = function(lconfig) { config = lconfig; }
this.setNGrp = function(lnGrp) { nGrp = lnGrp; } 
this.setNKw = function(lnKw) { nKw = lnKw; } 

this.getBaseUrl = function() { return baseUrl; }
this.getSource = function() { return source; }
this.getLinkspotId = function() { return linkspotId; }
this.getLinkspotIdMap = function() { return linkspotIdMap; }
this.getConfig = function() { return config; }
this.getNGrp = function() { return nGrp; }
this.getNKw = function() { return nKw; }
}

//This function creates an overtureLinks object from the linkData passed in
//note linkData will normally be the zSr array populated by Overture
function populateOvertureLinks (linkData) {
var i = 0;
var rank = 0;
var ret = new overtureLinks();

while (i < linkData.length) {
rank++;
var description = linkData[i++];
var unused = linkData[i++];
var clickUrl = linkData[i++];
var title = linkData[i++];
var sitehost = linkData[i++];
var bidded = linkData[i++];

//skip the first returned add
if (rank > 1) {
var currItem = new overtureItem(description, unused, clickUrl, title, sitehost, bidded);
ret.addItem(currItem);
}
}

return ret;
}

//This function creates an overtureLinks object from the linkData passed in for linksSpots
//note linkData will normally be the mapKey array populated by Overture
function populateOvertureLinkspots (linkData) {
var ret = new overtureLinks();

for (var i=0; i<linkData.length; i++) {
var currItem = new overtureLinkSpotItem(linkData[i].title, linkData[i].keywords);
ret.addItem(currItem);
}

return ret;
}

//function to show all the links
function showLinkData(linkData, startIndex, endIndex) {
if (startIndex == undefined) {
startIndex = 0;
}
var links = linkData.getItems();
var outString = "<table border=1><tr><th>Description</th><th>Unused</th><th>ClickUrl</th><th>Title</th><th>SiteHost</th><th>Bidded</th></tr>";
var arrLength = links.length;
if (endIndex == undefined || endIndex == 0) {
endIndex = arrLength;
}
else {
if (endIndex > arrLength) {
endIndex = arrLength;
}
}
var i = startIndex;
while ( i < endIndex) {
var currItem = links[i];
outString += "<tr><td>" + checkEmpty(currItem.getDescription()) + "</td><td>" + checkEmpty(currItem.getUnused()) + "</td><td>" + checkEmpty(currItem.getClickUrl()) + "</td><td>" + checkEmpty(currItem.getTitle()) + "</td><td>" + checkEmpty(currItem.getSitehost()) + "</td><td>" + checkEmpty(currItem.getBidded()) + "</td></tr>\n";
i++;
} 

outString += "</table>";
return outString;
}

//returns a nbsp character if a string is empty
function checkEmpty(str) {
if ( (str == undefined) || (str == "")) {
return "&nbsp;";
}
else { 
return str;
}
}

//This function creates a link to fetch the Content Match data from Overture
function createOvertureSearchLink(conf) {
var baseLink = conf.getBaseUrl();
var partner = conf.getPartner();
var keywords = conf.getKeywords();
var type = conf.getType();
var keywordCharEnc = conf.getKeywordCharEnc();
var outputCharEnc= conf.getOutputCharEnc();
var urlFilters = conf.getUrlFilters();
var termFilters = conf.getTermFilters();
var serveUrl = conf.getServeUrl();
var maxCount = conf.getMaxCount(); 

if ( isEmpty(baseLink)) {
baseLink = g_baseOvertureSearchUrl;
}
if ( isEmpty(partner)) {
partner = g_overtureSearchPartner;
}

if ( isEmpty(serveUrl)) {
serveUrl = window.location.href;
}

var retString = baseLink + "?Partner=" + urlEncode(partner);

if ( !isEmpty(keywordCharEnc) ) {
retString += "&keywordCharEnc=" + urlEncode(keywordCharEnc);
}

if ( !isEmpty(outputCharEnc) ) {
retString += "&outputCharEnc=" + urlEncode(outputCharEnc);
}

if ( !isEmpty(keywords) ) {
retString += "&Keywords=" + urlEncode(keywords);
}

if ( !isEmpty(type) ) {
retString += "&type=" + urlEncode(type);
}

if ( !isEmpty(urlFilters)) {
retString += "&urlFilters=" + urlFilters;
}

if ( !isEmpty(termFilters)) {
retString += "&termFilters=" + termFilters;
}

if ( !isEmpty(serveUrl)) {
retString += "&serveUrl=" + urlEncode(serveUrl);
}

if ( !isEmpty(maxCount)) {
retString += "&maxCount=" + urlEncode(maxCount);
}

return retString;
}

//This function creates a link to fetch the Content Match data from Overture
function createOvertureCMLink(conf) {
var baseLink = conf.getBaseUrl();
var source = conf.getSource();
var ctxtUrl = conf.getCtxtUrl();
var ctxtId = conf.getCtxtId();
var ctxtIdMap = conf.getCtxtIdMap();
var ctxtCat = conf.getCtxtCat();
var ctxtCatMap = conf.getCtxtCatMap();
var ctxtKeywords = conf.getCtxtKeywords();
var mkt = conf.getMkt();
var type = conf.getType();
var typeMap = conf.getTypeMap();
var keywordCharEnc = conf.getKeywordCharEnc();
var outputCharEnc= conf.getOutputCharEnc();
var config = conf.getConfig();
var maxCount = conf.getMaxCount();

//cb is cache buster. its a random number to prevent caching
var cb = randomnumber=Math.floor(Math.random()*100000);

if (ctxtUrl == undefined) {
ctxtUrl = "http://" + window.location.hostname + window.location.pathname;
}

if (isEmpty(ctxtId) && !isEmpty(ctxtIdMap)) {
//get the ctxtId from the specified map based on the current uri
ctxtId = getMappingValue(ctxtIdMap);
}

if (isEmpty(ctxtCat) && !isEmpty(ctxtCatMap)) {
//get the ctxtCat from the specified map based on the current uri
ctxtCat = getMappingValue(ctxtCatMap);
}

if (isEmpty(type) && !isEmpty(typeMap)) {
//get the type from the specified map based on the current uri
type = getMappingValue(typeMap);
}

if (isEmpty(baseLink)) {
baseLink = g_baseOvertureCMUrl;
}
if (isEmpty(source)) {
source = g_overtureCMSource;
}

var retString = baseLink + "?source=" + urlEncode(source);

if (!isEmpty(keywordCharEnc)) {
retString += "&keywordCharEnc=" + urlEncode(keywordCharEnc);
}

if ( !isEmpty(outputCharEnc) ) {
retString += "&outputCharEnc=" + urlEncode(outputCharEnc);
}

if ( !isEmpty(ctxtId) ) {
retString += "&ctxtId=" + urlEncode(ctxtId);
}

if ( !isEmpty(ctxtCat) ) {
retString += "&ctxtCat=" + urlEncode(ctxtCat);
}

if ( !isEmpty(ctxtKeywords) ) {
retString += "&ctxtKeywords=" + urlEncode(ctxtKeywords);
}

if (!isEmpty(ctxtUrl)) {
retString += "&ctxtUrl=" + urlEncode(ctxtUrl);
}

if ( !isEmpty(type) ) {
retString += "&type=" + urlEncode(type);
}

if ( !isEmpty(cb) ) {
retString += "&cb=" + urlEncode(cb);
}

if ( !isEmpty(config) ) {
retString += "&config=" + urlEncode(config);
}

if (!isEmpty(maxCount) ) {
retString += "&maxCount=" + urlEncode(maxCount);
}

if (!isEmpty(mkt) ) {
retString += "&mkt=" + urlEncode(mkt);
}

return retString;

}

//This function creates a link to fetch the linkspot data from Overture
function createOvertureLinkspotLink(conf) {
var baseLink = conf.getBaseUrl();
var source = conf.getSource();
var linkspotId = conf.getLinkspotId();
var linkspotIdMap = conf.getLinkspotIdMap();
var config = conf.getConfig();
var nGrp = conf.getNGrp();
var nKw= conf.getNKw();

if ( (baseLink == undefined) || (baseLink == "")) {
baseLink = g_baseOvertureLinkspotUrl;
}
if ( (source == undefined) || (source == "")) {
source = g_overtureLinkspotSource;
}

if (isEmpty(linkspotId) && !isEmpty(linkspotIdMap)) {
//get the linkspotId from the specified map based on the current uri
linkspotId = getMappingValue(linkspotIdMap);
}

var retString = baseLink + "?config=" + urlEncode(config);

if ( !isEmpty(linkspotId) ) {
retString += "&linkspotId=" + urlEncode(linkspotId);
}

if ( !isEmpty(nKw) ) {
retString += "&NKw=" + urlEncode(nKw);
}

if ( !isEmpty(nGrp) ) {
retString += "&NGrp=" + urlEncode(nGrp);
}

if ( !isEmpty(source) ) {
retString += "&source=" + urlEncode(source);
}
return retString;
}

//load in the array
function loadOvertureLinks() {
this.overturLinks;
if(zSr != undefined) {
this.overturLinks = populateOvertureLinks(zSr);
//if (this.overturLinks == undefined) {
// document.writeln("<P>!!! links is undefined !!!<br>");
//}
}
}

//load in the array for linkspots
function loadOvertureLinkspots() {
if(!isEmpty(mapkey)) {
overturLinkspots = populateOvertureLinkspots(mapkey);
//if (this.overturLinkspots == undefined) {
// document.writeln("<P>!!! spot links is undefined !!!<br>");
//}
}
}

//This function gets the first 10 keywords from the page metatag
function getMetaTagKeywords () {
var keywords = "";
//try to get the keywords from the metatags
if (document.getElementsByName) {
var metaArray = document.getElementsByName('keywords');
var maxLength = metaArray.length;
if (maxLength > 10) { maxLength = 10; }
for (var i=0; i<maxLength; i++) {
if (i>0) { keywords += " "; }
keywords += metaArray[i].content; 
}
}
return keywords;
}

//Creates and prints the javascript src= code to include the 
//Content Match links
function loadOvertureCMInclude(conf) {
var url = createOvertureCMLink(conf);
printSrcInclude(url);
}

//Creates and prints the javascript src= code to include the 
//Search links
function loadOvertureSearchInclude(conf) {
var url = createOvertureSearchLink(conf);
printSrcInclude(url)
}

//Creates and prints the javascript src= code to include the 
//Links spots
function loadOvertureLinkspotInclude(conf) {
var url = createOvertureLinkspotLink(conf);
printSrcInclude(url)
}

//Prints the javascript src= code to include the zSr array with the links
function printSrcInclude(url) {
document.write("<script language=\"Javascript\" src=\"" + url + "\"></script>");
}

//Checks to see if a string is empty
function isEmpty(str) {
if ( (str == undefined) || (str == "")) {
return true;
}
else {
return false;
}
}

//HTTP Encode a url
function urlEncode(sStr) {
return escape(sStr)
.replace(/\+/g, '%2B')
.replace(/\"/g,'%22')
.replace(/\//g, '%2F')
.replace(/\'/g, '%27');
}

//This function takes a map (a hashtable) and a string. If the string is omitted the current uri is
//used. Each key of the map is compared to the string and if the key matches the begining of the 
//String (i.e. key /music would match /music/foobar/index.jhtml), then the value for the key is
//returned as the result. Used for mapping ids to sections of the site
function getMappingValue(map, str) {
if (str == undefined) {
//if no match string is given then use the document uri
str = window.location.pathname;
}

for (var key in map) {
var exp = "^" + key;
var re = new RegExp(exp, "i");
if(re.test(str)) {
return map[key];
}
}
return undefined;
}

