﻿var useAdvOpts = 1;

$(document).ready(
    function() {
        ToggleAdvSrchOpts();

        $('#srchMoreInputsToggle').click(
            function() {
                ToggleAdvSrchOpts();
                if ($(this).text().toLowerCase().indexOf('+') >= 0) {
                    $(this).text('Advanced Search(-)');
                }
                else {
                    $(this).text('Advanced Search(+)');
                }
            }
        );

        String.prototype.trim = function() {
            return this.replace(/^\s+|\s+$/g, "");
        }
        String.prototype.ltrim = function() {
            return this.replace(/^\s+/, "");
        }
        String.prototype.rtrim = function() {
            return this.replace(/\s+$/, "");
        }


        $('#searchTermInput').click(
            function() {
                $(this).val('');
            }
        );

        $('#searchTermInput').keypress(
            function(e) {
                if (e.which == 13 && $(this).val().length > 0) {
                    $('#srchBtnGo').click();
                }
            }
        );


        $('#srchBtnGo').click(
            function() {
                var redirLoc = "";

                var srchTerm = $('#searchTermInput').val();

                if (srchTerm.trim() == "Enter keyword(s)" || srchTerm.trim() == '') {
                    if (useAdvOpts && ($('#end_datepicker').val() != "" || $('#state_select').val() != "217")) {
                        srchTerm = 'all';
                    }
                    else {
                        return;
                    }
                }

                var redirected = false;

                $('#search_box').fadeTo("fast", 0.5,
                    function() {
                        $('#spinner_logo').show();

                        $.get("/ajax/ajxSearchMapping.aspx?inTrm=" + escape(srchTerm),
                            function(data) {
                                redirLoc = data;

                                if (useAdvOpts) {
                                    redirLoc += "?startDate=" + escape($('#start_datepicker').val()) + "&endDate=" +
                                        escape($('#end_datepicker').val()) + "&loc=" + escape($('#state_select').val());
                                }

                                location.href = redirLoc;

                                redirected = true;
                            }
                        );
                    }
                );

                if (!redirected) {
                    $('#search_box_sb').fadeTo("fast", 0.5,
                        function() {
                            $('#spinner_logo').show();

                            $.get("/ajax/ajxSearchMapping.aspx?inTrm=" + escape(srchTerm),
                                function(data) {
                                    redirLoc = data;

                                    if (useAdvOpts) {
                                        redirLoc += "?startDate=" + escape($('#start_datepicker').val()) + "&endDate=" +
                                            escape($('#end_datepicker').val()) + "&loc=" + escape($('#state_select').val());
                                    }

                                    location.href = redirLoc;
                                }
                            );
                        }
                    );
                }
            }
        );



        var time = new Date();
        var futureTime;
        if (time.getMonth() < 9) {
            futureTime = new Date(time.getFullYear(), time.getMonth() + 4, time.getDate());
        } else {
            futureTime = new Date(time.getFullYear() + 1, (time.getMonth() + 4) % 11, time.getDate());
        }

        $('#start_datepicker').datepicker({ showOn: 'both', buttonImage: '/img/structure/search/btn_calendar.gif', buttonImageOnly: 'true', buttonText: 'Select a date', yearRange: '-00:+05' });
        //$('#start_datepicker').val(time.getMonth()+ 1 + "/" + time.getDate() + "/" + time.getFullYear());

        $('#end_datepicker').datepicker({ showOn: 'both', buttonImage: '/img/structure/search/btn_calendar.gif', buttonImageOnly: 'true', buttonText: 'Select a date', yearRange: '-00:+05' });
        //$('#end_datepicker').val(futureTime.getMonth() + 1 + "/" + futureTime.getDate() + "/" + futureTime.getFullYear());









        /*JangoMailAjaxing*/
        $('#jangoMailSubBtnGo').click(
            function() {
                if ($('#jangoMailSubEmailTxt').val().length > 0) {
                    $('#jangoMailStatusDesc').html('Attempting signup, one moment please...');

                    
                    $('#jangoMailStatusDesc').load('/ajax/ajxjangosubscript.aspx?em=' + $('#jangoMailSubEmailTxt').val() + newsletterTargets + zip);
                }
            }
        );
    }
);


function ToggleAdvSrchOpts()
{
    $('#advSrchInputs').toggle();
    if(useAdvOpts)
    {
        useAdvOpts = 0;
    }
    else
    {
        useAdvOpts = 1;
    }
}

  function CreateBookmarkLink() {
  
  title = document.title; 
  url = window.location.href;
  
  if (window.sidebar) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title, url,"");
    } else if( window.external ) { // IE Favorite
        window.external.AddFavorite( url, title); }
    else if(window.opera && window.print) { // Opera Hotlist
   var a = document.createElement("A");
     a.rel = "sidebar";
   a.target = "_search";
   a.title = title;
    a.href = url;
    a.click(); }
  }


