function setCookie(c_name,value,expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie=c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString() + "; path=/");
}

function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) { 
			c_start = c_start + c_name.length + 1; 
			c_end = document.cookie.indexOf(";",c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		} 
	}
	return "";
}

function addEngine(xml,icon,provider,cat) {
	var ua = navigator.userAgent.toLowerCase();
	if(ua.indexOf('msie 7.0') != -1) {
		// IE 7
		if(window.external && ("AddSearchProvider" in window.external)) {
			try {
				window.external.AddSearchProvider(xml);
				setTimeout('postSearchProviderInstallation()', 1000);
			} catch(x) {
				if(70==(x.number&0xFFFF)) {
					alert("For security reasons, you must use the mouse\n(or the Enter key) to click the Install button.");
				} else {
					alert("Unable to add search provider. [" + (x.number & 0xFFFF) + "]");
				}
				return false;
			}
		}
	} else if(ua.indexOf('firefox/') != -1) {
		// Firefox 2
		try {
			if((typeof window.sidebar=="object")&&(typeof window.sidebar.addSearchEngine=="function"))
			{
				window.sidebar.addSearchEngine(xml,icon,provider,cat);
				setTimeout('postSearchProviderInstallation()', 1000);
			}
		} catch (x) {
			if(70==(x.number&0xFFFF)) {
					alert("For security reasons, you must use the mouse\n(or the Enter key) to click the Install button.");
				} else {
					alert("Unable to add search provider. [" + (x.number & 0xFFFF) + "]");
				}
				return false;
		}
	} else {
		//No search engine support (IE 6, Opera, etc).
	}
}

function postSearchProviderInstallation()
{	
	if(getCookie("psp")=="noThanks") {
		// Installation is coming from the condensed panel
		$("#icePage_preferredSearchProvider_ClosedPanel").slideUp("0.20", function(){
			setCookie("psp", "installed", 365);
		});
	} else {
		// Installation is coming from the opened panel
		$("#icePage_preferredSearchProvider_OpenedPanel").slideUp("0.20", function(){
			setCookie("psp", "installed", 365);
		});
	}
}

function checkSearchProviderInstallationStatus()
{
	var ua = navigator.userAgent.toLowerCase();
	var installed = 0;
	try{
		provider = location.protocol + "//" + location.host;
		installed = window.external.IsSearchProviderInstalled(provider);
		//alert("installed: " + window.external.IsSearchProviderInstalled(provider))
		// For Firefox, the IsSearchProviderInstalled method doesn't work; so we need to check the cookie
		if(getCookie("psp") == "installed") {
			installed = 1;
		}
		
		if( installed > 0 ) {
			var openedDiv = document.getElementById("icePage_preferredSearchProvider_OpenedPanel");
			var closedDiv = document.getElementById("icePage_preferredSearchProvider_ClosedPanel");
			
			if (openedDiv != null && openedDiv.style.display != "none") {
				openedDiv.style.display = "none";
				return false;
			}
			else if (closedDiv) {
				if(closedDiv.style.display != "none") {
					closedDiv.style.display = "none";
					return false;
				}
			}
		}
	} catch(error) {}
}

function logClick(url) {
	$.get(url);
}

function pspNoThanks(level) {
	if(level == "noThanks") {
		$("#icePage_preferredSearchProvider_OpenedPanel").slideUp("0.20", function(){
			$("#icePage_preferredSearchProvider_ClosedPanel").slideDown("0.20", function(){
				setCookie("psp", "noThanks", 365);
			});
		});
		return false;
	}
	else if (level == "never") {
		$("#icePage_preferredSearchProvider_ClosedPanel").slideUp("0.20", function(){
			setCookie("psp", "never", 365);
		});
		return false;
	}
}

function ToggleVerticalTab (qcat, verticalLinkId )
{
    var qcattop = document.getElementById("icePage_SearchBoxTop_qcat");
    var qcatbottom = document.getElementById("icePage_SearchBoxBottom_qcat");

    if (qcattop)
    {
        qcattop.value = qcat;
    }
    if (qcatbottom)
    {
        qcatbottom.value = qcat;
    }

    var tabs = document.getElementsByTagName('a');
    for( var i=0; i<tabs.length; i++ ) {
        var tabi = tabs[i];

        if ( tabi.id.indexOf( qcat ) != -1)
        {
            if (tabi.className == "VerticalTabInactive" ) {
                tabi.className="VerticalTabActive";
            }
        } else if ( tabi.className=="VerticalTabActive" ) {
            tabi.className="VerticalTabInactive";
        }
    }
   
    if(qcat=="News" || qcat=="Video")
    {
      hideAdvSearch("true");
        }
    else {
        hideAdvSearch("false");
        }

    // TODO: Implement logging
    //var img = new Image();
    //img.src = "http://msxml.infospace.com/_1_2SYEUMY04WO5R6M__core.ws.56/search/5_6/logging/category.htm?category=" + searchType + "&ran=" + Math.round(Math.random()*10000);
    
    var resultsFromTop = document.getElementById("icePage_SearchBoxTop_ResultsFromChoices_ResultsFrom");
    var resultsFromBottom = document.getElementById("icePage_SearchBoxBottom_ResultsFromChoices_ResultsFrom");
    
    if(resultsFromTop)
    {
        if(qcat == "Web")
        {
            resultsFromTop.style.visibility = "visible";
        }
        else
        {
            resultsFromTop.style.visibility = "hidden";
        }
    }
    
    if(resultsFromBottom)
    {
        if(qcat == "Web")
        {
             resultsFromBottom.style.visibility = "visible";
        }
        else
        {
            resultsFromBottom.style.visibility = "hidden";
        }
    }
}

function toggleAdvSearch(advSearchLnk) 
{
    var advSearch = document.getElementById("icePage_advancedSearch_AdvSearchWrapper");
    var advSearchFlag = document.getElementById("icePage_advancedSearch_advsearch");
    var advSearchLnk = document.getElementById(advSearchLnk);
   
    if(null!=advSearch && null!=advSearchFlag && null!=advSearchLnk)
    {   
        if(advSearch.style.display == 'none')
        {
            advSearchFlag.value = "true";
            advSearch.style.display="block";
            advSearchLnk.className="advOptionLnkBold";
            disableMainSearch('true');
        }
        else
        {
            advSearchFlag.value="false"; 
            advSearch.style.display="none";
            advSearchLnk.className="advOptionLnk"; 
            disableMainSearch("false");
            
        }
    } 
}
function hideAdvSearch(flag){

var advSearch = document.getElementById("icePage_advancedSearch_AdvSearchWrapper");
var advSearchLnk = document.getElementById('icePage_SearchBoxTop_AdvSearchLinkRight_AdvLnkWrapper_AdvOptionLnk');
var advSearchLnkBtm = document.getElementById('icePage_SearchBoxTop_AdvSearchLinkBottom_AdvLnkWrapper_AdvOptionLnk');

var advSearchLnkBtmSptr = document.getElementById('icePage_SearchBoxTop_VerticalLinkSeperator2');


var advSearchFlag = document.getElementById("icePage_advancedSearch_advsearch");

if(flag=="true"){
        if(advSearchLnk)advSearchLnk.style.visibility="hidden";
        if(advSearch)advSearch.style.visibility="hidden";
        if(advSearchLnkBtm){ advSearchLnkBtm.style.display="none";
        if(advSearchLnkBtmSptr)advSearchLnkBtmSptr.style.display="none";
        }
        
    }
 else if(flag=="false"){
        
            if(advSearchLnk) 
            {
                advSearchLnk.style.visibility="visible";
                advSearchLnk.style.display="inline";    
            }
            //if(advSearch)advSearch.style.display="block";
            if(advSearchLnkBtm){ 
                advSearchLnkBtm.style.display="block";
                if(advSearchLnkBtmSptr)advSearchLnkBtmSptr.style.display="block";
            }

        if( advSearch && advSearchFlag && advSearchFlag.value =="true") { 
               advSearch.style.visibility="visible";
               advSearchLnk.style.visibility="visible"; 
               if(advSearchLnkBtm){ 
                  advSearchLnkBtm.style.display="block";
                  if(advSearchLnkBtmSptr)advSearchLnkBtmSptr.style.display="block";
               }
           }
 }   
    
    

}

function updateQAll() {
var qallObj = getAdvParam("qAll");
var qPhraseObj = getAdvParam("qPhrase"); 
var qAnyObj = getAdvParam("qAny"); 
var qNotObj = getAdvParam("qNot"); 

if(qallObj)qall=qallObj.value; 
if(qPhraseObj)qPhrase=qPhraseObj.value;
if(qAnyObj)qAny=qAnyObj.value;
if(qNotObj)qNot=qNotObj.value;

var domainText = document.getElementById("icePage_advancedSearch_AdvancedSearchWeb_domaint");
var topQkw =document.getElementById("icePage_SearchBoxTop_qkw").value;

if(qallObj && qPhraseObj && qAnyObj && qAnyObj){
        if(null!=topQkw)qallObj.value = topQkw;
            qPhraseObj.value="";
            qAnyObj.value="";
            qNotObj.value="";
   }
   
  if (null != document.getElementById("icePage_SearchResults_SearchBoxMarketing_mqkw"))
  { 
    document.getElementById("icePage_SearchResults_SearchBoxMarketing_mqkw").value = "";
  }
   
 if( null!=domainText)domainText.value="";

}

function validateAdSearchEntry() {


var qall = getAdvParam("qAll");
var qPhrase = getAdvParam("qPhrase"); 
var qAny = getAdvParam("qAny"); 

if(null==qall && null==qPhrase && null ==qAny) return;

if((trimAll(qall.value)=="") && (trimAll(qPhrase.value)=="") && (trimAll(qAny.value) == "")){
                alert("You must also enter a word(s) in one or more of the following fields:\n- All of these words\n- The exact phrase\n- Any of these words\n");
                qall.focus();
                return false;
}

    return true;
}

function ZipSearch()
{
    document.getElementById('icePage_SearchBoxTop_qkw').value = document.getElementById('icePage_SearchResults_IntelliFindContainer_IFMovieResult_IFMovieLocationHidden').value + ' ' + document.getElementById('icePage_SearchResults_IntelliFindContainer_IFMovieResult_IFMovieLocationTextBox').value;                            
}

function ZipSearchBottom()
{
    document.getElementById('icePage_SearchBoxTop_qkw').value = document.getElementById('icePage_SearchResults_IntelliFindContainerBottom_IFMovieResult_IFMovieLocationHidden').value + ' ' + document.getElementById('icePage_SearchResults_IntelliFindContainerBottom_IFMovieResult_IFMovieLocationTextBox').value;                            
}

function HideRecentSearches()
{
    var srches = document.getElementById("icePage_SearchLinks_RecentSearches_RecentSearchRepeaterContainer");
	if (srches)
	{
	    srches.style.display = 'none';
	}
    var hideBtn = document.getElementById("icePage_SearchLinks_RecentSearches_HideButton");
	if (hideBtn)
	{
	    hideBtn.style.display = 'none';
	}
    var viewBtn = document.getElementById("icePage_SearchLinks_RecentSearches_ViewButton");
	if (viewBtn)
	{
	    viewBtn.style.display = 'inline';
	}
    document.cookie = 'wsViewRecent=0; path=/';
}

function ViewRecentSearches()
{
    var srches = document.getElementById("icePage_SearchLinks_RecentSearches_RecentSearchRepeaterContainer");
	if (srches)
	{
	    srches.style.display = '';
	}
    var viewBtn = document.getElementById("icePage_SearchLinks_RecentSearches_ViewButton");
	if (viewBtn)
	{
	    viewBtn.style.display = 'none';
	}
    var hideBtn = document.getElementById("icePage_SearchLinks_RecentSearches_HideButton");
	if (hideBtn)
	{
	    hideBtn.style.display = '';
	}
    document.cookie = 'wsViewRecent=1; path=/';
    
}

function SetRecentSearchesView()
{
    // All we need to look for is the cookie being set
    // to 0 (hide).  In any other case we assume 1 (view).
    if ((document.cookie.length > 0) && (document.cookie.indexOf("wsViewRecent=0") != -1))
    {
        HideRecentSearches();
    }
    else
    {
        ViewRecentSearches();
    }
    var nosrches = document.getElementById("icePage_SearchLinks_RecentSearches_NoRecentSearches");
	if (nosrches)
	{
	    nosrches.style.display = 'none';
	}
}

function ClearRecentSearches()
{
    document.cookie = 'wsRecent=; path=/';
     document.cookie = 'wsViewRecent=; path=/';

    var btns = document.getElementById("icePage_SearchLinks_RecentSearches_RecentSearchesButtons");
	if (btns)
	{
	    btns.style.display = 'none';
	}
    var srches = document.getElementById("icePage_SearchLinks_RecentSearches_RecentSearchRepeaterContainer");
	if (srches)
	{
	    srches.style.display = 'none';
	}
    var nosrches = document.getElementById("icePage_SearchLinks_RecentSearches_NoRecentSearches");
	if (nosrches)
	{
	    nosrches.style.display ='block';
	}
}
function trimAll( strValue ) {

    var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function disableMainSearch(flag){

var qkwTop = document.getElementById("icePage_SearchBoxTop_qkw");
var qkwBottom = document.getElementById("icePage_SearchBoxBottom_qkw");


var qkwSubmitTop = document.getElementById("icePage_SearchBoxTop_qkwsubmit");
var qkwSubmitBottom = document.getElementById("icePage_SearchBoxBottom_qkwsubmit");

var qcattop = document.getElementById("icePage_SearchBoxTop_qcat").value;

if(qcattop=="News" || qcattop=="Video") return; 

    if(flag=="true") 
    {   
        if(qkwTop){
         qkwTop.disabled = true  ;
         qkwTop.style.backgroundColor="#cfcfbf";
        }
        
        if(qkwBottom){
         qkwBottom.disabled = true  ;
         qkwBottom.style.backgroundColor="#cfcfbf";
        }
        
        
        if(qkwSubmitTop) {
        qkwSubmitTop.disabled =true;
        qkwSubmitTop.src = qkwSubmitTop.src.replace("goFetchBtn.gif","goFetchBtnGrayed.gif");
        }
        if(qkwSubmitBottom) {
        qkwSubmitBottom.disabled =true;
        qkwSubmitBottom.src = qkwSubmitBottom.src.replace("goFetchBtn.gif","goFetchBtnGrayed.gif");
        }
    }
    else if(flag=="false"){
        
        if(qkwTop)
        {
        qkwTop.disabled = false  ;
        qkwTop.style.backgroundColor="White";
        }
        
         if(qkwBottom){
            qkwBottom.disabled = false  ;
            qkwBottom.style.backgroundColor="White";
        }
         
       if(qkwSubmitTop) {
                qkwSubmitTop.disabled =false;
                qkwSubmitTop.src = qkwSubmitTop.src.replace("goFetchBtnGrayed.gif","goFetchBtn.gif");
                }
        if(qkwSubmitBottom) {
                qkwSubmitBottom.disabled =false;
                qkwSubmitBottom.src = qkwSubmitBottom.src.replace("goFetchBtnGrayed.gif","goFetchBtn.gif");
        }
    }
}


function getAdvParam(name) { 

var qcattop = document.getElementById("icePage_SearchBoxTop_qcat").value;
if(qcattop=="Images")qcattop="Image";
if(qcattop.toLowerCase()=="dns")qcattop="Web";

var param = document.getElementById("icePage_advancedSearch_AdvancedSearch"+qcattop+"_AdvancedSearchCommon_"+name);

return param;
}
function getAdvQuery(){

var qall = getAdvParam("qAll");
var qPhrase = getAdvParam("qPhrase"); 
var qAny = getAdvParam("qAny"); 
var qNot = getAdvParam("qNot");

if(qall)qall=qall.value; 
if(qPhrase)qPhrase=qPhrase.value;
if(qAny)qAny=qAny.value;
if(qNot)qNot=qNot.value;

var advQuery = "&padv=1";

if(qall && trimAll(qall)!="" )advQuery +="&qall="+encodeURIComponent(qall);
if(qPhrase && trimAll(qPhrase)!="" )advQuery += "&qphrase="+encodeURIComponent(qPhrase);
if(qAny && trimAll(qAny)!="" )advQuery +="&qany="+encodeURIComponent(qAny);
if(qNot && trimAll(qNot)!="")advQuery +="&qnot="+encodeURIComponent(qNot);

return advQuery;

}

function Navigate_AdvSearch(url){

    var qcattop = document.getElementById("icePage_SearchBoxTop_qcat").value;
    var topQkw =document.getElementById('icePage_SearchBoxTop_qkw').value


    if(qcattop)url=url.replace("{qcat}",qcattop);
    
    if(topQkw)
        url=url.replace("{qkw}",topQkw);
    else
        url=url.replace("&qkw={qkw}",topQkw);

  
    window.open(url,"_self","menubar=1,resizable=1,width=350,height=250");

}

function QuoteSearch() {
    if (document.getElementById('icePage_SearchResults_IntelliFindContainer_IFStock_quoteSearchText').value != "Enter company symbol") {
        document.getElementById('icePage_SearchBoxTop_qkw').value = document.getElementById('icePage_SearchResults_IntelliFindContainer_IFStock_quoteSearchText').value;
    }
}