// destinations: name#id#branchcode#compcode#sessiondestination#source nie ta

var CI = "Channel Islands and the Isle of Man#28521#HWB#H#ci#directci".split("#");

// inclusive search

var JER_inclusive = "JD#28565#KWA#K#ci#directci".split("#");
var GCI_inclusive  = "GU#28566#KWA#K#ci#directci".split("#");
var JERSELF_inclusive  = "JD#107800#KSW#K#ci#directci".split("#");
var GCISELF_inclusive  = "GU#107982#KSW#K#ci#directci".split("#");

// destination override

var DestOverride_flights = "GCI#IOM#JER".split("#");
var DestOverride_ferry = "GU#IM#JE".split("#");

// global variables
var site_url = $("#site_url").val();

var url = site_url + "/booking_form_developement/xmlgenerator?asset=";
// local proxy for debugging
// var url = 'proxy.php?url=' + site_url + "/booking_form_developement/xmlgenerator/?asset=";
var quote_url = site_url + "/holiday_quote";
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());
 //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());
 else
  destination_check(getCookie('first_select'));
//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");
 }
 else if (option_pack == "flights_box") {
  $("#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"){
  $("#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");

    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 availiable</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")
   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 CI[1]:
 session_destination(CI);
 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 JERSELF_inclusive[1]:
 session_destination(JERSELF_inclusive);
 break;
 case GCISELF_inclusive[1]:
 session_destination(GCISELF_inclusive);
 break;
 default:
 break;
 }
}

function destination_check(select_value) {
    switch (select_value) {
        case CI[1]:
        $("#booking_form_title").text("Check prices and availability");
        $("#age").text("(2-11)");
        //$("#rooms_slide").hide(duration);
        $("#departure_slide").hide(duration);
        rooms("1");
        if ($("#anite_code > input:eq(0)").val() == "" && $("#MainAccom").val() == "")
         $("#options_slide").hide(duration);
        else
         $("#options_slide").show(duration);
        break;
        default:
        $("#booking_form_title").text("Book a Hotel");
        $("#age").text("(2-11)");
        //$("#rooms_slide").hide(duration);
        $("#options_slide").hide(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;
 switch (opt) {
 case 0:
  switch ($("#AccomLocation").val()) {
  case JER_inclusive[0]:
   port_value = DestOverride_flights[2];
  break;
  case GCI_inclusive[0]:
   port_value = DestOverride_flights[0];
  break;
  case JERSELF_inclusive[0]:
   port_value = DestOverride_flights[2];
  break;
  case GCISELF_inclusive[0]:
   port_value = DestOverride_flights[0];
  break;
  default:
  break;
  }
 break;
 case 1:
  switch ($("#AccomLocation").val()) {
  case JER_inclusive[0]:
   port_value = DestOverride_ferry[2];
  break;
  case GCI_inclusive[0]:
   port_value = DestOverride_ferry[0];
  break;
  case JERSELF_inclusive[0]:
   port_value = DestOverride_ferry[2];
  break;
  case GCISELF_inclusive[0]:
   port_value = DestOverride_ferry[0];
  break;
  default:
  break;
  }
 break;
 default:
 break;
 }
 $("#OutArrAir").val(port_value);
 $("#RetDepAir").val(port_value);
}

function change_select(order, id, obj, select, value) {

 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());
 else
  destination_check($("#first_select option:selected").val());
//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 (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");
	 }

 if (document.getElementById("agent_code"))
  $("#search_form").attr("target", "_blank");

 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($("#first_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);
 setCookie('option_pack', null, cookie_duration);
 jQuery.each($("#options_slide > input"), function() {
  if (this.checked)
   setCookie('option_pack', this.id, cookie_duration);
 })
 setCookie('flights', flights, cookie_duration);
 setCookie('ferry', ferry, cookie_duration);
 setCookie('departure_index', $("#departure_select option:selected").text(), cookie_duration);
 setCookie('vehicle_type_index', document.getElementById("vehicle_type").selectedIndex, 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);

//new isle of man
if (document.getElementById("vehicle_type_isle_of_man")) {
	setCookie('vehicle_type_index_isle_of_man', document.getElementById("vehicle_type_isle_of_man").selectedIndex, cookie_duration);
}
  setCookie('vehicle_make_isle_of_man', $("#vehicle_make_isle_of_man").val(), cookie_duration);
 setCookie('vehicle_length_isle_of_man', $("#vehicle_length_isle_of_man").val(), cookie_duration);
 setCookie('vehicle_reg_isle_of_man', $("#vehicle_reg_isle_of_man").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('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);

if ($("#departure_select option:selected").val().toString().toLowerCase() == "low" && ($("#flights_box").attr("checked") || $("#ferry_box").attr("checked"))) {
	sub_cancel();
} else {

  $("#search_form").submit();
}
}


function sub_cancel() {
 $("body:eq(0)").append("<div id='first_layer'></div>");
 $("#first_layer").css({height:$(window).height(), width:$(window).width(), opacity:0.7});
 $("#first_layer").fadeIn("slow");
 $("body:eq(0)").append("<div id='second_layer'></div>");
 $("#second_layer").css({left:$(window).width()/2 - 120});
 $("#second_layer").append("<p>You have selected a departure point which is not availiable to book online</p><p>In order to book selected holiday please fill in the Holiday Quote Form</p><input type='button' id='sub_cancel_proceed' value='PROCEED' /><input type='button' id='sub_cancel_cancel' value='CANCEL' />");
 $("#sub_cancel_proceed").click(function() {sub_cancel_proceed();});
 $("#sub_cancel_cancel").click(function() {
   $("#second_layer").hide("slow", function() {
     $(this).remove();
   });
   $("#first_layer").fadeOut("slow", function() {
     $(this).remove();
   });
 });
 $("#second_layer").show("slow");
}

function sub_cancel_proceed() {
$("#second_layer").children().remove();
$("#second_layer").append("<h2>Loading...</h2><div id='ajax-loader'><p>Please Wait...</p></div>");
$.ajax({
        url: quote_url,
        error: function() {
         window.location = quote_url;
        },
        success: function(doc){
         $("body:first").fadeOut("slow", function() {quote_form_append(doc);})
        }
  });
}

function quote_form_append(doc) {
 var date_value = $("#day_select option:selected").text();
 var month_value = $("#month_year_select option:selected").val();
 var year_value = $("#month_year_select option:selected").text().toString().split(" ")[1];
 var duration_value = $("#duration_select option:selected").val();
 var destination_value = $("#AccomLocation").val().toString().charAt(0);
 var flights_value = $("#flights_box").attr("checked");
 var ferry_value = $("#ferry_box").attr("checked");
 var port_value = $("#departure_select option:selected").text();
 var adult_value = $("#adult_select option:selected").text();
 var children_value = $("#children_select option:selected").text();
 var infant_value = $("#infant_select option:selected").text();
 var vehicle_type_value = $("#vehicle_type option:selected").val();
//new isle of man not imp. but needs conditions value???
 if ($("#second_select").val() == '28567'){
 var vehicle_type_isle_of_man_value = $("#vehicle_type_isle_of_man option:selected").val();
 }
 //until here


 var agent_code_value = $("#agent_code").val();
 var agency_name_value = $("#agency_name").val();

 $("body:first").children().remove();
 $("body:first").append("<div id='wrapper'></div>");
 $("#wrapper").append($(doc).children());
 $("#wrapper > li:eq(0)").remove();
 $("#wrapper > li:eq(0)").remove();
 $("select[id='q21446:q1value[d]'] > option:contains("+date_value+")").attr("selected", "true");
 $("select[id='q21446:q1value[m]'] > option:eq("+(index($("select[id='q21446:q1value[m]']"), month_value)+1)+")").attr("selected", "true");
 $("select[id='q21446:q1value[y]'] > option:contains("+year_value+")").attr("selected", "true");
 $("select[id='q21446_q3'] > option:eq("+(parseInt(duration_value)-1)+")").attr("selected", "true");
 $("select[id='q21447_q1'] > option:contains("+destination_value+")").attr("selected", "true");
 $("select[id='q21452_q2'] > option:eq(1)").attr("selected", "true");
 if (flights_value) {
  document.getElementById('q21452_q1_0').checked = true;
  $("select[id='q21452_q3'] > option:contains("+port_value+")").attr("selected", "true");
 }
 if (ferry_value) {
  document.getElementById('q21452_q1_1').checked = true;
  $("select[id='q21452_q4'] > option:contains("+port_value+")").attr("selected", "true");
  if (vehicle_type_value != " ")
   $("select[id='q21452_q2'] > option:eq(0)").attr("selected", "true");
 }
 $("select[id='q21449_q44'] > option:contains("+adult_value+")").attr("selected", "true");
 $("select[id='q21449_q16'] > option:contains("+infant_value+")").attr("selected", "true");
 $("select[id='q21449_q17'] > option:contains("+children_value+")").attr("selected", "true");
 $("input[id='q21449_q37']").val(agent_code_value);
 $("input[id='q21449_q38']").val(agency_name_value);
 $("body:first").fadeIn("slow");
}
