﻿var MANUFACTURER = 1;
var ASSOCIATION = 2;
var INSURANCE_COMPANY = 3;
var OTHER = 4;
var APPBY_NONE = 0;

var NOT_SELECTED = -2;
var SHOW_ALL = -1;
var DASH_DIVIDER = "--------------------------------";
var SHOW_ALL_OPTION_TEXT = "Show All";

var USE_MAP = 1;
var USE_LIST = 2;

var SELECT_APPROVER_FIRST_LINE = "Please Select an Approver";

//index to set default to Show All
var SELECT_APPROVER_SHOWALL_INDEX = 2;

var EOL = "\n";

//ui error messages

var ERROR_MESSAGE_NO_SERVICE_SELECTED = "Please select an actual Service from the Service drop-down box," +
EOL + "before pressing the Go button";

var ERROR_MESSAGE_NO_APPROVER_SELECTED
 = "Please select an actual Approver or select 'Show All'" +
 EOL + "from the Approver drop-down box," +
 EOL + "before pressing the Go button";


function FillApproverBox(pSelectServiceBox, pSelectApproverBox, pArrServices)
{

  
    var pSelectServiceBox_selectedIndex = pSelectServiceBox.selectedIndex;
    
    if (pSelectServiceBox_selectedIndex < 2) return false;
    
    var serviceName = pSelectServiceBox.options[pSelectServiceBox_selectedIndex].text;
    var serviceIdSelected = pSelectServiceBox.options[pSelectServiceBox_selectedIndex].value*1;
    
    var arrServicesCount = pArrServices.length;
    var i=0;
    var loop = true;
    
    var selectBoxApproverElementCount = 0;
    
    pSelectApproverBox.options[selectBoxApproverElementCount++] = new Option(SELECT_APPROVER_FIRST_LINE, NOT_SELECTED);
    pSelectApproverBox.options[selectBoxApproverElementCount++] = new Option(DASH_DIVIDER, NOT_SELECTED);
    pSelectApproverBox.options[selectBoxApproverElementCount++] = new Option(SHOW_ALL_OPTION_TEXT, SHOW_ALL);

    while (i < arrServicesCount && loop)
    {
        if (pArrServices[i].serviceId == serviceIdSelected && pArrServices[i].approverTypeId >= 1) //find fist entry
        {
            var approverTypeIdPrev = -1;

            do  //add lines loop
            {
                var approverName = pArrServices[i].approverName;
                var approverId = pArrServices[i].approverId;
                var approverTypeId = pArrServices[i].approverTypeId;
                var approverTypeName = "";
                
                if (approverTypeId != approverTypeIdPrev)
                {
                    switch(approverTypeId)
                    {
                        case MANUFACTURER: approverTypeName = "MANUFACTURERS"; break;
                        case ASSOCIATION: approverTypeName = "ASSOCIATION MEMBERS"; break;
                        case INSURANCE_COMPANY: approverTypeName = "INSURANCE COMPANYS"; break;
                        case OTHER: approverTypeName = "OTHER"; break;
                    }
                    pSelectApproverBox.options[selectBoxApproverElementCount++] = new Option(DASH_DIVIDER, NOT_SELECTED);
                    pSelectApproverBox.options[selectBoxApproverElementCount++] = new Option(approverTypeName, NOT_SELECTED);
                    pSelectApproverBox.options[selectBoxApproverElementCount++] = new Option(DASH_DIVIDER, NOT_SELECTED);
                    approverTypeIdPrev = approverTypeId;
                }

                pSelectApproverBox.options[selectBoxApproverElementCount++] = new Option(approverName, approverId);
                i++;
            
            } while (i < arrServicesCount && serviceIdSelected == pArrServices[i].serviceId);
            pSelectApproverBox.options.length = selectBoxApproverElementCount;
            loop = false;
        } //if
        i++;
    
    }//while

    pSelectApproverBox.selectedIndex = SELECT_APPROVER_SHOWALL_INDEX;
    pSelectApproverBox.focus();
   
}//func

function selectServiceCar_OnChange()
{
    FillApproverBox(document.forms[0].selectServiceCar, document.forms[0].selectApproverCar, arrServicesAutomotive);
}

function selectServiceHome_OnChange()
{
    FillApproverBox(document.forms[0].selectServiceHome, document.forms[0].selectApproverHome, arrServicesHome);
}
function selectServiceBusinessLeisure_OnChange()
{
    FillApproverBox(document.forms[0].selectServiceBusinessLeisure, document.forms[0].selectApproverBusinessLeisure, arrServicesBuisinessAndLeisure);
}

function Go(serviceId, serviceName, approverId, approverName, listOrMap)
{
    serviceName = EnCode(serviceName);
    approverName = EnCode(approverName);
    var pageUrl;

    if (listOrMap==USE_MAP)
    {
        pageUrl = "/Asp/map_01.asp";
        window.location = pageUrl + "?" +
        "sid=" + serviceId + "&aid=" + approverId + "&snm=" + serviceName + "&anm=" + approverName;
    }
    else if (listOrMap==USE_LIST)
    {
            pageUrl = "/Search/list-towns.aspx";
            window.location = pageUrl + "?" +
            "sid=" + serviceId + "&aid=" + approverId;
    }
}

function CheckSelectionsOk(pSelectServiceBox, pSelectApproverBox)
{
    var pSelectServiceBox_selectedIndex = pSelectServiceBox.selectedIndex;
    var pSelectApproverBox_selectedIndex = pSelectApproverBox.selectedIndex;

    if (pSelectServiceBox_selectedIndex < 2)
    {
        alert(ERROR_MESSAGE_NO_SERVICE_SELECTED);
        pSelectServiceBox.focus();
        return false;
    }

    if (pSelectServiceBox_selectedIndex < SELECT_APPROVER_SHOWALL_INDEX)
    {
        alert(ERROR_MESSAGE_NO_APPROVER_SELECTED);
        pSelectApproverBox.focus();
        return false;
    }
    
    var approverIdSelected = pSelectApproverBox.options[pSelectApproverBox_selectedIndex].value*1;

    if (approverIdSelected == NOT_SELECTED)
    {
      alert(ERROR_MESSAGE_NO_APPROVER_SELECTED);
      pSelectApproverBox.focus();
      return false;
    }
    
    return true;
}

function GoUsingSelectBoxes(pSelectServiceBox, pSelectApproverBox, listOrMap)
{
    if (!CheckSelectionsOk(pSelectServiceBox, pSelectApproverBox)) return false;

    var pSelectServiceBox_selectedIndex = pSelectServiceBox.selectedIndex;
    var pSelectApproverBox_selectedIndex = pSelectApproverBox.selectedIndex;

    var approverIdSelected = pSelectApproverBox.options[pSelectApproverBox_selectedIndex].value*1;

    var serviceIdSelected = pSelectServiceBox.options[pSelectServiceBox_selectedIndex].value*1;
    var serviceNameSelected = pSelectServiceBox.options[pSelectServiceBox_selectedIndex].text;
    
    var approverNameSelected = pSelectApproverBox.options[pSelectApproverBox_selectedIndex].text;

    Go(serviceIdSelected, serviceNameSelected, approverIdSelected, approverNameSelected, listOrMap);
}

function buttonGoMapCarOnClick()
{
    GoUsingSelectBoxes(document.forms[0].selectServiceCar, document.forms[0].selectApproverCar, USE_MAP);
}

function buttonGoListCarOnClick()
{
    GoUsingSelectBoxes(document.forms[0].selectServiceCar, document.forms[0].selectApproverCar, USE_LIST);
}



function buttonGoMapHomeOnClick()
{
    GoUsingSelectBoxes(document.forms[0].selectServiceHome, document.forms[0].selectApproverHome, USE_MAP);
}

function buttonGoListHomeOnClick()
{
    GoUsingSelectBoxes(document.forms[0].selectServiceHome, document.forms[0].selectApproverHome, USE_LIST);
}

function buttonGoMapBuslOnClick()
{
    GoUsingSelectBoxes(document.forms[0].selectServiceBusinessLeisure, document.forms[0].selectApproverBusinessLeisure, USE_MAP);
}

function buttonGoListBuslOnClick()
{
    GoUsingSelectBoxes(document.forms[0].selectServiceBusinessLeisure, document.forms[0].selectApproverBusinessLeisure, USE_LIST);
}

//--- Remember Area Functions

var CAR_GO = 0;
var HOME_GO = 1;
var BUSL_GO = 2

function GoLocation(serviceId, serviceName, approverId, approverName, locationId, locationName)
{
    serviceName = EnCode(serviceName);
    approverName = EnCode(approverName);
    var pageUrl;

    pageUrl = "/Search/sp-list.aspx";
   
    
    window.location = pageUrl + "?" +
        "sid=" + serviceId + "&aid=" + approverId + "&lid=" + locationId;
}

function GoLocationUsingSelectBoxes(pSelectServiceBox, pSelectApproverBox, locationId, locationName)
{
    if (!CheckSelectionsOk(pSelectServiceBox, pSelectApproverBox)) return false;
    
    var pSelectServiceBox_selectedIndex = pSelectServiceBox.selectedIndex;
    var pSelectApproverBox_selectedIndex = pSelectApproverBox.selectedIndex;
    
    var approverIdSelected = pSelectApproverBox.options[pSelectApproverBox_selectedIndex].value*1;

    var serviceIdSelected = pSelectServiceBox.options[pSelectServiceBox_selectedIndex].value*1;
    var serviceNameSelected = pSelectServiceBox.options[pSelectServiceBox_selectedIndex].text;
    
    var approverNameSelected = pSelectApproverBox.options[pSelectApproverBox_selectedIndex].text;

    GoLocation(serviceIdSelected, serviceNameSelected, approverIdSelected, approverNameSelected, locationId, locationName);
}

function GoArea(goType, locationId, locationName)
{
    var pSelectServiceBox, pSelectApproverBox;

    switch (goType)
    {
        case CAR_GO:
            pSelectServiceBox = document.forms[0].selectServiceCar;
             pSelectApproverBox = document.forms[0].selectApproverCar;
            break;
        case HOME_GO:
            pSelectServiceBox = document.forms[0].selectServiceHome;
            pSelectApproverBox = document.forms[0].selectApproverHome;
            break;
        case BUSL_GO:
            pSelectServiceBox = document.forms[0].selectServiceBusinessLeisure;
            pSelectApproverBox = document.forms[0].selectApproverBusinessLeisure;
            break;
    }

    GoLocationUsingSelectBoxes(pSelectServiceBox, pSelectApproverBox, locationId, locationName);
}