var PageStart;
var Passed;
var NewPassed;

var properties = new Array();
var ListData = new Array();
var ListDataIndex;

var SearchData = new Array();
var SearchDataIndex;

var PP_Pages;
var PP_Search;
var PP_Properties;
var PropertyType;

var PP_Suburbs;
var AreaSelected;
var Price1;
var Price2;
var NewPrice1;
var NewPrice2;
var DisplayPrice1;
var DisplayPrice2;
var OriginalPrice1;
var SelectedSuburb;
var SelectedStart;
var FoundAuction;
var SortPrice;

var OriginalSuburb;

var PropertyPrice1;
var PropertyPrice2;

var Suburbs = new Array();
var PP_Property = new Array();
var PP_Line = new Array();
var PP_Suburb = new Array();
var PP_Building = new Array();
var PP_Price = new Array();
var PP_Text = new Array();
var PP_Image = new Array();
var PP_VT = new Array();
var TempListData = new Array();
var TempSortData = new Array();
var SortIndex = new Array();

  PageStart = 0;
  ListDataIndex = 0;
  PP_Search = "";
  PP_Properties = "";
  PP_Pages = "";

  LoadALAN();

  for (i = 0;i <= ListData.length - 1;i++) {
    ConvertToPrice("First",ListData[i][18]);

    FoundAuction = 0;
    if (PropertyPrice1.indexOf("AUCTION") > -1) {FoundAuction = 1}
    if (PropertyPrice1.indexOf("Auction") > -1) {FoundAuction = 1}
    if (PropertyPrice1.indexOf("auction") > -1) {FoundAuction = 1}

    if (parseInt(FoundAuction) === 1) {
      SortPrice = PropertyPrice2.substring(PropertyPrice2.indexOf('-'));
	}
	else {
      SortPrice = ListData[i][18].substring(ListData[i][18].indexOf('-'));
    }

    SortPrice = FixPrice(SortPrice);

    for (p = 0;p < 15 - SortPrice.length;p++) {SortPrice = "0" + SortPrice}
    TempSortData[i] = FixPrice(SortPrice) + '&' + i;
  }

  TempSortData.sort();

  for (i = 0;i <= TempSortData.length - 1;i++) {
    TempSortData[i] = TempSortData[i].substring(TempSortData[i].indexOf('&') + 1);
  }
  	
  for (i = 0;i <= TempSortData.length - 1;i++) {
  	TempListData[i] = ListData[TempSortData[i]];
  }	

  for (i = 0;i <= TempSortData.length - 1;i++) {
    ListData[i] = TempListData[i];
  }	

//  RefreshPage();
function GetValue(){
//  Return ("Yogi");
}

function readPassedValue() {
var TestChar;

  Price1 = "";
  Price2 = "";
  SelectedSuburb = "";
  SelectedStart = 0;
  AreaSelected = 0;
//  location.search = "?radiobutton=buy&PType=All&list=KIAMA+AREA&Thumbnail=checkbox&Submit=Submit&hiddenField=";

  Passed = location.search.substring(1);
  parms = Passed.split('&');

  if (Passed == "") {
    Price1 = "All";
    Price2 = "All";
    SelectedSuburb = "All";
    SelectedStart = 1;
    AreaSelected = "";
	PropertyType = "All";
    NewPassed = "=" + PropertyType + "&" + Price1 + "&" + Price2 + "&" + SelectedSuburb + "&";
    return;
  }
 
  PropertyType = unescape(parms[0].substring(parms[0].indexOf('=')));
  PropertyType = PropertyType.substring(1);

  Price1 = unescape(parms[1].substring(parms[1].indexOf('=')));
  Price2 = unescape(parms[2].substring(parms[2].indexOf('=')));

  SelectedSuburb = unescape(parms[3].substring(parms[3].indexOf('=')));
  SelectedStart = unescape(parms[4].substring(parms[4].indexOf('=')));
 // SelectedStart = SelectedStart.substring(1);

  if (SelectedStart === "Search") {SelectedStart = 1}
  if (SelectedStart === "=Search") {SelectedStart = 1}

//  Price1 = FixPrice(Price1);
//  Price2 = FixPrice(Price2);

  NewPrice1 = 0;
  NewPrice2 = 900000000;

  DisplayPrice1 = "All";

  if (Price1 === "=1") {
    NewPrice1 = 0;
	NewPrice2 = 200000;
    DisplayPrice1 = "$0 - $200k";
  }	
  if (Price1 === "=2") {
    NewPrice1 = 200000;
	NewPrice2 = 300000;
    DisplayPrice1 = "$200k - $300k";
  }	
  if (Price1 === "=3") {
    NewPrice1 = 300000;
	NewPrice2 = 500000;
    DisplayPrice1 = "$300k - $500k";
  }	
  if (Price1 === "=4") {
    NewPrice1 = 500000;
	NewPrice2 = 700000;
    DisplayPrice1 = "$500k - $700k";
  }	
  if (Price1 === "=5") {
    NewPrice1 = 700000;
	NewPrice2 = 900000000;
    DisplayPrice1 = "$700k+";
  }	
  if (Price1 === "=6") {
    NewPrice1 = 0;
	NewPrice2 = 900000000;
    DisplayPrice1 = "All";
  }	

  SelectedSuburb = unescape(SelectedSuburb.substring(SelectedSuburb.indexOf('=')));
//  SelectedSuburb = SelectedSuburb.substring(1,SelectedSuburb.indexOf("+")) + " " + SelectedSuburb.substring(SelectedSuburb.indexOf("+") + 1)
	
  NewPassed = "=" + PropertyType + "&" + Price1 + "&" + Price2 + "&" + SelectedSuburb + "&";

//  NewPassed = "select=" + Price1 + "&select" + Price2 + "&SelectSuburb" + SelectedSuburb + "&Submit=";

//select=100000&select=1000000000&SelectSuburb=Kiama&Submit=Search
  
  OriginalSuburb = SelectedSuburb;

//  if (SelectedSuburb === "THIRROUL AREA") {
//    SelectedSuburb = "AUSTINMER,CLIFTON,COALCLIFF,COLEDALE,HELENSBURGH,OTFORD,SCARBOROUGH,STANWELL PARK,STANWELL TOPS,THIRROUL,THIRROUL EAST,WOMBARRA";
//    AreaSelected = 1;
//  }

  if (AreaSelected === 1) {
    Suburbs = SelectedSuburb.split(',');
    for (i = 0;i <= suburbs.length - 1;i++) {Suburbs[i] = Suburbs[i].toUpperCase()}
  }
  else {
    Suburbs[0] = SelectedSuburb.substring(1);
	Suburbs[0] = Suburbs[0].toUpperCase()
  }	
}

function FixPrice(SourcePrice) {
var TempPrice;

  TempPrice = ""
  for (k = 0;k <= SourcePrice.length - 1;k++) {
    if (parseInt(SourcePrice.substring(k,k+1)) >= 0) {TempPrice = TempPrice + SourcePrice.substring(k,k + 1)} 
  }

  if (TempPrice.length < 1) {
    return 'All';
  }	
  else {	
    return TempPrice;
  }	
}

function ConvertToPrice(RunMode,SourcePrice) {
var TempPrice;

  TempPrice = "";

  for (k = 0;k <= SourcePrice.length - 1;k++) {
    if (SourcePrice.substring(k,k + 1) != " " && SourcePrice.substring(k,k + 1) != "," &&SourcePrice.substring(k,k + 1) != "$") {
      TempPrice = TempPrice + SourcePrice.substring(k,k + 1);
    }	
  }
 
  if (SourcePrice.indexOf("-") > 0) {
    PropertyPrice1 = unescape(TempPrice.substring(0,TempPrice.indexOf('-')));
    PropertyPrice2 = unescape(TempPrice.substring(TempPrice.indexOf('-') + 1));
    OriginalPrice1 = unescape(SourcePrice.substring(0,SourcePrice.indexOf('-') - 1));
  }	
  else {
    OriginalPrice1 = SourcePrice;
    PropertyPrice1 = TempPrice;
	PropertyPrice2 = "x";
  }	
}

function RefreshPage() {
  var AllSuburbs = new Array();

  var MaxIndex;
  var CookieValue;
  var CurrentSuburb;
  var TempSuburb;
  var TempSuburbUpper;
  var CurrentType;
  var FoundSuburb;
  var FoundPrice;
  var FoundPropType;
  var PropertyCount;
  var FoundCount;
  var PageCount;
  var CurrentPage;
  var TempOffice;
  var StartRec;
  var DocumentText;
  var PageIndex;
  var TempString;
  var VTString;
  var VTPos;

  readPassedValue();
  PropertyCount = 0;
  MaxIndex = 9;

  for (i=0; i <= 9; i++) {
    PP_Property[i] = "";
    PP_Line[i] = "";
    PP_Suburb[i] = "";
    PP_Price[i] = "";
    PP_Text[i] = "";
    PP_Image[i] = "";
    PP_VT[i] = "";
  
  }

  TempString =  'onError="this.src=';
  TempString = TempString + "'./images/blank.gif'";

  FoundCount = 0;
  for (i=0; i <= ListData.length - 1; i++) {
    CurrentSuburb = ListData[i][1];
	CurrentSuburb = CurrentSuburb.toUpperCase();

    AllSuburbs[i] = ListData[i][1];

    CurrentType = ListData[i][2];
	FoundSuburb = 0;
	FoundPrice = 0;
	FoundAuction = 0;

    for (j=0;j <= Suburbs.length - 1;j++) {
      if (CurrentSuburb.indexOf(" ") > 0) {
        TempString = CurrentSuburb.split(" ");
		CurrentSuburb = TempString[0] + "+" + TempString[1];
	  }

 	  if (CurrentSuburb === Suburbs[j]) {
	    FoundSuburb = 1;
  	  }   
// window.alert(CurrentSuburb + " A " + Suburbs[j] + " a " + FoundSuburb);	
	}

    if (SelectedSuburb === "All") {FoundSuburb = 1}
    if (SelectedSuburb === "=All") {FoundSuburb = 1}

	TempOffice = ListData[i][26] + " OFFICE";
	TempOffice = TempOffice.toUpperCase();
    if (SelectedSuburb === TempOffice) {FoundSuburb = 1}

    ConvertToPrice("First",ListData[i][18]);

    if (PropertyPrice1.indexOf("AUCTION") > -1) {FoundAuction = 1}
    if (PropertyPrice1.indexOf("Auction") > -1) {FoundAuction = 1}
    if (PropertyPrice1.indexOf("auction") > -1) {FoundAuction = 1}

	if (parseInt(PropertyPrice1) >= parseInt(NewPrice1) && parseInt(PropertyPrice1) <= parseInt(NewPrice2)) {FoundPrice = 1}
	if (parseInt(PropertyPrice2) >= parseInt(NewPrice1) && parseInt(PropertyPrice2) <= parseInt(NewPrice2)) {FoundPrice = 1}
    if (parseInt(PropertyPrice1) === 0) {FoundPrice = 1};
	if (Price2 === "All") {FoundPrice = 1}

    if (FoundAuction == 1) {
	  FoundPrice = 1;
      PropertyPrice1 = PropertyPrice1.substring(PropertyPrice1.indexOf("-") - 1);
	  PropertyPrice2 = "";
	}  
	else {
	  if (PropertyPrice2 != "x") {PropertyPrice1 = PropertyPrice1 + " - " + PropertyPrice2}
    }

   FoundPropType = 0;
   if (PropertyType != "FOREIGN") {
     if (PropertyType === "All") {FoundPropType = 1}
     if (PropertyType === ListData[i][2]) {FoundPropType = 1}
     if (PropertyType === "LAND" && ListData[i][2] === "RESLAND") {FoundPropType = 1}
     if (PropertyType === "LAND" && ListData[i][2] === "RURALLAND") {FoundPropType = 1}
   }
   else {
     if (ListData[i][77] === "FOREIGN") {FoundPropType = 1}
   }	 

//  if (ListData[i][0] == 37) {
//    window.alert(PropertyPrice1.indexOf("Auction") + " a " + FoundPrice + " a " + FoundPropType + " a " + FoundSuburb + " a " + ListData[i][0]);
//  }
//    window.alert(SelectedStart + " Yogi " + PropertyType + " a " + ListData[i][2] + " a " + Price1 + " a " + Price2 + " a " + PropertyPrice1 + " a " + FoundPrice + " a " + SelectedSuburb + " a " + FoundSuburb);
	if (FoundPropType === 1 && FoundSuburb === 1 && FoundPrice === 1) {  
 // window.alert("Found " + CurrentSuburb + " a " + CookieValue);	
      FoundCount = FoundCount + 1;
      if (FoundCount >= SelectedStart) {
        PP_Property[PropertyCount] = ListData[i][0] + '.htm';
        PP_Line[PropertyCount] = 'ID: ' + ListData[i][0];
        PP_Suburb[PropertyCount] = '<a href="./PPSales/' + PP_Property[PropertyCount] + '" class="txt_header_black">' + ListData[i][1] + '</a>';
	    PP_Price[PropertyCount] = '<a href="./PPSales/' + PP_Property[PropertyCount] + '" class="txt_main_black">' + OriginalPrice1 + '</a>';

        PP_Text[PropertyCount] = ListData[i][19] + ' ' + ListData[i][20];

       if (ListData[i].length >= 77) {
          PP_Building[PropertyCount] = ListData[i][76];
		}
		else {
          PP_Building[PropertyCount] = "";
		}  

        if (ListData[i][6] === "") {
          PP_Image[PropertyCount] = '<a href="./PPSales/' + PP_Property[PropertyCount] + '"><img alt="150x100" width="150" height="100" border="0" src="../images/blank.gif"></a>';
	    }
		else {
//          PP_Image[PropertyCount] = '<a href="./PPSales/' + PP_Property[PropertyCount] + '"><img alt="150x100" width="150" height="100" border="0" src="./PPSales/T' + ListData[i][6] + '"></a>';
          PP_Image[PropertyCount] = '<a href="./PPSales/' + PP_Property[PropertyCount] + '"><img alt="150x100" width="150" height="100" border="0" src="./PPSales/T' + ListData[i][6] + '" ' + TempString + '"></a>';
	    } 
        PP_VT[PropertyCount] = ListData[i][75];
  	    if (PP_VT[PropertyCount] != "") {	
	      PP_VT[PropertyCount] = PP_VT[PropertyCount].substring(0,PP_VT[PropertyCount].indexOf("><")) + ' target="_blank"' + PP_VT[PropertyCount].substring(PP_VT[PropertyCount].indexOf("><"));
    	}
	    PropertyCount = PropertyCount + 1;
	  } 	
	}

    if (PropertyCount === 0) {
	  PageCount = 0;
	}
	else {  
	  PageCount = SelectedStart + MaxIndex;
	}
	  
	if (PageCount > FoundCount) {PageCount = FoundCount}
 
//    if (parseInt(Price1) === 0) {
//	  DisplayPrice1 = "$0";
//	}
//	else {
//	  DisplayPrice1 = "$" + NewPrice1.substring(0,NewPrice1.length - 3) + "k";
//	}
//    DisplayPrice2 = "$" + NewPrice2.substring(0,NewPrice2.length - 3) + "k";    

//	if (parseInt(NewPrice2) === 1000000) {DisplayPrice2 = "$1m"}
//	if (parseInt(NewPrice2) > 1000000) {DisplayPrice2 = "$1m+"}

    if (FoundCount === 0) {  
//      PP_Properties = FoundCount + " " + OriginalSuburb + " properties for ";
      PP_Properties = '<div class="displaypropertyheading">No Properties Found</div>';
	  PP_Suburb[0] = " no properties found ..." 
	}
	else {  
//      PP_Properties = FoundCount + " " + OriginalSuburb + " properties for sale " + SelectedStart + " to " + PageCount; 
      if (Price1 === "All") {
        PP_Properties = '<div class="displaypropertyheading">Properties (All) '  + SelectedStart + ' to ' + PageCount + ' of ' + FoundCount + '</div>'; 
	  }
	  else {
          PP_Properties = '<div class="displaypropertyheading">Properties (' + DisplayPrice1 + ') '  + SelectedStart + ' to ' + PageCount + ' of ' + FoundCount + '</div>'; 
      }
	}

    PageCount = FoundCount / 10;
 
    if (PageCount === parseInt(PageCount,10)) {
  	  PageCount = parseInt(PageCount,10);
	}
	else {
	  PageCount = parseInt(PageCount,10) + 1;
	}  

    CurrentPage = SelectedStart / 10;
	CurrentPage = parseInt(CurrentPage,10) + 1;

  }	
  PP_Pages = '<p align="center">';

  for (j=1;j <= PageCount;j++) {
	StartRec = ((j - 1) * 10) + 1;
	if (j === CurrentPage) {
      PP_Pages = PP_Pages + '<a href="./listproperty.asp?' + NewPassed + '1" class="txt_main_black"><font color="#CC0909"> ' + j + ' </font></a>';
    }
	else {
      PP_Pages = PP_Pages + '<a href="./listproperty.asp?' + NewPassed + StartRec + '" class="txt_main_black"><font color="#999999"> ' + j + ' </font></a>';
	} 		
	if (j < PageCount) {PP_Pages = PP_Pages + '<font color="#999999">|</font>'}
  }
  PP_Pages = PP_Pages + '</p>';

  DisplaySuburbSelect();
 
  DocumentText = PP_Properties + PP_Pages;
  if (PropertyCount < 9) {
    PageIndex = PropertyCount - 1;
	}
  else {
    PageIndex = 9;
  }		

  for (i = 0;i <= parseInt(PageIndex);i++) {
    DocumentText = DocumentText + '<div class="displayproperty">';
//    DocumentText = DocumentText + '<div class="displaypropertyheading">' + PP_Suburb[i] + '</div><div class="displaypropertyheading" align="Right">' + PP_Building[i] + '</div>';

    DocumentText = DocumentText + '<div class="displaypropertyheading" align = "left"><table width="95%" border="0"><tr><td>' + PP_Suburb[i] + '</td><td><div align="right">' + PP_Building[i] + '</div></td></tr></table></div>';

    DocumentText = DocumentText + '<p>' + PP_Image[i] + PP_Text[i] + ' </p>';
//    DocumentText = DocumentText + 'Bed: ' + PP_Beds[i] + ' Bath: ' + PP_Baths[i] + ' Car: ' + PP_Cars[i];

    if (PP_VT[i] != "") {DocumentText = DocumentText + PP_VT[i].substring(0,PP_VT[i].indexOf('>')) + '><img border="0" src="./Images/VT_Tour_Small.gif"></a>'}
 
	DocumentText = DocumentText + '<p class="blue"><B>' + PP_Price[i] + '</b></p>';
	DocumentText = DocumentText + '</div>';
  }
  
  DocumentText = DocumentText + PP_Pages + "<p>&nbsp;</p>";
  document.write(DocumentText);
}

function DisplaySuburbSelect() {
var tempSuburb;
  var AllSuburbs = new Array();

  for (i=0; i <= ListData.length - 1; i++) {
    AllSuburbs[i] = ListData[i][1];
  }
  
  AllSuburbs = AllSuburbs.sort();
  PP_Suburbs = '<select name="SelectSuburb" class="input-box"><option selected value="All">All</option>';

  TempSuburb = "";
  for (i = 0;i <= AllSuburbs.length - 1;i++) { 
    if (AllSuburbs[i] != TempSuburb) {
  	  TempSuburb = AllSuburbs[i];
	  TempSuburbUpper = TempSuburb.toUpperCase();
      if (Suburbs[0] === TempSuburbUpper) {
        PP_Suburbs = PP_Suburbs + '<option selected value="' + AllSuburbs[i] + '">' + AllSuburbs[i] + '</option>';
	  }
	  else {
        PP_Suburbs = PP_Suburbs + '<option value="' + AllSuburbs[i] + '">' + AllSuburbs[i] + '</option>';
      }		
    }  
  }		
  PP_Suburbs = PP_Suburbs + '</select>';
}
//<div class="displayproperty">
//  <div class="displaypropertyheading"><a href="displayproperty.asp">Secluded getaway - Surfers Paradise</a></div>
//  <p><a href="displayproperty.asp"><img src="images/55024-1.jpg" width="150" height="100" border="0"></a>Beautifully presented 3 B/R single level brick home located in quiet and private security gated setting. Features light open plan living with high raked ceiling, modern timber kitche ...... <a href="displayproperty.asp">More</a> </p>
//  <p>Bed: 1 Bath: 1 Car: 3</p>
//  <p class="blue"><B>$200,000 - $225,000</B></p>
//</div>











