﻿/*
#region "History"
'20090505 - RH - Removed old obsolete search stuff
'20090505 - RH - Removed Token from querystring, we dont need it, comes from the session
'20090310 - RH - Removed duff UseDefaults function
'20090310 - RH - Changed mode definitions
'20090310 - RH - Changes to hide/show areas instead of disabling
'20090312 - RH - Tweaked usedefaults to function correctly
'20090324 - RH - Added in date validation
'20090326 - RH - Changed formatting
'20090326 - RH - Added ajax form population, to allow for valid route setup
'20090331 - RH - Work to retain values floowing adition of ajax updates
'20090331 - RH - ie fix, and error handling alterations
'20090408 - RH - locations seclection ajax fix
#end region
*/
  
function SearchBox() {
    this.Mode='PS';
    
    this.Start=function() {
        //this.Change(this.Mode);
        this.UseDefaults();
    }; 
    
    this.Change=function(newmode, pop) {
        this.Mode=newmode;

        switch(this.Mode) {                       
            case 'AS':
                $('sb_arr1').show();
                $('sb_arr2').hide();
                $('sb_dep').hide();
                $('sb_rm_span').show();
                $('sb_rm').show();
                if(pop) this.PopulateDestinations(0);
                break;
            case 'FS':
                $('sb_arr2').show();
                $('sb_arr1').hide(); 
                $('sb_dep').show();           
                $('sb_rm').value=1;
                $('sb_rm_span').hide();
                $('sb_rm').hide();
                if(pop) this.PopulateDestinations($F('sb_dep'));
                break;
            case 'PS':
                $('sb_dep').show();   
                $('sb_arr1').show();
                $('sb_arr2').hide();
                $('sb_rm_span').show();
                $('sb_rm').show();
                if(pop) this.PopulateDestinations($F('sb_dep'));
                break;
        }   
        
        if(!pop) this.Populate();    
    };
        
    this.Populate=function() {
        this.Populate_Rooms();              
        this.Populate_Duration();
        this.Populate_Passengers();        
    }
    
    this.Populate_Rooms=function() {
        var curval=$F('sb_rm');

        $('sb_rm').options.length=0;
        
        for(var i=1; i<7; i++) {
            if(i==1) {
                $('sb_rm').options.add(new Option(i + ' Room', i));
            } else {
                $('sb_rm').options.add(new Option(i + ' Rooms', i));
            }
        }      
        
        $('sb_rm').value=curval;    
    }
    
    this.GetOccupancyString=function() {
        var occ='';
        
        for(var i=1; i<=$F('sb_rm');i++) {       
            occ += $F('sb_ad_' + i) + ',' + $F('sb_ch_' + i)+',' + $F('sb_in_' + i);
            if(i<$F('sb_rm')) {
                occ+='|'
            }
        } 
        
        return occ;
    }

    this.Go = function() {
        var s = '';
       
        
          if ($F('sb_ad_1') == '0' && $F('sb_ch_1')==0)   { 
            Website.Alert('Warning', 'Please select passengers');
                    return;
                  }   

        
        for(var i=1; i<=$F('sb_rm');i++) {  
        if ($F('sb_ch_' + i) == '99')   {  
            Website.Alert('Warning', 'Please select children');
                    return;
                  }  
                  
          if ($F('sb_in_' + i) == '99')   {  
            Website.Alert('Warning', 'Please select infants');
                    return;
                  }           

        } 
               
        switch (this.Mode) {
            case 'AS':
                if ($F('sb_arr1') == '0') {
                    Website.Alert('Warning', 'Destination is required');
                    return;
                }

                s = qsb(s, 'Action', 'AS');
                s = qsb(s, 'StartDate', $F('sb_sd'));
                s = qsb(s, 'Duration', $F('sb_dur'));
                s = qsb(s, 'DestinationLocationID', $F('sb_arr1'));
                s = qsb(s, 'Occupancy', this.GetOccupancyString())

                break;
            case 'FS':
                if ($F('sb_dep') == '0') {
                    Website.Alert('Warning', 'Departure airport is required');
                    return;
                }

                if ($F('sb_arr2') == '0') {
                    Website.Alert('Warning', 'Destination is required');
                    return;
                }
                
                s = qsb(s, 'Action', 'FS');
                s = qsb(s, 'StartDate', $F('sb_sd'));
                s = qsb(s, 'Duration', $F('sb_dur'));
                s = qsb(s, 'Occupancy', this.GetOccupancyString());
                s = qsb(s, 'DepartureLocationID', $F('sb_dep'));
                s = qsb(s, 'DestinationLocationID', $F('sb_arr2'));
                break;
            case 'PS':
                if ($F('sb_dep') == '0') {
                    Website.Alert('Warning', 'Departure airport is required');
                    return;
                }

                if ($F('sb_arr1') == '0') {
                    Website.Alert('Warning', 'Destination is required');
                    return;
                }
                
                s = qsb(s, 'Action', 'PS');
                s = qsb(s, 'StartDate', $F('sb_sd'));
                s = qsb(s, 'Duration', $F('sb_dur'));
                s = qsb(s, 'Occupancy', this.GetOccupancyString());
                s = qsb(s, 'DepartureLocationID', $F('sb_dep'));
                s = qsb(s, 'DestinationLocationID', $F('sb_arr1'));
                break;
        }
         
        var startdate = new Date($F('sb_sd'))
        var todaysdate = new Date($F('today'));
        
        if(startdate<todaysdate) {
            Website.Alert('Warning', 'This date has passed');
            return;
        }
        
        $('content').hide();
        
    
        top.location.href = '/Search.aspx?Searching=True&ClearBasket=True' + s;
    } 
    
    this.Build=function(string, name, value) {
        return string + '&' + name + '=' +value;
    }
    
    
    this.PopulateAirports=function(i)  {
    
  
       if(this.Mode=='PS') { 
         var currval=$F('sb_dep');  
            $('sb_dep').options.length=0;
            $('sb_dep').options.add(new Option('Loading...', 0, true));  
            
            
              new Ajax.Request('/ajax.ashx?_t=' + ticks() + '&action=VAD&LID=' + i, {
                method:'get',
                onSuccess: function(transport) {
                    try {
                        $('sb_dep').options.length=0;
                        $('sb_dep').options.add(new Option('Select Departure Airport...', 0, true)); 
                        Website.SearchBox.Data=transport.responseText.evalJSON() || 'No Response'; 
                        Website.SearchBox.Data.Locations.each(function(item) {
                        $('sb_dep').options.add(new Option(item.Label, item.ID));  
                           
                        });
                        $('sb_dep').value=currval;
                    } catch(e) {
                        //Website.HandleError('Error Populating: ' + e);
                    }
                },
                onFailure: function() { 
                   Website.HandleError('Error Populating');
                }
            });  
       }
    
    
       if(this.Mode=='FS') { 
            var currval=$F('sb_dep');                       
            $('sb_dep').options.length=0;
            $('sb_dep').options.add(new Option('Loading...', 0, true));   
                        
            new Ajax.Request('/ajax.ashx?_t=' + ticks() + '&action=VAD&LID=' + i, {
                method:'get',
                onSuccess: function(transport) {
                    try {
                        $('sb_dep').options.length=0;
                        $('sb_dep').options.add(new Option('Select Departure Airport...', 0, true)); 
                        Website.SearchBox.Data=transport.responseText.evalJSON() || 'No Response'; 
                        Website.SearchBox.Data.Locations.each(function(item) {
                           
                                $('sb_dep').options.add(new Option(item.Label, item.ID));                              }
                        );
                        $('sb_dep').value=currval;
                    } catch(e) {
                        //Website.HandleError('Error Populating: ' + e);
                    }
                },
                onFailure: function() { 
                   Website.HandleError('Error Populating');
                }
            });  
    }
    
    }
    
    this.PopulateDestinations=function(i) {
    
        if(this.Mode=='FS') { 
            var currval=$F('sb_arr2'); 
                                 
            $('sb_arr2').options.length=0;
            $('sb_arr2').options.add(new Option('Loading...', 0, true));   
                        
            new Ajax.Request('/ajax.ashx?_t=' + ticks() + '&action=VD&LID=' + i, {
                method:'get',
                onSuccess: function(transport) {
                    try {
                        $('sb_arr2').options.length=0;
                        $('sb_arr2').options.add(new Option('Select Arrival Airport...', 0, true)); 
                        Website.SearchBox.Data=transport.responseText.evalJSON() || 'No Response'; 
                        Website.SearchBox.Data.Locations.each(function(item) {
                         if(item){
                            if(item.Type==2) {
                                $('sb_arr2').options.add(new Option(item.Label, item.ID));  
                            }
                            };
                        });
                        $('sb_arr2').value=currval;
                          
                      
                    } catch(e) {
                    //alert(e);
                        //Website.HandleError('Error Populating: ' + e);
                    }
                },
                onFailure: function() { 
                   Website.HandleError('Error Populating');
                }
            });  
            
            
            
            
            
            
        }
       
        
        if(this.Mode=='PS' || this.Mode=='AS') {
            var currval=$F('sb_arr1');
            
            if($('SearchBox_Defaults_DestinationLocationID')) currval=$F('SearchBox_Defaults_DestinationLocationID');                             
            
            $('sb_arr1').options.length=0;
            $('sb_arr1').options.add(new Option('Loading...', 0, true));   
                        
            new Ajax.Request('/ajax.ashx?_t=' + ticks() + '&action=VD&LID=' + i, {
                method:'get',
                onSuccess: function(transport) {
                    try {
                        $('sb_arr1').options.length=0;
                        $('sb_arr1').options.add(new Option('Select Destination...', 0, true));   
                        
                        Website.SearchBox.Data=transport.responseText.evalJSON() || 'No Response'; 
                        Website.SearchBox.Data.Locations.each(function(item) {
                            if(item){
				    if(item.Type==5) {
					var o=new Option(item.Label, item.ID);

					o.style.fontWeight='bold';

					$('sb_arr1').options.add(o);  

					if (item.Children) {
					    item.Children.each(function(item2) {
						if (item2.Type){
						if (item2.Type == 4) {
						    $('sb_arr1').options.add(new Option("\xA0\xA0\xA0\xA0" + item2.Label, item2.ID));
						};
						};
					    });
					};
				    };
                            };
                        });
                        
                        $('sb_arr1').value=currval;
                        // alert(currval);
                    } catch(e) {
                    	//alert(e);
                        //Website.HandleError('Error Populating: ' + e);
                    }
                },
                onFailure: function() { 
                   Website.HandleError('Error Populating');
                }
            }); 
        }
    }
    
    this.Populate_Duration=function() {
        var currval=$F('sb_dur');
        
        $('sb_dur').options.length=0;
        
        for(var i=1; i<29; i++) {
            if(i%7==0) {
                if(i==7) {
                    $('sb_dur').options.add(new Option('1 Week', 7, true));
                } else {
                    $('sb_dur').options.add(new Option((i/7) + ' Weeks', i));
                }
            } else {                                  
                if(i==1) {
                     $('sb_dur').options.add(new Option(i + ' Night', i));
                } else {
                     $('sb_dur').options.add(new Option(i + ' Nights', i));
                }                                
            }
        }  
        
        $('sb_dur').value=currval;    
    }
    
    this.Populate_Passengers=function() {
        var ca;
        var cc;
        var ci;
        
        for(var i=1; i<=6; i++) {
            $('sb_ad_' + i).style.display='none';
            $('sb_ch_' + i).style.display='none';
            $('sb_in_' + i).style.display='none';
        }
        
        for(var i=1; i<=$F('sb_rm'); i++) {
            ca=$F('sb_ad_' + i);
            cc=$F('sb_ch_' + i);
            ci=$F('sb_in_' + i);
            
            //adults,children,infants
            $('sb_ad_' + i).options.length=0;
            $('sb_ch_' + i).options.length=0;
            $('sb_in_' + i).options.length=0;
            
            $('sb_ad_' + i).style.display='block';
            $('sb_ch_' + i).style.display='block';
            $('sb_in_' + i).style.display='block';
            
       
               
             $('sb_ch_' + i).options.add(new Option('Ages 2-11',99));
             $('sb_in_' + i).options.add(new Option('Ages 0-1',99));
             
             $('sb_ch_' + i).options.add(new Option('',99));
             $('sb_in_' + i).options.add(new Option('',99));
            
             $('sb_ad_' + i).options.add(new Option('0 Adults',0));
             $('sb_ch_' + i).options.add(new Option('0 Children',0));
             $('sb_in_' + i).options.add(new Option('0 Infants',0));
            
          
         
            for(var j=1;j<10;j++) {               
                $('sb_ad_' + i).options.add(new Option(j + (j==1?' Adult':' Adults'),j));
                $('sb_ch_' + i).options.add(new Option(j + (j==1?' Child':' Children'),j));
                $('sb_in_' + i).options.add(new Option(j + (j==1?' Infant':' Infants'),j));
            }   
            
            $('sb_ad_' + i).value=ca;
            $('sb_ch_' + i).value=cc;
            $('sb_in_' + i).value=ci;
        } 
    }
    
    this.UseDefaults=function() {
        this.Change($F('sb_def_m'),false);   
        
        $('sb_type_' + this.Mode).checked=true;                
        
        
        $('sb_dep').value=$F('sb_def_dli');        
                       
        if(this.Mode=='FS') {        
            if(!HasItem($('sb_arr2'),$F('sb_def_ali'))){
                $('sb_arr2').options.add(new Option($F('sb_def_ali'),$F('sb_def_ali')));
                $('sb_arr2').value=$F('sb_def_ali');
            } else {
                $('sb_arr2').value=$F('sb_def_ali');
            };
        } else {
            //$('sb_arr1').value=$F('sb_def_ali');
            
            if(!HasItem($('sb_arr1'),$F('sb_def_ali'))){
                $('sb_arr1').options.add(new Option($F('sb_def_ali'),$F('sb_def_ali')));
                $('sb_arr1').value=$F('sb_def_ali');
            } else {
                $('sb_arr1').value=$F('sb_def_ali');
            };
            
        }
        
        $('sb_sd').value=$F('sb_def_sd');
        $('sb_dur').value=$F('sb_def_d');
                
        var occupancy= $F('sb_def_oc').split('|');
        
        $('sb_rm').value=occupancy.size(); 
        
        this.Populate_Passengers();
        
        occupancy.each(function(s, index) {        
            $('sb_ad_' + (index+1)).value=occupancy[index].split(',')[0];            
            $('sb_ch_' + (index+1)).value=occupancy[index].split(',')[1];
            $('sb_in_' + (index+1)).value=occupancy[index].split(',')[2];
        });   
                
        if($('SearchBox_Defaults_DestinationLocationID')) $('sb_arr1').value=$F('SearchBox_Defaults_DestinationLocationID');                             
        
        switch(this.Mode) {                       
            case 'AS':
                this.PopulateDestinations(0);
                break;
            case 'FS':
                this.PopulateDestinations($F('sb_dep'));
                break;
            case 'PS':
                this.PopulateDestinations($F('sb_dep'));
                break;
        }   
    }    
}