﻿
/**********************************************************************************************************
 TMSearchHandler contains that functions which is use in Advertiser.ascx  
 TMSearchHandler handles open window popups,glow compare button etc.
***********************************************************************************************************/

function TMSearchHandler()
{    
    //minimum advertiser for compare
    this._minimumAdvertiserForCompare=1;
    //store selected window reference after click compare button
    this._selAdvArray=new Array();
    // We use this array to save the reference of opened window by advertiser id.
    this._advArray=new Array();
    // In this array we save the reference of same window by index.
    this._selWinArray=new Array();
    // so we are using both of array like a hashtable.
    // at the time of closing the window we check the reference of opened window in both array.
    // ((window)[IndexOfOpenedWindow]==(window)[AdvertiserId])?CloseWindow:DoNothing;
    // this is used to update for sequential index.
    this._currentDateStamp = (new Date()).getMilliseconds();  
    this._counter=0;
    
}

// Inherit TMJsUtility Class
TMSearchHandler.prototype = new TMJsUtility();
// correct the constructor pointer because it points to TMJSUtility once we inherited.
TMSearchHandler.prototype.constructor = TMSearchHandler;

// this will construct a url to redirect to provider with saving search history (optional)
TMSearchHandler.prototype.RedirectToAdvertiser=function(saveCookie_)
{   
    this.EnableDisableValidator()
    if(typeof(Page_ClientValidate) == 'function' && Page_ClientValidate()==false) 
    {    
        return false;
    }
    else
    {
        // save cookie only when user click on compare button.
        return this.BuildURLAndSaveCookie(saveCookie_); 
    }   
}


TMSearchHandler.prototype.EnableDisableValidator = function()
{
	try	
	{
		var triptype = (document.getElementById(this._rdbTripTypeID).checked) ? 1 : 2 ;
		if(triptype==1)
		{
		    ValidatorEnable(document.getElementById(this._reqCompareDateValID),false) ;	
		}
		else
		{
		    ValidatorEnable(document.getElementById(this._reqCompareDateValID),true) ;
		}
	}
	catch(err){}
    return false;
} 
//*******************************************************************
//               Function Definitions
//*******************************************************************
 
//Function call from TMRotator,PartialPostBackHandler
TMSearchHandler.prototype.CheckUnCheck = function(advertiserId_,ischecked_)
{       
    try
    {    
        var advertiser = document.getElementById(advertiserId_);
      
        if(this.RedirectToAdvertiser(false)==false) 
        {
          //if no search details exists then do unchecked the checkbox.  
           advertiser.checked=false;  
           return false; 
        }
       
       var advertiserId=advertiser.value.split('-')[0];
       //check uncheck when user clicks on checkbox.
       if(ischecked_==true)
        {
            advertiser.checked=(advertiser.checked)?false:true;
        }
        if(advertiser.checked)
        {
             advertiser.checked=false;
              var win=this._advArray[advertiserId_];
              if(this._selWinArray!=null && win!=null)
              {
                  for(var i=0,length=this._selWinArray.length;i<length;i++)
                  { 
                     if(this._selWinArray[i]==this._advArray[advertiserId_])
                     {
                        this._advArray[advertiserId_].close();
                        this._selWinArray.splice(i, 1);
                        this._counter--;  
                     }
                  }	
              }	
        }
        else
        {
             advertiser.checked=true;
             var url =this._searchInitiatorUrl+'&adtype=1&addetails='+advertiser.value+'&rdr=0';
             var win = window.open(url,'advertiser_'+advertiserId+this._currentDateStamp,'target=_blank,location=1,status=1,scrollbars=1,resizable=1,toolbar=1,menubar=yes,titlebar=yes,width=50, height=50, left=50, top=50'); 
             
             if(win == null || typeof(win)== "undefined" )
             {
               this.ShowProcessingRequest();
             }  
             else
             { win.blur();}
             
             this._selWinArray[this._counter]=this._advArray[advertiserId_]=win;
             this._counter++;	   
        }
    
        this.GlowCompareButton(); 

    }
    catch(ex){alert(ex.message);}
}

//use to highlight compare button if no of advertiser is greater than minimumAdvertiserForCompare
TMSearchHandler.prototype.GlowCompareButton = function()
{ 
    var imgBtnCompare =jQuery('#'+this._glowButtonClientId);  
  
     if( this.NoOfSelectedAdvertisers() >= this._minimumAdvertiserForCompare )
      {  
      
       jQuery('#'+this._glowButtonClientId)
       .attr('src',"Portals/0/Images/compare.gif")
       .attr('title',"Compare fare")
       .css('cursor','hand')
       .end();           
     }        
    else
    {       
         jQuery('#'+this._glowButtonClientId)
         .attr('src',"Portals/0/Images/compare_dim.gif")
         .attr('title',"Select atleast three sites to compare")
         .css('cursor',"hand")
             .end();       
   }
}
  


//Function call from Advertiser.ascx
// Connect with advertiser upon user's selection.
TMSearchHandler.prototype.ConnectWithAdvertiser = function()
{ 

    if(this.RedirectToAdvertiser(true)==false) 
    {
        return false;
    }


    if( this.NoOfSelectedAdvertisers() < this._minimumAdvertiserForCompare)
    {  
        alert("Please select atleast "+this._minimumAdvertiserForCompare+" logos."); 
    }
    else
    {
        return this.OpenPopUpWindow(); 
    }
    return false;       
}  
     
TMSearchHandler.prototype.NoOfSelectedAdvertisers = function()
{ 
  return (this._selWinArray!=null)?this._selWinArray.length:0;
}


    
 // open multiple windows
TMSearchHandler.prototype.OpenPopUpWindow = function()
{  
    var windowWidth=screen.width;
    var windowHeight=screen.height;   
    var totalPopups=this.CountPopUps();
    for(var i=0,length=this._selAdvArray.length;i<length;i++)
    { 
      try
        {   
        //for side by side layout 
        var width,height,left,top; 
        if(totalPopups == 1 ||totalPopups == 2 ||totalPopups == 3)
        {
             width=Math.floor(windowWidth/totalPopups); 
             height=windowHeight;
             left=Math.floor((windowWidth/totalPopups)*i);
             top=0; 
       }   
       else if(totalPopups == 4 ) //For grid layout
       {
         width=Math.floor(windowWidth/2);
         height=Math.floor(windowHeight/2)-50;        
         switch(i)
         {
           case 0: 
                left=0;
                top=0;
                break;
           case 1: 
                left=width;
                top=0;
                break;
           case 2: 
                left=0;
                top=height;
                break;
           case 3: 
                left=width;
                top=height;
                break;               
          }          
       }
       else
       {
         width=windowWidth-i*20;
         height= windowHeight-i*20;
         left=i*20;
         top=i*20;
      }
          
        //set window-url and size & position of windows
        if(this._selAdvArray[i]!=null)
        {    
            var selectedWindow=this._selAdvArray[i].window;
            var redirectURL=selectedWindow.location.href;   
            redirectURL=redirectURL.replace("rdr=0","rdr=1")+this.queryStringForSearchDetails;  
            selectedWindow.location.href=redirectURL; 
            selectedWindow.resizeTo(width,height);
            selectedWindow.moveBy(left,top);
            selectedWindow.window.focus(); 
        } 
     }
        catch(ex) {continue;  }  
    } 
  window.location.href=window.location.href.replace("#","");   
}

//Count window popups
TMSearchHandler.prototype.CountPopUps = function()
{ 
    var index=0;
    if(this._selWinArray!=null)
    {
       for(var i=0,length=this._selWinArray.length;i<length;i++)
        { 
          try
          { 
               if(this._selWinArray[i] !=null) 
               { 
                  var selWinObj= this._selWinArray[i].window;
                  if(selWinObj!=null && !selWinObj.closed)
                  {
                      this._selAdvArray[index]=this._selWinArray[i]; 
                      index++;
                  }                
               }            
           }
           catch(ex){continue;}              
        } 
    }
  return index;   
}

//For GeoTargetbased advetisers
TMSearchHandler.prototype.ResponseHandler = function(responseText_)
{
    if (responseText_!=null && responseText_.length>1)
    {
         jQuery('#'+this._GeoBasedAdvertiserContainer).html(responseText_).slideDown(1500);
    }
    else
    {
        jQuery('#'+this._GeoBasedAdvertiserContainer).slideUp(1500).html('');
    }
     this.GlowCompareButton(); 
}
 /*******************************************************************/
    // Function is define in SearchAir,SearchHotel,SearchCar 
/*******************************************************************/

var hiddenDDL = new Array();
TMSearchHandler.prototype.ShowProcessingRequest = function()
{
   var popUpDiv=document.getElementById("divPopup");
   var backGroundDiv=document.getElementById("screen");
    
    backGroundDiv.className='screen';
    backGroundDiv.style.width=Math.max(document.documentElement.scrollWidth, document.body.scrollWidth) + 'px';
    backGroundDiv.style.height=Math.max(document.documentElement.scrollHeight, document.body.scrollHeight) + 'px';
    backGroundDiv.style.zIndex=10000;
    
    popUpDiv.style.display='block';
    popUpDiv.style.left = 100;
    popUpDiv.style.top = 200+document.body.scrollTop;
    popUpDiv.style.zIndex=backGroundDiv.style.zIndex+1;
     
    //IE6 Bug with SELECT element always showing up on top
    if ((Sys.Browser.agent === Sys.Browser.InternetExplorer) && (Sys.Browser.version < 7)) 
    { 
        var selectElements = document.getElementsByTagName('SELECT');
        for (var k = 0 ; k < selectElements.length; k++) 
        {
            if(selectElements[k].style.visibility != "hidden")
	        {
		        hiddenDDL[hiddenDDL.length] = selectElements[k];
                selectElements[k].style.visibility = "hidden";
            }
        }
     }
}

var runnerAdWtrMrk = "Select Runner Advertiser";
TMSearchHandler.prototype.ShowRemainingAdvertsier = function()
{   
    document.onclick=check;
    var divTopRunnerAdvertsier=document.getElementById('dvTopRunnerAdvertiser');
    var divRemainingRunnerAdvertsier = document.getElementById('dvRemainingRunnerAdvertsier');  
    divRemainingRunnerAdvertsier.style.top= absYFrmTp(divTopRunnerAdvertsier) + divTopRunnerAdvertsier.clientHeight +"px";
    divRemainingRunnerAdvertsier.style.left= absXFrmLft(divTopRunnerAdvertsier);
    divRemainingRunnerAdvertsier.style.display="";   
}
TMSearchHandler.prototype.absXFrmLft = function(el)
{
    var absX = 0;
    var actElm = el;
    while(actElm)
    {
	    absX += actElm.offsetLeft;
	    actElm = actElm.offsetParent;
    }
    return absX;
}

TMSearchHandler.prototype.absYFrmTp = function(el)
{
    var absY = 0;
    var actElm = el;
    
    while(actElm)
    {
	    absY += actElm.offsetTop;
	    actElm = actElm.offsetParent;
	    
    }
  
    return absY;
}


TMSearchHandler.prototype.GetSelectedRunnerAdvertsier = function(chkBoxID)
{
    var checkBox = document.getElementById(chkBoxID);
    var labelValue= chkBoxID.split('_')[0];
    var txtRunnerAdvertsierList = document.getElementById('dvTopRunnerAdvertiser');
   
    if(checkBox.checked)  
    {       
        if(txtRunnerAdvertsierList.innerHTML.indexOf(runnerAdWtrMrk) > -1)
        {
            txtRunnerAdvertsierList.innerHTML ="";
        }
        else 
        {
            txtRunnerAdvertsierList.innerHTML+= ", ";
        }
        txtRunnerAdvertsierList.innerHTML += labelValue ;         
    }
    else
    {          
        if(txtRunnerAdvertsierList.innerHTML.indexOf(labelValue)>-1)
        {
            var nameToRemove = ", "  + labelValue ;
            txtRunnerAdvertsierList.innerHTML = txtRunnerAdvertsierList.innerHTML.replace(nameToRemove,"");
        }   
        if(txtRunnerAdvertsierList.innerHTML.indexOf(labelValue)>-1)
        {
            txtRunnerAdvertsierList.innerHTML = txtRunnerAdvertsierList.innerHTML.replace(labelValue,"");
        }
        if (txtRunnerAdvertsierList.innerHTML == "")
        {
            txtRunnerAdvertsierList.innerHTML = runnerAdWtrMrk;
        }
    }
}

var closeDDL=false;
function check(e)
{
    var target = (e && e.target) || (event && event.srcElement);
    var obj = document.getElementById('dvRemainingRunnerAdvertsier');

    if(closeDDL)
        checkParent(target)?obj.style.display='none':null;
    
    if(document.getElementById('dvRemainingRunnerAdvertsier') != null &&
       document.getElementById('dvRemainingRunnerAdvertsier').style.display=="")
    {
        closeDDL = true;
    }
}

function checkParent(t)
{
    while(t.parentNode)
    {
        if(t==document.getElementById('dvRemainingRunnerAdvertsier'))
        {
            return false
        }
        t=t.parentNode
    }
    closeDDL = false;
    return true
} 

