/* 
  Function: getArgs v1.0 Anders Lönnberg

  Get the querystring as an object
  Example:
 
  URL:    ?anders=good&jonas=hangover

  CODE:   var my_args = getArgs();
          alert(my_args.anders);
	  alert(typeof(args.anders) == 'undefined');

  OUTPUT: "good"
          "false"
  
*/
function getArgs() 
{
   var args = new Object(); 
   var query = location.search.substring(1); 
   var pairs = query.split("&"); 

   for(var i = 0; i < pairs.length; i++) 
   { 
     var pos = pairs[i].indexOf('='); 
     if (pos == -1) 
       continue; 
     
     var argname = pairs[i].substring(0,pos); 
     var value = pairs[i].substring(pos+1); 
     args[argname] = unescape(value); 
   } 

   return args; 
}

function g(elementID) {
	return document.getElementById(elementID);
}

function PopUpWindow(href_url,win_name,prop) {
	var thewin = window.open(href_url,win_name,prop);
	thewin.focus();	
}

function CheckNewsletterEmail(def) {
	var email = g('email_address');
	if (email.value == '') {
		email.value = def;
	}
}

function ResetNewsletterValue(def) {
	var email = g('email_address');
	if (email.value == def) {
		email.value = '';
	}
}

function InputContentSwitcher(obj,defaultText,dir) {
	if (dir == 'blur') {
		if (obj.value.length == 0) {
			obj.value = defaultText
		}
	} else if (dir == 'focus') {
		if (obj.value == defaultText) {
			obj.value = '';
		}
	}
}

function controllValues(formobj) {
	if ((formobj.dh_dep_id.value == '') && (formobj.dep_name.value == '')) {
  		alert('Du mangler at angive afrejse by!');
  		return false;
	} else if ((formobj.dh_dest_id.value == '') && (formobj.dest_name.value == '')) {
  		alert('Du mangler at angive destination!')
  		return false;
	} else {
  		return true;
	}
}

function changeSearchMethod(radio) {
	var searchMethod = radio.value;
	var dflexible = g('Dates_Flexible');
	var dfixed = g('Dates_Fixed');

	// HIDE ALL:
	dflexible.style.display = 'none';
	dfixed.style.display = 'none';

	if (searchMethod == 'date') {
		dfixed.style.display = 'block';
	} else if (searchMethod == 'flexible_dates') {
		dflexible.style.display = 'block';
	}
}

function enableInputTags(node,enable) {
	for (var i=0; i < node.childNodes.length; i++) {
		if (node.childNodes[i].tagName == 'INPUT' || node.childNodes[i].tagName == 'SELECT') {
			node.childNodes[i].disabled = !enable;
		} else {
			enableInputTags(node.childNodes[i],enable);
		}
	}
}

function ChangePaymentMethod(sel) {
	HideAllPaymentInfo();
	if (sel == 'card_Visa' || sel == 'card_Diners' || sel == 'card_Eurocard' || sel == 'card_Mastercard') {
		ShowCCInfo();
		g('cc_cvv').style.display = 'block';
	} else if (sel == 'card_Amex') {
		ShowCCInfo();
	}
}

function HideAllPaymentInfo() {
	g('lbl_cc').style.display = 'none';
	g('cc').style.display = 'none';
	g('lbl_cc_valid_month').style.display = 'none';
	g('cc_valid_month').style.display = 'none';
	g('cc_valid_year').style.display = 'none';
	g('lbl_cc_cvv').style.display = 'none';
	g('cc_cvv').style.display = 'none';
	g('imgCreditCardLogos').style.display = 'none';
	g('imgCreditCard').style.display = 'none';
}

function ShowCCInfo() {
	g('lbl_cc').style.display = 'block';
	g('cc').style.display = 'block';
	g('lbl_cc_valid_month').style.display = 'block';
	g('cc_valid_month').style.display = 'block';
	g('cc_valid_year').style.display = 'block';
	g('lbl_cc_cvv').style.display = 'block';
	g('imgCreditCardLogos').style.display = 'block';
	g('imgCreditCard').style.display = 'block';
}

function ChangeSearchType(itemType) {
	// SET FOCUS ON SUBMIT BUTTON:
	g('SearchSubmitImage').focus();

	ResetAllSearchTypes();
	g(itemType).className = 'ItemSelected';

	if (itemType == 'SearchPackets') {
		SetSearchFormValues('5454','/ld/flight/cgi-bin/pre_search.cgi','');
		SetTexts('Vælg destination','Søg Pakkerejser');
		SetComboID(itemType,0);
		g('FrontPageSearchHeading').innerHTML = '';
		g('FrontPagePackets').style.display = 'block';
		g('FrontPageDates').style.display = 'block';
		g('FrontPageTravellers').style.display = 'block';
		g('section').value = 'product';
		g('dest_name').value = '... eller skriv rejsemål';

	} else if (itemType == 'SearchFlights') {
		SetSearchFormValues('5522','/ld/flight/cgi-bin/pre_search.cgi','');
		SetTexts('Vælg destination','Søg Fly');
		SetComboID(itemType,1);
		g('FrontPagePackets').style.display = 'block';
		g('section').value = 'flight';
		g('FrontPageDates').style.display = 'block';
		g('FrontPageTravellers').style.display = 'block';
		g('dest_name').value = '... eller skriv rejsemål';

	} else if (itemType == 'SearchHotels') {
		SetSearchFormValues('5523','/ld/hotel/cgi-bin/pre_search.cgi','');
		SetTexts('Rejsemål','Søg Hotel');
		SetComboID(itemType,2);
		g('section').value = 'hotel';
		g('dh_dep_id').style.display = 'none';
		g('FrontPagePackets').style.display = 'block';
		g('FrontPageDates').style.display = 'block';
		g('FrontPageTravellers').style.display = 'block';
		g('dest_name').value = '... eller skriv rejsemål';

	} else if (itemType == 'SearchCars') {
		SetSearchFormValues('5524','/ld/rental_car/cgi-bin/pre_search.cgi','');
		SetTexts('Rejsemål','Søg Billeje');
		SetComboID(itemType,3);
		g('section').value = 'rental_car';
		g('dh_dep_id').style.display = 'none';
		g('FrontPageDates').style.display = 'block';
		g('FrontPagePackets').style.display = 'block';
		g('start_time').style.display = 'block';
		g('end_time').style.display = 'block';
		g('dest_name').value = '... eller skriv rejsemål';

	} else if (itemType == 'SearchEvents') {
		SetSearchFormValues('5525','/ld/rental_car/cgi-bin/pre_search.cgi','');
		SetComboID(itemType,4);
		SetTexts('','Søg Events');
		g('FrontPageEvents').style.display = 'block';
		g('dest_name').value = '';
	}

	SwitchDestinationSelectNames(itemType);
}

function ResetAllSearchTypes() {
	// RESET BUTTONS:
	g('SearchPackets').className = '';
	g('SearchFlights').className = '';
	g('SearchHotels').className = '';
	g('SearchCars').className = '';
	g('SearchEvents').className = '';

	// HIDE TIME SELECT BOXES:
	g('start_time').style.display = 'none';
	g('end_time').style.display = 'none';

	// RESET FIELDSETS:
	g('combo_id').style.display = 'block';
	g('dh_dep_id').style.display = 'block';
	g('FrontPagePackets').style.display = 'none';
	g('FrontPageDates').style.display = 'none';
	g('FrontPageTravellers').style.display = 'none';
	g('FrontPageEvents').style.display = 'none';
}

function SetTexts(destFirstText,heading) {
	g('dh_dest_id').options[0].text = destFirstText;
	g('FrontPageSearchHeading').innerHTML = heading;
}

function SetSearchFormValues(clickID,nextPage,comboID) {
	g('click_id').value = clickID;
	g('combo_id').value = comboID;
	g('next_page').value = nextPage;
}

function SetComboID(comboType,comboID) {
	var list = g('combo_id');	
	list.options.length = 0;

	if (comboType == 'SearchPackets') {
		list.options[0] = new Option('Fly + hotel','10');
		list.options[1] = new Option('Fly + billeje','11');
		list.options[2] = new Option('Fly + billeje + hotel','20');
		list.style.display = 'block';
	} else {
		list.options[0] = new Option('',comboID);
		list.style.display = 'none';
	}
}

function SwitchDestinationSelectNames(searchType) {
	if (searchType == 'SearchEvents') {
		g('dh_dest_id_events').attributes['name'].nodeValue = 'dh_dest_id';
		g('dh_dest_id').attributes['name'].nodeValue = 'dh_dest_id_notused';
	} else {
		g('dh_dest_id_events').attributes['name'].nodeValue = 'dh_dest_id_notused';
		g('dh_dest_id').attributes['name'].nodeValue = 'dh_dest_id';
	}
}



/*	CALENDAR SCRIPTS:
	----------------------------------------------------------------------------  */
var month_days = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
var isNav = (navigator.appName == "Netscape");
var location_ref = location.href; 
	
function get_dd_month_year_day(calendar) {
   //var ym_drop = eval('document.dates.' + calendar + '_year_month');
   var ym_drop = g(calendar + '_year_month');
   year_month = ym_drop.options[ym_drop.selectedIndex].value;

   var year = year_month.substr(0,4);
   var month = year_month.substr(5);

   //var day_drop = eval('document.dates.'+calendar+'_day');
   //var day = day_drop.value;
   var day = g(calendar + '_day').value;

   return new Array(year,month,day);
}

function leapyear(y) {
    return (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0));
}

function go_to_month(step,calendar) {
	var y_m = get_dd_month_year_day(calendar);

	var d = y_m[2];
	var limit = '';   

	if (calendar == 'end') {
		var start_month = get_dd_month_year_day('start');
		limit = start_month[0] + '-' + start_month[1];
	}  

	var new_y_m = get_next_valid_month(y_m, step, limit);

	if (new_y_m == '') {
		if (step == 0 && calendar == 'end') {
			set_month(limit,'end',limit);
	    }
		return;
	}

	var new_y_m_arr =  new_y_m.split('-');

	if (d > month_days[Number(new_y_m_arr[1])]) {
		d = month_days[Number(new_y_m_arr[1])];
	} 

	if (calendar == 'start') {
		set_month(new_y_m + '-' + d,'start','');

		var end_month = get_dd_month_year_day('end'); 
		if (end_month[0] < new_y_m_arr[0] || (end_month[0] == new_y_m_arr[0] && end_month[1] < new_y_m_arr[1])) {
			set_month(new_y_m + '-' + d,'end',new_y_m);
		}
	} else {
		set_month(new_y_m + '-' + d,'end',limit);
	}
}

function find_and_set_value(dd,my_value) {
	for (var i = 0; i < dd.options.length; i++) {
		if (dd.options[i].value == my_value) {
			dd.selectedIndex = i;
			break;
		}
	}
}

function set_month(new_y_m,calendar,first_limit) {
	var new_y_m_arr =  new_y_m.split('-');
	var date_year = new_y_m_arr[0] + '-' + new_y_m_arr[1];
	var day = new_y_m_arr[2];

	// Enable 'pointers'
	g(calendar + '_next_month').style.cursor = get_next_valid_month(new_y_m_arr,1,first_limit) == '' ? 'pointer' : 'hand';
	g(calendar + '_prev_month').style.cursor = get_next_valid_month(new_y_m_arr,-1,first_limit) == '' ? 'pointer' : 'hand';

	//var ym_drop = eval('document.dates.'+calendar+'_year_month');
	var ym_drop = g(calendar + '_year_month');
	find_and_set_value(ym_drop, date_year);

	//var d_drop = eval('document.dates.'+calendar+'_day');
	var d_drop = g(calendar + '_day');
	find_and_set_value(d_drop,day);
	update_calendar_image(calendar);
}

function get_next_valid_month(y_m, step, first_limit) {
	var m = Number(y_m[1]) + step;
	var y = Number(y_m[0]);

	if (m > 12) {
		m = 1;
		y = y+1;
	}

	if (m < 1) {
		m = 12;
		y = y-1;
	}

	if (m < 10) {
			m = '0' + m;
	}

	var new_y_m = y+'-'+m;

	if (first_limit == '') {
		first_limit = first_month;
	}

	if (first_limit != '' && new_y_m < first_limit) {
		return '';
	}

	if (last_month != '' && new_y_m > last_month) {
		return '';
	}

	return new_y_m;
}

function update_day_by_click(e) {	   
	var hit = get_xy_and_target(e);
	var new_day = get_day_by_click(hit[0], hit[1], hit[2]);
	if (new_day > 0) {
		// var day_drop = eval('document.dates.'+hit[0]+'_day');
		var day_drop = g(calendar + '_day');
		day_drop.selectedIndex = new_day - 1;

		update_calendar_image(hit[0]);
	}
}

function get_day_by_click(calendar,x,y) {
	var row = parseInt(y / 14);
	var col = parseInt(x / 18);

	var ym = get_dd_month_year_day(calendar);
	var currdate = new Date(ym[0], Number(ym[1]) - 1, 1);

	var new_day = col + row * 7 - ((currdate.getUTCDay())) + 1;

	ym[2] = new_day;
	return valid_day(ym);
}

function valid_day(ymd) {
	var the_day = Number(ymd[2]);
	if (the_day < 1) {
		return -1;
	}

	var month = Number(ymd[1]);
	var days  = month_days[month];

	if (month == 2 && leapyear(Number(ymd[0]))) {
		days++;
	}

	if (the_day > days) {
		return -1;
	}

	return the_day;
}

function get_a_valid_day(ymd) {
	var the_day = Number(ymd[2]);
	if (the_day < 1) {
		return '01';
	}

	var month = Number(ymd[1]);
	var days  = month_days[month];

	if (month == 2 && leapyear(Number(ymd[0]))) {
		days++;
	}

	if (the_day > days) {
		return days;
	}

	return the_day > 9 ? the_day : '0'+the_day;
}

function update_calendar_image(calendar) {
	var im = g(calendar + '_cal_image');  
	var ym = get_dd_month_year_day(calendar);

	im.src = cal_url + '&year=' + ym[0] + '&month=' + ym[1] + '&blue_date=' + ym[2];
	im.style.display = '';
}

function get_xy_and_target(e) {
	var x,y,targ,calendar;

	if(isNav) {  
		targ = e.target;
		calendar = targ.id.substr(0, targ.id.indexOf('_'));
		var cal_image = g(calendar + '_cal_image');  
		x = e.layerX - cal_image.x - 1;
		y = e.layerY - cal_image.y - 35;
	} else {
		E = event;
		x = event.offsetX - 1;
		y = event.offsetY - 37;     
		targ = event.srcElement;
		calendar = targ.id.substr(0, targ.id.indexOf('_'));
	}

	return new Array(calendar,x,y,E,targ);
}

function get_mouse_over(e) {
	var hit = get_xy_and_target(e);
	var the_day = get_day_by_click(hit[0],hit[1],hit[2]);

	var targ = hit[4];

	targ.style.cursor = the_day > 0 ? 'hand' : 'pointer';
	targ.disabled = the_day < 1;
	targ.onclick = the_day > 0 ? update_day_by_click : '';
}



/*	STRIP TOP, LEFT AND BOTTOM OFF CONTENT:
	----------------------------------------------------------------------------  */
function ShrinkContent(place) {
	var wwidth = GetWindowWidth();

	if (parseInt(wwidth) > 700) {
		return;	
	}

	if (place == 1) {
		g('SkyScraper').style.display = 'none';
		g('Logo').style.display = 'none';
		g('Menu').style.display = 'none';
		g('SubMenu').style.display = 'none';
		g('LeftColumn').style.display = 'none';
		g('MainContentHolder').style.width = '620px';
		g('Wrapper').style.width = '620px';
	} else if (place == 2) {
		g('FooterLinks').style.display = 'none';
		g('FooterFlags').style.display = 'none';
		g('FooterLogos').style.display = 'none';
		g('FooterCountries').style.display = 'none';
	}
}

function GetWindowWidth() {
	var winWidth = 0;

	if (self.innerWidth) {
		winWidth = self.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		winWidth = document.documentElement.clientWidth;
	} else if (document.body) {
		winWidth = document.body.clientWidth;
	}

	return winWidth;
}



function insert_city_id_selections(SelectedValue,cityvalue) {
    var city_option;
    var TempArray = new Array();

    // Insert array build above
    try { TempArray = eval(SelectedValue); }
    catch(e){};
    
    city_option = document.dates.dh_dest_id;
    city_option.options.length = TempArray.length == 0 || SelectedValue == 0 ? 1 : TempArray.length;

    pre_country = TempArray.length > 0 ? SelectedValue : 0;

    // -----------------------------------------------
    // Fill dropdown with selected array built above
    // -----------------------------------------------

    // Loop over each value in the temp array
    var selIndex = 0;
    for(var index = 0; index < TempArray.length; index++)
    {
      // split value into a text and a value-part
      var text_part = TempArray[index].substr(0, TempArray[index].indexOf(":"));
      var value_part = TempArray[index].substr(TempArray[index].indexOf(":")+1,TempArray[index].length);
      
      // Insert into menu
      city_option.options[index].text = text_part;
      city_option.options[index].value = value_part;

      // Preselect
	  if (value_part==cityvalue && selIndex == 0){
	     selIndex = index;
	  }	  
    }

    city_option.disabled = TempArray.length == 0 || SelectedValue == 0;
    city_option.selectedIndex = city_option.length == 2 ? 1 : selIndex;

    insert_airport_id_selections(city_option.value, '');
}

function insert_airport_id_selections(SelectedValue,airportvalue) {
    var airport_option;
    var TempArray = new Array();

    // Insert array build above
    try { TempArray = eval(SelectedValue); }
    catch(e){};

    airport_option = document.dates.iata_code;
    airport_option.options.length = TempArray.length == 0 || SelectedValue == 0 ? 1 : TempArray.length;

    pre_city = TempArray.length > 0 ? SelectedValue : 0;

    // -----------------------------------------------
    // Fill dropdown with selected array built above
    // -----------------------------------------------

    // Loop over each value in the temp array
    var selIndex = 0;
    for(var index = 0; index < TempArray.length; index++)
    {
      // split value into a text and a value-part
      var text_part  = TempArray[index].substr(0, TempArray[index].indexOf(":"));
      var value_part = TempArray[index].substr(TempArray[index].indexOf(":")+1, TempArray[index].length);

      // Insert into menu
      airport_option.options[index].text = text_part;
      airport_option.options[index].value = value_part;

      // Preselect
      if (value_part==airportvalue && selIndex == 0){
          selIndex = index;
      }	  
    }
    airport_option.disabled = TempArray.length == 0 || SelectedValue == 0;
    airport_option.selectedIndex = airport_option.length == 2 ? 1 : selIndex;
}

function redir_to_partner_page(this_filename, this_site)
{
	if(location.href.indexOf('next_page_partner.cgi') < 0)
	{
	// Get this part: /ls/customer_service/faq/?partner=sterling#Flight_link	
	    var url=location.href.substr(location.href.indexOf('/'+this_site));
	
	// Split into baseurl+querystring   and   anchor
		var split_anchor = url.split('#');  // split_anchor[0] = base+querystr, split_anchor[1] = anchor
	
	// split into baseurl + querystring	
		var parts=split_anchor[0].split('?');	// parts[0] = base_url, parts[1] = querystring
		
		// Is the path to the script a specified file or just the dir?
		var new_url = '/'+this_site+'/common/cgi-bin/next_page_partner.cgi?template=/www/docs'+parts[0];
		if(parts[0].lastIndexOf(this_filename) < parts[0].length - this_filename.length)
		{
		    new_url += '/'+this_filename;
		}
		
		// add querystring if we got any
		if(parts.length > 1)
		{
			new_url += '&'+parts[1];
		}
		// add anchor if any
		if(split_anchor.length > 1)
		{
		      new_url += '#'+split_anchor[1];
	 	}
		location.href = new_url;
	}
}


// FIRST PAGE CANNONBALL =============================================================
function checkTravelType(selected) {
	if ( selected == "1" ) {
		document.getElementById('FrontPageSearchHeading').style.display = "inline";
		document.getElementById('FrontPageSearchHeadingHotel').style.display = "none";
		document.getElementById('destination').style.display = "inline";
		document.getElementById('destinationHotel').style.display = "none";
		document.getElementById('startDayLabel').style.display = "inline";
		document.getElementById('startDayLabelHotel').style.display = "none";
		document.getElementById('endDayLabel').style.display = "inline";
		document.getElementById('endDayLabelHotel').style.display = "none";		
		document.getElementById('dh_dest_id').disabled = false;
		document.getElementById('dh_dest_idHotel').disabled = true;
		document.getElementById('dates').action = "/ld/destination/cgi-bin/freetext_search.cgi";
		document.getElementById('noOfRooms').style.display = "none";		
	}
	else if ((selected == "2")) {
		document.getElementById('FrontPageSearchHeading').style.display = "none";
		document.getElementById('FrontPageSearchHeadingHotel').style.display = "inline";
		document.getElementById('destination').style.display = "none";
		document.getElementById('destinationHotel').style.display = "inline";
		document.getElementById('startDayLabel').style.display = "none";
		document.getElementById('startDayLabelHotel').style.display = "inline";
		document.getElementById('endDayLabel').style.display = "none";
		document.getElementById('endDayLabelHotel').style.display = "inline";
		document.getElementById('dh_dest_id').disabled = true;
		document.getElementById('dh_dest_idHotel').disabled = false;
		document.getElementById('dates').action = "/ld/hotel/cgi-bin/pre_search.cgi";
		document.getElementById('noOfRooms').style.display = "inline";
	}
	else if ((selected == "10") || (selected == "11") || (selected == "20")) {
		document.getElementById('FrontPageSearchHeading').style.display = "inline";
		document.getElementById('FrontPageSearchHeadingHotel').style.display = "none";
		document.getElementById('destination').style.display = "inline";
		document.getElementById('destinationHotel').style.display = "none";
		document.getElementById('startDayLabel').style.display = "inline";
		document.getElementById('startDayLabelHotel').style.display = "none";
		document.getElementById('endDayLabel').style.display = "inline";
		document.getElementById('endDayLabelHotel').style.display = "none";
		document.getElementById('dh_dest_id').disabled = false;
		document.getElementById('dh_dest_idHotel').disabled = true;		
		document.getElementById('dates').action = "/ld/destination/cgi-bin/freetext_search.cgi";

		// dont show if flight + rental car
		if (selected == "11") {
			document.getElementById('noOfRooms').style.display = "none";
		} else {
			document.getElementById('noOfRooms').style.display = "inline";
		}
	}
}
// FIRST PAGE CANNONBALL =============================================================


// STERLING SITE =============================================================
function rowlink(theUrl)
  {
    document.location.href = theUrl;
  }
function tableruler()
{
	if (document.getElementById && document.createTextNode)
	{
		var tables=document.getElementsByTagName('table');
		for (var i=0;i<tables.length;i++)
		{
			if(tables[i].className=='offers')
			{
				var trs=tables[i].getElementsByTagName('tr');
				for(var j=0;j<trs.length;j++)
				{
					if(trs[j].parentNode.nodeName=='TBODY')
					{
						trs[j].onmouseover=function(){this.className='over';return false}
						trs[j].onmouseout=function(){this.className='';return false}
					}
				}
			}
		}
	}
}
// STERLING SITE =============================================================