// destinations: name#id#branchcode#compcode#sessiondestination#source nie ta

var America = "America#28525#PC2#P#America#premieramerica".split("#");
var Australia = "Australia#140310#RE2#R#asia#premierasia".split("#");
var BaliLombok = "Bali and Lombok#28551#RE2#R#asia#premierasia".split("#");
var BorneoBrunei = "Borneo and Brunei#28554#RE2#R#asia#premierasia".split("#");
var Canada = "Canada#80393#PC2#P#America#premieramerica".split("#");
var Caribbean = "The Carribean#28515#RE2#R#asia#premierasia".split("#");
var CI = "Channel Islands and the Isle of Man#28521#QWB#Q#ci#premierci".split("#");
var ChinaTibet = "China and Tibet#28553#RE2#R#asia#premierasia".split("#");
var Dubai = "Dubai#143857#RE2#R#asia#premierasia".split("#");
var Egypt = "Egypt#146005#RE2#R#asia#premierasia".split("#");
var FarEast = "The Far East#28514#RE2#R#asia#premierasia".split("#");
var Fiji = "Fiji#142429#RE2#R#asia#premierasia".split("#");
var HongKong = "Hong Kong#139834#RE2#R#asia#premierasia".split("#");
var IndianOcean = "The Indian Ocean#28520#RE2#R#asia#premierasia".split("#");
var IOM = "Isle of Man#28567#MWA#M#iom#premieriom".split("#");
var Malaysia = "Malaysia#28548#RE2#R#asia#premierasia".split("#");
var MiddleEast = "The Middle East#28519#RE2#R#asia#premierasia".split("#");
var NewZealand = "New Zealand#140311#RE2#R#asia#premierasia".split("#");
var Scotland = "Scotland#111894#MWA#M#iom#premieriom".split("#");
var SingaporeBintan = "Singapore and Bintan#28550#RE2#R#asia#premierasia".split("#");
var SouthAfrica = "South Africa#117213#RE2#R#asia#premierasia".split("#");
var Thailand = "Thailand#28547#RE2#R#asia#premierasia".split("#");
var UKCityBreaks = "UK City Breaks#151101#MAW#M#gb#premiergb".split("#");
var EuroCityBreaks = "European City Breaks#151102#SWA#S#eu#premiereu".split("#");

// inclusive search

var JER_inclusive = "JD#28565#GWB#G#ci#premierci".split("#");
var JERSELF_inclusive = "JD#118840#GSW#G#ci#premierci".split("#");
var GCISELF_inclusive = "GU#152544#GSW#G#ci#premierci".split("#");
var GCI_inclusive  = "GU#28566#GWB#G#ci#premierci".split("#");
var IOM_inclusive  = "IM#28567#MWB#M#iom#premieriom".split("#");
var BELFAST_inclusive = "UK|BFS#151104#MSW#M#gb#premiergb".split("#");
var EDINBURGH_inclusive = "UK|EDI#151103#MSW#M#gb#premiergb".split("#");
var GLASGOW_inclusive = "UK|GLA#151108#MSW#M#gb#premiergb".split("#");
var AMSTERDAM_inclusive = "NL|AMS#151116#SWB#S#eu#premiereu".split("#");
var BRUSSELS_inclusive = "BE|BRU#151118#SWB#S#eu#premiereu".split("#");
var MAJORCA_inclusive = "ES|PMI#151119#SWB#S#eu#premiereu".split("#");
var NICE_inclusive = "FR|NCE#151120#SWB#S#eu#premiereu".split("#");

// destination override

var DestOverride_flights = "GCI#IOM#JER#BFS#EDI#GLA#AMS#BRU#PMI#NCE".split("#");
var DestOverride_ferry = "GU#IM#JE".split("#");

// global variables
var site_url = $("#site_url").val();
var url = site_url + "/booking_form_development/xmlgenerator?asset=";
var quote_url = site_url + "/holiday_quote/holiday_quote_-_channel_islands_and_isle_of_man";
var flights = '', ferry = '';
var version_ie = parseInt(navigator.appVersion.toString().split("MSIE ")[1]);
var version = parseInt(navigator.appVersion);
var duration = 500;
var cookie_duration = 14;
var date = new Date(new Date().setDate(new Date().getDate() + 4));

// prepare the booking form

$(document).ready(function() {
 if (version <= 4 && version_ie <= 6)
  duration = 0;
 if ($("#default_location").val() != "") {
  if ($("#hotel_bookable").val() == "0")
   return false;
  prepare_functions();
  if (getCookie('location').toString() == location.toString())
   prepare_booking_form_using_cookies();
  else
   prepare_booking_form();
 }
});

// cookie functions

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 setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}

// prepare booking form

function prepare_booking_form() {
 change_select(0, $("#default_location").val(), $("#first_select_slide"), $("#first_select"), $("#default_code").val());
 $("#agency").text($("#agency_name").val());
 $("#area").text($("#default_name").val());
 if ($("#hotel_code").val() != "") {
  $("#hotel_name_slide").show();
  $("#first_select_slide").hide();
  $("#hotel").text($("#hotel_name").val());
  $("#MainAccom").val($("#hotel_code").val());
 }
 anite_code($("#default_code").val());
 prepare_date(date.getDate(), date.getMonth(), 0, date.getFullYear(), date);
 destination_check($("#first_value").val(), 0);
 //new
 //destination_check($("#second_value").val());

//here
 $("#booking_form").slideDown(2000);
}

function prepare_booking_form_using_cookies() {

  change_select(0, $("#default_location").val(), $("#first_select_slide"), $("#first_select"), getCookie('first_select'));
  change_select(1, getCookie('first_select'), $("#second_select_slide"), $("#second_select"), getCookie('second_select'));
  change_select(2, getCookie('second_select'), $("#third_select_slide"), $("#third_select"), getCookie('third_select'));
  change_select(3, getCookie('third_select'), $("#fourth_select_slide"), $("#fourth_select"), getCookie('fourth_select'));
  change_select(4, getCookie('fourth_select'), $("#null"), $("#null"), 0);

 $("#agency").text($("#agency_name").val());
 $("#area").text($("#default_name").val());
 if ($("#hotel_code").val() != "") {
  $("#hotel_name_slide").show();
  $("#first_select_slide").hide();
  $("#hotel").text($("#hotel_name").val());
  $("#MainAccom").val($("#hotel_code").val());
 }

 $("#agent_initials").val(getCookie('agent_initials'));

  prepare_date(getCookie('day'), getCookie('month_actual'), getCookie('month_index'), getCookie('year'), new Date(getCookie('year'), getCookie('month'), getCookie('day')));

 if (getCookie('duration_select') != "")
  $("#duration_select option:eq("+getCookie('duration_select')+")").attr("selected", "true");
 if (getCookie('adult_select') != "")
  $("#adult_select option:eq("+getCookie('adult_select')+")").attr("selected", "true");
 $("#children_select option:eq("+getCookie('children_select')+")").attr("selected", "true");
 $("#infant_select option:eq("+getCookie('infant_select')+")").attr("selected", "true");
 $("#rooms_select option:eq("+getCookie('rooms_select')+")").attr("selected", "true");
 if (getCookie('adu1_select') != "")
  $("#adu1_select option:eq("+getCookie('adu1_select')+")").attr("selected", "true");
 $("#chi1_select option:eq("+getCookie('chi1_select')+")").attr("selected", "true");
 $("#adu2_select option:eq("+getCookie('adu2_select')+")").attr("selected", "true");
 $("#chi2_select option:eq("+getCookie('chi2_select')+")").attr("selected", "true");
 $("#adu3_select option:eq("+getCookie('adu3_select')+")").attr("selected", "true");
 $("#chi3_select option:eq("+getCookie('chi3_select')+")").attr("selected", "true");
 $("#adu4_select option:eq("+getCookie('adu4_select')+")").attr("selected", "true");
 $("#chi4_select option:eq("+getCookie('chi4_select')+")").attr("selected", "true");
 $("#adu5_select option:eq("+ getCookie('adu5_select')+")").attr("selected", "true");
 $("#chi5_select option:eq("+ getCookie('chi5_select')+")").attr("selected", "true");
 $("#promo_code_text").val(getCookie('promo_code_text'));

 if ($("#first_value").val() != "")
  destination_check($("#first_value").val(), 0);
 else
  destination_check(getCookie('first_select'), 0);
//new
 //if ($("#second_value").val() != "")
  //destination_check($("#second_value").val());
 //else
  //destination_check(getCookie('second_select'));*/
//here

 var option_pack = getCookie('option_pack');

 if (option_pack == "hotel_box") {
  $("#hotel_box").attr("checked", "true");
  $("#options_slide").show();
 }
 else if (option_pack == "flights_box") {
  $("#options_slide").show();
  $("#flights_box").attr("checked", "true")
  departure_check(0);
  $("#departure_slide").show(duration);
  load(getCookie('flights'), $("#departure_select"), getCookie('departure_index'));
 }
 else if (option_pack == "ferry_box"){
  $("#options_slide").show();
  $("#ferry_box").attr("checked", "true")
  departure_check(1);
  $("#departure_slide").show(duration);
//new part second_value??
  if ($("#second_select").val() == '28567'){
    $("#ferry_slide_isle_of_man").show(duration);
    load(getCookie('ferry'), $("#departure_select"), getCookie('departure_index'));
    $("#vehicle_type_isle_of_man option:eq("+getCookie('vehicle_type_index_isle_of_man')+")").attr("selected", "true");
    vehicle_type_isle_of_man(document.getElementById("vehicle_type_isle_of_man"));
    $("#vehicle_make_isle_of_man").val(getCookie('vehicle_make_isle_of_man'));
    $("#vehicle_length_isle_of_man").val(getCookie('vehicle_length_isle_of_man'));
    $("#vehicle_reg_isle_of_man").val(getCookie('vehicle_reg_isle_of_man'));
  }else{
  //until here
  $("#ferry_slide").show(duration);
  load(getCookie('ferry'), $("#departure_select"), getCookie('departure_index'));
  $("#vehicle_type option:eq("+getCookie('vehicle_type_index')+")").attr("selected", "true");
  vehicle_type(document.getElementById("vehicle_type"));
  $("#vehicle_make").val(getCookie('vehicle_make'));
  $("#vehicle_length").val(getCookie('vehicle_length'));
  $("#vehicle_reg").val(getCookie('vehicle_reg'));
  }
 }

 $("#booking_form").slideDown(2000);
}

function prepare_functions() {

 $("#first_select").change(function() {change_select(1, $("#" + this.id + " option:selected").val(), $("#second_select_slide"), $("#second_select"), 0)});
 $("#second_select").change(function() {change_select(2, $("#" + this.id + " option:selected").val(), $("#third_select_slide"), $("#third_select"), 0)});
 $("#third_select").change(function() {change_select(3, $("#" + this.id + " option:selected").val(), $("#fourth_select_slide"), $("#fourth_select"), 0)});
 $("#fourth_select").change(function() {change_select(4, $("#" + this.id + " option:selected").val(), $("#null"), $("#null"), 0)});

 $("#flights_box").click(function() {departure_check(0); $("#ferry_slide").hide(duration);$("#ferry_slide_isle_of_man").hide(duration); $("#departure_slide").show(duration); load(flights, $("#departure_select"), 0);});
 $("#ferry_box").click(function() {
 //new part second_value ??

       if ($("#second_select").val() == '28567'){
       $("#ferry_slide_isle_of_man").show(duration);
	     $("#vehicle_type_isle_of_man option:eq(0)").attr("selected", "true");
		 vehicle_type_isle_of_man(document.getElementById("vehicle_type_isle_of_man"));
	     departure_check(1);
	     $("#departure_slide").show(duration);
	   }else{
      //until here

         $("#ferry_slide").show(duration);
	     $("#vehicle_type option:eq(0)").attr("selected", "true");
	     vehicle_type(document.getElementById("vehicle_type"));
	     departure_check(1);
	     $("#departure_slide").show(duration);
       }
	 load(ferry, $("#departure_select"), 0);
	 });



 $("#vehicle_type").change(function() {vehicle_type(this);});
 //new
 $("#vehicle_type_isle_of_man").change(function() {vehicle_type_isle_of_man(this);});
 //here


 $("#hotel_box").click(function() {$("#ferry_slide").hide(duration);$("#ferry_slide_isle_of_man").hide(duration); $("#departure_slide").hide(duration);});

 $("#month_year_select").change(function() {prepare_date(parseInt($("#day_select option:selected").val()), parseInt($("#"+this.id+" option:selected").val()), this.selectedIndex, date.getFullYear(),  null)});

 $("#day_select").change(function() {prepare_date(parseInt($("#day_select option:selected").val()), parseInt($("#month_year_select option:selected").val()), 0, date.getFullYear(),  null)});

 $("#rooms_select").change(function() {rooms($("#" + this.id + " option:selected").val())});

 $("#booking_form_sub_button").click(function() {if (check_form()) {validate_form();}});

 $("#adult_select").change(function() {$("#adu1_select option:eq("+this.selectedIndex+")").attr("selected", "true")});
 $("#children_select").change(function() {$("#chi1_select option:eq("+this.selectedIndex+")").attr("selected", "true")});

}

function prepare_date(day, m, index, y, d) {
    var month = new Array(12);
    month[0] = "January";
    month[1] = "February";
    month[2] = "March";
    month[3] = "April";
    month[4] = "May";
    month[5] = "June";
    month[6] = "July";
    month[7] = "August";
    month[8] = "September";
    month[9] = "October";
    month[10] = "November";
    month[11] = "December";

    if (m == 3 || m == 5 || m == 8 || m == 10) {
       $("#day_select option:eq(28)").css("display", "block");
       $("#day_select option:eq(39)").css("display", "block");
       $("#day_select option:eq(30)").css("display", "none");
       days = 30;
    }
    else if (m == 1 && parseFloat(y/4) - Math.round(parseInt(y/4)) == 0) {
       $("#day_select option:eq(28)").css("display", "block");
       $("#day_select option:eq(29)").css("display", "none");
       $("#day_select option:eq(30)").css("display", "none");
       days = 29;
    }
    else if (m == 1) {
       $("#day_select option:eq(28)").css("display", "none");
       $("#day_select option:eq(29)").css("display", "none");
       $("#day_select option:eq(30)").css("display", "none");
       days = 28;
    }
    else {
       $("#day_select option:eq(28)").css("display", "block");
       $("#day_select option:eq(29)").css("display", "block");
       $("#day_select option:eq(30)").css("display", "block");
       days = 31;
    }

    if (day > days)
        day = days;
    $("#day_select option:eq("+(day-1)+")").attr("selected", "true");

    // Workaround to fix a problem where some dates are showing up as "1899" or "1900".
    // 2008-12-29
    // dsimmons@squiz.co.uk
    if (d != null) {
        if (d.getFullYear() == '1899' || d.getFullYear() == '1900') {

            // Set d to null, so that it will get regenerated in the next statement below
            d = null;
        }
    }

    if (d != null) {
        d.setDate(1);
        $("#month_year_select").children().remove();
        for (i=0; i<12; i++) {
           $("#month_year_select").append('<option value="'+d.getMonth()+'">'+month[d.getMonth()]+' '+d.getFullYear()+'</option>');
           $("#month_year_select").children("option:eq("+index+")").attr("selected", "true");
           d.setMonth(d.getMonth() + 1);
        }
    }
    date = new Date($("#month_year_select option:selected").text().toString().split(" ")[1], $("#month_year_select option:selected").val(), $("#day_select option:selected").val());
}

function verify_date(num) {
 if (num < 10)
  return "0" + num;
 else
  return num;
}

function load(id, select, value) {
 
 select.children().remove();
 select.append('<option value="0">Loading...</option>')
 $.ajax({
  url: url + id,
  error: function() {
   select.children().remove();
   select.append('<option>Not available</option>')
  },
  success: function(doc){
   select.children().remove();
   select.append(doc);
   if (select.children("option:contains('Flights')").val() != null)
    flights = select.children("option:contains('Flights')").val();
   select.children("option:contains('Flights')").remove();
   if (select.children("option:contains('Ferry')").val() != null)
    ferry = select.children("option:contains('Ferry')").val();
   select.children("option:contains('Ferry')").remove();
   select.children("option:eq("+index(select, value)+")").attr("selected", "true");

    $("#flights_box").css('display', 'inline');
	$("#flights_box").next().css('display', 'inline');
	$("#ferry_box").css('display', 'inline');
	$("#ferry_box").next().css('display', 'inline');
	
	if (flights == '') {
	 $("#flights_box").hide();
	 $("#flights_box").next().hide();
	}
	if (ferry == '') {
	 $("#ferry_box").hide();
	 $("#ferry_box").next().hide();
	}

   if (version <= 4 && version_ie >= 0) {
    jQuery.each(select.children("option[class='disabled']"), function() {$(this).replaceWith("<optgroup label='"+$(this).val()+"'/>");});
   }
  }
 });
}

function index(select, value) {
 var index = 0;
 jQuery.each(select.children("option"), function(i) {
  if ($(this).val() == value)
   index = i;
  });
 jQuery.each(select.children("option"), function(i) {
  if ($(this).text() == value)
   index = i;
  });
 return index;
}

function session_destination_check(value) {
 switch (value) {
 case America[1]:
 session_destination(America);
 break;
 case Australia[1]:
 session_destination(Australia);
 break;
 case BaliLombok[1]:
 session_destination(BaliLombok);
 break;
 case BorneoBrunei[1]:
 session_destination(BorneoBrunei);
 break;
 case Canada[1]:
 session_destination(Canada);
 break;
 case CI[1]:
 session_destination(CI);
 break;
 case ChinaTibet[1]:
 session_destination(ChinaTibet);
 break;
 case Dubai[1]:
 session_destination(Dubai);
 break;
 case Egypt[1]:
 session_destination(Egypt);
 break;
 case Fiji[1]:
 session_destination(Fiji);
 break;
 case HongKong[1]:
 session_destination(HongKong);
 break;
 case Malaysia[1]:
 session_destination(Malaysia);
 break;
 case NewZealand[1]:
 session_destination(NewZealand);
 break;
 case Scotland[1]:
 session_destination(Scotland);
 break;
 case SingaporeBintan[1]:
 session_destination(SingaporeBintan);
 break;
 case SouthAfrica[1]:
 session_destination(SouthAfrica);
 break;
 case Thailand[1]:
 session_destination(Thailand);
 break;
 case IOM[1]:
 session_destination(IOM);
 break;
 case Caribbean[1]:
 session_destination(Caribbean);
 break;
 case IndianOcean[1]:
 session_destination(IndianOcean);
 break;
 case FarEast[1]:
 session_destination(FarEast);
 break;
 case MiddleEast[1]:
 session_destination(MiddleEast);
 break;
 case UKCityBreaks[1]:
 session_destination(UKCityBreaks);
 break;
 case EuroCityBreaks[1]:
 session_destination(EuroCityBreaks);
 break;
 default:
 break;
 }
}

function session_inclusive_destination_check(value) {
 switch (value) {
 case JER_inclusive[1]:
 session_destination(JER_inclusive);
 break;
 case GCI_inclusive[1]:
 session_destination(GCI_inclusive);
 break;
 case IOM_inclusive[1]:
 session_destination(IOM_inclusive);
 break;
 case JERSELF_inclusive[1]:
 session_destination(JERSELF_inclusive);
 break;
 case GCISELF_inclusive[1]:
 session_destination(GCISELF_inclusive);
 break;
 case BELFAST_inclusive[1]:
 session_destination(BELFAST_inclusive);
 break;
 case EDINBURGH_inclusive[1]:
 session_destination(EDINBURGH_inclusive);
 break;
 case GLASGOW_inclusive[1]:
 session_destination(GLASGOW_inclusive);
 break;
 case AMSTERDAM_inclusive[1]:
 session_destination(AMSTERDAM_inclusive);
 break;
 case BRUSSELS_inclusive[1]:
 session_destination(BRUSSELS_inclusive);
 break;
 case MAJORCA_inclusive[1]:
 session_destination(MAJORCA_inclusive);
 break;
 case NICE_inclusive[1]:
 session_destination(NICE_inclusive);
 break;
 default:
 break;
 }
}

function destination_check(select_value, value) {

    switch (select_value) {
        case America[1]:
        $("#booking_form_title").text("Book a Hotel");
        $("#age").text("(2-18)");
        $("#rooms_slide").show(duration);
        $("#options_slide").show(duration);
        $("#departure_slide").hide(duration); 
        rooms($("#rooms_select option:selected").val());
        break;
        case CI[1]:
        $("#booking_form_title").text("Check prices and availability");
        $("#age").text("(2-11)");
        $("#rooms_slide").show(duration);
        $("#options_slide").show(duration);
        $("#departure_slide").hide(duration);
        rooms($("#rooms_select option:selected").val());
        break;
        default:
        $("#booking_form_title").text("Book a Hotel");
        $("#age").text("(2-11)");
        $("#rooms_slide").show(duration);
        $("#options_slide").show(duration);
        $("#departure_slide").hide(duration);
        rooms("1");
        break;
    }


     
    $("#ferry_slide").hide(duration);
	//new
	$("#ferry_slide_isle_of_man").hide(duration)
		//here
    $("#flights_box").removeAttr("checked");
    $("#ferry_box").removeAttr("checked");
    $("#hotel_box").removeAttr("checked");
    $("#vehicle_type option:eq(0)").attr("selected", "true");
    vehicle_type(document.getElementById("vehicle_type"));
	//new
	$("#vehicle_type_isle_of_man option:eq(0)").attr("selected", "true");
    vehicle_type_isle_of_man(document.getElementById("vehicle_type_isle_of_man"));
	//here
}

function rooms(number) {
    switch (number) {
        case "1":
        $("#rooms1_slide").hide(duration);
        $("#rooms2_slide").hide(duration);
        $("#rooms3_slide").hide(duration);
        $("#rooms4_slide").hide(duration);
        $("#rooms5_slide").hide(duration);
        break;
        case "2":
        $("#rooms1_slide").show(duration);
        $("#rooms2_slide").show(duration);
        $("#rooms3_slide").hide(duration);
        $("#rooms4_slide").hide(duration);
        $("#rooms5_slide").hide(duration);
        break;
        case "3":
        $("#rooms1_slide").show(duration);
        $("#rooms2_slide").show(duration);
        $("#rooms3_slide").show(duration);
        $("#rooms4_slide").hide(duration);
        $("#rooms5_slide").hide(duration);
        break;
        case "4":
        $("#rooms1_slide").show(duration);
        $("#rooms2_slide").show(duration);
        $("#rooms3_slide").show(duration);
        $("#rooms4_slide").show(duration);
        $("#rooms5_slide").hide(duration);
        break;
        case "5":
        $("#rooms1_slide").show(duration);
        $("#rooms2_slide").show(duration);
        $("#rooms3_slide").show(duration);
        $("#rooms4_slide").show(duration);
        $("#rooms5_slide").show(duration);
        break;
    }
}

function vehicle_type(id) {
if (id !== null && $("#" + id.id + " option:selected").val() == " ") {
  $("#vehicle_make").attr("value", "N/A");
  $("#vehicle_make").attr("disabled", "true");
  $("#vehicle_length").attr("value", "N/A");
  $("#vehicle_length").attr("disabled", "true");
  $("#vehicle_reg").attr("value", "N/A");
  $("#vehicle_reg").attr("disabled", "true");
 }
 else {
  $("#vehicle_make").attr("value", "");
  $("#vehicle_make").removeAttr("disabled");
  $("#vehicle_length").attr("value", "450");
  $("#vehicle_length").removeAttr("disabled");
  $("#vehicle_reg").attr("value", "");
  $("#vehicle_reg").removeAttr("disabled");
 }
}
//new
function vehicle_type_isle_of_man(id) {
if (id !== null && $("#" + id.id + " option:selected").val() == " ") {
  $("#vehicle_make_isle_of_man").attr("value", "N/A");
  $("#vehicle_make_isle_of_man").attr("disabled", "true");
  $("#vehicle_length_isle_of_man").attr("value", "N/A");
  $("#vehicle_length_isle_of_man").attr("disabled", "true");
  $("#vehicle_reg_isle_of_man").attr("value", "N/A");
  $("#vehicle_reg_isle_of_man").attr("disabled", "true");
 }
 else {
  $("#vehicle_make_isle_of_man").attr("value", "");
  $("#vehicle_make_isle_of_man").removeAttr("disabled");
  $("#vehicle_length_isle_of_man").attr("value", "450");
  $("#vehicle_length_isle_of_man").removeAttr("disabled");
  $("#vehicle_reg_isle_of_man").attr("value", "");
  $("#vehicle_reg_isle_of_man").removeAttr("disabled");
 }
}
//here

function departure_check(opt) {
 var port_value;
 var value = new Array();
 $("#anite_code > input").each(function() {value.push(jQuery(this).val());});

 switch (opt) {
 case 0:
  if (departure_check_values(value, JER_inclusive[0])) {
	port_value = DestOverride_flights[2];
  }
  if (departure_check_values(value, GCI_inclusive[0])) {
	port_value = DestOverride_flights[0];
  }
  if (departure_check_values(value, IOM_inclusive[0])) {
	port_value = DestOverride_flights[1];
  }
  if (departure_check_values(value, JERSELF_inclusive[0])) {
	port_value = DestOverride_flights[2];
  }
  if (departure_check_values(value, GCISELF_inclusive[0])) {
	port_value = DestOverride_flights[0];
  }
  if (departure_check_values(value, BELFAST_inclusive[0])) {
	port_value = DestOverride_flights[3];
  }
  if (departure_check_values(value, EDINBURGH_inclusive[0])) {
	port_value = DestOverride_flights[4];
  }
  if (departure_check_values(value, GLASGOW_inclusive[0])) {
	port_value = DestOverride_flights[5];
  }
  if (departure_check_values(value, AMSTERDAM_inclusive[0])) {
	port_value = DestOverride_flights[6];
  }
  if (departure_check_values(value, BRUSSELS_inclusive[0])) {
	port_value = DestOverride_flights[7];
  }
  if (departure_check_values(value, MAJORCA_inclusive[0])) {
	port_value = DestOverride_flights[8];
  }
  if (departure_check_values(value, NICE_inclusive[0])) {
	port_value = DestOverride_flights[9];
  }
 break;
 case 1:
  if (departure_check_values(value, JER_inclusive[0])) {
	port_value = DestOverride_ferry[2];
  }
  if (departure_check_values(value, GCI_inclusive[0])) {
	port_value = DestOverride_ferry[0];
  }
  if (departure_check_values(value, IOM_inclusive[0])) {
	port_value = DestOverride_ferry[1];
  }
  if (departure_check_values(value, JERSELF_inclusive[0])) {
	port_value = DestOverride_ferry[2];
  }
  if (departure_check_values(value, GCISELF_inclusive[0])) {
	port_value = DestOverride_ferry[0];
  }
 break;
 default:
 break;
 }
 $("#OutArrAir").val(port_value);
 $("#RetDepAir").val(port_value);
}

function departure_check_values(values, obj) {

	var boolean = new Array();
	obj = obj.split("|");
	
	for (i=0; i<obj.length; i++) {
		for (j=0; j<values.length; j++) {
			if (obj[i] === values[j]) {
				boolean.push(true);
			}
		}
	}
	
	return obj.length === boolean.length;
}

function change_select(order, id, obj, select, value) {

 if (id == 0 || isNaN(id) || id == "" || id == "null") {
 }
 else {
  flights = '';
  ferry = '';
 }

 if (id != "null" && id != "" && id != null && id != "00")
  anite_code(id);
 if (value != "null" && value != "" && value != null && isNaN(value) && value != "00")
  anite_code(value);

 if ($("#first_value").val() != "")
  destination_check($("#first_value").val(), id);
 else
  destination_check($("#first_select option:selected").val(), id);
//here
/*if ($("#second_value").val() != "")
  destination_check($("#second_value").val());
 else
  destination_check($("#second_select option:selected").val());*/
//until here

 switch (order) {
  case 1:
   $("#third_select_slide").hide(duration);
   $("#third_select").children().remove();
   $("#fourth_select_slide").hide(duration);
   $("#fourth_select").children().remove();
  break;
  case 2:
   $("#fourth_select_slide").hide(duration);
   $("#third_select").children().remove();
  break;
  default:
  break;
 }

 if (obj.css('display') == 'block') {
 	$('#options_slide').hide(duration);
    $("#flights_box").removeAttr("checked");
    $("#ferry_box").removeAttr("checked");
    $("#hotel_box").removeAttr("checked");
 }

 if (id == 0 || isNaN(id) || id == "" || id == "null")
  obj.hide(duration);
 else {
  obj.show(duration);
  load(id, select, value);
 } 

}

function check_form() {
 if ($("#anite_code > input:eq(0)").val() == "" && $("#MainAccom").val() == "")
  $("#destination_error").show()
 else
  $("#destination_error").hide()
 if ($("#options_slide").css("display") == "block" && $("#ferry_box").attr("checked") == undefined && $("#flights_box").attr("checked") == undefined && $("#hotel_box").attr("checked") == undefined)
  $("#options_error").show()
 else
  $("#options_error").hide()
 if (($("#ferry_box").attr("checked") || $("#flights_box").attr("checked")) && $("#departure_select option:eq(0)").attr("selected"))
  $("#departure_error").show()
 else
  $("#departure_error").hide()

	 //new
if ($("#second_select").val() == '28567'){

if ($("#ferry_box").attr("checked") && $("#vehicle_type_isle_of_man option:selected").val() != " " && $("#vehicle_make_isle_of_man").val() == "")
  $("#vehicle_make_isle_of_man_error").show()
 else
  $("#vehicle_make_isle_of_man_error").hide()
 if ($("#ferry_box").attr("checked") && $("#vehicle_type_isle_of_man option:selected").val() != " " && $("#vehicle_length_isle_of_man").val() == "")
  $("#vehicle_length_isle_of_man_error").show()
 else
  $("#vehicle_length_isle_of_man_error").hide()
 if ($("#ferry_box").attr("checked") && $("#vehicle_type_isle_of_man option:selected").val() != " " && $("#vehicle_reg_isle_of_man").val() == "")
  $("#vehicle_reg_isle_of_man_error").show()
 else
  $("#vehicle_reg_isle_of_man_error").hide()

}else{

  if ($("#ferry_box").attr("checked") && $("#vehicle_type option:selected").val() != " " && $("#vehicle_make").val() == "")
  $("#vehicle_make_error").show()
 else
  $("#vehicle_make_error").hide()
 if ($("#ferry_box").attr("checked") && $("#vehicle_type option:selected").val() != " " && $("#vehicle_length").val() == "")
  $("#vehicle_length_error").show()
 else
  $("#vehicle_length_error").hide()
 if ($("#ferry_box").attr("checked") && $("#vehicle_type option:selected").val() != " " && $("#vehicle_reg").val() == "")
  $("#vehicle_reg_error").show()
 else
  $("#vehicle_reg_error").hide()

}

 //here
 if ($("#agent_initials").val() == "")
  $("#initials_error").show()
 else
  $("#initials_error").hide()
 submit = true;
 jQuery.each($("li.errors"), function() {
  if (this.style.display == "block")
   submit = false;
 })
 return submit;
}

function anite_code(code) {
  $("#anite_code > input").val("");
  if(typeof code != 'undefined' && isNaN(code)) {
   code = code.split("#");
   jQuery.each(code, function(i) {
    $("#anite_code > input:eq("+i+")").val(code[i]);
   });
  }
}

function session_destination(id) {
 $("#CompCode").val(id[3]);
 $("#BranchCode").val(id[2]);
 $("#Source").val(id[5]);
 $("#Referrer").val(id[5]);
 $("#SessionDestination").val(id[4]);
}

function validate_form() {

 if ($("#hotel_box").attr("checked") && $("#hotel_code").val() != "") {
  $("#search_form").attr("action", "http://ecom.premierholidays.co.uk/ecom/AtopWeb/live/En/AccomOnly/ATOPWeb.ASP?WCU=NewAccOnly&WCI=CWIAvailabilityCriteria&WCE=Check");
  $("#searchType").val("ACCOMONLY");
 }
 else if ($("#flights_box").attr("checked") && $("#hotel_code").val() != "") {
  $("#search_form").attr("action", "http://ecom.premierholidays.co.uk/ecom/AtopWeb/live/En/Inclusive/ATOPWeb.ASP?WCI=CWIAvailability&WCE=CriteriaForm&WCU=NewSched&bst=ATOPFare");
  $("#searchType").val("INCLUSIVE");
 }
 else if ($("#flights_box").attr("checked")) {
  $("#search_form").attr("action", "http://ecom.premierholidays.co.uk/ecom/AtopWeb/live/En/Inclusive/ATOPWeb.ASP?WCI=CWIAvailability&WCE=CriteriaForm&WCU=NewSched&bst=ATOPFareSearch");
  $("#searchType").val("INCLUSIVESEARCH");
 }
 else if ($("#ferry_box").attr("checked") && $("#hotel_code").val() != "") {
  $("#search_form").attr("action", "http://ecom.premierholidays.co.uk/ecom/AtopWeb/live/En/Inclusive/ATOPWeb.ASP?WCI=CWIFerry&WCE=Avail&WCU=NewSched&bst=IncFerry");
  $("#searchType").val("INCLUSIVE");
 }
 else if ($("#ferry_box").attr("checked")) {
  $("#search_form").attr("action", "http://ecom.premierholidays.co.uk/ecom/AtopWeb/live/En/Inclusive/ATOPWeb.ASP?WCI=CWIFerry&WCE=Avail&WCU=NewSched&bst=IncFerry");
  $("#searchType").val("");
 }
 else {
  $("#search_form").attr("action", "http://ecom.premierholidays.co.uk/ecom/AtopWeb/live/En/AccomOnly/ATOPWeb.ASP?WCU=NewAccOnly&WCI=CWIAvailabilityCriteria&WCE=Check");
  $("#searchType").val("ACCOMSEARCH");
 }



 session_destination_check($("#first_value").val());
 //new
 //session_destination_check($("#second_value").val());
 //here
 session_destination_check($("#default_location").val());
 session_destination_check($("#first_select option:selected").val());
 session_destination_check($("#second_select option:selected").val());
 if ($("#ferry_box").attr("checked") || $("#flights_box").attr("checked")) {
  session_inclusive_destination_check($("#default_location").val());
  session_inclusive_destination_check($("#second_select option:selected").val());
 }

 $("#Operator").val($("#agent_initials").val());
 $("#Agent").val($("#agent_code").val());
 $("#AutoLogonABTA").val($("#agent_code").val());
 $("#AdultPax").val($("#adult_select option:selected").val());
 $("#ChildPax").val($("#children_select option:selected").val());
 $("#InfantPax").val($("#infant_select option:selected").val());
 $("#Duration").val($("#duration_select option:selected").val());
 $("#OutDepDate").val(verify_date(parseInt(date.getDate())) + "/" + verify_date(parseInt((date.getMonth()+1))) + "/" + date.getFullYear());
 ret_date = new Date(date);
 ret_date.setDate(date.getDate() + parseInt($("#duration_select option:selected").val()));
 $("#RetDepDate").val(verify_date(parseInt(ret_date.getDate())) + "/" + verify_date(parseInt((ret_date.getMonth()+1))) + "/" + ret_date.getFullYear());
 $("#OutDepAir").val($("#departure_select option:selected").val());
 $("#RetArrAir").val($("#departure_select option:selected").val());
 $("#PromotionCode").val($("#promo_code_text").val());
 $("#HotelRooms").val($("#rooms_select option:selected").val());
 $("#adu_room1").val($("#adu1_select option:selected").val());
 $("#adu_room2").val($("#adu2_select option:selected").val());
 $("#adu_room3").val($("#adu3_select option:selected").val());
 $("#adu_room4").val($("#adu4_select option:selected").val());
 $("#adu_room5").val($("#adu5_select option:selected").val());
 $("#chi_room1").val($("#chi1_select option:selected").val());
 $("#chi_room2").val($("#chi2_select option:selected").val());
 $("#chi_room3").val($("#chi3_select option:selected").val());
 $("#chi_room4").val($("#chi4_select option:selected").val());
 $("#chi_room5").val($("#chi5_select option:selected").val());


 //new
 if ($("#second_select").val() == '28567'){
	 $("#VehType").val($("#vehicle_type_isle_of_man option:selected").val());
 }else{
 $("#VehType").val($("#vehicle_type option:selected").val());
 }
 $("#OutDepAir").val($("#departure_select option:selected").val());
 $("#RetArrAir").val($("#departure_select option:selected").val());
if ($("#second_select").val() == '28567'){
	$("#VehMake").val($("#vehicle_make_isle_of_man").val());
	$("#VehLength").val($("#vehicle_length_isle_of_man").val());
      $("#VehRegistration").val($("#vehicle_reg_isle_of_man").val());
}else{
 $("#VehMake").val($("#vehicle_make").val());
 $("#VehLength").val($("#vehicle_length").val());
 $("#VehRegistration").val($("#vehicle_reg").val());
}
 //here


//here new value???
/*if ($("#second_select").val() == '28567'){
	  $("#VehType_isle_of_man").val($("#vehicle_type_isle_of_man option:selected").val());
      $("#OutDepAir").val($("#departure_select option:selected").val());
      $("#RetArrAir").val($("#departure_select option:selected").val());
      $("#VehMake_isle_of_man").val($("#vehicle_make_isle_of_man").val());
      $("#VehLength_isle_of_man").val($("#vehicle_length_isle_of_man").val());
      $("#VehRegistration_isle_of_man").val($("#vehicle_reg_isle_of_man").val());
 }

$("#VehType").val($("#vehicle_type option:selected").val());
 $("#OutDepAir").val($("#departure_select option:selected").val());
 $("#RetArrAir").val($("#departure_select option:selected").val());
 $("#VehMake").val($("#vehicle_make").val());
 $("#VehLength").val($("#vehicle_length").val());
 $("#VehRegistration").val($("#vehicle_reg").val());
 */
//new
  leaving_booking_form();
}

function leaving_booking_form() {
 setCookie('agent_initials', $("#agent_initials").val(), cookie_duration);
 setCookie('first_select', $("#first_select").val(), cookie_duration);
 setCookie('second_select', $("#second_select").val(), cookie_duration);
 setCookie('third_select', $("#third_select").val(), cookie_duration);
 setCookie('fourth_select', $("#fourth_select").val(), cookie_duration);







 //until here

 setCookie('day', $("#day_select option:selected").val(), cookie_duration);
 setCookie('month', $("#month_year_select option:eq(0)").val(), cookie_duration);
 setCookie('month_actual', $("#month_year_select option:selected").val(), cookie_duration);
 setCookie('month_index', document.getElementById('month_year_select').selectedIndex, cookie_duration);
 setCookie('year', $("#month_year_select option:eq(0)").text().toString().split(" ")[1], cookie_duration);
 setCookie('duration_select', document.getElementById('duration_select').selectedIndex, cookie_duration);
 //setCookie('option_pack', $("#options_slide input:checked").attr("id"), cookie_duration);
 //setCookie('departure_index', $('#departure_select option:selected').val(), cookie_duration);
 //setCookie('flights', flights, cookie_duration);
 //setCookie('ferry', ferry, cookie_duration);
 setCookie('adult_select', document.getElementById('adult_select').selectedIndex, cookie_duration);
 setCookie('children_select', document.getElementById('children_select').selectedIndex, cookie_duration);
 setCookie('infant_select', document.getElementById('infant_select').selectedIndex, cookie_duration);
 setCookie('rooms_select', document.getElementById('rooms_select').selectedIndex, cookie_duration);
 setCookie('adu1_select', document.getElementById('adu1_select').selectedIndex, cookie_duration);
 setCookie('chi1_select', document.getElementById('chi1_select').selectedIndex, cookie_duration);
 setCookie('adu2_select', document.getElementById('adu2_select').selectedIndex, cookie_duration);
 setCookie('chi2_select', document.getElementById('chi2_select').selectedIndex, cookie_duration);
 setCookie('adu3_select', document.getElementById('adu3_select').selectedIndex, cookie_duration);
 setCookie('chi3_select', document.getElementById('chi3_select').selectedIndex, cookie_duration);
 setCookie('adu4_select', document.getElementById('adu4_select').selectedIndex, cookie_duration);
 setCookie('chi4_select', document.getElementById('chi4_select').selectedIndex, cookie_duration);
 setCookie('adu5_select', document.getElementById('adu5_select').selectedIndex, cookie_duration);
 setCookie('chi5_select', document.getElementById('chi5_select').selectedIndex, cookie_duration);
 setCookie('promo_code_text', $("#promo_code_text").val(), cookie_duration);
 setCookie('location', location, cookie_duration);

 setCookie('vehicle_make', $("#vehicle_make").val(), cookie_duration);
 setCookie('vehicle_length', $("#vehicle_length").val(), cookie_duration);
 setCookie('vehicle_reg', $("#vehicle_reg").val(), cookie_duration);

 if ($("#departure_select option:selected").val().toString().toLowerCase() == "low" && ($("#flights_box").attr("checked") || $("#ferry_box").attr("checked"))) {
  sub_cancel();
 }
 else {
  $("#search_form").submit();
 }
  
}