
var bFoundEngineToReplace=false;

function ss(w)
{
	window.status=w;
	return true;
}

function cs()
{
	window.status='';
}


function sshref(el) 
{		
	var strUrl	=	el.href;	
	ss(strUrl);
}



function LogClick(strToolbarId,strLogURL)
{
	//create an Ajax request to log the clicked url
	var strUrl = "LogClick.aspx?LogType=1&ctid=" + strToolbarId + "&URL=" + escape(strLogURL);
	
	EBAjax.ServerRequest("GET",strUrl,null);
}


function LogADClick(strToolbarId,strLogURL,iADPosition,iSearchTestMode)
{
	//create an Ajax request to log the clicked url
	var strUrl = "LogClick.aspx?LogType=1&ctid=" + strToolbarId + "&URL=" + escape(strLogURL) + "&adpos=" + iADPosition + "&SearchTestMode=" + iSearchTestMode;
	
	EBAjax.ServerRequest("GET",strUrl,null);
}


function LogSearchEngineClick(strToolbarId,strSearchEngineType)
{	
	
	var strSearchTerm = "";
		
	if (window.RegExp && window.encodeURIComponent)
	{
		//get the current search term from the saerch box
		var q	=	document.getElementById('q_top');
		strSearchTerm	=	q.value;		
	}
	
	//create an Ajax request to log the clicked url
	var strUrl = "LogClick.aspx?LogType=2&SearchEngineType="+ strSearchEngineType +"&ctid=" + strToolbarId + "&SearchTerm=" + escape(strSearchTerm);	
	
	EBAjax.ServerRequest("GET",strUrl,null);	
	
	return true;
}

function ServerResponse(xmlDoc){}


//loads the language preferences to the 
//radio buttons on the page
function LoadSearchLanguagePreferences(strSearchWebValue,strToolbarLanguageValue,strWhichRadioChecked)
{
	var radioSearchToolbarLanguage	= document.getElementById('RadioSearchToolbarLanguage');
	var radioSearchAllLanguages		= document.getElementById('RadioSearchAllLanguages');
	
	radioSearchToolbarLanguage.value= strToolbarLanguageValue;
	
	radioSearchAllLanguages.value	= strSearchWebValue;
	
	if (strWhichRadioChecked == '1')
	{
		radioSearchAllLanguages.checked = true; 
	}
	else
	{
		radioSearchToolbarLanguage.checked=true;
	}	
}



function ToggleAdvancedSearchTDDisplay(bShow)
{
	var tdAdvancedSearch	= document.getElementById('AdvancedSearchTD');
	
	if (bShow	==	'0')
	{
		tdAdvancedSearch.style.visibility = 'hidden';		
	}
}


function SetSearchLanguagePreferences(strSeletecedLanguage)
{
	var radioSearchToolbarLanguage		= document.getElementById('RadioSearchToolbarLanguage');

	radioSearchToolbarLanguage.value	= strSeletecedLanguage;

	radioSearchToolbarLanguage.checked	= true;
}


function ChangeQueryTerm(el) 
{
	if (window.RegExp && window.encodeURIComponent)
	{
		var q	=	document.getElementById('q_top');
		var ue	=	el.href;
		var qe	=	encodeURIComponent(q.value);
		
		if(ue.indexOf("q=")!=-1)//most engines case
		{
			var rplcReg=new RegExp("q=[^&$]*");
			el.href=ue.replace(rplcReg,"q="+qe);
		}
		else if(ue.indexOf("KW=")!=-1)//shopping.com case
		{
			var rplcReg=new RegExp("KW=[^&$]*");
			el.href=ue.replace(rplcReg,"KW="+qe);
		}		
		else
		{
				el.href=ue+"&q="+qe;
		}
		
	}
	return 1;
}




function OpenNewWin(actionType,destinationURL)
{		
	winref = window.open(destinationURL,'navwin',
	'left=20,top=20,width=500,height=500,toolbar=1,resizable=1,location=1,status=1,menubar=1,adressbar=1,scrollbars=1');	
	
	winref.focus();	
}




function SearchWeb()
{
	var searchWebUrlLink	=	document.getElementById('searchWebUrl');
	
	if (searchWebUrlLink != null)
	{	
		location.href= searchWebUrlLink;
	}
}




/*
	Changes the top frame size according
	to it's content
*/
var bSetFrameSize = true;

function SetFrameSize()
{	

	if (bSetFrameSize)
	{
		
		
		var AdditionalEnginesTD			 = document.getElementById ("tdAdditionalEngines");	
		
		if (AdditionalEnginesTD != null)
		{	
			var strAdditionalEnginesTDHeight = AdditionalEnginesTD.clientHeight + 2;
				//alert(strAdditionalEnginesTDHeight)
			var objFramesetParentBody = null;

			try
			{
				objFramesetParentBody = parent.frames['top'].parent.document.body;
			}
			catch(errMain) 
			{
			
			}
			
			
				
			if(objFramesetParentBody)
			{				
				strAddRows = strAdditionalEnginesTDHeight + 'px,*';					
						
				objFramesetParentBody.rows = strAddRows;		
			}
		}	
	}	
}


function SetTopFrameSize()
{

	
	var AdditionalEnginesTD			 = document.frames[0].document.getElementById ("tdAdditionalEngines");	
	
		if (AdditionalEnginesTD != null)
		{	
			var strAdditionalEnginesTDHeight = AdditionalEnginesTD.offsetHeight + 2;
				
			var objFramesetParentBody = null;
			
			try
			{
				objFramesetParentBody = parent.frames['top'].parent.document.body;
			}
			catch(errMain) 
			{
			
			}
				
			if(objFramesetParentBody)
			{				
				strAddRows = strAdditionalEnginesTDHeight + 'px,*';					
						
				objFramesetParentBody.rows = strAddRows;		
			}	
		}	

}



function EscapeFrames(strURL)
{
	try
	{
		parent.frames['top'].parent.document.location.href = strURL;	
	}
	catch(errMain) 
	{
	}
}




function IsFrameExist()
{
	var bRes = false;
	
	var objFramesetParentBody	= null;

	try
	{
		objFramesetParentBody = parent.frames['top'].parent.document.body;
	}
	catch(errMain) 
	{
	}
		
	if (objFramesetParentBody != null)
	{
		bRes = true;
	}
	
	return bRes;
}


function FocusFrameSubmit()
{
	var	q	=	document.getElementById('q_top');

	if (q!=null)
	{
		q.focus();
	}
}




//gets all 'span' html object, exept of ">>" and "<<"
function GetSpans(objDivParent)
{
	var arrSpan	= objDivParent.getElementsByTagName('span');
	var arr		= new Array()
	for(var i=0;i<arrSpan.length;i++)
	{
		if(arrSpan[i].id != 'linkMore' && arrSpan[i].id != 'linkLess')
		{
			arr.push(arrSpan[i]);
		}
	}
	return arr;
}

// sets the initial search engines list display,
// according to the window width.
function initializeEnginesDisplay()
{
	var divMoreOffset	= 21;
	var objDivParent	= document.getElementById('divEnginesParent');
	var strCookieState	= EBCookies.GetCookie('AdditionalMenuState')
	
	var objAnchorSpan	= null;
	var objCurrentSpan	= null;
	var iPrev;
	
	var bodyDir				= document.getElementsByTagName('body')[0].dir;
	var bRtl			= (bodyDir.toLowerCase() == "rtl") ? true : false;
	var isHide			= false;
	var totalWidth = objDivParent.offsetWidth;
	var isFirstTime		= true;
	var objInsertBeforeSpan;
	var prevSpanOffset  = 0;
	var spanOffset		= 0;
	if(bRtl)
	{
		objDivParent.dir = "rtl"
	}
	
	var arrSpan			= GetSpans(objDivParent);

	//run over all the span elements (all engines urls)
	for(var i=0;i<arrSpan.length;i++)
	{								
		objCurrentSpan = arrSpan[i];
		
		//we set them all to 'inline' first		
		objCurrentSpan.style.display  = 'inline';	
		
		//set the right direction
		objCurrentSpan.dir = bodyDir;
		
		//calculate if this engine fits to the container
		spanOffset = objCurrentSpan.offsetWidth;
		totalWidth -= spanOffset;
		
		if(totalWidth < 0)
		{
			isHide = true;
			
			objCurrentSpan.style.display = 'none';
			
			//we save the first element to hide
			if(isFirstTime)
			{
				if(totalWidth + spanOffset - divMoreOffset < 0)
				{
					objInsertBeforeSpan = arrSpan[i-1];
					arrSpan[i-1].style.display = 'none';
				}
				else
				{
					objInsertBeforeSpan = objCurrentSpan;
				}	
				isFirstTime			= false;
			}
		}
		prevSpanOffset = spanOffset;
	}
	
	// get the "more>>" url
	var objDivMore		= document.getElementById('divMore');

	if(isHide)
	{
		//insert ">>" before the first element to hide,(will be last one in the row)
		objDivParent.insertBefore(objDivMore,objInsertBeforeSpan);
		
		objDivMore.style.display = 'inline';
			
		bFoundEngineToReplace = true;
	}
	else
	{
		objDivMore.style.display = 'none';
		
		var strCookieState = EBCookies.GetCookie('AdditionalMenuState')		
		
		bFoundEngineToReplace = false;
	}
		
}


//call on onload / resize event
function initAdditionalEnginesSettings()
{
		
	var strCookieState = EBCookies.GetCookie('AdditionalMenuState')		
	
	if(strCookieState == '0')
	{	
		ToggleAdditionalEngines('0');
	}
	else if (strCookieState == '1')
	{
	
		//create the engines list
		initializeEnginesDisplay();
		
		//show additional engines
		ToggleAdditionalEngines('1');
	}
	else
	{	
		initializeEnginesDisplay();
	}
	
	SetFrameSize();
	//bSetFrameSize = false;
}

function ChangeChevronState()
{
	
}

function ToggleAdditionalEngines(show)
{
		var objDivParent	= document.getElementById('divEnginesParent');
		var objtdMainSearchInterface	= document.getElementById('tdMainSearchInterface');
		var objDivLess		= document.getElementById('divLess');
		var objDivMore		= document.getElementById('divMore');	
		var arrSpan			= GetSpans(objDivParent)//objDivParent.getElementsByTagName('span');
		
		if (show == '1')
		{		
		
			if(objtdMainSearchInterface	!= null)
			{
				objDivParent.style.width = objtdMainSearchInterface.clientWidth - 30;
			}
			
			
			for(var i=0;i<arrSpan.length;i++)
			{
				arrSpan[i].style.display  = 'inline';				
			}
			
			if (objDivMore.style.display != 'none')		
			{
				//replace "more>>" with "Less<<"
				objDivMore.style.display='none';
				
				objDivParent.appendChild(objDivLess);
			}
			
			//if we are on 'show' state, and no engine to replace was found,
			//init again ro remove the '<<' if it was exist
			if(!bFoundEngineToReplace)
			{
			//	initializeEnginesDisplay();
			}
							
			
			//remember the state
			EBCookies.SetCookie('AdditionalMenuState','1');	
		}
		else
		{	
			if(objtdMainSearchInterface	!= null)
			{	
				objDivParent.style.width="345";									
			}
			
			initializeEnginesDisplay();	

			EBCookies.SetCookie('AdditionalMenuState','0');								
		}
		
		SetFrameSize();
				
}



function DetectToolbarInstallation(strToolbarID)
{
	
	var bIsToolbarInstalled	= true;
	
	try
	{	
		 bIsToolbarInstalled			=	IsToolbarInstalled(strToolbarID);
	}
	catch(err)
	{
	
	}
		
	if (!bIsToolbarInstalled)
	{
		var mBannerDiv	=	document.getElementById('BannerDiv');
		
		if (mBannerDiv != null)
		{
		
			try
			{			
				mBannerDiv.style.display = 'block';
			}
			catch (err){}
		}
	}

}


/*Weather*/

function CelsiusToFahrenheit(dTemperatureCelsius)
{
	// Convert Celsius to Fahrenheit.

	dTemperatureFahrenheit = (dTemperatureCelsius * 9 / 5) + 32;

	return dTemperatureFahrenheit;
}

 

function FahrenheitToCelsius(dTemperatureFahrenheit)
{
	// Convert Fahrenheit to Celsius.
	dTemperatureCelsius = (dTemperatureFahrenheit - 32) * 5 / 9;

	return dTemperatureCelsius;
}



function MilesToKM(iMiles)
{
		return iMiles/ 1.6;
}



function KMToMiles(iKM)
{
		return iKM * 1.6;
}





function ChangeWeatherUnits(strChangeToUnits)
{
	var tblweather	 = document.getElementById('tblWeather');
	
	var spnArr		= tblweather.getElementsByTagName('span');
	
	var spnObj		= null;
	
	var iResult		= 0;
	
	var iUnitsValue = 0;
	
	var fResult		= 0;
	
	var fUnitsValue = 0;	
	
	
	//run over all span objects to identify
	
	if (strChangeToUnits	==	'c')
	{
		//change all span values
		for(i=0;i<spnArr.length;i++)
		{
			spnObj = spnArr[i];
			
				if (spnObj.innerHTML != 'N/A')
				{
					if (spnObj.getAttribute('name') == 'spnWeatherUnitValue')
					{
						iUnitsValue	=	parseInt(spnObj.innerHTML);
										
						iResult		=	FahrenheitToCelsius(iUnitsValue);
				
						iResult = Math.round(iResult)
						
						spnObj.innerHTML = iResult;
					}
					else if (spnObj.getAttribute('name') == 'spnWeatherUnitSign')
					{
						spnObj.innerHTML = '&#186 C';
					}
					else if (spnObj.getAttribute('name') == 'spnChangeWeatherUnits')
					{
						spnObj.innerHTML = "<A id=\"hrefChangeWeatherUnits\" class=\"AdditionalEnginesSearch\"   href=\"javascript:ChangeWeatherUnits('f')\"><span style=\"letter-spacing:-0.3mm\" >&#186 F</span></A>";
					}
					else if (spnObj.getAttribute('name') == 'spnWindSpeedUnits')
					{
						iUnitsValue			= parseInt(spnObj.innerHTML);
		
						iResult				= KMToMiles(iUnitsValue);
						
						iResult				= Math.round(iResult)
		
						spnObj.innerHTML	=  iResult;										
					}
					else if (spnObj.getAttribute('name') == 'spnWindSpeedUnitsSign')
					{
						spnObj.innerHTML = 'kmh';								
					}	
					else if (spnObj.getAttribute('name') == 'spnVisibilityUnitsValue')
					{				

						fUnitsValue			= parseFloat(spnObj.innerHTML);
						
						fResult				= KMToMiles(fUnitsValue);
						
						spnObj.innerHTML	= EBUtilities.FormatNumber(fResult,2,true,false,false);								
					}	
					else if (spnObj.getAttribute('name') == 'spnVisibilityUnitsSign')
					{
						spnObj.innerHTML = 'kilometers';								
					}						
				}
		}	
					
	} 
	else if (strChangeToUnits	==	'f')		
	{
		//change all span values
		for(i=0;i<spnArr.length;i++)
		{
			spnObj = spnArr[i];
			
			if (spnObj.innerHTML != 'N/A')
			{										
				if (spnObj.getAttribute('name') == 'spnWeatherUnitValue')
				{
					iUnitsValue	=	parseInt(spnObj.innerHTML);
									
					iResult		=	CelsiusToFahrenheit(iUnitsValue);
			
					iResult = Math.round(iResult)
					
					spnObj.innerHTML = iResult;
				}
				else if (spnObj.getAttribute('name') == 'spnWeatherUnitSign')
				{
					spnObj.innerHTML = '&#186 F';
				}				
				else if (spnObj.getAttribute('name') == 'spnChangeWeatherUnits')
				{
					spnObj.innerHTML = "<A id=\"hrefChangeWeatherUnits\" class=\"AdditionalEnginesSearch\"  href=\"javascript:ChangeWeatherUnits('c')\"><span style=\"letter-spacing:-0.3mm\" >&#186 C</span></A>";
				}
				else if (spnObj.getAttribute('name') == 'spnWindSpeedUnits')
				{
					iUnitsValue			= parseInt(spnObj.innerHTML);
	
					iResult				= MilesToKM(iUnitsValue);
					
					iResult				= Math.round(iResult)
	
					spnObj.innerHTML	= iResult;								
				}
				else if (spnObj.getAttribute('name') == 'spnWindSpeedUnitsSign')
				{
					spnObj.innerHTML = 'mph';								
				}	
				else if (spnObj.getAttribute('name') == 'spnVisibilityUnitsValue')
				{								
					fUnitsValue			= parseFloat(spnObj.innerHTML);
	
					fResult				= MilesToKM(fUnitsValue);
					
					spnObj.innerHTML	= EBUtilities.FormatNumber(fResult,2,true,false,false);							
				}	
				else if (spnObj.getAttribute('name') == 'spnVisibilityUnitsSign')
				{
					spnObj.innerHTML = 'miles';								
				}							
			}
		}			
	}		

}


var globalWeatherInitaialInfo = null;

var globalChangeUnitsContent = null;

function toggleWeatherTableContent(iType)
{

	var spnToggleWeatherContent =  document.getElementById("spnToggleWeatherContent");
	
	var hrefChangeUnits			=  	document.getElementById("hrefChangeWeatherUnits");
	
	var spnForcastTitle				=	document.getElementById("spnForcastTitle");
	
	var spnWeatehrTitleChangeUnits	=	document.getElementById("spnWeatehrTitleChangeUnits");
			
	if (iType==1)//show additional locations
	{
			
		var strAdditionalCitiesHTML	=	strASPXAlternativeCitiesHTML;
		
		var divWeather				=	document.getElementById("divWeatherContentInfo");
			
		
		if (globalWeatherInitaialInfo	==	null)
		{
			if (divWeatherContentInfo)
			{
				globalWeatherInitaialInfo	=	divWeatherContentInfo.innerHTML;
			}
		}
		
		if (globalChangeUnitsContent	==	null)
		{
			if (hrefChangeUnits)
			{
				globalChangeUnitsContent	=  hrefChangeUnits.href;	
			}
		}		
		
		divWeather.innerHTML				=	strAdditionalCitiesHTML;	
		
		spnToggleWeatherContent.innerHTML	= '<a class=\"AdvancedSearchLinkCaption\"   href=\"javascript:toggleWeatherTableContent(0);\">Back to forecast</a>';	
				
		if (hrefChangeUnits != null)
		{
			disableAnchor(hrefChangeUnits,true);
		}
		
		spnForcastTitle.style.display='none';
		
		if (spnWeatehrTitleChangeUnits)
		{
			spnWeatehrTitleChangeUnits.style.display='none';
		}
	
	}
	else // show the weather table
	{
		
		spnToggleWeatherContent.innerHTML	= '<a class=\"AdvancedSearchLinkCaption\"  href=\"javascript:toggleWeatherTableContent(1);\">More matching locations...</a>';	
		
		var divWeather						=	document.getElementById("divWeatherContentInfo");	
			
		divWeather.innerHTML				=	globalWeatherInitaialInfo;
		
		if (hrefChangeUnits != null)
		{
			disableAnchor(hrefChangeUnits,false);			
		}
		
		spnForcastTitle.style.display='inline';
		
		if (spnWeatehrTitleChangeUnits)
		{
			spnWeatehrTitleChangeUnits.style.display='inline';
		}
	}
		
}


function disableAnchor(obj, disable){

  if(disable)
  {
    var href = obj.getAttribute("href");
    
    if(href && href != "" && href != null)
    {
       obj.setAttribute('href_bak', href);
    }
   
    obj.removeAttribute('href');
   
    obj.style.color="gray";
    obj.style.fontSize	='12px';
    
  }
  else
  {
      
    obj.setAttribute('href', obj.attributes['href_bak'].nodeValue);
    
    obj.style.color="#0000CC";
  }
}

function CheckHP(strHPText,strHPURL)
{
	if (!hp.isHomePage(strHPURL)) 
	{document.write("<a class=\"AdditionalEnginesSearch\" href=\"#\" onclick=\"style.behavior='url(#default#homepage)';setHomePage('" +strHPURL +"');window.location.href=window.location.href;return false;\">"+strHPText+"</a>");}
}


