//var iChars = "*|,\":<>[]{}`\';()&$#%";
//var illegalChars = /[\W_]/;          
    var illegalChars= /[\(\)\<\>\;\:\\\/\"\[\]]/;
    daysinmonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    themonths = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov','Dec');
    var adddays = 0;  
    var adddays2 = 0;
    var DepartureMonth = 0;
    var DepartureDay = 0;
    var ArrivalMonth = 0;
    var ArrivalDay = 0;
    var ArrivalYear = 0;
    function zzznumofmonth(mon){
            if(mon=='Jan')    return '0';
            else if(mon=='Feb') return '1';
            else if(mon =='Mar') return '2';
            else if(mon =='Apr') return '3';
            else if(mon =='May') return '4';
            else if(mon =='Jun') return '5';
            else if(mon =='Jul') return '6';
            else if(mon =='Aug') return '7';
            else if(mon =='Sep') return '8';
            else if(mon =='Oct') return '9';
            else if(mon =='Nov') return '10';
            else if(mon =='Dec') return '11';
    }            
    //1
    
    
    function checkCruiseForm(){
        var theactionURL;
        
        var cruise_month = "";
        var cruise_year = "";
        // Cruise date is given in the following format mmXyyyy, split into month and year - not used at the moment
        var cruise_monthyear = document.CruiseSearch.dates.value;
        if ((cruise_monthyear != null)  && (cruise_monthyear != "")) {
            var dateSplit = cruise_monthyear.split("X");
            // The split must have resulted in two elements
            if ((dateSplit != null) && (dateSplit.length == 2)) {
                cruise_month = dateSplit[0];
                cruise_year = dateSplit[1];
            }
        }
        theactionURL = "http://www.aaa.com/scripts/WebObjects.dll/AAAOnline?page=PartnerRedirect&PID=412&association=caa&club=273" + 
            "&destination=" + document.CruiseSearch.destination.value  +
            "&cruiseline=" + document.CruiseSearch.cruiseline.value +
            "&dur=" + document.CruiseSearch.duration.value +
            "&date=" + cruise_monthyear;
        document.CruiseSearch.action = theactionURL;
        document.CruiseSearch.submit();         
    }
  
  	function checkNewCruiseForm(){
        var theactionURL;
        
        theactionURL = "http://www.bcaa.cruiseshipcenters.ca/Cruise/Search.aspx?selection=.ca&ReturnURL=cruiseshipcenters.ca&URL=cruise" + 
      "&cl=" + document.CruiseSearch.cruiseline.value +
			"&ds=" + document.CruiseSearch.destination.value  +
			"&dtf=" + document.CruiseSearch.dates.value +
      "&dr=" + document.CruiseSearch.duration.value;
            
        document.CruiseSearch.action = theactionURL;
        document.CruiseSearch.submit();         
    }
    
    function IScheckbox(){
        if(document.HotelSearch.includeSurroundings.checked)
            document.HotelSearch.includeSurroundings.value = 'YES';
        else
            document.HotelSearch.includeSurroundings.value = 'NO';
    }
    
    function FDcheckbox(){
        if(document.AirSearch.flexibleDates.checked)
            document.AirSearch.flexibleDates.value = 'YES';
        else
            document.AirSearch.flexibleDates.value = 'NO';
    }
    function checkHotelForm(){
        var nowdate = new Date(); 
        nowmonth = nowdate.getMonth();
        nowday = nowdate.getDate();
        nowyear = nowdate.getFullYear();
        nowfulldate = new Date(nowyear, nowmonth, nowday);
        DepartureMonth = zzznumofmonth(document.HotelSearch.checkinMonth.value);
        DepartureDay = document.HotelSearch.checkinDay.value;
        ArrivalMonth = zzznumofmonth(document.HotelSearch.checkoutMonth.value);
        ArrivalDay = document.HotelSearch.checkoutDay.value;
        if(new Date(nowyear, DepartureMonth, DepartureDay) < nowfulldate)
            DepartureYear = nowyear + 1;
        else 
            DepartureYear = nowyear;
        var Depfulldate = new Date(DepartureYear, DepartureMonth, DepartureDay);
        if(new Date(nowyear, ArrivalMonth, ArrivalDay) < nowfulldate)
            ArrivalYear = nowyear + 1;
        else 
            ArrivalYear = nowyear;        
        Arrfulldate = new Date(ArrivalYear, ArrivalMonth, ArrivalDay);
        NumdaysMonthDep = TotalDaysMonth(DepartureMonth, DepartureYear);
        NumdaysMonthArr = TotalDaysMonth(ArrivalMonth, ArrivalYear);
        
        if (DepartureDay > NumdaysMonthDep){
            alert("Please enter a valid check in date");
            document.HotelSearch.checkinDay.focus();
        }
        else if (ArrivalDay > NumdaysMonthArr){
                alert("Please enter a valid check out date");
                document.HotelSearch.checkoutDay.focus();
            }
            else if(Depfulldate > Arrfulldate){
                alert("Please enter a check in date that is prior to your check out date.");
                document.HotelSearch.checkoutDay.focus();
            }
                else if(illegalChars.test(document.HotelSearch.city.value)){
                    alert("Please enter the city using the city name and the 2 letter code for province or state (eg Toronto, ON or Seattle, WA). For cities outside of Canada or the US, enter only the city name (eg Paris)");   
                    document.HotelSearch.city.focus();
                }
                    else if(document.HotelSearch.city.value == ""){
                     alert("Please enter a city name"); 
                     document.HotelSearch.city.focus();
                    }
                        else
                            
                            {
                                var this_location = location;
                                var thedestination = document.HotelSearch.city.value;
                                var theplace = thedestination.toLowerCase();
                                var newplace = checkHotelCity(theplace);
                                document.HotelSearch.city.value = newplace;            
                                if (theplace != newplace){
                                    //ATI URLs
                                    if(/myportal/.test(this_location)){
                                    document.HotelSearch.action = '/wps/myportal/utilities/quickfinder';
                                    document.HotelSearch.submit(); 
                                    }
                                    else{
                                document.HotelSearch.action = 'http://acsc.americantours.com/searchHTL.php';
                                document.HotelSearch.submit(); 
                                        }
                                    }
                                else if(/myportal/.test(this_location)){
                                document.HotelSearch.action = '/wps/myportal/utilities/quickfinder';
                                document.HotelSearch.submit(); 
                                    }
                                else{
                                document.HotelSearch.action = '/wps/portal/utilities/quickfinder';
                                document.HotelSearch.submit(); 
                                }
                         } 
            }
            
// Add the ATI locations below
    function checkHotelCity(city){
        var result = 0;
        switch (result){
            case (city.indexOf('las vegas')):
                return 'Las Vegas';
              break    
            case (city.indexOf('vegas')):
                return 'Las Vegas';
              break    
            case (city.indexOf('new york')):
                  return 'New York';
            break
            case (city.indexOf('los angeles')):
                  return 'Los Angeles';
            break
            case (city.indexOf('anaheim')):
                  return 'Anaheim';
            break
            case (city.indexOf('disneyland')):
                  return 'Anaheim';
            break
            case (city.indexOf('san diego')):
                  return 'San Diego';
            break
            case (city.indexOf('san francisco')):
                  return 'San Francisco';
            break
            case (city.indexOf('miami')):
                  return 'Miami';
            break
            case (city.indexOf('orlando')):
                  return 'Orlando';
            break
            case (city.indexOf('disneyworld')):
                  return 'Orlando';
            break
            case (city.indexOf('ft. lauderdale')):
                  return 'Ft. Lauderdale';
            break
            case (city.indexOf('phoenix')):
                  return 'Phoenix';
            break
            case (city.indexOf('scottsdale')):
                  return 'Scottsdale';
            break
            case (city.indexOf('seattle')):
                  return 'Seattle';
            break
            case (city.indexOf('flagstaff')):
                  return 'Flagstaff';
            break
            case (city.indexOf('palm springs')):
                  return 'Palm Springs';
            break
            case (city.indexOf('santa barbara')):
                  return 'Santa Barbara';
            break
            case (city.indexOf('naples')):
                  return 'Naples';
            break
            case (city.indexOf('bonita springs')):
                  return 'Bonita Springs';
            break
            case (city.indexOf('napa')):
                  return 'Napa';
            break
            case (city.indexOf('sonoma')):
                  return 'Sonoma';
            break
            case (city.indexOf('honolulu')):
                  return 'Honolulu';
            break
            case (city.indexOf('maui')):
                  return 'Maui';
            break
            case (city.indexOf('calgary')):
                  return 'Calgary';
            break
            case (city.indexOf('toronto')):
                  return 'Toronto';
            break
            default:
                return city;
        }
    }


function checkCarForm(){    
            var nowdate = new Date(); 
            nowmonth = nowdate.getMonth();
            nowday = nowdate.getDate();
            nowyear = nowdate.getFullYear();
            nowfulldate = new Date(nowyear, nowmonth, nowday);
            DepartureMonth = zzznumofmonth(document.CarSearch.pickupMonth.value);
            DepartureDay = document.CarSearch.pickupDay.value;
            ArrivalMonth = zzznumofmonth(document.CarSearch.dropoffMonth.value);
            ArrivalDay = document.CarSearch.dropoffDay.value;        
            if(new Date(nowyear, DepartureMonth, DepartureDay) < nowfulldate)
                DepartureYear = nowyear + 1;
            else 
                DepartureYear = nowyear;            
            var Depfulldate = new Date(DepartureYear, DepartureMonth, DepartureDay);
            if(new Date(nowyear, ArrivalMonth, ArrivalDay) < nowfulldate)
                ArrivalYear = nowyear + 1;
            else 
                ArrivalYear = nowyear;        
            Arrfulldate = new Date(ArrivalYear, ArrivalMonth, ArrivalDay);
            NumdaysMonthDep = TotalDaysMonth(DepartureMonth, DepartureYear);
            NumdaysMonthArr = TotalDaysMonth(ArrivalMonth, ArrivalYear);
            var chkdepdate = Depfulldate.getDate() + '/' + Depfulldate.getMonth() + '/' + Depfulldate.getFullYear();
            var chkarrdate = Arrfulldate.getDate() + '/' + Arrfulldate.getMonth() + '/' + Arrfulldate.getFullYear();
            RangeDates(); 
            if (DepartureDay > NumdaysMonthDep)
                alert("Please enter a valid pick up date");
            else if (ArrivalDay > NumdaysMonthArr)
                alert("Please enter a valid drop off date");
            else if(Depfulldate > Arrfulldate){
                alert("Please enter a pick up date that is prior to the drop off date");
                document.CarSearch.dropoffDay.focus();
            }
                else if(illegalChars.test(document.CarSearch.pickupCity.value)){
                    alert("Please enter the pick up city using the city name and the 2 letter code for province or state (eg Toronto, ON or Seattle, WA). For cities outside of Canada or the US, enter only the city name (eg Paris)");   
                    document.CarSearch.pickupCity.focus();         
                }
                else if(document.CarSearch.pickupCity.value == ""){
                     alert("Please enter a pick up city"); 
                     document.CarSearch.pickupCity.focus();
                }
                   else if(document.CarSearch.zipcode.value == ""){
                     alert("Please enter the postal code as it appears on your drivers licence"); 
                     document.CarSearch.zipcode.focus();
                }
                else if((chkdepdate == chkarrdate) && (document.CarSearch.pickupTime.value=='Any time')){
                    alert('Sorry, the "Anytime" option is not available for same day search');
                    document.CarSearch.pickupTime.focus();
                    }
                else if((chkdepdate == chkarrdate) && (document.CarSearch.dropoffTime.value=='Any time')){
                    alert('Sorry, the "Anytime" option is not available for same day travel');
                    document.CarSearch.dropoffTime.focus();
                }
                else
                   {
                   var this_location = location;
                    if(/myportal/.test(this_location)){
                        document.CarSearch.action = "/wps/myportal/utilities/quickfinder";
                        document.CarSearch.submit(); 
                        }
                    else{
                        document.CarSearch.action = "/wps/portal/utilities/quickfinder";
                       document.CarSearch.submit(); 
                    }
             } 
    }
    function checkAirForm(){
        var nowdate = new Date(); 
        nowmonth = nowdate.getMonth();
        nowday = nowdate.getDate();
        nowyear = nowdate.getFullYear();
        nowfulldate = new Date(nowyear, nowmonth, nowday);
        DepartureMonth = zzznumofmonth(document.AirSearch.depMonth.value);
        DepartureDay = document.AirSearch.depDay.value;
        ArrivalMonth = zzznumofmonth(document.AirSearch.retMonth.value);
        ArrivalDay = document.AirSearch.retDay.value;        
        if(new Date(nowyear, DepartureMonth, DepartureDay) < nowfulldate)
            DepartureYear = nowyear + 1;
        else 
            DepartureYear = nowyear;        
        var Depfulldate = new Date(DepartureYear, DepartureMonth, DepartureDay);
        if(new Date(nowyear, ArrivalMonth, ArrivalDay) < nowfulldate)
            ArrivalYear = nowyear + 1;
            else 
            ArrivalYear = nowyear;
        Arrfulldate = new Date(ArrivalYear, ArrivalMonth, ArrivalDay);
        NumdaysMonthDep = TotalDaysMonth(DepartureMonth, DepartureYear);
        NumdaysMonthArr = TotalDaysMonth(ArrivalMonth, ArrivalYear);
        var chkdepdate = Depfulldate.getDate() + '/' + Depfulldate.getMonth() + '/' + Depfulldate.getFullYear();
        var chkarrdate = Arrfulldate.getDate() + '/' + Arrfulldate.getMonth() + '/' + Arrfulldate.getFullYear();
        RangeDates();
        if((Depfulldate < nowfulldate) || (Depfulldate > TopRangeDate)){
            alert('Please select a departure date between ' + themonths[nowfulldate.getMonth()] + ' ' + (nowfulldate.getDate()+1) + ',' +  nowfulldate.getYear() + ' and ' + themonths[TopRangeDate.getMonth()] + ' ' + TopRangeDate.getDate() + ',' +  TopRangeDate.getYear() + ' .');
            document.AirSearch.depDay.focus();
        }
        else if((Arrfulldate < nowfulldate) || (Arrfulldate > TopRangeDate)){
            alert('Please select a return date between ' + themonths[nowfulldate.getMonth()] + ' ' + nowfulldate.getDate() + ',' +  nowfulldate.getYear() + ' and ' + themonths[TopRangeDate.getMonth()] + ' ' + TopRangeDate.getDate() + ',' +  TopRangeDate.getYear() + ' .');
            document.AirSearch.retDay.focus();
        }
        else if(Depfulldate > Arrfulldate){
            alert("Please enter a departure date that is prior to your return date");
            document.AirSearch.retDay.focus();
        }
        else if (DepartureDay > NumdaysMonthDep){
            alert("Please enter a valid departure date");
            document.AirSearch.depDay.focus();
        }
        else if (ArrivalDay > NumdaysMonthArr){
            alert("Please enter a valid arrival date");
            document.AirSearch.retDay.focus();
        }
        else if(illegalChars.test(document.AirSearch.depCity.value)){
            alert("Please enter the departure city using the city name and the 2 letter code for province or state (eg Toronto, ON or Seattle, WA). For cities outside of Canada or the US, enter only the city name (eg Paris)");   
            document.AirSearch.depCity.focus();
        }
        else if(illegalChars.test(document.AirSearch.arrCity.value)){
            alert("Please enter the destination city using the city name and the 2 letter code for province or state (eg Toronto, ON or Seattle, WA). For cities outside of Canada or the US, enter only the city name (eg Paris");   
            document.AirSearch.arrCity.focus();                  
        }
        else if(document.AirSearch.depCity.value == "") {
            alert("Please enter a departure city"); 
            document.AirSearch.depCity.focus();
        }
        else if(document.AirSearch.arrCity.value == ""){
            alert("Please enter a destination city"); 
            document.AirSearch.arrCity.focus();
        }
        else if(document.AirSearch.depCity.value.length < 3) {
            alert("Please enter a departure city with 3 letters or more"); 
            document.AirSearch.depCity.focus();
        }
        else if(document.AirSearch.arrCity.value.length < 3) {
            alert("Please enter a destination city with 3 letters or more"); 
            document.AirSearch.arrCity.focus();
        }
    else if((chkdepdate == chkarrdate) && (document.AirSearch.depTime.value=='Any time')){
            alert('Sorry, the "Anytime" option is not available for same day travel');
            document.AirSearch.depTime.focus();
            }
    else if((chkdepdate == chkarrdate) && (document.AirSearch.retTime.value=='Any time')){
            alert('Sorry, the "Anytime" option is not available for same day travel');
            document.AirSearch.depTime.focus();
            }
    else if((chkdepdate == chkarrdate) && (document.AirSearch.retTime.value==document.AirSearch.depTime.value)){
            alert('Sorry, the departure time and the return time cannot be the same for same day travel');
            document.AirSearch.depTime.focus();
            }
    //else if(document.AirSearch.depCity.value == 'vancouver')
        //document.AirSearch.depCity.value = 'YVR';
    //else if(document.AirSearch.arrCity.value = 'toronto')        
        //document.AirSearch.arrCity.value = 'YYZ';
        else
            {
            validateDestinations();
               var this_location = location;
            if(/myportal/.test(this_location)){
                document.AirSearch.action = "/wps/myportal/utilities/quickfinder";
                document.AirSearch.submit(); 
            }
            else{
                document.AirSearch.action = "/wps/portal/utilities/quickfinder";
                document.AirSearch.submit(); 
            }                    
           } 
         }
    function validateDestinations(){
        departure = document.AirSearch.depCity.value;    
        var thedepcode = checkdestination(departure.toLowerCase());
        var newdepcode = checkprovince(thedepcode);
        document.AirSearch.depCity.value = newdepcode;            
        arrival = document.AirSearch.arrCity.value;    
        var thearrcode = checkdestination(arrival.toLowerCase());
        var newarrcode = checkprovince(thearrcode);
        document.AirSearch.arrCity.value = newarrcode;
    }
    function checkprovince(thedepcode){
        if(/b.c./.test(thedepcode)){
            if(/vancouver/.test(thedepcode))
                return 'YVR'
            else
                return thedepcode.replace(/b.c./,'BC');
        }
        else if(/a.b./.test(thedepcode))
            return thedepcode.replace(/a.b./,'AB');
        else if(/ont./.test(thedepcode))
            return thedepcode.replace(/ont./,'ON');
        else if(/alta./.test(thedepcode))
            return thedepcode.replace(/alta./,'AB');
        else
            return thedepcode;
    }
        
    function checkdestination(city){
        switch (city){
            case 'vancouver':
                return 'YVR';
              break    
            case 'toronto':
                  return 'YYZ';
            break
            case 'calgary':
                  return 'YYC';
            break
            case 'winnipeg':
                return 'YWG';
            break
            case 'edmonton':
                  return 'YEG';
            break
            case 'victoria':
                  return 'YYJ';
            break
            case 'montreal':
                  return 'YUL';
            break
            case 'saskatoon':
                  return 'YXE';
            break
            case 'halifax':
                  return 'YHZ';
            break
            case 'ottawa':
                  return 'YOW';
            break
            case 'saint john':
                  return 'YSJ';
            break
            default:
                return city;
        }
    }
    function TotalDaysMonth(chckmonth, chckyear) {
        if(chckmonth==1){
            if (((chckyear % 4)==0) && ((chckyear % 100)!=0) || ((chckyear % 400)==0))
                {
                return 29;}
            else 
                {
                return 28; }
        }
        else{
        //alert(daysinmonth[chckmonth]);
        return daysinmonth[chckmonth];}
    }
    
    function RangeDates(){
      adddays=331;
      // Change
      /* SettingDates(adddays, 0);
         TRMonth = DepartureMonth;
         TRDay = DepartureDay;
         TRYear = DepartureYear;
         TopRangeDate = new Date(TRYear, TRMonth, TRDay);
       */
      futureDate = new Date();    
      futureDate.setDate(futureDate.getDate() + adddays);
      TopRangeDate = futureDate;
    }
    
    function SettingAirDates(){
        adddays = 21;
        adddays2 = 7;
        SettingDates(adddays,adddays2);
        document.AirSearch.depMonth.value = themonths[DepartureMonth];
        document.AirSearch.depDay.value = DepartureDay;
        document.AirSearch.retMonth.value = themonths[ArrivalMonth];
        document.AirSearch.retDay.value = ArrivalDay;
    }
    
    function SettingCarDates(){
        adddays = 21;
        adddays2 = 7;
        SettingDates(adddays,adddays2);
        document.CarSearch.pickupMonth.value = themonths[DepartureMonth];
        document.CarSearch.pickupDay.value = DepartureDay;
        document.CarSearch.dropoffMonth.value = themonths[ArrivalMonth];
        document.CarSearch.dropoffDay.value = ArrivalDay;
    }
    function SettingHotelDates(){
        adddays = 0;
        adddays2 = 2;
        SettingDates(adddays,adddays2);
        document.HotelSearch.checkinMonth.value = themonths[DepartureMonth];
        document.HotelSearch.checkinDay.value = DepartureDay;
        document.HotelSearch.checkoutMonth.value = themonths[ArrivalMonth];
        document.HotelSearch.checkoutDay.value = ArrivalDay;
    }
            
    function SettingDates(adddays, adddays2){
        var isrealdate = true;
        var today = new Date();
        themonth = today.getMonth();
        theday = today.getDate();
        theyear = today.getFullYear();        
        var numdaysmonth = TotalDaysMonth(themonth, theyear);
        theday = theday + adddays; // add # of days ahead to dd
        while(isrealdate){        
            if (theday > numdaysmonth) { 
                themonth = themonth + 1; 
                if (themonth == 12) {
                    themonth = 0; 
                    theyear = theyear + 1 
                    var numdaysmonth2 = TotalDaysMonth(themonth, theyear);
                    numdaysmonth = numdaysmonth2;
                }
                theday = theday - numdaysmonth; 
                numdaysmonth = daysinmonth[themonth]; 
                }
                else{
                    isrealdate = false;
                }
            }
            DepartureMonth = themonth;
            DepartureDay = theday;
            DepartureYear = theyear;
            ArrivalMonth = DepartureMonth;
            ArrivalDay = DepartureDay + adddays2;
            ArrivalYear = theyear;
            isrealdate = true;
            while(isrealdate){        
                if (ArrivalDay > numdaysmonth) { 
                    ArrivalMonth = ArrivalMonth + 1; 
                    if (ArrivalMonth == 12) {
                        ArrivalMonth = 0; 
                        ArrivalYear = ArrivalYear + 1 
                        var numdaysmonth3 = TotalDaysMonth(themonth, theyear);
                        numdaysmonth = numdaysmonth3; 
                    }
                    ArrivalDay = ArrivalDay - numdaysmonth; 
                    numdaysmonth = daysinmonth[ArrivalMonth]; 
                    }
                    else{
                        isrealdate = false;
                    }
                }
            }
function selectChildAge(num){
//    document.getElementById('name').style.visibilty = "hidden";
    document.getElementById('childAge').style.display = "block";
    document.getElementById('child1').style.visibility = "hidden";
    document.getElementById('child2').style.visibility = "hidden";
    document.getElementById('child3').style.visibility = "hidden";
    if (num == ""){
        document.getElementById('childAge').style.display = "none";
//        document.getElementById('name').style.visibilty = "hidden";
        document.HotelSearch.childage1.value = "";
        document.HotelSearch.childage2.value = "";
        document.HotelSearch.childage3.value = "";
    }
    if (num == 1){
//        document.getElementById('name').style.visibility = "visible";
        document.getElementById('child1').style.visibility = "visible";
    }
    if (num == 2){
//        document.getElementById('name').style.visibility = "visible";
        document.getElementById('child1').style.visibility = "visible";
        document.getElementById('child2').style.visibility = "visible";
    }
    if (num == 3){
//        document.getElementById('name').style.visibility = "visible";
        document.getElementById('child1').style.visibility = "visible";
        document.getElementById('child2').style.visibility = "visible";
        document.getElementById('child3').style.visibility = "visible";
    }
    if (num == 4){
//        document.getElementById('name').style.visibility = "visible";
        document.getElementById('child1').style.visibility = "visible";
        document.getElementById('child2').style.visibility = "visible";
        document.getElementById('child3').style.visibility = "visible";
    }
}