﻿///<reference path="~/MapSearch.aspx" />
/// <reference path="~/javascriptIntellissense/VeJavaScriptIntellisenseHelper.js" />

var map;
var maptag = null;
var selectedregion = $get("ctl00_content_ddRegion");
var lat_value = null;
var lon_value = null;
if (typeof (DESIGN_TIME) == 'undefined') {
    map = null;
}
else {
    map = new VEMap();
}
function pageLoad() {

    if (maptag != 'serverdata') {
        map = new VEMap('myMap');
        var selectedregion = $get("ctl00_content_ddRegion");
        if (selectedregion.value == "n") {
            lat_value = '29.9544444';
            lon_value = '-90.075';
        }
        else {
            lat_value = '30.4505556';
            lon_value = '-91.1544444';
        }
        map.LoadMap(new VELatLong(lat_value, lon_value));
        map.SetZoomLevel(10);
    }
}
function CallWebService() {
    var selectedregion = $get("ctl00_content_ddRegion");
    var isNewOleans = false;
    if (selectedregion.value == "n") {
        isNewOleans = true;
    } else { isNewOleans = false; }
    var selZip = $get("ctl00_content_txtZipCode");
    var selBeds = $get("ctl00_content_ddBeds");
    var selBaths = $get("ctl00_content_ddBaths");
    var selPriceFrom = $get("ctl00_content_ddPriceFrom");
    var selPriceTo = $get("ctl00_content_ddPriceTo");
    var selSubdivision = $get('ctl00_content_txtSubdivision');
    var mlsNumber = $get('ctl00_content_txtMlsNumber');
    var street = $get('ctl00_content_txtStreet');
    var citynamelist = GetCheckedValues(); //$get('ctl00_content_MultiSelectDropDown1_hfCityNames').value;
    var city = $get('ctl00_content_MultiSelectDropDown1_hfCities').value;
    checkPropertyType();
    AdditionalFeatures();
    var c = document.getElementById('ctl00_content_hfFeatureType').value;
    var propertytype = document.getElementById('ctl00_content_hfPropertytype').value;
    var propertytype2 = document.getElementById('ctl00_content_hfPropertytype').value;
    PropertyService.GetPropertiesByZip(mlsNumber.value, street.value, city, selZip.value, selBeds.value, selBaths.value, selPriceFrom.value, selPriceTo.value, selSubdivision.value, propertytype, c, "", true, isNewOleans, citynamelist, OnGetPropertiesByZipComplete);
    maptag = 'serverdata';
    return true;
}
function GetCheckedValues() {
    var elements = document.getElementById('ctl00_content_MultiSelectDropDown1' + '_' + 'divMain').getElementsByTagName("input");
    var mycount = elements.length;
    var returnString = '';
    for (i = 0; i < mycount; i++) {
        if (elements[i].checked)
            returnString += elements[i].title + ',';
    }
    return returnString;
}
function EndGetData(arg) {
    document.getElementById("grid").innerHTML = arg;
}
function OnGetPropertiesByZipComplete(mlsproperties) {
    var shp;
    var arrProperties = new Array;
    var curLatLong;
    var infoBox = null;
    var mnl = false;

    map.DeleteAllShapes();
    for (var i = 0; i < mlsproperties.length; i++) {
        var id = mlsproperties[i].Id;
        curLatLong = new VELatLong(mlsproperties[i].Latitude, mlsproperties[i].Longitude);
        arrProperties.push(curLatLong);
        shp = new VEShape(VEShapeType.Pushpin, curLatLong);
        if (id.match("MNL")) { id = ""; } else { id = "MLS#: " + mlsproperties[i].Id + "<BR />" }

        shp.SetTitle(id + mlsproperties[i].address + "<BR />" + mlsproperties[i].City + ', ' + mlsproperties[i].State);
        if (ProcessStringState(mlsproperties[i].picture)) {
            infoBox = "<img src=" + "pictures/" + mlsproperties[i].picture + " width='240' height='180' />";
        }
        else {
            infoBox = "<img src=" + "pictures/none2.jpg" + " width='240px' height='190px' />";
        }
        infoBox += "<p><b>Price: $</b> " + mlsproperties[i].price + " ";
        infoBox += "<b>Beds:</b> " + mlsproperties[i].bedrooms + " ";
        infoBox += "<b>Baths:</b> " + mlsproperties[i].bathrooms + "</p>";
        if (ProcessStringState(mlsproperties[i].Description)) {
            infoBox += "<p><B>Description</B><br />";
        }
        infoBox += mlsproperties[i].Description + "</p>";
        infoBox += "<p><a href=" + 'Details.aspx?id=' + mlsproperties[i].Id + "&t=" + mlsproperties[i].type + "&m=t" + " target='_blank'>" + "Click here to see more details</a></p>"


        shp.SetDescription(infoBox);
        map.AddShape(shp);
    }
    if (arrProperties.length > 0) {
        map.SetMapView(arrProperties);
        if (arrProperties.length > 1) {
            document.getElementById('ctl00_content_lblNumberOfProperties').innerHTML = arrProperties.length + " properties were found.";
        }
        else {
            document.getElementById('ctl00_content_lblNumberOfProperties').innerHTML = arrProperties.length + " property was found.";
        }
    }
    else {
        document.getElementById('ctl00_content_lblNumberOfProperties').innerHTML = "No properties were found.";
        map.SetZoomLevel(10);
    }
}
function CustomPushpin() {
    var shape = new VEShape(VEShapeType.Pushpin, map.GetCenter())
    shape.SetCustomIcon(targetImage);
    shape.SetTitle("Custom Icon");
    shape.SetDescription("any html");
    map.AddShape(shape);
    map.Pan(200, 0);
}
function ProcessStringState(str) {
    if (str == null || str.replace(/\s/g, "") == "") {
        return false;
    }
    else {
        return true;
    }
}
function iterateCheckBoxList(checkBoxListId) {
    var elementRef = document.getElementById(checkBoxListId);
    var checkBoxArray = elementRef.getElementsByTagName('input');
    var checkedValues = '';
    alert('List length: ' + checkBoxArray.length);
    for (var i = 0; i < checkBoxArray.length; i++) {
        var checkBoxRef = checkBoxArray[i];
        if (checkBoxRef.checked == true) {
            var labelArray = checkBoxRef.parentNode.getElementsByTagName('label');
            if (labelArray.length > 0) {
                if (checkedValues.length > 0)
                    checkedValues += ', ';
                checkedValues += labelArray[0].innerHTML;
            }
        }
    }
    alert('Items checked: ' + checkedValues);
    return checkedValues;
}
function ProcessRadioButton() {
    var radioButtons = document.getElementById("ctl00_content_rbIsFurnished");
    for (var x = 0; x < radioButtons.length; x++) {
        if (radioButtons[x].checked) {
            document.getElementById('ctl00_content_hfIsFurnished').value = radioButtons[x].value;
        }
    }
}
function GetPoolPetFriendly() {
    var pool = document.getElementsByName("ctl00_content_chPool");
    var petfriendly = document.getElementsByName("ctl00_content_chPetFriendly");
    var furnished = document.getElementsByName("ctl00_content_chkFurnished");
    var unfurnished = document.getElementsByName("ctl00_content_chkUnfurnished");
    if (pool.checked == 1)
        document.getElementById('ctl00_content_hfPool').value = true;
    if (petfriendly.checked == 1)
        document.getElementById('ctl00_content_hfPetFriendly').value = 'true';
    if (furnished.checked == 1)
        document.getElementById('ctl00_content_hfFurnished').value = true;
    if (unfurnished.checked == 1)
        document.getElementById('ctl00_content_hfUnfurnished').value = true;
}
function GetRadioButtonValue() {
    var radio = document.getElementById('ctl00_content_rbIsFurnished');
    for (var j = 0; j < 3; j++) {
        if (radio[j].checked) {
            document.getElementById('ctl00_content_hfIsFurnished').value = radio[j].value;
        }
    }
}
function checkFupp() {
    document.getElementById('ctl00_content_hfFupp').value = null;
    document.getElementById('ctl00_content_hfPoolChecked').value = false;
    document.getElementById('ctl00_content_hfPetChecked').value = false;
    document.getElementById('ctl00_content_hfFurnishedChecked').value = false;
    document.getElementById('ctl00_content_hfUnfurnishedChecked').value = false;
    var tablefupp = document.getElementById('ctl00_content_chkfupp');
    var cfupp = tablefupp.getElementsByTagName("input");
    var w = 0;
    var fuppArray = new Array();
    var fuppString = null;
    for (var i = 0; i < cfupp.length; i++) {
        if (cfupp[i].checked) {
            fuppArray.push(cfupp[i].value);
            if (cfupp[i].value == 'pool')
                document.getElementById('ctl00_content_hfPoolChecked').value = cfupp[i].checked;
            if (cfupp[i].value == 'pet')
                document.getElementById('ctl00_content_hfPetChecked').value = cfupp[i].checked;
            if (cfupp[i].value == 'furnished')
                document.getElementById('ctl00_content_hfFurnishedChecked').value = cfupp[i].checked;
            if (cfupp[i].value == 'unfurnished')
                document.getElementById('ctl00_content_hfUnfurnishedChecked').value = cfupp[i].checked;
            w++;
        }
    }
    if (fuppArray.length > 1) {
        document.getElementById('ctl00_content_hfFupp').value = fuppArray.join(',');
    }
    else if (fuppArray.length == 1) {
        document.getElementById('ctl00_content_hfFupp').value = fuppArray;
    }
}
function AdditionalFeatures() {
    var FeaturesArray = new Array();
    var checkboxArray = document.getElementById("ctl00_content_chkfupp").getElementsByTagName("input");
    for (var i in checkboxArray) {
        if (checkboxArray[i].checked) {
            var sFeatureType = checkboxArray[i].parentNode.childNodes[1].innerHTML;
            if (sFeatureType == "Furnished" || sFeatureType == "Unfurnished" || sFeatureType == "Pool" || sFeatureType == "Pet Friendly") {
                FeaturesArray.push(sFeatureType.split(' ')[0].toLowerCase());
            }
            else {
                alert('No Match');
            }
        }
    }
    document.getElementById('ctl00_content_hfFeatureType').value = (FeaturesArray.length > 1) ? FeaturesArray.join(',') : FeaturesArray;
}
function checkPropertyType() {
    var residentialArray = new Array();
    var tbody = null;
    tbody = document.getElementById("ctl00_content_chkPropertyType").getElementsByTagName("input");
    var residentialString = null;
    for (var i in tbody) {
        if (tbody[i].checked) {
            var td = tbody[i].parentNode.childNodes[1].innerHTML;
            switch (td) {
                case "Single Family Home":
                    residentialArray.push('SGL')
                    break;
                case "Townhouse":
                    residentialArray.push('TNH')
                    break;
                case "Condominium":
                    residentialArray.push('CND')
                    break;
                case "Apartment Community":
                    residentialArray.push('APT');
                    break;
                case "Duplex / Double":
                    residentialArray.push('DUP');
                    break;
                case "3Plex":
                    residentialArray.push('3PL');
                    break;
                case "4Plex":
                    residentialArray.push('4PL');
                    break;
                default: alert('No Match');
            }
        }
    }
    document.getElementById('ctl00_content_hfPropertytype').value = (residentialArray.length > 1) ? residentialArray.join(',') : residentialArray;
}
function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}




function SHMulSel(ControlClientID, e) {
    var textBoxMain = window.document.getElementById(ControlClientID + '_' + 'tbm');
    var divisionMain = window.document.getElementById(ControlClientID + '_' + 'divMain');

    var displayStatus = divisionMain.style.display;
    if (displayStatus == 'block') {
        divisionMain.style.display = 'none';
        if (window.document.getElementById(ControlClientID + '_' + 'hapb').value == 'True') {
        }
    }
    else {
        divisionMain.style.display = 'block';
    }
    var evt = (window.event == null) ? e : window.event;
    evt.cancelBubble = true;
}

function DisplayTitle(control) {
    control.title = control.value;
}
function CheckValues(ControlClientID) {
    alert('check values');
    var control = ControlClientID + '_' + 'hsiv';
    var parentControl = window.document.getElementById(control);
}

function ChangeColor(color, evt) {
    var ele;

    if (window.event != null)
        ele = window.event.srcElement;
    else
        ele = evt.target;

    if (ele.className == 'MultiselectListItemLabel')
        ele = ele.parentElement == null ? ele.parentNode : ele.parentElement;

    if (ele.className == 'MultiselectListItem')
        ele.style.backgroundColor = color;
}

function HideSelectionList(ControlClientID) {
    alert('hello' + ControlClientID);
    var displayStatus = window.document.getElementById(ControlClientID + '_' + 'divMain').style.display;
    if (displayStatus == 'block') {
        window.document.getElementById(ControlClientID + '_' + 'divMain').style.display = 'none';
    }
    else {
        window.document.getElementById(ControlClientID + '_' + 'divMain').style.display = 'block';
    }
}

function HideOnFocusLost(ControlClientID) {
    var divCollection = new Array(ControlClientID + "_ddvs_divMain", ControlClientID + "_ddbu_divMain", ControlClientID + "_dddv_divMain", ControlClientID + "_dddt_divMain", ControlClientID + "_ddsec_divMain", ControlClientID + "_ddcy_divMain", ControlClientID + "_ddcnt_divMain", ControlClientID + "_ddusr_divMain", ControlClientID + "_ddjg_divMain");
    for (index = 0; index < divCollection.length; index++) {
        var divisionMain = document.getElementById(divCollection[index]);
        if (divisionMain != null && divisionMain.style.display == 'block')
            divisionMain.style.display = 'none';
    }
}

