// Creating the Array Function
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 is the function to insert and extract data from the array:
function manageData(ctrl, floor_number) {
	// May need to add the plot
	if (ctrl.checked){
		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);
		}
	// Or remove it
	}else{
		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(aid) {
	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 {
		if (aid == propertyArticle) {
			// plugin your processing script here
			document.frmFloor.chkvalues.value	= myFloor;
			document.frmFloor.bedrooms.value	= document.statusForm.bedFilter.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 filterFunc(dataSet, row, rowNumber) {
	var oBed	= document.statusForm.bedFilter;
	var oPrice	= document.statusForm.priceFilter;

	// if the availability has not been selected OR the availability matches current row
	if((oBed.selectedIndex == 0 || row["bedrooms"] >= oBed.options[oBed.selectedIndex].value)) {
		if(oPrice.selectedIndex == 0){
			return row;
		}else if(oPrice.options[oPrice.selectedIndex].value == "7000000" && row["price"] <= 7000000){
			return row;
		}else if(oPrice.options[oPrice.selectedIndex].value == "3000000" && row["price"] <= 3000000){
			return row;
		}else if(oPrice.options[oPrice.selectedIndex].value == "1000000" && row["price"] <= 1000000){
			return row;
		}else if(oPrice.options[oPrice.selectedIndex].value == "500000" && row["price"] <= 500000){
			return row;
		}else{
			return null;
		}
	} else {
		return null; // Return null to remove the row from the data set.
	}
}

function IsNewFloor(floorStr) {
	var lastFloor = gLastFloorUsed;
	gLastFloorUsed = floorStr;
	return lastFloor != gLastFloorUsed;
}

function openFloorPlan( plotID ) {
	window.location = "index.cfm?articleID=" + statusArticle + "&articleAction=floorplan&plotID=" + plotID;
}
