﻿function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}
function ltrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/, "");
}
function rtrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/, "");
}

var SearchTool;
var count = 1;
var scrollprop = 0;
var timeout;
var toClear = 0;
var focusAddress = "";
var focusSqft = '';
function CashflowSearch() {
    var Address = document.getElementById("cashflowaddress").value;
    if (Address == '' || Address == 'Chicago, IL or 94523') {
        document.getElementById("searchspan").style.display = "block";
        return false;
    }


    var measure = document.getElementsByName("rbcolum");
    var ab;
    for (var i = 0; i < measure.length; i++) {
        if (measure[i].checked) {
            ab = measure[i].value;
        }
        var selcrit;
        if (ab == 19)
            selcrit = "&filter.minAskingPriceDiscount=10&filter.maxAskingPriceDiscount=50";
        else if (ab == 14)
            selcrit = "&filter.minCFBT=0";
        else
            selcrit = "&filter.minCapRate=4";
    }

    window.location = "/Search/Search?searchType=search&filter.location=" + EncodeAddress(Address) + "&filter.SortOrders=DESC&filter.MeasureType.MeasureId=" + ab + selcrit;
}
function divSearch(div1, div2, rbchange) {

    document.getElementById(div1).style.display = "block";
    document.getElementById(div2).style.display = "none";
    if (rbchange != "no") {
        document.getElementById(rbchange).checked = "checked";
    }


}
function setFocusValue() {
    focusAddress = trim(document.getElementById("address").value);

}
function setFocusSqftValue() {
    focusSqft = trim(document.getElementById("sqftarea").value);

}
function scrollleft() {
    document.getElementById("prop" + count).style.display = "none";
    count = count + 1;
    if (count != 11) {
        scrollprop = 1;

        //alert(document.getElementById("tbl").left);
        //document.getElementById("tbl").style.left=parseInt(document.getElementById("tbl").style.left)-100;
        //document.getElementById("prop" + count).style.display = "block";
        $("#prop" + count).fadeIn("slow");
    }
    else {
        //document.getElementById("prop1").style.display = "block"
        $("#prop1").fadeIn("slow");
        count = 1;
    }
    //alert(document.getElementById("tbl").left);
}

function scrollright() {

    document.getElementById("prop" + count).style.display = "none";
    count = count - 1;
    scrollprop = 1;
    if (count != 0) {

        //alert(document.getElementById("tbl").left);
        //document.getElementById("tbl").style.left=parseInt(document.getElementById("tbl").style.left)-100;
        //document.getElementById("prop" + count).style.display = "block";
        $("#prop" + count).fadeIn("slow");
    }
    else {
        // document.getElementById("prop10").style.display = "block"
        $("#prop10").fadeIn("slow");
        count = 10;
    }
}
function scrollrightload() {
    if (scrollprop == 1)
        return false;
    document.getElementById("prop" + count).style.display = "none";
    count = count - 1;
    if (count != 0) {

        //alert(document.getElementById("tbl").left);
        //document.getElementById("tbl").style.left=parseInt(document.getElementById("tbl").style.left)-100;
        //document.getElementById("prop" + count).style.display = "block";
        $("#prop" + count).fadeIn("slow");
    }
    else {
        // document.getElementById("prop10").style.display = "block"
        $("#prop10").fadeIn("slow");
        count = 10;
    }
}
function moretooloption() {
    var s = document.getElementById("moretooloption");
    if (s.style.visibility == "hidden") {
        s.style.visibility = "visible";
    }
    else
        s.style.visibility = "hidden";
}
function URLEncode(plaintext) {
    // The Javascript escape and unescape functions do not correspond
    // with what browsers actually do...
    var SAFECHARS = "0123456789" + 				// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()"; 				// RFC2396 Mark characters
    var HEX = "0123456789ABCDEF";

    var encoded = "";
    for (var i = 0; i < plaintext.length; i++) {
        var ch = plaintext.charAt(i);
        if (ch == " ") {
            encoded += "+"; 			// x-www-urlencoded, rather than %20
        } else if (SAFECHARS.indexOf(ch) != -1) {
            encoded += ch;
        } else {
            var charCode = ch.charCodeAt(0);
            if (charCode > 255) {
                alert("Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted.");
                encoded += "+";
            } else {
                encoded += "%";
                encoded += HEX.charAt((charCode >> 4) & 0xF);
                encoded += HEX.charAt(charCode & 0xF);
            }
        }
    } // for

    return encoded;
};
function clearTools() {
    if (focusAddress != trim(document.getElementById("address").value)) {
        document.getElementById("SelectBedRooms").selectedIndex = 0;
        document.getElementById("SelectBathRooms").selectedIndex = 0;
        document.getElementById('propertytype').selectedIndex = 0;
        document.getElementById('propdetails').style.visibility = 'hidden';
        document.getElementById("dvEst").innerHTML = "";
        document.getElementById("sqftarea").value = "";
        var s = document.getElementById("moretooloption");
        if (s.style.visibility == "visible") {
            s.style.visibility = "hidden";


        } 
    }

}
function setClear() {
    toClear = 1;
}
function redirectreport() {
    var esti = document.getElementById("estimates");
    var proptype = document.getElementById("propertytype");
    var bed = document.getElementById("SelectBedRooms");
    var bath = document.getElementById("SelectBathRooms");
    var sqft = document.getElementById("sqftarea");
    var url = "?";
    var loc = document.getElementById("address").value;
    if (proptype.value != -1) {
        var prop;
        if (proptype.value == 1)
            prop = "sfh"
        if (proptype.value == 2)
            prop = "fh"
        if (proptype.value == 3)
            prop = "condo"
        if (proptype.value == 4)
            prop = "duplex"
        url += "propertytype=" + prop + "&";
    }
    if (bed.value != -1)
        url += "beds=" + bed.value + "&";
    if (bath.value != -1)
        url += "baths=" + bath.value + "&";
    if (sqft.value != '')
        url += "sqft=" + sqft.value + "&";
    var loctrim = loc;
    var locs = loctrim;
    if (esti.value == 1)
        window.location = "/rental-property/For-Rent/" + trim(EncodeAddress(locs)) + url;
    else
        window.location = "/Investment-property/For-Sale/" + trim(EncodeAddress(locs)) + url;

}
function Timeout() {
    var est = document.getElementById("dvEst");
    est.innerHTML = "Unable to estimate";
    document.getElementById('updating1').style.display = 'none'
    document.getElementById('tblEst').style.display = 'block';
}
function hideMessage() {
    document.getElementById("dvEst").innerHTML = "";
    document.getElementById("propdetails").style.visibility = "hidden";

}
function hideMessage1() {
    if (focusSqft != trim(document.getElementById("sqftarea").value)) {
        document.getElementById("dvEst").innerHTML = "";
        document.getElementById("propdetails").style.visibility = "hidden";
    }
}
function Submit() {
    timeout = setTimeout("Timeout()", 15000);
    //    if (SearchTool == 0) {
    //        document.getElementById("upgradetool").style.display = 'block';
    //        return false;
    //    }

    focusAddress = document.getElementById("address").value;
    focusSqft = document.getElementById("sqftarea").value;
    var proptype = document.getElementById("propertytype");
    var s = document.getElementById("moretooloption");
    if (s.style.visibility == "visible") {
        if (proptype.value == -1) {
            alert("Please select property type.")
            return false;
        }
    }
    var est = document.getElementById("dvEst");
    est.innerHTML = '';
    document.getElementById('updating1').style.display = 'block'
    document.getElementById('tblEst').style.display = 'none';

    var loc = document.getElementById("address").value;

    var UR = "/Home/Tool?propertytype=" + document.getElementById("propertytype").value + "&address=" + EncodeAddress(loc) + "&estimates=" + document.getElementById("estimates").value + "&SelectBedRooms=" + document.getElementById("SelectBedRooms").value + "&SelectBathRooms=" + document.getElementById("SelectBathRooms").value + "&sqftarea=" + document.getElementById("sqftarea").value;
    var parent;
    $.ajax({
        type: "GET",
        url: UR,
        cache: false,
        success: function (msg) {
            // alert(timeout);
            clearTimeout(timeout);

            document.getElementById('updating1').style.display = 'none'
            document.getElementById('tblEst').style.display = 'block'
            var esttype = document.getElementById("estimates").value;
            var s = document.getElementById("moretooloption");
            s.style.visibility = "visible";
            if (msg == "Unable to estimate") {
                est.innerHTML = msg;
                document.getElementById("propdetails").style.visibility = "hidden";
                if (document.getElementById('propertytype').selectedIndex == 0)
                    est.innerHTML = "Unknown property type";
                if (document.getElementById('SelectBedRooms').selectedIndex == 0)
                    est.innerHTML = "Unknown beds";
                if (document.getElementById('propertytype').selectedIndex == 0 && document.getElementById('SelectBedRooms').selectedIndex == 0)
                    est.innerHTML = "Unknown property type/beds";

            }
            else {
                document.getElementById("propdetails").style.visibility = "visible";
                var colarray = msg.split("/");
                for (i = 0; i < document.getElementById('SelectBedRooms').length; i++) {
                    if (document.getElementById('SelectBedRooms').options[i].value == colarray[1]) {
                        document.getElementById('SelectBedRooms').selectedIndex = i
                    }
                }

                for (i = 0; i < document.getElementById('SelectBathRooms').length; i++) {
                    if (document.getElementById('SelectBathRooms').options[i].value == colarray[2]) {
                        document.getElementById('SelectBathRooms').selectedIndex = i
                    }
                }

                for (i = 0; i < document.getElementById('propertytype').length; i++) {
                    if (document.getElementById('propertytype').options[i].value == colarray[3]) {
                        document.getElementById('propertytype').selectedIndex = i
                    }
                }
                if (colarray[4] != 0)
                    document.getElementById("sqftarea").value = colarray[4];
                if (esttype == "1") {
                    est.innerHTML = "Estimated Rent: " + colarray[0];
                }
                else {



                    //                    for (var i = 0; i < colarray.length; i++) {
                    //                        alert(colarray[i]);
                    //                    }
                    est.innerHTML = "Estimated Price: " + colarray[0];
                }
            }



            //  GetChild(msg);
        }

    });

}

var idname = "cf";
function checking() {
    document.getElementById('res').style.visibility = 'hidden';
}
function checked() {
    removeallselect();
    idname = "di";
    document.getElementById('imgdi').src = "/Content/Images/Home/DiscYell.png";
    document.getElementById('res').style.visibility = 'visible';
}
function checkedcf() {
    removeallselect();
    idname = "cf";
    document.getElementById('imgcf').src = "/Content/Images/Home/CashYell.png";
    document.getElementById('res').style.visibility = 'visible';
}
function checkedfe() {
    removeallselect();
    idname = "fe";
    document.getElementById('imgfe').src = "/Content/Images/Home/FeYell.png";
    document.getElementById('res').style.visibility = 'visible';
}
function removeallselect() {
    document.getElementById('imgdi').src = "/Content/Images/Home/tab_di.png";
    document.getElementById('imgcf').src = "/Content/Images/Home/tab_cf.png";
    document.getElementById('imgfe').src = "/Content/Images/Home/tab_fe.png";
}
function switchcolor(colno, id) {
    if (idname != id) {
        if (id == 'di') {

            if (colno == 2)
                document.getElementById('imgdi').src = "/Content/Images/Home/DiscYell.png";
            else
                document.getElementById('imgdi').src = "/Content/Images/Home/tab_di.png";
        }
    }
    if (idname != id) {
        if (id == 'cf') {

            if (colno == 2)
                document.getElementById('imgcf').src = "/Content/Images/Home/CashYell.png";
            else
                document.getElementById('imgcf').src = "/Content/Images/Home/tab_cf.png";
        }
    }
    if (idname != id) {
        if (id == 'fe') {

            if (colno == 2)
                document.getElementById('imgfe').src = "/Content/Images/Home/FeYell.png";
            else

                document.getElementById('imgfe').src = "/Content/Images/Home/tab_fe.png";
        }
    }
}

function Search() {

    var Address = trim(document.getElementById("PropertyAddress").value);

    if (Address != '' && Address != "'" + '100 N Whisman Rd, Mountain View, CA' + "'")
        window.location = "/investment-property/For-Sale/" + EncodeAddress(Address);
    else {
        document.getElementById("invtMess").style.display = "block";
        document.getElementById("invtMess").innerHTML = "Please enter address";
    }



}
function checkingreshotdeal() {
    document.getElementById('reshotdeal').style.visibility = 'hidden';

}



function checkedreshotdeal() {
    document.getElementById('reshotdeal').style.visibility = 'visible';

}

function checkingurl() {

    document.getElementById('resurl').style.visibility = 'hidden';
    // $("#res").html('');
}



function checkedurl() {
    document.getElementById('resurl').style.visibility = 'visible';


}
function rssredirect() {
    window.location = "http://feeds.feedburner.com/FinestExpert";
}
