﻿var mapSearchError = "";
var zipRegex = new RegExp(/^[0-9]{5}$/);
var caRegex = new RegExp(/^[0-9a-zA-Z]{3}[ ]{1}[0-9a-zA-Z]{3}$/);
var siteSection;

function validateSearch(zip, state) {

    // International search
    // if international radio btn selected then make sure a country is selected

    // This applies only to the US & CA search
    if (!zipRegex.test(zip) && !caRegex.test(zip) && state == "0") {
        mapSearchError += "Please enter a valid postal code or select a state/province.";
        return false;
    } else {
        return true;
    }
    
}

function submitMapSearch() {
    var $this = $(this);
    var zip = $this.closest('.salesSearch').find('input.zip').attr('value');
    var state = $this.closest('.salesSearch').find('.state').val();
    var app = $this.closest('.salesSearch').find('.app').val();
    
    if (!validateSearch(zip, state)) {
        $('p.mapSearchError').html(mapSearchError).show();
        return;
    }
     
    if (isResidential) {
           siteSection = "Residential";
    } else {
            siteSection = "Engineered";
    }
     
    // International search
    // if international radio button is selected; redirect with the value of the country
     
    // If this is a US search
    if (zipRegex.test(zip) || caRegex.test(zip)) {
        window.location = siteSection + "WhereToBuy.aspx?pc=" + zip + "&app=" + app;
    } else {
        window.location = siteSection + "WhereToBuy.aspx?st=" + state + "&app=" + app;
    }
    
    return false;
}

$(document).ready(function() {
    $('.btn_mapSearch').click(submitMapSearch);
    $('.zip').focus(function() {
        $(this).val('');
        $(this).unbind('focus', this)
    });
});

