﻿//Error message table
    var errorList = new Array();
    errorList["BadZip"] = "This is not a properly formatted 5-digit zip code.";
    errorList["CriteriaNotSuppliedFault"] = "You must provide a city, state, zip code, or agent last name.";
    errorList["PostalCodeNotFoundFault"] = "This zip code does not exist.";
    errorList["StateNotSuppliedFault"] = "You must provide a state or zip code.";
    errorList["CityNotSuppliedFault"] = "You must include a city and state for an office search.";
    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.";
    
    function submitSearch()
    {
        var type = Trim(document.getElementById('search-type').value);
        var language = Trim(document.getElementById('ddlLangAgent').value);
        if (type == "office") language = Trim(document.getElementById('ddlLangOffice').value);
        var city = Trim(document.getElementById('txtCity').value);
        var state = Trim(document.getElementById('ddlState').value);
        var zipcode = Trim(document.getElementById('txtZip').value);
        var office = Trim(document.getElementById('txtOfficeName').value);
        var firstname = Trim(document.getElementById('txtFirst').value);
        var lastname = Trim(document.getElementById('txtLast').value);
        
        var redirectUrl = "/AgentsBrokers/" + language;

        if (zipcode != 'Zip Code' && zipcode != '') {
            redirectUrl += '/' + zipcode;

        } else {
            if (state != '')
                redirectUrl += '/' + state;
            if (city != 'City' && city != '')
                redirectUrl += '/' + city;
        }
        
        if (type == 'office') {
            redirectUrl += '/Offices.aspx?';
            if (office != 'Broker Office Name' && office != '')
                redirectUrl += 'office=' + office;
        
        } else {
            redirectUrl += '/Agents.aspx?';
            if (firstname != 'Agent First Name' && firstname != '')
                redirectUrl += '&first=' + firstname;
            if (lastname != 'Agent Last Name' && lastname != '')
                redirectUrl += '&last=' + lastname;
        }
        
        // redirect to the results page
        if (validateFields()) 
            window.location = redirectUrl;
    }
    
    function postError(errorCode)
    {
        pnlNoResults = document.getElementById("ctl00_main_pnlNoResults");
        if (pnlNoResults) pnlNoResults.style.display = "none";

        document.getElementById("error-message").innerHTML = errorList[errorCode];
        document.getElementById("pnlError").style.display = "block";
        
        
        var city = document.getElementById("txtCity");
        var state = document.getElementById("ddlState");
        var zip = document.getElementById("txtZip");
        var lastname = document.getElementById("txtLast");

        switch (errorCode)
        {
            case "BadZip":
            case "PostalCodeNotFoundFault":
                zip.style.color = "red";
            break;
             case "CriteriaNotSuppliedFault":
                city.style.color = "red";
                state.style.color = "red";
                zip.style.color = "red";
                lastname.style.color = "red"
            break;
            case "StateNotSuppliedFault":
                state.style.color = "red";
                zip.style.color = "red";
            break;
            case "CityNotSuppliedFault":
            case "CityNotFoundFault":
                city.style.color = "red";
       }
    }

     function clearInput2(action, input)	{
        var inputId = document.getElementById(input);
        switch(input)	{
	        case 'txtCity':
		        text = 'City';
	        break;
	        case 'txtZip':
		        text = 'Zip Code';
            break;
	        case 'txtFirst':
		        text = 'Agent First Name';
            break;
	        case 'txtLast':
		        text = 'Agent Last Name';
            break;
	        case 'txtOfficeName':
		        text = 'Broker Office Name';
            break;	    
        }
        if (action == 'focus')	{
		    inputId.style.color = null;
	        if (trimAll(inputId.value) == trimAll(text)){
		        inputId.value = '';
	        }
        }
        else if (inputId.value == '')	{
	        inputId.value = text;
        }
    }
   function selectSearchType(type) 
    {
        if (type == 'findagent')
        {
            document.getElementById('find-agent-agent').style.display = 'block';
            document.getElementById('find-agent-office').style.display = 'none';
            document.getElementById('check-findagent').className = 'radio-checked';
            document.getElementById('check-findoffice').className = 'radio-unchecked';
            document.getElementById('search-type').value = 'agent';
        }
        if (type == 'findoffice')
        {
            document.getElementById('find-agent-office').style.display = 'block';
            document.getElementById('find-agent-agent').style.display = 'none';
            document.getElementById('check-findoffice').className = 'radio-checked';
            document.getElementById('check-findagent').className = 'radio-unchecked';
            document.getElementById('search-type').value = 'office';
        }
    }
    
    function expandOffice(btn)
    {
        var row = btn.replace('expand-button-', '');
        if (document.getElementById(row).style.display == 'none')
        {
            document.getElementById(row).style.display = 'block';
            document.getElementById(btn).className = 'expand-office on';
        }
        else
        {
            document.getElementById(row).style.display = 'none';
            document.getElementById(btn).className = 'expand-office';
        }
    }
    
    function validateFields()
    {
        var city = document.getElementById("txtCity").value;
        if (city == "City") city = "";
        var state = document.getElementById("ddlState").value;
        var zip = document.getElementById("txtZip").value;
        if (zip == "Zip Code") zip = "";
        var lastname = document.getElementById("txtLast").value;
        if (lastname == "Agent Last Name") lastname = ""

        var agent;
        var errorDiv = document.getElementById("errorDiv")
        
        if (document.getElementById("search-type").value == "agent")
            agent = true;
        else
            agent = false;
            
        re = /^\d{5}$/;   
        if (agent && city == "" && state == "" && zip == "" && lastname == "")
        {
            postError("CriteriaNotSuppliedFault");
            return false;
        }
        else if (zip != "" && !re.test(zip))
        {
            postError("BadZip");
            return false;
        }
        else if (!agent && state == "" && zip == "")
        {
            postError("StateNotSuppliedFault");
            return false;
        }
        else return true;
        
            
    }
    
    function Trim(str)
    {
        var	str = str.replace(/^\s\s*/, ''),
            ws = /\s/,
            i = str.length;
        while (ws.test(str.charAt(--i)));
        return str.slice(0, i + 1);
    }
    
    function AgentWebsiteWT(websiteURL, agent_Id, state_Id)
    {
        if (websiteURL.substring(0, 4) == "http") {
            window.open(websiteURL);
        } else {
            window.open("http://" + websiteURL);
        }

        dcsMultiTrack('DCS.dcsuri', '/Views/General/FindAgentResults/AgentWeblink.html', 'WT.ti', 'Agent Website Link', 'WT.cg_n', 'indirect conversion', 'WT.si_n', '', 'WT.si_x', '', 'WT.si_cs', '', 'WT.z_engage_type', 'Indirect', 'WT.z_engage_event', 'Agent Weblink', 'WT.z_offsite_url', websiteURL, 'WT.z_agent_id', agent_Id, 'WT.z_agent_state', state_Id, 'WT.z_office_id', '');
        pageTracker._trackPageview('agent_redirect.html');
        return false;
    }

    function OfficeWebsiteWT(websiteURL, office_Id)
    {
        if (websiteURL.substring(0, 4) == "http") {
            window.open(websiteURL);
        } else {
            window.open("http://" + websiteURL);
        }

        dcsMultiTrack('DCS.dcsuri','/Views/General/FindOfficeResults/OfficeWeblink.html','WT.ti','Office Website Link','WT.cg_n','indirect conversion','WT.si_n','','WT.si_x','','WT.si_cs','','WT.z_engage_type','Indirect','WT.z_engage_event','Office Weblink','WT.z_offsite_url',websiteURL,'WT.z_agent_id','','WT.z_agent_state','','WT.z_office_id',office_Id);
        return false;
    }
