// JavaScript Document
function getParams(home) {
	/*
	if(home)
		document.getElementById("lresult").innerHTML="<img src=modalbox/spinner.gif style='border:0px;'></img> <br />Creating account...<br />this may take a minute.";
 	*/
//	document.getElementById("moveDiv").style.marginTop = 80 + "px";
	sql = "select * from pro_idx where 1 ";
    zip = false;
	poster = '{s:8:"searchme";s:1:"1";';   //create php serialize format of query.  prepend a:#of fields at end of function
	counter = 1;  //array count;
	document.getElementById('listmap').value="listview";
	isBeby = false;
	
	
//1.25.10 - not using adv anymore - using simulated form post	
		  //first check if an mls number
	
//1.25.10 - check first for beby
//get location part
	if((document.getElementById("beby").value.length > 1) && (document.getElementById("beby").value.indexOf("Where do you want")==-1) && (document.getElementById("beby").value.indexOf("Enter an address")==-1)) {
//it's beby
//need latitude/long 
 		myLatlon = document.getElementById('latlon').value.split(",");
  		myLat = myLatlon[0];
		myLon = myLatlon[1];
		myWithin = document.getElementById("within").value;
		sql = "select pro_idx.*, l.lat, l.lon, sqrt(69.1 * (" + myLat + " - lat) * 69.1*(" + myLat + "- lat)+61.2 * (" + myLon + "-lon) * 61.2 * (" + myLon + "-lon)) AS DISTANCE FROM pro_idx , latlon l  where l.mlsnum = pro_idx.MLSNUM and sqrt(69.1 * (" + myLat + " - lat) * 69.1*(" + myLat + "-lat)+61.2 * (" + myLon + "-lon) * 61.2 * (" + myLon + "-lon)) <= " + myWithin + " ";	
		document.getElementById('listmap').value="mapview";
		temp = document.getElementById("beby").value;
		temp = temp.replace('/"/g','');
		temp = temp.replace("/'/g",'');
 		poster += 's:4:"beby";s:' + temp.length + ':"' + temp + '";';
		poster += 's:6:"latlon";s:' + document.getElementById('latlon').value.length + ':"' + document.getElementById('latlon').value + '";';
		poster += 's:6:"within";s:' + document.getElementById('within').value.length + ':"' + document.getElementById('within').value + '";';
		counter+=3;
		isBeby=true;
 		
//now check zip 
//1.25.10 - zip/mls use the same field 
	} else if(document.getElementById("zipcode").value.length > 1) {
		sql1 = document.getElementById("zipcode").value;
		if (IsNumeric(sql1)) {
			if(document.getElementById("zipcode").value.length==5) {
				sql = sql + "and zipcode = " + sql1 + " ";
			} else {
				sql = sql + "and mlsnum = " + sql1 + " ";
			}
			poster += 's:7:"zipcode";s:' + document.getElementById('zipcode').value.length + ':"' + document.getElementById('zipcode').value + '";';
			counter++;
		} else {
			alert("Please enter only numbers for ZIPCODE or MLS Number");
			document.sel.zipcode.focus();
			return false;
		}
	} else {  //must be a county
//if you have a zipcode - no need to check city or subdiv
	  	if(document.getElementById("county").selectedIndex) {
			sql1 = document.getElementById("county").value;
 			sql = sql + "and COUNTY = '" + sql1 + "' ";
			poster += 's:6:"county";s:' + document.getElementById('county').value.length + ':"' + document.getElementById('county').value + '";';
			counter++;
		}
	  	if(document.getElementById("city").selectedIndex) {
			str1=document.getElementById("city").value;
			sql = sql + "and CITY = '" + str1 + "' ";
			poster += 's:4:"city";s:' + document.getElementById('city').value.length + ':"' + document.getElementById('city').value + '";';
			counter++;
 	  	} else if(bigcity.length > 1) {
			a=0;
			c=0;
			count=0;
			temp = bigcity;
			str2 = '';
			str3 = '';
			while ((b=temp.indexOf("+")) || (temp.length>3)) {
				if(b < 0) b=temp.length;
 				t1=temp.substring(a,b);
				if(t1.length < 3) break;
				if(count) {
				    str2 = str2 + "','" + t1;
//				    str3 = str3 + "7" + t1.replace(/ /g,"8");
				} else {
				    str2 = t1;
//				    str3 = t1.replace(/ /g,"8");
				}
				temp=temp.substr(b+1,temp.length);
				count++;
			}
			sql = sql + "and CITY in ('" + str2 + "') ";
			poster += 's:5:"cityz";s:' + str2.length + ':"' + str2 + '";';
			counter++;
			
 	  	}
	  	if(document.getElementById("citysub").selectedIndex) {	
	  		str1 = document.getElementById("citysub").value;
			sql = sql + "and subdivision like '%" + sql1 + "%' ";
			poster += 's:7:"citysub";s:' + document.getElementById('citysub').value.length + ':"' + document.getElementById('citysub').value + '";';
			counter++;
			
		}
	}

//property details
	if(document.getElementById("type").selectedIndex) {
			switch(document.getElementById("type").selectedIndex.value) {
				case "CONDO":
			  		sql = sql +  "and propertycodes='CONDO' ";
 					break;
				case "TOWNH":	
			  		sql = sql +  "and propertycodes='TOWNH' ";
 					break;
				case "VILLA":	
			  		sql = sql +  "and propertycodes='VILLA' ";
 					break;
				default:	
			  		sql = sql +  "and propertycodes='SINGL' ";
 					break;
			}
			poster += 's:4:"type";s:' + document.getElementById('type').value.length + ':"' + document.getElementById('type').value + '";';
			counter++;		
	}
	
	if(document.getElementById("minprice").selectedIndex) {
		sql1 = document.getElementById("minprice").value;
		sql = sql + "and LISTPRICE >= " + sql1 + " ";
		poster += 's:8:"minprice";s:' + document.getElementById('minprice').value.length + ':"' + document.getElementById('minprice').value + '";';
		counter++;				
 	}

	if(document.getElementById("maxprice").selectedIndex) {
		sql1 = document.getElementById("maxprice").value;
		sql = sql + "and LISTPRICE <= " + sql1 + " ";
		poster += 's:8:"maxprice";s:' + document.getElementById('maxprice').value.length + ':"' + document.getElementById('maxprice').value + '";';
		counter++;						
 	}
	
	if(document.getElementById("beds").selectedIndex) {
		sql1 = document.getElementById("beds").value;
		sql = sql + "and BEDS >= " + sql1 + " ";
		poster += 's:4:"beds";s:' + document.getElementById('beds').value.length + ':"' + document.getElementById('beds').value + '";';
		counter++;						
 	}

	if(document.getElementById("baths").selectedIndex) {
		sql1 = document.getElementById("baths").value;
		sql = sql + "and BATHSFULL >= " + sql1 + " ";
		poster += 's:4:"baths";s:' + document.getElementById("baths").value.length + ':"' + document.getElementById('baths').value + '";';
		counter++;								
 	}
	
	if(document.getElementById("lotsize").selectedIndex) {
	    switch (document.getElementById("lotsize").value) {
			case "1":
			  	sql = sql +  "and lotsizecom >= 1 ";
 				break;
			case "2":
			  	sql = sql +  "and lotsizecom >= 2 ";
 				break;
			case "3":
			  	sql = sql +  "and lotsizecom >= 5 ";
 				break;
			case "4":
			  	sql = sql +  "and lotsizecom >= 10 ";
 				break;
			case "5":
			  	sql = sql +  "and lotsizecom >= 20 ";
 				break;
			case "98":
			  	sql = sql +  "and lotsizecom >= .25 ";
 				break;
 			default:
			  	sql = sql +  "and lotsizecom >= .5 ";
 				break;
		}
		poster += 's:7:"lotsize";s:' + document.getElementById('lotsize').value.length + ':"' + document.getElementById('lotsize').value + '";';
		counter++;							
	}
	
	if(document.getElementById("square").selectedIndex) {
		sql1 = document.getElementById("square").value;
		sql = sql + "and SQFTHEATED >= " + sql1 + " ";
		poster += 's:6:"square";s:' + document.getElementById('square').value.length + ':"' + document.getElementById('square').value + '";';
		counter++;								
 	}
	
	if(document.getElementById("year").selectedIndex) {
		sql1 = document.getElementById("year").value;
		sql = sql + "and YEARBUILT >= " + sql1 + " ";
		poster += 's:4:"year";s:' + document.getElementById('year').value.length + ':"' + document.getElementById('year').value + '";';
		counter++;								
 	}

	if(document.getElementById("days").selectedIndex) {
		selIndex = document.getElementById("days").selectedIndex;	
		daysValue = document.getElementById("days")[selIndex].value;
	    switch (daysValue) {
			case "1":
			  	sql = sql +  "and datediff(curDate(),date(LISTDATE))<=1 ";
				break;
			case "10":
			  	sql = sql +  "and datediff(curDate(),date(LISTDATE))<=10 ";
				break;
			case "20":
			  	sql = sql +  "and datediff(curDate(),date(LISTDATE))<=20 ";
				break;
			case "30":
				sql = sql + "and datediff(curDate(),date(LISTDATE))<=30 ";
				break;
			case "60":
				sql = sq; +  "and datediff(curDate(),date(LISTDATE))<=60 ";
				break;
			default:
				sql = sql +  "and datediff(curDate(),date(LISTDATE))<=90 ";
				break;
		}
		poster += 's:4:"days";s:' + document.getElementById('days').value.length + ':"' + document.getElementById('days').value + '";';
		counter++;						
		
	}
	
	if(!document.getElementById("waterfront").selectedIndex==0) {	
		switch (document.getElementById("waterfront").selectedIndex) {
			case 1:
			  	sql = sql +  "and (waterfronttype like '%GFOCN%') ";
 				break;
			case 2:
				sql = sql +  "and (waterfronttype like '%INTRA%') ";
 				break;
			case 3:
				sql = sql +  "and (waterfronttype like '%GFOCN%' or waterfronttype like '%INTRA%') ";
 				break;			
			default:
			  	sql = sql +  "and length(waterfronttype) > 1 "; 		
 				break;
 		}	  
		poster += 's:10:"waterfront";s:' + document.getElementById('waterfront').value.length + ':"' + document.getElementById('waterfront').value + '";';
		counter++;						
		
	}	
	  

	if(!document.getElementById("foreclosure").selectedIndex==0) {	
	    switch (document.getElementById("foreclosure").selectedIndex) {
			case 1:
			  	sql = sql +  "and (distressprop like '%ANK%' or distressprop like '%REO%') ";
 				break;
			case 2:
			  	sql = sql +  "and (distressprop like '%SHORT%' or distressprop like '%PREFC%' or distressprop like '%INFC%' ) ";
 				break;
			default:
			  	sql = sql +  "and (distressprop like '%SHORT%' or distressprop like '%PREFC%' or distressprop like '%INFC%' or distressprop like '%ANK%' or distressprop like '%REO%' ) ";			
 				break;
 		}	  
		poster += 's:10:"foreclosure";s:' + document.getElementById('foreclosure').value.length + ':"' + document.getElementById('foreclosure').value + '";';
		counter++;						
		
	}	
	   
	if(document.getElementById("pool").selectedIndex) {
		if (document.getElementById("pool").selectedIndex==1) {
			sql = sql + "and (POOL like '%GUNTE%' or POOL like '%INGRN%' or POOL like '%HEATD%') ";
 		}
		else {
			sql = sql + "and POOL like '%NONE%' ";
 		}
		poster += 's:4:"pool";s:' + document.getElementById('pool').value.length + ':"' + document.getElementById('pool').value + '";';
		counter++;						
		
	}
	
//complete serialize
	poster = 'a:' + counter + ':' + poster + '}';
	document.getElementById("poster").value = poster;
 //	alert("poster = " + poster);
	document.getElementById("sql").value=sql;
//clean up beby field 
 
 if ((document.getElementById("beby").value.indexOf("Where do you want")>=0) || (document.getElementById("beby").value.indexOf("Enter an address")>=0)) {
 		document.getElementById("beby").value='';
	}
	
	
}
  
function clearBeby(choice) {
	if(choice) str="Enter an address";
	else str="Where do you";
	if(document.getElementById('beby').value.indexOf(str)>=0) {
		document.getElementById('beby').value='';
	}
}

function checkBeby() {
 //don't want to pass the "enter a blah, blah if user doesn't enter anything	
 	if ((!document.getElementById('beby').value.indexOf('Where do you')) || (document.getElementById('beby').value.length < 2)) {
  		document.getElementById('beby').value = '';
	} else {
 		document.getElementById('zipcode').value='';
		document.getElementById('city').selectedIndex=0;
		document.getElementById('cityz').value='';
 //if user has not selected latlon with tab - need to get it
	}
 	return true;
}

function rtrim(str) {
	for (var i=str.length-1;str.charAt(i)==" ";i--)
		str = str.substring(0,i);
		
	return str;
}

function addButtons() {
	var ni = document.getElementById("hiddenDiv");
	var newDiv = document.createElement('div');
	newDiv.innerHTML='<span style="position:absolute;bottom:-50px;left:0px;height:100px"><input type=button style="height:30px;font-weight:bold;" value="Correct" onclick="correctLocation();">&nbsp;&nbsp;&nbsp;<input type=button style="height:30px;font-weight:bold;" value="Not Correct" onclick="incorrectLocation();"></span>';
	ni.appendChild(newDiv);
  }

  function maps() {
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = 'images/purple-pushpin.png';
                
// Set up our GMarkerOptions object
	markerOptions = { icon:blueIcon };
	  
	  i=latlon.indexOf(",");
	  lat = parseFloat(latlon.substr(0,i-1));
	  lon = parseFloat(latlon.substr(i+1,latlon.length-1));
	  /*
 	  	  lat=27.9465;
	  lon=-82.459269;
	*/
      if (GBrowserIsCompatible()) {
		 document.getElementById('hiddenDiv').style.height = 290;
		 document.getElementById('hiddenDiv').style.width = 290;
		 document.getElementById('hiddenDiv').style.borderColor = "#a00";
		 document.getElementById('hiddenDiv').style.borderWidth = 2;
		 document.getElementById('hiddenDiv').style.display = "block";
				document.getElementById('hiddenDiv').style.border =  '3px solid red';
		 
		if (navigator.appName.indexOf("Microsoft") == -1) { 		
				document.getElementById('hiddenDiv').style.marginRight = 0;
		} else {
				document.getElementById('hiddenDiv').style.marginRight = 10;
		}
		document.getElementById('hiddenDiv').style.marginBottom = 10;
		var map = new GMap2(document.getElementById("hiddenDiv"), new GSize(250,250));
				 
		var point = new GLatLng(lat,lon);
		map.setCenter(point, 12);
				map.setUIToDefault();
		
		var marker = new GMarker(new GLatLng(lat,lon));
		//map.addOverlay(marker,markerOptions);
		map.addOverlay(new GMarker(new GLatLng(lat,lon), markerOptions));
       }
    }
	
function correctLocation() {
	if(!document.getElementById("within").selectedIndex)
		document.getElementById('within').value=5;
	document.getElementById('zipcode').disabled=true;
	document.getElementById('city').disabled=true;
	document.getElementById('county').disabled=true;
	document.getElementById('citysub').disabled=true;
	document.getElementById('order').selectedIndex=9;
	document.getElementById('listmap').value="mapview";
	document.getElementById('hiddenDiv').style.display = "none"; 
	document.getElementById('type').focus();
	document.getElementById('nearby').style.display="none";
	
}

function incorrectLocation() {
 	document.getElementById('within').value=5;
	document.getElementById('zipcode').disabled=false;
	document.getElementById('city').disabled=false;
	document.getElementById('county').disabled=false;
	document.getElementById('citysub').disabled=false;
	document.getElementById('order').selectedIndex=1;
	document.getElementById('listmap').value="listview";
	document.getElementById('hiddenDiv').style.display = "none"; 	
	document.getElementById('beby').focus();
//document.getElementById('nearby').style.display="block";
}


function checkZip() {
	zip = document.getElementById('zipcode').value;
	if(zip.length=5) {
		if(IsNumeric(zip)) {
 			document.getElementById('beby').value = '';
			document.getElementById('within').selectedIndex = 0;
			document.getElementById('city').selectedIndex = 0;
			document.getElementById('cityz').value = '';
		} else {
			document.getElementById('zipcode').value='';
			return false;
		}
	} else  {
		document.getElementById('zipcode').value='';
		return false;
	}
}



function loadingNew() {
	document.getElementById("landing").innerHTML = "<img src=images/ajax.gif>";
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

 
function openCalc(PropertyPrice){
window.open('calculator.php?price='+ PropertyPrice,'Calculator','width=350,height=250');
}

/*
function showImg(img) {
	document.getElementById("main_pic").innerHTML = "<a href=" + img + " rel='lightbox[roadtrip]'><img src=" + img + " height=270 width=350></img></a>";
	
}
*/

function updateName() { 
str = $$('#MB_content input:checked').pluck('value');
bigcity=str.join(",");
adv=str.join(",");
doChange('multiple',1000);
document.getElementById("cityz").value=bigcity;
} 



function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
  
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function checkLeft() {
/*
  for (i=0;i<3;i++) {	
    if(document.sel.results[i].checked) {
		results = i;
		break;
 	}
  }
 */ 
		
   if(document.sel.name.value == "") {
	  alert( "Please enter your name.");
	  document.sel.name.focus();
	  return false;
  }
  else if (document.sel.email.value == "") {
    alert( "Please enter your email address." );
    document.sel.email.focus();
    return false ;
  }
  else if ((document.sel.email.value.indexOf(".") < 2) || (!document.sel.email.value.indexOf("@"))) {
    alert('Please enter a valid email address.');
    document.sel.email.focus();
    return false;
  }

  if (document.sel.password.value.length < 5) {
	  alert('Please create a password of at least 5 characters.');
	  document.sel.password.focus();
	  return false;
  }
  else if (document.sel.password.value != document.sel.pass2.value) {
	alert('Password entries do not match.');
	document.sel.password.focus();
	return false;
  }
  
  if (document.sel.searchName.value.length < 1) {
    alert('Please enter a name for this search.');
	document.sel.searchName.focus();
	return false;
  }
  /*
  else if(!document.getElementById("moving").selectedIndex) {
	  alert("Please tell us your moving time frame.");
	  document.sel.moving.focus();
	  return false;
  }
  */
  
//	document.getElementById("lresult").innerHTML="<img src='modalbox/spinner.gif' border=0><br />Creating account...<br />this may take a minute.";
	document.getElementById("lresult").style.display="block";

  return true;

}



function clearAll(home) {
	
//	document.getElementById("mydiv").style.display = "none";
	if(home) {
			document.getElementById("beby").value = "Enter an address, neighborhood or point of interest to beBY";
			document.getElementById("name").value = "";
			document.getElementById("email").value = "";
			document.getElementById("comments").value = "";
			document.getElementById("password").value = "";
			document.getElementById("pass2").value = "";
			document.getElementById("moving").selectedIndex = 0;
			document.getElementById("searchName").value = "";
			document.getElementById('nearby').style.display="block";	
	} else {
			document.getElementById("beby").value = "Where do you want to beBY?";
			document.getElementById('zipcode').disabled=false;
			document.getElementById('city').disabled=false;
			document.getElementById('county').disabled=false;
			document.getElementById('citysub').disabled=false;
			document.getElementById('order').selectedIndex=1;
			document.getElementById('nearby').style.display = "block";
			document.getElementById('beby').disabled=false;
			document.getElementById('within').disabled=false;
			
	}
	document.getElementById("zipcode").value = "";
	document.getElementById("type").selectedIndex=0;
 	document.getElementById("foreclosure").selectedIndex=0;
	document.getElementById("minprice").value=0;
	document.getElementById("maxprice").value=0;
	document.getElementById("beds").value=0;
	document.getElementById("baths").value=0;
	document.getElementById("square").value=0;
	document.getElementById("year").value=0;
	document.getElementById("days").value=0;
	document.getElementById("waterfront").selectedIndex=0;
	document.getElementById("pool").selectedIndex=0;
	document.getElementById("citysub").value = 0;
	document.getElementById("city").value=0;
 	document.getElementById('within').value=5;
	document.getElementById('zipcode').disabled=false;
	document.getElementById('city').disabled=false;
	document.getElementById('county').disabled=false;
	document.getElementById('citysub').disabled=false;
	
}

function doClear(theText) {
     if (theText.value == theText.defaultValue) {
         theText.value = ""
     }
 }
 
function checkModal() {
 	str = $$('#MB_content input').pluck('value');
}


function updateName() { 
str = $$('#MB_content input:checked').pluck('value');
bigcity=str.join(",");
document.getElementById("cityz").value=bigcity;
adv=str.join(",");
doChange('multiple',1000);
 
//alert("char=" + bigcity.charCodeAt(4) + "-" + bigcity.charCodeAt(5));
						
} 

function saveSearch() {
 		getParams(0); 
//if beby - make sure the latlon was gotten
 		if((isBeby) && (!mapChecked)) {
// 			doChange('latlon',0);
           	doChange('latlon',0);
		 	Modalbox.show("saveSearch.php", {title: "Save your search", method: "post", params: Form.serialize("sel"), transitions:false});
		} else {
		 	Modalbox.show("saveSearch.php", {title: "Save your search", method: "post", params: Form.serialize("sel"), transitions:false});
		}		
 }




