// Eurostar Booking Wizard scripts

var bookPostBackElement;

function InitializeRequest(sender, args) 
{
    bookPostBackElement = args.get_postBackElement();
    if (bookPostBackElement != null)
    {
         var litWaitText = $get(litWaitTextID);
         if(litWaitText != null)
         {
             if(bookPostBackElement.id == btnSearch)
             {
                litWaitText.innerHTML = 'Please wait while we check Eurostar availability...';
             }
             else
             {
                litWaitText.innerHTML = 'Please wait while we place your tickets on hold...';
             }
         }
         $get(pnlMainID).style.display = "none";
         $get(pnlWaitID).style.display = "block";
    }
}
    
function EndRequest (sender, args) 
{
    if (bookPostBackElement != null && bookPostBackElement.id == btnSearch)
    {
     $get(pnlWaitID).style.display = "none";
     $get(pnlMainID).style.display = "block";
    }
    if (args.get_error() && args.get_error().name == 'Sys.WebForms.PageRequestManagerTimeoutException') 
    {
           alert('There was a problem communicating with the Eurostar reservation system - please try again.');
           // remember to set errorHandled = true to keep from getting a popup from the AJAX library itself 
           args.set_errorHandled(true);
    }
}
        
function SetInfantsMaxCount(ddlAdultsID, ddlInfantsID)
{
    var ddlAdults = $get(ddlAdultsID);
    if(ddlAdults != null)
    {
        var ddlInfants = $get(ddlInfantsID);
        
        if(ddlInfants != null)
        {
            while(ddlInfants.options.length > 0)
            {
                ddlInfants.remove(0);
            }
            
            var maxInfants = ddlAdults.options[ddlAdults.selectedIndex].text;
            
            for(i=0;i<=maxInfants;i++)
            {
                var infOpt = document.createElement('option');
                infOpt.text = i;
                infOpt.value = i;
                try
                {
                    // FX
                    ddlInfants.add(infOpt, null);
                }
                catch(ex)
                {
                    // IE
                    ddlInfants.add(infOpt);
                }
            }
        }
    }
}

function RenderChildAges(ddlChildrenID, containerID)
{
    var ddlChildren = $get(ddlChildrenID);
    if(ddlChildren != null)
    {
        var childCount = ddlChildren.options[ddlChildren.selectedIndex].text;
        var ageContainer = $get(containerID);
        
        if(childCount > 0)
        {
            ageContainer.style.display = 'block';
            ageContainer.innerHTML = '';
            
            var childAgesHtml = '';
            
            for(i=0;i<childCount;i++)
            {
                var currentAgeHtml = "<div><div style='width:84px;'>Child Age #" + (i+1) + "</div><select id='ddlAges" + i + "' name='ddlAges" + i + "'>";
                for(j=4;j<=11;j++)
                {
                    currentAgeHtml += "<option value='"+ j +"'>"+ j +"</option>";
                }
                currentAgeHtml += "</select></div>";
                childAgesHtml += currentAgeHtml;
            }
            
            ageContainer.innerHTML = childAgesHtml;
        }
        else
        {
            ageContainer.innerHTML = '';
        }
    }
}

function RecreateAges(ddlChildrenID, containerID, childAges)
{
    RenderChildAges(ddlChildrenID, containerID);
    
    for(i=0;i<childAges.length;i++)
    {
        var currentChildDdl = $get("ddlAges" + i);
        for(j=0;j<currentChildDdl.options.length;j++)
        {
            if(currentChildDdl.options[j].text == childAges[i])
            {
                currentChildDdl.selectedIndex = j;
                break;
            }
        }
    }
}

function SetReturnDate(txtTravelDateID, litReturnDateID)
{
    var txtTravelDate = $get(txtTravelDateID);
    if(txtTravelDate != null)
    {
        var litReturnDate = $get(litReturnDateID);
        
        if(litReturnDate != null)
        {
            litReturnDate.innerHTML = txtTravelDate.value;
        }
    }
}

function SendToVsp()
{
    var vspForm = document.createElement("form");
    vspForm.setAttribute("name", "vsp");
    vspForm.method = "POST";
    vspForm.action = document.getElementById('VspFormUrl').value;
    vspForm.appendChild(CreateHiddenField("VPSProtocol", "2.22"));    
    vspForm.appendChild(document.getElementById('TxType'));
    vspForm.appendChild(document.getElementById('Vendor'));
    vspForm.appendChild(document.getElementById('Crypt'));        
    document.body.appendChild(vspForm);
    vspForm.submit();
}

function CreateHiddenField(name, value)
{
    var hiddenField = document.createElement("input");
    hiddenField.setAttribute('type', 'hidden');
    hiddenField.setAttribute('value', value );
    hiddenField.setAttribute('name', name );
    hiddenField.setAttribute('id', name );

    return hiddenField; 
}

function CheckRequestStatus()
{
    var reqStatusHiddenField = document.getElementById('reqStatus')

    if(reqStatusHiddenField != null && reqStatusHiddenField.value == 'success')
    {
        SendToVsp();
    }
}
 