var myCompactArray = new Array();
var myCompactLinksArray = new Array();
var divObj;
var curTitle;
var myCompactArray;
var myCompactLinksArray;
var curSel;
var userLink;
var ebay=false;

var webRoot = "http://www.terapeak.com/";

function compact_d(dontWait) { // display
	
	//  The 'dontWait' variable is there because before when the scripts we're calling it wasn't with ajax
	//  so we were getting errors where elements in the array weren't defined.  Basically it's there so
	//  older legacy code will work.
	if (typeof dontWait != 'undefined' ) {
  	divHTML = "<table width=130 cellpadding=0 cellspacing=0 border=0>";
  	divHTML = divHTML + "<tr><td>" + compact_sl() + "</td></tr>";
  	divHTML = divHTML + "<tr><td><font style='font-family: arial; font-size: 8pt;'><b>" + curTitle + "</b></font></td></tr>";
  	for(i = 0; i < myCompactArray.length; i++) {
  		var tmp = myCompactArray[i];
  		if(tmp.length > 17) { tmp = tmp.substr(0,15) + ".."; }
  		link = myCompactLinksArray[i];
  		if(ebay==true) {
  			link = "http://www.qksrv.net/click-1569476-5463217?loc=http://search.ebay.com/" + link;
  		} else {
  			link = "http://www.terapeak.com/click.php?pageType=search&dosearch=1110480879&query=" + link + "&act=" + userLink;
  		}
  		divHTML = divHTML + "<tr><td><font style='font-face: arial; font-size: 8pt;'>" + (i+1) + ". <a href='" + link + "' target='_blank' title='"+ myCompactArray[i] +"'>" + tmp + "</a></font></td></tr>";
  	}
  	divHTML = divHTML + "<tr><td><font style='font-family: arial; font-size: 8pt;'>Powered by <b><i><font style='font-family: arial; color:#6B6B73;font-size:10pt;'>tera</font><font style='font-family: arial; color:#0039A5;font-size:10pt;'>peak</font></i></font></td></tr>";
  	divHTML = divHTML + "</table>";
  
  	divObj.innerHTML = divHTML;
  	return;
  }
}

function compact_sl() { // draw select list
	
	var selected = new Array(4);
	selected[curSel] = " selected";

	html = "<select id='sel' onChange='compact_sc();' style='width:130px;'>";
	html = html + "<option value='movies'" + selected[0] + ">Top Ten Movies</option>";
	html = html + "<option value='albums'" + selected[1] + ">Top Ten Albums</option>";
	html = html + "<option value='games'" + selected[2] + ">Top Ten Games</option>";
	html = html + "<option value='books'" + selected[3] + ">Top Ten Books</option>";
	html = html + "</select>";
	return html;

}

function compact_sc() { // select category

	var elem = document.getElementById("sel");
	var sel = elem.value;

	if(sel == 'movies') {
		curTitle = 'Top 10 Movies on eBay';
		curSel = 0;
	}
	if(sel == 'albums') {
		curTitle = 'Top 10 Albums on eBay';
		curSel = 1;
	}
	if(sel == 'games') {
		curTitle = 'Top 10 Games on eBay';
		curSel = 2;
	}
	if(sel == 'books') {
		curTitle = 'Top 10 Books on eBay';
		curSel = 3;
	}

	doCompactxml(sel);

}

function compact_i(div, userID, dflt, useebay) { // install

  divObj = div;
  
  divHTML = "<table width=130 cellpadding=0 cellspacing=0 border=0>";
	divHTML = divHTML + "<tr><td>" + compact_sl() + "</td></tr>";
	divHTML = divHTML + "<tr><td><font style='font-family: arial; font-size: 8pt;'><b>" + curTitle + "</b></font></td></tr>";
	divHTML = divHTML + "<tr><td><font style='font-family: arial; font-size: 8pt;'>Powered by <b><i><font style='font-family: arial; color:#6B6B73;font-size:10pt;'>tera</font><font style='font-family: arial; color:#0039A5;font-size:10pt;'>peak</font></i></font></td></tr>";
	divHTML = divHTML + "</table>";

	divObj.innerHTML = divHTML;

	if(dflt == 'movies') {
		curTitle = 'Top 10 Movies on eBay';
		curSel = 0;
	} else if(dflt == 'albums') {
		curTitle = 'Top 10 Albums on eBay';
		curSel = 1;
	} else if(dflt == 'games') {
		curTitle = 'Top 10 Games on eBay';
		curSel = 2;
	} else if(dflt == 'books') {
		curTitle = 'Top 10 Books on eBay';
		curSel = 3;
	} else {
		curTitle = 'Top 10 Movies on eBay';
		curSel = 0;
	}
	ebay = useebay;
	compact_sc();
	if(!userID) {
		userLink = "37856-4640"; // default userID
	} else {
		userLink = userID;
	}

}

  function sendCompactRequest(url,callback,postData) {
  	
  	var req = createCompactXMLHTTPObject();
  	if (!req) return;
  	var requestMethod = (postData) ? "POST" : "GET";
  	 	
  	req.open(requestMethod,url,true);
  	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
  	if (postData)
  		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
  	req.onreadystatechange = function () {
  		if (req.readyState != 4) return;
  		if (req.status != 200 && req.status != 304) {
  //			alert('HTTP error ' + req.status);
  			return;
  		}
  		callback(req);
  	}
  	if (req.readyState == 4) return;
  	req.send(postData);
  }
  
  var XMLHttpFactories = [
  	function () {return new XMLHttpRequest()},
  	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
  	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
  	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
  ];
  
  function createCompactXMLHTTPObject() {
  	var xmlhttp = false;
  	for (var i=0;i<XMLHttpFactories.length;i++) {
  		try {
  			xmlhttp = XMLHttpFactories[i]();
  		}
  		catch (e) {
  			continue;
  		}
  		break;
  	}
  	return xmlhttp;
  }


  function handleCompactRequest(req) {

	  parseCompactXML(req.responseXML);
	  compact_d(1);
  }

  function parseCompactXML(xmlSource) {
    var titleArray = xmlSource.getElementsByTagName('Title');
    var sampleArray = xmlSource.getElementsByTagName('UPC');

    if(sampleArray.length == 0) {
      var sampleArray = xmlSource.getElementsByTagName('ISBN');
    }
    
    var n_elems = 10;

    myCompactArray = new Array(); // clear previous
    myCompactLinksArray = new Array(); // clear previous
    for (i = 0; i < n_elems; i++) {
      myCompactArray.push(titleArray[i].firstChild.nodeValue);
      myCompactLinksArray.push(sampleArray[i].firstChild.nodeValue);
    }
    

  }
  function doCompactxml(productType) {
  
    var xmlCall = productType;
    sendCompactRequest(webRoot + 'top10/topTenTransfer.php?type='+xmlCall, handleCompactRequest,'');
    
  }