Array.prototype.findIndex = function(value){
	var notFound ="";
	for(var i=0;i<this.length;i++){
		if(this[i]==value){
			return i;
		}
	}
	return notFound;
};

Array.prototype.clear=function(){ 
	this.length = 0; 
};

// This is override because the spry taking the index 0 as false
Array.prototype.existsVal = function(value){
	var notFound =false;
	for(var i=0;i<this.length;i++){
		if(this[i]==value){
			return true;
		}
	}
	return notFound;
};

//Here’s the function to insert and extract data from the array:
function manageData(ctrl, floor_number){
	//check the state of the checkbox
	if (ctrl.checked){
		// must be a new floor, were adding
		if(myFloor.length > 4){
			alert('A maximum of five plots can be compared at any one time. Please reselect and try again.');
			ctrl.checked = false;
		} else {
			myFloor.push(floor_number);
		}
	} else {
		// were removing
		idx = myFloor.findIndex(floor_number);
		myFloor.splice(idx,1);
	}

	// reset for good measure
	document.frmFloor.chkvalues.value=myFloor;
	idx="";
	floor_number="";
}

// This gets called by a button being clicked on my form.
function giveMeTheResults(){
	if(myFloor.length < 2){
		alert('You must select at least 2 plots for comparison.');
	} else if(myFloor.length > 5){
		alert('A maximum of five plots can be compared at any one time. Please reselect and try again.');
	} else {
		// plugin your processing script here
		document.frmFloor.chkvalues.value	=	myFloor;
		document.frmFloor.bedrooms.value	= 	document.statusForm.bedFilter.value;
		document.frmFloor.status.value		= 	document.statusForm.statusFilter.value;
		document.frmFloor.price.value		= 	document.statusForm.priceFilter.value;
		document.frmFloor.submit();
	}	
	
	return false;
}

function backToDetailPage()	{
	document.getElementById('frmCompare').submit();
	return false;
}

function clearFloorSelection(){
	myFloor.clear();
	document.frmFloor.chkvalues.value = '';
}

function filterRows(dataSet, row, rowNumber) {
	if(oPlot.existsVal(row["plotnumber"]))
		return row;
	else
		return null;
}

function IsNewFloor(floorStr) {
	var lastFloor = gLastFloorUsed;
	gLastFloorUsed = floorStr;
	return lastFloor != gLastFloorUsed;
}

function openFloorPlan(plot_id, block_name) {
	var thisbId = 0;
	if (block_name.indexOf("Amelia") != -1)
		thisbId = 87;
	if (block_name.indexOf("Pinnacle") != -1)
		thisbId = 93;
	if (block_name.indexOf("Beverley") != -1)
		thisbId = 101;

	window.location = "index.cfm?articleID=74&plot_id=" + plot_id + "&bID=" + thisbId;
}


function filterFunc(dataSet, row, rowNumber) {
	var oBed	= document.statusForm.bedFilter;
	var oStatus	= document.statusForm.statusFilter;
	var oPrice	= document.statusForm.priceFilter;
	// if the availability has not been selected OR the availability matches current row
	if (
	   	(oStatus.selectedIndex == 0 || row["availability"] == oStatus.options[oStatus.selectedIndex].value) &&
		(oBed.selectedIndex == 0 || row["bedrooms"] == oBed.options[oBed.selectedIndex].value)
	) {
		if(oPrice.selectedIndex == 0) {
			return row;
		} else if(row["price"] != 0) {
			if(oPrice.options[oPrice.selectedIndex].value == "400000Min" && row["price"] >= 400000){
				return row;
			}else if(oPrice.options[oPrice.selectedIndex].value == "400000" && row["price"] <= 400000){
				return row;
			}else if(oPrice.options[oPrice.selectedIndex].value == "300000" && row["price"] <= 300000){
				return row;
			}else if(oPrice.options[oPrice.selectedIndex].value == "200000" && row["price"] <= 200000){
				return row;
			}
		} else {
			return null;
		}
	} else {
		return null; // Return null to remove the row from the data set.
	}
}
