﻿function showHideOptions(searchType)
{
        var flightOption =document.frmMultiSearch.uscBetweenIslands$rblNeedFlightBetweenIsland;
        var carOption   =document.frmMultiSearch.uscCarSearch$rblIslandCarOption;  
        
        $("#divFlight_search").hide();
        $("#divReturning_home").hide();
        $("#divCar_search").hide();
        $("#tblFlightRequired").hide();
        
       if(searchType=="HotelOnly")
        {
           $("#divFlight_search").hide();
           document.getElementById("uscBetweenIslands_Interisland").style.visibility="visible"; 
           $("#tdHotelCheckIn1").show();
           $("#tdHotelCheckOut1").show();
           $("#tdHotelCheckIn2").show();
           $("#tdHotelCheckOut2").show();
           $("#tdNeedflight").show();
           $("#tdNeedFlightOptional").hide();
           showHideInterIslandFlight();
           $("#imgSearch").attr("value", "Search for Hotels");
        }
         else if(searchType=="HotelCar")
        {
            document.getElementById("uscBetweenIslands_Interisland").style.visibility="visible"; 
            $("#divCar_search").show();
            ShowCarOption();
            $("#tblFlightRequired").show();
            $("#tdHotelCheckIn1").show();
            $("#tdHotelCheckOut1").show();
            $("#tdHotelCheckIn2").show();
            $("#tdHotelCheckOut2").show();
            $("#tdNeedflight").show();
            $("#tdNeedFlightOptional").hide();
            showHideInterIslandFlight();
            $("#imgSearch").attr("value", "Search for Hotels + Cars");
        }
        else if(searchType=="AirHotel")
        {
            $("#divFlight_search").show();
            document.getElementById("uscBetweenIslands_Interisland").style.visibility="hidden"; 
            $("#Interisland").hide();
            $("#divReturning_home").show();
            flightOption[0].checked="checked";
            $("#tblFlightRequired").show();
            $("#tdHotelCheckIn1").hide();
            $("#tdHotelCheckOut1").hide();
            $("#tdHotelCheckIn2").hide();
            $("#tdHotelCheckOut2").hide();
            $("#tdNeedflight").hide();
            $("#tdNeedFlightOptional").show();
            $("#imgSearch").attr("value", "Search for Flights + Hotels");
        }
        else if(searchType=="AirHotelCar")
        {
            $("#divFlight_search").show();
            document.getElementById("uscBetweenIslands_Interisland").style.visibility="hidden"; 
            $("#divReturning_home").show();
            $("#divCar_search").show();
            ShowCarOption();
            flightOption[0].checked="checked";
            $("#tdHotelCheckIn1").hide();
            $("#tdHotelCheckOut1").hide();
            $("#tdHotelCheckIn2").hide();
            $("#tdHotelCheckOut2").hide();
            $("#tdNeedflight").hide();
            $("#tblFlightRequired").show();
            $("#tdNeedFlightOptional").show();
            $("#imgSearch").attr("value", "Search for Flights + Hotels + Cars");
        }
       
       // $("#imgSearch").attr("src","images/btn"+searchType+".jpg");
        
}

function showHideInterIslandFlight()
{
    isflightSelected=getSelectedRadioButton(document.frmMultiSearch.uscBetweenIslands$rblNeedFlightBetweenIsland).value;
    if(isflightSelected=="Yes")
    {
            $("#tblFlightRequired").show();
            $("#tdHotelCheckOut1").hide();
            $("#tdHotelCheckIn2").hide();
            //$("#tdHotelCheckOut1").hide();
            //$("#tdHotelCheckIn2").hide();
        }
    else
    {
            $("#tblFlightRequired").hide();
            $("#tdHotelCheckOut1").show();
            $("#tdHotelCheckIn2").show();
            //$("#tdHotelCheckOut1").show();
            //$("#tdHotelCheckIn2").show();
         }
  
}
function ShowCarOption()
{
     var carOption   =document.frmMultiSearch.uscCarSearch$rblIslandCarOption;  
    carSelected=getSelectedRadioButton(document.frmMultiSearch.uscCarSearch$rblIslandCarOption).value;
    if(carSelected=="")
    {
            carOption[0].checked="checked";
        }
    else if  (carSelected=="Island1")
    {
            carOption[1].checked="checked";
         }
         else if  (carSelected=="Island2")
    {
            carOption[2].checked="checked";
         }
          else if  (carSelected=="Both")
    {
            carOption[3].checked="checked";
         }
  
}
function getSelectedRadioButton(CtlOption)
{
    //var CtlOption =document.frmMultiSearch.rblTypeofTrip;
    for(var i = 0; i < CtlOption.length; i++) 
        if(CtlOption[i].checked) 
          return CtlOption[i];
}

function getIslandType()
{
    var CtlOption =document.frmMultiSearch.uscCarSearch$rblIslandCarOption;
    for(var i = 0; i < CtlOption.length; i++) 
        if(CtlOption[i].checked) 
          return i;
}
function validateSearchCriteria()
{
    var dateCompareRes;
    var tripType=getSelectedRadioButton(document.frmMultiSearch.rblTypeofTrip).value;

    if(tripType=="AirHotel" || tripType=="AirHotelCar")
    {
        var txtLeavingFrom=$("#uscFligtSearch_txtLeavingFrom");
        
        if(!CheckEmptyField(txtLeavingFrom,"Leaving from required"))
            return false;
            
        var departDate= $("#uscFligtSearch_txtDepartDate");
        if(!CheckEmptyField(departDate,"Date required"))
            return false;
         
        if(!validateDateFormat(departDate.val(),'U','F'))
        {
            setFoucs(departDate);
            return false;
        }
        
        var betweenIslandsDepartDate=$("#uscBetweenIslands_txtDepartDate");
        if(!CheckEmptyField(betweenIslandsDepartDate,"Date required"))
            return false;
         
        if(!validateDateFormat(betweenIslandsDepartDate.val(),'U','F'))
        {
            setFoucs(betweenIslandsDepartDate);
            return false;
        }
        
        dateCompareRes=CompareDate(departDate.val(),betweenIslandsDepartDate.val());
        if(dateCompareRes==-1)
        {
            alert("Between Islands depart date should be greater than fly from home");
            setFoucs(betweenIslandsDepartDate);
            return false;
        }
        
         var txtReturnDate=$("#uscReturnToHome_txtReturnDate");
         if(!CheckEmptyField(txtReturnDate,"Date required"))
            return false;
            
        if(!validateDateFormat(txtReturnDate.val(),'U','F'))
        {
            setFoucs(txtReturnDate);
            return false;
        }
        
        dateCompareRes=CompareDate(betweenIslandsDepartDate.val(),txtReturnDate.val());
        if(dateCompareRes==-1)
        {
            alert("Return date should be greater than between Islands depart date");
            setFoucs(txtReturnDate);
            return false;
        }
    }

        //checking if the Island 1 is empty
       var ddlDestination1=$("#uscIsland1_ddlDestination");
        if(!CheckEmptyField(ddlDestination1,"Island1 required"))
            return false;
    
    if(tripType=="HotelOnly" || tripType=="HotelCar")
    {
        
        var betweenIslandsDepartDate=$("#uscBetweenIslands_txtDepartDate");
        var FlightRequired=$("#uscBetweenIslands_rblNeedFlightBetweenIsland_0");
       
        var CheckinDate=$("#uscIsland1_txtCheckinDate")
        if(!CheckEmptyField(CheckinDate,"Check-in date required"))
            return false;
        
        if(!validateDateFormat(CheckinDate.val(),'U','F'))
        {
            setFoucs(CheckinDate);
            return false;
        }
        
        //if the InterIsland flight is chosen
        if(FlightRequired.attr("CHECKED"))
            {
                var betweenIslandsDepartDate=$("#uscBetweenIslands_txtDepartDate");
                if(!CheckEmptyField(betweenIslandsDepartDate,"Date required"))
                    return false;

                if(!validateDateFormat(betweenIslandsDepartDate.val(),'U','F'))
                {
                    setFoucs(betweenIslandsDepartDate);
                    return false;
                }
                
                var CheckinDate=$("#uscIsland1_txtCheckinDate")
                
                dateCompareRes=CompareDate(CheckinDate.val(),betweenIslandsDepartDate.val());
                if(dateCompareRes==-1)
                {
                    alert("Between Islands depart date should be greater than Check-in date");
                    setFoucs(betweenIslandsDepartDate);
                    return false;
                }
               
                var CheckoutDate=$("#uscIsland2_txtCheckoutDate");
                if(!CheckEmptyField(CheckoutDate,"Check-out date required"))
                return false;
                
                if(!validateDateFormat(CheckoutDate.val(),'U','F'))
                {
                    setFoucs(CheckoutDate);
                    return false;
                }
                dateCompareRes=CompareDate(betweenIslandsDepartDate.val(),CheckoutDate.val());
                if(dateCompareRes==-1)
                {
                    alert("Island 2 check-out date should be greater than Between Islands depart date ");
                    setFoucs(CheckoutDate);
                    return false;
                }
            }
            else
            {
                var CheckoutDate=$("#uscIsland1_txtCheckoutDate");
                if(!CheckEmptyField(CheckoutDate,"Check-out date required"))
                    return false;
                if(!validateDateFormat(CheckoutDate.val(),'U','F'))
                {
                    setFoucs(CheckoutDate);
                    return false;
                }
                dateCompareRes=CompareDate(CheckinDate.val(),CheckoutDate.val());
                if(dateCompareRes==-1)
                {
                    alert("Check-out date should be greater than check-in date");
                    setFoucs(CheckoutDate);
                    return false;
                }
                var CheckinDate=$("#uscIsland2_txtCheckinDate")
                if(!CheckEmptyField(CheckinDate,"Check-in date required"))
                    return false;
                if(!validateDateFormat(CheckinDate.val(),'U','F'))
                {
                    setFoucs(CheckinDate);
                    return false;
                }
                var island1CheckoutDate=$("#uscIsland1_txtCheckoutDate");
                dateCompareRes=CompareDate(island1CheckoutDate.val(),CheckinDate.val());
                if(dateCompareRes==-1)
                {
                    alert("Island2 Check-in date should be greater than Island1 check-out date");
                    setFoucs(CheckinDate);
                    return false;
                } 
                var CheckoutDate=$("#uscIsland2_txtCheckoutDate");
                if(!CheckEmptyField(CheckoutDate,"Check-out date required"))
                    return false;
                if(!validateDateFormat(CheckoutDate.val(),'U','F'))
                {
                    setFoucs(CheckoutDate);
                    return false;
                }
                 dateCompareRes=CompareDate(CheckinDate.val(),CheckoutDate.val());
                if(dateCompareRes==-1)
                {
                    alert("Check-out date should be greater than check-in date");
                    setFoucs(CheckoutDate);
                    return false;
                }
            }
          }
    
        var ddlMinRating=$("#uscIsland1_ddlMinRating");
        var ddlMaxRating=$("#uscIsland1_ddlMaxRating");
        var MaxRatingDiff=$("#uscIsland1_hdnMaxRatingDiff");
        if(!ValidateRating(ddlMinRating,ddlMaxRating,MaxRatingDiff))
        return false;
        
        var ddlDestination2=$("#uscIsland2_ddlDestination");
        if(!CheckEmptyField(ddlDestination2,"Island2 required"))
            return false;

        if(trim(ddlDestination1.val())==trim(ddlDestination2.val()))
        {
            alert("Island1 and Island2 should not be same");
            setFoucs(ddlDestination2);
            return false;
        }
    var ddlMinRating=$("#uscIsland2_ddlMinRating");
    var ddlMaxRating=$("#uscIsland2_ddlMaxRating");
    var MaxRatingDiff=$("#uscIsland2_hdnMaxRatingDiff");
    
       if(!ValidateRating(ddlMinRating,ddlMaxRating,MaxRatingDiff))
            return false;

        //DisplayProcessingImage("searchDiv");
        if (top.location.href.search("/farefinder/") > 0) {
            parent.parent.BlockingPage(AgencyLogoPath);
        }
        else {
            BlockPage();
        }
    
    return true;
}

function BlockPage() {
    var div = document.createElement("div");
    div.cellSpacing = '1px';
    div.cellPadding = '2px';
    div.style.border = '0px';
    div.style.position = 'absolute';
    var top = 0;
    var left = 0;
    div.style.top = "0px";
    div.style.left = "0px";
    div.id = 'blockdiv';
    document.body.appendChild(div);
    if ($.browser.msie) {
        div.style.height = $(document).height();
        div.style.width = $(document).width();
    }
    else {
        div.style.height = document.height + 50 + "px";
        div.style.width = "100%";
    }
    $('#blockdiv').block({ message: null });
    if (AgencyLogoPath.length > 0) {
        document.getElementById("imgAgencyLogo").src = AgencyLogoPath;
        document.getElementById("imgAgencyLogo").style.visibility = "visible";
    }
    else
        document.getElementById("imgAgencyLogo").style.visibility = "hidden";


}

//Validating MinRate and MaxRate.
function ValidateRating(minRateCtl, maxRateCtl,MaxRatingDiffCtl)
{
    var DiffValue;
    
    DiffValue=parseInt(maxRateCtl.val())-parseInt(minRateCtl.val());
    
    if(DiffValue<0)
    {
        alert('MaxRating should be greater than MinRating')
        setFoucs(maxRateCtl);
        return false;
    }
    
    var MaxRatingDiff=parseInt(MaxRatingDiffCtl.val())
    if(DiffValue>MaxRatingDiff)
    {
        alert('Difference between MaxRating and MinRating should not be greater than ' + MaxRatingDiff)
        setFoucs(maxRateCtl);
        return false;
    }
        
    return true;    
}




function CheckEmptyField(Clt,ErrMsg)
{
    if(Clt.val()=="")
    {
        alert(ErrMsg);
        setFoucs(Clt);
        return false;
    }
    return true;
}

function setFoucs(ctl)
{
        ctl.select();
        ctl.focus();
}

function UpdateDate(SourceCtlId,TargetCtlId)
{
    var SourceCtl=$("#"+SourceCtlId);
    var TargetCtl=$("#"+TargetCtlId);
    var DaysAdvance=$("#hdnHotelDayInterval").val();
    
    if(validateDateFormat(SourceCtl.val(),'U','F'))
    {
        var ChkinDate=new Date(SourceCtl.val());
        var newCheckOutDate=new Date(ChkinDate.getTime()+ parseInt(DaysAdvance)*24*60*60*1000);
        var day,month;
        day=newCheckOutDate.getDate();
        month=newCheckOutDate.getMonth() +1;
    
        if(day.toString().length<2)
            day='0'+day.toString();
        if(month.toString().length<2) 
            month='0'+month.toString(); 
        
        TargetCtl.val(month+'/'+day+'/'+ newCheckOutDate.getFullYear());
    }
}



