var dobArray = new Array();
var dobCounter = 0;
var loopLimit = 0;
var currencySelector = 'false';
function loadNewLanguage(newLanguage){
    
    var szURL = location.pathname
    var href = szURL + "?language=" + newLanguage;
    return href;
}
// bug 33226 specific code
// helper function for the autoFill function
function daysInMonth(month,year) 
{  
	var dd = new Date(year, month, 0);
	return dd.getDate();
} 
// only execute this code when adding a cross sell hotel from the passenger details page 
// this function autofills the childrens ages for a hotel extra search, if the extra search
// initiated on the passenger details page and the user had already added some passenger information 
// i.e. childrens date of birth
function autoFill(loopLimit, roomNumber, totalChildCount, start)
{
	//limit the amount of times this code will execute
	if(loopLimit <= totalChildCount)
	{	
		var age;
		var today = new Date(); 
		var i = 1;		
		for(i=1; i <= dobArray.length-1; i++)//&& i == start; i++)
		{			
			//one take the date of birth of one child at a time so match the index of the array with the id of the dropdown to autopopulate
			if(i == start)
			{
				// do the calculations and get a age
				var d = dobArray[i];				
				d = d.split("-");
				var byr = parseInt(d[0]); 
				var nowyear = today.getFullYear();				
				var bmth = parseInt(d[1],10)-1;   // radix 10!				
				var bdy = parseInt(d[2],10);   // radix 10!
				var dim = daysInMonth(bmth+1,byr);
				age = nowyear - byr;
				var nowmonth = today.getMonth();
				var nowday = today.getDate();
				if (bmth > nowmonth) {age = age - 1}  // next birthday not yet reached
				else if (bmth == nowmonth && nowday < bdy) {age = age - 1}

				// make age a string, and append the age
				age = age+'';				
				form['Search/HotelInformation/RoomInformation$'+roomNumber+'$/ChildAges/childAge$'+start+'$'].value = age;	
			}
		}
	}
}
// end bug 33226 specific code


function toggleSMSNotify(iindex){

	if (document.getElementById("SMSNotify"+iindex).checked) {
		document.getElementById("PhoneUseType"+iindex).value = "3";
		document.getElementById("PhoneTechType"+iindex).value = "1";
		
	}
	else{	
		document.getElementById("PhoneUseType"+iindex).value = "5";
		document.getElementById("PhoneTechType"+iindex).value = "5";
	}
}

// Unbind the mouseover and mouseout events for spans with rel=tooltip
function unbindTooltipEvents() {
	OJ('span[rel=tooltip]').unbind('mouseover');
	OJ('span[rel=tooltip]').unbind('mouseout');
}	

// this function shows the currency id popup for the google map
function displayPopup(divId)
{
	document.getElementById(divId).style.display = "block";	
}
// this function hides the currency id popup for the google map
function hidePopup(divId)
{	
	document.getElementById(divId).style.display = "none";	
}

// Bind the mouseover and mouseout events for spans with rel=tooltip
function bindTooltipEvents() {
	//Select all anchor tag with rel set to tooltip
		
	var currencyTemplateID = "";
	OJ('span[rel=tooltip]').mouseover(function(e) {	
	
		//Grab the title attribute's value and assign it to a variable
		currencyTemplateID = OJ(this).attr('title');		
			
		//Remove the title attribute's to avoid the native tooltip from the browser
		OJ(this).attr('title','');
			
		//Append the tooltip template and its value				
		document.getElementById(currencyTemplateID).style.display = "block";
		
		//Set the X and Y axis of the tooltip
		OJ('#'+currencyTemplateID).css('top', e.pageY + 10 );
		OJ('#'+currencyTemplateID).css('left', e.pageX - 30 );			
					
	}).mouseout(function() {
		
		//add the title element back to the price element
		OJ(this).attr('title',currencyTemplateID);
		document.getElementById(currencyTemplateID).style.display = "none";	
	});
}

//hack to fix the currency drop down in ie, and when resizing 
function currencyDropdown(){
	
	OJ('#currencyDiv .dd-list .list-outer input').removeAttr("width");	
	OJ('#vall_input').css("width", "150px");
	var hoverDiv = OJ("#holder").width()-18;	
	OJ('#currencyDiv .dd-list .list-outer').css("width", hoverDiv+"px");			
	
	OJ(window).bind("resize", resizeWindow);
	function resizeWindow( e ) {		
		OJ('#vall_input').css("width", "150px");
		var hover = OJ("#holder").width()-18;				
		OJ('#currencyDiv .dd-list .list-outer').css("width", hover+"px");	
	}
}

OJ(document).ready(function()
{
	//style the currency selection box
	OJ('#vall').selectbox();	

	
	bindTooltipEvents();
	
	//check what page we are on searchresult pages and fix the dropdown
	var urlPath = location.pathname;
	if( currencySelector == 'true'){
		if(urlPath.indexOf("/air/searchResults.do") != -1 || urlPath.indexOf("/hotels/") != -1 || urlPath.indexOf("/package/searchResults.do") != -1 )
		{			
			currencyDropdown();
		}
	}
			
	OJ("label.checkbox").click(function(){
		var chb = OJ(this).attr("for");
		chb = "#" + chb;
		if ( OJ(this).parent().find("input:checked").length == 0 ) {
			OJ(this).parent().find(".check").css("background", "url(images/form/checkbox_18x18_checked.gif)");
		} else {
			OJ(this).parent().find(".check").css("background", "url(images/form/checkbox_18x18.gif)");
		}
	});
	
	OJ("span.checkbox").each(function(){
		if( OJ(this).find("input:checked").length > 0 ) {
			
			OJ(this).addClass("checked");
		}
	});
	
	OJ("span.checkbox a").click(function(){
		console.log(OJ(this).parent().find("input:checked").length);
		if( OJ(this).parent().find("input:checked").length == 0 ) {
			OJ(this).parent().addClass("checked");
			OJ(this).parent().find("input").attr("checked", "checked");
		} else {
			OJ(this).parent().removeClass("checked");
			OJ(this).parent().find("input").attr("checked", "");
		}
		return false;
	});
	
	OJ("div.login a").click(function(){
		OJ(OJ(this).attr("href")).toggle();
	});
	OJ("div.register a").click(function(){
		OJ(OJ(this).attr("href")).toggle();
	});
	
	OJ("div.contacts ul a").click(function(){
		OJ("div.contacts div.block").hide();
	});
	
	OJ("div.contacts li a").click(function(){
		OJ("div.contacts div.block").hide();
		OJ("div.contacts li").removeClass("active");
		OJ(this).parent().addClass("active");
		OJ(OJ(this).attr("href")).show();
		
		return false;
	});
	
	OJ("#additional_options_toggle").click(function(){
		toggleMoreOptions();
	});
	
	OJ("#w-service-selection").mouseleave(function(){
		OJ("#panelServiceSelection").hide();
	});
	
	OJ("#foreignOfficialPopup").mouseleave(function(){
		OJ("#officialPopup").hide();
	});
});

function sortBy(submitOption){
	document.sortFilterSearchResults['sortByList'].value = submitOption;
	document.sortFilterSearchResults.submit();
}

function toggleConnections(itineraryPosition,legPosition,connectionPosition){
	if(document.getElementById("connection_"+itineraryPosition+"_"+legPosition+"_"+connectionPosition).style.display == "none")
	{
		document.getElementById("connection_"+itineraryPosition+"_"+legPosition+"_"+connectionPosition).style.display = "block";
		document.getElementById("link_"+itineraryPosition+"_"+legPosition+"_"+connectionPosition).className ="toggle-link-open";
	}
	else{
		document.getElementById("connection_"+itineraryPosition+"_"+legPosition+"_"+connectionPosition).style.display = "none";
		document.getElementById("link_"+itineraryPosition+"_"+legPosition+"_"+connectionPosition).className ="toggle-link";
	}
}

function autoTab(element, nextElement){
    if (element.value.length == element.maxLength && nextElement != null) {
        element.form.elements[nextElement].focus();
    }
}

var cX = 0; var cY = 0; var rX = 0; var rY = 0;
function UpdateCursorPosition(e){ 
	cX = e.pageX; cY = e.pageY;
}
function UpdateCursorPositionDocAll(e){ 
	cX = event.clientX; cY = event.clientY;
}

if(document.all) { document.onmousemove = UpdateCursorPositionDocAll; }
else { document.onmousemove = UpdateCursorPosition; }

function positionPopup(popupID) {
	if(self.pageYOffset) {
		rX = self.pageXOffset;
		rY = self.pageYOffset;
	}
	else if(document.documentElement && document.documentElement.scrollTop) {
		rX = document.documentElement.scrollLeft;
		rY = document.documentElement.scrollTop;
	}
	else if(document.body) {
		rX = document.body.scrollLeft;
		rY = document.body.scrollTop;
	}
	if(document.all) {
		cX += rX; 
		cY += rY;
	}
	popupID.style.left = (cX+10) + "px";
	popupID.style.top = (cY+10) + "px";
}

function toggleFloatingPopup(popupID) {
	if(popupID.length < 1) { 
		return;
	}
	var element = document.getElementById(popupID);
	positionPopup(element);
	if(element.style.display == "none") { 
		element.style.display = "block"; 
	}
	else { 
		element.style.display = "none"; 
	}
}



