// ******************** CLASSIC/ONEBOX  ************************************ //
function switch_mode(mode) {
    url = "widgets.php?mode=" + mode;
    var request;
    if (window.XMLHttpRequest) { // Mozilla, etc
	request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
	request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (!request) { /* request failed, use regular refresh */
	/* request failed, use regular refresh */
	document.location.reload(true);
	return false;
    }
    request.open('GET', url, true);
    request.send(null);
    
    var classic_box = document.getElementById('classic_box');
    var onebox_box = document.getElementById('onebox_box');
    
    if (mode == 'classic') {
	classic_box.className = 'show';
	onebox_box.className = 'hidden';
    } else if (mode == 'onebox') {
	classic_box.className = 'hidden';
	onebox_box.className = 'show';
    }

    var error_box = document.getElementById('errors');
    if (error_box) {
	error_box.className = 'hidden';
    }
}


// ******************** CALENDAR ******************************************* //
// ----- popup calendar -------------------------------------------------------
var popup_dragging = false;
var popup_target;
var popup_mouseX;
var popup_mouseY;
var popup_mouseposX;
var popup_mouseposY;
var popup_oldfunction;

// ----- getObj --------------------------------------------------------------
function getObj(objID) {
    if (document.getElementById) {return document.getElementById(objID);}
    else if (document.all) {return document.all[objID];}
    else if (document.layers) {return document.layers[objID];}
}


// ----- popup_display -------------------------------------------------------
function popup_display(x)
{
  var win = window.open();
  for (var i in x) win.document.write(i+' = '+x[i]+'<br>');
}

// ----- popup_exit ----------------------------------------------------------
function popup_exit(e)
{
  var ie      = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById('popup');

  //popup_mouseup(e);
  element.style.visibility = 'hidden';
  element.style.display    = 'none';
}


// ----- popup_show ----------------------------------------------------------
function popup_show(id, drag_id, exit_id, position, x, y, position_id, form_id, cal_header)
{
  element      = document.getElementById(id);
  drag_element = document.getElementById(drag_id);
  exit_element = document.getElementById(exit_id);

  element.style.position   = "absolute";
  element.style.visibility = "visible";
  element.style.display    = "block";

  new Draggable(id);

  var position_element = document.getElementById(position_id);
  
  for (var p = position_element; p; p = p.offsetParent) {
    if (p.style.position != 'absolute'){
	x += p.offsetLeft; 
        y += p.offsetTop ;
    }
  }

  x += position_element.clientWidth;
  
  if (page_type == 's') {
      if (BrowserDetect.browser == "Firefox") {
	  x -= 380;
      } else if (BrowserDetect.browser == "Explorer") {
	  x -= 350;
      } 
  }
  y += 20;
  element.style.left = x+'px';
  element.style.top  = y+'px';

  drag_element['target']   = id;
  //exit_element.onclick     = popup_exit;
  
  
  today = new Date();
  year = today.getFullYear();
  current_month = today.getMonth();
  month_id = position_id + 'Month';
  var month_index = document.getElementById(month_id).selectedIndex;
  month = document.getElementById(month_id).options[month_index].value;
  
  if(current_month > month) {
      year = year+1;
  }
  open_cal(year, month, position_id, form_id, cal_header);
}


// ----- popup_mousepos --------------------------------------------------------
function popup_mousepos(e)
{
  var ie = navigator.appName == "Microsoft Internet Explorer";

  popup_mouseposX = ie ? window.event.clientX : e.clientX;
  popup_mouseposY = ie ? window.event.clientY : e.clientY;
}


// ******************** AIRPORT LOCATOR *********************************** //
// ----- airport_locator_exit ----------------------------------------------

function airport_locator_exit(exit_id)
{
  var ie      = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById('exit_id');

  element.style.visibility = 'hidden';
  element.style.display    = 'none';
}

// ------ airport lookup -------------------------------------------------- //
function airport_fill_in (id, drag_id, exit_id,  position, x, y, position_id, form_id, field)
{
  element      = document.getElementById(id);
  drag_element = document.getElementById(drag_id);
  exit_element = document.getElementById(exit_id);

  element.style.position   = "absolute";
  element.style.visibility = "visible";
  element.style.display    = "block";

  
  var position_element = document.getElementById(position_id);

  for (var p = position_element; p; p = p.offsetParent)
    if (p.style.position != 'absolute'){
        x += p.offsetLeft;
        y += p.offsetTop ;
    }

    x += position_element.clientWidth;
    
    element.style.left = x+'px';
    element.style.top  = y+'px';

  drag_element['target']   = id;
  //exit_element.onclick     = popup_exit(exit_id);

  url = "airport_locator_new.php?form_name=" + form_id + "&field=" + field;
  wpos = getAnchorWindowPosition(form_id);
  wpos.x -= 130;
  wpos.y += 20;
  locatorwin = window.open(url, position_id, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,dependent,resizable=1,copyhistory=0,screenX='+wpos.x+',screenY='+wpos.y+',width=400,height=400');
  locatorwin.focus();
}




// ******************** MODULES ******************************************* //
// ----- set_searchtype ----------------------------------------------------
var searchtype;
function set_searchtype(st) {
    searchtype = st;
}

// ----- show_hide_add_module ------------------------------------------------
function show_hide_add_module(id)
{
    var ie      = navigator.appName == "Microsoft Internet Explorer";
    var element = document.getElementById(id);

    if(element.className == 'clearfix hidden') {
	element.className = 'clearfix show';
        document.getElementById('mo-hide').className = "mo-hide";
        if (language == "fr") {
	    document.getElementById('mo-hide').innerHTML = "cacher modules";
        } else if (language == "it") {
	    document.getElementById('mo-hide').innerHTML = "nascondi schede";
        } else if (language == "es") {
	    document.getElementById('mo-hide').innerHTML = "Ocultar";
        } else if (language == "pl") {
	    document.getElementById('mo-hide').innerHTML = "schowaj modu&#322;y";
        } else if (language == "de") {
	    document.getElementById('mo-hide').innerHTML = "Module ausblenden";
        } else if (language == "jp") {
	    document.getElementById('mo-hide').innerHTML = "閉じる";
	} else {
	    document.getElementById('mo-hide').innerHTML = "hide modules";
	}
        document.getElementById('mo-add-container').style.visibility = "visible";
        document.getElementById('mo-add-container').style.display = "block";
        document.getElementById('mo-help-container').style.visibility = "visible";
        document.getElementById('mo-help-container').style.display = "block";
        element.style.display = "block";
        element.style.visibility = "visible";
	
	/* update widget list */ 
	update_search_widgets('1', 's');
    } else {
	element.className = 'clearfix hidden';
        document.getElementById('mo-hide').className = "mo-show";
        if (language == "fr") {
	    document.getElementById('mo-hide').innerHTML = "afficher modules";
        } else if (language == "it") {
	    document.getElementById('mo-hide').innerHTML = "mostra schede";
        } else if (language == "es") {
	    document.getElementById('mo-hide').innerHTML = "Mostrar";
        } else if (language == "pl") {
	    document.getElementById('mo-hide').innerHTML = "poka&#380; modu&#322;y";
        } else if (language == "de") {
	    document.getElementById('mo-hide').innerHTML = "Module anzeigen";
        } else if (language == "jp") {
	    document.getElementById('mo-hide').innerHTML = "開く";
	} else {    
	    document.getElementById('mo-hide').innerHTML = "show modules";
	}
        document.getElementById('mo-add-container').style.visibility = "hidden";
        document.getElementById('mo-add-container').style.display = "none";
        document.getElementById('mo-help-container').style.visibility = "hidden";
        document.getElementById('mo-help-container').style.display = "none";
        element.style.display = "none";
        element.style.visibility = "hidden";
        //document.getElementById('modules-header').className = "grey_border_bottom";
	
	/* update widget list */ 
	update_search_widgets('0', 's');
    }
}

// ----- open_close_widget_library --------------------------------------------
function open_close_widget_library(id)
{
    var ie      = navigator.appName == "Microsoft Internet Explorer";
    var element = document.getElementById(id);
    
    if (element.className == 'plus') {
	element.className = 'minus';
        document.getElementById('modules-library').style.visibility = "visible";
        document.getElementById('modules-library').style.display = "block";
        document.getElementById('mo-add-right').className = "right_bar_on";
        document.getElementById('mo-add-left').className = "left_bar_on";
        document.getElementById('module-library-content').style.visibility = "visible";
        document.getElementById('module-library-content').style.display = "block";
        document.getElementById('help-content').style.visibility = "hidden";
        document.getElementById('help-content').style.display = "none";
	document.getElementById('mo-help').className = "question";
        document.getElementById('mo-help-right').className = "right_bar_off";
        document.getElementById('mo-help-left').className = "left_bar_off";
    } else if (element.className == 'minus') {
	element.className = 'plus';
        document.getElementById('modules-library').style.visibility = "hidden";
        document.getElementById('modules-library').style.display = "none";
        document.getElementById('mo-add-right').className = "right_bar_off";
        document.getElementById('mo-add-left').className = "left_bar_off";
        document.getElementById('module-library-content').style.visibility = "hidden";
        document.getElementById('module-library-content').style.display = "none";
    } else if (element.className == 'question') {
	element.className = 'blue_minus';
        document.getElementById('mo-add').className = "plus";
        document.getElementById('modules-library').style.visibility = "visible";
        document.getElementById('modules-library').style.display = "block";
        document.getElementById('mo-add-right').className = "right_bar_off";
        document.getElementById('mo-add-left').className = "left_bar_off";
        document.getElementById('module-library-content').style.visibility = "hidden";
        document.getElementById('module-library-content').style.display = "none";
        document.getElementById('help-content').style.visibility = "visible";
        document.getElementById('help-content').style.display = "block";
        document.getElementById('mo-help-right').className = "right_bar_on";
        document.getElementById('mo-help-left').className = "left_bar_on";
    } else if (element.className == 'blue_minus') {
	element.className = 'question';
        document.getElementById('modules-library').style.visibility = "hidden";
        document.getElementById('modules-library').style.display = "none";
        document.getElementById('module-library-content').style.visibility = "hidden";
        document.getElementById('module-library-content').style.display = "none";
        document.getElementById('mo-help-right').className = "right_bar_off";
        document.getElementById('mo-help-left').className = "left_bar_off";
    }
}

// ----- open_close_results_library --------------------------------------------
function open_close_results_library(id)
{
    var ie      = navigator.appName == "Microsoft Internet Explorer";
    var element = document.getElementById(id);
    
    if (element.className == 'module-content hidden') {
	element.className = 'module-content show';
        document.add_content_image.src = 'images/mod_icon_minus.png';
	document.getElementById('module-add').className = 'module-add-active';
	
	/* update widget list */ 
	update_search_widgets('0', 'r');
    } else {
	element.className = 'module-content hidden';
        document.add_content_image.src = 'images/add_content.png';
	document.getElementById('module-add').className = 'module-box';
	
	/* update widget list */ 
	update_search_widgets('1', 'r');
    }
    
}

// ----- minimize_box --------------------------------------------------
function minimize_box(id) {
    var element = document.getElementById(id + '_box');
    var img = document.getElementById(id + '_image');
    var encl = document.getElementById(id + '_a');
    if (element.className == 'module-content hidden') {
	element.className = 'module-content show';
        encl.className = "anchor_image show";
        img.src = 'images/module_expanded.png';
    } else {
	element.className = 'module-content hidden';
        encl.className = "anchor_image hide";
        img.src = 'images/module_collapsed.png';
    }
}

// ----- box_close ------------------------------------------------------------
function box_close(ul_id, widget_name, page_id) {
    var ie      = navigator.appName == "Microsoft Internet Explorer";
    var li_id = ul_id + "_" + widget_name;
    var element = document.getElementById(li_id);
    disable_sortable();
    if (ie) {
	document.getElementById(li_id).removeNode(true);
    } else {
	var parent = element.parentNode;
	parent.removeChild(element);
    }
    create_sortable();
    

    if (page_id == "s") {
	if (document.getElementById('mo-help').className == 'blue_minus') {
	    document.getElementById('mo-help').className = 'question';
	    document.getElementById('mo-help-container').visibility = 'hidden';
	    document.getElementById('mo-help-container').display = 'none';
	    document.getElementById('mo-add').className = 'minus';
	    document.getElementById('modules-library').style.visibility = "visible";
	    document.getElementById('modules-library').style.display = "block";
	    document.getElementById('mo-add-right').className = "right_bar_on";
	    document.getElementById('mo-add-left').className = "left_bar_on";
	    document.getElementById('module-library-content').style.visibility = "visible";
	    document.getElementById('module-library-content').style.display = "block";
	    document.getElementById('help-content').style.visibility = "hidden";
	    document.getElementById('help-content').style.display = "none";
	    document.getElementById('mo-help-right').className = "right_bar_off";
	    document.getElementById('mo-help-left').className = "left_bar_off";
	} else if (document.getElementById('mo-add').className == 'plus') {
	    open_close_widget_library('mo-add');
	}
    }

    /* update widget list */
    update_widgets_wl(page_id);
}

// ----- restore_widgets ------------------------------------------------------
function restore_widgets(widget_name, page_id) {
    var newElement = document.createElement('li');
    
    disable_sortable();
    if (page_id == "s") {
	var id_name = 'searchcol1_' + widget_name;
	newElement.setAttribute('id',id_name);
	var contain = document.getElementById('searchcol1');
	contain.insertBefore(newElement, contain.firstChild);
	/* document.getElementById('searchcol1').appendChild(newElement); */
    } else if (page_id == "r") {
	var id_name = 'resultcol1_' + widget_name;
	newElement.setAttribute('id',id_name);
	var contain = document.getElementById('resultcol1');
	contain.insertBefore(newElement, contain.firstChild);
	/* document.getElementById('resultcol1').appendChild(newElement); */
    }
    create_sortable();

    update_widgets_wl(page_id);

    url = "widgets.php?showwidget=" + widget_name + "&rt=" + searchtype;
    var request;
    if (window.XMLHttpRequest) { // Mozilla, etc
	request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
	request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (!request) { return false; }
    request.onreadystatechange = function() {
	if (request.readyState == 4) {
	    if (request.status == 200) {
		var data = request.responseText;
		newElement.innerHTML = data;
		fix_widget_content(widget_name);
	    }
	}
    }
    request.open('GET', url, true);
    request.send(null);
}

// Standardized parameters for widget ajax calls
function AjaxParams() {
    var str = '';
    var params = new Array();
    if (typeof(origin ) != 'undefined') { params.push('origin='+origin); }
    if (typeof(destination ) != 'undefined') { params.push('destination='+destination); }
    if (typeof(dmon ) != 'undefined') { params.push('dmon='+dmon); }
    if (typeof(dday ) != 'undefined') { params.push('dday='+dday); }
    if (typeof(rmon ) != 'undefined') { params.push('rmon='+rmon); }
    if (typeof(rday ) != 'undefined') { params.push('rday='+rday); }
    if (typeof(ntix ) != 'undefined') { params.push('ntix='+ntix); }
    if (typeof(cabin ) != 'undefined') { params.push('cabin='+cabin); }
    if (typeof(triptype ) != 'undefined') { params.push('triptype='+triptype); }
    if (typeof(currency ) != 'undefined') { params.push('currency='+currency); }
    if (typeof(destination_enc ) != 'undefined') { params.push('destination_enc='+destination_enc); }
    if (typeof(pulocation) != 'undefined') { params.push('pulocation='+pulocation); }
    if (typeof(pumon ) != 'undefined') { params.push('pumon='+pumon); }
    if (typeof(puday ) != 'undefined') { params.push('puday='+puday); }
    if (typeof(puhour ) != 'undefined') { params.push('puhour='+puhour); }
    if (typeof(domon ) != 'undefined') { params.push('domon='+domon); }
    if (typeof(doday ) != 'undefined') { params.push('doday='+doday); }
    if (typeof(dohour ) != 'undefined') { params.push('dohour='+dohour); }
    if (typeof(atairport ) != 'undefined') { params.push('atairport='+atairport); }
    if (typeof(cartype ) != 'undefined') { params.push('cartype='+cartype); }
    if (typeof(queryid ) != 'undefined') { params.push('queryid='+queryid); }
    if (typeof(typefilter ) != 'undefined') { params.push('typefilter='+typefilter); }
    if (typeof(cimon ) != 'undefined') { params.push('cimon='+cimon); }
    if (typeof(ciday ) != 'undefined') { params.push('ciday='+ciday); }
    if (typeof(comon ) != 'undefined') { params.push('comon='+comon); }
    if (typeof(coday ) != 'undefined') { params.push('coday='+coday); }
    if (typeof(nguests ) != 'undefined') { params.push('nguests='+nguests); }
    if (typeof(nrooms ) != 'undefined') { params.push('nrooms='+nrooms); }
    if (typeof(activity ) != 'undefined') { params.push('activity='+activity); }
    if (typeof(language ) != 'undefined') { params.push('language='+language); }
    if (typeof(searchtype ) != 'undefined') { params.push('searchtype='+searchtype); }
    if (typeof(update_url ) != 'undefined') { params.push('update_url='+escape(update_url)); }
    if (typeof(widgetpass_community ) != 'undefined') { params.push('widgetpass_community='+escape(widgetpass_community)); }

    str = params.join('&');
    return str
}

function Widget(widget_name) {
    this.name = widget_name;
    this.elem_id = '';
    this.ajax_url = '';
    this.params = new Array();
}

var AsyncWidgets = new Array();
var w;

//-------------------- Add async widgets info here ------------------

/*
w = new Widget('communityr');
w.ajax_url = 'widgets/generic.php';
w.elem_id = 'communityr';
w.params.push('m_name=community');
w.params.push('width=170');
w.params.push('height=498');
AsyncWidgets.push(w);

w = new Widget('communitys');
w.ajax_url = 'widgets/generic.php';
w.elem_id = 'communitys';
w.params.push('m_name=community');
w.params.push('width=295');
w.params.push('height=387');
AsyncWidgets.push(w);
*/

w = new Widget('flickr');
w.ajax_url = 'widgets/flickr.php';
w.elem_id = 'flickr';
AsyncWidgets.push(w);

w = new Widget('weather');
w.ajax_url = 'widgets/weather.php';
w.elem_id = 'weather';
AsyncWidgets.push(w);

w = new Widget('books');
w.ajax_url = 'widgets/books.php';
w.elem_id = 'books';
AsyncWidgets.push(w);

// ----------------------------------------------------------

function get_widget(widget) {
    for (i in AsyncWidgets) {
	if (AsyncWidgets[i].name == widget) {
	    return AsyncWidgets[i];
	}
    }
    return null;
}

function update_communityr() {
    var o = document.getElementById('communityr_iframe');
    if (o) {
	var url = 'http://mobissimo.thingfo.com/index.php?';
	var params = new Array();
	if (typeof(origin ) != 'undefined') { params.push('origin='+origin); }
	if (typeof(destination ) != 'undefined') { params.push('destination='+destination); }
	if (typeof(dmon ) != 'undefined') { params.push('dmon='+dmon); }
	if (typeof(dday ) != 'undefined') { params.push('dday='+dday); }
	if (typeof(rmon ) != 'undefined') { params.push('rmon='+rmon); }
	if (typeof(rday ) != 'undefined') { params.push('rday='+rday); }
	if (typeof(queryid ) != 'undefined') { params.push('queryid='+queryid); }
	params = params.join('&');
	o.src = url + params;
    }
}

function update_communitys() {
    var o = document.getElementById('communitys_iframe');
    if (o) {
	var url = 'http://mobissimo.thingfo.com/index.php?';
	var params = new Array();
	if (typeof(origin ) != 'undefined') { params.push('origin='+origin); }
	if (typeof(destination ) != 'undefined') { params.push('destination='+destination); }
	if (typeof(dmon ) != 'undefined') { params.push('dmon='+dmon); }
	if (typeof(dday ) != 'undefined') { params.push('dday='+dday); }
	if (typeof(rmon ) != 'undefined') { params.push('rmon='+rmon); }
	if (typeof(rday ) != 'undefined') { params.push('rday='+rday); }
	if (typeof(queryid ) != 'undefined') { params.push('queryid='+queryid); }
	params = params.join('&');
	o.src = url + params;
    }
}

function update_star() {
    var star_param = new RegExp('stars=[^&]*&?');
    if (update_url.match(star_param)) {
        var s = String(update_url.match(star_param));
        s = s.replace('stars=', '');
        s = s.replace(/&$/, '');
        var ratings = s.split(",,");
        for (i = 0; i < ratings.length; i++) { 
            p = ratings[i];
            document.getElementById("rating_filter_" + ratings[i]).checked = true;
        }
    } else {
        document.getElementById("rating_filter_0").checked = true;
    }
}

function update_searchparams() {
        if (searchtype == "air") {
            o = document.getElementById('air');
            if (o) {
                o.depCity.value = origin;
                o.retCity.value = destination;
                o.depMonth.value = dmon;
                o.depDay.value = dday;
                o.retMonth.value = rmon;
                o.retDay.value = rday;
		o.NumTix.value = ntix;
                o.Class.value = cabin;
                rto = document.getElementById(triptype);
                if (rto) {
                    rto.checked = true;
                }
            }
        } else if (searchtype == "hotel") {
            o = document.getElementById('hotel');
            if (o) {
                o.dest.value = destination;
                o.arrMonth.value = cimon;
                o.arrDay.value = ciday;
                o.depMonth.value = comon;
                o.depDay.value = coday;
                o.numGuests.value = nguests;
                o.numRooms.value = nrooms;
            }
        } else if (searchtype == "car") {
            o = document.getElementById('car');
            if (o) {
                o.pulocation.value = pulocation;
                if (atairport) {
                    o.atairport.checked = true;
                } else {
                    o.atairport.checked = false;
                }
                o.pickupMonth.value = pumon;
                o.pickupDay.value = puday;
                o.pickupHour.value = puhour;
                o.dropoffMonth.value = domon;
                o.dropoffDay.value = doday;
                o.dropoffHour.value = dohour;
                o.cartype.value = cartype;
            }
        } else if (searchtype == "activity") {
            o = document.getElementById('air');
            if (o) {
                o.depCity.value = destination;
                o.activity.value = activity;
                o.depMonth.value = dmon;
                o.depDay.value = dday;
                o.retMonth.value = rmon;
                o.retDay.value = rday;
                o.NumTix.value = ntix;
                o.Class.value = cabin;
                rto = document.getElementById(triptype);
                if (rto) {
                    rto.checked = true;
                }
            }
        }
}

function fix_widget_content(widget) {
    var targetWidget = get_widget(widget);
    if (targetWidget !== null) {
	// dynamic widgets
	var params = AjaxParams();
	var contentType = "application/x-www-form-urlencoded; charset=UTF-8";
	var url = targetWidget.ajax_url;
	var div = targetWidget.elem_id;
	var extraparams = '';
	if (targetWidget.params.length >0) {
	    for (var i = 0; i<targetWidget.params.length; i++) {
		extraparams += '&'+targetWidget.params[i];
	    }
	}
	params += extraparams;

	var request = mkreq();
	if (!request) { return false; }
	request.onreadystatechange = function() {
	    if (request.readyState == 4) {
		if (request.status == 200) {
		    var data = request.responseText;
		    var o = document.getElementById(div);
		    if (o) {
			    o.innerHTML = data;
		    }
		}
	    }
	}
	request.open('POST', url, true);
	request.setRequestHeader("Content-Type", contentType);
	request.send(params);
    } else {
	//static widgets
	if (widget == 'searchparams') {
	    update_searchparams();
	} else if (widget == 'star') {
	    update_star();
	} else if (widget == 'supplier') {
	    update_display();
	} else if (widget == "currency") {
	    var o = document.getElementById("currency");
	    if (o) { o.value = currency; }
	} else if (widget == "cartype") {
	    var o = document.getElementById("typefilter");
	    if (o) { o.value = typefilter; }
	    var p = document.getElementById("cartype_queryid");
	    if (p) { p.value = queryid; }
	} else if (widget == "communityr") {
	    update_communityr();
	} else if (widget == "communitys") {
	    update_communitys();
	}
    }
}

function show_library() {
    url = "widgets.php?showlibrary=1";   
    var request;
    if (window.XMLHttpRequest) { // Mozilla, etc
	request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
	request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (!request) { /* request failed, use regular refresh */
	/* request failed, use regular refresh */
	document.location.reload(true);
	return false;
    }
    request.open('GET', url, true);
    request.send(null);
}


function box_img_mouseover(id) { 
    if (document.images) { 
	img = document.getElementById(id + "_image");
	encl = document.getElementById(id + "_a");
	if (encl.className == 'anchor_image' || encl.className == 'anchor_image show') {
	    img.src = "images/module_expanded.png";
	} else {
	    img.src = "images/module_collapsed.png";
	}
    }
}

function box_img_mouseout(id, icon) {
    if (document.images) {
	img = document.getElementById(id + "_image");
	img.src = icon;
    }
}

function mkreq() {
    var request;
    if (window.XMLHttpRequest) { // Mozilla, etc
	request = new XMLHttpRequest();
	/* request.overrideMimeType("text/xml"); */
    } else if (window.ActiveXObject) { // IE
	request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return(request);
}

function update_search_widgets(open_hide, st) {
    url = 'widgets.php?' + st + '=1&open_hide=' + open_hide;
    var request;
    if (window.XMLHttpRequest) { // Mozilla, etc
	request = new XMLHttpRequest();
	/* request.overrideMimeType("text/xml"); */
    } else if (window.ActiveXObject) { // IE
	request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (!request) { /* request failed, use regular refresh */
	/* request failed, use regular refresh */
	document.location.reload(true);
	return false;
    }
    request.open('GET', url, true);
    request.send(null);
}

var widgettype = "";
function update_widgets_nowl() {
    /* update widgets; do not update widget library */
    update_widgets(true, widgettype);
}

function update_widgets_wl(page_id) {
    /* update widgets; update widget library */
    update_widgets(true, page_id);
}

function update_library(request, page_id) {
    if (request.readyState == 4) {
	if (request.status == 200) {
	    url = "widgets.php?showlibrary=1&page_id=" + page_id + "&rt=" + searchtype;
	    var nrequest = mkreq();
	    if (!nrequest) { return false; }
	    nrequest.onreadystatechange = function() {
		if (nrequest.readyState == 4) {
		    if (nrequest.status == 200) {
			var data = nrequest.responseText;
			var wlib = document.getElementById("modules-library")
			wlib.innerHTML = data;
			lib_id = document.getElementById("modules-library");
			lib_cont = lib_id.getElementsByTagName("li");
		 	var add_module_text = "";	
			var string = '<div id="help-content" class="module-inner" style="display: none; visibility: hidden;">';
			if (language == "fr") {
			    if (lib_cont.length == 0){
				add_module_text = "Vous avez rajout&eacute; tous les modules disponibles";
			    } else {
				add_module_text = "Cliquez sur le nom du module pour le rajouter!";
			    }
			    string += '<p>A l\'aide des MobiModules vous pourrez personnaliser votre exp&eacute;rience sur Mobissimo et rajouter les sujets de voyages qui vous interessent. Chaque semaine des nouveaux modules...  pour vos souris!</p>';
			} else if (language == "it") {
			    if (lib_cont.length == 0){
				add_module_text = "hai aggiunto tutte le schede disponibili";
			    } else {
				add_module_text = "clicca sul nome da aggiungere";
			    }
			    string += '<p>mobischede. Rendi unica la tua esperienza su mobissimo e personalizza contenuti, attivit&agrave; e richieste</p>';
			} else if (language == "es") {
			    if (lib_cont.length == 0){
				add_module_text = "Has a&ntilde;adido todos m&oacute;dulos disponibles.";
			    } else {
				add_module_text = "haz clic en nombre para a&ntilde;adir";
			    }
			    string += '<p>MobiM&oacute;dulos te permiten crear tu &uacute;nica MobiExperiencia personalizando contenidos relacionados con viajes, actividades y aplicaci&oacute;nes. Vuelve! Cada semana a&ntilde;adimos m&aacute;s.</p>';
			} else if (language == "pl") {
			    if (lib_cont.length == 0){
				add_module_text = "Doda&#322;e&#347; wszystkie dost&#281;pne modu&#322;y";
			    } else {
				add_module_text = "Wci&#347;nij nazw&#281; by doda&#263;";
			    }
			    string += '<p>MobiModu&#322;y pozwalaj&#261; Ci spersonalizowa&#263; nasz&#261; stron&#281;. Mo&#380;esz dodawa&#263; modu&#322;y, zawarto&#347;&#263; i aplikacje zwi&#261;zane z podr&oacute;&#380;ami. Co tydzie&#324; dodajemy wi&#281;cej!</p>';
			} else if (language == "de") {
			    if (lib_cont.length == 0){
				add_module_text = "Sie haben alle verf&uuml;gbaren Module hinzugef&uuml;gt.";
			    } else {
				add_module_text = "zum Hinzuf&uuml;gen auf Namen klicken";
			    }
			    string += '<p>&Uuml;ber MobiModules k&ouml;nnen Sie reisebezogene Inhalte, Funktionen und Anwendungen hinzuf&uuml;gen und so Ihre ganz pers&ouml;nliche Mobissimo-Seite gestalten. Schauen Sie bald wieder vorbei, wir f&uuml;gen jede Woche etwas Neues hinzu!</p>';
			} else if (language == "jp") {
			    if (lib_cont.length == 0){
				add_module_text = "追加可能なガジェット";
			    } else {
				add_module_text = "ガジェット名をクリックして下さい";
			    }
			    string += '<p>旅行関連のガジェットを用意しました。</p><p>自由に組み合わせてあなたらしいページにカスタマイズしましょう。</p><p>ガジェットはどんどん追加されていく予定です。</p>';
			} else {
			    if (lib_cont.length == 0){
				add_module_text = "You've added all available modules";
			    } else {
				add_module_text = "click a name to add";
			    }
			    string += '<p> MobiModules let you personalize your Mobissimo experience with travel related content, features, and applications. Check back often, we add more every week. </p>';
			}
			string += '</div>';
	                wlib.innerHTML += string; 
			
			lib_cont_id = document.getElementById("click_to_add");
			lib_cont_id.innerHTML = '(';
			lib_cont_id.innerHTML += add_module_text;
			lib_cont_id.innerHTML += ')';
		    }
		}
	    };
	    nrequest.open('GET', url, true);
	    nrequest.send(null);
	}
    }
}

function update_widgets(update_all, page_id) {
    if (page_id == "s") {
	var url = 'widgets.php?s=1&' + Sortable.serialize('searchcol1') + '&' + Sortable.serialize('searchcol2') + '&' + Sortable.serialize('searchcol3');
    } else {
	var url = 'widgets.php?r=1&' + Sortable.serialize('resultcol1');
    }
    var request = mkreq();
    if (!request) { return false; }
    if (update_all) {
	request.onreadystatechange = function() {
	    update_library(request, page_id);
	};
    }
    request.open('GET', url, true);
    request.send(null);
}

/* global update var is update_url */
function filter_airlines() {
    var i = 0;
    airlines = "";
    while (i < 100) {
	o = document.getElementById("airline-" + String(i));
	if (o) {
	    if (o.checked == true) {
		if (airlines) { airlines += ",,"; }
		airlines += o.value;
	    }
	} else {
	    break;
	}
	i++;
    }
    airline_url_parm = escape(airlines);
    sfilter("airlines", airlines);
}

function select_all_airlines(v) {
    var i = 0;
    while (i < 100) {
	o = document.getElementById("airline-" + String(i));
	if (o) {
	    o.checked = v;
	} else {
	    break;
	}
	i++;
    }
    if (v == true) {
	filter_airlines();
    }
}


function select_one_airline(n) {
    var i = 0;
    while (i < 100) {
	o = document.getElementById("airline-" + String(i));
	if (o) {
	    if (i == n) {
		o.checked = true;
	    } else {
		o.checked = false;
	    }
	} else {
	    break;
	}
	i++;
    }
    filter_airlines();
}

/* global update var is update_url */
function filter_rating() {
    var i = 0;
    stars = "";
    while (i < 5) {
	var o = document.getElementById("rating_filter_" + String(i));
	if (o) {
	    if (o.checked == true) {
		if (stars) { stars += ",,"; }
		stars += o.value;
	    }
	} else {
	    break;
	}
	i++;
    }
    stars_url_parm = escape(stars);
    sfilter("stars", stars);
}

function select_all_rating() {
    if (document.getElementById("rating_filter_0").checked == true) {
	document.getElementById("rating_filter_0").checked = true; 
	var i = 1;
	while (i < 5) {
	    o = document.getElementById("rating_filter_" + String(i));
	    if (o) {
		o.checked = false;
	    }
	    i++;
	}
	filter_rating();
    } else {
	document.getElementById("rating_filter_0").checked = false; 
    }
}

function select_rating() {
    if (document.getElementById("rating_filter_0").checked == true) {
	document.getElementById("rating_filter_0").checked = false; 
    }
    filter_rating();
}



/* ================= recommendation box ===============*/
function rec_mouseover(id) {
    e1 = document.getElementById('rec_'+id+'_icon');
    e1.className = 'recommendation_'+id+'_dark';
    e2 = document.getElementById('rec_'+id+'_body');
    e2.className = 'recommendation_body_dark';
    e3 = document.getElementById('rec_'+id+'_text');
    if (id == 'nonstop' || id == 'market' || id == 'alternate' || id == 'business' || id == '3star' || id == '4star' || id == '5star') {
	e3.className = 'recommendation_text_on';
	e4 = document.getElementById('rec_'+id+'_detail');
	e4.style.display = 'block';
    } else {
	e3.className = 'recommendation_static_text_over';

    }
}

function rec_mouseout(id) {
    e1 = document.getElementById('rec_'+id+'_icon');
    e1.className = 'recommendation_'+id;
    e2 = document.getElementById('rec_'+id+'_body');
    e2.className = 'recommendation_body';
    e3 = document.getElementById('rec_'+id+'_text');
    if (id == 'nonstop' || id == 'market' || id == 'alternate' || id == 'business' || id == '3star' || id == '4star' || id == '5star') {
	e3.className = 'recommendation_text';
	e4 = document.getElementById('rec_'+id+'_detail');
	e4.style.display = 'none';
    } else {
	e3.className = 'recommendation_static_text';
    }
}

function american_mouseover(id) {
    o1 = document.getElementById('results_tr_'+id);
    o1.className = 'hover';
    
    o2 = document.getElementById('american_ad_'+id);
    o2.style.display = '';
}

function american_mouseout(id) {
    o1 = document.getElementById('results_tr_'+id);
    o1.className = 'hover_fix';
    
    o2 = document.getElementById('american_ad_'+id);
    o2.style.display = 'none';
}


function mobifriends_mouseover(id) {
    o1 = document.getElementById('results_tr_'+id);
    o1.className = 'hover';
    
    o2 = document.getElementById('mobifriends_lineunder_'+id);
    o2.style.display = '';
}

function mobifriends_mouseout(id) {
    o1 = document.getElementById('results_tr_'+id);
    o1.className = 'hover_fix';
    
    o2 = document.getElementById('mobifriends_lineunder_'+id);
    o2.style.display = 'none';
}


//------------ Hotel results javascript ----------------//
// ----- open_close_detail ------
function open_close_detail(detail_id) {
    var element = document.getElementById(detail_id);
    if (document.getElementById('hotel_detail_' + detail_id).style.visibility == "hidden") {
	document.getElementById('detail_id_' + detail_id).innerHTML = 'CLOSE';
	document.getElementById('hotel_detail_' + detail_id).style.visibility = "visible";
	document.getElementById('hotel_detail_' + detail_id).style.display = "block";
    } else {
	document.getElementById('detail_id_' + detail_id).innerHTML = 'DETAILS';
	document.getElementById('hotel_detail_' + detail_id).style.visibility = "hidden";
	document.getElementById('hotel_detail_' + detail_id).style.display = "none";
    }
}
