//*** listener for chamging dates from calendar application 
//*****************************************************************
function dateChangeListener()
{
	index = document.getElementById('datesArray').selectedIndex;
	data = document.getElementById('datesArray').options[index].value;
	if (!selectedDate)
		selectedDate = data;
	else if (selectedDate != data)
	{	
		changeValue(document.getElementById('date' + (index + 1)));
	}
}


//*** select actual flyer from list 
//*****************************************************************
function olink( fid )
{
	// dont submit when no departure airport is selected!!!

	var is_departure = false ; 
	
	for(var i=0; i < document.FlyerSelect.elements.length; i++)
	{
		var e = document.FlyerSelect.elements[i];
		if ((e.name != 'airportCode') && (e.type == 'checkbox'))
		{
			if (e.checked == true)
			{
				is_departure = true ; 
			} 
		}
	}


	if ( is_departure == true )
	{
		window.location.href = mainlink_offer+'&fid='+fid ; 
	}
	else
	{
		window.location.href = mainlink_offer+'&fid='+fid ; 
		//alert ('Bitte zuerst Ihre Abflughäfen auswählen !')
	}
}







//*** show flyerofferlist 
//*****************************************************************
function show_offerlist( inparent )
{
	preloader_offerlist( inparent ) ;
	
	var ajax = new AjaxRequest();
	ajax.onSuccess 	= cbSuccess;
	ajax.onLoading 	= cbLoading;
	ajax.onError 	= cbError;
	ajax.call( 'm.php?nm=STB&tm=ST&fi=offer_results&tm=ST&copytoparent='+inparent+'&show=1', 'GET' );

	function cbSuccess(response)
	{		
		if (inparent == false )
		{
			document.getElementById('offer-results').innerHTML = response ; 
			document.getElementById('offer-results').style.display = 'inline' ;
			ajax_hideTooltip();

		} else {
			parent.document.getElementById('offer-results').innerHTML = response ; 
			parent.document.getElementById('offer-results').style.display = 'inline' ;
			parent.ajax_hideTooltip();
		}
	}
	function cbLoading(){;}
	function cbError(){;}
}	


//*** reset selected departures 
//*****************************************************************
function unset_departures ()
{
	for(var i=0; i < document.FlyerSelect.elements.length; i++)
	{
		var e = document.FlyerSelect.elements[i];
		if ((e.name != 'airportCode') && (e.type == 'checkbox'))
		{
			e.checked = false ;
		}

	}

	//document.FlyerSelect.OfferAirline.value='' ;

}


//*** show flyerofferlist in parent instance (called from iframe)
//*****************************************************************
function init_resultarea ()
{
	document.getElementById('offer-results').style.background = '#FFF' ;
	ajax_hideTooltip();
}


//*** show flyerofferlist in parent instance (called from iframe)
//*****************************************************************
function preloader_offerlist (inparent)
{
	var loadingmessage = '<div style=\"padding:50px\"> <table align=\"center\" style="\height:100px;width:300px;background:#FFF; color:black; border:1px solid black\" > <tr><td align=\"center\"><img src="img/loading.gif"></td></tr><tr><td align=\"center\">Lade Angebote ...</td></tr></table></div>'; 

	if (inparent == false )
	{
		document.getElementById('offer-results').style.height = '600px' ; 
		document.getElementById('offer-results').innerHTML = loadingmessage ; 
	} else {
		parent.document.getElementById('offer-results').style.height = '600px' ; 
		parent.document.getElementById('offer-results').innerHTML = loadingmessage ; 
	}
}	

//*** show departure airports in parent instance (called from iframe)
//*****************************************************************
function preloader_departures (inparent)
{
	var loadingmessage = '<div style=\"padding:50px\"> <table align=\"center\" style="\height:100px;width:100%;\" > <tr><td align=\"center\"><img src="img/loading.gif"></td></tr><tr><td align=\"center\">...</td></tr></table></div>'; 

	if (inparent == false )
	{
		document.getElementById('search_departures').innerHTML = loadingmessage ; 
	} else {
		parent.document.getElementById('search_departures').innerHTML = loadingmessage ; 
	}
}	

//***
//*****************************************************************


//*** show searchmask right of resultlist 
//*****************************************************************
function show_searchmask( inparent  )
{
	var ajax = new AjaxRequest();
	ajax.onSuccess 	= cbSuccess;
	ajax.onLoading 	= cbLoading;
	ajax.onError 	= cbError;
//	ajax.call( 'm.php?nm=STB&tm=ST&fi=offer_search&tm=ST&copytoparent='+inparent+'&show=1&noshowlist=0', 'GET' );
	ajax.call( 'm.php?nm=STB&tm=ST&fi=offer_search&tm=ST&copytoparent='+inparent+'&show=1&noshowlist=0&debug=1', 'GET' );
		
	function cbSuccess(response)
	{		
		if (inparent == false )
		{
			document.getElementById('offer-searchmask').innerHTML = response ; 
			document.getElementById('offer-searchmask').style.display = 'inline' ;
		} else {
			parent.document.getElementById('offer-searchmask').innerHTML = response ; 
			parent.document.getElementById('offer-searchmask').style.display = 'inline' ;
		}
	}
	function cbLoading(){;}
	function cbError(){;}

}	



//*** show step2 searchmask right of resultlist 
//*****************************************************************
function show_step2_searchmask( inparent  )
{
	var ajax = new AjaxRequest();
	ajax.onSuccess 	= cbSuccess;
	ajax.onLoading 	= cbLoading;
	ajax.onError 	= cbError;
	ajax.call( 'm.php?nm=STB&tm=ST&fi=offer_search&tm=ST&copytoparent='+inparent+'&show=1&noshowlist=0&debug=1', 'GET' );
		
	function cbSuccess(response)
	{		
		if (inparent == false )
		{
			document.getElementById('offer-searchmask').innerHTML = response ; 
			document.getElementById('offer-searchmask').style.display = 'inline' ;
		} else {
			parent.document.getElementById('offer-searchmask').innerHTML = response ; 
			parent.document.getElementById('offer-searchmask').style.display = 'inline' ;
		}
	}
	function cbLoading(){;}
	function cbError(){;}
}	



//*** remove region selection, reload searchmask, departurepanel and offerlist
//*****************************************************************
function remove_destination( inparent, regionstep )
{

	var ajax		= new AjaxRequest();
	ajax.onSuccess 	= cbSuccess;
	ajax.onLoading 	= cbLoading;
	ajax.onError 	= cbError;
	ajax.call( 'm.php?nm=STB&tm=ST&fi=offer_search&tm=ST&copytoparent='+inparent+'&show=1&regionstep='+regionstep, 'GET' );
		
	function cbSuccess(response)
	{		
		if (inparent == false )
		{
			document.getElementById('offer-searchmask').innerHTML = response ; 
			document.getElementById('offer-searchmask').style.display = 'inline' ;
		} else {
			parent.document.getElementById('offer-searchmask').innerHTML = response ; 
			parent.document.getElementById('offer-searchmask').style.display = 'inline' ;
		}
		show_offerlist( inparent ) ; 
		set_departures ( inparent, '', false ) ;
	}
	function cbLoading(){;}
	function cbError(){;}
}


//*** use that for map in offerlist, to determine different mapsituations, not ready yet
//*****************************************************************
function ShowOffermap(fid)
{
	ajax_showTooltip('x-IBE/controlers/ModuleHotelAvailable/region_tooltip_city_list_map.php?regionid=&amp;language=',this, '350', '350');
}



//*** hide elemets 
//*****************************************************************
function HideElements(elementid)
{
	document.getElementById(elementid).style.display = 'none' ;
}

//*** show destinationstree
//*****************************************************************
function ShowDestinations()
{
	var ajax			= new AjaxRequest();
	ajax.onSuccess 		= cbSuccess;
	ajax.onLoading 		= cbLoading;
	ajax.onError 		= cbError;
	ajax.call( 'm.php?nm=STB&tm=ST&fi=DestinationsTree', 'GET' );
	
	function cbSuccess(response)
	{		
		document.getElementById('destpanel_hover').innerHTML = response ; 
		document.getElementById('destpanel_hover').style.display = 'inline' ;
	}
	function cbLoading(){;}
	function cbError(){;}
}

//*** show hidde elements
//*****************************************************************
function showHidden(change)
{
    if (document.all){
        var unhide = document.getElementById(change);
        var isShowing = (unhide.style.display == 'inline');
        unhide.style.display = isShowing ? 'none' : 'inline';
    } else {
    	var unhide = document.getElementById(change);
    	var isShowing = (unhide.style.display == 'table');
    	unhide.style.display = isShowing ? 'none' : 'table';
    }
}


//*** show flyerofferlist in parent instance (called from iframe)
//*****************************************************************
function OpenHotelCategory(elementId,action,blockNm)
{
	var temp = document.getElementsByName('tocheck'+blockNm);
	var toshow = document.getElementById('HotelItemIFrame'+blockNm+'-'+elementId);
//	alert( toshow ) ; 
	for (var divLoop=0; divLoop < temp.length; divLoop++)
	{
		var thisOne = document.getElementById('HotelItemIFrame'+blockNm+'-'+temp[divLoop].value);
		if (thisOne == toshow)
		{
			thisOne.style.display = '';
		}
		else
		{
			if (thisOne.style.display == '')
			{
				thisOne.style.display = 'none';
			}
			else
			{
			}
		}
	}

	var toShow = document.getElementById('HotelItemTR'+blockNm+'-'+elementId);
	for (var tableLoop = 0; tableLoop < temp.length; tableLoop++)
	{
		var thisOne = document.getElementById('HotelItemTR'+blockNm+'-'+temp[tableLoop].value);
		if(thisOne != toShow)
		{
			thisOne.style.display = '';
		}
	}

	document.getElementById('HotelItemTR'+blockNm+'-'+elementId).style.display = 'none';
	var tempName = 'IFrame'+elementId;

	alert (tempName) ; 

	if (document.getElementById(tempName).src != action)
	{
		document.getElementById(tempName).style.display = 'none';
		document.getElementById(tempName).src = action;
		document.getElementById('HotelPreloader'+elementId).style.display = 'inline';
	

		alert (tempName) ; 

		var ajax = new AjaxRequest();
		ajax.onSuccess 	= cbSuccess;
		ajax.onLoading 	= cbLoading;
		ajax.onError 	= cbError;
		ajax.call( action, 'GET' );
	
		function cbSuccess(response)
		{		
			document.getElementById(tempName).innerHTML = response ; 
			document.getElementById(tempName).style.display = 'inline' ;
		}
		function cbLoading(){;}
		function cbError(){;}
	}
}

//*** show Preloader in given element-tid 
//*****************************************************************
function show_preloader ( inparent, xelement )
{
	var loadingmessage = '<div style=\"padding:50px\"> <table align=\"center\" style="\height:100px;width:300px;background:#FFF; color:black; border:1px solid black\" > <tr><td align=\"center\"><img src="img/loading.gif"></td></tr><tr><td align=\"center\"></td></tr></table></div>'; 

alert (xelement) ; 

	if (inparent == false )
	{
		//document.getElementById(xelement).style.border = '10px solid #000000' ;
		document.getElementById(xelement).innerHTML = loadingmessage ; 
	} else {
		parent.document.getElementById(xelement).innerHTML = loadingmessage ; 
	}
}	


//*** show Preloader in given element-tid 
//*****************************************************************
function show_ajaxpreloader ( inparent, xelement )
{
	var loadingmessage = '<div class="\AjaxLoader\" ></div>'; 

	if (inparent == false )
	{
		document.getElementById(xelement).innerHTML = loadingmessage ; 
	} else {
		parent.document.getElementById(xelement).innerHTML = loadingmessage ; 
	}
}	



//*** opens a div with traveloder /// not needed at moment 
//*****************************************************************
function showCombiDiv(numb,landID)
{
	if(document.getElementById('combiDiv'+numb+landID))
	{
		if(document.getElementById('combiDiv'+numb+landID).style.display == 'none')
			document.getElementById('combiDiv'+numb+landID).style.display = '';
		else
			document.getElementById('combiDiv'+numb+landID).style.display = 'none';
	}
}

//*** pointer for tr row 
//*****************************************************************
function changeColorForDestinationList(theRow, theAction)
{
  var theDefaultClass = 'listTableRowLow';
  var thePointerClass = 'listTableRowHigh';
  var theMarkClass    = 'listTableRowMark';    
	
	switch(theAction)
	{
	  case "over" :
		  if(theRow.className != theMarkClass)
		    theRow.className = thePointerClass ;
	  break;
	  case "out" :
		  if(theRow.className != theMarkClass)
		    theRow.className = theDefaultClass ;
	  break;
	  case "click" :
		
		if(theRow.className != theMarkClass)
		    theRow.className = theMarkClass ;
		else theRow.className = thePointerClass ;
      
	  break;
	}
}

//*** convert first char to upper char 
//*****************************************************************
function UpperFirstChar(obj)
{
	obj.value = obj.value.substring(0,1).toUpperCase()+obj.value.substring(1,obj.value.length)
}


//*** load content into iframe
//*****************************************************************
function loadintoIframe(iframeid, url)
{
	if (document.getElementById)
		document.getElementById(iframeid).src=url
}

//*** convert entities
//*****************************************************************
function ConvertEntities(sHTML)
{
  if (sHTML=="") return "";
  var sText = sHTML;
  if (typeof document.createElement != "undefined")
  {
    var oElem = document.createElement("span");
    if (typeof oElem.innerHTML != "undefined")
    {
      oElem.innerHTML = sHTML;      
      if (typeof oElem.firstChild != "undefined" && typeof oElem.firstChild.nodeValue != "undefined");
        sText = oElem.firstChild.nodeValue;
    }
  }
  return sText;
}

//*** AJAX functions 
//*****************************************************************
function AjaxRequest() {

	this.response = '';
	
	this.error = '';

	this.onSuccess = 	function () {
							alert( 'Please override onSuccess callback.' );
						};
	this.onLoading = 	function () {
							alert( 'Please override onLoading callback.' );
						};
	this.onError = 		function ( err) {
							alert( err );
						};
	this.call = 		function( uri, method ) {							
							
							var selfReference = this;
							this.http.open( method, uri );
							this.http.onreadystatechange = function(){AjaxRequestHandler(selfReference );}
							this.http.send( null );
							/* handling for POST method
							this.http.abort;
							this.http.open('post',  'back_end.php');
							this.http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
							this.http.send('arg1=val1&arg2=val2&arg3=val3');
							*/							
						};
	
	this.createRequestObject = 	function () {
									var request_o; //declare the variable to hold the object.
									var browser = navigator.appName; //find the browser name
									if( browser == "Microsoft Internet Explorer" ){
										/* Create the object using MSIE's method */
										request_o = new ActiveXObject( "Microsoft.XMLHTTP" );
									}else{
										/* Create the object using other browser's method */
										request_o = new XMLHttpRequest();
									}
									return request_o; //return the object
								};

	this.http = this.createRequestObject(); 						

}

function AjaxRequestHandler(client) {
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	//debugToDiv( client.http.responseText, 'divId', 0 );

	if( client.http.readyState == 4 ){ 
		var response = '';
		response = client.http.responseText ; 
		//alert(client.http.responseText);
		//eval("response = " + client.http.responseText);

		if( client.http.status != 200 ) {
			client.onError( 'HTTP Error, received status ' + client.http.status + ' instead of expected 200' );
		} else if( response[ '__errorString' ] != null ) {
			client.onError( response[ '__errorString' ] );
		} else {
			client.onSuccess( response );
		}
	} else if( client.http.readyState == 1 ) {
		client.onLoading();
	}
}

function debugToDiv ( oToDebug, sDivId, iIndent ) {
	document.getElementById( sDivId ).innerHTML += '<pre>' ; 
	for ( mProperty in oToDebug ) {
		var oMember = oToDebug[ mProperty ];
		var j = 0;
	
		for( j=0; j<iIndent; j++ ) {
			document.getElementById( sDivId ).innerHTML += '	';
		}
		if( typeof( oMember ) == 'object' ) {
			document.getElementById( sDivId ).innerHTML += mProperty + ' (Object) <br />';
			debugToDiv( oMember, sDivId, iIndent + 1 );
		} else {
			document.getElementById( sDivId ).innerHTML += ( mProperty + ' => ' + oMember + '<br />' );
		}
	}
	document.getElementById( sDivId ).innerHTML += '</pre>' ; 
}


function AJAX_stateHandler(client) {
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	//debugToDiv( client.http.responseText, 'divId', 0 );

	if( client.http.readyState == 4 ){ 
		var response = '';
		eval("response = " + client.http.responseText);
		if( client.http.status != 200 ) {
			client.onError( 'HTTP Error, received status ' + client.http.status + ' instead of expected 200' );
		} else if( response[ '__errorString' ] != null ) {
			client.onError( response[ '__errorString' ] );
		} else {
			client.onSuccess( response );
		}
	} else if( client.http.readyState == 1 ) {
		client.onLoading();
	}
}

