﻿
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function validatePrice(obj, obj1, page) {
var cityVal;
var strQuery = "";
var keyVal="";
var minPrice = document.getElementById(obj).value;
var maxPrice = document.getElementById(obj1).value;

if (page == 'home') {

var ddBeds = document.getElementById("drpdwnBeds");
var ddBaths = document.getElementById("drpdwnBaths");
var ddGarages = document.getElementById("drpdwnGarageSpaces");
var chkBuy = document.getElementById("ChkBxBuy");
var chkRent = document.getElementById("ChkBxRent");
var chkShortSale=document.getElementById("chkShortSale");
var chkForeclosure=document.getElementById("chkForeclosures");
var dbedVal, dbathVal, minPriceVal, maxPriceVal;

cityVal = document.getElementById('txtCityHome').value;
cityVal = cityVal.replace("mls ", "");

cityVal=cityVal.replace(/~/g,"").replace(/`/g,"").replace(/!/g,"").replace(/@/g,"").replace(/#/g,"").replace(/\$/g,"").replace(/%/g,"").replace(/\^/g,"").replace(/&/g,"").replace(/\*/g,"").replace(/\(/g,"").replace(/\)/g,"").replace(/\-/g,"").replace(/_/g,"").replace(/\+/g,"").replace(/=/g,"").replace(/{/g,"").replace(/\[/g,"").replace(/}/g,"").replace(/\]/g,"").replace(/\\/g,"").replace(/\|/g,"").replace(/:/g,"").replace(/</g,"").replace(/>/g,"").replace(/\./g,"").replace(/\?/g,"").replace(/\//g,"").replace(/\'/g,"").replace(/\"/g,"").replace(/\\/g,"");
cityVal=cityVal.trim();

keyVal = document.getElementById('txtKeyword').value;
keyVal = keyVal.replace("mls ", "");

keyVal=keyVal.replace(/~/g,"").replace(/`/g,"").replace(/!/g,"").replace(/@/g,"").replace(/#/g,"").replace(/\$/g,"").replace(/%/g,"").replace(/\^/g,"").replace(/&/g,"").replace(/\*/g,"").replace(/\(/g,"").replace(/\)/g,"").replace(/\-/g,"").replace(/_/g,"").replace(/\+/g,"").replace(/=/g,"").replace(/{/g,"").replace(/\[/g,"").replace(/}/g,"").replace(/\]/g,"").replace(/\\/g,"").replace(/\|/g,"").replace(/:/g,"").replace(/</g,"").replace(/>/g,"").replace(/\./g,"").replace(/\?/g,"").replace(/\//g,"").replace(/\'/g,"").replace(/\"/g,"").replace(/\\/g,"");
keyVal=keyVal.trim();

var cityArr1 = cityVal.split(";");
var cityWithoutStateAbbr = "";
var count = 0;
var strZipcodes = "";
var strCity = "";
var strMLSID = "";
}
if (minPrice != "") {
minPrice = minPrice.replace(",", "");
minPrice = minPrice.replace("$", "");
minPrice = ltrim(minPrice);
document.getElementById(obj).value=minPrice;
if (isNaN(minPrice)) {
alert("Please only enter a numerical value for the minimum price");
document.getElementById(obj).focus();
return false;
}
if (minPrice.indexOf('.') > 0) {
alert("Please only enter a numerical value for the minimum price");
document.getElementById(obj).focus();
return false;
}
}
if (maxPrice != "") {
maxPrice = maxPrice.replace(",", "");
maxPrice = maxPrice.replace("$", "");
maxPrice = ltrim(maxPrice);
document.getElementById(obj1).value=maxPrice;
if (isNaN(maxPrice)) {
alert("Please only enter a numerical value for the maximum price");
document.getElementById(obj1).focus();
return false;
}
if (maxPrice.indexOf('.') > 0) {
alert("Please only enter a numerical value for the maximum price");
document.getElementById(obj1).focus();
return false;
}
}

if (page == 'home') {
if(cityVal.toLowerCase() == "address, city, neighborhood, street, zip code, or mls id" || cityVal.toLowerCase() == "")
{
alert("Please enter an Address, City, Neighborhood, Street, Zip Code, or MLS ID into the location textbox");
document.getElementById('txtCityHome').focus();
return false;
}

if (cityVal.toLowerCase() != "address, city, neighborhood, street, zip code, or mls id") {
var cityArr = strCity.split(",")
var MlsIdArr = strMLSID.split(",")
if (cityVal != "") {
//if (cityArr.length > 1) {
    
    cityVal = cityVal.toLowerCase().replace("mls ", "");
    cityVal = cityVal.toLowerCase().replace(" mls", "");
    cityVal = cityVal.toLowerCase().replace("fl ", "");
    cityVal = cityVal.toLowerCase().replace(" fl", "");
    cityVal = cityVal.toLowerCase().replace(/ /g, "-");
    cityVal = cityVal.toLowerCase().replace(/,-/g, ",");
    cityVal = cityVal.toLowerCase().replace(/-,/g, ",");
    
    strQuery = cityVal + "-loc/";

}
}

if(keyVal.toLowerCase() !='search by keywords' &&  keyVal!='')
{
keyVal = keyVal.toLowerCase().replace("mls ", "");
keyVal = keyVal.toLowerCase().replace(" mls", "");
keyVal = keyVal.toLowerCase().replace("fl ", "");
keyVal = keyVal.toLowerCase().replace(" fl", "");
keyVal = keyVal.toLowerCase().replace(/ /g, "-");
keyVal = keyVal.toLowerCase().replace(/,-/g, ",");
keyVal = keyVal.toLowerCase().replace(/-,/g, ",");

strQuery=strQuery+keyVal+"-kw/";
}

if (chkBuy.checked == true && chkRent.checked == true)
strQuery = strQuery + "y-buy/y-rent/";
else if (chkBuy.checked == true)
strQuery = strQuery + "y-buy/";
else if (chkRent.checked == true)
strQuery = strQuery + "y-rent/";

minPriceVal = minPrice
maxPriceVal = maxPrice

if (minPrice != "" && maxPrice != "") {
minPriceVal = minPriceVal.replace(/,/g, "").replace("$", "");
maxPriceVal = maxPriceVal.replace(/,/g, "").replace("$", "");
strQuery = strQuery + minPriceVal + "-" + maxPriceVal + "-price/";
}
else if (maxPrice != "" && minPrice == "") {
maxPriceVal = maxPriceVal.replace(/,/g, "").replace("$", "");
strQuery = strQuery + "0-" + maxPriceVal + "-price/";
}
else if (minPrice != "" && maxPrice == "") {
minPriceVal = minPrice.replace(/,/g, "").replace("$", "");
strQuery = strQuery + minPriceVal + "-price/";
}

dbedVal = ddBeds.options[ddBeds.selectedIndex].value.replace("+", "");
dbathVal = ddBaths.options[ddBaths.selectedIndex].value.replace("+", "");
dgarVal = ddGarages.options[ddGarages.selectedIndex].value.replace("+", "");
if (dbedVal > 0)
strQuery = strQuery + dbedVal + "-beds/";
if (dbathVal > 0)
strQuery = strQuery + dbathVal + "-baths/";
if (dgarVal > 0)
strQuery = strQuery + dgarVal + "-carg/";
if(chkShortSale.checked==true)
{
strQuery = strQuery + "y-nss/";
}
if(chkForeclosure.checked==true)
{
strQuery = strQuery + "y-reo/";
}

location.href = "/fl/" + strQuery;
return false;
}
else
return true;
}

function ltrim(s)
{
	var l = 0;
	while (l < s.length  && s.charAt(l) == " ")
	{	l++; }
	return s.substring(l, s.length);
}


function ReplaceAll(Source, stringToFind, stringToReplace) {

var temp = Source;

var index = temp.indexOf(stringToFind);

while (index != -1) {

temp = temp.replace(stringToFind, stringToReplace);

index = temp.indexOf(stringToFind);

}

return temp;

}
function checkSearchBox(txtbox) {
//alert(document.getElementById(txtbox.id).value.length);
if (txtbox.value != '' && document.getElementById(txtbox.id).value.length>=1) {
        document.getElementById("divMainSearchBlock").style.display = 'none';
    }
}
function OpenMainSearchBlock(txtbox)
{
document.getElementById("txtCityHome").style.borderColor="#4D90FE";
if(txtbox.value=='Address, City, Neighborhood, Street, Zip Code, or MLS ID')
txtbox.value='';
document.getElementById("divMainSearchBlock").style.display='inline';
return false;
}
function CloseMainSearchBlock(txtbox)
{debugger;
//$('#txtCityHome').focusout();
document.getElementById("txtCityHome").style.borderColor="#9B8E5C";
if(txtbox.value=='')
txtbox.value='Address, City, Neighborhood, Street, Zip Code, or MLS ID';
document.getElementById("divMainSearchBlock").style.display='none';
}
function OpenKeywordSearchBlock(txtbox)
{
if(txtbox.value=='Search by keywords')
txtbox.value='';
document.getElementById("divKeywordSearchBlock").style.display='inline';
return false;
}
function CloseKeywordSearchBlock(txtbox)
{
if(txtbox.value=='')
txtbox.value='Search by keywords';
document.getElementById("divKeywordSearchBlock").style.display='none';
return false;
}
$(document).click(function (e) {

        if ($(e.target).is('.ac_results')) {
            //alert("dropcheck");
        }
        else {
            $('.ac_results').hide();
	
        }

    });
/* COOKIES OBJECT */
var Cookies = {
    // Initialize by splitting the array of Cookies
    init: function () {
        var allCookies = document.cookie.split('; ');
        for (var i = 0; i < allCookies.length; i++) {
            var cookiePair = allCookies[i].split('=');
            this[cookiePair[0]] = cookiePair[1];
        }
    },
    // Create Function: Pass name of cookie, value, and days to expire
    create: function (name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
        this[name] = value;
    },
    // Erase cookie by name
    erase: function (name) {
        this.create(name, '', -1);
        this[name] = undefined;
    }
};
Cookies.init();
