// BosClassifieds Javascript Module
// Copyright BosDev, Inc      All rights reserved
//
// Purpose: To allow users to embed desired information into
// their existing HTML pages with a simple Javascript call
// while offering flexibility in display options

// Usage: Set your configuration settings below.  Then include 
// the following line into your HTML page
// <script type="text/javascript" language="Javascript" src="http://www.example.com/ads/jsdisplay.js"></script>

// Configuration: Answer the questions below to configure 
// what, and how you wish to display the results.
var classifiedsUrl = "http://www.bloomingtononline.net/classifieds/";	//Set this to the complete URL to your classifieds, example http://www.example.com/ads/
var categories = "";      	//Set this to a comma delimited list of categories you wish to display, such as "1,32,45"
				//If the categories is left to "", newest ads in the entire system are shown
var numberAds = 10;		//Set this to the maximum number of ads to show				
var types = "";           	//Set this to a comma delimited list of ad types you wish to display, such as "1,2,3"
var numberColumns = 1;    	//Set this to the number of column you wish to display results in
var displayWidth = "100%"	//Set this to the width of the desired display, such as "200", or "50%"
var showTitle = 1;        	//Set this to 1 to show the ad title
var showCategory = 0;     	//Set this to 1 to show the category
var showImage = 1;        	//Set this to 1 to show the primary image
var showDesc = 0;        	//Set this to 1 to show a partial description
var showPrice = 1;        	//Set this to 1 to show the price
var showLocation = 0;     	//Set this to 1 to the city/state
var showDateBegan = 0;		//Set this to 1 to show the ad start date
var showDateEnd = 0; 		//Set this to 1 to show the ad end date


// -------------------------------------- //
// DO NOT MODIFY ANYTHING ELSE BELOW      //
// -------------------------------------- //

function handleAdsResponse(type) {
  	var parsedResults = '';
  	var currentCell = 0;
  	var rowOpen = 0;
  	var classifiedsText = '';
	var results = adsData.responseXML;

	classifiedsText += '<div id="classifieds">';
	classifiedsText += '<table width="'+displayWidth+'">';

	var allClassifieds = results.getElementsByTagName("listing");

	for (var i=0;i<allClassifieds.length;i++) {
		var currentClassified = allClassifieds[i];

		var adElement = currentClassified.getElementsByTagName("title").item(0);
		var adTitle = adElement.firstChild.nodeValue;
		var adElement = currentClassified.getElementsByTagName("category").item(0);
		var adCategory = adElement.firstChild.nodeValue;
		var adElement = currentClassified.getElementsByTagName("type").item(0);
		var adType = adElement.firstChild.nodeValue;
		var adElement = currentClassified.getElementsByTagName("description").item(0);
		var adDesc = adElement.firstChild.nodeValue;
		var adElement = currentClassified.getElementsByTagName("begin").item(0);
		var adBegin = adElement.firstChild.nodeValue;
		var adElement = currentClassified.getElementsByTagName("end").item(0);
		var adEnd = adElement.firstChild.nodeValue;
		var adElement = currentClassified.getElementsByTagName("location").item(0);
		var adLocation = adElement.firstChild.nodeValue;
		var adElement = currentClassified.getElementsByTagName("image").item(0);
		var adImage = adElement.firstChild.nodeValue;

		if(currentCell >= numberColumns) {
			currentCell = 0;
			rowOpen = 0;
			classifiedsText += '</tr>';
			}
		
		if(rowOpen == 0) { 
			classifiedsText += '<tr>'; 
			rowOpen = 1;
			}
		
		classifiedsText += '<td valign="top">';
		
		if(showImage == 1 && adImage != "") { classifiedsText += '<img src="'+adImage+'" align="left" />'; }
		if(showTitle == 1) { classifiedsText += adTitle+'<br />'; }
		if(showCategory == 1) { classifiedsText += '<span class="category">'+adCategory+'</span><br />'; }
		if(showDesc == 1) { classifiedsText += '<span class="description">'+adDesc+'</span><br />'; }
		if(showLocation == 1) { classifiedsText += '<span class="location">'+adLocation+'</span><br />'; }
		if(showDateBegan == 1) { classifiedsText += '<span class="date">'+adBegin+'</span><br />'; }
		if(showDateEnd == 1) { classifiedsText += '<span class="date">'+adEnd+'</span><br />'; }
		
		classifiedsText += '<hr></td>';
		currentCell++;
		}	
	
	classifiedsText += '</table>';
	classifiedsText += '</div>';
	document.getElementById('classifieds').innerHTML = classifiedsText;
	}

function getHTTPObject() {
  	var xmlhttp;

	/*@cc_on
	@if (@_jscript_version >= 5)
		try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) {
			try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (E) { xmlhttp = false; }
		}
		@else xmlhttp = false;
  	@end @*/

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try { xmlhttp = new XMLHttpRequest(); }
		catch (e) { xmlhttp = false; }
		}

	return xmlhttp;
	}

function showClassifieds() {
	var theUrl = classifiedsUrl+'rss.php?fromjs=true&cats='+categories+'&types='+types+'&limit='+numberAds;
	adsData.open("GET",theUrl,true);
	adsData.onreadystatechange = function() {
		if (adsData.readyState == 4) {
			if (adsData.status == 200) { handleAdsResponse(); }
        		}
		}
	adsData.send(null);
	}
	
var adsData = getHTTPObject();
document.write('<div id="classifieds"></div>');
showClassifieds();	