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);
}

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";
        }
        else
        {
            advSearchFlag.value="false"; 
            advSearch.style.display="none";
            advSearchLnk.className="advOptionLnk"; 
            
        }
    } 
}

function hideAdvSearch(flag)
{
    //var advSearch = document.getElementById("icePage_advancedSearch_AdvSearchWrapper");
    var advSearch = document.getElementById("icePage_SearchBoxTop_AdvancedSearchLinkWrapper");
    var advSearchLnk = document.getElementById('icePage_SearchBoxTop_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(advSearchLnk)advSearchLnk.style.display="none";
        if(advSearch)advSearch.style.display="none";
        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="block";    
        }
        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";
           advSearch.style.display = "block";
           
           //advSearchLnk.style.visibility="visible"; 
           advSearchLnk.style.display="block";    
           
           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!=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 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 getAdvParam(name) { 

var qcattop = document.getElementById("icePage_SearchBoxTop_qcat").value;
if(qcattop=="Images")qcattop="Image";

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;

}

var WS = {};
// This function is used to attach an event to an object on the page dynamically
WS.registerEvent = function(elementId, eventName, eventMethod) {
    var obj = typeof elementId == 'string' ? document.getElementById(elementId) : elementId;
    if (obj) {
        try {
            if (obj.addEventListener) {
                if (eventName.substr(0,2) == 'on')
                    eventName = eventName.substr(2);
                obj.addEventListener(eventName, eventMethod, false);
            }
            else if (obj.attachEvent)
                obj.attachEvent(eventName, eventMethod);    
        }
        catch (ex) {
            // do nothing, event is not registered
        }
    }
}