
function set_form_vals(el)
{
    var check_in_dis_  = document.getElementById('check_in_dis');
    var check_out_dis_  = document.getElementById('check_out_dis');
    
    var check_in_day   = document.getElementById('id_check_inDay');
    var check_out_day   = document.getElementById('id_check_outDay');
    
    var num_rooms_sel  = document.getElementById('num_rooms');
    var adults_sel  = document.getElementById('adults');
    
    var area1_sel  = document.getElementById('area1');
    var area2_sel  = document.getElementById('area2');
    var nights_sel  = document.getElementById('nights');
    var area2_activities_sel  = document.getElementById('area2_activities');
    var car_type_sel  = document.getElementById('car_type');
   // alert(area2_activities_sel.selectedIndex);
    var room1_box  = document.getElementById('room1');
    var air1_box  = document.getElementById('air1');
    var car1_box  = document.getElementById('car1');
    var activity1_box  = document.getElementById('activity1');
    var serviceclass = document.getElementById('serviceclass');
    var roundtrip_override = document.getElementById('roundtrip_override');
    var roundtrip = document.getElementById('roundtrip');
    check_service_class();
    check_round_trip();
    check_exact_charter_dates();
    
    set_shared_id_stuff();
    
    organise_child_ages();
    
    
    if(el && typeof el.checked != "undefined") show_hide_book_eng(el, room1_box, air1_box, car1_box, activity1_box);

    if(check_in_day && check_out_day)
    {
        var check_in_year_month  = document.getElementById('check_in_year_month').value;
        var in_arr = check_in_year_month.split("-");
        var check_in_month = in_arr[1];
        var check_in_year  = in_arr[0];
        
        var check_out_year_month  = document.getElementById('check_out_year_month').value;
        var out_arr = check_out_year_month.split("-");
        var check_out_month = out_arr[1];
        var check_out_year  = out_arr[0];
        
        if(dateExists(check_in_day.value, check_in_month, check_in_year) == false)
        {
            check_in_day.value = 28;
        }
        if(dateExists(check_out_day.value, check_out_month, check_out_year) == false)
        {
            check_out_day.value = 28;
        }
        
        var dc = new Date();
        var dcy = dc.getFullYear()+"";
        dcy = dcy.substr(0, 2);
        
        if(check_in_year.length == 2) var check_in_year_full = dcy+check_in_year;
        if(check_out_year.length == 2) var check_out_year_full = dcy+check_out_year;
        
        
        var d_depart = new Date();
        d_depart.setMonth(parseInt(check_in_month) - 1,check_in_day.value) 
        d_depart.setFullYear(check_in_year_full);
        
        var d_return = new Date();
        d_return.setMonth(parseInt(check_out_month) - 1,check_out_day.value) 
        d_return.setFullYear(check_out_year_full);
        
        
        if(el.id == 'nights')
        {
            var de = new Date(d_depart);
            de.setDate(d_depart.getDate() + parseInt(el.value, 10));
            
            var smallOutYear = de.getFullYear()+'';
            check_out_year = smallOutYear.substr(2, 4);
            check_out_month = de.getMonth()+1;
            check_out_day.value = de.getDate();
            set_select('id_check_outDay', check_out_day.value);
            set_select('check_out_year_month', check_out_year+'-'+check_out_month);
            
        }
        else if(el && (el.id == 'id_check_inDay' || el.id == 'check_in_year_month'))
        {// after changing the departing date make sure the returning date is at least the same day
            
            if($('#nights').length)
            {
                d_return = new Date(d_depart);
                d_return.setDate(d_depart.getDate() + parseInt($('#nights').val(), 10));
                
                
                var smallOutYear = d_return.getFullYear()+'';
                check_out_year = smallOutYear.substr(2, 4);
                check_out_month = d_return.getMonth()+1;
                check_out_day.value = d_return.getDate();
                set_select('id_check_outDay', check_out_day.value);
                set_select('check_out_year_month', check_out_year+'-'+check_out_month);
            }
            else if(d_depart>d_return)
            {
                check_out_year = check_in_year;
                check_out_month = check_in_month;
                check_out_day.value = check_in_day.value;
                set_select('id_check_outDay', check_in_day.value);
                set_select('check_out_year_month', check_in_year+'-'+check_in_month);
            }
        }
        else if (el && (el.id == 'id_check_outDay' || el.id == 'check_out_year_month'))
        {
            if(d_depart>d_return)
            {
                check_in_year = check_out_year;
                check_in_month = check_out_month;
                check_in_day.value = check_out_day.value;
                set_select('id_check_inDay', check_out_day.value);
                set_select('check_in_year_month', check_out_year+'-'+check_out_month);
            }
            
            if($('#nights').length)
            {
                $('#nights').val(daysBetween(d_depart, d_return));
            }
        }
        
        
        var hidden_check_in_date = document.getElementById('date1');
        var hidden_check_out_date = document.getElementById('date2');
        
        hidden_check_in_date.value = check_in_day.value+"/"+check_in_month+"/"+check_in_year;
        check_in_dis_.innerHTML = get_day_from_date(check_in_day.value, check_in_month, check_in_year);
        
        check_out_dis_.innerHTML = get_day_from_date(check_out_day.value, check_out_month, check_out_year);
        hidden_check_out_date.value = check_out_day.value+"/"+check_out_month+"/"+check_out_year;        
    }
    
    var room1_val, air1_val, car1_val, activity1_val;
    
    if(room1_box) room1_val = get_checkbox_value(room1_box);
    else room1_val = "";
    
    if(air1_box) air1_val = get_checkbox_value(air1_box);
    else air1_val = "";
    
    if(car1_box) car1_val = get_checkbox_value(car1_box);
    else car1_val = "";
    
    if(area2_sel && area2_activities_sel)
    {
        if(activity1_box.checked == true)
        {
            activity1_val = get_checkbox_value(activity1_box);
            area2_sel.name  = "area2_disabled";
            area2_activities_sel.name  = "area2";
        }
        else
        {
            activity1_val = "";
            area2_sel.name  = "area2";
            area2_activities_sel.name  = "area2_disabled";
        }
    }
    
    
    var locVar  = document.getElementById('loc_var');
    if(typeof(locVar) != "undefined")var locVarVal = locVar.value;
    else var locVarVal = '';
    
    var incFile  = document.getElementById('inc_file');
    if(typeof(incFile) != "undefined")var incFileVal = incFile.value;
    else var incFileVal = '';
    
    var formArcPackages = document.getElementById('form_arc_packages_id');
    formArcPackages.action = document.getElementById('default_booking_engine_action').value;
    if(formArcPackages.action == '') formArcPackages.action = "https://bookings.americanholidays.com/travel/arc_waiting.cfm";
    
    if(air1_val)
    {
        if( incFileVal != 'agent_north.php' && incFileVal != 'agent_home.php' && (locVarVal == '' || locVarVal == 'ie') && area2_sel && (area2_sel.value == "YYZ" || area2_sel.value == "YUL"))
        {
            formArcPackages.action = "https://canada.americanholidays.com/travel/arc_waiting.cfm";
        }
    }

    
    if(check_in_day)            setCookie("availability[check_in_day]",check_in_day.value, 30);
    if(check_in_month)          setCookie("availability[check_in_month]",check_in_month, 30);
    if(check_in_year)           setCookie("availability[check_in_year]",check_in_year, 30);
    
    if(check_out_day)           setCookie("availability[check_out_day]",check_out_day.value, 30);
    if(check_out_month)         setCookie("availability[check_out_month]",check_out_month, 30);
    if(check_out_year)          setCookie("availability[check_out_year]",check_out_year, 30);
    
    
    if(area1_sel)               setCookie("availability[area1]",area1_sel.value, 30);
    if(area2_sel)               setCookie("availability[area2]",area2_sel.value, 30);
    if(nights_sel)              setCookie("availability[nights]",nights_sel.value, 30);
    if(area2_activities_sel)    setCookie("availability[activites]",area2_activities_sel.value, 30);
    if(car_type_sel)            setCookie("availability[car_type]",car_type_sel.value, 30);
    if(hidden_check_in_date)    setCookie("availability[date1]",hidden_check_in_date.value, 30);
    if(hidden_check_out_date)   setCookie("availability[date2]",hidden_check_out_date.value, 30);
    if(num_rooms_sel)           setCookie("availability[num_rooms]",num_rooms_sel.value, 30);
    if(adults_sel)              setCookie("availability[adults]",adults_sel.value, 30);
    
    
    if(room1_box)               setCookie("availability[room1]",room1_val, 30);
    if(air1_box)                setCookie("availability[air1]",air1_val, 30);
    if(car1_box)                setCookie("availability[car1]",car1_val, 30);
    if(activity1_box)           setCookie("availability[activity1]",activity1_val, 30);
    
    if(serviceclass)           setCookie("availability[serviceclass]",serviceclass.value, 30);
    
    if(roundtrip)
    {
        if(roundtrip_override)
        {
            setCookie("availability[roundtrip_override]",roundtrip_override.value, 30);
            var roundtrip_override_div  = document.getElementById('roundtrip_override_div');
            if (roundtrip_override_div.className == "nodisplay")
            {
                roundtrip.value = "true";
            }
            else
            {
                roundtrip.value = roundtrip_override.value;
            }  
        }
        else
        {
            roundtrip.value = "true";
        }
    }
    
    
    

    //var hidden_origin = document.getElementById('origin_id');
    //hidden_origin.value = host+"?area1="+escape(area1_sel.value)+"&area2="+escape(area2_sel.value)+"&date1="+escape(hidden_check_in_date.value)+"&date2="+escape(hidden_check_out_date.value)+"&num_rooms="+escape(num_rooms_sel.value)+"&adults="+escape(adults_sel.value)+"&children="+escape(children_sel.value)+"&infants="+escape(infants_sel.value)+"&accommodation_id="+accommodation_id+"&special_id="+special_id+"&room1="+room1_val+"&air1="+air1_val+"&car1="+car1_val+"&activity1="+activity1_val+"&activites="+area2_activities_sel.value+children_age_insert;//+"&"+query
}


function dateExists(date, month, year){
    if(year.length == 2)
    {
        var dc = new Date();
        var dcy = dc.getFullYear()+"";
        year = dcy.substr(0, 2)+year;
    }
    var d = new Date(year, parseInt(month)-1, date);
    if(d.getDate() === parseInt(date, 10))return true;
    else return false;
}

function get_checkbox_value(el)
{
    if(el.type == "hidden" && el.value == "true")return 1;
    else if(el.type == "hidden")return "";
    else if(el.checked == true)return 1;
    else return "";
    
}


function get_day_from_date(date, month, year){
    
    var days_of_week = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
    
    if(year.length == 2)
    {
        var dc = new Date();
        var dcy = dc.getFullYear()+"";
        year = dcy.substr(0, 2)+year;
    }
    var d = new Date(year, parseInt(month)-1, date);
    
    return days_of_week[d.getDay()];
    
}
function check_round_trip()
{
    var area2  = document.getElementById('area2');
    var destination_div  = document.getElementById('destination_div');
    var air1_box  = document.getElementById('air1');
    if(air1_box) air1_val = get_checkbox_value(air1_box);
    else air1_val = "";
    
    var b_eng_roundtrip_override  = document.getElementById('b_eng_roundtrip_override');
    
    if(b_eng_roundtrip_override)
    {
        var roundtrip_override_values = b_eng_roundtrip_override.getElementsByTagName("INPUT");
        
        var roundtrip_override_on = false;
        
        for(var i=0; i<roundtrip_override_values.length; i++)
        {
            if(roundtrip_override_values[i].value == area2.value)
            {
                roundtrip_override_on = true;
                break;
            }
        }
    
        var roundtrip_override_div  = document.getElementById('roundtrip_override_div');
        if (roundtrip_override_div.className == "nodisplay")
        {
        var roundtrip_override = document.getElementById('roundtrip_override');
        var roundtrip = document.getElementById('roundtrip');
    
            for (var i=0; i<roundtrip_override.length; i++)
            {
                if (roundtrip.value ==roundtrip_override.options[i].value)
                {
                    roundtrip_override.selectedIndex=i;   
                }
            }
            
        }   
        
        var roundtrip_override_sel  = document.getElementById('roundtrip_override');
        
        if(roundtrip_override_on && air1_val)
        {
            roundtrip_override_sel.name = "roundtrip_override";
            roundtrip_override_div.className = "form_row divider_bottom";
            destination_div.className = "form_row";
        }
        else
        {
            roundtrip_override_sel.name = "roundtrip_override_disabled";
            roundtrip_override_div.className = "nodisplay";
            
        }
    }
    
}
function check_service_class()
{
    var area2  = document.getElementById('area2');
    var destination_div  = document.getElementById('destination_div');
    var air1_box  = document.getElementById('air1');
    if(air1_box) air1_val = get_checkbox_value(air1_box);
    else air1_val = "";
    
    var b_eng_serviceclass  = document.getElementById('b_eng_serviceclass');
    
    if(b_eng_serviceclass)
    {
        var serviceclass_values = b_eng_serviceclass.getElementsByTagName("INPUT");
        
        var serviceclass_on = false;
        
        for(var i=0; i<serviceclass_values.length; i++)
        {
            if(serviceclass_values[i].value == area2.value)
            {
                serviceclass_on = true;
                break;
            }
        }
    
        var serviceclass_div  = document.getElementById('serviceclass_div');
        var serviceclass_sel  = document.getElementById('serviceclass');
        
        if(serviceclass_on && air1_val)
        {
            serviceclass_sel.name = "serviceclass";
            serviceclass_div.className = "form_row";
            destination_div.className = "form_row";
        }
        else
        {
            serviceclass_sel.name = "serviceclass_disabled";
            serviceclass_div.className = "nodisplay";
            
        }
    }
    
}

function set_dropdowns(cal)
{
    if (cal.dateClicked)
    {
        var field = cal.params.inputField.id;
        var hidden_check_in_date = document.getElementById('date1').value;
        var hidden_check_out_date = document.getElementById('date2').value;
        
        var check_in_day   = document.getElementById('id_check_inDay');
        var check_in_year_month  = document.getElementById('check_in_year_month');
        
        if(field == "date1")
        {
            var change_field = 'id_check_inDay';
            
            var in_arr = hidden_check_in_date.split("/");
            var check_in_day_val = in_arr[0];
            var check_in_month_val = in_arr[1];
            var check_in_year_val = in_arr[2];
            
            set_select('id_check_inDay', check_in_day_val);
            set_select('check_in_year_month', check_in_year_val+'-'+check_in_month_val);
        }
        else if(field == "date2")
        {
            var change_field = 'id_check_outDay';
            
            var out_arr = hidden_check_out_date.split("/");
            var check_out_day_val = out_arr[0];
            var check_out_month_val = out_arr[1];
            var check_out_year_val = out_arr[2];
            
            set_select('id_check_outDay', check_out_day_val);
            set_select('check_out_year_month', check_out_year_val+'-'+check_out_month_val);
        }
        else
        {
            return;
        }
        
        var input_date1 = document.getElementById(field);
        set_form_vals(document.getElementById(change_field));
    }
}

function set_select(el_id, val)
{
    var el   = document.getElementById(el_id);
    
    if(el)
    {
        if(el.options.length)for(var i=0; i<el.options.length; i++)
        {
            if(el.options[i].value == val)
            {
                el.selectedIndex = i;
                break;
            }
        }
    }
}


function people_manage(submit)
{
    /* ar inserted this cookioe is cleared by website server on next page load.*/
    setCookie("load_type","booking_search", 30);
    /* */

    
    /*    ar to fix set DESTINATION error 21/12/2009 -     */
    /* This fix means that a package will always be selected if none selected. */  
    var room1_box  = document.getElementById('room1');
    var air1_box  = document.getElementById('air1');
    var car1_box  = document.getElementById('car1');
    var activity1_box  = document.getElementById('activity1');        
        
    var allow_submit  = 0;
    if(room1_box.checked)               allow_submit = 1;
    if(air1_box.checked)                allow_submit = 1;
    if(car1_box.checked)                allow_submit = 1;
    if(activity1_box.checked)           allow_submit = 1;
    if (allow_submit == 0)
    {
        if(typeof room1_box != undefined) room1_box.checked = true;
        if(typeof air1_box != undefined)air1_box.checked = true;
    }
    /* ends ar insterted 21/12/2009 */
    
    

    if(typeof(see_if_date_disabled) == 'function')
    {
        var hidden_check_in_date = document.getElementById('date1').value;
        var in_arr = hidden_check_in_date.split("/");
        var check_in_day_val = in_arr[0];
        var check_in_month_val = in_arr[1];
        var check_in_year_val = in_arr[2];
        
        var ds = new Date(20+''+check_in_year_val, check_in_month_val-1, check_in_day_val, 0, 0, 0, 0);
        
        //alert(ds)
        
        if(see_if_date_disabled(ds, 20+''+check_in_year_val, check_in_month_val-1, check_in_day_val) == true)
        {
            msgBox('\u000AThe start date you have selected is not allowed.\u000ADates must be future dates.\u000ADates must be less than 330 days from todays date.', "Attention");
            return false;
        }
        
        var hidden_check_out_date = document.getElementById('date2').value;
        var out_arr = hidden_check_out_date.split("/");
        var check_out_day_val = out_arr[0];
        var check_out_month_val = out_arr[1];
        var check_out_year_val = out_arr[2];
        
        var de = new Date(20+''+check_out_year_val, check_out_month_val-1, check_out_day_val, 0, 0, 0, 0);
        
        if(see_if_date_disabled(de, 20+''+check_out_year_val, check_out_month_val-1, check_out_day_val) == true)
        {
            msgBox('\u000AThe end date you have selected is not allowed.\u000ADates must be future dates.\u000ADates must be less than 330 days from todays date.', "Attention");
            return false;
        }
        
    }
    
    

    var activity1_box  = document.getElementById('activity1');
    if(typeof activity1_box == undefined || get_checkbox_value(activity1_box) == "") // activities not selected
    {
        var max_people = 9;
        var available_infants = 0;
        
        var adults_sel  = document.getElementById('adults');
        var children_sel  = document.getElementById('children');
        var infants_sel  = document.getElementById('infants');
        
        var people_mess  = document.getElementById('bottom_message_area');
        
        var total_people = parseInt(adults_sel.value, 10) + parseInt(children_sel.value, 10) + parseInt(infants_sel.value, 10);
        
        if(total_people > max_people)
        {
            var message = "Maximum of "+max_people+" people allowed per booking";
            people_mess.className = "people_message divider_top";
            people_mess.innerHTML = message;
            if(submit == false)set_form_vals();
            else
            {
                msgBox(message+'.\u000A\u000APlease change your number of selected Adults ('+adults_sel.value+'), Children ('+children_sel.value+') or Infants ('+infants_sel.value+').', "Attention");
                return false;
            }
        }
        else
        {
            var clonable  = document.getElementById('visa_link_content');
            
            if(clonable && clonable.firstChild) var clone_content = clonable.firstChild.cloneNode(true);
        
            
            people_mess.innerHTML = "";
            people_mess.className = "visa_link divider_top";
            if(clonable && clonable.firstChild)people_mess.appendChild(clone_content);
            
	    
	    
            var dynamic_children_input = document.getElementById('dynamic_children');
	       /* starts ar inserted */
	        for(var k=1; k<= children_sel.length-1; k++)
            {
                if (k > dynamic_children_input.value)
		        {
                    document.getElementById('child_age'+k).value = "";
                }
		    }
            /* ends ar inserted */		
	    
	    
            if(submit == false)set_form_vals();
            else
            {
                var need_child_ages = false;
                if(dynamic_children_input.value)
                {
                    for(var i=1; i<=dynamic_children_input.value; i++)
                    {
                        var child_age = document.getElementById('child_age'+i);
                        if(child_age.value == "")
                        {
                            var childAgesLink = document.getElementById('child_ages_link');
                            if(childAgesLink)
                            {
                                //GB_showCenter('Enter the ages of the children on the trip', childAgesLink.href, 285, 320, set_vals_and_submit);
                                TopUp.displayTopUp(childAgesLink);
                            }
                            else
                            {
                                //GB_showCenter('Enter the ages of the children on the trip', "/get_ages.php", 285, 320, set_vals_and_submit);
                                TopUp.displayTopUp(childAgesLink);
                            }
                            need_child_ages = true;
                            break;
                        }
                    }
                }
                
                if(need_child_ages == false && air1_box.checked && infants_sel.selectedIndex)
                {
                    for(var i=1; i<=infants_sel.selectedIndex; i++)
                    {
                        var infantTypeSeat = document.getElementById('infant'+i);
                        if(infantTypeSeat.value == "")
                        {
                            var childAgesLink = document.getElementById('child_ages_link');
                            if(childAgesLink)
                            {
                                //GB_showCenter('Enter the ages of the children on the trip', childAgesLink.href, 285, 320, set_vals_and_submit);
                                TopUp.displayTopUp(childAgesLink);
                            }
                            else
                            {
                                //GB_showCenter('Enter the ages of the children on the trip', "/get_ages.php", 285, 320, set_vals_and_submit);
                                TopUp.displayTopUp(childAgesLink);
                            }
                            need_child_ages = true;
                            break;
                        }
                    }
                }
                
                
                if(need_child_ages) // if child ages needed let it go first
                {
                    return false;
                }
                else
                {
                    if(air1_box.checked)var cal_stat = get_calendar_status();
                    else var cal_stat = false;
                    
                    if(cal_stat)
                    {
                        var calendarLink  = document.getElementById('calendar_link');
                        TopUp.displayTopUp(calendarLink);
                        return false;
                    }
                    else
                    {
                        record_search();
                        return true;
                    }
                }
            }
        }
    }
    else  // activities selected
    {
        record_search();
        return true;
    }
}

function set_vals_and_submit(cal_displayed)
{
    var age_testing = document.getElementById('age_testing');
    
    if(cal_displayed) var cal_stat = false;
    else
    {
        var cal_stat = get_calendar_status();
        var calendarLink  = document.getElementById('calendar_link');
        TopUp.displayTopUp(calendarLink);
    }
    
    if(cal_stat == false)       // we do not need to display a calendar
    {
        if(cal_displayed || (age_testing && age_testing.value == "submit"))  // if either the calendar was submitted or an age form was submitted
        {
            set_form_vals(0);
            record_search();
            document.form_arc_packages.submit();
        }
    }
}

function organise_child_ages()
{
    var children_sel  = document.getElementById('children');
    var dynamic_children_input  = document.getElementById('dynamic_children');
    var infants_sel  = document.getElementById('infants');
    var age_list = document.getElementById('age_list');
    
    // combine the child and infant values in the dynamic_children field
    dynamic_children_input.value = parseInt(children_sel.value, 10) + parseInt(infants_sel.value, 10);
    
    var infant1_input  = document.getElementById('infant1');
    var infant2_input  = document.getElementById('infant2');
    var infant3_input  = document.getElementById('infant3');
    var infant4_input  = document.getElementById('infant4');
    
    if(dynamic_children_input.value > 0)
    {
        var running_number_of_children = 0;
        var running_number_of_infants = 0;
        
        if(typeof(children_sel.selectedIndex) != "undefined") running_number_of_children = children_sel.selectedIndex;
        else if(children_sel.value) running_number_of_children = parseInt(children_sel.value, 10);
        
        if(infants_sel)
        {
            running_number_of_infants = infants_sel.selectedIndex;
        }
        
        var children_age_insert = "";
        var child_age_list = "";
        
        for(var i=1; i<=dynamic_children_input.value; i++)
        {
            var child_age = document.getElementById('child_age'+i);
            if(child_age)
            {
                if(child_age.value == 1)
                {
                    if(running_number_of_infants == 0)child_age.value = "";
                    else running_number_of_infants--;
                }
                else
                {
                    if(running_number_of_children == 0)child_age.value = 1;
                    else running_number_of_children--;
                }
                child_age_list += child_age.value+", ";
                children_age_insert += '&child_age'+i+'='+child_age.value;
                setCookie("availability[child_age"+i+"]",child_age.value, 30);
            }
            delete(child_age);
        }
        child_age_list = ": "+child_age_list.replace(/\,\s$/, "");
        age_list.innerHTML = child_age_list;
    }
    else if(age_list)
    {
        age_list.innerHTML = "";
    }
    
    
    if(children_sel)            setCookie("availability[children]",children_sel.value, 30);
    if(infants_sel)             setCookie("availability[infants]",infants_sel.value, 30);
    if(infant1_input)           setCookie("availability[infant1]",infant1_input.value, 30);
    if(infant2_input)           setCookie("availability[infant2]",infant2_input.value, 30);
    if(infant3_input)           setCookie("availability[infant3]",infant3_input.value, 30);
    if(infant4_input)           setCookie("availability[infant4]",infant4_input.value, 30);
}

function get_calendar_status()
{
    if(typeof(calLocs) != "undefined")
    {
        var area1Sel  = document.getElementById('area1');
        var area2Sel  = document.getElementById('area2');
        
        var combId = area1Sel.value+"-"+area2Sel.value;
        if(typeof(calLocs[combId]) != "undefined")
        {
            var flight_option_group_ids = '';
            
            for(var i in calLocs[combId])
            {
                flight_option_group_ids += calLocs[combId][i]+'-';
            }
            
            
            flight_option_group_ids += ":";
            
            var calendarLink  = document.getElementById('calendar_link');
            var roundTrip = document.getElementById('roundtrip');
            var serviceClass = document.getElementById('serviceclass');
            
            if(roundTrip.value == "true")
            {
                combId = area2Sel.value+"-"+area1Sel.value;
                if(typeof(calLocs[combId]) != "undefined")
                {
                    for(var i in calLocs[combId])
                    {
                        flight_option_group_ids += calLocs[combId][i]+'-';
                    }
                    
                    
                    flight_option_group_ids += ":";
                }
                else return false;
            }
            
            var date1  = document.getElementById('date1');
            var date2  = document.getElementById('date2');
            
            
            var locVar  = document.getElementById('loc_var').value;
            var langVar  = document.getElementById('lang_var').value;
            
            calendarLink.href = "/calendar_des.php?flight_option_group_ids="+flight_option_group_ids+"&roundtrip="+roundTrip.value+"&serviceclass="+serviceClass.value+"&leg=1&set_date_1="+date1.value+"&set_date_2="+date2.value+"&location="+locVar+"&lang="+langVar;
            
            return true;
    
        }
        else return false;
    }
    else return false;
}

function check_exact_charter_dates()
{
    var area1Sel  = document.getElementById('area1');
    var area2Sel  = document.getElementById('area2');
    var useExactCharterDates  = document.getElementById('use_exact_charter_dates');
    
    if(useExactCharterDates)
    {
        var combId = area1Sel.value+"-"+area2Sel.value;
        if(typeof(calLocs[combId]) != "undefined")
        {
            var roundTrip = document.getElementById('roundtrip');
            
            if(roundTrip.value == "true")
            {
                combId = area2Sel.value+"-"+area1Sel.value;
                if(typeof(calLocs[combId]) == "undefined")
                {
                    useExactCharterDates.value = 'false';
                }
            }
            useExactCharterDates.value = 'true';
        }
        else useExactCharterDates.value = 'false';
    }
}

function submitCalendar()
{
    set_vals_and_submit(true);
    //TopUp.overlayClose();
}


function record_search()
{
    $.ajax({
        url: '/extras/ajax_record_search.php',
        async: false,
        data: $("#form_arc_packages_id").serialize()
    });
}


function show_hide_book_eng(el, room1_box, air1_box, car1_box, activity1_box)
{
    var type_div  = document.getElementById('type_div');
    var multi_div  = document.getElementById('multi_div');
    var departure_div  = document.getElementById('departure_div');
    var destination_div  = document.getElementById('destination_div');
    var activities_div  = document.getElementById('activities_div');
    var nights_div  = document.getElementById('nights_div');
    var rooms_div  = document.getElementById('rooms_div');
    var people_div  = document.getElementById('people_div');
    var discount_code  = document.getElementById('discount_code');
    var departing_text  = document.getElementById('departing_text');
    var returning_text  = document.getElementById('returning_text');
    var bottom_message_area  = document.getElementById('bottom_message_area');
    var carType_div  = document.getElementById('car_type_div');
    var carType  = document.getElementById('car_type');
    var serviceClass  = document.getElementById('serviceclass');
    var serviceClass_div = document.getElementById('serviceclass_div');
    
    var roundTrip  = document.getElementById('roundtrip_override');
    var roundTrip_div = document.getElementById('roundtrip_override_div');
    
    // The default is that hotel and flights are checked if nothing else is checked
    if(room1_box.checked == false && air1_box.checked == false && car1_box.checked == false && activity1_box.checked == false)
    {
        if(el.id == room1_box.id)air1_box.checked = true;
        else if(el.id == air1_box.id)room1_box.checked = true;
        else
        {
            room1_box.checked = true;
            air1_box.checked = true;
        }
        
    }
    
    // Actitities Selected
    if(activity1_box.checked == true && (el.id != "activity1" && activity1_box.checked == true) == false)
    {
        room1_box.checked = false;
        air1_box.checked = false;
        car1_box.checked = false;
        
        type_div.className = "form_row divider_bottom";
        if(multi_div)multi_div.className = "nodisplay";
        departure_div.className = "nodisplay";
        destination_div.className = "nodisplay";
        serviceClass_div.className = "nodisplay";
        roundTrip_div.className = "nodisplay";
        activities_div.className = "form_row divider_bottom divider_top";
        if(nights_div)nights_div.className = "nodisplay";
        rooms_div.className = "nodisplay";
        people_div.className = "nodisplay";
        departing_text.innerHTML = "Activity Start Date: ";
        returning_text.innerHTML = "Activity End Date: ";
        bottom_message_area.className = "nodisplay";
    }
    else
    {
        activity1_box.checked = false;
        
        
        if(room1_box.checked == true && nights_div)nights_div.className = "form_row divider_top divider_bottom";
        
        type_div.className = "form_row";
        if(multi_div)multi_div.className = "multi_div";
        
        if(air1_box.checked != true) departure_div.className = "nodisplay";
        else departure_div.className = "form_row";
        
        if(car1_box.checked == true && room1_box.checked != true && air1_box.checked != true) destination_div.className = "form_row";
        else if(air1_box.checked == true && serviceClass.name == "serviceclass") destination_div.className = "form_row";
        else if(air1_box.checked == true && roundTrip.name == "roundtrip_override") destination_div.className = "form_row";
        else destination_div.className = "form_row divider_bottom";
        
        activities_div.className = "nodisplay";
        rooms_div.className = "form_row divider_top divider_bottom";
        people_div.className = "divider_bottom divider_top";
        departing_text.innerHTML = "Departing: ";
        returning_text.innerHTML = "Returning: ";
        
        if(car1_box.checked == true && room1_box.checked != true && air1_box.checked != true)
        {
            carType_div.className = "form_row divider_bottom";
            carType.name = "car_type";
        }
        else
        {
            carType_div.className = "nodisplay";
            carType.name = "car_type_disabled";
        }
        
        if(car1_box.checked == true && room1_box.checked == false && air1_box.checked == false)
        {
            people_div.className = "nodisplay";
        }
        else
        {
            people_div.className = "divider_bottom divider_top";
        }
        
        bottom_message_area.className = "visa_link divider_top";
    }
    
    
    
    
    if(room1_box.checked == true && air1_box.checked == false )
    {
        departing_text.innerHTML = "Check in: ";
        returning_text.innerHTML = "Check out: ";
    }
    else if(car1_box.checked == true && room1_box.checked == false && air1_box.checked == false)
    {
        departing_text.innerHTML = "Pickup Date: ";
        returning_text.innerHTML = "Dropoff Date: ";
    }
    
    if(room1_box.checked == false)
    {
        rooms_div.className = "nodisplay";
    }
    else
    {
        rooms_div.className = "form_row divider_top divider_bottom";
    }
    
    if(discount_code)
    {
        if((room1_box.checked == true && air1_box.checked == true) ||
           (air1_box.checked == true && car1_box.checked == true) ||
           (room1_box.checked == true && car1_box.checked == true)
           )
        {
            discount_code.value = "amh052";
        }
        else if(activity1_box.checked == true || car1_box.checked == true || room1_box.checked == true)
        {
            discount_code.value = "amh051";
        }
        else
        {
            discount_code.value = ""; // should only be the case if flights selected
        }
    }
    
}

function set_car_type(code)
{
    var carType  = document.getElementById('car_type');
    
    if(carType.name == "car_type")
    {
        set_select("car_type", code);
    }
}

function set_shared_id_stuff()
{
    var area2_sel  = document.getElementById('area2');
    var b_eng_fieldset  = document.getElementById('b_eng_fieldset');
    var room1_box  = document.getElementById('room1');
    if(room1_box)
    {
        if(room1_box.type == "checkbox") room1_val = get_checkbox_value(room1_box);
        else room1_val = room1_box.value;
    }
    else room1_val = "";
    
    if(room1_val && typeof idRel_sil != "undefined" && typeof idRel_sil[area2_sel.value] != "undefined")
    {
        var hotelId = document.getElementById('hotel_id');
        if(!hotelId)
        {
            var hotelId = document.createElement('INPUT');
            hotelId.type = "hidden";
            hotelId.id = hotelId.name = "hotel_id";
            var allHotels = document.createElement('INPUT');
            allHotels.type = "hidden";
            allHotels.name = allHotels.id = "all_hotels";
            allHotels.value = "true";
            
            b_eng_fieldset.appendChild(hotelId);
            b_eng_fieldset.appendChild(allHotels);
        }
        hotelId.value = idRel_sil[area2_sel.value];

    }
    else
    {
        var hotelId = document.getElementById('hotel_id');
        if(hotelId)
        {
            var allHotels = document.getElementById('all_hotels');
            hotelId.parentNode.removeChild(hotelId);
            allHotels.parentNode.removeChild(allHotels);
        }
    }
    
}


function change_date_value(field, fullDate)
{
    
    var in_arr = fullDate.split("/");
    var day = in_arr[0];
    var month = in_arr[1];
    var year = in_arr[2];
    
    if(field == "date1")
    {
        set_select('id_check_inDay', day);
        set_select('check_in_year_month', year+'-'+month);
        var date_dis_  = document.getElementById('check_in_dis');
    }
    else
    {
        set_select('id_check_outDay', day);
        set_select('check_out_year_month', year+'-'+month);
        var date_dis_  = document.getElementById('check_out_dis');
    }

    var hidden_date = document.getElementById(field);
    
    hidden_date.value = day+"/"+month+"/"+year;
    date_dis_.innerHTML = get_day_from_date(day, month, year);
}

function config_beng()
{
 //  alert('Here');
 //   var area2_input  = document.getElementById('area2');
    if(area2_input)
    {
        var varSet = getCookie('var_set');
        if (varSet!=null && varSet!="")
        {
            var room1_box  = document.getElementById('room1');
            var air1_box  = document.getElementById('air1');
            var car1_box  = document.getElementById('car1');
            var activity1_box  = document.getElementById('activity1');
    
            var room1_cook = getCookie('availability[room1]');
            var air1_cook = getCookie('availability[air1]');
            var car1_cook = getCookie('availability[car1]');
            var activity1_cook = getCookie('availability[activity1]');
            
            Custom.clear;                       // from /extras/custom-form-elements.js
            
            room1_box.checked = room1_cook;
            set_check_box(room1_box)
            
            air1_box.checked = air1_cook;
            set_check_box(air1_box);
            
            car1_box.checked = car1_cook;
            set_check_box(car1_box);
            
            activity1_box.checked = activity1_cook;
            set_check_box(activity1_box);
            
            set_form_vals(room1_box);
            
        }
        setCookie("var_set",1, 30);
    }
}


// from /extras/custom-form-elements.js
function set_check_box(element)
{
    thespan = element.previousSibling;
    if(element.checked == true) {
        thespan.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
    }else if(element.checked != true ) {
        thespan.style.backgroundPosition = "0 -" + checkboxHeight + "px";
    } 
}


function setCookie(name,value,days) {
    if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

//*****************************
//  Custom Alert Box
//  Free to use with credits in tact.
//  Written By Adam Matthews aka Basscyst
//	AdamDMatthews@Gmail.com
//*****************************
function msgBox(msg,hdr){
	if(!document.getElementById('alerts')){
		var div=document.createElement('div');
		div.setAttribute('id','alerts');
		document.body.appendChild(div);
	}
	var div=document.createElement('div');
	div.className="alertbox";
	var h3=document.createElement('h3');
	h3.className="alerttitle";
	var p=document.createElement('p');
	p.className="alerttxt";
	var footdiv=document.createElement('p');
	footdiv.className="alertfoot";
	div.appendChild(h3);
	div.appendChild(p);
	div.appendChild(footdiv);
	var but=document.createElement('input');
	but.setAttribute('type','button');
	but.className='alertbut';
	but.setAttribute('value','Close');
	footdiv.appendChild(but);
	var hdr=(hdr) ? hdr : "Alert!";
	h3.appendChild(document.createTextNode(hdr));
	var cut=msg.split("\n");
	var len=cut.length;
	p.appendChild(document.createTextNode(cut[0]));
	for(var i=1;i<len;i++){	
		p.appendChild(document.createElement('br'));
		p.appendChild(document.createTextNode(cut[i]));
	}
	document.getElementById('alerts').appendChild(div);
	window.onscroll=function(){
		placeAlerts();	
	}
	window.onresize=function(){
		placeAlerts();	
	}
	placeAlerts();
}
var posX;
var posY;
function mouseXY(e){
	if (!e){
		var e = window.event;	
	}
	if (e.clientX)
	{
	     posX = e.clientX + document.documentElement.scrollLeft;
	     posY = e.clientY + document.documentElement.scrollTop;
	}
	else
	{
	     posX = Math.max(e.pageX,0);
	     posY = Math.max(e.pageY,0);
	}
	var coord=new Array();
	return coord;
}
if(document.captureEvents){
	document.captureEvents(Event.MOUSEMOVE)
}
function placeAlerts(){
	var alerts=document.getElementById('alerts').getElementsByTagName('div');
	var len=alerts.length;
	var x=0;
	var y=300;
	var w=document.body.clientWidth;
	var h=document.body.clientHeight;
	for(var i=0;i<len;i++){
		alerts[i].style.zIndex=i+100;
		alerts[i].getElementsByTagName('h3')[0].onmousedown="";
		alerts[i].getElementsByTagName('input')[0].onclick="";
		
		if(window.pageYOffset){
				alerts[i].style.top=y+(window.pageYOffset)+'px';
			}else{
				alerts[i].style.top=y+(document.documentElement.scrollTop)+'px';
			}
		alerts[i].style.left=(w / 2)- (343 / 2) + x +'px';;
		x=x+15;
		y=y+15;
		if(i==len-1){
			var h3=alerts[i].getElementsByTagName('h3')[0];
			var but=alerts[i].getElementsByTagName('input')[0];
			but.onclick=function(){
				this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);	
				var alerts=document.getElementById('alerts').getElementsByTagName('div');
				if(alerts.length==0){
					window.onscroll="";
				}
				placeAlerts();
			}
			h3.onmousedown=function(event){
				this.parentNode.setAttribute('id','active_alert');
				var event=(event)?event:arguments[0];
					mouseXY(event);
				start_x=posX;
				start_left=document.getElementById('active_alert').style.left.replace('px','');
				adjust=posX-start_left;
				document.onmousemove=function(event){
					var event=(event)?event:arguments[0];
					mouseXY(event);
					var obj=document.getElementById('active_alert');
					obj.style.left=posX-adjust+'px';
					obj.style.top=posY-5+'px';
				};
			}
			h3.onmouseup=function(){
				document.onmousemove="";
				this.parentNode.setAttribute('id','');
			}	
		}
	}	
}

function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

function daysBetween(first, second) {

    // Copy date parts of the timestamps, discarding the time parts.
    var one = new Date(first.getFullYear(), first.getMonth(), first.getDate());
    var two = new Date(second.getFullYear(), second.getMonth(), second.getDate());

    // Do the math.
    var millisecondsPerDay = 1000 * 60 * 60 * 24;
    var millisBetween = two.getTime() - one.getTime();
    var days = millisBetween / millisecondsPerDay;

    // Round down.
    return Math.floor(days);
}


