//======================================================================
// Global variables
//======================================================================
if (typeof whichMarket == "undefined") var whichMarket = "";	// should be set in client's page or via querystring

var usingQuery = false;
if (typeof usingForm != "boolean") usingForm = true;
if ((location.search.length > 1 && getQstring("iid") == "") || (getQstring("iid") != "" && location.search.split("=").length > 2)) {
	usingForm = false;
	usingQuery = true ;
}

	// if there's not a valid querystring, assume they are using the form to search; 
	// otherwise, it's coming from querystring
var lastaction;	
var headingClicked = false;
var searchClicked = false;
var debug = 0;
var wid = "116,77,75,243".split(",")//table cell widths
// sorttitles: the properties for each column to sort on ( see function idoc for col. properties)
var sorttitles = "title,dateorder,location,description".split(",")
var sortMessage = "&nbsp;<span class='small'>(click column headings to sort)</span>"
var strMkt = (getQstring("m") != "") ? getQstring("m") : whichMarket;	
var section // keeps track of the section you are in (for sorting)
var sortcolumn // keeps track of the column header clicked for sorting a section
var n = 0;  // counter used with the dataset constructor
var rowClass = "odd"
var isMoz = false; // for Netscape 6 compatibility
var NS4 = false;
if (!document.all && !document.layers && document.getElementsByTagName("*")) {
  document.all = document.getElementsByTagName("*")
  isMoz = true
}
if (document.layers) { 
	NS4 = true;
	alert("Your browser is not supported. Please upgrade to a version 5 or later browser.")
 }
var tmpd = new Array();
var docObj;
var origV;
	   
//======================================================================
// getQstring: returns the value of a querystring argument
//======================================================================
function getQstring(v)
{
	var r;
	var s = location.search.split("?") 
	if (s.length <= 1) { return ""; }
	
	var q = s[1].split("&")
	var re = /\(TM\)/gi
	var rePlus = /\+/gi
	for (var i=0;i<q.length;i++)
	{
	    r = q[i].split("=");
		if (unescape(r[0]) == v) { 
			if (document.layers) {
				return unescape(r[1].replace(rePlus,"%20")); 
			} else {
				return unescape(r[1]); 
			}
		}
	}
    return "";
}

//======================================================================
// init: build the  drop down lists and 
// initialize the column sort order flags
//======================================================================
function init(whichPage)
{
	docObj = window

   docObj.document.fHB.Topics.options.length = 2;// for netscape 4
   // build the topic and location dropdowns
   dropdown(docObj.document.fHB.Topics,Topic,"topic")
   dropdown(docObj.document.fHB.Geos,Geo,"geo")
      
   /*
   // reload the search value if the page is being loaded from the form
	// because each type is its own section, we have to keep track
	// of the sections sortorder
	*/
	sortorder = new Array();
	var top = docObj.document.fHB.Topics
	for ( var i=0;i<top.length;i++)
	{ // for each topic initialize the lastsorted value
		sortorder[top[i].value] = new Object();
	   	sortorder[top[i].value].lastsorted = "dateorder";
		sortorder[top[i].value].lastorder = 0;
	}	
	
   if (usingForm || (!usingForm && getQstring("f") == "yes")) {
   		document.getElementById("formTable").style.display = "block";
   }
   if (document.getElementById("formTable").style.display == "block") {
   		document.sf.s.focus();
		//usingForm = true;
   }
  
	if ((!usingForm && location.search != "") || showResults) dosearch("");
}
//======================================================================
// dropdown: list the checkboxes corresponding to the document types
// obj is the select object, ar is array with the value content and qs is
// the querystring value corresponding to the last selected item
//======================================================================
function dropdown(obj,ar,prop)
{
   obj.options.length=0;   
   re = /&#...;/g;
   
   		// check if items are still valid given the particular market set on page
    checkDropdown(obj,ar,prop);
   for (var i=0;i<ar.length;i++)
   {
   	var tempAr = ar[i].replace(re,"®");;
	 ar[i]=ar[i].replace(re,"&#174;");// remove any special &#codes
	 obj.options[obj.options.length] = new Option(tempAr,ar[i]);
	 obj.options[obj.options.length-1].selected = checkSelected(obj,prop);
   }
}

//======================================================================
// checkSelected: select appropriate items based on querystring 
//======================================================================
function checkSelected(obj,prop) {
	if (obj.length == 1 && (usingForm || (prop == "topic" && getQstring("t") == "") || (prop == "geo" && getQstring("l") == ""))) {
		return true;
	} else {
		if (prop == "topic") {
			var m = "t";
		} else if (prop == "geo") {
			var m = "l";
		} else {
			return false;
		}
		
		var qArr = getQstring(m).split("|");
		for (var i=0; i<qArr.length; i++) {
			if (obj.options[obj.options.length-1].value.toLowerCase() == qArr[i].toLowerCase()) {
				return true;
			}
		}
	}
	return false;
}

//======================================================================
// checkDropdown: check that dropdown still has valid items for the 
// particular value of "whichMarket" on the page
//======================================================================
function checkDropdown(obj,ar,prop)
{	
	var validItem = true;
	var tempAr = new Array();
	for (var i=0;i<ar.length;i++) {
		
		if (i>0) {
			validItem = false;
			for (var j=0; j<d.length; j++) {
				if (regMkt(d[j].market,strMkt) && regTest(eval("d["+j+"]."+prop),ar[i])) {
					if (isEventCurrent(d[j].dateEnd)) {	// filter out dates older that are over
						validItem = true;
						break;
					}
				}
			}
		}
		if (validItem) tempAr.push(ar[i]);
	}
		// recreate original array with new values
	if (ar.length != tempAr.length) {
		ar.length = 0;
		for (var i=0; i<tempAr.length; i++) {
			ar[i] = tempAr[i];
		}
	}
}
//======================================================================
// remake the Type array if strMkt is not blank.
// Items go into the Type array if strMkt values are in the data.js and 
// match the original Type values.
//======================================================================
function remakeType()
{
	var tempAr = new Array();
	var validItem = false;
	var k = 0;
	var tmpMkt =  strMkt.split("|");
    /* go through the Type array (so we keep the same order) and see if
	  there is a value mrk entry in data.js.  If so keep the value.
	  NOTE:  both the data.js market value, and the strMkt market value
	  might be piped delimited, so the comparison has to look at the pieces*/
	for (var i=0;i<Type.length;i++)
	{
		for (var j=0; j<d.length; j++) 
		{   // look for a matching market and eventType
		    for (var m in tmpMkt)
			{
				if (regMkt(d[j].market,tmpMkt[m]) && regTest(d[j].eventType,Type[i])) // regTest takes care of the pipes in the market property
				{
					if (isEventCurrent(d[j].dateEnd)) { // filter out events that are over
						validItem = true;
					}
				}
			}
			if (validItem) break;
		}
		if (validItem) { 
			tempAr[k] = Type[i]; 
			k++; 
			validItem = false;
		}
	}
	Type.length = 0;
	Type = tempAr;
}
//======================================================================
// doCheckBoxes: list the checkboxes corresponding to the document types
//======================================================================
function doCheckBoxes()
{
	var cnt;
	remakeType();
	
	if (Type.length == 1) // then there are no checkboxes because there is only one type
	{
	   var strCheckboxes = "<input type='hidden' name='cb' value='0'>";
	}
	else
	{
	   
	   strCheckboxes = "<p class='margin'>";
	   for (i=0; i<Type.length; i++)
	   {
	   		var checkedStatus = testcb(i);
			
			strCheckboxes += "<input type='checkbox' name='cb' value='"+i+"' "+ checkedStatus +">&nbsp;"+Type[i]+"&nbsp;&nbsp;&nbsp;";
			
	   }
	  strCheckboxes += "</p>";	  
	}
	return strCheckboxes;
}
//======================================================================
// testcb: check to see if the check box should be selected
//======================================================================
function testcb(v)
{
	if (usingForm || getQstring("e") == "") return " checked";
	
	var eArr = getQstring("e").split("|");
	for (var i=0; i<eArr.length; i++) {
		if (Type[v].toLowerCase() == eArr[i].toLowerCase()) { 
			return " checked";
		}
	}
	return "";
}
//======================================================================
// idoc: constructor for data table
//======================================================================
function idoc(a,b,c,d,e,f,g,h,i,j,k,l,m){
   this.dateorder = a;
   this.market = trim(b);
   this.eventType=trim(c);
   this.topic=trim(d);
   this.title=trim(e);
   this.URL=trim(f);
   this.dateStart=trim(g);
   this.dateEnd=trim(h);
   this.dateDesc=trim(i);
   this.location=trim(j);
   this.geo =trim(k);
   this.description=trim(l);   
   this.keywords=trim(m);
}
//======================================================================
// dodisplay: search the category select object for selected values
// for each one, find the data records that match and display them with
// the appropriate headers
//======================================================================
function dodisplay()
{	
   dodebug("dodisplay\n")
   lastaction="sections"; // keep track for sorting
   var j = showdocs(); // get the documents
   if (j != "alert") { // if no errors then ...
		 	if (j == "") { j = "No documents returned; try different categories." };
	   		if (document.getElementById) {
			
				document.getElementById("ds").innerHTML = j;
				/*
				var temp = window.open("","temp","")
				temp.document.open()
				temp.document.write("<html><body>"+j + "</body></html>")
				temp.document.close()
				*/
			}
	   		
   }
   document.sf.s.value = "";
   //repositionPage();
   return false;
}
//======================================================================
//  showdocs: build the table showing the matching documents
//  for all of the selected sections.
//======================================================================
function showdocs()
{
	dodebug("showdocs\n"+d.length)
	var j = "";
	var checked = false;	    
	
   // for each topic category selected search the data table
		var pcArr = dropdownItemsSelected("Topics");
		var locationArr = dropdownItemsSelected("Geos");
		//t = eventTypesSelected();
		for ( var i=0;i<docObj.document.fHB.Topics.length;i++)
		{
			   if ( docObj.document.fHB.Topics[i].selected) {	
			   		if (document.getElementById) 
					{ /* each section has to be sorted before displaying
					     if this section is not the section clicked on then we will
						 resort it with its last sort order using the sortorder array */
					  copytotmp(docObj.document.fHB.Topics[i].value);
					  if (headingClicked) {// only do this when clicking column heads
					   sortit(sortorder[docObj.document.fHB.Topics[i].value].lastsorted, i);
					  }
					}
			        j = j + doSection(i,docObj.document.fHB.Topics[i].value,locationArr.join("|")); 
					checked=true;
			   }
		}	

	    if (!checked && trim(getQstring("order")) == "") { 
	  	   	alert("Please check one of the check boxes.");
	  		return "alert";
	  	}

	return j;
}	
/*======================================================================
copytotmp:  copies matching topics to a temp array for sorting and display
========================================================================*/
function copytotmp(t)
{

   var cnt = 0;
   tmpd.length=0;
   for (var i=0; i<d.length; i++)
   {
       if (d[i]["topic"].indexOf(t) != -1 || // topic matchs section
	        				t  == "All"   || // section = all
							d[i]["topic"] == "All") // or this topic should be in all sections
	   {
		 tmpd[cnt] = d[i];
		 cnt = cnt + 1
	   }
   }
}
//======================================================================
// eventTypesSelected: concatenates event types
//======================================================================

function eventTypesSelected() {
		var tArr = new Array();
		if (Type.length == 1 ) // only one Type, so no checkboxes
		{
		   return(0);
		} 
		else 
		{
			for ( var i=0;i<docObj.document.fHB.cb.length;i++)
			{
				   // alert(docObj.document.fHB.cb[i].value)
				   if ( docObj.document.fHB.cb[i].checked) {	
				   		tArr.push(docObj.document.fHB.cb[i].value)
				   }
			}	
		}
		return tArr.join("|");
}

//======================================================================
// dropdownItemsSelected: builds array of category selections
//======================================================================

function dropdownItemsSelected(obj) {
	var obj = docObj.document.fHB[obj];
	
	var pcArr = new Array();
	for ( var i=0;i<obj.length;i++)
	{
		   if ( obj.options[i].selected) {	
		   		
		   		pcArr.push(obj.options[i].value)
				if (i == 0) {
					// "All" is selected so deselect everything else
					obj.selectedIndex = 0;
					break;
				}
		   }
	}	
	return pcArr;
}

//======================================================================
// selectAll: selects all items in a dropdown
//======================================================================

function selectAll(obj) {
	var obj = docObj.document.fHB[obj];
	for ( var i=1;i<obj.length;i++)
	{
		obj.options[i].selected = true;
	}
	obj.options[0].selected = true;
}

//======================================================================
// doSection:  displays a section of matching docs (ie. white papers)
//======================================================================
function doSection(t,a, m)
{	/*
	t is the event checkboxes piped delimited
	a is the section name (string value)
	m is the selected locations piped delimited
	*/
	dodebug("doSection: " + t + "..." + a + "..." + m)
	if (a == "All") a = "";
	if (m == "All") m = "";
	var j = "";//"<table width='500' bgcolor='#FFFFFF' border='0' cellspacing='0' cellpadding='0'>\n";
	var r = getdocs(tmpd,t,a,m);
	if (a == "") a = "All Topics";
	if (r != "") { j = j + doheader("0",t,a) + r; }
	else { 
		if (a == "All Topics") {
			var strMessage = "No results found."
		} else {
			var strMessage = "No results for &quot;" + a + "&quot;";
		}
		j = j + doheader("0",t,a) + "<tr><td colspan='5' bgcolor='#FFFFFF'><p class='noResults'>" + strMessage + "</p></td></tr>\n" 
	}

	j = j + "</table>\n</td></tr>\n</table>\n<br>\n";
	return j;
}
//======================================================================
// getdocs: loop through all the documents and return the ones that match 
// the requested section
//======================================================================
function getdocs(ar,t,a,m)
{	
	rowClass="odd";
	var j = "";
	var eventTypeArr = new Array();
	a = unescape(a)
	m = unescape(m)
	var cnt = 0;	
	
	for (var i=0;i<ar.length;i++)
	{  
	    if (eventTypesSelected() != 0) 
		{	eventTypeArr = eventTypesSelected().split("|"); } else {	eventTypeArr[0]="0"; }
		for (var k = 0; k<eventTypeArr.length; k++) {
			// alert("testing " + d[i].eventType + " and " + Type[eventTypeArr[k]])
			if (regMkt(ar[i].market,strMkt)) {//and
				//alert("Row " + i + " ("+d[i].market+") matches " +whichMarket)
			if (regTest(ar[i].eventType,Type[eventTypeArr[k]])){//and 
				//alert("matched " + d[i].eventType + " and " + Type[eventTypeArr[k]])
			if (regTest(ar[i].topic,"All") || regTest(ar[i].topic,a)){//and 
				
			if (regTest(ar[i].geo,m) ){
				//alert("a: " + a + "...m: " + m);
				if (isEventCurrent(ar[i].dateEnd)) {
					tmpd[cnt] = ar[i]; 	
					j = j + buildrow(cnt++,"0");
				}
			}}}}
		}
	}
	return j;
}

//======================================================================
// isEventCurrent: tests to see if event is over or not
// Allows it to remain up to 3 days past expiration
//======================================================================

function isEventCurrent(date) {
	if (date == "") {
		return true;
	} else {
		var today = new Date();
		today.setHours(0);	
		today.setMinutes(0);
		today.setSeconds(0);
		var eventEnd = new Date(date)
		eventEnd.setHours(72) // set time to be 3 days later to allow dates to appear for 3 days after they expire
		if (Date.parse(eventEnd) < Date.parse(today)) { 
			return false;
		} else {
			return true;
		}
	}
}

//======================================================================
// regTest: use a regular expression to test for a string in a multi-field datastring
// (expects the fields to be comma delimited)
//======================================================================
function regTest(a,b)
{	
	var re = /&#174;|&#153;|\(TM\)/gi
	a = a.replace(re,"")
	b = b.replace(re,"")

   eval('re3 = new RegExp("[,|]*\\s*?'+b+'\\s*?[,|]*","gi")');
   return re3.test(a);
}

//======================================================================
// regMkt: splits market values on pipes and tests for a match
//======================================================================
function regMkt(a,b)
{	
	if (b == "") return true; // if no market is set, match all
	
	var isMatch = false;
	var aArr = a.split("|");
	var bArr = b.split("|");

	for (var i=0; i<aArr.length; i++) {
		for (var j=0; j<bArr.length; j++) {
			if (aArr[i].toLowerCase() == bArr[j].toLowerCase()) {
				isMatch = true;
				break;
			}
		}
		if (isMatch) break;
	}
	return isMatch;
}


//======================================================================
//  buildrow: Build a row of data.  If f is 0 then skip the column showing the section
//  if f is 1 then show the section and type columns
//======================================================================
function buildrow(i,f)
{
   if (rowClass == "odd") { rowClass="even"; } else { rowClass="odd"; }
   var j = "<tr valign='top' bgcolor='#FFFFFF'>";

   if (NS4) cellSpacer = "<br>";
   else cellSpacer = "";
  
		
		j = j + "<td class='smtext'><p>"+doTitle(i)+"</p></td>";
		j = j + "<td class='smtext'><p>"+doDate(i)+"</p></td>";
		j = j + "<td class='smtext'><p>"+addPara(tmpd[i].location)+"</p></td>";
		j = j + "<td class='smtext'><p>"+doDesc(tmpd[i].description)+"</p></td>";
		j = j + "</tr>\n";
	
	return j;
}  
//======================================================================
// doDate: converts date format to friendly format and concatentates
// start date with end date. If start date is blank, it uses the content
// from dateDesc
//======================================================================
function doDate(i) {
	if (tmpd[i].dateStart != "") {
		var arMonth = new Array("January ","February ","March ","April ","May ","June ","July ","August ","September ", "October ","November ","December ")
		
		var dateSplit = tmpd[i].dateStart.split("/");
		var strStartMonth = arMonth[dateSplit[0]-1];
		var strStartDay = dateSplit[1];
		var strStartYear = dateSplit[2];
		
		dateSplit = tmpd[i].dateEnd.split("/");
		var strEndMonth = arMonth[dateSplit[0]-1];
		var strEndDay = dateSplit[1];
		var strEndYear = dateSplit[2];
		
		if (strStartYear == strEndYear) {  // same year event
			if (strStartMonth == strEndMonth) { // same month event
				if (strStartDay == strEndDay) { // same day event
					var strDate = strStartMonth + strStartDay + ", " + strStartYear;
				} else { // different day in same month
					var strDate = strStartMonth + strStartDay + "-" + strEndDay + ", " + strStartYear;
				}
			} else {  // different month
				var strDate = strStartMonth + strStartDay + "-" + strEndMonth + strEndDay + ", " + strStartYear;
			}
		} else { // completely different dates
			var strDate = strStartMonth + strStartDay + ", " + strStartYear + "-" + strEndMonth + strEndDay + ", " + strEndYear;
		}
		tmpd[i].keywords = tmpd[i].keywords + " " + strDate;
		return strDate;
	} else {
		return parseHTML(addPara(tmpd[i].dateDesc));
	}
}
//======================================================================
// getData: makes sure that commas have spaces following them so cells
// can break long lines, and also returns a nonbreaking space for netscape
// if the cell is empty
//======================================================================
function getData(a)
{
   var re = new RegExp(",([A-Za-z0-9])","g")
   a = a.replace(re,", $1");
   var re = /174; 4/gi
   a = a.replace(re,"174;&nbsp;4");
   if (a == "") { return "&nbsp;"; } else {return a; }
}
//======================================================================
// doTitle: adds correct paragraph marks and graphics for register or archive
//======================================================================
function doTitle(i) {
   	a = tmpd[i].title;
	a = addTitleLink(a,i);
	a = addPara(a);
   	return a;
}

//======================================================================
// vSpacer: adds vertical spacing
//======================================================================
function vSpacer(num) {
	return "<img src='/design/shared/pix/spacer.gif' width='1' height='"+ num +"' border='0'><br>";
}

//======================================================================
// addTitleLink: adds URL around title
//======================================================================
function addTitleLink(a,index) {
	if (tmpd[index].URL != "") {
		var strLink;
		var tempTitleArr = new Array();
		var titleArr = a.split("|");
		for (var i=0; i<titleArr.length; i++) {
			strLink = "<a href='"+ tmpd[index].URL +"' target='_blank'>" + titleArr[i] + "</a>";
			tempTitleArr.push(strLink);
		}
	
	return tempTitleArr.join("|");
	} else {
		return a;
	}
}

//======================================================================
// doDesc: replaces pipes with paragraph marks
//======================================================================
function doDesc(a) {
	a = addParaBullet(a);
	a = parseHTML(a);
	a = addPara(a);
	a = replaceSpan(a);
   	return a;
}

//======================================================================
// addPara: replaces pipes with paragraph marks
//======================================================================
function addPara(a) {
	a = parseHTML(a);
   	var re = /\|/gi
   	a = a.replace(re,"</p>\n<p>");
   	return a;
}

//======================================================================
// replaceSpan: replaces span with with paragraph marks
//======================================================================
function replaceSpan(a) {
   	var re = /<\/span><\/p>\n<p>/gi
   	a = a.replace(re,"<br></span>\n");
   	return a;
}

//======================================================================
// parseHTML: replaces fake tags with real ones
//======================================================================
function parseHTML(a) {
   	var re = /<blue>/gi
   	a = a.replace(re,"<span class='eventBlue'>");
	re = /<\/blue>/gi
   	a = a.replace(re,"</span>");
	
	var re = /<link=*(.*?)>(.*?)<\/link>/i
	if (re.test(a)) {
		var matchArray = re.exec(a);
		var linkURL = matchArray[1];
		var linkText = matchArray[2];		
		
		if (linkURL == "") linkURL = linkText;
		if (linkURL.indexOf("http") == -1) linkURL = "http://" + linkURL;
		a = a.replace(re,"<a href='"+linkURL+"' target='_blank'>"+linkText+"</a>");	
	}
   	return a;
}

//======================================================================
// fixSlash: put spaces around slash so lines break if needed
//======================================================================
function fixSlash(a,character)
{
   var re = new RegExp("\\/","g")
   a = a.replace(re,"\/"+character);
   if (a == "") { return "&nbsp;"; } else {return a; }
}

//======================================================================
// addParaBullet: looks for bulleted items in paragraphs
//======================================================================
function addParaBullet(a) {
	var tempArr = a.split("|");
	for (var i=0; i<tempArr.length; i++) {
		if (tempArr[i].charAt(0) == "-") {	// bullet should go here
			var re = /^- (.*)$/gi
			tempArr[i] = tempArr[i].replace(re,addBullet("$1",1));
		}
	}
	a = tempArr.join("|")
   	return a;
}

//======================================================================
// addBullet: adds bullets in front of each application and technology
//======================================================================
function addBullet(a,isPara)
{
	if (a == "&nbsp;" || a == "") return a;

	var tableBegin = "<table border='0' cellpadding='0' cellspacing='0'>";
	var tableEnd = "</table>\n";
	if (isMoz) spacer = "<img src='/design/shared/pix/spacer.gif' width='3' height='3' alt=''><br>"
	
	if (isPara) {
		var strBulletBegin = "<tr valign='top'><td class='small'><table border='0' cellpadding='0' cellspacing='0' width='13'><tr><td colspan='3' width='13' height='4'></td></tr>\n<tr><td width='7' height='3'></td><td bgcolor='#a6a6a6' width='3' height='3'></td><td width='3' height='3'></td></tr>\n</table>\n</td><td class='small'>";
		var strBulletEnd = "<br></td></tr><tr>\n";
	} else {
		var strBulletBegin = "<tr valign='top'><td class='small'><table border='0' cellpadding='0' cellspacing='0' width='6'><tr><td colspan='2' width='6' height='4'></td></tr>\n<tr><td bgcolor='#a6a6a6' width='3' height='3'></td><td width='3' height='3'></td></tr>\n</table>\n</td><td class='small'>";
		var strBulletEnd = "<br></td></tr><tr><td height='7' colspan='2'></td></tr>\n";
	}
	
	var strContent = "";
	var strTemp = a.split("|");
	for (i=0; i<strTemp.length; i++) {
		strContent = strContent + strBulletBegin + trim(strTemp[i]) + strBulletEnd;
	}
   return tableBegin + strContent + tableEnd;
}
//======================================================================
// findUrl: adds hyperlinks to solutions by referencing array in data.js
//======================================================================
function findUrl(a)
{
	if (a == "&nbsp;") return a;
	var strUrls = "";
	var navArrow = (!NS4) ? "class='navArrow' " : "" 
	var strTemp = a.split(",");
	for (i=0; i<strTemp.length; i++) {
		if (typeof Solutions[trim(strTemp[i])] == "undefined") {
			strUrls = trim(strTemp[i]);
		} else {
		strUrls = strUrls + "<a href='"+Solutions[trim(strTemp[i])]+"' target='_blank' class='solutionUrl'><img src='/design/network/solutions/pix/navarrow_blue_sm.gif' " + navArrow + "width='10' height='11' alt='Learn More' border='0'>&nbsp;" + trim(strTemp[i]) +"</a><br>";
		}
	}
  
   return strUrls;
}

//======================================================================
// doheader: displays the table headers with sorting 
// f is a flag indicating whether or not this is a search listing
// t is the section being displayed
//======================================================================
function doheader(f,t,a)
{	
	dodebug("doheader:\nf:" + f + "\nt:" + t + "\na:" + a)
	var headerType;
   	if (a.indexOf("search|") == 0) {
		// coming from search function
		if (!usingForm) {
			if (getQstring("h") != "") {
				headerType = getQstring("h");
			} else if (typeof tableHeading != "undefined" && tableHeading != "") {
				headerType = tableHeading;
			} else {
				headerType = "List Results";
			}
		} else if (!searchClicked && getQstring("h") != "") {
			headerType = getQstring("h");
		} else if (!searchClicked && typeof tableHeading != "undefined" && tableHeading != "") {
			headerType = tableHeading;
		} else {
			headerType = "Search results for &quot;" + a.split("|")[1] + "&quot;";
		}
		
   	} else if (a.indexOf("show|") == 0) {
		// coming from search function
		headerType = a.split("|")[1] + " Listings";
   	} else {
   		headerType = a;
   	}
    var j = "\n<!-- Start of header tabs -->\n"
   		
		j = j + "<table border='0' cellspacing='0' cellpadding='0' width='564'><tr valign='bottom'><td><p class='headBlue'>"+ headerType + "<br><img src='/design/shared/pix/spacer.gif' width='1' height='3' alt=''></p></td><td align='right'>" + sortMessage + "<br><img src='/design/shared/pix/spacer.gif' width='1' height='4' alt=''></td></tr></table>";

	 j = j + "<table border='0' cellspacing='0' cellpadding='0'><tr><td bgcolor='#a6a6a6'>"
	 j = j + "<table border='0' cellspacing='1' cellpadding='0' width='564'>"
	  	   
		var strTitle = (1) ? "<a href='#' class='columnHead' onClick='return dosort(\""+sorttitles[0]+"\","+t+")'>Title</a>" : "Title"; 
		var strDate = (1) ? "<a href='#' class='columnHead' onClick='return dosort(\""+sorttitles[1]+"\","+t+")'>Date</a>" : "Date"
 		var strLocation = (1) ? "<a href='#' class='columnHead' onClick='return dosort(\""+sorttitles[2]+"\","+t+")'>Location</a>" : "Location"
  		var strDesc = (1) ? "<a href='#' class='columnHead' onClick='return dosort(\""+sorttitles[3]+"\","+t+")'>Description</a>" : "Description"

		j = j + "<tr bgcolor='#f0f0f0'>";
		j = j + "<td align='center' valign='bottom'><p class='smcopy'><img src='/design/shared/pix/spacer.gif' width='"+wid[0]*1+"' height='1' alt=''><br><b>"+strTitle+"</b></p></td>";
		j = j + "<td align='center' valign='bottom'><p class='smcopy'><img src='/design/shared/pix/spacer.gif' width='"+wid[1]*1+"' height='1' alt=''><br><b>"+strDate+"</b></p></td>";
		j = j + "<td align='center' valign='bottom'><p class='smcopy'><img src='/design/shared/pix/spacer.gif' width='"+wid[2]*1+"' height='1' alt=''><br><b>"+strLocation+"</b></p></td>";
		j = j + "<td align='center' valign='bottom'><p class='smcopy'><img src='/design/shared/pix/spacer.gif' width='"+wid[3]*1+"' height='1' alt=''><br><b>"+strDesc+"</b></p></td>";
		j = j + "</tr>\n";

		j = j + "<!-- end of header tabs -->\n"		

   return j;
}
//======================================================================
// doinline: print inline links to each section
//======================================================================
function doinline(f,t)
{
   var l = "<a name='Section"+t+"'></a>" // links 
   var c = 0// count of number of check boxes checked

   if (Type.length > 1)
   {
		for (i=0;i<docObj.document.fHB.cb.length;i++)
		{	
				if (f==1 && (parseInt(t) != i) || (NS4 && getQstring("s") != "" && (parseInt(t) != i))) {	
					// coming from search button
			   		
					c = c + 1;
			   } else if ( docObj.document.fHB.cb[i].checked && (parseInt(t) != i)) {	
			   		// coming from show button
			   		
					c = c + 1;
			   } 
		}
   }
  
   if (c > 0) { return "&nbsp;&nbsp;&nbsp;"+l;}
   return l;
}

//======================================================================
// sortcolumn: print different header if this column in the one sorted
// a is the  property to sort on
// b is the index in the sortorder array
// c is the text to display as the column head
// k is the offset on the width for the cells
//======================================================================
function sortcolumn(a,b,c,k)
{
  dodebug("sortcolumn\nprop:"+a+"\nindex to sortorder array:"+b+"\ncol head:"+c+"\n")
  if (a == sortorder[b].lastsorted)
  {
    return "<td align='center' class='head' width='"+wid[k+a]+"'><a class='head' href='#' onclick='return dosort("+'"'+a+'",'+b+")'>["+c+"]</a></td>\n" ; 
  }
  else
  {
    return "<td align='center' class='head' width='"+wid[k+a]+"'><a class='head' href='#' onclick='return dosort("+'"'+a+'",'+b+")'>"+c+"</a></td>\n" ;
  }
}
//======================================================================
// doNSsearch: searches
//======================================================================
function doNSsearch(form)
{	
	xStr = "left=" + eval(screen.width - 655) + ",top=0,width=622,height=540,scrollbars=yes,resizable=no"
	
	docWin = window.open("","docWin",xStr)	
	//} 
	form.action = "popup.htm";
	form.target = "docWin";
	docWin.focus();
	//docWin.reload();
	return;
}
//======================================================================
// stripChar: strips out special characters from data.js to compare
//======================================================================
function stripChar(str)
{	
	var re = /&#174;|&#153;|\*|\(TM\)/gi
	str = str.replace(re,"")
	var re = /&nbsp;/gi
	str = str.replace(re," ")
	return escape(str);
}
//======================================================================
// dosearch: searches the table for v
//======================================================================
function dosearch(v)
{	
	dodebug("dosearch\n"+v)
	var v = (getQstring("s") != "" && !usingForm) ? getQstring("s") : v;
	var localtmpd = new Array();
	
	var re = new RegExp('"',"gi")
	origV = v.replace(re,"");
	
	if (usingForm) {
		if (Type.length > 1) { // reset checkbox defaults
			for (var i=0; i<Type.length; i++) {
				docObj.document.fHB.cb[i].checked = true;
			}
		}
		
		if (document.getElementById) {	// reset dropdown defaults
			docObj.document.fHB.Topics.options.selectedIndex = 0; // to move scrollbar up
			docObj.document.fHB.Geos.options.selectedIndex = 0; // to move scrollbar up
		}
	}
	rowClass="odd";
	var t = "";
	if (trim(v) == "") { v = "*"; }
	var specialChars = '^\\+,®,\\*,©,™,"';
	var specials = specialChars.split(",");
	var repl = ",,.*,&copy;,,".split(",");
	for (var i=0;i<specials.length;i++)
	{
	   var re = new RegExp(specials[i],"g");
	   v = v.replace(re,repl[i]);
	}
	
	//alert(v)
	lastaction="search";
	var j = ""
	var cnt = 0;
	re = new RegExp(escape(v),"i");

		// take market value from querystring first; if empty, take from variable set by client

			tmpd.length=0;
			for (var i=0;i<d.length;i++)
			{	
							
				if (    (usingForm && arguments.length == 2 && re.test(stripChar(d[i].eventType)))      ||
				
						(usingForm && arguments.length == 1 && regMkt(d[i].market,strMkt) && (re.test(stripChar(d[i].eventType)) || re.test(stripChar(d[i].topic)) || re.test(stripChar(d[i].title)) || re.test(stripChar(d[i].URL))  || re.test(stripChar(d[i].dateStart))  || re.test(stripChar(d[i].dateEnd))  || re.test(stripChar(d[i].dateDesc)) || re.test(stripChar(d[i].location)) || re.test(stripChar(d[i].geo))  || re.test(stripChar(d[i].description)) ||  re.test(stripChar(d[i].keywords))))     ||
							
						(!usingForm && !usingQuery && showResults) ||		

						(!usingForm && (  
											(d[i].topic.toLowerCase() == "all" ||  regTest(d[i].topic,getQstring("t")))  && regTest(d[i].geo,getQstring("l")) && regTest(d[i].eventType,getQstring("e")) &&  regTest(d[i].market,getQstring("m")) && (getQstring("t")!= "" || getQstring("l") != "" || getQstring("e") != "" || getQstring("m") != "")  ||

						(getQstring("k") != "" && regTest(d[i].keywords,getQstring("k")))    ||
						
						(getQstring("s") != "" && (re.test(stripChar(d[i].eventType)) || re.test(stripChar(d[i].topic)) || re.test(stripChar(d[i].title)) || re.test(stripChar(d[i].dateStart)) || re.test(stripChar(d[i].dateEnd)) || re.test(stripChar(d[i].dateDesc))  || re.test(stripChar(d[i].location)) || re.test(stripChar(d[i].geo))  || re.test(stripChar(d[i].description)) ||  re.test(stripChar(d[i].keywords))))  
						
											  
										)    
						)   
					)  
				{ 
				// found one, so copy it to the tmpd array
						
						localtmpd[cnt] = d[i];
						cnt++;
				}
			}
			
			var j =  getdocs(localtmpd,-1,"","");
			outputtmpd(1); // output the tmpd array
	
	return false;
}
//======================================================================
// repositionPage: to make sure IE users can see that results have changed
// below the fold
//======================================================================

function repositionPage() {
	if (document.URL.indexOf("popup.htm") > -1) return;
	if (document.all && document.body.scrollTop <= 167 && (document.body.clientHeight - document.body.scrollTop) < 555) {
		document.body.scrollTop = 167;
	}
}
	
//======================================================================
// dosort: set the sorted flag and then redisplay the output
// v is the column being sorted, t is the index into the type table of the
// section
//======================================================================

function dosort(v,t)
{   
	headingClicked = true;
	if (t == -1) return false;
	var tmp=docObj.document.fHB.Topics.options[t].value;
	
	dodebug("dosort\ncolumn to sort:"+v+"\nt:"+tmp+"\nlastsorted:"+sortorder[tmp].lastsorted)
   
	if (sortorder[tmp].lastsorted != v) {
		// sorting should always be ascending for new column sorts
		sortorder[tmp].lastorder = 1
	} 
	sortorder[tmp].lastsorted = v;
	section = tmp;  // set the section to be what section was clicked

	if (lastaction == "search") 
	{ 	
		sortit(sortorder[tmp].lastsorted,t); 
		outputtmpd(1);
	}
	else 
	{  
		if (checkit(docObj.document.fHB)) {
			dodisplay(tmp,docObj.document.fHB.Geos.options[docObj.document.fHB.Geos.selectedIndex].value) 	
		}
		
		
    }
	return false;
}
//======================================================================
// outputtmpd - output the tmpd array, called after searching
//======================================================================
function outputtmpd()
{
		t = ""; var j = "";
		for (var cnt=0;cnt<tmpd.length;cnt++)
		{
			t = t + buildrow(cnt,"1");
		}

		if (t == "") { j = j+doheader("1", -1,"search|"+origV)+"<tr><td colspan='5' bgcolor='#FFFFFF'><p class='noResults'>No results found.</p></td></tr>\n";}
		else 
		{ 
				if (arguments.length == 1) {
					j = j + doheader("1",0,"search|"+origV); 
				} else if (arguments.length == 2) {
					j = j + doheader("1",0,"show|"+origV); 
				}
				j = j + t; 
		 }
			
		j = j + "</table>\n</td></tr>\n</table><br>\n\n";
	
	
		if (document.getElementById) 
		{ 
			document.getElementById('ds').innerHTML = j;
		/*
		var temp = window.open("","temp","")
		temp.document.open()
		temp.document.write("<html><body>"+j + "</body></html>")
		temp.document.close()
		*/
		}
		else { document.write(j); 
		}
}

//======================================================================
// sortit: do the actual sorting and set the order flag.
// v is the column being sorted, t is the index into the type table of the
// section
//======================================================================
function sortit(v,t)
{	

	var top = docObj.document.fHB.Topics

	// ok, if this section is the one that was clicked then toggle the order flag and sort
	if (top[t].value == section) 
	{
		if (sortorder[top[t].value].lastorder == 1) 
		{ 	
			BubbleSort(tmpd,v,0); 
			sortorder[top[t].value].lastorder = 0;
			sortorder[top[t].value].lastsorted = v;
		} 
		else 
		{  
			BubbleSort(tmpd,v,1); 
			sortorder[top[t].value].lastorder = 1;
			sortorder[top[t].value].lastsorted = v;
		} 
	}
	else // just sort as it was
    {
		BubbleSort(tmpd,sortorder[top[t].value].lastsorted,sortorder[top[t].value].lastorder)
	}
}
//======================================================================
// bubblesort: The bubble sort has three arguments, 
//		the array of objects to sort, 
//		the property to sort on
//		and the order to sort: 1 to compare with a >, else with a <
//====================================================================== 
function BubbleSort(objArray,prop,order) 
{	
   for (var i=0; i<objArray.length-1; i++)
        for (var j=i+1; j<objArray.length; j++)
		    if (order == 1)
			{
				if (typeof objArray[i][prop]  == "number") 
				{
					if (objArray[j][prop] > objArray[i][prop]) 
					{	 	doswitch(objArray,i,j) 		}
				}
				else 
				{	
					if (objArray[j][prop].toUpperCase() > objArray[i][prop].toUpperCase()) 
					{	 	doswitch(objArray,i,j) 		}
				}
			}
			else
			{
				if (typeof objArray[i][prop]  == "number") 
				{
					if (objArray[j][prop] < objArray[i][prop]) 
					{	 	doswitch(objArray,i,j) 		}
				}
 				else 
				{
					if (objArray[j][prop].toUpperCase() < objArray[i][prop].toUpperCase()) 
					{	 	doswitch(objArray,i,j) 		}
				}
			}
}
	
//======================================================================
// doswitch: used by bubblesort to switch two items
//======================================================================
function doswitch(objArray,i,j)	
{
		var dummy = objArray[i];
	  	objArray[i] = objArray[j];
	  	objArray[j] = dummy;
}
//======================================================================
// vbscript like text functions 
//======================================================================
function trim(str) { 
	str = str + "";
	return rtrim(ltrim(str))
}
function ltrim(str) { 
	str = str + "";
	while (str.substring(0,1)==" ") str = str.substring(1);
	return str;
}
function rtrim(str){ 
	str = str + "";
	while (str.substring(str.length-1)==" ") str = str.substring(0,str.length-1);
	return str;
}
//======================================================================
// checkit: make sure the select boxes and check boxes are set before submitting the form
//======================================================================

function checkit(form)
{	
	if (Type.length > 1)
	{
	  	var ch = false
	 	for (i=0;i<docObj.document.fHB.cb.length;i++)
		{
	   		if ( docObj.document.fHB.cb[i].checked) {ch=true;	}
		}	
		if (!ch) { alert("Please check one of the check boxes."); return false; }
	}
	
	if (dropdownItemsSelected("Topics").length == 0 || dropdownItemsSelected("Geos").length == 0 )
	{
	  alert("Please select from both drop down lists."); return false;
	}
	return true;
}

//======================================================================
// doform: check form and then submit
//======================================================================
var docWin;
function doform(form)
{	
	if (document.getElementById) 
	{ 
		headingClicked = false;
	   var tmp = docObj.document.fHB;
	   return dodisplay();
	} 
}

//======================================================================
// checkReload
//======================================================================
function checkReload() {
	if (docObj.name=='docWin' && document.referrer != '' && document.layers) {
		location.reload();
	}
}

//======================================================================
// showProps
//======================================================================
function showProps(obj,objName) {
   var result = ""
   var count = 0
   for (var i in obj) {
      result += objName + "." + i + " = " + obj[i] + "\n"
      count++
      if (count == 25) {
         alert(result)
         result = ""
         count = 0
      }
   }
   if (count != 0) alert(result)
}
//======================================================================
// dodebug
//======================================================================
function dodebug(s)
{
   if (debug == 1) { if (confirm(s)) { return; } else {debug = 0; }}
}
