 var productTemp='';
 // Refreshing resorts drop-down
	      function updateReviewDestinations(data) {
	
	         dwr.util.removeAllOptions("destinationSelect");
	         dwr.util.addOptions("destinationSelect", data);
	         //calling this because on change of destination to default "select destination"
	   //should also change the accommodation in addition to resorts.
	   	onReviewResortChange();
      		}
	

	/*DE JS Methods*/
	 function onRegionChange() {
        var region = dwr.util.getValue("resortSelect");
		var proposition = dwr.util.getValue("propositionIDField");
		try{
	        AjaxSearchTools.populateDestinationDetails(region,proposition, updateDestinationSelect);
		}catch(err){}
      }

	  function updateDestinationSelect(data){
		  if(data[0] != '-1')
		  {
			  if(document.getElementById("topDestionation") != null)
			  {
				document.getElementById("topDestionation").value = data[1];
			  }
			  document.getElementById("destinationSelect").value = data[0];
		  }
	  }


	// Javascript function called on change of destinations drop-down,
	// to populate destination-dependent regions-DE
    function onDestinationChange() {
	if((document.getElementById('searchPanelIdentifier')) == null)
		{
			var proposition = dwr.util.getValue("propositionIDField");
			var destination = dwr.util.getValue("destinationSelect");
			try{
				AjaxSearchTools.populateRegions(destination, proposition, updateResorts);
			}catch(err){}
		}
     }


	 	// Refreshing resorts drop-down
    function updateResorts(data) {
        dwr.util.removeAllOptions("resortSelect");
		//document.getElementById("resortSelect").options[0]=new Option("alle Regionen","-1");
        //dwr.util.addOptions("resortSelect", data);
		var output = ['<option value="-1">alle Regionen</option>'];
        jQuery.each(data, function(key, value){
	        output.push('<option value="'+ key +'">'+ value +'</option>');
        });
        jQuery('#resortSelect').html(output.join(''));
		if(navigator.userAgent.match("Chrome"))
			sortSelect();
				
    }

	//Client Side sorting for Chrome - DE-700
	function sortSelect() {
		var my_options = $("#resortSelect option:gt(0)");
		my_options.sort(function(a,b) {
			if (a.text > b.text) return 1;
			else if (a.text < b.text) return -1;
			else return 0;
		});		
		jQuery("#resortSelect").empty().append(my_options);
		jQuery("#resortSelect").prepend('<option value="-1" selected="selected">alle Regionen</option>');
	}

/*DE JS Methods*/


	function addPackageToShortList(id,url){
		
		  var id1=id;

		  maximumAddPackageToShortList(id1,url);
		  return false;

		}
		function maximumAddPackageToShortList(id,url){
			if(id){
				AjaxSearchResultTools.reqPackageAddToShortList('', id, '', function(data) {
					if(data == 'SessionTimeOut'){
						document.getElementById('compareDiv').style.display = "none";
					}
					
				});
				
				AjaxSearchResultTools.getShortlistContent('/search/de/compareDesign1.jsp', id, function(data) {
					 //var compareURL = window.location.protocol + "//" + window.location.host + "/" + "/search/de/compareDesign1.jsp";
					 window.open("/search/de/compareDesign1.jsp","merkzettel","height=650,width=680,dependent=yes,scrollbars=yes,menubar=no,resizable,status=no,toolbar=no");  
					 //if(newwindow != null || typeof(newwindow) != 'undefined'){ newwindow.focus(); }
				});
				
			}
		}

		function addPackageToShortList(id,url,pageId){
		  var id1=id;
		  maximumAddPackageToShortList(id1,url,pageId);
		  var merkzettel = window.open("/search/de/compareDesign1.jsp?pageId="+pageId,"merkzettel","height=650,width=680,scrollbars=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes");
		  return false;

		}
		function maximumAddPackageToShortList(id,url,pageId){
			/**Code Modification start for DE-2201
			* Description: shortlist page will now redirect the parent window to termine page
			* on the click of zur buchung
			* Author: Harsh
			*/
			if(url == null && url == ''){
				url = window.location.href;
			}
			if(id){
				DEShortListTools.reqPackageAddToShortList('', id, url, function(data) {
					if(data == 'SessionTimeOut'){
						document.getElementById('compareDiv').style.display = "none";
					}
					
				});
				
				AjaxSearchResultTools.getShortlistContent('/search/de/compareDesign1.jsp', id, function(data) {
					 //var compareURL = window.location.protocol + "//" + window.location.host + "/" + "/search/de/compareDesign1.jsp";
					 //window.open("/search/de/compareDesign1.jsp?pageId="+pageId,"merkzettel","height=650,width=680,dependent=yes,scrollbars=yes,menubar=no,resizable,status=no,toolbar=no");  
					 //if(newwindow != null || typeof(newwindow) != 'undefined'){ newwindow.focus(); }
				});
			
			/**
			*Code Modification end for DE-2201
			*/			
			}
		}
		
		
		 function removePackageFromShortList(id){		 
				DEShortListTools.reqPackageRemoveFromShortList(id, function(data) {					
					window.location.reload(true);				
				});
				
			
		}
		 function removeFlightPackageFromShortList(bookingid){		 
			if(bookingid){		
				AjaxSearchResultTools.removeFlightPackageFromShortList(bookingid, function(data) {					
					window.location.reload(true);				
				});
				
			}
		}

		function shortlistItemCount(shortListCount){
		if(shortListCount==0){

		document.getElementById( "compareDiv").style.display = "none";
		}
	}

	function removeShortList(data){
			//document.getElementById('shortList').style.display = (data <= 0)? 'none' : 'block';
			document.getElementById('sizeofshortlist').innerHTML= data + ' Angebote';			
		}


function updateDERecentlyViewedList(selectedPackageId, pageId, currencyCode,pageurlname){  
   selectedPackId = selectedPackageId;
   currency = currencyCode;   
   //alert(selectedPackageId);
   //alert(pageId);
   //alert(currencyCode);
   AjaxSearchResultTools.updateRecentlyViewedList(selectedPackageId,pageId,currencyCode,pageurlname); 
  }

 //This method makes the Ajax call for recosting
 //Changes done in this method for DEBAU-143 Author:Harsh 
 function reCost(id,bookingId,IFF,pageId,supplier,hotelInfo,bookingUrl,roomsInfo,urlName,airportName,bookingCode,tourOpCode,strDuration,omniDate,airportDepCode,cntRegion) {
	 
	 // For tracking
	 s.pageName=getPageName(s.pageName)+"terminverfuegbar";
	 s.events = "event8";
	 
	//Setting departure date to prop and evar
	s.eVar5 = omniDate;
	s.prop5 = omniDate;
	
	//Setting duration to prop and evar
	s.eVar6 = strDuration;
	s.prop6 = strDuration;
	
	//Setting tour operator
	s.eVar9=supplier;
	s.prop9=supplier;
	
	
		//alert("cntRegion: " + cntRegion);
	 var url=bookingUrl+"?page_url_name="+urlName+"&search=booking&IFF="+IFF+"&bookingId="+bookingId+"&pageId="+pageId;
	 var imgUrl="http://cluster1.loc1.ffm.synaix.de/images/content/va_logos/small/"+supplier+".gif";
	 if(pageId=='hotel' || pageId=='wellness' || pageId=='holidayHomes') {
		var preisTxt = "alle Personen";
 		if(pageId=='holidayHomes')	preisTxt = "das Objekt";
		var htmlContent="<div class=\"resortSelectedHeader bold\">Das von Ihnen gepr&#252;fte Angebot</div><div class=\"resortSpecial\"><div class=\"colA\">$TEXT2<br /><br /><a href=\"javascript:;\" class=\"angebotLink spclLink\" onclick=\"addFlightInfoToShortList('"+bookingId+"','"+IFF+"')\" title=\"Angebot merken\">Angebot merken</a></div><div class=\"colC\">$TEXT3</div><div class=\"colD\">$LINK1</div><div class=\"colE\">$ROOMS</div><div class=\"colH\"><p class=\"colHtxt bold\">Gesamtpreis f&#252;r<br />" + preisTxt + "</p><p><a href='$LINK2' class=\"dealPrice\" title=\"Preis\">$TEXT4,-&euro;</a></p><p><a href='$LINK3' class=\"zurButton\" title=\"Zur Buchung\">&nbsp;</a></p></div></div>";
		
		//Setting s.products as per clarification in DEBAU-637
		s.products = productTemp;
	 }
	 else {
		var htmlContent="<div class=\"resortSelectedHeader bold\">Es sind noch Plätze frei</div><div class=\"colA\"><a href=\"javascript:;\" class=\"regionInfoLink bold\" onclick=\"callAccomDetailsWithPrice("+cntRegion+",'airport', '"+pageId+"','$TEXT4')\"  title=\"Abflughafen-Informationen aufrufen\">$TEXT5</a><br /><a href=\"javascript:;\" class=\"linkNext spclLink\" onclick=\"openPopup('/search/de/charterpopup.jsp?BOOKING_ID=" + bookingId + "',450,300);\" title=\"Flugzeiten anzeigen\">Flugzeiten anzeigen</a><br /><a href=\"javascript:;\" class=\"angebotLink spclLink\" onclick=\"addFlightInfoToShortList('"+bookingId+"','"+IFF+"')\" title=\"Angebot merken\">Angebot merken</a></div><div class=\"colB\">$TEXT2</div><div class=\"colC\">$TEXT3</div><div class=\"colD\">$LINK1</div><div class=\"colE\">$ROOMS</div><div class=\"colH\"><p class=\"colHtxt bold\">Gesamtpreis f&#252;r<br />alle Personen</p><p><a href='$LINK2' class=\"dealPrice\" title=\"Preis\">$TEXT4,-&euro;</a></p><p><a href='$LINK3' class=\"zurButton\" title=\"Zur Buchung\">&nbsp;</a></p></div>";
		
		//Setting s.products as per clarification in DEBAU-637
		s.products = productTemp + ",F;" + airportDepCode;
	 }
	 DEAjaxCostTools.calculateCost(bookingId, function(data){
	 
		//Sold out check
		if(data=='3') {
			// For tracking
			s.pageName=getPageName(s.pageName)+"terminnichtverfuegbar";
			s.events = "event9";
			var s_code=s.t();
			if(s_code)document.write(s_code);
			callNext(id,bookingId,IFF,pageId,supplier,hotelInfo,bookingUrl,roomsInfo,urlName,airportName,bookingCode,tourOpCode,strDuration,omniDate,airportDepCode);	
		} else {
			var text = data.split('|');
			//if(text[0]!='ERR') {
			if(typeof text[2] != 'undefined'){
				htmlContent = htmlContent.replace('$TEXT1',text[0]);
				temp = '#ID' + id + ' .regionInfoLink';
				airportName = jQuery(temp).text();
				htmlContent = htmlContent.replace('$TEXT5',airportName);
				temp = '#ID' + id + ' .colB';
				text[1] = jQuery(temp).text();
				htmlContent = htmlContent.replace('$TEXT2',text[1]);
				temp = '#ID' + id + ' .colC';
				var depDate = text[1]
				var durtn = text[2] + " Tage";
				text[2] = jQuery(temp).text();
				text[2].replace(/^\s+|\s+$/g, '');
				//s.eVar6=text[2]; 
				htmlContent = htmlContent.replace('$TEXT3',text[2]);
				temp = '#ID' + id + ' .colE';
				htmlContent = htmlContent.replace('$ROOMS',jQuery(temp).html());
				temp = '#ID' + id + ' .colD';
				htmlContent = htmlContent.replace('$LINK1',jQuery(temp).html());
				htmlContent = htmlContent.replace('$LINK2',url);
				htmlContent = htmlContent.replace('$LINK3',url);
				htmlContent = htmlContent.replace('$TEXT4',text[3]);
				htmlContent = htmlContent.replace('$TEXT4',text[3]);
				if(text[4]!='' && text[5]!=''){
					//s.eVar9=supplier;
					//s.prop9=supplier;
					s.eVar14=text[6];
					//Flight information appended only if journey is not in hotel/wellness/holidayHomes
					if(pageId != 'hotel' && pageId != 'wellness' && pageId != 'holidayHomes') {
						s.products = s.products + '_' + text[4];	
					}
					
				}
				
			} else{
				s.pageName=getPageName(s.pageName)+"terminnichtverfuegbar";
				s.events = "event9";
				var s_code=s.t();
				if(s_code)document.write(s_code);
				htmlContent = "<div class=\"resortSelectedHeader bold\">" + text[1] + "</div><div class=\"resortError bold\" style=\"line-height: 18px;\">" + text[0] + "</div>";
			}
			if(!cancel){			
				jQuery('#RESULT'+id).html(htmlContent);
				jQuery('#ID'+id).hide();
				jQuery('#RESULT'+id).show();
				jQuery('#bjOverlay').hide();
			}
			
			//Calling Omniture tracking code so that it sends the data after the costing call
			var s_code=s.t();
			if(s_code)document.write(s_code);
		}
		
	});			
 }
 
 /** Add flight info to short list*/
 function addFlightInfoToShortList(bookingId,IFFCode){
	try{
		DEShortListTools.addFlightInfoToShortList(bookingId, IFFCode,window.location.href);				
		AjaxSearchResultTools.getPageContent('/search/de/compareDesign1.jsp', function(data) {
				var bookUrl='/search/de/compareDesign1.jsp?bookingId=' + bookingId + '&iff_code='+IFFCode;
				window.open(bookUrl,'name','height=650,width=680,scrollbars=1'); 
		});
	}catch(err){}
}
function checkRefresh(win){
	var url = win.location.href;
	if(url!=null && url.indexOf("flag")==-1){
		win.location.href = url + "&flag="+true
		win.document.reload(true);
	}
}
 /** Add charter flight info to short list*/
 function addCharterFlightInfoToShortList(bookingId){
	DEShortListTools.addCharterFlightInfoToShortList(bookingId,window.location.href);				
	AjaxSearchResultTools.getPageContent('/search/de/compareDesign1.jsp', function(data) {
			 
	});
	var bookUrl='/search/de/compareDesign1.jsp?bookingId=' + bookingId;
    var shortlist = window.open(bookUrl,'name','height=650,width=680,scrollbars=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes');
	return false;
}



//* Gift Coupon Validation */

function giftCouponValidation() {
	jQuery("#error").hide();
	var coupon = document.getElementById('voucher').value;
	DEAjaxVoucherToolValidation.validateCoupon(coupon, function(data) {
		  
		 /**  START  OF CODE MODIFICATION FOR DE – 1687:
		 * Description: check added for null and temp length to avoid alert message(error message)
		 * AUTHOR: Harsh Gandhi
		**/
		if(data != null){
			var temp = new Array();
			temp = data.split('||');
			if(temp.length > 1){
				//document.getElementById('voucherFlag').value=temp[0];
				 document.getElementById('voucherFlag').value=temp[1];	
				 jQuery("#error").html(temp[0]).show();		
			}
		}
		
		/**  END  OF  CODE MODIFICATION FOR DE – 1687:
		**/

	});
 }

 /* Flight Information */

function getFlightInformation(bookingId) {
    var htmlContent="<span class='bdLeft'>$LABEL1 $LABEL2</span><p>$LABEL3 $LABEL6</p><p><strong>$LABEL4</strong></p>";
	DEAjaxCostTools.getBookingFlight(bookingId, function(data) {				
			htmlContent = htmlContent.replace('$LABEL1',label1);
			htmlContent = htmlContent.replace('$LABEL2',label2);
			htmlContent = htmlContent.replace('$LABEL6',label2);
			htmlContent = htmlContent.replace('$LABEL4',label4);
			htmlContent = htmlContent.replace('$LABEL3',label3);
			data = data.replace('$LABEL5',label5);
			htmlContent = htmlContent + data;
			jQuery("#flightInfo").html(htmlContent).show();						
	});
 }

 function addPackageInfo(id,bookingId) {	
	if(previousId != -1) {
		jQuery('#ID'+previousId).show();
		jQuery('#RES'+previousId).hide();
	}
	previousId = id;
	DEAjaxCostTools.calculateCost(bookingId, function(data){
		if((data=='3' || data=='2' || data=='4') && !cancel) {
			window.location.reload(true);			
		}
	});	
 }
 
 
 function redirectPage(pageId,searchType,id,bookingId) {
	 if(bookingId!=null){
	   window.location.href = "/search/de/deAccomodationDetail.jsp?pageId="+pageId+"&search="+searchType+"&IFF="+id+"&booking_id="+bookingId;
	 }
	 else{
	    window.location.href = "/search/de/deAccomodationDetail.jsp?pageId="+pageId+"&search="+searchType+"&IFF="+id;
	 }
}

function getPageName(pageName){
	var pageVars  = pageName.split('_');
	pageName='';
	for(var index=0; index<pageVars.length-1;index++){
		pageName = pageName + pageVars[index] + "_";
	}
	return pageName;
}


function getAutosuggestdata(brandCode, proposition, isQuickSearch, updateFunc) {
	try{
        AjaxSearchTools.rtrveJsonDestRegionString(brandCode, proposition, isQuickSearch, updateFunc);
	}catch(err){
		
	}
}

function updateAutosuggestdata(textId) {
	
	return (function(data){
		if (data != null) {
			
			SEARCH_WIZARD.autoSuggestSearchField.setSource( data);
			
			jQuery(function () {
			    
			    SEARCH_WIZARD.autoSuggestSearchField.initializeAutocomplete(data, textId);
			    
			});
		}
	});
	
}


function onRegionUpdate(regionID) {
   //var region = dwr.util.getValue("resortSelect");
	var proposition = dwr.util.getValue("propositionIDField");
	autoSuggestAjax.ajaxCalls.setRegion(regionID);
	try{
       AjaxSearchTools.getDestDetails(regionID,proposition, autoSuggestAjax.ajaxCalls.updateDestination);
       var destination = dwr.util.getValue("destinationSelect");
	}catch(err){}
}



function updateDestSelect(data) {
	
	if ((data != null) && (data[0] != "-1")){
		 dwr.util.setValue("topDestionation", data[1]);
		 dwr.util.setValue("destinationSelect", data[0]);
	 }
	
}

var autoSuggestAjax = {
        ajaxCalls:  {
		selectedRegion 		: "",
		destinationSelect   : "",

		setRegion 			: function(regionId){
								this.selectedRegion = regionId; 
							},

		setDestination      : function(destinationID){
                                this.destinationSelect = destinationID; 
                            },

	    updateDestination 	: function (data) {
	    	
						    	if ((data != null) && (data[0] != "-1")){
						    		 dwr.util.setValue("topDestionation", data[1]);
						    		 dwr.util.setValue("destinationSelect", data[0]);
						    		 
						    		 autoSuggestAjax.ajaxCalls.setDestination(data[0]);
						    		 autoSuggestAjax.ajaxCalls.destinationChanged();
						    	 }
						    	
						    },
		destinationChanged  : function () {
            
						        var proposition = dwr.util.getValue("propositionIDField");
						        try{
						            AjaxSearchTools.populateRegions(autoSuggestAjax.ajaxCalls.destinationSelect, proposition, autoSuggestAjax.ajaxCalls.updateRegionSelect);
						        }catch(err){}
                                
                            },
        updateRegionSelect  : function (data) {
                                
                                if (data != null){
                                    updateResorts(data);
                                    jQuery("#resortSelect option[value=" + autoSuggestAjax.ajaxCalls.selectedRegion + "]").attr("selected", "selected");
                                    //dwr.util.setValue("resortSelect", autoSuggestAjax.ajaxCalls.regionId);
                                 }
                                
                            }
        }
}


