﻿//var eppraisal_homevalue_base_url = "http://localhost:12345";
var eppraisal_homevalue_base_url = "http://www.eppraisal.com";

window.onload
{
    if (typeof (jQuery) == 'undefined') {
        if (document.getElementById('jquery_1_3_2') == null) {
            with (document.getElementsByTagName("head")[0].appendChild(document.createElement("script"))) {
                setAttribute("id", "jquery_1_3_2", 0);
                setAttribute("type", "text/javascript", 0);
                setAttribute("src", eppraisal_homevalue_base_url + '/scripts/jquery-1.3.2.min.js', 0);
            }
        }
    }
    if (document.getElementById('eppraisal_widget_css') == null) {
        with (document.getElementsByTagName("head")[0].appendChild(document.createElement("link"))) {
            setAttribute("id", "eppraisal_widget_css", 0);
            setAttribute("type", "text/css", 0);
            setAttribute("rel", "stylesheet", 0);
            setAttribute("href", eppraisal_homevalue_base_url + '/css/widgets.css', 0);
        }
    }
    if (document.getElementById('eppraisal_fusion_charts') == null) {
        with (document.getElementsByTagName("head")[0].appendChild(document.createElement("script"))) {
            setAttribute("id", "eppraisal_fusion_charts", 0);
            setAttribute("type", "text/javascript", 0);
            setAttribute("src", eppraisal_homevalue_base_url + '/FusionCharts/JSClass/FusionCharts.js', 0);
        }
    }
}
function eppraisal_homevalue_showWidget() {
    if ($('#eppraisalHomeValueWidget').exists()) {
        $('#eppraisalHomeValueWidget')
            .css('background-color', '#ffffff')
            .css('border', '2px solid #0298d0')
            .css('padding', '10px')
            .css('width', '300px')
            .css('text-align', 'center')
            .show();
    } else {
        document.write('Unable to load widget.  Missing element: "eppraisalHomeValueWidget"');
    }
}
function eppraisal_homevalue_showSearch(address, cityState) {
    var url = eppraisal_homevalue_base_url + '/widgets/homevaluesearch';
    if (typeof (cityState) != 'undefined' && cityState != '') {
        url += '/' + encodeURIComponent(address) + '/' + encodeURIComponent(cityState) + '.mvc?callback=?';
    } else {
        url += '.mvc?callback=?';
    }
    try {
        $('#eppraisalHomeValueWidgetContent').hide();
        $.getJSON(url, null, function(data) {
            $("#eppraisalHomeValueWidgetContent").html(data.html);
            $('#eppraisalHomeValueWidgetContent').show();
        });
    } catch (ex) {
        $('#eppraisalHomeValueWidgetContent').html('<p>Error loading URL: </p><p>' + url + '</p><p>Exception: ' + ex.Message + '</p>');
    }
    if ($('#eppraisalHomeValueSearchLink').exists()) {
        $('#eppraisalHomeValueSearchLink').hide();
    }
}
function eppraisal_homevalue_showResult(address, cityState) {
    cityState = cityState.replace(' ', '-');
    var url = eppraisal_homevalue_base_url + '/widgets/homevalueresult/' + encodeURIComponent(address) + '/' + encodeURIComponent(cityState) + '.mvc?callback=?';
    try {
        $('#eppraisalHomeValueWidgetContent').hide();
        $.getJSON(url, null, function(data) {
            $("#eppraisalHomeValueWidgetContent").html(data.html);
            $('#eppraisalHomeValueWidgetContent').show();
        });
    } catch (ex) {
        $('#eppraisalHomeValueWidgetContent').html('<p>Error loading URL: </p><p>' + url + '</p><p>Exception: ' + ex + '</p>');
    }
}
function eppraisal_homevalue_initialize() {
    if ($('#eppraisalHomeValueWidget').exists()) {
        $('#eppraisalHomeValueWidget').prepend('<div id="eppraisalHomeValueWidgetContent"></div>');
        if (typeof (eppraisal_HomeValue_Street) == 'undefined' || eppraisal_HomeValue_Street == '' ||
             typeof (eppraisal_HomeValue_CityStateZip) == 'undefined' || eppraisal_HomeValue_CityStateZip == '') {
            try {
                eppraisal_homevalue_showSearch('');
            } catch (ex) {
                $('#eppraisalHomeValueWidgetContent').html('<p>Error loading search form:</p><p>' + ex + '</p>').show();
            }
        } else {
            try {
                eppraisal_homevalue_showResult(eppraisal_HomeValue_Street, eppraisal_HomeValue_CityStateZip);
            } catch (ex) {
                $('#eppraisalHomeValueWidgetContent').html('<p>Error loading result:</p><p>' + ex + '</p>').show();
            }
        }
    }
    eppraisal_homevalue_showWidget();
}
function eppraisal_homevalue_keyPress(evt, addressElementId, cityStateElementId) {
    var isHandled = false;
    evt = (evt) ? evt : (window.event) ? event : null;
    if (evt) {
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
        if (charCode == 13) { eppraisal_homevalue_showResult($('#' + addressElementId).val(), $('#' + cityStateElementId).val()); isHandled = true; }
    }
    return isHandled;
}

setTimeout(function() {
    $.fn.exists =
            function() {
                return ($(this).length !== 0);
            };
    eppraisal_homevalue_initialize();
}, 300);


