﻿$(document).ready(function() {

    /* locator page steps */
    // default - disable everything but step 1
    $('.city, .state, .zip').attr('disabled', 'disabled');
    $('.two, .three').animate({ opacity: 0.2 });
    /* step 1 - check a box */

    $("input[value^='memorialization'],input[value^='residential']").parents('tr').hide();
    $("input[type='radio']").click(function() {
        if ($('input:checked').length > 0) {
            $('.city, .state, .zip').removeAttr('disabled');
            $('.two').animate({ opacity: 1 });
            $('.city').keyup();
            $('.state').change();
            if ($('.searchButton .formSearch').hasClass('disabled')) {
                $('.zip').keyup();
            }

            if (!$('.searchButton').hasClass('disabled')) {
                $('.searchButton').click();
            }
        } else {
            $('.city, .state, .zip').attr('disabled', 'disabled');
            $('.two, .three').animate({ opacity: 0.2 });
        }
    });

    $('.city').click(function() {
        if ($(this).val() == 'City') $(this).val('');
    });

    $('.zip').click(function() {
        if ($(this).val() == 'Postal Code') $(this).val('');
    });

    /* step 2 - input City / state OR Zip */
    $('.city').keyup(function(e) {
        if ($('.state').val() != "" && $(this).val() != "" || $('.zip').val() != "Postal Code") {
            //passed
            turn3on();
            if (e.keyCode == 13) {
                __doPostBack(replaceAll($("[id$='LnkSearch']").attr('id'), '_', '$'), '');
            }
        } else {
            turn3off();
        }
    });

    $('.state').change(stateChange);
    $('.state').keyup(stateChange);

    $('.zip').change(function() {
    });

    $('.zip').keyup(function(e) {
        $('.zip').val($('.zip').val().replace(/[^0-9A-Z a-z.,]+/g, ''));
        if ($('.zip').val() != "Postal Code" || $(".state").val() != "") {
            //passed
            turn3on();
            if (e.keyCode == 13) {
                __doPostBack(replaceAll($("[id$='LnkSearch']").attr('id'), '_', '$'), '');
            }
        } else {
            turn3off();
        }

    });

    $('.city').blur(function() {
        if ($(this).val() == '') {
            $(this).val('City');
        }
    });

    $('.zip').blur(function() {
        if ($(this).val() == '') {
            $(this).val('Postal Code');
        }
    });

    function turn3on() {
        $('.three').animate({ opacity: 1 });
        $('.searchButton').css("background-position", "center bottom");
        $('.searchButton .formSearch').show().removeClass('disabled');
        $('.searchButton').removeClass('disabled');

    }
    function turn3off() {
        $('.three').animate({ opacity: 0.2 });
        $('.searchButton').css("background-position", "center top");
        $('.searchButton .formSearch').hide().addClass('disabled');
    }

    $('#color_tabs a').click(function() {
        turn3off();
        $('#color_tabs a.selected').removeClass('selected');
        $("input[type='radio']").parents('tr').hide();
        $("input[value^='" + $(this).attr('class') + "']").parents('tr').show();
        updateHash($(this).attr('class'));
        switch ($(this).attr('class')) {
            case 'architectural':
                $('.city').hide();
                $('div.resultsWrapper').attr('id', 'archResult');
                break;
            case 'memorialization':
                $('.city').show();
                $('div.resultsWrapper').attr('id', 'memResult');
                break;
            case 'residential':
                $('.city').show();
                $('div.resultsWrapper').attr('id', 'resResult');
                break;
        }

        $(this).addClass('selected');
        initEnabled();
        /*turn3off();
        $('.city, .state, .zip').attr('disabled', 'disabled');
        $('.two, .three').animate({ opacity:0.2 });*/
        return false;
    });

    $('.formSearch').click(function() {
        if ($('.searchButton').hasClass('disabled')) {
            return false;
        }
    });

    var hashTag = document.location.hash.substring(1, document.location.hash.length);
    if (hashTag != "") {
        $("." + hashTag).click();
    }


    initEnabled();

    function stateChange() {
        if ((($('.city').val() != 'city' && $('.city').val() != "City") || $('.city:visible').length == 0) && $('.state').val() != "" || $('.zip').val() != "Postal Code") {
            //passed
            turn3on();
        } else {
            turn3off();
        }
    }


});


function initEnabled() {
 if ($('input:checked:visible').length > 0) {
	    $('.city, .state, .zip').removeAttr('disabled');
	    $('.two').animate({ opacity:1 });
	    
	    $('.city').keyup();
	    $('.state').change();
	    if($('.searchButton .formSearch').hasClass('disabled')) {
	        $('.zip').keyup();
	    }
	    
	    if(!$('.searchButton').hasClass('disabled')) 
	    {
	        $('.searchButton').click();
	    }
	    
    } else {
	    $('.city, .state, .zip').attr('disabled', 'disabled');
	    $('.two, .three').animate({ opacity:0.2 });
    }
}

function updateHash(tag) {
    var loc = new String(window.location);
    if(loc.indexOf("#") > -1) {
        loc = loc.substring(0,loc.indexOf("#"));
    }
    loc = loc + "#" + tag;
    window.location = loc;
}

function replaceAll(OldString, FindString, ReplaceString) {
  var SearchIndex = 0;
  var NewString = ""; 
  while (OldString.indexOf(FindString,SearchIndex) != -1)    {
    NewString += OldString.substring(SearchIndex,OldString.indexOf(FindString,SearchIndex));
    NewString += ReplaceString;
    SearchIndex = (OldString.indexOf(FindString,SearchIndex) + FindString.length);         
  }
  NewString += OldString.substring(SearchIndex,OldString.length);
  return NewString;
}
