function DateParse(xferDirection,fromtab){
    var tmpDate = new Date();
    var arrSelectedDate = $("input:hidden#tmp"+fromtab+"Date"+xferDirection).val().split(":");
    if(arrSelectedDate.length==3){
        tmpDate.setFullYear(arrSelectedDate[0]);
        tmpDate.setMonth(parseInt(arrSelectedDate[1],10)-1);
        tmpDate.setDate(parseInt(arrSelectedDate[2],10));
    }
    return tmpDate;
}
function leftPad(x,cha,length){x=x.toString();while(x.length<length)x=cha+x;return x;}
function fnDropDownReduce(objSource,intRemaining){for(intI=objSource.options.length-1;intI>=intRemaining;intI--)objSource.remove(intI);}
function UpdateRetAirports(fromtab,defaultvalue){
    var depcode = $("select#OBpoint"+fromtab).val();
    var obj = document.getElementById("IBPoint"+fromtab);
    $(".childselector").each(function(j){childAgesDropDown(this);})
    fnDropDownReduce(obj,1);
    $.post("./_xml/GetRetAirport.aspx", { Type: fromtab, DepartCode: depcode }, function(data){ for (var i = 0; i < data.length; i++) { obj.options[obj.options.length] = new Option(data[i].optionText,data[i].option); } obj.value=defaultvalue; },"json");
}
function StopSalesValidation(){
    var boolAllowSearch = true;
    var SearchType = ($("#searchbox").tabs('option','selected')==0?"FO":"PK");
    var strPostData = "OBDate="+$("#tmpOBDate"+SearchType).val().replace(':','');
    strPostData += "&IBDate="+$("#tmpIBDate"+SearchType).val().replace(':','');
    strPostData += "&OBPoint="+$("#OBpoint"+SearchType).val();
    strPostData += "&IBPoint="+$("#IBpoint"+SearchType).val();
    $.ajax({ type: "POST", url: "./_xml/GetStopSale.aspx", data: strPostData, cache: false, async: false , success: function(data){ boolAllowSearch = (data=="Y"? false : true); } });
    if (!boolAllowSearch) alert("Sorry - This route isn't available! Why not check our one of our other great destinations");
    return boolAllowSearch;
}
function processPopup(cal) {
    var button = cal.params.button.id;
    var xferDirection = (button.indexOf('OB') != -1 ? "OB" : "IB");
    var fromtab = button.substring(button.length,button.length-2);
    setDropdowns(xferDirection,fromtab);
}
function processDropdown(xferDirection,field,fromtab){
    var DateTimeFirst = Date.parse($("input:hidden#tmp"+xferDirection+"Date"+fromtab).val().replace(":","/").replace(":","/"));
if (field=="MonthYear" && $("select#"+xferDirection+"MonthYear"+fromtab).val()==""){
        $("select#"+xferDirection+"Day"+fromtab).val("");
        $("input:hidden#tmp"+xferDirection+"Date"+fromtab).val("");
        var objDay = document.getElementById(xferDirection+"Day"+fromtab);
        fnDropDownReduce(objDay,1);
    }
    else if (field=="Day" && $("select#"+xferDirection+"MonthYear"+fromtab).val()==""){
        $("select#"+xferDirection+"MonthYear"+fromtab).val("");
        $("input:hidden#tmp"+xferDirection+"Date"+fromtab).val("");
        var objDay = document.getElementById(xferDirection+"Day"+fromtab);
        fnDropDownReduce(objDay,1);
    }
    else{
        $("input:hidden#tmp"+xferDirection+"Date"+fromtab).val($("select#"+xferDirection+"MonthYear"+fromtab).val()+":"+($("select#"+xferDirection+"Day"+fromtab).val()=="" ? "01" : $("select#"+xferDirection+"Day"+fromtab).val()));
    }
    setDropdowns(xferDirection,fromtab);
    if (xferDirection=="OB" && $("input:hidden#tmpIBDate"+fromtab).val()!="" && $("input:hidden#tmpOBDate"+fromtab).val().length==10){
        var DaysDifference = (Date.parse($("input:hidden#tmpOBDate"+fromtab).val().replace(":","/").replace(":","/"))-DateTimeFirst)/1000/60/60/24;
        var ReturnDateTime = new Date($("input:hidden#tmpIBDate"+fromtab).val().substr(0,4),parseInt($("input:hidden#tmpIBDate"+fromtab).val().substr(5,2),10)-1,parseInt($("input:hidden#tmpIBDate"+fromtab).val().substr(8,2),10)+DaysDifference);
        var strNew = ReturnDateTime.getFullYear() + ":";
        strNew += leftPad(ReturnDateTime.getMonth()+1,'0',2) + ":";
        strNew += leftPad(ReturnDateTime.getDate(),'0',2);
        $("input:hidden#tmpIBDate"+fromtab).val(strNew);
        setDropdowns(xferDirection,fromtab);
    }
}
function setDropdowns(xferDirection,fromtab){
    var varDate = $("input:hidden#tmp"+xferDirection+"Date"+fromtab).val();
    if(varDate.length==10){
        $("select#"+xferDirection+"MonthYear"+fromtab).val(varDate.substr(0,7));
        var arrSelectedDate = $("select#"+xferDirection+"MonthYear"+fromtab).val().split(":");
        if(arrSelectedDate.length==2){
            var objDay = document.getElementById(xferDirection+"Day"+fromtab);
            fnDropDownReduce(objDay,0);
            var daysinmonth = 32 - new Date(arrSelectedDate[0], parseInt(arrSelectedDate[1],10)-1, 32).getDate();
            if (xferDirection=="IB"){
                objDay.options[objDay.options.length] = new Option(" -  ","");
            }
            for(i = 0;i < daysinmonth;i++){
                objDay.options[objDay.options.length] = new Option(leftPad(i+1,'0',2),leftPad(i+1,'0',2));
            }
            if (parseInt(varDate.substr(8,10),10)>daysinmonth){
                $("input:hidden#tmp"+xferDirection+"Date"+fromtab).val(varDate.substr(0,7)+":"+leftPad(daysinmonth,'0',2))
                varDate = $("input:hidden#tmp"+xferDirection+"Date"+fromtab).val();
            }
            $("select#"+xferDirection+"Day"+fromtab).val(varDate.substr(8,10));
        }
    }else{
        $("input:hidden#tmp"+xferDirection+"Date"+fromtab).val("");
        $("select#"+xferDirection+"MonthYear"+fromtab).val("");
        $("select#"+xferDirection+"Day"+fromtab).val("");
        var objDay = document.getElementById(xferDirection+"Day"+fromtab);
        fnDropDownReduce(objDay,1);
    }
}
function SetDropdownOnFlightDatesOff(){
    $(".retdates select.datesdrop").each(function() { $(this).css({"background-color": "#FFFFFF"}); });
    $(".depdates select.datesdrop").each(function() { $(this).css({"background-color": "#FFFFFF"}); });
    $("div.calendarbox").hide();
    $("div.calendarbox2").hide();
}
var SetDropdownOnFlightDatesWhich = "OB";
function SetDropdownOnFlightDates(calendar) {
    fromtab = $("input:hidden#SearchType").val();
    $("input:hidden#tmp"+SetDropdownOnFlightDatesWhich+"Date"+fromtab).val(calendar.date.print("%Y:%m:%d"));
    setDropdowns(SetDropdownOnFlightDatesWhich,fromtab);
    if ($("input:hidden#tmp"+SetDropdownOnFlightDatesWhich+"Date"+fromtab).val()!="") SetDropdownOnFlightDatesWhich = (SetDropdownOnFlightDatesWhich=='IB'?'OB':'IB');
    // Set location here
	$("."+(SetDropdownOnFlightDatesWhich=='IB'?'ret':'dep')+"dates select.datesdrop").each(function() {
		$(this).css({"background-color": "#C6F2FF"});
	});
	$("."+(SetDropdownOnFlightDatesWhich=='IB'?'dep':'ret')+"dates select.datesdrop").each(function() {
		$(this).css({"background-color": "#FFFFFF"});
	});
	$(".cal"+(SetDropdownOnFlightDatesWhich=='IB'?'dep':'ret')+"choose").hide();
	$(".cal"+(SetDropdownOnFlightDatesWhich=='IB'?'ret':'dep')+"choose").show();
    if (SetDropdownOnFlightDatesWhich == "OB"){ SetDropdownOnFlightDatesOff(); }
};

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

var intInitTab = 0;

$(document).ready(function(){
    $(document).pngFix();
    $(document).imagerolloverlinks();
    $(document).inputhelpertext();
	
	$("#flashmovie1").flash({
    	src: "_images/MainBox-Bournemouth.swf",
		width: 410,
    	height: 172,
		wmode: "transparent"
	});
	
	$("#flashmovie2").flash({
    	src: "_images/Teleticket_Holiday_Launch.swf",
		width: 150,
    	height: 689
	});
	
	$("#flashmovie3").flash({
    	src: "_images/Search.swf",
		width: 718,
    	height: 125
	});


    $("table.flightguidetable").each(function(i) { $("#flightguidetext1").hide(); $("#flightguidetext2").hide(); $("#flightguidetext3").hide(); });
	var $tabs = $("#searchbox").tabs({
		selected : intInitTab,
   		select: function(event, ui) {
			//childAgesDropsFO();
			//childAgesDropsPK();
		}
	});
	//$("select").each(function(i){
		//$(this).css({"background-color":"red"});
	//});
	//$("label.departureinfo").click(function(){
	//	$(this).effect("bounce");
	//});
	//$(".messagebox").corner();
	$(".dislaimer").corner("round 5px").parent().css('padding', '1px').corner("round 5px");
	SetNumberOfRooms();
	hotelInfo();
	
	$.validator.addMethod("futuredate", function(value, element, params) { return true;
		var formdate = element.value.replace(/:/g, "/");
		var formdate = new Date(formdate);
		var today = new Date();
		difference = formdate - today;
		days = Math.round(difference/(1000*60*60*24));
		if (days < 0) {
			return false;
		} else {
			return true;
		}
	}, $.format("Ooops, you’ve entered an invalid date!"));
	
	$.validator.addMethod("hasduration", function(value, element, params) {
		var thisdate = element.value.replace(/:/g, "/");
		var thisdate = new Date(thisdate);
		var departuredate = $(element).prev().val().replace(/:/g, "/");
		var departuredate = new Date(departuredate);
		difference2 = thisdate - departuredate;
		days2 = Math.round(difference2/(1000*60*60*24));
		if (days2 < 0) {
			return false;
		} else {
			return true;
		}
	}, $.format("Ooops, you’ve entered an invalid duration!"));


	$("#Form1").validate({
		errorClass: "errorlabel",
		rules: {
            tmpOBDateFO: "futuredate",
            tmpIBDateFO: "hasduration"
			//enq_arr_point: {
      		//	required: function(element) {
        	//		return $("#retMonth").val() != "";
      		//	}
    		//},
			//enq_arr_point2: {
      		//	required: function(element) {
        	//		return $("#retMonth").val() != "";
      		//	}
    		//}
		},
		submitHandler: function(form) {
			if(agentOrDirect=="direct"){
				$("#UserIDFO").val("");
				$("#UserReferenceFO").val("");
				$("#UserPasswordFO").val("");
			}
			form.submit();
		}
	});


	$("#Form2").validate({
		errorClass: "errorlabel",
		rules: {			
            tmpOBDatePK: "futuredate",
            tmpIBDatePK: "hasduration"
			//enq_arr_point: {
      		//	required: function(element) {
        	//		return $("#retMonth").val() != "";
      		//	}
    		//},
			//enq_arr_point2: {
      		//	required: function(element) {
        	//		return $("#retMonth").val() != "";
      		//	}
    		//}
		},
		submitHandler: function(form) {
			if(agentOrDirect=="direct"){
				$("#UserIDPK").val("");
				$("#UserReferencePK").val("");
				$("#UserPasswordPK").val("");
			}
			form.submit();
		}
	});
	setInterval(function () {
		$(".blinkthis").each(function () {
			this.style.visibility = this.style.visibility == 'hidden' ? 'visible' : 'hidden';
		});
	}, 1000);
	childAgesDropsFO();
	childAgesDropsPK();
	$("select#OBpointFO").change(function(){
		childAgesDropsFO();
	});
});


function SetNumberOfRooms(){
		var $roomcount = $("#hol_rooms").val();
		$(".roompax").hide();
		for (i=0;i<=$roomcount;i++){
			$("#roomrow"+i).show();
		}
	$("#hol_rooms").change(function(){
		var $roomcount = $("#hol_rooms").val();
		$(".roompax").hide();
		for (i=0;i<=$roomcount;i++){
			$("#roomrow"+i).show();
		}
	});
}



function childAgesDropDown(j) {
	if ($(j).val() > 0 && ($("select#OBpointFO").val() == "BOH" || ($("#searchbox").tabs('option','selected')==0?"FO":"PK") == "PK")) {
		parentid = $(j).parent().attr("id");
		parentid = parentid.substring(7);
		searchtype = ($("#searchbox").tabs('option','selected')==0?"FO":"PK");
		$(j).siblings(".childages").show();
		$(j).siblings(".childages").children("select").hide();
		$(j).siblings(".childages").children("div").children("label").hide();
		for (i=1;i<=$(j).val();i++) {
			$(".labelchild"+parentid+i).show();
			if (($("#searchbox").tabs('option','selected')==0?"FO":"PK") == "PK") {
				$("#hol_room"+parentid+"_children_age"+i).show();
			} else {
				$("#PaxChildAge"+i+searchtype).show();
				if (i < 5) {
					$(j).siblings(".childagesmultiple").hide();
				} else {
					$(j).siblings(".childagesmultiple").show();
				}
			}
		}
	} else {
		$(j).siblings(".childages").hide();
		$(j).siblings(".childages").children("select").hide();
	}
}

function childAgesDropsFO() {
	$(".childselectorfo").each(function(j){
		childAgesDropDown(this);
		$(this).change(function(){ childAgesDropDown(this); })
	});
}

function childAgesDropsPK() {
	$(".childselectorpk").each(function(j){
		childAgesDropDown(this);
		$(this).change(function(){ childAgesDropDown(this); })
	});
}


///
function addSource() {
	var enq_source = document.Form1.enq_source;
	
	page_name = new String(document.location +"") 	//make sure location is a string.

	if (page_name.indexOf("source=") != -1){
		stem = page_name.substring(page_name.indexOf("source="),page_name.length)
		//alert("stem: " +stem)
		if (stem.indexOf("&") != -1){	// find the end of source variable - either EOL or &
			enq_source.value = stem.substring(7,stem.indexOf("&") )
		}else{
			enq_source.value = stem.substring(7,stem.length)
		}
	}else{
		// use the referrer as the source
		if (document.referrer){
			stem = document.referrer.substring(7,document.referrer.length)
			enq_source.value = stem.substring(0,stem.indexOf("/") )
		}
	}
	// enq_source.value = "prowebtest";
	// alert (enq_source.value)

	/*if (page_name.indexOf("orig=") != -1){
		stem = page_name.substring(page_name.indexOf("orig="),page_name.length)
		if (stem.indexOf("&") != -1){	// find the end of orig variable - either EOL or &
			orig = stem.substring(5,stem.indexOf("&") )
		}else{
			orig = stem.substring(5,stem.length)
		}
		for (var i=0; i<document.Form1.enq_dep_point.options.length; ++i){
			if (document.Form1.enq_dep_point.options[i].value == orig){
				document.Form1.enq_dep_point.options[i].selected = true;
				menu_change();
			}
		}
	}*/
document.Form2.enq_source.value = document.Form1.enq_source.value;
}


function hotelInfo() {
	$(".packageinfotoggle").click(function(){
		if ($(this).attr("src") == "./_images/button-more-info.jpg") {
			$(this).attr({"src" : "./_images/button-less-info.jpg"});
			$(this).siblings(".packageextended").slideDown();
			$(this).siblings(".packageextended").children(".packagedescblock").children(".packagedesctext").jScrollPane({
				showArrows:		true,
				dragMinHeight:	11,
				dragMaxHeight:	11,
				scrollbarWidth:	15
			});
		} else {
			$(this).attr({"src" : "./_images/button-more-info.jpg"});
			$(this).siblings(".packageextended").slideUp();
		}
	});
	
	$(".imgcarousel").each(function() {
		$(this).height($(this).children("a").length * 70+"px");
		//Check to see if scrolling is possible
		$(this).prev("img.imgscrolldown").attr({"src":($(this).children("a").length > 3 ? "./_images/button-down-active.png":"./_images/button-down-inactive.png")});

		//Do the scrolling
		var topmargin = 0;
		
		$(this).parent().children("img.imgscrollup").click(function(){
			if (topmargin >= 0) {
			} else {
				topmargin = topmargin +70;
				$(this).parent().children("div").animate({"top":""+topmargin+"px"}, 500);
				imgscroll(topmargin,$(this).parent().children("div"));
			}
		});
		
		$(this).parent().children("img.imgscrolldown").click(function(){
			if (topmargin <= -$(this).parent().children("div").height() + 210) {
				//alert("Hi");
				//alert($(this).parent().children("div").height());
				imgscroll(topmargin,$(this).parent().children("div"));
			} else {
				topmargin = topmargin -70;
				$(this).parent().children("div").animate({"top":""+topmargin+"px"}, 500);
				//alert($(this).parent().children("div").height());
				imgscroll(topmargin,$(this).parent().children("div"));
			}
		});
	});	
}

function imgscroll(topmargin,obj) {
	//alert(obj);
	if (topmargin <= -obj.height() + 210) {
		obj.parent().children("img.imgscrolldown").attr({"src":"./_images/button-down-inactive.png"});
	} else {
		obj.parent().children("img.imgscrolldown").attr({"src":"./_images/button-down-active.png"});
	}
	
	if (topmargin < 0) {
		obj.parent().children("img.imgscrollup").attr({"src":"./_images/button-up-active.png"});
	} else {
		obj.parent().children("img.imgscrollup").attr({"src":"./_images/button-up-inactive.png"});
	}
}
function calculateSurcharge() {
	var frm = document.form_step4;
	var enq_cc_amount = document.getElementById("enq_cc_amount");
	var cc_fee = document.getElementById("cc_fee");
	var showccamt = document.getElementById("showccamt");
	var cc_type = frm.CCardTypes.options[frm.CCardTypes.selectedIndex].value;
	// var cc_type = frm.enq_cc_type.options[frm.enq_cc_type.selectedIndex].value;
	
	var cc_amount = parseFloat(enq_cc_amount.innerHTML);
	
	
	var cc_charge = 0;
	var CCTotalValue = 0;
	var CCChargeValue = 0;

	if (cc_type == "VIS" || cc_type == "MST" || cc_type == "AMX") {
		cc_charge = 103;
		}
	else if (cc_type == "SWI" || cc_type == "DLT") {
		cc_charge = 100;
		}
	else {
		alert('Invalid Card Type ' + cc_type);
		return;
	}

	if (cc_charge > 0 ) {
		CCTotalValue = String(Math.floor(cc_charge*cc_amount)/100)
		CCChargeValue = (cc_charge-100)*cc_amount/100;
		
		//alert(CCTotalValue +", "+ CCChargeValue);
		CCChargeValue = currency(CCChargeValue);
		CCTotalValue = currency(CCTotalValue);
		//alert(CCTotalValue +", "+ CCChargeValue);

		showccamt.value = CCTotalValue;
		cc_fee.value = CCChargeValue;
	}
}

function currency(anynum) {
   //-- Returns passed number as string in $xxx,xxx.xx format.
   anynum=eval(anynum)
   workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
   if (workStr.indexOf(".")==-1){workStr+=".00"}
   dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
   pStr=workStr.substr(workStr.indexOf("."))
   while (pStr.length<3){pStr+="0"}

   //--- Adds comma in thousands place.
   if (dNum>=1000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
   }

   //-- Adds comma in millions place.
   if (dNum>=1000000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
   }
   retval = dStr + pStr
   //-- Put numbers in parentheses if negative.
   if (anynum<0) {retval="("+retval+")"}
   return retval
}



function superValuePop() {
		alert("This is a Super Value Fare Flight!!\n\nPlease note, that Super value fares have certain terms...\n* Full payment is due straight away\n* Super Value fares are non changeable, non refundable");
}

function OpenJawPop() {
		alert("Look : The outbound and airport airports are different!");
}

function BlackpoolPop() {
		alert("Freebie! Free Car Parking. We're giving you up to 15-days car parking included in this price. Parking is onsite at Blackpool airport.\n\n\u00A310 Don't Forget : Blackpool airport will charge you a per person development fee");
}

function NorwichPop() {
		alert("Airport Development Fee (ADF). Norwich charge the following ADF at check-in:\nAdults-5GBPpp\nChildren (2-15yrs)-1GBPpp\nInfants (under 2)-Free");
}