﻿// JScript File

var LastCallingControl = null;

function VehicleDBAjaxURLAppend(ID, Name)
{
   var x = document.getElementById(ID);
   if (x == null) return "";
   
   return "&" + Name + "=" + x.value;
}

function VehicleDBAjaxURLAppendText(ID, Name)
{
   var x = document.getElementById(ID);
   if (x == null) return "";
   
   var selIndex = x.selectedIndex;
   if (selIndex == null) return "";
   if (selIndex == -1) return "";
   
   return "&" + Name + "=" + x.options[selIndex].text;
}

function VehicleDBAjaxUpdate(control)
{
    if (control == null) return;
    
    LastCallingControl = control;
    var ControlName = control.name;
    
    var url = "/PageCode/VehicleDB.aspx?CallingControlName=" + ControlName;
    
    url += VehicleDBAjaxURLAppend("ctgInsuranceQuestion_ModelYear", "ModelYear");
    url += VehicleDBAjaxURLAppend("ctgInsuranceQuestion_ModelMake", "ModelMake");
    url += VehicleDBAjaxURLAppend("ctgInsuranceQuestion_ModelName", "ModelName");
    url += VehicleDBAjaxURLAppendText("ctgInsuranceQuestion_ModelName", "ModelNameText");
    url += VehicleDBAjaxURLAppend("ctgInsuranceQuestion_ModelDescription", "ModelDescription");
    url += VehicleDBAjaxURLAppend("ctgInsuranceQuestion_TrailerYear", "TrailerYear");
    url += VehicleDBAjaxURLAppend("ctgInsuranceQuestion_TrailerMake", "TrailerMake");    
    
    if (ControlName == "ctgInsuranceQuestion_EngineYear1" || ControlName == "ctgInsuranceQuestion_EngineMake1")
    {
        // Get value from numbered field, but send without number
        url += VehicleDBAjaxURLAppend("ctgInsuranceQuestion_EngineYear1", "EngineYear");        
        url += VehicleDBAjaxURLAppend("ctgInsuranceQuestion_EngineMake1", "EngineMake");        
    }

    if (ControlName == "ctgInsuranceQuestion_EngineYear2" || ControlName == "ctgInsuranceQuestion_EngineMake2")
    {
        // Get value from numbered field, but send without number
        url += VehicleDBAjaxURLAppend("ctgInsuranceQuestion_EngineYear2", "EngineYear");        
        url += VehicleDBAjaxURLAppend("ctgInsuranceQuestion_EngineMake2", "EngineMake");        
    }

    if (ControlName == "ctgInsuranceQuestion_EngineYear3" || ControlName == "ctgInsuranceQuestion_EngineMake3")
    {
        // Get value from numbered field, but send without number
        url += VehicleDBAjaxURLAppend("ctgInsuranceQuestion_EngineYear3", "EngineYear");        
        url += VehicleDBAjaxURLAppend("ctgInsuranceQuestion_EngineMake3", "EngineMake");        
    }

    if (ControlName == "ctgInsuranceQuestion_EngineYear4" || ControlName == "ctgInsuranceQuestion_EngineMake4")
    {
        // Get value from numbered field, but send without number
        url += VehicleDBAjaxURLAppend("ctgInsuranceQuestion_EngineYear4", "EngineYear");        
        url += VehicleDBAjaxURLAppend("ctgInsuranceQuestion_EngineMake4", "EngineMake");        
    }
    
    loadXMLDoc(url, VehicleDBAjaxCallBack);
}

function VehicleDBAjaxCallBack()
{
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            VehicleDBAjaxApply();
         } else {
            //alert("There was a problem retrieving the XML data:\n" + req.statusText);
         }
    }
}



function VehicleDBAjaxApply()
{
    AjaxProcessResponse();
}

