﻿// JScript File containing functions used in ClientValidationFunction in CustomValidators 
// validating passengers in PassengersDataWithChildren.ascx and PassengersWithVehiclesData.ascx
// client-side validating replacing server-side in accordance with CASE 6642. shayd.

/// specific controls in either of both Controls (ascx) above to validate.
var PdwcaAdults, PdwcaChildren, AdultPassengers, SeniorPassengers, ChildPassengers, InfantPassengers, StudentPassengers;

/// loads the actual existing controls into variables above to be validated according to existing logic.
function GetPassengerControls()
{
    var selects = document.getElementsByTagName("SELECT");
    for(var i=0; i<selects.length; i++)
    {
        if (selects[i].progid != null)
        {
            if (selects[i].progid == "PdwcaAdults")
                PdwcaAdults = selects[i];
            if (selects[i].progid == "PdwcaChildren")
                PdwcaChildren = selects[i];
            if (selects[i].progid == "AdultPassengers")
                AdultPassengers = selects[i];
            if (selects[i].progid == "SeniorPassengers")
                SeniorPassengers = selects[i];
            if (selects[i].progid == "ChildPassengers")
                ChildPassengers = selects[i];
            if (selects[i].progid == "InfantPassengers")
                InfantPassengers = selects[i];
            if (selects[i].progid == "StudentPassengers")
                StudentPassengers = selects[i];
        }
    }
}

/// valid if there's at least 1 passenger of any type selected, else invalid.
function CustomvalidatorNoPassengersSelected_ClientValidate(src, args)
{
    GetPassengerControls();
    if (PdwcaAdults != null)
    {
        if (PdwcaAdults.selectedIndex == 0 && PdwcaChildren.selectedIndex == 0)
            return false;
        else
            return true;
    }
    else
    {
        if ((AdultPassengers == null || AdultPassengers.selectedIndex == 0) &&
            (ChildPassengers == null || ChildPassengers.selectedIndex == 0) &&
            (SeniorPassengers == null || SeniorPassengers.selectedIndex == 0) &&
            (StudentPassengers == null || StudentPassengers.selectedIndex == 0) &&
            (InfantPassengers == null || InfantPassengers.selectedIndex == 0))
            return false;
        else
            return true;
    }
}

/// valid unless there can be "Infants", and any are selected, without any adult selected for the service.
function customValidatorInfantWithNoAdult_ClientValidate(src, args)
{
    GetPassengerControls();
    if (PdwcaAdults != null) // there are only "infants" in PassengersWithVehiclesData.ascx,
        return true;         // in PassengersDataWithChildrenAges.ascx.cs there are only "adults" and "children"
    else
    {
        if ((InfantPassengers != null && InfantPassengers.selectedIndex > 0) &&
            (AdultPassengers == null || AdultPassengers.selectedIndex == 0) &&
            (SeniorPassengers == null || SeniorPassengers.selectedIndex == 0) &&
            (StudentPassengers == null || StudentPassengers.selectedIndex == 0))
            return false;
        else
            return true;
    }
}

function PassengersValidation_Init(id)
{
	var PassengersValidation = document.getElementById(id);
	if(PassengersValidation==null)
		return null;
}
