//<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
//
//<HTML>
//<HEAD></HEAD>
//<BODY>
//
//<script type="text/javascript">
//<!--
//<![CDATA[

var todayString;
var soonString;
var laterString;
var yearPrefix;

function SetDates() {
	tmpStr = "0";
	today = new Date();
	todayYear = today.getFullYear().toString();
//document.write("todayYear = " + todayYear + "<br>");
	todayMonth = (today.getMonth() + 1).toString();
//document.write("todayMonth = " + todayMonth + "<br>");
	todayMonth = "0" + todayMonth;
//document.write("todayMonth = " + todayMonth + "<br>");
	todayMonth = todayMonth.substr(todayMonth.length-2,2);
//document.write("todayMonth = " + todayMonth + "<br>");
	todayDate = today.getDate().toString();
//document.write("todayDate = " + todayDate + "<br>");
	todayDate = tmpStr + todayDate;
//document.write("todayDate = " + todayDate + "<br>");
	todayDate = todayDate.substr(todayDate.length-2,2);
//document.write("todayDate = " + todayDate + "<br>");
	todayString = todayYear + todayMonth + todayDate;
//document.write("todayString = " + todayString + "<br>");
	todayDays = ((today.getFullYear()-1970) * 364.25) + ((today.getMonth() + 1) * 30) + today.getDate();
//document.write("todayDays = " + todayDays + "<br>");
	
	soonDate = new Date( (todayDays + 60) * 24*60*60*1000);
//document.write("soonDate = " + soonDate.toString() + "<br>");
 	soonYear = soonDate.getFullYear().toString();
//document.write("soonYear = " + soonYear + "<br>");
	soonYear = soonDate.getFullYear();
//document.write("soonYear = " + soonYear + "<br>");
	soonMonth = (soonDate.getMonth() + 1).toString();
//document.write("soonMonth = " + soonMonth + "<br>");
	soonMonth = tmpStr + soonMonth;
//document.write("soonMonth = " + soonMonth + "<br>");
	soonMonth = soonMonth.substr(soonMonth.length-2,2);
//document.write("soonMonth = " + soonMonth + "<br>");
	soonDay = soonDate.getDate().toString();
//document.write("soonDay = " + soonDay + "<br>");
	soonDay = tmpStr + soonDay;
//document.write("soonDay = " + soonDay + "<br>");
	soonDay = soonDay.substr(soonDay.length-2,2);
//document.write("soonDay = " + soonDay + "<br>");
	soonString = soonYear + soonMonth + soonDay;
//document.write("soonString = " + soonString + "<br>");

	laterDate = new Date((todayDays + 190) * 24*60*60*1000);
	laterYear = laterDate.getFullYear().toString();
	laterMonth = (laterDate.getMonth() + 1).toString();
	laterMonth = tmpStr + laterMonth;
	laterMonth = laterMonth.substr(laterMonth.length-2,2);
	laterDay = laterDate.getDate().toString();
	laterDay = tmpStr + laterDay;
	laterDay = laterDay.substr(laterDay.length-2,2);
	laterString = laterYear + laterMonth + laterDay;
	
	yearPrefix = today.getFullYear().toString().substr(0, 2);
//todayString = "20060309";
//soonString = "20060509";
//laterString = "20060909";
//yearPrefix = "20";
	return;
}

function compareLeaseRent(val1, val2) {
	return parseInt(val1) - parseInt(val2);
}

function compareLeaseRentReverse(val1, val2) {
	return parseInt(val2) - parseInt(val1);
}

function SortListings(primaryVec, pinStyleVec, orderVec, orderStyle) {
	var vecLength = primaryVec.length;
	var ref = "";
	var tmpVec = new Array();
	var tmpStr = "";
	var tmpStr2 = "";
	var tmpArray;

//	if (todayString == "") { SetDates(); }
	SetDates();

//	document.write("todayString = " + todayString + " / soonString = " + soonString + 
//				   " / laterString = " + laterString + "<BR>");


//	if (secondaryVec.length <= 0) { secondaryVec = new Array("",""); }
	if (orderStyle == 0 || orderStyle == 1) {
		for (i=0; i<vecLength; i++) {
			tmpStr = primaryVec[i].getAttribute("lease");
			tmpStr = tmpStr != "" ? tmpStr : "Available Now";
//			tmpStr2 = "0000" + primaryVec[i].getAttribute("rent");
			tmpStr2 = primaryVec[i].getAttribute("rent");
			tmpStr2 = "0000" + tmpStr2;
			tmpStr2 = tmpStr2.substr(tmpStr2.length-4,4);

			re = /[0-9]+/;
			re2 = /n./i;
			re3 = /not /i;
			if ( tmpStr.match(re2) || tmpStr.match(re3) ) {
				tmpStr = laterString;
			} else if ( ! tmpStr.match(re) ) {
				tmpStr = todayString;
			} else {
			//Process the "Available 8/1/06" style string 
			re1 = /Available /i;
			re2 = /[a-zA-Z ]*([0-9]+)\/([0-9]+)\/([0-9]+)$/;
			tmpStr = tmpStr.replace(re1, "");
			tmpArray = re2.exec(tmpStr);
			var tmpYear = tmpArray[3] ? tmpArray[3] : "05";
			tmpYear = yearPrefix + tmpYear.substr(tmpYear.length-2,2);
			var tmpMonth = tmpArray[1];
			tmpMonth = "00" + tmpMonth;
			tmpMonth = tmpMonth.substr(tmpMonth.length-2,2);
			var tmpDate = tmpArray[2];
			tmpDate = "00" + tmpDate;
			tmpDate = tmpDate.substr(tmpDate.length-2,2);
			tmpStr = tmpYear + tmpMonth + tmpDate;
			if (tmpStr < todayString) { tmpStr = todayString; }
			}
			
			ref = i;
			ref = "0000" + ref;
			ref = ref.substr(ref.length-4,4);
			tmpVec[i] = tmpStr + tmpStr2 + ref;
		}
//		Quicksort(tmpVec,0,vecLength-1);
		tmpVec.sort(compareLeaseRent);
		for (i=0; i<vecLength; i++) {
//			orderVec[i] = parseInt(tmpVec[i].substr(-4,4));
		orderVec[i] = tmpVec[i].substr(tmpVec[i].length-4,4);
		orderVec[i] = parseInt(orderVec[i],10);
//	document.write("tmpVec = " + tmpVec[i] + " / orderVec = " + orderVec[i] + "<BR>");
		}
	} else if (orderStyle == 2) {
		for (i=0; i<vecLength; i++) {
			tmpStr = primaryVec[i].getAttribute("lease");
			tmpStr = tmpStr != "" ? tmpStr : "Available Now";
			tmpStr2 = primaryVec[i].getAttribute("rent");
			tmpStr2 = "0000" + tmpStr2;
			tmpStr2 = tmpStr2.substr(tmpStr2.length-4,4);

			re = /[0-9]+/;
			re2 = /n./i;
			re3 = /not /i;
			if ( tmpStr.match(re2) || tmpStr.match(re3) ) {
				tmpStr = laterString;
			} else if ( ! tmpStr.match(re) ) {
				tmpStr = todayString;
				pinStyleVec[i] = 1; // 1 = green
			} else {
				//Process the "Available 8/1/06" style string 
				re1 = /Available /i;
				re2 = /[a-zA-Z ]*([0-9]+)\/([0-9]+)\/([0-9]+)$/;
				tmpStr = tmpStr.replace(re1, "");
				tmpArray = re2.exec(tmpStr);
				var tmpYear = tmpArray[3] ? tmpArray[3] : "05";
				tmpYear = yearPrefix + tmpYear.substr(tmpYear.length-2,2);
				var tmpMonth = tmpArray[1];
				tmpMonth = "00" + tmpMonth;
				tmpMonth = tmpMonth.substr(tmpMonth.length-2,2);
				var tmpDate = tmpArray[2];
				tmpDate = "00" + tmpDate;
				tmpDate = tmpDate.substr(tmpDate.length-2,2);
				tmpStr = tmpYear + tmpMonth + tmpDate;
				if (compareLeaseRent(tmpStr, soonString) <= 0) { 
					tmpStr = todayString; 
					pinStyleVec[i] = 1; // 1 = green
				} else if (compareLeaseRent(tmpStr, soonString) > 0 && compareLeaseRent(tmpStr, laterString) <= 0) {
					tmpStr = soonString;
					pinStyleVec[i] = 2; // 2 = blue
				} else {
					tmpStr = laterString;
					pinStyleVec[i] = 0; // 0 = red
				}
			}
			
			ref = i;
			ref = "0000" + ref;
			ref = ref.substr(ref.length-4,4);
			tmpVec[i] = tmpStr + tmpStr2 + ref;
		}
//		Quicksort(tmpVec,0,vecLength-1);
		tmpVec.sort(compareLeaseRent);
		for (i=0; i<vecLength; i++) {
//			orderVec[i] = parseInt(tmpVec[i].substr(-4,4));
		orderVec[i] = tmpVec[i].substr(tmpVec[i].length-4,4);
		orderVec[i] = parseInt(orderVec[i],10);
//	document.write("tmpVec = " + tmpVec[i] + " / orderVec = " + orderVec[i] + "<BR>");
		}
	} else if (orderStyle == 3) {
		for (i=0; i<vecLength; i++) {
			tmpStr = primaryVec[i].getAttribute("lease");
			tmpStr = tmpStr != "" ? tmpStr : "Available Now";
			tmpStr2 = primaryVec[i].getAttribute("rent");
			tmpStr2 = "0000" + tmpStr2;
			tmpStr2 = tmpStr2.substr(tmpStr2.length-4,4);

			re = /[0-9]+/;
			re2 = /n./i;
			re3 = /not /i;
			if ( tmpStr.match(re2) || tmpStr.match(re3) ) {
				tmpStr = laterString;
			} else if ( ! tmpStr.match(re) ) {
				tmpStr = todayString;
				pinStyleVec[i] = 1; // 1 = green
			} else {
				//Process the "Available 8/1/06" style string 
				re1 = /Available /i;
				re2 = /[a-zA-Z ]*([0-9]+)\/([0-9]+)\/([0-9]+)$/;
				tmpStr = tmpStr.replace(re1, "");
				tmpArray = re2.exec(tmpStr);
				var tmpYear = tmpArray[3] ? tmpArray[3] : "05";
				tmpYear = yearPrefix + tmpYear.substr(tmpYear.length-2,2);
				var tmpMonth = tmpArray[1];
				tmpMonth = "00" + tmpMonth;
				tmpMonth = tmpMonth.substr(tmpMonth.length-2,2);
				var tmpDate = tmpArray[2];
				tmpDate = "00" + tmpDate;
				tmpDate = tmpDate.substr(tmpDate.length-2,2);
				tmpStr = tmpYear + tmpMonth + tmpDate;
				if (compareLeaseRent(tmpStr, soonString) <= 0) { 
					tmpStr = todayString; 
					pinStyleVec[i] = 1; // 1 = green
				} else if (compareLeaseRent(tmpStr, soonString) > 0 && compareLeaseRent(tmpStr, laterString) <= 0) {
					tmpStr = soonString;
					pinStyleVec[i] = 2; // 2 = blue
				} else {
					tmpStr = laterString;
					pinStyleVec[i] = 0; // 0 = red
				}
			}
			
			ref = i;
			ref = "0000" + ref;
			ref = ref.substr(ref.length-4,4);
			tmpVec[i] = tmpStr + tmpStr2 + ref;
		}
//		Quicksort(tmpVec,0,vecLength-1);
		tmpVec.sort(compareLeaseRentReverse);
		for (i=0; i<vecLength; i++) {
//			orderVec[i] = parseInt(tmpVec[i].substr(-4,4));
		orderVec[i] = tmpVec[i].substr(tmpVec[i].length-4,4);
		orderVec[i] = parseInt(orderVec[i],10);
//	document.write("tmpVec = " + tmpVec[i] + " / orderVec = " + orderVec[i] + "<BR>");
		}
	}
	return;
}

/**************************************************************
	This function adapted from the algorithm given in:
		Data Abstractions & Structures Using C++, by
		Mark Headington and David Riley, pg. 586.

	Quicksort is the fastest array sorting routine for
	unordered arrays.  Its big O is n log n.
 **************************************************************/
//function Quicksort(vec, loBound, hiBound)
//{
//	document.write("Quicksort entry " + loBound + " / " + hiBound + ": lo -- " + 
//		vec[loBound] + "hi -- " + vec[hiBound] + "<BR>");
//	var pivot, loSwap, hiSwap, temp;
//
//	// Two items to sort
//	if (hiBound - loBound == 1)
//	{
//		if (vec[loBound] > vec[hiBound])
//		{
//			temp = vec[loBound];
//			vec[loBound] = vec[hiBound];
//			vec[hiBound] = temp;
//		}
//		return;
//	}
//
//	// Three or more items to sort
//	pivot = vec[parseInt((loBound + hiBound) / 2)];
//	vec[parseInt((loBound + hiBound) / 2)] = vec[loBound];
//	vec[loBound] = pivot;
//	loSwap = loBound + 1;
//	hiSwap = hiBound;
//
//	do {
//		// Find the right loSwap
//		while (loSwap <= hiSwap && vec[loSwap] <= pivot)
//			loSwap++;
//
//		// Find the right hiSwap
//		while (vec[hiSwap] > pivot)
//			hiSwap--;
//
//		// Swap values if loSwap is less than hiSwap
//		if (loSwap < hiSwap)
//		{
//			temp = vec[loSwap];
//			vec[loSwap] = vec[hiSwap];
//			vec[hiSwap] = temp;
//		}
//	} while (loSwap < hiSwap);
//
//	vec[loBound] = vec[hiSwap];
//	vec[hiSwap] = pivot;
//
//
//	// Recursively call function...  the beauty of quicksort
//
//	// 2 or more items in first section		
//	if (loBound < hiSwap - 1)
//		Quicksort(vec, loBound, hiSwap - 1);
//
//
//	// 2 or more items in second section
//	if (hiSwap + 1 < hiBound)
//		Quicksort(vec, hiSwap + 1, hiBound);
//}
//
//
//function PrintArray(vec,lo,hi)
///**************************************************************
//	Simply print out an array from the lo bound to the
//	hi bound.
// **************************************************************/
//{
//	var i;
//	for (i = lo; i <= hi; i++)
//		document.write(vec[i] + "<BR>");
//}
//

//	// Create an array and stuff some values in it
//	var x = new Array(10);
//	x[0] = 10;
//	x[1] = 1;
//	x[2] = 3;
//	x[3] = 8;
//	x[4] = 2;
//	x[5] = 11;
//	x[6] = 4;
//	x[7] = 22;
//	x[8] = 12;
//	x[9] = 6;
//
//	document.write("Here is a jumbled array:<BR>");
//	PrintArray(x,0,9);
//
//	Quicksort(x,0,9);	// Sort the array using quicksort
//
//	document.write("<BR>Now the array is sorted!<BR>");
//	PrintArray(x,0,9);
//	
//	document.write("<BR>Now the test from dbb<BR>");
//	today = new Date();
//	todayYear = today.getFullYear().toString();
//	todayMonth = "00" + (today.getMonth() + 1).toString();
//	todayMonth = todayMonth.substr(-2,2);
//	todayDate = "00" + today.getDate().toString();
//	todayDate = todayDate.substr(-2,2);
//	todayString = todayYear + todayMonth + todayDate;
//	document.write("Today date : " + todayString  +"<BR>");
//	yearPrefix = today.getFullYear().toString().substr(0, 2);
//	tmpStr = "07";
//	newYear = yearPrefix + tmpStr.substr(-2,2);
//	document.write( "test year : " + newYear + " (should be 2007)<BR>" );
//
//	laterDate = new Date(Date.now() + 180*24*60*60*1000);
//	laterYear = laterDate.getFullYear().toString();
//	laterMonth = "00" + (laterDate.getMonth() + 1).toString();
//	laterMonth = laterMonth.substr(-2,2);
//	laterDate = "00" + laterDate.getDate().toString();
//	laterDate = laterDate.substr(-2,2);
//	laterString = laterYear + laterMonth + laterDate;
//	document.write( "later date : " + laterString + " (should be today + 180 days)<BR>" );
//
//
//	re1 = /Available /i;
//	re = /[a-zA-Z ]*([0-9]+)\/([0-9]+)\/([0-9]+)$/;
//	tmpStr = "Available 8/1/07";
//	tmpStr = tmpStr.replace(re1, "");
//	tmpArray = re.exec(tmpStr);
//	tmpYear = tmpArray[3] ? tmpArray[3] : "05";
//	tmpYear = yearPrefix + tmpYear.substr(-2,2);
//	tmpMonth = "00" + tmpArray[1];
//	tmpMonth = tmpMonth.substr(-2,2);
//	tmpDate = "00" + tmpArray[2];
//	tmpDate = tmpDate.substr(-2,2);
//	tmpStr = tmpYear + tmpMonth + tmpDate;
////	tmpStr = tmpStr.replace(re, "$3$1$2");
//
//
//	document.write(tmpStr + "<BR>");
//	
//	tmpArray = re.exec("Available 8/1/06");
//	PrintArray(tmpArray,0,3);
//	tmpArray = re.exec("Available 12/11/07");
//	PrintArray(tmpArray,0,3);
//	tmpArray = re.exec("Available Now");
//	PrintArray(tmpArray,0,3);
//	
//</script>
//	
//</BODY>
//</HTML>