﻿//Error message table
    var errorList = new Array();
    errorList["BadZip"] = "This is not a properly formatted 5-digit zip code.";
    errorList["CriteriaNotSuppliedFault"] = "You must provide the minimum of either a zip code or a state to search.";
    errorList["AgentCriteriaNotSuppliedFault"] = "Please provide search information.";
    errorList["LastNameNotSuppliedFault"] = "You may not search by first name alone.  Please provide a last name.";
    errorList["PostalCodeNotFoundFault"] = "This zip code does not exist.";
    errorList["StateNotSuppliedFault"] = "City and state must both be provided for a city search.";
    errorList["CityNotSuppliedFault"] = "Please refine your search by providing city.";
    errorList["CityNotFoundFault"] = "This city is either invalid or not recognized by our system.";
    errorList["StateNotFoundFault"] = "This is not a valid state code.";
    errorList["SystemFault"] = "System is not available - please try later.";
    errorList["AuthenticationFailure"] = "System is not available - please try later.";

    jQuery(function() {
        jQuery('.find-btn-submit').click(submitSearch);
        jQuery('.find-btn-clear').click(clearForm);
    });
    
    function submitSearch() {
        var language = jQuery('#ddlLang').val();
        var city = jQuery.trim(jQuery('#txtCity').val());
        var state = jQuery('#ddlState').val();
        var zipcode = jQuery.trim(jQuery('#txtZip').val());
        var firstname = jQuery.trim(jQuery('#txtFirst').val());
        var lastname = jQuery.trim(jQuery('#txtLast').val());
        var area = jQuery('#chkSearcharea').is(':checked');
        var officename = jQuery.trim(jQuery('#txtOfficeName').val());

        var re = /^\d{5}$/;

        if (state == '' && zipcode == '' && city == '' && firstname == '' && lastname == '' && pageType == 'agent')
            postError("AgentCriteriaNotSuppliedFault");
        else if (state == '' && zipcode == '' && city == '' && pageType == 'office')
            postError("CriteriaNotSuppliedFault");
        else if (zipcode == '' && state == '' && pageType == 'office')
            postError("StateNotSuppliedFault");
        else if (state == '' && zipcode == '' && city == '' && firstname != '' && lastname == '' && pageType == 'agent')
            postError("LastNameNotSuppliedFault");
        else if (zipcode != '' && !re.test(zipcode))
            postError("BadZip");
        else { 
            var redirectUrl = (pageType == 'agent') ? '/Agents/' : '/Offices/';
            redirectUrl += jQuery.trim(language);
            if (zipcode != '') redirectUrl += '/' + zipcode;
            if (state != '') redirectUrl += '/' + state;
            if (city != '') redirectUrl += '/' + city;

            var query = '';
            if (firstname != '') query = addParm(query, 'fname', firstname);
            if (lastname != '') query = addParm(query, 'lname', lastname);
            if (officename != '') query = addParm(query, 'office', officename);
            if (area) query = addParm(query, 'area', 'false');

            window.location = redirectUrl + query;
        }
        return false;
    }

    function addParm(parmString, name, val) {
        parmString = (parmString == '') ? '?' : parmString + "&";
        parmString += name + '=' + val;
        return parmString;
    }

    function clearForm() {
        jQuery('#frmAgent :text, #ddlState').val('');
        jQuery('#ddlLang').val('1');
        jQuery('#chkSearcharea').attr('checked', false);
        return false; 
    }
    
    function postError(errorCode) {
        jQuery('#error-message').text(errorList[errorCode]);
        jQuery('#frmAgent label').removeAttr('style');
        jQuery('#pnlError').show();
        jQuery('#pnlResults').hide();
        jQuery('#pnlNoResults').hide();
        jQuery('#pnlDesc').hide();

        switch (errorCode)
        {
            case "AgentCriteriaNotSuppliedFault":
                jQuery('label[for="ddlState"]').css('color', 'red');
                jQuery('label[for="txtZip"]').css('color', 'red');
                jQuery('label[for="txtCity"]').css('color', 'red');
                jQuery('label[for="txtFirst"]').css('color', 'red');
                jQuery('label[for="txtLast"]').css('color', 'red');
                break;
            case "CriteriaNotSuppliedFault":
                jQuery('label[for="ddlState"]').css('color', 'red');
                jQuery('label[for="txtZip"]').css('color', 'red');
                break;
            case "LastNameNotSuppliedFault":
                jQuery('label[for="txtFirst"]').css('color', 'red');
                jQuery('label[for="txtLast"]').css('color', 'red');
                break;
            case "BadZip":
            case "PostalCodeNotFoundFault":
                jQuery('label[for="txtZip"]').css('color', 'red');
                break;
            case "CityNotSuppliedFault":
            case "CityNotFoundFault":
                jQuery('label[for="txtCity"]').css('color', 'red');
                break;
            case "StateNotSuppliedFault":
            case "StateNotFoundFault":
                jQuery('label[for="ddlState"]').css('color', 'red');
                break;
        }
    }
